tensorflow-micropython-examples icon indicating copy to clipboard operation
tensorflow-micropython-examples copied to clipboard

stm32: Fix I2S peripheral is not working on H7

Open mocleiri opened this issue 4 years ago • 3 comments

First you need to use specific SPI pins when connecting the microphone. Also there is a switch needed to be enabled in the mpconfigboard.h file.

// turn on I2S
#define MICROPY_HW_I2S1         (1)

There is also a MICROPY_HW_I2S2 switch. I'm not sure if the nucleo h743 supports more i2s channels or not.

../../lib/stm32lib/STM32H7xx_HAL_Driver/Inc/stm32h7xx_hal_i2s.h:229:42: error: overflow in conversion from 'long unsigned int' to 'int8_t' {aka 'signed char'} changes value from '512' to '0' [-Werror=overflow]
  229 | #define I2S_DATAFORMAT_32B               (SPI_I2SCFGR_DATLEN_1)
      |                                          ^
machine_i2s.c:282:16: note: in expansion of macro 'I2S_DATAFORMAT_32B'
  282 |         return I2S_DATAFORMAT_32B;
      |                ^~~~~~~~~~~~~~~~~~
machine_i2s.c: In function 'i2s_init':
machine_i2s.c:583:9: warning: implicit declaration of function '__HAL_RCC_PLLI2S_ENABLE' [-Wimplicit-function-declaration]
  583 |         __HAL_RCC_PLLI2S_ENABLE();  // start I2S clock
      |         ^~~~~~~~~~~~~~~~~~~~~~~
machine_i2s.c: In function 'machine_i2s_init_helper':
machine_i2s.c:793:9: error: 'I2S_InitTypeDef' has no member named 'ClockSource'
  793 |     init->ClockSource = I2S_CLOCK_PLL;
      |         ^~
machine_i2s.c:793:25: error: 'I2S_CLOCK_PLL' undeclared (first use in this function); did you mean 'IS_RCC_PLL'?
  793 |     init->ClockSource = I2S_CLOCK_PLL;
      |                         ^~~~~~~~~~~~~
      |                         IS_RCC_PLL
machine_i2s.c:793:25: note: each undeclared identifier is reported only once for each function it appears in
cc1: all warnings being treated as errors

mocleiri avatar Jan 04 '22 01:01 mocleiri

I was able to build after updating the H7 HAL files.

The stm32lib repo is a submodule at micropython/lib/stm32lib

I cloned the upstream STM32CubeH7 repo:

 git clone https://github.com/STMicroelectronics/STM32CubeH7.git

Then ran the update script:

$ cd micropython/lib/stm32lib
$ ./fetch_from_upstream.sh STM32H7 ~/git/STM32CubeH7/

Then the stm32 build worked to build the firmware.

I'm going to have to file a pull request to update these files and in the interim change the micropython submodule back to my fork which can use the forked and fixed stm32lib repo.

mocleiri avatar Jan 04 '22 04:01 mocleiri

I also have to remove the defines to constants defined in the new HAL in micropython-modules/microlite/micropython.mk:

-CXXFLAGS_USERMOD += -D__FPU_PRESENT=1
+# CXXFLAGS_USERMOD += -D__FPU_PRESENT=1 

-CXXFLAGS_USERMOD += -D__ARM_FEATURE_DSP=1
+# CXXFLAGS_USERMOD += -D__ARM_FEATURE_DSP=1 

mocleiri avatar Jan 04 '22 04:01 mocleiri

I did something wrong and the build didn't actually work. On closer inspection there are differences in the name of the HAL functions that are used in machine_i2s.c from the STM32F4 and the HAL available on the STM32H7.

I need to bring in some board awareness to machine_i2s.c and then see if I can figure out which methods to use on the H7 HAL.

mocleiri avatar Jan 07 '22 00:01 mocleiri