More on STM32Cube and the Standard Peripheral Library

This evening I decided to have a go at flipping the LED on my STM32 board using STM32CubeMX to generate a TrueSTUDIO project. Within the project I decided to use the SysTick timer to implement the “flipping” code. After appreciating that the SysTick is initialized for 1ms period and that the Pin where the LED is connected is correctly set to Output by the auto-generated code, and its port is correctly enabled, I had to figure out where to put the flipping code.

It turns out that the HAL documentation is pretty much auto-generated from code comments so after some digging I came to this function:

/**
  * @brief  SYSTICK callback.
  * @retval None
  */
__weak void HAL_SYSTICK_Callback(void)
{
  /* NOTE : This function should not be modified, when the callback is needed,
            the HAL_SYSTICK_Callback could be implemented in the user file
   */
}

So I decided to implement it in my user source as per below:

/**
  * @brief  SYSTICK callback implementation.
  * @param  None
  * @retval None
  */
void HAL_SYSTICK_Callback(void)
{
  static uint32_t led_timer;

  if (++led_timer >= 500) {
    led_timer = 0;
    /* Toggle LED state */
    HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
  }
}

Result? No LED flipping.

OK, I have no motivation to troubleshoot this at the moment, but with everything auto-generated for me for initialization and configuration purposes, the chances I forgot something in the above implementation are quite slim.

Fine, I moved on to the Standard Peripheral Library, which apparently enjoys a better reputation in the STM32 user community. I tried the SysTick code example for TrueSTUDIO and it did not compile at all: 22 errors. Example:

selected processor does not support `ldrex r0,[r0]' in Thumb mode

Once I added the -mcpu=cortex-m3 compiler option, I was left with just two compiler errors, one of which was:

registers may not be the same --strexb r0,r0,[r1]'

At this point Google came to the rescue. I edited core_cm3.c within the CMSIS library as per this Gist:

-   __ASM volatile ("strexb %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
+   __ASM volatile ("strexb %0, %2, [%1]" : "=&r" (result) : "r" (addr), "r" (value) );
...
-   __ASM volatile ("strexh %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
+   __ASM volatile ("strexh %0, %2, [%1]" : "=&r" (result) : "r" (addr), "r" (value) );

After that, the project compiled fine for the MCU I use. After flashing the binary, I ended up with a flipping LED 🙂

I certainly had to write more code and fix compilation/configuration issues with the Standard Peripheral Library, but that might be related to the fact the TrueSTUDIO template project provided therein is not up-to-date and the build toolchain in TrueSTUDIO was certainly updated since STM developers last tested the SysTick example. With the free version of TrueSTUDIO one cannot cherry pick the toolchain version either… Hm, I wonder whether that’s on purpose…

Based on these two experiences I would say that I am more inclined on using the Standard Peripheral Library route but I would definitely want to give the STM32CubeMX -> TrueStudio+STM32Cube another go.

Many years ago I was already past this stage with my Atmel SAM7S256 ARM  MCU and their framework. I was at some point using DMA in conjunction with SPI in order to drive an LCD, as per image below:

ARM board and new display by Luigi Di Fraia

ARM board and new display

It feels like going back a life time when you have to evaluate a framework you are not familiar with and bugs might be lurking anywhere waiting for you to hit them 😦

This entry was posted in Retrocomputing, Technical and tagged , , , , , , , . Bookmark the permalink.

One Response to More on STM32Cube and the Standard Peripheral Library

  1. Pingback: Bonus content for STM32F10x standard peripheral library added to GitHub | Luigi Di Fraia's e-Footsteps

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s