2020-02-03 09:25 AM
Below is the code for sending data at 2 Mbps baudrate.
#include "stm32f4xx.h"
#include "stm32f4xx_hal.h"
#include "stm32f446xx.h"
#include "stm32f4xx_hal_cortex.h"
#include "stm32f4xx_hal_tim.h"
#include "main.h"
#include <string.h>
#include <stdint.h>
#define BUFFER_SIZE 20486
#define DATA_SIZE 20480
UART_HandleTypeDef uart2_handle;
TIM_HandleTypeDef tim_config;
uint8_t data_buffer[BUFFER_SIZE] = {0};
volatile int timer_count = 0;
int main(void)
{
data_buffer[0] = 'T';
data_buffer[1] = 'R';
data_buffer[2] = 'A';
data_buffer[3] = 'A';
memset((void *)data_buffer+4, 0x55, DATA_SIZE);
data_buffer[BUFFER_SIZE-2] = 'Z';
data_buffer[BUFFER_SIZE-1] = 'Z';
//Initialize the peripherals
HAL_Init();
//SystemConfig
SystemClock_Config();
//Initialize the UART
UART2_Init();
//Repeat two data transfers after every 800ms
//uint8_t start_buffer[2] = {'T', 'R'};
HAL_StatusTypeDef uart_status;
while(1)
{
do
{
uart_status = HAL_UART_Transmit_IT(&uart2_handle, (uint8_t *)data_buffer, (uint16_t)sizeof(data_buffer)/sizeof(data_buffer[0]));
if(uart_status == HAL_OK)
{
break;
}
else if(uart_status == HAL_ERROR)
{
error_handler();
break;
}
}while(uart_status == HAL_BUSY);
}
}
void UART2_Init(void)
{
//Enable the UART2 and GPIOA clock.
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_USART2_CLK_ENABLE();
//Configure the UART2 peripheral
uart2_handle.Instance = USART2;
uart2_handle.Init.BaudRate = 2000000;
uart2_handle.Init.Mode = UART_MODE_TX;
uart2_handle.Init.WordLength = UART_WORDLENGTH_8B;
uart2_handle.Init.StopBits = UART_STOPBITS_1;
uart2_handle.Init.Parity = UART_PARITY_NONE;
uart2_handle.Init.OverSampling = UART_OVERSAMPLING_8;
if(HAL_UART_Init(&uart2_handle) != HAL_OK)
{
//Initialization failed.
error_handler();
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
error_handler();
}
/**Initializes the CPU, AHB and APB busses 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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
error_handler();
}
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
void error_handler(void)
{
//Hang in a while loop.
while(1);
}
2020-02-06 11:46 PM
This type of problems are also because HAL "lock" is not interrupt safe.