Hi Espressif :
Here is one data structure os_timer_t that I hope to get the time differences for measuring
when one callback function is set and triggered, then finished and disarm.
Can you give me a simple example to show how to use the os_timer_t structure to get
time difference information ? Or any suggested solutions that can help me to measure the time
difference. Thank you !
os_timer_t usage ?
-
- Posts: 48
- Joined: Wed May 04, 2016 7:32 pm
-
- Posts: 48
- Joined: Wed May 04, 2016 7:32 pm
Re: os_timer_t usage ?
Postby PaulTsai111 » Thu Jun 02, 2016 3:30 pm
Hi Espressif :
Below is one example that I used last day for testing the system_get_time() api.
But After I provided the bin files and testing on the IoT Ref Compatible Board,
I can not seen any system time information and timediff information.
Where I miss or wrong when employ this api ?
#include <user_buttons.h>
#define PIN_IN 2
#define GPIO_OUT_PIN 13
#define value 1
#define CHECK_BIT(var,pos) ((var) & (1 << (pos)))
//LOCAL void button_init();
//LOCAL uint32_t PIN_IN;
LOCAL uint8 button_last_state = 0;
LOCAL os_timer_t rotary_debounce_timer;
LOCAL os_timer_t button_timer;
void ICACHE_FLASH_ATTR button_push(uint8 debounce){
uint32 Starttime = system_get_time();
if(debounce == 1 ){
os_printf("start debouncing !\n");
os_timer_disarm(&button_timer);
os_timer_setfn(&button_timer,(os_timer_func_t *)button_push,0);
os_timer_arm(&button_timer, 100, 1);
}
if(CHECK_BIT(PIN_IN,0) == 0 && button_last_state == 0)
{
os_printf("Button press\n");
os_timer_disarm(&button_timer);
os_timer_setfn(&button_timer,(os_timer_func_t *)button_push,1);
os_timer_arm(&button_timer,1000,0);
button_last_state = 1;
}
if(CHECK_BIT(PIN_IN,0)== 1 && button_last_state == 1 )
{
os_printf("stop debouncing !\n");
uint32 Timediff = system_get_time()-Starttime ;
button_last_state = 0;
os_printf("time diff: %d\n",Timediff);
}
}
LOCAL void ICACHE_FLASH_ATTR rotary_debounce(uint8 direction)
{
gpio_pin_intr_state_set(GPIO_ID_PIN(13), GPIO_PIN_INTR_ANYEDGE);
if (direction ==1)
os_printf("Up\n");
else
os_printf("Down\n");
}
LOCAL void ICACHE_FLASH_ATTR rotary_intr_handler(int8_t key)
{
uint8 direction = 0;
uint32 inputs;
inputs = PIN_IN; // read datas from PIN_IN to inputs
if (CHECK_BIT(inputs,13) == CHECK_BIT(inputs,12)) // check bit 12 and bit 13 is equal ?
direction = 1;
os_printf("check bit 12 13 equal ?\n");
uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
gpio_pin_intr_state_set(GPIO_ID_PIN(13), GPIO_PIN_INTR_DISABLE);
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(13));
os_timer_disarm(&rotary_debounce_timer);
os_timer_setfn(&rotary_debounce_timer,rotary_debounce,direction);
os_timer_arm(&rotary_debounce_timer, 350, 0);
}
LOCAL void ICACHE_FLASH_ATTR buttons_init()
{
os_printf("button_init start !\n");
// Register an interrupt handler
ETS_GPIO_INTR_ATTACH(rotary_intr_handler,GPIO_OUT_PIN);
// Disable interrupts
ETS_GPIO_INTR_DISABLE();
// set which chip io maps to which pad io
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U,FUNC_GPIO13);
// set the gpio to input
gpio_out_set(GPIO_OUT_PIN,value);
// set gpio registers
gpio_register_set(GPIO_PIN_ADDR(GPIO_OUT_PIN), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE)| GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE)| GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE));
// clear gpio status
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS,BIT(GPIO_OUT_PIN));
// re-enable gpio interrupt
gpio_pin_intr_state_set(GPIO_ID_PIN(GPIO_OUT_PIN),GPIO_PIN_INTR_ANYEDGE);
// global re- enable interrupts
ETS_GPIO_INTR_ENABLE();
os_printf("gpio_interrupt_enable completed !\n");
// set timer when button_push events call backs , the timer scale is 50ms , restarts when timer to be 0
os_timer_disarm(&button_timer);
os_timer_setfn(&button_timer,(os_timer_func_t *)button_push,1);
os_timer_arm(&button_timer,50,1);
}
void user_init(void)
{
os_printf("SDK Version: %s\n",system_get_sdk_version());
buttons_init();
}
Below is one example that I used last day for testing the system_get_time() api.
But After I provided the bin files and testing on the IoT Ref Compatible Board,
I can not seen any system time information and timediff information.
Where I miss or wrong when employ this api ?
#include <user_buttons.h>
#define PIN_IN 2
#define GPIO_OUT_PIN 13
#define value 1
#define CHECK_BIT(var,pos) ((var) & (1 << (pos)))
//LOCAL void button_init();
//LOCAL uint32_t PIN_IN;
LOCAL uint8 button_last_state = 0;
LOCAL os_timer_t rotary_debounce_timer;
LOCAL os_timer_t button_timer;
void ICACHE_FLASH_ATTR button_push(uint8 debounce){
uint32 Starttime = system_get_time();
if(debounce == 1 ){
os_printf("start debouncing !\n");
os_timer_disarm(&button_timer);
os_timer_setfn(&button_timer,(os_timer_func_t *)button_push,0);
os_timer_arm(&button_timer, 100, 1);
}
if(CHECK_BIT(PIN_IN,0) == 0 && button_last_state == 0)
{
os_printf("Button press\n");
os_timer_disarm(&button_timer);
os_timer_setfn(&button_timer,(os_timer_func_t *)button_push,1);
os_timer_arm(&button_timer,1000,0);
button_last_state = 1;
}
if(CHECK_BIT(PIN_IN,0)== 1 && button_last_state == 1 )
{
os_printf("stop debouncing !\n");
uint32 Timediff = system_get_time()-Starttime ;
button_last_state = 0;
os_printf("time diff: %d\n",Timediff);
}
}
LOCAL void ICACHE_FLASH_ATTR rotary_debounce(uint8 direction)
{
gpio_pin_intr_state_set(GPIO_ID_PIN(13), GPIO_PIN_INTR_ANYEDGE);
if (direction ==1)
os_printf("Up\n");
else
os_printf("Down\n");
}
LOCAL void ICACHE_FLASH_ATTR rotary_intr_handler(int8_t key)
{
uint8 direction = 0;
uint32 inputs;
inputs = PIN_IN; // read datas from PIN_IN to inputs
if (CHECK_BIT(inputs,13) == CHECK_BIT(inputs,12)) // check bit 12 and bit 13 is equal ?
direction = 1;
os_printf("check bit 12 13 equal ?\n");
uint32 gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);
gpio_pin_intr_state_set(GPIO_ID_PIN(13), GPIO_PIN_INTR_DISABLE);
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status & BIT(13));
os_timer_disarm(&rotary_debounce_timer);
os_timer_setfn(&rotary_debounce_timer,rotary_debounce,direction);
os_timer_arm(&rotary_debounce_timer, 350, 0);
}
LOCAL void ICACHE_FLASH_ATTR buttons_init()
{
os_printf("button_init start !\n");
// Register an interrupt handler
ETS_GPIO_INTR_ATTACH(rotary_intr_handler,GPIO_OUT_PIN);
// Disable interrupts
ETS_GPIO_INTR_DISABLE();
// set which chip io maps to which pad io
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTCK_U,FUNC_GPIO13);
// set the gpio to input
gpio_out_set(GPIO_OUT_PIN,value);
// set gpio registers
gpio_register_set(GPIO_PIN_ADDR(GPIO_OUT_PIN), GPIO_PIN_INT_TYPE_SET(GPIO_PIN_INTR_DISABLE)| GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_DISABLE)| GPIO_PIN_SOURCE_SET(GPIO_AS_PIN_SOURCE));
// clear gpio status
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS,BIT(GPIO_OUT_PIN));
// re-enable gpio interrupt
gpio_pin_intr_state_set(GPIO_ID_PIN(GPIO_OUT_PIN),GPIO_PIN_INTR_ANYEDGE);
// global re- enable interrupts
ETS_GPIO_INTR_ENABLE();
os_printf("gpio_interrupt_enable completed !\n");
// set timer when button_push events call backs , the timer scale is 50ms , restarts when timer to be 0
os_timer_disarm(&button_timer);
os_timer_setfn(&button_timer,(os_timer_func_t *)button_push,1);
os_timer_arm(&button_timer,50,1);
}
void user_init(void)
{
os_printf("SDK Version: %s\n",system_get_sdk_version());
buttons_init();
}
Who is online
Users browsing this forum: No registered users and 13 guests
Login
Newbies Start Here
Are you new to ESP8266?
Unsure what to do?
Dunno where to start?
Start right here!
Latest SDK
Documentation
Complete listing of the official ESP8266 related documentation release by ESPRESSIF!
Must read here!
- All times are UTC+08:00
- Top
- Delete all board cookies
About Us
Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. We are the manufacturer of ESP8266EX.