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

A sample of emulating millis() from arduino in PSoC (CY8CKIT-044/CY8CKIT-059)

MotooTanaka
Level 9
First comment on KBA 1000 replies posted 750 replies posted
Level 9

Hi,

こんにちは、

 

I found the following discussion

下記のようなスレッドを見つけました

https://community.infineon.com/t5/PSoC-5-3-1/a-way-to-use-millies-from-arduino-in-Psoc/td-p/328317

 

and learned that my sample below was not enough.

以前上げた下記のサンプルでは不十分だったと知りました。

https://community.infineon.com/t5/Code-Examples/Measuring-time-interval-using-SysTick-CY8CKIT-059-CY...

 

So here it is, an emulated "millis()" function of arduino in PSoC 😉

というわけで、Arduino の "millis()" という関数の真似をしてみました。

 

As I checked the Arduino Online Reference, the usage of millis() was

Arduino のオンラインリファレンスを参照したところ、millis() の使い方は下記のようなものでした

 

unsigned long time;

void setup() {
  Serial.begin(9600);
}

void loop() {
  time = millis();
  Serial.println(time);

  delay(1000); // 1秒おきに送信
}

 

So I changed the main loop something like

それで、こちらのメインループの以下のような形で動くようにしてみました

 

    for(;;)
    {
        time = millis() ;
        print_time(time) ;
        
        CyDelay(1000) ; 
    }

 

 

To my previous sample, I added a couple of functions

以前のサンプルに、下記2つの関数を追加しました。

 

1. millis()

 

uint32_t millis(void)
{
    return( tick_count ) ;
}

 

2. clear_millis()

 

void clear_millis(void)
{
    tick_count = 0 ;
}

 

Schematic 

002-schematic.JPG

CY8CKIT-044

005-059-schematic.JPG

CY8CKIT-059

 

Pins

003-pins.JPG

CY8CKIT-044

006-059_pins.JPG

CY8CKIT-055

Tera Term log

CY8CKIT-044

001-044_tera_term_log.JPG

CY8CKIT-059

004-059_tera_term_log.JPG

main.c

CY8CKIT-044

 

#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 uint32_t tick_count = 0 ;

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

}

void clear_millis(void)
{
    tick_count = 0 ;
}

uint32_t millis(void)
{
    return( tick_count ) ;
}

void print_time(uint32_t value)
{
    char buf[65] ;
    
    snprintf(buf, 64, "millis: %u\n\r", value) ;
    print(buf) ;
}

int main(void)
{   
    uint32_t time = 0 ;
    int sys_tick_slot = 0 ;
    
    CyGlobalIntEnable; /* Enable global interrupts. */
    UART_Start() ;

    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("millis() test with SysTick") ;
    
    clear_millis() ; // optional
    
    for(;;)
    {
        time = millis() ;
        print_time(time) ;
        
        CyDelay(1000) ; 
    }
}

 

 

CY8CKIT-059

 

#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 uint32_t tick_count = 0 ;

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

}

void clear_millis(void)
{
    tick_count = 0 ;
}

uint32_t millis(void)
{
    return( tick_count ) ;
}

void print_time(uint32_t value)
{
    char buf[65] ;
    
    snprintf(buf, 64, "millis: %u\n\r", value) ;
    print(buf) ;
}

int main(void)
{   
    uint32_t time = 0 ;
    int sys_tick_slot = 0 ;
    
    CyGlobalIntEnable; /* Enable global interrupts. */
    UART_Start() ;

    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("millis() test with SysTick") ;
    
    clear_millis() ; // optional
    
    for(;;)
    {
        time = millis() ;
        print_time(time) ;
        
        CyDelay(1000) ; 
    }
}

 

 

Hope this can be some use for those with need of "millis()" 😉

これで“millis()” を使いたい方の助けになると良いのですが。 (^_^)v

 

17-Dec-2021

moto

0 Likes
0 Replies