PSoC 5LP Simple Samples for Uart input in char array save

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

cross mob
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Today I noticed that my response to the question below was not kind enough,

especially for those new to PSoC.

Uart input in char array save

So I wrote a couple of simpler samples for UART input char into array using CY8CKIT-059.

One is using polling and the other is using an interrupt.

I hope these are easier to understand comparing with the sample I suggested in the question.

(1) Polling mode (without using interrupt)

Note: This actually utilizes the component's internal interrupt, but from the view point of the programmer, it's a polling mode. (I hope)

schematic

001-polling_schematic.JPG

UART config

007-polling-uart-cnfig1.JPG

006-polling-UART-config2.JPG

pins

002-polling-pins.JPG

main.c

==============

#include "project.h"

#include "stdio.h"

#define CR '\r'

#define NL '\n'

#define BS '\b'

#define RX_BUF_LEN 64

char rx_buf[RX_BUF_LEN+1] ; /* 1byte for NULL */

void print(char *str)

{

    UART_PutString(str) ;

}

void printc(char c)

{

    UART_PutChar(c) ;

}

int line_received = 0 ;

int rx_index = 0 ;

int get_line(void)

{

    uint8_t c ;

    while((line_received == 0) && (UART_GetRxBufferSize() > 0)) {

        c = UART_GetByte() ;

        switch(c) {

        case 0x00:

        case 0xFF:

            break ;

        case BS: /* backspace */

            if (rx_index > 1) {

                rx_index-- ;

                rx_buf[rx_index] = 0 ;

                print("\b \b") ;

            }

            break ;

        case NL:

        case CR:

            printc(c) ;

            rx_buf[rx_index] = 0 ;

            rx_index = 0 ;

            line_received = 1 ;

            break ;

        default:

            printc(c) ;

            rx_buf[rx_index] = c ;

            rx_index++ ;

            if (rx_index >= RX_BUF_LEN) { /* buffer overflow */

                rx_buf[RX_BUF_LEN] = 0 ; /* terminate the string */

                line_received = -1 ; /* not 0 but not 1 as this is error */

                rx_index = 0 ;

            }

        }

    }

    return(line_received) ;

}

void prompt(void)

{

    print("> ") ;

}

int main(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start() ;

 

    print("\x1b[2J\x1b[;H") ; /* clear screen */

    print("UART polling test ") ;

    snprintf(rx_buf, RX_BUF_LEN, "(%s %s)\n", __DATE__, __TIME__) ;

    print(rx_buf) ;

 

    prompt() ;

    for(;;)

    {

        if (get_line()) {

            print("Line: ") ;

            print(rx_buf) ;

            print("\n") ;

            line_received = 0 ;

            prompt() ;

        }

    }

}

==============

Tera Term log

003-teraterm-log.JPG

(2) Using an interrupt

schematic

002-interrupt-schematic.JPG

UART config

008-interrupt-UART-config1.JPG

009-interrupt-UART-config2.JPG

pins

Note: pins are same with (1)

005-interrupt-pins.JPG

main.c

==================

#include "project.h"

#include "stdio.h"

#define CR '\r'

#define NL '\n'

#define BS '\b'

#define RX_BUF_LEN 64

volatile char rx_buf[RX_BUF_LEN] ;

#define STR_LEN 32

char str[STR_LEN+1] ; /* 1 byte for NULL */

void print(char *str)

{

    UART_PutString(str) ;

    while(UART_GetTxBufferSize() > 0) ;

}

void printc(char c)

{

    UART_PutChar(c) ;

}

volatile int line_received = 0 ;

volatile int rx_write_index = 0 ;

int rx_read_index = 0 ;

int str_index = 0 ;

CY_ISR(uart_isr)

{

    uint32_t status ;

    uint8_t c ;

  

    status = UART_ReadRxStatus() ;

    if (status & UART_RX_STS_FIFO_NOTEMPTY){

        c = UART_ReadRxData() ;

        if ((c != 0x00) && (c != 0xFF)) {

            rx_buf[rx_write_index] = c ;

            rx_write_index = (rx_write_index + 1) % RX_BUF_LEN ;

        }

    }

}

int get_line(void)

{

    uint8_t c ;

    while((line_received == 0)&&(rx_read_index != rx_write_index)) {

        c = rx_buf[rx_read_index] ;

        switch(c) {

        case 0x00:

        case 0xFF:

            break ;

        case BS: /* backspace */

            if (str_index > 0) {

                str_index-- ;

                str[str_index] = 0 ;

                print("\b \b") ;

            }

            break ;

        case NL:

        case CR:

            printc(c) ;

            str[str_index] = 0 ;

            str_index = 0 ;

            line_received = 1 ;

            break ;

        default:

            printc(c) ;

            str[str_index] = c ;

            str_index++ ;

            if (str_index >= STR_LEN) { /* buffer overflow */

                str[STR_LEN] = 0 ; /* terminate the string */

                line_received = -1 ; /* not 0 but not 1 as this is error */

                str_index = 0 ;

            }

        }

        rx_read_index = (rx_read_index + 1) % RX_BUF_LEN ;

    }

    return(line_received) ;

}

void prompt(void)

{

    print("> ") ;

}

int main(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

  

    uart_rx_int_ClearPending() ;

    uart_rx_int_StartEx(uart_isr) ;

    UART_Start() ;

  

    print("\x1b[2J\x1b[;H") ; /* clear screen */

    print("UART interrupt test ") ;

    snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

  

    prompt() ;

    for(;;)

    {

        if (get_line()) {

            print("Line: ") ;

            print(str) ;

            print("\n") ;

            line_received = 0 ;

            prompt() ;

        }

    }

}

==================

Tera Term log

004-teraterm-log-interrupt.JPG

moto

P.S. (edited) UART config screen shots were added.

0 Replies