1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #include "bsp_rtc.h"
- #define rtc_cb_max 32
- static RTC_callback rtc_cb[rtc_cb_max] = {0};
- //nRF52系列芯片内部有3个RTC,其中RTC0是给协议栈softdevice用,RTC1分配给 App timer用
- const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2); /**< Declaring an instance of nrf_drv_rtc for RTC2. */
- static rtc_t m_rtc;
- /** @brief: Function for handling the RTC0 interrupts.
- * Triggered on TICK and COMPARE0 match.
- */
- static void rtc_handler(nrf_drv_rtc_int_type_t int_type)
- {
- if (int_type == NRF_DRV_RTC_INT_COMPARE0)
- {
- for(int i=0;i<rtc_cb_max;i++) {
- if(rtc_cb[i]){
- rtc_cb[i]((unsigned short int*)(&m_rtc.Compare_ms)); //回调函数
- }
- }
- }
- }
- int RTC_Regist(RTC_callback cb)
- {
- for(int i=0;i<rtc_cb_max;i++) {
- if(rtc_cb[i]==cb) return -2;
- if(rtc_cb[i]==0){
- rtc_cb[i] = cb; //回调函数
- return 0;
- }
- }
- return -1;
- }
- unsigned short int RTC_GetCompareTime(void)
- {
- return m_rtc.Compare_ms;
- }
- int RTC_SetCompareTime(unsigned short int ms)
- {
- if(ms > 0 && ms < 0xFFFF){
- m_rtc.Compare_ms = ms;
- return 0;
- }
- return -1;
- }
- void RTC_CompareEnable(void)
- {
- uint32_t err_code;
-
- err_code = nrf_drv_rtc_cc_set(&rtc,RTC_CHANNEL,m_rtc.Compare_ms,true);
- APP_ERROR_CHECK(err_code);
- // nrf_rtc_int_enable(rtc.p_reg, RTC_CHANNEL_MASK);
- m_rtc.is_CompareEnable = 1;
- }
- void RTC_CompareDisable(void)
- {
- uint32_t err_code;
-
- err_code = nrf_drv_rtc_cc_disable(&rtc,RTC_CHANNEL);
- APP_ERROR_CHECK(err_code);
- // nrf_rtc_int_disable(rtc.p_reg, RTC_CHANNEL_MASK);
- m_rtc.is_CompareEnable = 0;
- }
- unsigned char RTC_GetCompareEnableFlag()
- {
- return m_rtc.is_CompareEnable;
- }
- void lfclk_config(void)
- {
- ret_code_t err_code = nrf_drv_clock_init();
- APP_ERROR_CHECK(err_code);
- nrf_drv_clock_lfclk_request(NULL);
- }
- void RTC_Init(void)
- {
- uint32_t err_code;
-
- //Initialize RTC instance
- nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
- config.prescaler = RTC_PRESCALER;
- err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
- APP_ERROR_CHECK(err_code);
-
- // //Power on RTC instance
- // nrf_drv_rtc_enable(&rtc);
- }
|