#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stm32l0xx_hal.h>
/* ST Nucleo Green user LED (PB3)
* As defined in https://www.st.com/resource/en/user_manual/um1956-stm32-nucleo32-boards-mb1180-stmicroelectronics.pdf
*/
#define LED_PORT GPIOB
#define LED_PIN GPIO_PIN_3
#define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
#define VCP_TX_Pin GPIO_PIN_2
#define VCP_RX_Pin GPIO_PIN_15
UART_HandleTypeDef UartHandle;
void GPIO_Init();
void Uart_Init();
volatile uint32_t led_ready = 0;
/*
Called by SysTick_Handler
See: https://developer.arm.com/documentation/107565/0101/Use-case-examples/rtos-context-switch/Basic-Building-Blocks/The-SysTick-Timer
*/
void osSystickHandler(void)
{
static uint32_t tick_count = 0;
tick_count++;
//printf("tick count %d\n", tick_count);
if ((tick_count % 500) == 0)
{
printf("led ready!\n");
led_ready = 1; // Signal main loop to toggle LED
}
}
int main(void)
{
Uart_Init();
printf("UART initialized.\n");
HAL_Init();
printf("HAL initialized.\n");
GPIO_Init();
printf("GPIO init\n");
HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
printf("TogglePin\n");
while (1) {
//printf("here 1\n");
if (led_ready) {
HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
led_ready = 0;
}
}
return 0;
}
/*
Initialize GPIOs
*/
void GPIO_Init()
{
GPIO_InitTypeDef GPIO_Config;
GPIO_Config.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_Config.Pull = GPIO_NOPULL;
GPIO_Config.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_Config.Pin = LED_PIN;
LED_PORT_CLK_ENABLE();
HAL_GPIO_Init(LED_PORT, &GPIO_Config);
}
/*
Re-routing of the C library printf function to the UART.
The UART outputs a message on the HyperTerminal.
Board: STM32L031K6-Nucleo
Tx Pin: PA.02 (A7 : pin 5 on CN4)
Rx Pin: PA.03 (A2 : pin 10 on CN4)
_________________________
| ______________| _______________
| |USART | | HyperTerminal |
| | | | |
| | TX |______________________|RX |
| | | | |
| | | RS232 Cable | |
| | | | |
| | RX |______________________|TX |
| | | | |
| |______________| |_______________|
| |
| |
| |
| |
|_STM32_Board_____________|
The USART2 is configured as follows:
- BaudRate = 115200 baud
- Word Length = 8 Bits (7 data bit + 1 parity bit)
- One Stop Bit
- Odd parity
- Hardware flow control disabled (RTS and CTS signals)
- Both TX and RX are enabled
*/
void Uart_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = VCP_TX_Pin|VCP_RX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
UartHandle.Instance = USART2;
UartHandle.Init.BaudRate = 115200;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_ODD;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
if (HAL_UART_Init(&UartHandle) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
}
void Error_Handler(void)
{
while(1);
}
/*
* _write function is used to redirect the printf output to the UART.
* It is called by the C library printf function.
*/
int _write(int file, uint8_t *ptr, int len)
{
switch (file)
{
case STDOUT_FILENO:
case STDERR_FILENO:
if (HAL_UART_Transmit(&UartHandle, ptr, len, HAL_MAX_DELAY) != HAL_OK)
{
return -1;
}
break;
default:
return -1;
}
return len;
}
Loading
st-nucleo-l031k6
st-nucleo-l031k6