The next struggle I faced was getting certain things to work while poking at the hardware directly using the low-level registers.
I studied the comprehensive datasheet and reference manual thinking I had a good handle on everything. While GPIO seemed to work just as expected, I had issues with getting I2C working properly, and also could not get flash working either.
I had avoided the Hardware Abstraction Layers (HAL Drivers) thus far, fearing the bloated flexibility would eat up my precious memory and not leave enough for what I wanted.
After many days of struggle, I tried a few HAL examples, and was surprised that they worked just as intended, so I ended up copying the HAL sample projects and retrofitting my code into the example's shell.
Bottom line is that using the HAL saves a bunch of time and frustration, and is also more intuitive to program, e.g.
The low-level way to set up GPIO
GPIOA->MODER = (GPIOA->MODER & ~((uint32_t)0b11<<(1<<1)));
GPIOA->PUPDR = (GPIOA->PUPDR & ~((uint32_t)0b11<<(1<<1)))
| ((uint32_t)0b01<<(1<<1));
The HAL way (more verbose, but much nicer)
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
The HAL helpers and defines are much easier to work with and make your code far more readable so you can ignore the complexities of the hardware quirks and focus on the more meaningful magic of your project.
For the curious, you can step through the HAL code in the debugger and see what it's doing at the hardware level. I was surprised to see that it didn't always follow the datasheet 1:1 which some unexplainable bit setting here and there. At least I don't need to worry too much about it now.
Another thing I found out far too late - Since this chip is based on an ARM Cortex M0+ (possibly not licensed though), you can search from problems using "STM32" or "Core M0+" when you get stuck on things and the solutions may actually work, or at least get you closer to the answer.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.