I2S example code

Post your cool example code here.
User avatar
RogerClark
Posts: 6375
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: I2S example code

Post by RogerClark » Wed Jan 20, 2016 10:06 pm

Hardware CS doesn't work correctly on the STM32F103.

Well, it doesnt work in a way that is normally any use. So we don't use it.

You'll have to write code to set and reset the CS line as required.

BTW. If you don't have more than one device on the SPI, can't you just leave CS set all the time.

The STM32 has 2 SPI channels, so if you have more than one SPI device, you could put one on SPI1 and one on SPI2 and set the CS on both all the time

madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: I2S example code

Post by madias » Fri Jan 22, 2016 3:54 pm

STM32F103 are really bad devices for I2S, caused by the "stripped down" clock divider in the f103. I think I have linked to this problem here in the forum. So you won't get an odd 44.1khz or 48khz (48 is even better)

victor_pv
Posts: 1472
Joined: Mon Apr 27, 2015 12:12 pm

Re: I2S example code

Post by victor_pv » Tue Jan 03, 2017 6:00 am

Madias, did you ever turn the i2s code into a library?
I am getting back to work on the stm32 and just ordered some pt8211.
I want to clean up the wav player code and turn it to use one of those.

User avatar
GrumpyOldPizza
Posts: 174
Joined: Fri Apr 15, 2016 4:15 pm
Location: Denver, CO

Re: I2S example code

Post by GrumpyOldPizza » Tue Jan 03, 2017 2:51 pm

victor_pv wrote:Madias, did you ever turn the i2s code into a library?
I am getting back to work on the stm32 and just ordered some pt8211.
I want to clean up the wav player code and turn it to use one of those.
ArduinoZero (MKRZERO) added a I2S class now that is also supported by a ArduinoSound libary:

https://github.com/arduino/ArduinoCore- ... /src/I2S.h

https://www.arduino.cc/en/Reference/I2S
https://www.arduino.cc/en/Reference/ArduinoSound

It might make sense to not reinvent the wheel there and use at least the I2S interface as a base. I discarded my own homegrown interface and added this I2S class to the STM32L4 code. There was only one modification needed to support the MCK output. This STM32 SPI/I2S peripheral would work great with that class interface. N.b. that this "I2S" class is not a kitchensink approach, but the minimum support to get most hardware on the other end of the I2S interface to work.

victor_pv
Posts: 1472
Joined: Mon Apr 27, 2015 12:12 pm

Re: I2S example code

Post by victor_pv » Tue Jan 03, 2017 2:57 pm

GrumpyOldPizza wrote:
victor_pv wrote:Madias, did you ever turn the i2s code into a library?
I am getting back to work on the stm32 and just ordered some pt8211.
I want to clean up the wav player code and turn it to use one of those.
ArduinoZero (MKRZERO) added a I2S class now that is also supported by a ArduinoSound libary:

https://github.com/arduino/ArduinoCore- ... /src/I2S.h

https://www.arduino.cc/en/Reference/I2S
https://www.arduino.cc/en/Reference/ArduinoSound

It might make sense to not reinvent the wheel there and use at least the I2S interface as a base. I discarded my own homegrown interface and added this I2S class to the STM32L4 code. There was only one modification needed to support the MCK output. This STM32 SPI/I2S peripheral would work great with that class interface. N.b. that this "I2S" class is not a kitchensink approach, but the minimum support to get most hardware on the other end of the I2S interface to work.
Makes sense, it would allow for libraries using it to be ported with the least effort, and for both F1 and L4. Would you mind posting the link to where your i2s files are too?
If Madias hasn't ported it yet, I will try using the bits he shared and following that class interface.

User avatar
GrumpyOldPizza
Posts: 174
Joined: Fri Apr 15, 2016 4:15 pm
Location: Denver, CO

Re: I2S example code

Post by GrumpyOldPizza » Tue Jan 03, 2017 3:11 pm

victor_pv wrote:
GrumpyOldPizza wrote:
victor_pv wrote:Madias, did you ever turn the i2s code into a library?
I am getting back to work on the stm32 and just ordered some pt8211.
I want to clean up the wav player code and turn it to use one of those.
ArduinoZero (MKRZERO) added a I2S class now that is also supported by a ArduinoSound libary:

https://github.com/arduino/ArduinoCore- ... /src/I2S.h

https://www.arduino.cc/en/Reference/I2S
https://www.arduino.cc/en/Reference/ArduinoSound

It might make sense to not reinvent the wheel there and use at least the I2S interface as a base. I discarded my own homegrown interface and added this I2S class to the STM32L4 code. There was only one modification needed to support the MCK output. This STM32 SPI/I2S peripheral would work great with that class interface. N.b. that this "I2S" class is not a kitchensink approach, but the minimum support to get most hardware on the other end of the I2S interface to work.
Makes sense, it would allow for libraries using it to be ported with the least effort, and for both F1 and L4. Would you mind posting the link to where your i2s files are too?
If Madias hasn't ported it yet, I will try using the bits he shared and following that class interface.
STM32L4 has a 2 layer implementation, one at the core side, and then layered below on the system side:

https://github.com/GrumpyOldPizza/ardui ... /src/I2S.h
https://github.com/GrumpyOldPizza/ardui ... rc/I2S.cpp


The class interface is rather straight forward. In essence you just need some DMA based receive/transmit backing it, with a callback for when a transmit/receive is done. The only non-obvious thing in the original interface is that the first receive/transmit bind the direction of the I2S interface ...

The internal system layer implementation is a tad more involved for STM32L4, as different voltage ranges and different PLLSAI1/PLLSAI2 usages for STM32L476 and STM32L432/STM32L433 make it somewhat interesting.

victor_pv
Posts: 1472
Joined: Mon Apr 27, 2015 12:12 pm

Re: I2S example code

Post by victor_pv » Sun Jan 08, 2017 7:03 pm

GrumpyOldPizza wrote:
STM32L4 has a 2 layer implementation, one at the core side, and then layered below on the system side:

https://github.com/GrumpyOldPizza/ardui ... /src/I2S.h
https://github.com/GrumpyOldPizza/ardui ... rc/I2S.cpp


The class interface is rather straight forward. In essence you just need some DMA based receive/transmit backing it, with a callback for when a transmit/receive is done. The only non-obvious thing in the original interface is that the first receive/transmit bind the direction of the I2S interface ...

The internal system layer implementation is a tad more involved for STM32L4, as different voltage ranges and different PLLSAI1/PLLSAI2 usages for STM32L476 and STM32L432/STM32L433 make it somewhat interesting.
Thanks, I have been having a look at both the Arduino and your implementation. Are you using the STM drivers for the lowest level? or you wrote your own?
I'll see if this coming week I have some time to read how the i2s peripheral works in the F1 and start writing.
I'll have to ask you some things about the DMA callback functions, but I will when I get to start working on that.

On a side note, I think a similar way of managing the callback should work for the SPI DMA transfers in the F1 core, currently we block instead of using callbacks.

User avatar
GrumpyOldPizza
Posts: 174
Joined: Fri Apr 15, 2016 4:15 pm
Location: Denver, CO

Re: I2S example code

Post by GrumpyOldPizza » Tue Jan 10, 2017 12:17 am

victor_pv wrote: Thanks, I have been having a look at both the Arduino and your implementation. Are you using the STM drivers for the lowest level? or you wrote your own?
I'll see if this coming week I have some time to read how the i2s peripheral works in the F1 and start writing.
I'll have to ask you some things about the DMA callback functions, but I will when I get to start working on that.

On a side note, I think a similar way of managing the callback should work for the SPI DMA transfers in the F1 core, currently we block instead of using callbacks.
Own driver layer. See https://github.com/GrumpyOldPizza/ardui ... 4xx/Source

The STM32L4 core does use DMA callbacks like this for SPI as well. Actually exposed via the SPI class interface:

Code: Select all

bool SPIClass::transfer(const void *txBuffer, void *rxBuffer, size_t count, void(*callback)(void));
Difference is that there is a per call callback, so you can thread throu a sequence of callbacks. For I2S you really just need a "refill" callback.

madias
Posts: 813
Joined: Mon Apr 27, 2015 11:26 am
Location: Vienna, Austria

Re: I2S example code

Post by madias » Thu Jan 19, 2017 5:36 pm

Dear Victor, glad to see you back :)
...but I'm really out of business, cause of my second newborn daughter.
As I can remember I didn't turned the i2s code into a library. All I can remember there was a conversation between us ended up in not using RTOS, but with your DMA examples, that worked flawlessly. I do not know the I2S-zero library, but I guess it's not really necessary, because getting I2S DMA working is only a few lines of code, so I think it's better working with "our" code.

victor_pv
Posts: 1472
Joined: Mon Apr 27, 2015 12:12 pm

Re: I2S example code

Post by victor_pv » Thu Jan 19, 2017 5:56 pm

madias wrote:Dear Victor, glad to see you back :)
...but I'm really out of business, cause of my second newborn daughter.
As I can remember I didn't turned the i2s code into a library. All I can remember there was a conversation between us ended up in not using RTOS, but with your DMA examples, that worked flawlessly. I do not know the I2S-zero library, but I guess it's not really necessary, because getting I2S DMA working is only a few lines of code, so I think it's better working with "our" code.
Congratulations on your newborn!
There are things more important in life than programming for a hobby, a baby is definitely one of them :)

Thanks for replying, I really hoped you had already prepared the library so I didn't have to.
Oh well, I have ordered some of those PT8211 DACs, once I get them I'll try to turn our code into a library, wish me good luck ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest