ret_code_t nrf_drv_lpcomp_init(const nrf_drv_lpcomp_config_t * p_config, lpcomp_events_handler_t events_handler) { if (m_state != NRF_DRV_STATE_UNINITIALIZED) { // LPCOMP driver is already initialized return NRF_ERROR_INVALID_STATE; } if (p_config == NULL) { p_config = &m_default_config; } #if PERIPHERAL_RESOURCE_SHARING_ENABLED if (nrf_drv_common_per_res_acquire(NRF_LPCOMP, IRQ_HANDLER_NAME) != NRF_SUCCESS) { return NRF_ERROR_BUSY; } #endif nrf_lpcomp_configure(&(p_config->hal) ); if (events_handler) { m_lpcomp_events_handler = events_handler; } else { return NRF_ERROR_INVALID_PARAM; } nrf_lpcomp_input_select(p_config->input); switch (p_config->hal.detection) { case NRF_LPCOMP_DETECT_UP: nrf_lpcomp_int_enable(LPCOMP_INTENSET_UP_Msk); break; case NRF_LPCOMP_DETECT_DOWN: nrf_lpcomp_int_enable(LPCOMP_INTENSET_DOWN_Msk); break; case NRF_LPCOMP_DETECT_CROSS: nrf_lpcomp_int_enable(LPCOMP_INTENSET_CROSS_Msk); break; default: break; } nrf_lpcomp_shorts_enable(NRF_LPCOMP_SHORT_READY_SAMPLE_MASK); nrf_drv_common_irq_enable(LPCOMP_IRQn, p_config->interrupt_priority); m_state = NRF_DRV_STATE_INITIALIZED; return NRF_SUCCESS; }
ret_code_t nrf_drv_lpcomp_init(const nrf_drv_lpcomp_config_t * p_config, lpcomp_events_handler_t events_handler) { ASSERT(p_config); ret_code_t err_code; if (m_state != NRF_DRV_STATE_UNINITIALIZED) { // LPCOMP driver is already initialized err_code = NRF_ERROR_INVALID_STATE; NRF_LOG_WARNING("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)ERR_TO_STR(err_code)); return err_code; } #if NRF_MODULE_ENABLED(PERIPHERAL_RESOURCE_SHARING) if (nrf_drv_common_per_res_acquire(NRF_LPCOMP, IRQ_HANDLER_NAME) != NRF_SUCCESS) { err_code = NRF_ERROR_BUSY; NRF_LOG_WARNING("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)ERR_TO_STR(err_code)); return err_code; } #endif nrf_lpcomp_configure(&(p_config->hal) ); if (events_handler) { m_lpcomp_events_handler = events_handler; } else { err_code = NRF_ERROR_INVALID_PARAM; NRF_LOG_WARNING("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)ERR_TO_STR(err_code)); return err_code; } nrf_lpcomp_input_select(p_config->input); switch (p_config->hal.detection) { case NRF_LPCOMP_DETECT_UP: nrf_lpcomp_int_enable(LPCOMP_INTENSET_UP_Msk); break; case NRF_LPCOMP_DETECT_DOWN: nrf_lpcomp_int_enable(LPCOMP_INTENSET_DOWN_Msk); break; case NRF_LPCOMP_DETECT_CROSS: nrf_lpcomp_int_enable(LPCOMP_INTENSET_CROSS_Msk); break; default: break; } nrf_lpcomp_shorts_enable(NRF_LPCOMP_SHORT_READY_SAMPLE_MASK); nrf_drv_common_irq_enable(LPCOMP_IRQn, p_config->interrupt_priority); m_state = NRF_DRV_STATE_INITIALIZED; err_code = NRF_SUCCESS; NRF_LOG_INFO("Function: %s, error code: %s.\r\n", (uint32_t)__func__, (uint32_t)ERR_TO_STR(err_code)); return err_code; }