0 Replies Latest reply on Jun 25, 2020 10:54 PM by MoTa_728816

    PSoC 4 Simple Samples for UART input a line (CY8CKIT-044)

    MoTa_728816

      Hi,

       

      Although I have posted tty utils before it may not be simple enough to be used.

      以前に tty utils をポストしてはいたのですが、簡潔さが不充分と感じたため

      Re: tty_utils a utility sample for CLI type program

       

      So I posted a line read sample for PSoC 5LP.

      昨夜、UART からの 1行読み込みのサンプルを PSoC 5LP 用に書いてポストしました。

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

       

      And here is a version for PSoC 4.

      で、折角なので PSoC 4 版も作成してみました。

       

      The followings are a version using polling and a version using interrupt.

      以下、ポーリング版と割込み利用版です。

       

      (1) Polling Version / ポーリング版

       

      schematic

      005-schematic.JPG

      pins

      006-pins.JPG

       

      Tera Term log

      004-tera-term-polling.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_UartPutString(str) ;

      }

       

      void printc(char c)

      {

          UART_UartPutChar(c) ;

      }

       

      int line_received = 0 ;

      int rx_index = 0 ;

       

      int get_line(void)

      {

          uint8_t c ;

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

              c = UART_UartGetByte() ;

              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() ;

              }

          }

      }

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

       

      (2) Interrupt version / 割込み使用版

       

      schematic

      002-schematic.JPG

       

      pins

      003-pins.JPG

       

      Tera Term log

      001-tera-term-interrupt.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_UartPutString(str) ;

          while(UART_SpiUartGetTxBufferSize() > 0) ;

      }

       

      void printc(char c)

      {

          UART_UartPutChar(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_GetRxInterruptSource() ;

          if (status & UART_INTR_RX_NOT_EMPTY){

              c = UART_UartGetByte() ;

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

                  rx_buf[rx_write_index] = c ;

                  rx_write_index = (rx_write_index + 1) % RX_BUF_LEN ;

              }

          }

          UART_ClearRxInterruptSource(status) ;

      }

       

      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. */

        

          tty_rx_int_ClearPending() ;

          tty_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() ;

              }

          }

      }

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

       

      26-Jun-2020

      moto