You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

197 lines
6.4 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include "znordic.h"
  2. #include <stdarg.h>
  3. #include <stdbool.h>
  4. #include <stdint.h>
  5. #include "boards.h"
  6. #include "nrf_delay.h"
  7. #include "nrf_drv_clock.h"
  8. #include "nrf_drv_rtc.h"
  9. #include "nrf_drv_timer.h"
  10. #include "nrf_log.h"
  11. #include "nrf_log_ctrl.h"
  12. #include "nrf_log_default_backends.h"
  13. #include "nrfx_rtc.h"
  14. /*******************************************************************************
  15. * RTC *
  16. *******************************************************************************/
  17. static const nrf_drv_rtc_t s_rtcHandle = NRF_DRV_RTC_INSTANCE(2); // Declaring an instance of nrf_drv_rtc for RTC2.
  18. static void rtcCallbackFunc(nrf_drv_rtc_int_type_t interruptType);
  19. /*******************************************************************************
  20. * CODE *
  21. *******************************************************************************/
  22. void znordic_init() {
  23. {
  24. /*******************************************************************************
  25. * ־ϵͳʼ?? *
  26. *******************************************************************************/
  27. ret_code_t err_code = NRF_LOG_INIT(NULL);
  28. APP_ERROR_CHECK(err_code);
  29. NRF_LOG_DEFAULT_BACKENDS_INIT();
  30. }
  31. {
  32. /*******************************************************************************
  33. * ʱʼ *
  34. *******************************************************************************/
  35. ret_code_t err_code = app_timer_init();
  36. APP_ERROR_CHECK(err_code);
  37. }
  38. {
  39. /*******************************************************************************
  40. * Դʼ *
  41. *******************************************************************************/
  42. ret_code_t err_code;
  43. err_code = nrf_pwr_mgmt_init();
  44. APP_ERROR_CHECK(err_code);
  45. }
  46. {
  47. /*******************************************************************************
  48. * Эջʹ *
  49. *******************************************************************************/
  50. ret_code_t err_code;
  51. err_code = nrf_sdh_enable_request();
  52. APP_ERROR_CHECK(err_code);
  53. uint32_t ram_start = 0;
  54. err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
  55. APP_ERROR_CHECK(err_code);
  56. // Enable BLE stack.
  57. err_code = nrf_sdh_ble_enable(&ram_start);
  58. APP_ERROR_CHECK(err_code);
  59. }
  60. {
  61. ret_code_t errCode;
  62. nrf_drv_rtc_config_t rtcConfig = NRF_DRV_RTC_DEFAULT_CONFIG; // Initialize RTC instance
  63. rtcConfig.prescaler = 4095; // ��ʵ??8HZ��Ƶ�ʣ���PRESCALER�Ĵ���Ӧ����??32768/8-1 = 4095
  64. errCode = nrf_drv_rtc_init(&s_rtcHandle, &rtcConfig, rtcCallbackFunc);
  65. APP_ERROR_CHECK(errCode);
  66. nrf_drv_rtc_tick_enable(&s_rtcHandle, true); // Enable tick event & interrupt
  67. nrf_drv_rtc_enable(&s_rtcHandle); // Power on RTC instance
  68. }
  69. }
  70. void znordic_loop() {
  71. while (true) {
  72. app_sched_execute();
  73. if (NRF_LOG_PROCESS() == false) {
  74. nrf_pwr_mgmt_run();
  75. }
  76. }
  77. }
  78. void znordic_force_flush_log() { NRF_LOG_FLUSH(); }
  79. void znrf_gpio_cfg_output(uint32_t pin_number, nrf_gpio_pin_pull_t pull) { //
  80. nrf_gpio_cfg(pin_number, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, pull, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE);
  81. }
  82. int16_t znrf_adc_channel_read_val(uint16_t channel) {
  83. nrf_saadc_value_t value;
  84. ret_code_t err_code;
  85. err_code = nrfx_saadc_sample_convert(channel, &value);
  86. if (err_code != NRF_SUCCESS) {
  87. ZLOGE("nrfx_saadc_sample_convert(%d) fail err_code:%d", channel, err_code);
  88. return 0;
  89. }
  90. return value;
  91. }
  92. /*******************************************************************************
  93. * RTC *
  94. *******************************************************************************/
  95. #define DEFAULT_TIME (1704038400 + 8 * 60 * 60) // 2024/01/01/00:00:00
  96. volatile uint32_t g_timestamp = 0;
  97. volatile uint32_t g_power_on_rtc = (DEFAULT_TIME);
  98. // static uint8_t s_timeCount1second = 0;
  99. uint32_t znordic_getpower_on_ms() {
  100. __disable_irq();
  101. uint32_t ret = g_timestamp * 125;
  102. __enable_irq();
  103. return ret;
  104. }
  105. uint32_t znordic_getpower_on_s() {
  106. __disable_irq();
  107. uint32_t reg = znordic_getpower_on_ms() / 1000;
  108. __enable_irq();
  109. return reg;
  110. }
  111. uint32_t znordic_haspassed_ms(uint32_t last) {
  112. uint32_t now = znordic_getpower_on_ms();
  113. if (now < last) {
  114. return 0xFFFFFFFF - last + now;
  115. } else {
  116. return now - last;
  117. }
  118. }
  119. void znordic_rtc_settime_s(uint32_t timestampNow) {
  120. if (timestampNow < znordic_getpower_on_s()) {
  121. return;
  122. }
  123. g_power_on_rtc = timestampNow - znordic_getpower_on_s();
  124. }
  125. uint32_t znordic_rtc_gettime_s(void) { return znordic_getpower_on_s() + g_power_on_rtc; }
  126. void znordic_rtc_settime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
  127. static struct tm s_tm;
  128. memset(&s_tm, 0, sizeof(s_tm));
  129. s_tm.tm_year = year - 1900;
  130. s_tm.tm_mon = month - 1;
  131. s_tm.tm_mday = day;
  132. s_tm.tm_hour = hour;
  133. s_tm.tm_min = min;
  134. s_tm.tm_sec = sec;
  135. s_tm.tm_isdst = -1;
  136. uint32_t nowtimestamp = mktime(&s_tm);
  137. uint32_t tosettime = nowtimestamp - znordic_getpower_on_s();
  138. if (tosettime > DEFAULT_TIME) { // 2024/01/01/00:00:00
  139. g_power_on_rtc = tosettime;
  140. }
  141. return;
  142. }
  143. void znordic_rtc_gettime(ztm_t* now) {
  144. time_t now_s = (g_timestamp * 125) / 1000 + g_power_on_rtc;
  145. *now = *localtime(&now_s);
  146. }
  147. static void rtcCallbackFunc(nrf_drv_rtc_int_type_t interruptType) {
  148. if (interruptType == NRF_DRV_RTC_INT_TICK) // �ж����ͣ��δ���??
  149. {
  150. g_timestamp++;
  151. // if (s_timeCount1second >= 7) // 125ms * 8 = 1s
  152. // {
  153. // s_timeCount1second = 0;
  154. // g_timestamp++;
  155. // } else {
  156. // s_timeCount1second++;
  157. // }
  158. }
  159. }
  160. const char* fmt(const char* fmt, ...) {
  161. static char buf[256];
  162. va_list args;
  163. va_start(args, fmt);
  164. vsnprintf(buf, 255, fmt, args);
  165. va_end(args);
  166. return buf;
  167. }
  168. const char* hex2str(const uint8_t* data, uint16_t len) {
  169. static char buf[256];
  170. for (size_t i = 0; i < len; i++) {
  171. buf[i * 2] = (data[i] >> 4) + '0';
  172. buf[i * 2 + 1] = (data[i] & 0x0f) + '0';
  173. }
  174. return buf;
  175. }