Problem receiving bytes using UART interruption (losing some bytes)

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

cross mob
User16943
Level 1
Level 1
So everything was going fine with the UART application, I was using Interrupt mode for both reception and transmission.

I'm working on a CAN application and I'm using a circular buffer. I need to receive bytes and transmit bytes one at a time through the serial port and I must use interruptions.

Before I was using UART_Receive and UART_Transmit with FIFO enabled.

When I started sending and receiving CAN messages with CAN_NODE the whole program got slower and I think it's because I needed to have the (UART_Receive == success) in the main loop so the program could enter the receive callback function so I could write the received byte to the circular buffer. The thing is this is not what is supposed to happen. I need to enter the receive callback function without having nothing in the main loop.

I tried some solutions that are already here (creating my own interrupt routine with INTERRUPT app and assigning interrupt_std_receive) but RBUF is uint16_t and I need to read an uint8_t at each time.

For example I'm supposed to receive something like "\r\nNORMAL POWER DOWN\r\n" and I'm receiving "\r\nRM W\r"


volatile uint32_t receive_byte_count_ble = 0;
uint8_t ReadData0_ble [3000] = {0};

volatile uint32_t R_count_ble = 0;

main(){
while(1){
}
}

void rx_callback(void){
ReadData0_ble[receive_byte_count_ble]=XMC_UART_CH_GetReceivedData(UART_BLE.channel);
receive_byte_count_ble++;
}

Any ideas? I just need to receive byte for byte and enter the callback function each time so I can save the byte to this array but bytes are missing.
0 Likes
2 Replies
User14604
Level 4
Level 4
First solution authored
RBUF is probably 16 bits, because UART allows word sizes of more than 8 bits. See fields FLE and WLE (frame / word length) in register SCTR of USIC. Only the configured amount of bits will be present in RBUF register on receival.
So maybe you are not losing data, because you are reading too much, but because some higher interrupt prevents you from accessing the interrupt often enough.

Simply create a DAVE app with RX interrupt and access the RBUF inside to access your data. Example for USIC 1, interrupt 0 RX handler. If "rxOverflows" is non-zero, your RX routine isn't called in time.


uint32_t rxOverflows; /** Counts occurrences of lost RX data. Amount of lost RX bytes may be higher! */
uint8_t rxBuffer[128]; /** Circular buffer for incoming bytes. */
uint32_t rxPointer; /** Points to next position to read into rxBuffer[]. */

/**
* Interrupt service routine for USIC 1.
*
* Receives incoming bytes.
*/
void
USIC1_0_IRQHandler() {

if (USIC1_CH1->PSR & (1U << USIC_CH_PSR_DLIF_Pos)) {
/* RX register shows data lost */
rxOverflows++;
}

/* add new character to your buffer */
rxBuffer[rxPointer++] = USIC1_CH1->RBUF;

if (rxPointer >= 128) {
rxPointer = 0;
}

// TODO: add check for rxBuffer overflow!
}



Live example can be found here: https://www.infineonforums.com/threads/6307-UART-bootloader-with-checksum-capability-%28project-atta...
ATTENTION: No DAVE, just plain C code!
0 Likes
User16943
Level 1
Level 1
Hello, I will try that when I can. Thank you very much.

RBUF is 16 bits. There is also RBUF0 and RBUF1.

When UART receives two bytes one after the other it fills RBUF0 first and RBUF1 after?

I really just want the program to enter the rx_handler when it receives a byte so that I can save that byte to my buffer. That simple.

Right now I just want a program that does only that (read byte for byte from the UART), no CAN interruptions no nothing. One step at a time. After I get this I will add the stuff I had before.

MEGA EDIT:

So I think I got it.

What I did:

3563.attach

I now for a fact that I'm receiving 86 characters and my rx_counter hits the number 86.



uint8_t rxBuffer[128];
uint32_t rxPointer = 0;

uint32_t rx_counter = 0;

void UARTBLE_IRQHandler(void){
rxBuffer[rxPointer++]= USIC2_CH1->RBUF;
rx_counter++;
}


Did I got it? Is there a better way to do it?

3564.attach

3565.attach
0 Likes