How to receive data from UART using STM32f746-Disco
Hi, I got a problem with the reception using USART7. I'm using it in the interrupt mode but the problem is that the data in the buffer is not coming well. I'm connecting STM32F407 board with the STM32F746 using UART. When the F7 transmits, there's no problem and the data is sending well BUT when I'm trying to receive from F4, the data that arrives is not the same that the F4 sends.
This is the main code I'm using in the F7 for receive.
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t rx_buff[12];
/* USER CODE END 0 */
int main(void)
{
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* Enable the CPU Cache */
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_CRC_Init();
MX_DMA2D_Init();
MX_FMC_Init();
MX_I2C3_Init();
MX_LTDC_Init();
MX_QUADSPI_Init();
MX_LIBJPEG_Init();
MX_UART7_Init();
MX_TouchGFX_Init();
/* Call PreOsInit function */
MX_TouchGFX_PreOSInit();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart7, rx_buff, 12);
/* USER CODE END 2 */
/* Init scheduler */
osKernelInitialize();
/* Create the thread(s) */
/* creation of defaultTask */
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
/* creation of TouchGFXTask */
TouchGFXTaskHandle = osThreadNew(TouchGFX_Task, NULL, &TouchGFXTask_attributes);
/* creation of videoTask */
videoTaskHandle = osThreadNew(videoTaskFunc, NULL, &videoTask_attributes);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
/* USER CODE BEGIN RTOS_EVENTS */
/* add events, ... */
/* USER CODE END RTOS_EVENTS */
/* Start scheduler */
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
static void MX_UART7_Init(void)
{
huart7.Instance = UART7;
huart7.Init.BaudRate = 115200;
huart7.Init.WordLength = UART_WORDLENGTH_8B;
huart7.Init.StopBits = UART_STOPBITS_1;
huart7.Init.Parity = UART_PARITY_NONE;
huart7.Init.Mode = UART_MODE_TX_RX;
huart7.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart7.Init.OverSampling = UART_OVERSAMPLING_16;
huart7.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart7.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart7) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
HAL_UART_Receive_IT(&huart7, rx_buff, 12); //You need to toggle a breakpoint on this line!
}
/* USER CODE END 4 */
And this is the code of the screen2view.
#include <gui/screen2_screen/Screen2View.hpp>
#include <cmath>
#include "main.h"
extern "C"
{
extern UART_HandleTypeDef huart7;
}
extern uint8_t rx_buff[12];
Screen2View::Screen2View()
{
}
void Screen2View::setupScreen()
{
Screen2ViewBase::setupScreen();
}
void Screen2View::tearDownScreen()
{
Screen2ViewBase::tearDownScreen();
}
void Screen2View::handleTickEvent()
{
lineRadar.invalidate();
deg = (((rx_buff[7] - 0x30) * 100) + ((rx_buff[8] - 0x30) * 10) + (rx_buff[9] - 0x30));
/*
if(180 == deg)
incr = -1;
else if(0 == deg)
incr = 1;
*/
lineRadar.setEnd(200*cos(deg*(3.1415/180))+200, -200*sin(deg*(3.1415/180))+200);
Unicode::snprintf(textAngleBuffer, TEXTANGLE_SIZE, "%03d", deg);
textAngle.invalidate();
lineRadar.invalidate();
//deg+=incr;
}
In this code, I'm sending from F4 this buffer: $D:XXA:090#
A:090 is the degree from a stepper motor and, in the F7 screen2view code, the variable deg is saving that number but it is never the same as what it was sended.
