I wanted to turn on an LED on a STM32 devkit.
To do so I needed to turn some GPIO off and on.
It turns out, this isn't so straightforward.
I did not want to tie myself to a particular framework, so I decided to target the CMSIS headers which are available everywhere (I think?), even surprisingly under Arduino.
That means going to the hardware registers directly though.
Between being sidetracked by paying work and doing the research and code for this I've been at this all day.
I've got a class that almost lets me set the various properties of a GPIO pin and turn it off and on.
bool cmsis::gpio_pin_t::output() const {
const int shl = 1<<index;
uint32_t odr = READ_REG(gpio_ports[(size_t)port].reg_base->ODR);
return !!(odr & shl);
}
void cmsis::gpio_pin_t::output_type(pin_output_type_t value) {
const unsigned shl = 1<<index;
MODIFY_REG(gpio_ports[(size_t)port].reg_base->OTYPER, shl, (shl * uint32_t(value)));
}
pin_output_type_t cmsis::gpio_pin_t::output_type() const {
const int shl = 1<<index;
return (pin_output_type_t)(READ_BIT(gpio_ports[(size_t)port].reg_base->OTYPER, shl) >> POSITION_VAL(shl));
}
void cmsis::gpio_pin_t::speed(pin_speed value) {
const unsigned shl = 1<<index;
MODIFY_REG(gpio_ports[(size_t)port].reg_base->OSPEEDR, (GPIO_OSPEEDER_OSPEEDR0 << (POSITION_VAL(shl) * 2U)),
(uint32_t(value) << (POSITION_VAL(shl) * 2U)));
}
pin_speed_t cmsis::gpio_pin_t::speed() const {
const int shl = 1<<index;
return (pin_speed_t)(READ_BIT(gpio_ports[(size_t)port].reg_base->OSPEEDR,
(GPIO_OSPEEDER_OSPEEDR0 << (POSITION_VAL(shl) * 2U))) >> (POSITION_VAL(shl) * 2U));
}
gpio_port_t cmsis::gpio_ports[] = {
{(GPIO_TypeDef *)GPIOA_BASE},
{(GPIO_TypeDef *)GPIOB_BASE}
#if defined GPIOC_BASE
,{(GPIO_TypeDef *)GPIOC_BASE}
#endif
#if defined GPIOD_BASE
,{(GPIO_TypeDef *)GPIOD_BASE}
In arduino it's just stuff like
pinMode(pin,OUTPUT);
digitalWrite(pin,HIGH);
There's a whole mess of behind the scenes there. And I ran face first into it.
I'm several hundred lines of C++ in and I've got it almost working. Just need to enable the clock for the pin group even though I'm not entirely sure what that is, just how to do it.
Here goes everything.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx