PGBoard Tutorial: blinking LED on STM32

RosarIOT
Posts: 1
Joined: Thu 08. Aug 2019 17:59:25

PGBoard Tutorial: blinking LED on STM32

Post by RosarIOT » Tue 10. Sep 2019 13:32:53

My goal is to create NB-IoT application, and I recently started to play with STM32 CubeMX, Nucleo-64 boards and firmware development. I will need for my application a basic User Interface based on buttons and LEDS, and I choose PGBoard because it has NB-IoT embedded module, which I will use in the future, 2 LEDS, one button, everything I need.

This simple tutorial shows how to light a LED on PGBoard. Since PGBoard is based on STM32 Discovery board, we have great examples in blogs and youtube. Here is a very good tutorial for blinking LED on Nucleo-64:

https://www.youtube.com/watch?v=FAv0LO5 ... ta04-30Z6u

Let's start

If you are using MacOS and you don't know how to get started, you can get useful info on this previous tutorial (viewtopic.php?f=65&t=17&p=17&hilit=maco ... cubemx#p17)
IMG_20190910_142759.jpg
IMG_20190910_142759.jpg (64.74 KiB) Viewed 807 times

The code!

PGBoard has 2 LEDs, one red and one green, and to simply start just open your project with SystemWorkbench, and edit your main.c file (nbiotevk/Src/main.c(

Code: Select all

…
int main(void)
{
  /* Configuration and Initialize for PGBoard */
  …
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE BEGIN 3 */
		NB_Handler();
		CmdLineHandler();
		BUTTON_Handler();
		NMEA_Handler();
		HALLSENSE_Handler();
		
		// add my code here to main loop
		LED_SetState(LED_GREEN, OxFF);
		HAL_Delay(1000);
		LED_SetState(LED_GREEN, 0x0);
		HAL_Delay(1000);

  }
  /* USER CODE END 3 */
}
…

The Green LED will blink, 1000ms ON and 1000ms off.

That's it, job done!



Wait, why don't we simply use HAL_GPIO_WritePin?

PGBoard comes with a useful function LED_SetState which gets a parameter to set sequence for every 100ms time slot:

Code: Select all

# nbiotevk/Drivers/BSP/led.c
...
# define SETLED(led,val) HAL_GPIO_WritePin(LED[led].GPIO_Port, LED[led].GPIO_Pin, ~val)
...
void LED_Handler(void){
...
	{
		SETLED(i, LED[i].State&1);
		LED[i].State >>= 1;
	}
}
...
This means that the LED state will take the value of each bit, sequentially changing every 100ms. Very smart and elegant solution!


Examples

With LED_SetState we can easily create UI for LED. Here is an example on how to get a quick double blink:

Code: Select all

// BLINK TWICE QUICKLY
// hex value 0x50
// binary value 1 0 1 0 0 0 0 0
LED_SetState(LED_GREEN, 0x50);
double_blink.gif
double_blink.gif (234.9 KiB) Viewed 836 times
It is possible to put any sequence, here are a few examples you can use:

Code: Select all

// LED ON
// hex value 0xFF
// binary value 1 1 1 1 1 1 1 1
LED_SetState(LED_GREEN, 0xFF);


// LED OFF
// hex value 0x0
// binary value 0 0 0 0 0 0 0 0


// BLINK TWICE
// hex value 0x33
// binary value 1 1 0 0 1 1 0 0
LED_SetState(LED_GREEN, 0x33);


// BLINK THREE TIMES QUICKLY
// hex value 0xA8
// binary value 1 0 1 0 1 0 0 0
LED_SetState(LED_GREEN, 0xA8);



Ok that's it for today, my goal for next article is to be able to pilot these LEDs through NB-IoT, keep tuned!