This program send the characters received, with interrupts.
main.c:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <ctype.h>
#include "usart.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
void blink_led(void);
static void __attribute__((section(".RamFunc"))) blink_PA3(void);
void hello_world(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
while (usart_kbhit()) {
char ch = usart_fgetc();
usart_fputc(ch);
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 2;
RCC_OscInitStruct.PLL.PLLN = 40;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
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;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
/** Enables the Clock Security System
*/
HAL_RCC_EnableCSS();
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/* USER CODE BEGIN USART2_Init 0 */
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
usart_init();
/* USER CODE END USART2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : PA3 */
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : LD3_Pin */
GPIO_InitStruct.Pin = LD3_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LD3_GPIO_Port, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
/* USER CODE BEGIN 4 */
static void __attribute__((section(".RamFunc"))) blink_PA3(void) {
while (1) {
GPIOA->BSRR = (1 << 3); // Set PA3 (Pin A2 in the Nucleo board)
GPIOA->BRR = (1 << 3); // Reset PA3
GPIOA->BSRR = (1 << 3); // Set PA3 (Pin A2 in the Nucleo board)
GPIOA->BRR = (1 << 3); // Reset PA3
GPIOA->BSRR = (1 << 3); // Set PA3 (Pin A2 in the Nucleo board)
GPIOA->BRR = (1 << 3); // Reset PA3
GPIOA->BSRR = (1 << 3); // Set PA3 (Pin A2 in the Nucleo board)
GPIOA->BRR = (1 << 3); // Reset PA3
GPIOA->BSRR = (1 << 3); // Set PA3 (Pin A2 in the Nucleo board)
GPIOA->BRR = (1 << 3); // Reset PA3
GPIOA->BSRR = (1 << 3); // Set PA3 (Pin A2 in the Nucleo board)
GPIOA->BRR = (1 << 3); // Reset PA3
GPIOA->BSRR = (1 << 3); // Set PA3 (Pin A2 in the Nucleo board)
GPIOA->BRR = (1 << 3); // Reset PA3
GPIOA->BSRR = (1 << 3); // Set PA3 (Pin A2 in the Nucleo board)
GPIOA->BRR = (1 << 3); // Reset PA3
GPIOA->BSRR = (1 << 3); // Set PA3 (Pin A2 in the Nucleo board)
GPIOA->BRR = (1 << 3); // Reset PA3
GPIOA->BSRR = (1 << 3); // Set PA3 (Pin A2 in the Nucleo board)
GPIOA->BRR = (1 << 3); // Reset PA3
}
}
void blink_led(void) {
HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_SET);
HAL_Delay(1000);
HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
HAL_Delay(100);
}
void hello_world(void) {
uint8_t message[] = "Hello World!\n"; //Data to send
HAL_UART_Transmit(&huart2, message, sizeof(message), 10); // Sending in normal mode
HAL_Delay(1000);
}
/*
* Put char in USART transmit register
*/
int __io_putchar(int ch) {
while (!(USART2->ISR & 0x0080))
; // Wait for transfer buffer to be empty
USART2->TDR = (ch & 0xFF);
return ch;
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1) {
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
usart.h:
/*
* Function declaration
*/
#include <ctype.h>
void usart_init(void);
void usart_fputs(char *str);
void usart_fputc(char ch);
void usart_send(void);
void usart_receive(void);
char usart_kbhit(void);
char usart_fgetc(void);
int16_t usart_free(int16_t init, int16_t end);
usart.c:
#include "main.h"
#include "usart.h"
/*
* Types and variables declaration
*/
#define USART USART2
#define USART_BUFF_SIZE (250)
#define USART_EOF (-1)
struct usart_buff_t {
char buff[USART_BUFF_SIZE + 1];
uint16_t init;
uint16_t end;
};
struct usart_buff_t usart_buff_in, usart_buff_out;
/*
* Inits USART
*/
void usart_init(void) {
USART->CR1 |= USART_CR1_RE; // RX enable.
USART->CR1 |= USART_CR1_TE; // TX enable.
USART->CR1 |= USART_CR1_UE; // USART enable.
USART->CR1 |= USART_CR1_RXNEIE; // USART Receive Interrupt Enable.
/* Disable the UART Parity Error Interrupt */
USART->CR1 &= ~(USART_CR1_PEIE | USART_CR1_CMIE);
/* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
USART->CR3 &= ~(USART_CR3_EIE);
// Enable interrupt from USART (NVIC level)
NVIC_EnableIRQ(USART2_IRQn);
}
/*
* Save string to USART buffer output
*/
void usart_fputs(char *str) {
while (*str) {
usart_fputc(*str++);
}
}
/*
* Save char to USART buffer output
*/
void usart_fputc(char ch) {
if (usart_free(usart_buff_out.init, usart_buff_out.end) == 0) {
return;
}
usart_buff_out.buff[usart_buff_out.init] = ch;
if (++usart_buff_out.init == USART_BUFF_SIZE)
usart_buff_out.init = 0;
USART->CR1 |= USART_CR1_TXEIE; // USART Transmit Interrupt Enable.
}
/*
* Test if input buffer is empty
* return 0 if buffer empty
*/
char usart_kbhit(void) {
if (usart_buff_in.init == usart_buff_in.end)
return 0;
return 1;
}
/*
* Get char from USART buffer input
*/
char usart_fgetc(void) {
int8_t ch;
if (usart_buff_in.init == usart_buff_in.end)
return USART_EOF;
ch = usart_buff_in.buff[usart_buff_in.init];
if (++usart_buff_in.init == USART_BUFF_SIZE)
usart_buff_in.init = 0;
return ch;
}
/*
* Return bytes remaining in rs232 buffer
* 0 = buffer full
* 1 = buffer with one space remaining
*/
int16_t usart_free(int16_t init, int16_t end) {
end -= init;
if (end >= 0)
return (USART_BUFF_SIZE - 1) - end;
else
return 1 - end;
}
/*
* Send char from buffer to USART
* Callable only by Interrupt Service Routine
*/
void usart_send(void) {
if (usart_buff_out.init == usart_buff_out.end) { // No more char to send
USART->CR1 &= ~USART_CR1_TXEIE; // USART Transmit Interrupt Disable.
return;
}
USART->TDR = usart_buff_out.buff[usart_buff_out.end];
usart_buff_out.end++;
if (usart_buff_out.end == USART_BUFF_SIZE)
usart_buff_out.end = 0;
}
/*
* Receive char from USART to buffer
* Callable only by Interrupt Service Routine
*/
void usart_receive(void) {
// If the input buffer is full, the input character is discarded.
if (usart_free(usart_buff_in.init, usart_buff_in.end) == 0) {
char ch = USART->RDR;
return;
}
usart_buff_in.buff[usart_buff_in.end] = USART->RDR;
usart_buff_in.end++;
if (usart_buff_in.end == USART_BUFF_SIZE)
usart_buff_in.end = 0;
}
/*
* USART IRQ Handler
*/
void USART2_IRQHandler(void) {
//check if we are here because of RXNE interrupt
if (USART->ISR & USART_ISR_RXNE) {
usart_receive();
}
//check if we are here because of TXEIE interrupt
if (USART->ISR & USART_ISR_TXE) {
usart_send();
}
// Error handling
}