Author Topic: Cypress Gurus..Please Help Me with PSOC 4200 UART RX Code  (Read 3096 times)

0 Members and 1 Guest are viewing this topic.

Offline SuzyCTopic starter

  • Frequent Contributor
  • **
  • Posts: 792
Cypress Gurus..Please Help Me with PSOC 4200 UART RX Code
« on: December 05, 2018, 04:04:58 pm »
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[48] 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 Error

 I 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[48];
static char j;

// in main()

    if(UA_CHECK_CAUSE_INTR(UA_INTR_RX_FULL))
     {
       for(j=0;j<48;j++)
        { Rx[j]=UA_UartGetByte();
        }
       UA_rx_ClearInterrupt();
     }   
    else if(UA_CHECK_CAUSE_INTR(UA_INTR_RX_ALL))
     {
        UA_rx_ClearInterrupt();
        UA_Stop();
        UA_Start();
     }
« Last Edit: December 05, 2018, 05:22:08 pm by SuzyC »
 

Offline ebclr

  • Super Contributor
  • ***
  • Posts: 2328
  • Country: 00
Re: Cypress Gurus..Please Help Me with PSOC 4200 UART RX Code
« Reply #1 on: December 18, 2018, 12:06:05 am »
 This mays helps

 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Cypress Gurus..Please Help Me with PSOC 4200 UART RX Code
« Reply #2 on: December 19, 2018, 02:28:11 am »
I am not a PSOC guru, but...
Quote
I have set the UART circular buffer RX FIFO buffer size to 48 bytes.
Why?  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 ...
Quote
Code: [Select]
// in main()  if(UA_CHECK_CAUSE_INTR(UA_INTR_RX_FULL))    else if(UA_CHECK_CAUSE_INTR(UA_INTR_RX_ALL))
        UA_rx_ClearInterrupt();
I 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."

Quote
Code: [Select]
        UA_Stop();
        UA_Start();
There usually isn't any reason to stop and restart a UART.

 

Offline ShowKemp

  • Contributor
  • Posts: 16
  • Country: ls
Re: Cypress Gurus..Please Help Me with PSOC 4200 UART RX Code
« Reply #3 on: December 19, 2018, 05:02:53 am »
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: [Select]
#include <stddef.h>
#include <stdbool.h>

#include "project.h"

#define BUFFER_SIZE 48

size_t buffer_index = 0;
char buffer[BUFFER_SIZE] = {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[i]);
            }
           
            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[buffer_index] = 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 */
 

Offline obiwanjacobi

  • Frequent Contributor
  • **
  • Posts: 988
  • Country: nl
  • What's this yippee-yayoh pin you talk about!?
    • Marctronix Blog
Re: Cypress Gurus..Please Help Me with PSOC 4200 UART RX Code
« Reply #4 on: December 19, 2018, 06:53:21 am »
Here is my PSoC5 project that also uses an UART (PSoC Creator 4.2).
https://github.com/obiwanjacobi/Zalt/tree/master/Source/SystemController

Perhaps 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.

[2c]
Arduino Template Library | Zalt Z80 Computer
Wrong code should not compile!
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Cypress Gurus..Please Help Me with PSOC 4200 UART RX Code
« Reply #5 on: December 24, 2018, 09:11:20 am »
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(?)
 

Offline henmill

  • Newbie
  • Posts: 9
  • Country: us
Re: Cypress Gurus..Please Help Me with PSOC 4200 UART RX Code
« Reply #6 on: July 08, 2022, 08:48:06 pm »
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!
 
The following users thanked this post: SuzyC


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf