bsp_rtc.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "bsp_rtc.h"
  2. #define rtc_cb_max 32
  3. static RTC_callback rtc_cb[rtc_cb_max] = {0};
  4. //nRF52系列芯片内部有3个RTC,其中RTC0是给协议栈softdevice用,RTC1分配给 App timer用
  5. const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2); /**< Declaring an instance of nrf_drv_rtc for RTC2. */
  6. static rtc_t m_rtc;
  7. /** @brief: Function for handling the RTC0 interrupts.
  8. * Triggered on TICK and COMPARE0 match.
  9. */
  10. static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
  11. {
  12. if (int_type == NRF_DRV_RTC_INT_COMPARE0)
  13. {
  14. for(int i=0;i<rtc_cb_max;i++) {
  15. if(rtc_cb[i]){
  16. rtc_cb[i]((unsigned short int*)(&m_rtc.Compare_ms)); //回调函数
  17. }
  18. }
  19. }
  20. }
  21. int RTC_Regist(RTC_callback cb)
  22. {
  23. for(int i=0;i<rtc_cb_max;i++) {
  24. if(rtc_cb[i]==cb) return -2;
  25. if(rtc_cb[i]==0){
  26. rtc_cb[i] = cb; //回调函数
  27. return 0;
  28. }
  29. }
  30. return -1;
  31. }
  32. unsigned short int RTC_GetCompareTime(void)
  33. {
  34. return m_rtc.Compare_ms;
  35. }
  36. int RTC_SetCompareTime(unsigned short int ms)
  37. {
  38. if(ms > 0 && ms < 0xFFFF){
  39. m_rtc.Compare_ms = ms;
  40. return 0;
  41. }
  42. return -1;
  43. }
  44. void RTC_CompareEnable(void)
  45. {
  46. uint32_t err_code;
  47. err_code = nrf_drv_rtc_cc_set(&rtc,RTC_CHANNEL,m_rtc.Compare_ms,true);
  48. APP_ERROR_CHECK(err_code);
  49. // nrf_rtc_int_enable(rtc.p_reg, RTC_CHANNEL_MASK);
  50. m_rtc.is_CompareEnable = 1;
  51. }
  52. void RTC_CompareDisable(void)
  53. {
  54. uint32_t err_code;
  55. err_code = nrf_drv_rtc_cc_disable(&rtc,RTC_CHANNEL);
  56. APP_ERROR_CHECK(err_code);
  57. // nrf_rtc_int_disable(rtc.p_reg, RTC_CHANNEL_MASK);
  58. m_rtc.is_CompareEnable = 0;
  59. }
  60. unsigned char RTC_GetCompareEnableFlag()
  61. {
  62. return m_rtc.is_CompareEnable;
  63. }
  64. void lfclk_config(void)
  65. {
  66. ret_code_t err_code = nrf_drv_clock_init();
  67. APP_ERROR_CHECK(err_code);
  68. nrf_drv_clock_lfclk_request(NULL);
  69. }
  70. void RTC_Init(void)
  71. {
  72. uint32_t err_code;
  73. //Initialize RTC instance
  74. nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
  75. config.prescaler = RTC_PRESCALER;
  76. err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
  77. APP_ERROR_CHECK(err_code);
  78. // //Power on RTC instance
  79. // nrf_drv_rtc_enable(&rtc);
  80. }