- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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 */
rx_pin_config.mode = XMC_GPIO_MODE_INPUT_INVERTED_TRISTATE;
XMC_GPIO_Init(XMC_GPIO_PORT2,1, &rx_pin_config);
XMC_UART_CH_SetInputSource(XMC_UART0_CH0,XMC_UART_CH_INPUT_RXD,USIC0_C0_DX0_P2_1);
/* Configure UART channel */
XMC_UART_CH_Init(XMC_UART0_CH0, &uart_config);
/* Start UART channel */
XMC_UART_CH_Start(XMC_UART0_CH0);
/* 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);
while(1)
{
/* Infinite loop */
uint16_t recvdata=XMC_UART_CH_GetReceivedData(XMC_UART0_CH0);
if(recvdata!=0)
{
XMC_UART_CH_Transmit(XMC_UART0_CH0, recvdata);
}
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
XMC_UART_CH_Start(XMC_UART2_CH0) ;
// Configure the receiving FIFO (16 bytes buffer) :
XMC_USIC_CH_RXFIFO_Configure(XMC_UART2_CH0,0,XMC_USIC_CH_FIFO_SIZE_16WORDS,0) ;
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 ...
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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,
Aashita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
XMC_UART_CH_Start(XMC_UART2_CH0) ;
// Configure the receiving FIFO (16 bytes buffer) :
XMC_USIC_CH_RXFIFO_Configure(XMC_UART2_CH0,0,XMC_USIC_CH_FIFO_SIZE_16WORDS,0) ;
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 ...
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello, I have been able to receive data with this program now, but the received data is inconsistent with the data I sent, I sent hexadecimal 01, 02, 03, but received 7F, 3F, 7E, I have Checked that the baud rate is consistent!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
All your bits are flipped in polarity (0 becomes F and vice versa) since you told the input pin to do so with XMC_GPIO_MODE_INPUT_INVERTED_TRISTATE.
Just configure your RX port with the default XMC_GPIO_MODE_INPUT_TRISTATE should work a lot better ...