4 Replies Latest reply on May 5, 2020 2:47 AM by MoTa_728816

    Can not send all senteced with UART on ISR

    ShYo_4617686

      Hi everybody.

      Please advise me an my trouble.

      I try to send a letter through UART.

      This program work on ISR interrupt routine by 50Hz.

      I could receive a serial data only 4 words which is "ABCD".

      Why can not receive all sentences?

       

      CY_ISR(ctrl_loop) {     //50Hz

         :

         :

          UART_1_PutString("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n\r");
          CyDelay(1);
      }

       

      void main(){

          HardwareReset();

           CYGlobalIntEnable; // Enable Global Interrupts

          isr_1_StartEx(ctrl_loop);

          for(;;){

           }

       

      PC terminal show only "ABCD"

      115200 bps

        • 1. Re: Can not send all senteced with UART on ISR
          MoTa_728816

          Hi,

           

          In general an ISR must be returned as soon as possible

          to avoid disturbing other activities in the program and/or the MCU.

           

          And because of its nature, usually a function  which allocates memory

          or takes very long time is not supposed to be in an ISR.

           

          Having said that, when I modified your ISR to

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

          CY_ISR(timer_isr)

          {

              Timer_1_ReadStatusRegister() ;

              UART_1_PutString("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n\r");

              CyDelay(10) ;

          }

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

           

          It worked.

          002-testB.JPG

           

          But to prevent troubles in the future, I would recommend to write a program something like

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

          volatile int timer_flag = 0 ;

           

          CY_ISR(timer_isr)

          {

              Timer_1_ReadStatusRegister() ;

              timer_flag = 1 ;

          }

           

          int main(void)

          {

              CyGlobalIntEnable; /* Enable global interrupts. */

              UART_1_Start() ;

             

              splash("5LP 50Hz Timer Test A") ;

                 

              isr_1_StartEx(timer_isr) ;

              Timer_1_Start() ;

             

              for(;;)

              {

                  if (timer_flag) {

                      timer_flag = 0 ;

                      UART_1_PutString("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n\r");

                      CyDelay(1) ;

                  }

              }

          }

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

           

          Result Tera Term log

          001-testA.JPG

           

          Attached are my samples using CY8CKIT-059

          uart_50Hz_200505A: recommended

          uart_50Hz_200505B: worked, but not recommended.

           

          moto

          • 2. Re: Can not send all senteced with UART on ISR
            ShYo_4617686

            Moto san

            Thank you very much for so quick response.

            I understood it, but I did not use timer block in order to make interrupt signal.

            May be should I use timer block?

             

            • 3. Re: Can not send all senteced with UART on ISR
              MoTa_728816

              Hi,

               

              Actually I didn't care about how you generate 50Hz interrupt, but your method also works.

              003-testC.JPG

               

              moto

              • 4. Re: Can not send all senteced with UART on ISR
                MoTa_728816

                Hi,

                 

                Actually using a timer for 50Hz event may not be efficient.

                As you might know, Cortex-M has a timer called "SysTick" which can be used without consuming other resource(s).

                 

                So following is another sample using "SysTick".

                 

                schematic

                Note: This time we don't need a counter nor clock and interrupt.

                005-schematic.JPG

                pins

                006-pins.JPG

                main.c

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

                #include "project.h"

                #include "stdio.h"

                 

                #define STR_LEN 64

                char    str[STR_LEN+1] ;

                void    print(char *str)

                {

                // UART_UartPutString(str) ; /* PSoC 4 */

                UART_1_PutString(str) ;     /* PSoC 5 */

                }

                 

                void cls(void)

                {

                    print("\033c") ; /* reset */

                    CyDelay(20) ;

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

                    CyDelay(20) ;

                }

                 

                void splash(char *prog_name)

                {

                    cls() ;

                    if (prog_name && *prog_name) {

                        print(prog_name) ;

                    }

                    print(" (") ;

                    print(__DATE__) ;

                    print(" ") ;

                    print(__TIME__) ;

                    print(")\n") ;

                }

                 

                volatile int timer_flag = 0 ;

                volatile uint32_t tick_count = 0 ;

                 

                CY_ISR(tick_callback)

                {

                    tick_count++ ;

                    if ((tick_count % 20) == 0) {

                        timer_flag = 1 ; /* 50Hz hit! */

                    }

                }

                 

                int find_empty_slot(void)

                {

                    int result = -1 ;

                    uint32_t i ;

                    for (i = 0 ; i < CY_SYS_SYST_NUM_OF_CALLBACKS ; i++ ) {

                        if (CySysTickGetCallback(i) == NULL) {

                            result = i ;

                            break ;

                        }

                    }

                    return(result) ;

                }

                 

                int main(void)

                {

                    int sys_tick_slot = 0 ;

                   

                    CyGlobalIntEnable; /* Enable global interrupts. */

                    UART_1_Start() ;

                   

                    splash("5LP 50Hz Timer Test D") ;

                   

                    sys_tick_slot = find_empty_slot() ;

                 

                    if (sys_tick_slot < 0) {

                        print("Sorry No empty SysTick Slot available\n\r") ;

                        while(1) { } /* halting here */

                    } else {

                        snprintf(str, STR_LEN, "Starting Tick at slot %d\n\r", sys_tick_slot) ;

                        print(str) ;

                        CySysTickStart() ;

                        CySysTickSetCallback(sys_tick_slot, tick_callback) ;

                    }

                       

                    for(;;)

                    {

                        if (timer_flag) {

                            timer_flag = 0 ;

                            UART_1_PutString("ABCDEFGHIJKLMNOPQRSTUVWXYZ\n\r");

                            CyDelay(1) ;

                        }

                    }

                }

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

                 

                Tera Term log

                004-testD.JPG

                moto