// STM32 Nucleo-L031K6 HAL Blink Example using SysTick Timer
// The LED (PB3) blinks every 1 second using HAL and SysTick-based interrupts
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "stm32l0xx_hal.h"
// ============================================
// Define the LED port and pin (PB3 = user LED)
// ============================================
#define LED_PORT GPIOB
#define LED_PIN GPIO_PIN_3
#define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
// ===========================
// Function Prototypes
// ===========================
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void HAL_SYSTICK_Callback(void);
// ===========================
// Main Program Entry
// ===========================
int main(void)
{
// 1. Initialize the HAL Library (sets up SysTick timer and basic config)
HAL_Init();
// 2. Configure system clock (to 32 MHz using PLL)
SystemClock_Config();
// 3. Initialize GPIO pin for LED
MX_GPIO_Init();
// 4. Main loop does nothing – blinking is done in interrupt
while (1) { }
return 0;
}
// ==================================================
// SysTick Interrupt Callback (called every 1 ms)
// Toggles the LED every 1000 ms (1 second)
// ==================================================
void HAL_SYSTICK_Callback(void)
{
static uint32_t counter = 0;
counter++;
if (counter >= 1000) // 1000 ms = 1 second
{
HAL_GPIO_TogglePin(LED_PORT, LED_PIN); // Toggle LED
counter = 0; // Reset counter
}
}
// ==================================================
// GPIO Initialization Function
// Configures PB3 as digital output
// ==================================================
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable clock to GPIOB peripheral
__HAL_RCC_GPIOB_CLK_ENABLE();
// Configure PB3 as push-pull output, no pull-up/down
GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
// Initialize the GPIO with the settings
HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);
}
// ==================================================
// System Clock Configuration Function
// Uses HSI (16 MHz) -> PLL x4 / 2 = 32 MHz SYSCLK
// ==================================================
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
// Enable power interface clock
__HAL_RCC_PWR_CLK_ENABLE();
// Set voltage scaling for high-speed operation
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
// Configure oscillator: use internal HSI (High-Speed Internal) oscillator
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
// Enable PLL: HSI x4 / 2 = 32 MHz
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV2;
// Apply oscillator config
HAL_RCC_OscConfig(&RCC_OscInitStruct);
// Configure clock dividers and system source
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
// Apply clock config with 1 wait state for Flash memory
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);
}
Loading
st-nucleo-l031k6
st-nucleo-l031k6