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.

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