#include <stdio.h> // Standard C I/O library (printf, etc.)
#include <stdint.h> // Standard integer types (uint8_t, uint32_t, etc.)
#include <string.h> // Standard string handling functions
#include <stm32c0xx_hal.h> // STM32C0 HAL (Hardware Abstraction Layer) main header
// ST Nucleo Green user LED (PA5)
#define LED_PORT GPIOA // GPIO port where the LED is connected
#define LED_PIN GPIO_PIN_5 // Pin number for the LED (PA5)
#define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOA_CLK_ENABLE // Macro to enable GPIOA clock
UART_HandleTypeDef huart2; // UART handle structure for USART2
void SystemClock_Config(void); // Prototype for system clock configuration function
static void MX_USART2_UART_Init(void); // Prototype for USART2 initialization function
void osSystickHandler(void)
{
// This function is intended to be called periodically from SysTick
// HAL_GetTick() returns the system tick count in milliseconds
// 1 Hz blinking:
if ((HAL_GetTick() % 500) == 0) // Check every 500 ms
{
HAL_GPIO_TogglePin(LED_PORT, LED_PIN); // Toggle LED state
}
}
void initGPIO()
{
GPIO_InitTypeDef GPIO_Config; // GPIO configuration structure
GPIO_Config.Mode = GPIO_MODE_OUTPUT_PP; // Configure pin as push-pull output
GPIO_Config.Pull = GPIO_NOPULL; // No internal pull-up or pull-down resistor
GPIO_Config.Speed = GPIO_SPEED_FREQ_HIGH; // High-speed GPIO output
GPIO_Config.Pin = LED_PIN; // Select LED pin (PA5)
LED_PORT_CLK_ENABLE(); // Enable clock for GPIOA peripheral
HAL_GPIO_Init(LED_PORT, &GPIO_Config); // Initialize GPIOA with the configuration
__HAL_RCC_GPIOA_CLK_ENABLE(); // (Redundant) Enable GPIOA clock again
}
int main(void)
{
HAL_Init(); // Initialize HAL library and SysTick
SystemClock_Config(); // Configure system clock
initGPIO(); // Initialize LED GPIO
MX_USART2_UART_Init(); // Initialize USART2 for serial communication
printf("Hello, %s!\n", "Wokwi"); // Send text over USART2 using printf
HAL_GPIO_TogglePin(LED_PORT, LED_PIN); // Toggle LED once at startup
while (1); // Infinite loop (program runs forever)
return 0; // Never reached
}
/**
@brief System Clock Configuration
@retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0}; // Oscillator configuration structure
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; // Clock tree configuration structure
/** Initializes the RCC Oscillators according to the specified parameters */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; // Use internal HSI oscillator
RCC_OscInitStruct.HSIState = RCC_HSI_ON; // Turn HSI on
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1; // No division of HSI
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; // Default calibration
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler(); // Handle oscillator configuration error
}
/** Initializes the CPU, AHB and APB buses clocks */
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1; // Select clocks to configure
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; // Use HSI as system clock
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; // SYSCLK = HSI
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1; // AHB clock = SYSCLK
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1; // APB1 clock = AHB clock
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler(); // Handle clock configuration error
}
}
/**
@brief USART2 Initialization Function
@param None
@retval None
*/
static void MX_USART2_UART_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0}; // GPIO configuration structure
__HAL_RCC_GPIOA_CLK_ENABLE(); // Enable clock for GPIOA
// PA2 ------> USART2_TX
// PA3 ------> USART2_RX
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3; // Select PA2 and PA3
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // Alternate function push-pull
GPIO_InitStruct.Pull = GPIO_NOPULL; // No pull-up or pull-down
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; // Low-speed GPIO
GPIO_InitStruct.Alternate = GPIO_AF1_USART2; // AF1 maps pins to USART2
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // Initialize GPIOA pins
huart2.Instance = USART2; // Select USART2 peripheral
huart2.Init.BaudRate = 115200; // Communication speed: 115200 baud
huart2.Init.WordLength = UART_WORDLENGTH_8B; // 8-bit data
huart2.Init.StopBits = UART_STOPBITS_1; // 1 stop bit
huart2.Init.Parity = UART_PARITY_NONE; // No parity bit
huart2.Init.Mode = UART_MODE_TX_RX; // Enable TX and RX
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; // No hardware flow control
huart2.Init.OverSampling = UART_OVERSAMPLING_16; // Oversampling by 16
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; // Disable one-bit sampling
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1; // No clock prescaling
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; // No advanced features
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler(); // Handle UART initialization error
}
}
void Error_Handler(void)
{
/* User can add custom error handling code here */
}
// The following makes printf() write to USART2:
#define STDOUT_FILENO 1 // File descriptor for standard output
#define STDERR_FILENO 2 // File descriptor for standard error
int _write(int file, uint8_t *ptr, int len)
{
switch (file)
{
case STDOUT_FILENO: // Standard output (printf)
HAL_UART_Transmit(&huart2, ptr, len, HAL_MAX_DELAY); // Send data via UART
break;
case STDERR_FILENO: // Standard error output
HAL_UART_Transmit(&huart2, ptr, len, HAL_MAX_DELAY); // Send data via UART
break;
default:
return -1; // Unsupported file descriptor
}
return len; // Return number of bytes written
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6