Porting to STM32-based custom board

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
mvdw
Posts: 25
Joined: Tue May 26, 2015 11:57 pm
Location: Brisbane
Contact:

Porting to STM32-based custom board

Post by mvdw » Wed May 06, 2020 7:33 am

I am trying to port micropython to my own board which has an STM32F767 on board. The issue I'm having is that my board does not have an LSE oscillator on board, only a 24MHz HSE.

Micropython by default uses the LSE 32kHz oscillator to generate the baud rates, but this is not necessary in the general case when configuring the device. Is there a way to force micropython to generate the uart clock from the HSE source? When I say "Is there a way" I of course mean "is there an easy way"...

mvdw
Posts: 25
Joined: Tue May 26, 2015 11:57 pm
Location: Brisbane
Contact:

Re: Porting to STM32-based custom board

Post by mvdw » Wed May 06, 2020 7:45 am

So it turns out that even though I don't have the 32kHz clock I still need to set the LSE_VALUE in my board, otherwise the compiler complains.

I have successfully compiled to a REPL on my serial port!!! Happy days!

Now I should be able to develop the rest of the app much faster.

mvdw
Posts: 25
Joined: Tue May 26, 2015 11:57 pm
Location: Brisbane
Contact:

Re: Porting to STM32-based custom board

Post by mvdw » Wed May 06, 2020 7:58 am

My board has an SD Card, on the SDMMC2 port. Micropython fails to build with the defines for the pins as:

Code: Select all

#define MICROPY_HW_SDMMC2_CK                (pin_D6)
#define MICROPY_HW_SDMMC2_CMD               (pin_D7)
#define MICROPY_HW_SDMMC2_D0                (pin_G9)
#define MICROPY_HW_SDMMC2_D1                (pin_G10)
#define MICROPY_HW_SDMMC2_D2                (pin_G11)
#define MICROPY_HW_SDMMC2_D3                (pin_G12)
#define MICROPY_HW_SDCARD_DETECT_PIN        (pin_D5)
#define MICROPY_HW_SDCARD_DETECT_PULL       (GPIO_PULLUP)
#define MICROPY_HW_SDCARD_DETECT_PRESENT    (GPIO_PIN_RESET)
Specifically, the failure is at the pin_G10 line (and I suspect also the pin_G11 and pin_G12). STM32Cube IDE allows these as valid pins, but my build gives the following error:

Code: Select all

boards/UC_BASEBOARD/mpconfigboard.h:94:46: error: 'pin_G10' undeclared (first use in this function); did you mean 'pin_A10'?
Is this an issue because I copied the pins.csv file directly from the NUCLEO_F767ZI folder, and didn't edit it? Is there a guide to writing the pins.csv file anywhere?

I am very excited by my progress so far just in one day.

mvdw
Posts: 25
Joined: Tue May 26, 2015 11:57 pm
Location: Brisbane
Contact:

Re: Porting to STM32-based custom board

Post by mvdw » Wed May 06, 2020 9:08 am

I've now edited my pins.csv file, and it seems like the board is working (I can toggle my LEDs, and I have a REPL).

So, super progress in less than a day!

Anyway, I also had to edit leds.c as my board has 6 LEDs rather than the maximum of 4 allowed by the original source. It was a very easy fix!

Happy to submit a patch if it's wanted.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Porting to STM32-based custom board

Post by jimmo » Thu May 07, 2020 3:40 am

Hi,

That's fantastic! Sorry I didn't get to your messages in time, but looks like you got it all going.

I guess no harm in sending a patch if it's not too much work!

mvdw
Posts: 25
Joined: Tue May 26, 2015 11:57 pm
Location: Brisbane
Contact:

Re: Porting to STM32-based custom board

Post by mvdw » Fri May 08, 2020 7:11 pm

Hi Jimmo:

Sorry but I don't know where to submit the patch directly, so here is the patch in unified diff format:

Code: Select all

 ports/stm32/led.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/ports/stm32/led.c b/ports/stm32/led.c
index 923171884..e0ca89a55 100644
--- a/ports/stm32/led.c
+++ b/ports/stm32/led.c
@@ -58,6 +58,12 @@ STATIC const pyb_led_obj_t pyb_led_obj[] = {
     {{&pyb_led_type}, 3, MICROPY_HW_LED3},
     #if defined(MICROPY_HW_LED4)
     {{&pyb_led_type}, 4, MICROPY_HW_LED4},
+    #if defined(MICROPY_HW_LED5)
+    {{&pyb_led_type}, 5, MICROPY_HW_LED5},
+    #if defined(MICROPY_HW_LED6)
+    {{&pyb_led_type}, 6, MICROPY_HW_LED6},
+    #endif
+    #endif
     #endif
     #endif
     #endif
@@ -77,7 +83,9 @@ void led_init(void) {
 #if defined(MICROPY_HW_LED1_PWM) \
     || defined(MICROPY_HW_LED2_PWM) \
     || defined(MICROPY_HW_LED3_PWM) \
-    || defined(MICROPY_HW_LED4_PWM)
+    || defined(MICROPY_HW_LED4_PWM) \
+    || defined(MICROPY_HW_LED5_PWM) \
+    || defined(MICROPY_HW_LED6_PWM) 
 
 // The following is semi-generic code to control LEDs using PWM.
 // It currently supports TIM1, TIM2 and TIM3, channels 1-4.
@@ -98,6 +106,12 @@ void led_init(void) {
 #ifndef MICROPY_HW_LED4_PWM
 #define MICROPY_HW_LED4_PWM { NULL, 0, 0, 0 }
 #endif
+#ifndef MICROPY_HW_LED5_PWM
+#define MICROPY_HW_LED5_PWM { NULL, 0, 0, 0 }
+#endif
+#ifndef MICROPY_HW_LED6_PWM
+#define MICROPY_HW_LED6_PWM { NULL, 0, 0, 0 }
+#endif
 
 #define LED_PWM_TIM_PERIOD (10000) // TIM runs at 1MHz and fires every 10ms
 
As you can see I just extended the LED defines through to 6 LEDs.

Cheers,
Matt.

Post Reply