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.

228 lines
7.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years 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. #if 0
  32. {
  33. ret_code_t err_code = ble_dfu_buttonless_async_svci_init();
  34. APP_ERROR_CHECK(err_code);
  35. }
  36. #endif
  37. {
  38. /*******************************************************************************
  39. * ʱʼ *
  40. *******************************************************************************/
  41. ret_code_t err_code = app_timer_init();
  42. APP_ERROR_CHECK(err_code);
  43. }
  44. {
  45. /*******************************************************************************
  46. * Դʼ *
  47. *******************************************************************************/
  48. ret_code_t err_code;
  49. err_code = nrf_pwr_mgmt_init();
  50. APP_ERROR_CHECK(err_code);
  51. }
  52. {
  53. /*******************************************************************************
  54. * Эջʹ *
  55. *******************************************************************************/
  56. ret_code_t err_code;
  57. err_code = nrf_sdh_enable_request();
  58. APP_ERROR_CHECK(err_code);
  59. uint32_t ram_start = 0;
  60. err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
  61. APP_ERROR_CHECK(err_code);
  62. // Enable BLE stack.
  63. err_code = nrf_sdh_ble_enable(&ram_start);
  64. APP_ERROR_CHECK(err_code);
  65. }
  66. {
  67. ret_code_t errCode;
  68. nrf_drv_rtc_config_t rtcConfig = NRF_DRV_RTC_DEFAULT_CONFIG; // Initialize RTC instance
  69. rtcConfig.prescaler = 4095; // ��ʵ??8HZ��Ƶ�ʣ���PRESCALER�Ĵ���Ӧ����??32768/8-1 = 4095
  70. errCode = nrf_drv_rtc_init(&s_rtcHandle, &rtcConfig, rtcCallbackFunc);
  71. APP_ERROR_CHECK(errCode);
  72. nrf_drv_rtc_tick_enable(&s_rtcHandle, true); // Enable tick event & interrupt
  73. nrf_drv_rtc_enable(&s_rtcHandle); // Power on RTC instance
  74. }
  75. }
  76. void znordic_loop() {
  77. while (true) {
  78. app_sched_execute();
  79. if (NRF_LOG_PROCESS() == false) {
  80. nrf_pwr_mgmt_run();
  81. }
  82. }
  83. }
  84. void znordic_force_flush_log() { NRF_LOG_FLUSH(); }
  85. void znrf_gpio_cfg_output(uint32_t pin_number, nrf_gpio_pin_pull_t pull) { //
  86. nrf_gpio_cfg(pin_number, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, pull, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE);
  87. }
  88. int16_t znrf_adc_channel_read_val(uint16_t channel) {
  89. nrf_saadc_value_t value;
  90. ret_code_t err_code;
  91. err_code = nrfx_saadc_sample_convert(channel, &value);
  92. if (err_code != NRF_SUCCESS) {
  93. ZLOGE("nrfx_saadc_sample_convert(%d) fail err_code:%d", channel, err_code);
  94. return 0;
  95. }
  96. return value;
  97. }
  98. /*******************************************************************************
  99. * RTC *
  100. *******************************************************************************/
  101. #define DEFAULT_TIME (1704038400 + 8 * 60 * 60) // 2024/01/01/00:00:00
  102. volatile uint32_t g_timestamp = 0;
  103. volatile uint32_t g_power_on_rtc = (DEFAULT_TIME);
  104. // static uint8_t s_timeCount1second = 0;
  105. uint32_t znordic_getpower_on_ms() {
  106. __disable_irq();
  107. uint32_t ret = g_timestamp * 125;
  108. __enable_irq();
  109. return ret;
  110. }
  111. uint32_t znordic_getpower_on_s() {
  112. __disable_irq();
  113. uint32_t reg = znordic_getpower_on_ms() / 1000;
  114. __enable_irq();
  115. return reg;
  116. }
  117. uint32_t znordic_haspassed_ms(uint32_t last) {
  118. uint32_t now = znordic_getpower_on_ms();
  119. if (now < last) {
  120. return 0xFFFFFFFF - last + now;
  121. } else {
  122. return now - last;
  123. }
  124. }
  125. void znordic_rtc_settime_s(uint32_t timestampNow) {
  126. if (timestampNow < znordic_getpower_on_s()) {
  127. return;
  128. }
  129. g_power_on_rtc = timestampNow - znordic_getpower_on_s();
  130. }
  131. uint32_t znordic_rtc_gettime_s(void) { return znordic_getpower_on_s() + g_power_on_rtc; }
  132. void znordic_rtc_settime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
  133. static struct tm s_tm;
  134. memset(&s_tm, 0, sizeof(s_tm));
  135. s_tm.tm_year = year - 1900;
  136. s_tm.tm_mon = month - 1;
  137. s_tm.tm_mday = day;
  138. s_tm.tm_hour = hour;
  139. s_tm.tm_min = min;
  140. s_tm.tm_sec = sec;
  141. s_tm.tm_isdst = -1;
  142. uint32_t nowtimestamp = mktime(&s_tm);
  143. uint32_t tosettime = nowtimestamp - znordic_getpower_on_s();
  144. if (tosettime > DEFAULT_TIME) { // 2024/01/01/00:00:00
  145. g_power_on_rtc = tosettime;
  146. }
  147. return;
  148. }
  149. void znordic_rtc_gettime(ztm_t* now) {
  150. time_t now_s = (g_timestamp * 125) / 1000 + g_power_on_rtc;
  151. *now = *localtime(&now_s);
  152. }
  153. static void rtcCallbackFunc(nrf_drv_rtc_int_type_t interruptType) {
  154. if (interruptType == NRF_DRV_RTC_INT_TICK) // �ж����ͣ��δ���??
  155. {
  156. g_timestamp++;
  157. // if (s_timeCount1second >= 7) // 125ms * 8 = 1s
  158. // {
  159. // s_timeCount1second = 0;
  160. // g_timestamp++;
  161. // } else {
  162. // s_timeCount1second++;
  163. // }
  164. }
  165. }
  166. const char* fmt(const char* fmt, ...) {
  167. static char buf[256];
  168. va_list args;
  169. va_start(args, fmt);
  170. vsnprintf(buf, 255, fmt, args);
  171. va_end(args);
  172. return buf;
  173. }
  174. const char* hex2str(const uint8_t* data, uint16_t len) {
  175. static char buf[256];
  176. for (size_t i = 0; i < len; i++) {
  177. buf[i * 2] = (data[i] >> 4) + '0';
  178. buf[i * 2 + 1] = (data[i] & 0x0f) + '0';
  179. }
  180. return buf;
  181. }
  182. /*******************************************************************************
  183. * FATFS *
  184. *******************************************************************************/
  185. static DWORD get_fattime_0(uint32_t year, uint32_t month, uint32_t day, uint32_t hour, uint32_t min, uint32_t sec) {
  186. // return ((DWORD)(year - 1980) << 25 | (DWORD)month << 21 | (DWORD)day << 16);
  187. DWORD fattime = 0;
  188. fattime |= ((DWORD)(year - 1980) << 25); // ���ݴ�1980�꿪ʼ
  189. fattime |= ((DWORD)(month + 1) << 21); // �·ݷ�Χ��1-12
  190. fattime |= ((DWORD)day << 16); // ��
  191. fattime |= ((DWORD)hour << 11); // ʱ
  192. fattime |= ((DWORD)min << 5); // ��
  193. fattime |= ((DWORD)sec / 2); // �룬��2��Ϊ��λ
  194. return fattime;
  195. }
  196. DWORD get_fattime(void) { return get_fattime_0(2024, 1, 23, 23, 29, 31); }
  197. extern uint32_t g_nrf_log_tx_pin;
  198. void znordic_set_uart_log_pin(uint32_t pin_number) {
  199. #if NRF_LOG_BACKEND_UART_ENABLED
  200. g_nrf_log_tx_pin = pin_number;
  201. #endif
  202. }