Simvik

Fix: the LED pin never changes

GPIO register writes are ignored because the port's clock was never switched on. An easy embedded C problem (Working with vendor SDKs and HALs), run and…

easy · Working with vendor SDKs and HALs · find-the-bug · gpio · clocks · registers

Bug report. On the new board the status LED on pin PA5 never lights. A scope shows the pin floating. The register code has been reviewed and the bit positions are right, and the vendor's example project that blinks the same pin works.

The difference: the vendor's generated init code does one thing before touching the GPIO registers. Fix led_init by changing one line. Other peripheral clocks may already be on, and must stay on.

Starter code

void led_init(void) {

  gpio_write(&GPIOA.MODER, (GPIOA.MODER & ~(3u << 10)) | (1u << 10));   // PA5 as output
}

void led_set(bool on) {
  if (on) gpio_write(&GPIOA.ODR, GPIOA.ODR | (1u << 5));
  else gpio_write(&GPIOA.ODR, GPIOA.ODR & ~(1u << 5));
}

Example tests

Initialise and switch on

led_init();
led_set(true);
show();

Expected output:

mode=1 out=1 clocks=0x1

Learn it first

More Working with vendor SDKs and HALs problems