Nordic 定时器系统app timer[获取时间戳]
获取时间戳
想要在Nordic 定时器系统中获取时间戳,也就是是在调用app_timer的时候时间戳要有效,我们可以看看定时器系统初始化:
ret_code_t app_timer_init(void)
{ret_code_t err_code;drv_rtc_config_t config = {.prescaler = APP_TIMER_CONFIG_RTC_FREQUENCY,.interrupt_priority = APP_TIMER_CONFIG_IRQ_PRIORITY};err_code = NRF_ATFIFO_INIT(m_req_fifo);if (err_code != NRFX_SUCCESS){return err_code;}err_code = drv_rtc_init(&m_rtc_inst, &config, rtc_irq);if (err_code != NRFX_SUCCESS){return err_code;}drv_rtc_overflow_enable(&m_rtc_inst, true);drv_rtc_compare_set(&m_rtc_inst, 1, DRV_RTC_MAX_CNT >> 1, true);if (APP_TIMER_KEEPS_RTC_ACTIVE){drv_rtc_start(&m_rtc_inst);}m_global_active = true;return err_code;
}
上面有一个关键的宏就是:APP_TIMER_KEEPS_RTC_ACTIVE
从字面意思看到当这个宏定义为1的时候在APP time都没有的时候RTC的计数依旧有效,这里打开这个宏设置为1:
在使用的时候获取一个时间戳初值:
if (p_evt->params.rx_data.p_data[0] == 'A'){NRF_LOG_DEBUG("Notify performance test start");timestamp_start=drv_rtc_counter_get(&m_rtc_inst);test_state=1;}
然后再要计算经历了多少时间的时候相减就好:
void data_arge( uint8_t *data_array)
{static uint32_t counter=0;uint32_t timestamp=drv_rtc_counter_get(&m_rtc_inst);timestamp=timestamp-timestamp_start;timestamp=(float)timestamp/16.384;int len=sprintf((char *)data_array, "duration time:%dms total_byte:%d mtu:%d speed:%dByte/s ", timestamp,counter,m_ble_nus_max_data_len,counter*1000/timestamp);for(int i=len;i<BLE_NUS_MAX_DATA_LEN;i++){data_array[i]='X';}counter+=m_ble_nus_max_data_len;
}
这里是想计算出毫秒来,上面为什么是除以16.384呢,是因为RTC初始化定义的频率APP_TIMER_CONFIG_RTC_FREQUENCY为16384 Hz :
这样就得到ms的时间戳了!