1 Reply Latest reply on May 21, 2020 10:26 PM by TakashiM_61

    Measuring time interval using SysTick (CY8CKIT-059/CY8CKIT-044)

    MoTa_728816

      Hi,

      こんにちは、

       

      I found a community question asking how to measure time taken for a particular function.

      ある関数の実行時間を測定したいという質問を見かけました。

       

      Although there is a very nice KBA, but it is assuming CapSense, may be adding another sample using a gpio (switch) won't harm.

      下記の素晴らしい KBA が既にあるのですが、対象が CapSense になっているので、GPIO (スイッチ) 版も有っていいかなと。

      Measuring Duration of CapSense Button Press Using SysTickTimer - KBA226863

       

      And also there is a nice stopwatch implementation of /odissey1-san, but I could not find the URL, could you reply one or you master piece here, too? > Odissey1-san

      また、/odissey-1 さんの高機能なストップウォッチもあるのですが、URL を見つけられませんでした。 URL か添付ファイルをお願いできますか? > Odissey1-san

       

      At least, I hope that I can reference this later ;-)

      少なくとも後で自分用に見つけられますし・・・

       

      So, I made samples for CY8CKIT-044 and CY8CKIT-059.

      CY8CKIT-044 と CY8CKIT-059 用にサンプルを作成してみました。

       

      === CY8CKIT-059 ===

      schematic

      002-schematic.JPG

      pins

      003-pins.JPG

      main.c

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

      #include "project.h"

      #include "stdio.h"

       

      #define STR_LEN 64

      char str[STR_LEN+1] ;

      void print(char *str)

      {

          UART_PutString(str) ;

      }

       

      void cls(void)

      {

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

          CyDelay(100) ;

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

          CyDelay(100) ;

      }

       

      void splash(char *title)

      {

          cls() ;

          if (title && *title) {

              print(title) ;

              print(" ") ;

          }

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

          print(str) ;

      }

       

      volatile int sw_flag ;

      volatile uint32_t tick_count = 0 ;

       

      CY_ISR(sw_isr)

      {

          SW1_ClearInterrupt() ;

          sw_flag = 1 ;

      }

       

      CY_ISR(tick_callback)

      {

          tick_count++ ;

      }

       

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

      }

             

      void show_delta_time(void)

      {

          static uint32_t prev_tick = 0 ;

          uint32_t new_tick = 0 ;

          uint32_t delta = 0 ;

         

          new_tick = tick_count ;

          delta = new_tick - prev_tick ;

          prev_tick = new_tick ;

          snprintf(str, STR_LEN, "Current: %d -> %d ms passed\n\r", new_tick, delta) ;

          print(str) ;

      }

       

      int main(void)

      {  

          int sys_tick_slot = 0 ;

         

          CyGlobalIntEnable; /* Enable global interrupts. */

          UART_Start() ;

         

          isr_1_StartEx(sw_isr) ;

       

          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 {

              CySysTickStart() ;

              CySysTickSetCallback(sys_tick_slot, tick_callback) ;

          }

         

          splash("Timer test with SysTick") ;

          print("Measuring time between SW1 push\n\r") ;

         

          for(;;)

          {

              if (sw_flag) {

                  show_delta_time() ;

                  sw_flag = 0 ;   

              }

          }

      }

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

       

      Tera Term log

      001-teraterm-log.JPG

       

      === CY8CKIT-044 ===

      schematic

      011-Schematic.JPG

      pins

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

      }

       

      void cls(void)

      {

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

          CyDelay(100) ;

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

          CyDelay(100) ;

      }

       

      void splash(char *title)

      {

          cls() ;

          if (title && *title) {

              print(title) ;

              print(" ") ;

          }

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

          print(str) ;

      }

       

      volatile int sw_flag ;

      volatile uint32_t tick_count = 0 ;

       

      CY_ISR(sw_isr)

      {

          SW2_ClearInterrupt() ;

          sw_flag = 1 ;

      }

       

      CY_ISR(tick_callback)

      {

          tick_count++ ;

      }

       

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

      }

             

      void show_delta_time(void)

      {

          static uint32_t prev_tick = 0 ;

          uint32_t new_tick = 0 ;

          uint32_t delta = 0 ;

         

          new_tick = tick_count ;

          delta = new_tick - prev_tick ;

          prev_tick = new_tick ;

          snprintf(str, STR_LEN, "Current: %d -> %d ms passed\n\r", new_tick, delta) ;

          print(str) ;

      }

       

      int main(void)

      {  

          int sys_tick_slot = 0 ;

         

          CyGlobalIntEnable; /* Enable global interrupts. */

          UART_Start() ;

         

          isr_1_StartEx(sw_isr) ;

       

          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 {

              CySysTickStart() ;

              CySysTickSetCallback(sys_tick_slot, tick_callback) ;

          }

         

          splash("Timer test with SysTick") ;

          print("Measuring time between SW1 push\n\r") ;

         

          for(;;)

          {

              if (sw_flag) {

                  show_delta_time() ;

                  sw_flag = 0 ;   

              }

          }

      }

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

       

      Tera Term log

      010-TeraTerm-log.JPG

      moto