2024-03-22 08:34 AM
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.
Solved! Go to Solution.
2024-03-22 11:11 PM
"hmmmm" you can send but not receive properly?
Have you tried to debug (and to make sure UART is working on both sides) via:
If this fails now still: maybe an issue with the signal levels: one MCU running as 3V3, the other on 1V8. Or: one is even 5V level, and the other 3V3 level.
Use a scope to verify the signals.
Also make sure, the UART settings (e.g. 7bit or 8bit, with parity or not, 1stop bit or 2stop bits...) are the same on both sides.
2024-03-22 01:06 PM
So what DO you receive in the F7? Have you looked at the buffer?
I don't see a way for the UART callback to tell the screen task that it has new data. That could allow the screen task to decode partially received data (or no data at all).
2024-03-22 11:11 PM
"hmmmm" you can send but not receive properly?
Have you tried to debug (and to make sure UART is working on both sides) via:
If this fails now still: maybe an issue with the signal levels: one MCU running as 3V3, the other on 1V8. Or: one is even 5V level, and the other 3V3 level.
Use a scope to verify the signals.
Also make sure, the UART settings (e.g. 7bit or 8bit, with parity or not, 1stop bit or 2stop bits...) are the same on both sides.
2024-03-23 08:14 AM
Ok, so I checked the UART with the scope and I found that sometimes the buffer from the F4 is coming well to the F7 and sometimes it is not. For my application I can just use a conditional asking if the first byte of the buffer is '$' and forget about the data loss but I wonder why some data is not coming well.
2024-03-25 03:33 AM
Hello @ferdlf ,
Your pointer UART handle should be a pointer.
Try changing extern line 7 of your screenview form "UART_HandleTypeDef huart7;" to "extern UART_HandleTypeDef *huart7;"
Regards,