123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #include <string.h>
- #include "app_timer.h"
- #include "es_tlm.h"
- #include "es_app_config.h"
- #include "es_battery_voltage.h"
- #include "es_stopwatch.h"
- #include "nrf_soc.h"
- #define TICKS_100_MS APP_TIMER_TICKS(100)
- static es_tlm_frame_t m_tlm;
- static uint32_t m_le_adv_cnt;
- static es_stopwatch_id_t m_time_sec_sw_id;
- static es_stopwatch_id_t m_tlm_refresh_sw_id;
- static void update_time(void)
- {
- static uint32_t time_total_100_ms = 0;
- uint32_t be_time_100_ms;
- time_total_100_ms += es_stopwatch_check(m_time_sec_sw_id);
- be_time_100_ms = BYTES_REVERSE_32BIT(time_total_100_ms);
- memcpy(m_tlm.sec_cnt, &be_time_100_ms, ES_TLM_SEC_CNT_LENGTH);
- }
- static void update_temp(void)
- {
- int32_t temp;
- (void)sd_temp_get(&temp);
- int16_t temp_new = (int16_t) temp;
- m_tlm.temp[0] = (uint8_t)((temp_new >> 2) & 0xFFUL);
- m_tlm.temp[1] = (uint8_t)((temp_new << 6) & 0xFFUL);
- }
- static void update_vbatt(void)
- {
- uint16_t vbatt;
- es_battery_voltage_get(&vbatt);
- m_tlm.vbatt[0] = (uint8_t)(vbatt >> 8);
- m_tlm.vbatt[1] = (uint8_t)vbatt;
- }
- static void update_adv_cnt(void)
- {
- uint32_t be_adv_cnt = BYTES_REVERSE_32BIT(m_le_adv_cnt);
- memcpy(m_tlm.adv_cnt, (uint8_t *)(&be_adv_cnt), ES_TLM_ADV_CNT_LENGTH);
- }
- void es_tlm_tlm_get(es_tlm_frame_t * p_tlm_frame)
- {
-
- update_time();
- update_adv_cnt();
- if (es_stopwatch_check(m_tlm_refresh_sw_id) > 0)
- {
- update_temp();
- update_vbatt();
- }
- memcpy(p_tlm_frame, &m_tlm, sizeof(es_tlm_frame_t));
- }
- void es_tlm_adv_cnt_inc(void)
- {
- m_le_adv_cnt++;
- }
- void es_tlm_init(void)
- {
- ret_code_t err_code;
- memset(&m_tlm, 0, sizeof(m_tlm));
- m_tlm.frame_type = ES_FRAME_TYPE_TLM;
- m_tlm.version = ES_TLM_VERSION_TLM;
- m_le_adv_cnt = 0;
- update_time();
- update_vbatt();
- update_temp();
- err_code = es_stopwatch_create(&m_time_sec_sw_id, APP_TIMER_TICKS(100));
- APP_ERROR_CHECK(err_code);
- err_code = es_stopwatch_create(
- &m_tlm_refresh_sw_id,
- APP_TIMER_TICKS(APP_CONFIG_TLM_TEMP_VBATT_UPDATE_INTERVAL_SECONDS * 1000));
- APP_ERROR_CHECK(err_code);
- }
|