static int init_rtc(struct device *dev, const nrfx_rtc_config_t *config, nrfx_rtc_handler_t handler) { struct device *clock; const struct counter_nrfx_config *nrfx_config = get_nrfx_config(dev); const nrfx_rtc_t *rtc = &nrfx_config->rtc; clock = device_get_binding(DT_NORDIC_NRF_CLOCK_0_LABEL "_32K"); if (!clock) { return -ENODEV; } clock_control_on(clock, (void *)CLOCK_CONTROL_NRF_K32SRC); nrfx_err_t result = nrfx_rtc_init(rtc, config, handler); if (result != NRFX_SUCCESS) { LOG_INST_ERR(nrfx_config->log, "Failed to initialize device."); return -EBUSY; } get_dev_data(dev)->top = COUNTER_MAX_TOP_VALUE; LOG_INST_DBG(nrfx_config->log, "Initialized"); return 0; }
static int ppi_setup(struct device *dev) { #if CONFIG_COUNTER_RTC_WITH_PPI_WRAP const struct counter_nrfx_config *nrfx_config = get_nrfx_config(dev); struct counter_nrfx_data *data = get_dev_data(dev); const nrfx_rtc_t *rtc = &nrfx_config->rtc; nrfx_err_t result; if (!nrfx_config->use_ppi) { return 0; } #ifdef DPPI_PRESENT result = nrfx_dppi_channel_alloc(&data->ppi_ch); if (result != NRFX_SUCCESS) { LOG_INST_ERR(nrfx_config->log, "Failed to allocate PPI channel."); return -ENODEV; } nrf_rtc_subscribe_set(rtc->p_reg, NRF_RTC_TASK_CLEAR, data->ppi_ch); nrf_rtc_publish_set(rtc->p_reg, NRF_RTC_EVENT_COMPARE_0, data->ppi_ch); (void)nrfx_dppi_channel_enable(data->ppi_ch); #else /* DPPI_PRESENT */ u32_t evt; u32_t task; evt = nrfx_rtc_event_address_get(rtc, NRF_RTC_EVENT_COMPARE_0); task = nrfx_rtc_task_address_get(rtc, NRF_RTC_TASK_CLEAR); result = nrfx_ppi_channel_alloc(&data->ppi_ch); if (result != NRFX_SUCCESS) { LOG_INST_ERR(nrfx_config->log, "Failed to allocate PPI channel."); return -ENODEV; } (void)nrfx_ppi_channel_assign(data->ppi_ch, evt, task); (void)nrfx_ppi_channel_enable(data->ppi_ch); #endif #endif /* CONFIG_COUNTER_RTC_WITH_PPI_WRAP */ return 0; }
static int init_timer(struct device *dev, const nrfx_timer_config_t *config) { const struct counter_nrfx_config *nrfx_config = get_nrfx_config(dev); const nrfx_timer_t *timer = &nrfx_config->timer; nrfx_err_t result = nrfx_timer_init(timer, config, event_handler); if (result != NRFX_SUCCESS) { LOG_INST_ERR(nrfx_config->log, "Failed to initialize device."); return -EBUSY; } nrfx_timer_compare(timer, TOP_CH, UINT32_MAX, false); LOG_INST_DBG(nrfx_config->log, "Initialized"); return 0; }