Here we'll look at using the SysTick Timer to make the LEDs blink. The SysTick is a really basic timer found in ARM Cortex M0+ processors from all manufacturers.
The SysTick will run an interrupt service routine that updates a global variable. That variable is used in the main loop to toggle the LEDs on and off (both Red and Green).
This requires a modification to the pin_mux.c file to enable PTE19 and PTD5 for the two LEDs:
void BOARD_InitPins(void)
{
/* ------------------ Port E for Red LED ---------------*/
/* Port E Clock Gate Control: Clock enabled */
CLOCK_EnableClock(kCLOCK_PortE);
gpio_pin_config_t gpioe_pin19_config = {
.pinDirection = kGPIO_DigitalOutput,
.outputLogic = 0U
};
/* Initialize GPIO functionality on pin PTE31 (pin 19) */
GPIO_PinInit(GPIOE, 31U, &gpioe_pin19_config);
/* PORTE31 (pin 19) is configured as PTE31 */
PORT_SetPinMux(PORTE, 31U, kPORT_MuxAsGpio);
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
/* ------------- Port D for Green LED ------------- */
/* Port D Clock Gate Control: Clock enabled */
CLOCK_EnableClock(kCLOCK_PortD);
gpio_pin_config_t gpiod_pin5_config = {
.pinDirection = kGPIO_DigitalOutput,
.outputLogic = 0U
};
/* Initialize GPIO functionality on pin PTD5 (pin 5) */
GPIO_PinInit(GPIOD, 5U, &gpiod_pin5_config);
/* PORTD5 (pin 5) is configured as PTD5 */
PORT_SetPinMux(PORTD, 5U, kPORT_MuxAsGpio);
/* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */
}
Then, the main code looks like this:
#include "pin_mux.h"
#include "board.h"
/*******************************************************************************
* Definitions
******************************************************************************/
#define BOARD_LED2_GPIO BOARD_LED_GREEN_GPIO
#define BOARD_LED2_GPIO_PIN BOARD_LED_GREEN_GPIO_PIN
#define BOARD_LED_GPIO BOARD_LED_RED_GPIO
#define BOARD_LED_GPIO_PIN BOARD_LED_RED_GPIO_PIN
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
volatile uint32_t g_systickCounter;
/*******************************************************************************
* Code
******************************************************************************/
void SysTick_Handler(void)
{
if (g_systickCounter != 0U)
{
g_systickCounter--;
}
}
void SysTick_DelayTicks(uint32_t n)
{
g_systickCounter = n;
while (g_systickCounter != 0U)
{
}
}
/*!
* @brief Main function
*/
int main(void)
{
/* Board pin init */
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitDebugConsole();
/* Set systick reload value to generate 1ms interrupt */
if (SysTick_Config(SystemCoreClock / 1000U))
{
while (1)
{
}
}
while (1)
{
/* Delay 1000 ms */
SysTick_DelayTicks(1000U);
GPIO_PortToggle(BOARD_LED_GPIO, 1u << BOARD_LED_GPIO_PIN);
GPIO_PortToggle(BOARD_LED2_GPIO, 1u << BOARD_LED2_GPIO_PIN);
}
}

James Andrew Smith is a Professional Engineer and Associate Professor in the Electrical Engineering and Computer Science Department of York University’s Lassonde School, with degrees in Electrical and Mechanical Engineering from the University of Alberta and McGill University. Previously a program director in biomedical engineering, his research background spans robotics, locomotion, human birth, music and engineering education. While on sabbatical in 2018-19 with his wife and kids he lived in Strasbourg, France and he taught at the INSA Strasbourg and Hochschule Karlsruhe and wrote about his personal and professional perspectives. James is a proponent of using social media to advocate for justice, equity, diversity and inclusion as well as evidence-based applications of research in the public sphere. You can find him on Twitter. You can find him on BlueSky. Originally from Québec City, he now lives in Toronto, Canada.
