Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

XMC™ Forum Discussions

Level 1
First reply posted 5 sign-ins First question asked
Level 1

The UART can send data but cannot receive data. The UART port uses P2.0 and P2.1. The following is my code. Can you explain what went wrong? Thank you!

#include "xmc_gpio.h"
#include "xmc_uart.h"

#define TICKS_PER_SECOND 1000
#define TICKS_WAIT 1000

const uint8_t message[] = "Hello world!!\n";

XMC_GPIO_CONFIG_t led1_config;
XMC_GPIO_CONFIG_t rx_pin_config;
XMC_GPIO_CONFIG_t tx_pin_config;
XMC_UART_CH_CONFIG_t uart_config;

int main(void)
/* Configure LED1 */
led1_config.mode = XMC_GPIO_MODE_OUTPUT_PUSH_PULL;
XMC_GPIO_Init(XMC_GPIO_PORT0,6, &led1_config);

/* UART configuration */
uart_config.data_bits = 8U;
uart_config.stop_bits = 1U;
uart_config.baudrate = 115200;

/* Configure RX pin */
XMC_GPIO_Init(XMC_GPIO_PORT2,1, &rx_pin_config);


/* Configure UART channel */
XMC_UART_CH_Init(XMC_UART0_CH0, &uart_config);
/* Start UART channel */

/* Configure TX pin */
tx_pin_config.mode = XMC_GPIO_MODE_OUTPUT_PUSH_PULL_ALT6;
XMC_GPIO_Init(XMC_GPIO_PORT2,0, &tx_pin_config);

/* Send a message via UART periodically */
SysTick_Config(SystemCoreClock / TICKS_PER_SECOND);

/* Infinite loop */
uint16_t recvdata=XMC_UART_CH_GetReceivedData(XMC_UART0_CH0);
XMC_UART_CH_Transmit(XMC_UART0_CH0, recvdata);



3 Replies
Moderator 250 replies posted 250 sign-ins 100 replies posted

Hi @winnie_zhan ,

Seeing your code, I suppose that you have tried following our DAVE based code example "UART TRANSMIT" and modified it accordingly. Is that the case?

Also, can you please try setting up the input source for UART channel after configuring the UART channel, then start it? Please let us know your results for the same. And, as I see through your code you are no where sending "Hello World" as message. Can you please attach your transmitted data trace?

Please add here in case of any further query.

Best Regards,



Level 1
First reply posted 5 sign-ins First question asked
Level 1

Hi,I used this function XMC_UART_CH_SetInputSource(XMC_UART0_CH0,XMC_UART_CH_INPUT_RXD,USIC0_C0_DX0_P2_1)

 setting up the input source for UART channel;and use XMC_UART_CH_Start(XMC_UART0_CH0)  start it,

i use XMC_UART_CH_GetReceivedData() This function always returns a value of 0 when receiving data.

The picture shows that the data of 0x5A is sent to XMC and the hello world sent by XMC is received



Level 2
5 sign-ins First solution authored First like received
Level 2

Hi winnie.

XMC_UART_CH_GetReceivedData() does not check if data is received. It always returns a value even if no data was received. The documentation also states : 


* Before returning the value, there is no check for data validity. User should check the appropriate

* data receive flags(standard receive/alternative receive/FIFO standard receive/FIFO alternative receive)

* before executing the API. Reading from an empty receive FIFO can generate a receive error event.


A better way to configure your UART is to configure a FIFO which would receive the data. That way you can check if the FIFO received something before calling XMC_UART_CH_GetReceivedData(). 

This code works for me (for UART2_CH0. Adjust to your own UART choice and pins) :

XMC_UART_CH_Init(XMC_UART2_CH0, &uart_port_config) ; 
XMC_UART_CH_SetInputSource(XMC_UART2_CH0, XMC_UART_CH_INPUT_RXD, USIC2_C0_DX0_P5_1) ; // P5.1 input RX

// Configure the receiving FIFO (16 bytes buffer) :

Now in your main loop you can test if a byte arrived :


while (1) { 


   // UART2_CH0 : check for data :
   if (XMC_USIC_CH_RXFIFO_GetLevel(XMC_UART2_CH0)!=0) {
         // At least one character arrived. Read it :

         uint8_t read_char = XMC_UART_CH_GetReceivedData(XMC_UART2_CH0) ;

         .... do what you want with this character ...