es_tlm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <string.h>
  41. #include "app_timer.h"
  42. #include "es_tlm.h"
  43. #include "es_app_config.h"
  44. #include "es_battery_voltage.h"
  45. #include "es_stopwatch.h"
  46. #include "nrf_soc.h"
  47. #define TICKS_100_MS APP_TIMER_TICKS(100) //!< Tick count for 100ms.
  48. static es_tlm_frame_t m_tlm;
  49. static uint32_t m_le_adv_cnt;
  50. static es_stopwatch_id_t m_time_sec_sw_id;
  51. static es_stopwatch_id_t m_tlm_refresh_sw_id;
  52. /**@brief Function for updating the ADV_SEC field of TLM*/
  53. static void update_time(void)
  54. {
  55. static uint32_t time_total_100_ms = 0;
  56. uint32_t be_time_100_ms; // Big endian version of 0.1 second counter.
  57. time_total_100_ms += es_stopwatch_check(m_time_sec_sw_id);
  58. be_time_100_ms = BYTES_REVERSE_32BIT(time_total_100_ms);
  59. memcpy(m_tlm.sec_cnt, &be_time_100_ms, ES_TLM_SEC_CNT_LENGTH);
  60. }
  61. /**@brief Function for updating the TEMP field of TLM*/
  62. static void update_temp(void)
  63. {
  64. int32_t temp; // variable to hold temp reading
  65. (void)sd_temp_get(&temp); // get new temperature
  66. int16_t temp_new = (int16_t) temp; // convert from int32_t to int16_t
  67. m_tlm.temp[0] = (uint8_t)((temp_new >> 2) & 0xFFUL); // Right-shift by two to remove decimal part
  68. m_tlm.temp[1] = (uint8_t)((temp_new << 6) & 0xFFUL); // Left-shift 6 to get fractional part with 0.25 degrees C resolution
  69. }
  70. /**@brief Function for updating the VBATT field of TLM*/
  71. static void update_vbatt(void)
  72. {
  73. uint16_t vbatt; // Variable to hold voltage reading
  74. es_battery_voltage_get(&vbatt); // Get new battery voltage
  75. m_tlm.vbatt[0] = (uint8_t)(vbatt >> 8);
  76. m_tlm.vbatt[1] = (uint8_t)vbatt;
  77. }
  78. static void update_adv_cnt(void)
  79. {
  80. uint32_t be_adv_cnt = BYTES_REVERSE_32BIT(m_le_adv_cnt);
  81. memcpy(m_tlm.adv_cnt, (uint8_t *)(&be_adv_cnt), ES_TLM_ADV_CNT_LENGTH);
  82. }
  83. void es_tlm_tlm_get(es_tlm_frame_t * p_tlm_frame)
  84. {
  85. // Note that frame type and TLM version fields are set in initialization.
  86. update_time();
  87. update_adv_cnt();
  88. if (es_stopwatch_check(m_tlm_refresh_sw_id) > 0)
  89. {
  90. update_temp();
  91. update_vbatt();
  92. }
  93. memcpy(p_tlm_frame, &m_tlm, sizeof(es_tlm_frame_t));
  94. }
  95. void es_tlm_adv_cnt_inc(void)
  96. {
  97. m_le_adv_cnt++;
  98. }
  99. void es_tlm_init(void)
  100. {
  101. ret_code_t err_code;
  102. memset(&m_tlm, 0, sizeof(m_tlm));
  103. m_tlm.frame_type = ES_FRAME_TYPE_TLM;
  104. m_tlm.version = ES_TLM_VERSION_TLM;
  105. m_le_adv_cnt = 0;
  106. update_time();
  107. update_vbatt();
  108. update_temp();
  109. err_code = es_stopwatch_create(&m_time_sec_sw_id, APP_TIMER_TICKS(100));
  110. APP_ERROR_CHECK(err_code);
  111. err_code = es_stopwatch_create(
  112. &m_tlm_refresh_sw_id,
  113. APP_TIMER_TICKS(APP_CONFIG_TLM_TEMP_VBATT_UPDATE_INTERVAL_SECONDS * 1000));
  114. APP_ERROR_CHECK(err_code);
  115. }