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.

157 lines
5.6 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. #include "znordic.h"
  2. #include <stdbool.h>
  3. #include <stdint.h>
  4. #include "boards.h"
  5. #include "nrf_delay.h"
  6. #include "nrf_drv_clock.h"
  7. #include "nrf_drv_rtc.h"
  8. #include "nrf_drv_timer.h"
  9. #include "nrf_log.h"
  10. #include "nrf_log_ctrl.h"
  11. #include "nrf_log_default_backends.h"
  12. #include "nrfx_rtc.h"
  13. #define SCHED_MAX_EVENT_DATA_SIZE APP_TIMER_SCHED_EVENT_DATA_SIZE /**< Maximum size of scheduler events. */
  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. APP_SCHED_INIT(SCHED_MAX_EVENT_DATA_SIZE, 20);
  25. {
  26. /*******************************************************************************
  27. * *
  28. *******************************************************************************/
  29. ret_code_t err_code = NRF_LOG_INIT(NULL);
  30. APP_ERROR_CHECK(err_code);
  31. NRF_LOG_DEFAULT_BACKENDS_INIT();
  32. }
  33. {
  34. /*******************************************************************************
  35. * *
  36. *******************************************************************************/
  37. ret_code_t err_code = app_timer_init();
  38. APP_ERROR_CHECK(err_code);
  39. }
  40. {
  41. /*******************************************************************************
  42. * *
  43. *******************************************************************************/
  44. ret_code_t err_code;
  45. err_code = nrf_pwr_mgmt_init();
  46. APP_ERROR_CHECK(err_code);
  47. }
  48. {
  49. /*******************************************************************************
  50. * 使 *
  51. *******************************************************************************/
  52. ret_code_t err_code;
  53. err_code = nrf_sdh_enable_request();
  54. APP_ERROR_CHECK(err_code);
  55. uint32_t ram_start = 0;
  56. err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
  57. APP_ERROR_CHECK(err_code);
  58. // Enable BLE stack.
  59. err_code = nrf_sdh_ble_enable(&ram_start);
  60. APP_ERROR_CHECK(err_code);
  61. }
  62. {
  63. ret_code_t errCode;
  64. nrf_drv_rtc_config_t rtcConfig = NRF_DRV_RTC_DEFAULT_CONFIG; // Initialize RTC instance
  65. rtcConfig.prescaler = 4095; // 如实现8HZ的频率,则PRESCALER寄存器应该设为32768/8-1 = 4095
  66. errCode = nrf_drv_rtc_init(&s_rtcHandle, &rtcConfig, rtcCallbackFunc);
  67. APP_ERROR_CHECK(errCode);
  68. nrf_drv_rtc_tick_enable(&s_rtcHandle, true); // Enable tick event & interrupt
  69. nrf_drv_rtc_enable(&s_rtcHandle); // Power on RTC instance
  70. }
  71. }
  72. int32_t znordic_get_event_max_size() { return SCHED_MAX_EVENT_DATA_SIZE; }
  73. void znordic_loop() {
  74. while (true) {
  75. app_sched_execute();
  76. if (NRF_LOG_PROCESS() == false) {
  77. nrf_pwr_mgmt_run();
  78. }
  79. }
  80. }
  81. void znrf_gpio_cfg_output(uint32_t pin_number, nrf_gpio_pin_pull_t pull) { //
  82. nrf_gpio_cfg(pin_number, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, pull, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE);
  83. }
  84. int16_t znrf_adc_channel_read_val(uint16_t channel) {
  85. nrf_saadc_value_t value;
  86. ret_code_t err_code;
  87. err_code = nrfx_saadc_sample_convert(channel, &value);
  88. if (err_code != NRF_SUCCESS) {
  89. ZLOGE("nrfx_saadc_sample_convert(%d) fail err_code:%d", channel, err_code);
  90. return 0;
  91. }
  92. return value;
  93. }
  94. /*******************************************************************************
  95. * RTC *
  96. *******************************************************************************/
  97. volatile uint32_t g_timestamp = 0;
  98. volatile uint32_t g_power_on_rtc = 0;
  99. uint32_t znordic_getpower_on_s() { return g_timestamp; }
  100. void znordic_rtc_settime_s(uint32_t timestampNow) {
  101. if (timestampNow < g_timestamp) {
  102. return;
  103. }
  104. g_power_on_rtc = timestampNow - g_timestamp;
  105. }
  106. uint32_t znordic_rtc_gettime_s(void) { return g_timestamp + g_power_on_rtc; }
  107. void znordic_rtc_settime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
  108. static struct tm s_tm;
  109. memset(&s_tm, 0, sizeof(s_tm));
  110. s_tm.tm_year = year - 1900;
  111. s_tm.tm_mon = month - 1;
  112. s_tm.tm_mday = day;
  113. s_tm.tm_hour = hour;
  114. s_tm.tm_min = min;
  115. s_tm.tm_sec = sec;
  116. s_tm.tm_isdst = -1;
  117. uint32_t nowtimestamp = mktime(&s_tm);
  118. g_power_on_rtc = nowtimestamp - g_timestamp;
  119. }
  120. void znordic_rtc_gettime(ztm_t *now) {
  121. time_t now_s = g_timestamp + g_power_on_rtc;
  122. *now = *localtime(&now_s);
  123. }
  124. static void rtcCallbackFunc(nrf_drv_rtc_int_type_t interruptType) {
  125. static uint8_t s_timeCount1second = 0;
  126. if (interruptType == NRF_DRV_RTC_INT_TICK) // 中断类型:滴答中断
  127. {
  128. if (s_timeCount1second >= 7) // 125ms * 8 = 1s
  129. {
  130. s_timeCount1second = 0;
  131. g_timestamp++;
  132. } else {
  133. s_timeCount1second++;
  134. }
  135. }
  136. }