2023-06-12 08:53 AM - edited 2023-11-20 03:25 AM
Hello,
I have a problem with RTC and UART reception using the DMA.
I am currently working with the L476RG core board. The RTC clock is the LSE. The UART is configured in cyclic mode on the Rx PIN.
I have done a simple data logging and could check that the moment I use the UART transmission the RTC increases one second.
The following table shows how the difference between the times changes when using HAL_UART_transmit.
When I comment the HAL_Usart_transmit line the difference between the hours goes away.
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include <stdbool.h>
#define RxBuff_SIZE 255
RTC_HandleTypeDef hrtc;
UART_HandleTypeDef huart4;
DMA_HandleTypeDef hdma_uart4_rx;
//! variables for RTC
char time_char[30] = {'\0'};
char date_char[30] = {'\0'};
//! Creation the structure to stock the RTC data
RTC_info_t RTC_info;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_RTC_Init(void);
static void MX_UART4_Init(void);
static void MX_SDMMC1_SD_Init(void);
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_DMA_Init();
MX_USART2_UART_Init();
MX_RTC_Init();
MX_UART4_Init();
MX_SDMMC1_SD_Init();
MX_FATFS_Init();
/* USER CODE BEGIN 2 */
//! Start DMA on Screen port
if (HAL_OK != HAL_UARTEx_ReceiveToIdle_DMA(&huart4, RxBuff, RxBuff_SIZE))
{
Error_Handler();
}
//! Disable the half interrup of the UART4
__HAL_DMA_DISABLE_IT(&hdma_uart4_rx, DMA_IT_HT);
while (1)
{
rtc_get_time_normal(&RTC_info, &hrtc);
rtc_print(&RTC_info, date_char, time_char);
deb_print(date_char);
deb_print(time_char);
HAL_UART_Transmit_IT(&huart4, (uint8_t*)data, len_data);
HAL_Delay(2000);
}
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t size)
{
if (huart->Instance == UART4) // <-- UART designator should correspond to UART port used
{
char CRLF_char[64] = {'\0'};
if (detectCRLF((char *)RxBuff, CRLF_char, sizeof(CRLF_char)) != true)
{
// Error in the message received from the KIM module
__NOP();
}
else
{
// If the received value contains CR and LF
// We copy the value
setFlagRx(RxBuff);
}
// copy the RxBuff to MainBuff
memcpy(MainBuff, RxBuff, size);
// Clear RxBuffer and reset DMA
//deb_print((char *)RxBuff);
memset(RxBuff, 0, RxBuff_SIZE);
HAL_UARTEx_ReceiveToIdle_DMA(&huart4, RxBuff, RxBuff_SIZE);
__HAL_DMA_DISABLE_IT(&hdma_uart4_rx, DMA_IT_HT);
}
}
uint8_t rtc_get_time_normal(RTC_info_t *time_param, RTC_HandleTypeDef* hrtc)
{
/**
* This function allows to obtain the time in a normal
* format (human). The values are stored in a structure
* pointer
*/
uint8_t ack = false;
RTC_DateTypeDef gDate = { 0 };
RTC_TimeTypeDef gTime = { 0 };
__HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
{
return false;
}
__HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
HAL_RTC_GetTime(hrtc, &gTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(hrtc, &gDate, RTC_FORMAT_BIN);
time_param->year = gDate.Year;
time_param->month = gDate.Month;
time_param->day = gDate.Date;
time_param->hour = gTime.Hours;
time_param->minute = gTime.Minutes;
time_param->second = gTime.Seconds;
return ack;
}
void rtc_print(RTC_info_t *time_param, char * date_char, char * time_char)
{
sprintf(date_char, "[RTC]-> Date %02d-%02d-%02d\r\n", time_param->day, time_param->month, time_param->year);
sprintf(time_char, "[RTC]-> Time %02d:%02d:%02d\r\n", time_param->hour, time_param->minute, time_param->second);
}
When I use the UART_transmit my other module responds by sending +OK or other data("xxxxxxxxxxxx"). This data is received by the DMA.
How can I fix this? I have already made several tests, and I am sure that it is the UART with the DMA that alter the RTC.
Thaks.
2023-06-12 09:22 AM
> I am currently working with the L476RG core board.
What board is that?
If you use UART without DMA, does it make any difference? My guess is, not.
Which pins does the UART use?
JW
2023-06-13 02:27 AM
I am working with the L476RG core board. I'm using the UART4.
UART4_TX = PA0
UART4_RX = PA1
I already test the UART only with the interrupt, but i still have the same problem.I don't know what to do