Cypress Gurus..Please Help Me with PSOC 4200 UART RX Code - Page 1
Forum section: electronics / general

author: SuzyC
author_rank: Frequent Contributor
timestamp: December 05, 2018, 04:04:58 pm
content: Hi, Cypress Geeks and Gurus! I need your help!There are more than a hundred ways to skin a cat and I can see that there might be many more incorrect ways to code RX-ing bytes with the Cypress PSOC 4200 SCB Standard UART.I have another processor Tx-ing exactly 48 bytes to the PSOC RX pin at 115200 baud every few seconds.I can't find any example on the web that shows how to receive multiple bytes by the Standard UART, I can find only a far too simple video that shows how to echo each byte received back to the sending terminal.Only one UART, alias UA, RX only mode.I have started the UART in main() correctly before the main() loop.I have a char buffer Rx to hold the receive bytes and the PSOC RX set to 115200,N,8,1 (same as TX source)I have set the UART circular buffer RX FIFO buffer size to 48 bytes.The only active interrupts I have selected is: RX FIFO buffer full, FIFO Overflow, Frame ErrorI have come up with the code below:Will this seemingly too simple code work and is it robust enough to deal with possible various RX receive errors?//var initialization before main()static char Rx;static char j;// in main()if(UA_CHECK_CAUSE_INTR(UA_INTR_RX_FULL)){for(j=0;j<48;j++){ Rx=UA_UartGetByte();}UA_rx_ClearInterrupt();}else if(UA_CHECK_CAUSE_INTR(UA_INTR_RX_ALL)){UA_rx_ClearInterrupt();UA_Stop();UA_Start();}

author: ebclr
author_rank: Super Contributor
timestamp: December 18, 2018, 12:06:05 am
content: This mays helps

author: westfw
author_rank: Super Contributor
timestamp: December 19, 2018, 02:28:11 am
content: I am not a PSOC guru, but...QuoteWhy? By its nature, a FIFO should be somewhat bigger than the max amount of data you expect to receive.I'm looking at:http://www.cypress.com/file/408071/download...QuoteI don't see CHECK_CAUSE_INTR documented. Surely this is something that would belong in an ISR function, and not in main(). The documentation strongly implies that when you specify a fifo size larger than the hardware fifo (which is only 8 or 16 bytes), they are automatically going to give you code that handles the interrupts and extra buffering... UartGetByte() says it retrieves bytes from the buffer, not from the hardware. And it has error status included in the return value, which you should probably look at if you are trying to be "robust."QuoteThere usually isn't any reason to stop and restart a UART.

author: ShowKemp
author_rank: Contributor
timestamp: December 19, 2018, 05:02:53 am
content: I'm not a psoc guru neither but i used a "circular buffer" and the Rx FIFO not empty interrupt to receive 48 chars, when i receive the 48 chars i print them into the serial console.Here's my schematic:Here's the UART SCB configuration:Here's the Rx FIFO interrupt configuration:And in the main.c file:Code:#include <stddef.h>#include <stdbool.h>#include "project.h"#define BUFFER_SIZE 48size_t buffer_index = 0;char buffer = {0};volatile bool is_buffer_full = false;void data_received(void);int main(void){CyGlobalIntEnable;isr_UART_StartEx(data_received);UART_Start();for(;;) {if (is_buffer_full) {UART_UartPutString("Data received: ");for (size_t i = 0; i < BUFFER_SIZE; i++) {UART_UartPutChar(buffer);}UART_UartPutString("\r\n");is_buffer_full = false;}}}void data_received(void){/* Checks for "RX FIFO AND not empty" interrupt */if(UART_INTR_RX_NOT_EMPTY & UART_GetRxInterruptSource()){/* Get the character from terminal */char data_rcvd = UART_UartGetChar();buffer = data_rcvd;buffer_index++;if (BUFFER_SIZE == buffer_index) {is_buffer_full = true;buffer_index = 0;}/* Clear UART "RX FIFO not empty interrupt" */UART_ClearRxInterruptSource(UART_INTR_RX_NOT_EMPTY);}B_LED_Write(~B_LED_Read());isr_UART_ClearPending();}/* [] END OF FILE */

author: obiwanjacobi
author_rank: Super Contributor
timestamp: December 19, 2018, 06:53:21 am
content: Here is my PSoC5 project that also uses an UART (PSoC Creator 4.2).https://github.com/obiwanjacobi/Zalt/tree/master/Source/SystemControllerPerhaps it will inspire you?My suggestion is to turn off all interrupts and first get it working in a dedicated loop with as less variables/dependencies as possible. That way you'll know what parts are working. After that introduce one thing at a time. Small steps.

author: westfw
author_rank: Super Contributor
timestamp: December 24, 2018, 09:11:20 am
content: I did some experiments with PSoC Creator, and it looks to me like if you set up the UART with a buffer larger than the FIFO, and "internal" interrupts, it will indeed include a typical SW-managed circular buffer in the code it generates. Messy to follow, but probably working(?)

author: henmill
author_rank: Contributor
timestamp: July 08, 2022, 08:48:06 pm
content: Clearly this is a 'dead thread', but just want to give a shoutout to @ShowKemp, that your example was very helpful for me to get my UART RX routine working well on the CY8CKIT-147.Thank you!