Skip to main content
Visitor II
March 17, 2020
Solved

Data corruption on UART - reg

  • March 17, 2020
  • 2 replies
  • 940 views
#include <stdint.h>
#include <stdio.h>
#include "bspapi.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "stm32f4xx.h"
 
TaskHandle_t Task3Handle = NULL;
 
 
static char key = 'Q';
extern int state = 0;
xQueueSetHandle xQueue;
 
void init_UsartConfiguration( void )
{
	/* BRR = Systemclock / ((Over2 - 0) * Baudrate)
	BRR in Decimal fraction is 0d546.875 == 0x222 (0d546 <--> 0x222) & (0.875*16 == 14) (0d14 <--> 0xE)
	Thus BRR --> 0x222E*/
 
	//USART1->BRR = 0x222E;
 USART1-> BRR = SystemCoreClock/9600;
	USART1->CR1 &= ~USART_CR1_OVER8; // Oversampling mode = 16
	USART1->CR1 &= ~USART_CR1_M; // Word length = 8 bits
	USART1->CR1 &= ~USART_CR1_PCE; // No parity
	USART1->CR1 |= USART_CR1_TE; // Transmitter enable
	USART1->CR1 |= USART_CR1_RE; // Receiver enable
	USART1->CR1 |= USART_CR1_UE; // USART enable
	USART1->CR1 |= USART_CR1_RXNEIE;
 //USART1->CR1	|= USART_CR1_TXEIE;
	USART1->CR2 &= ~(1<<12|1<<13); // STOP[1:0] = 00 thus one stop bits
	USART1 -> SR = 0;
}
 
void init_gpioClock()
{
	//Setting up RCC Register
 
	RCC -> AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
 RCC ->APB2ENR |= RCC_APB2ENR_USART1EN; //PA9 Serial_1 TX & PA10 Serial_1 RX
 
	GPIOA->MODER |= GPIO_MODER_MODER5_0;
	GPIOA->OSPEEDR = GPIO_OSPEEDER_OSPEEDR5_0;
	GPIOA->OTYPER = ((GPIO_OTYPER_OT_5) & (~GPIO_OTYPER_OT_5));
	GPIOA->PUPDR = GPIO_PUPDR_PUPDR5_0;
	/* GPIO Configurations for PA9 and PA10 which is the pins for USART1 */
	GPIOA ->MODER |= GPIO_MODER_MODER9_1| GPIO_MODER_MODER10_1;// PA9 Serial_1 TX //1<<19;
	GPIOA ->OTYPER|= 512;//;// 1<<9
	GPIOA ->OSPEEDR |= 512;//GPIO_OSPEEDER_OSPEEDR9_0; // 1<<9
	GPIOA ->PUPDR |= GPIO_PUPDR_PUPDR9_0 ; //1<<19
	GPIOA->AFR[1] |= 0x0770; // PA0 AFR value is AF07 so AFR[1] = 0x0770
}
 
char __uartSend()
{
	 char value = 'k';
	 while(1)
	 {
		 USART1->DR = value & 0xFF;
	 }
	 return 0;
}
 
 
int main(void)
{
 
 /* Configure the system clock */
 SystemInit();
 SystemCoreClockUpdate();
 init_gpioClock();
 init_UsartConfiguration();
 //xTimerCreate( "AutoReload",1000 ,pdTRUE, 0,prvTimerCallback );
 xQueue = xQueueCreate( 5, sizeof(uint8_t) );
 xTaskCreate(__uartSend, "send", 200, (void*)0, tskIDLE_PRIORITY, &Task3Handle);
 vTaskStartScheduler();
 return 0;
}

Hii,

I am new to freeRTOS and STM controllers. I am working on Nucleo-64 (STM32F411-re) development board. I was trying to send data through Uart. I was watching the data for correctness in Serial window. I've sent 'K' but in window I am recieving '0xf0' .

    This topic has been closed for replies.
    Best answer by Guenael Cadier

    Dear @Npiet.1​ 

    First, i would propose to write a single character in your __uartSend() function (and not push characters in TDR in an endless loop). This should allow at first to check your BRR setting.

    Then, when ok, if UART output is 9600 bauds, you have to submit characters in DR at same speed. Keeping the appropriate feeding rate is the function of the TX Empty bit in SR.

    => writing a new data for transmission in DR should be done only if TXE bit is 1 in SR. TXE=1 indicates previous data has already been shifted. Then DR space is available for new transmission.

    Hope this helps.

    Regards

    Guenael

    2 replies

    ST Employee
    March 17, 2020

    Dear @Npiet.1​ 

    First, i would propose to write a single character in your __uartSend() function (and not push characters in TDR in an endless loop). This should allow at first to check your BRR setting.

    Then, when ok, if UART output is 9600 bauds, you have to submit characters in DR at same speed. Keeping the appropriate feeding rate is the function of the TX Empty bit in SR.

    => writing a new data for transmission in DR should be done only if TXE bit is 1 in SR. TXE=1 indicates previous data has already been shifted. Then DR space is available for new transmission.

    Hope this helps.

    Regards

    Guenael

    Npiet.1Author
    Visitor II
    March 17, 2020

    Hi Guenael,

    I've changed HSE value from 25Mhz to 8Mhz in the below section of code from system_stm32f4xx.h headerfile . Data corruption us due to clock mismatch between receiver and transmitter end. Thanks it,s working now

    Regards,

    Naveen

    #include "stm32f4xx.h"
     
    #if !defined (HSE_VALUE) 
     
    #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */