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; }
void i2c_init(i2c_t *obj, PinName sda, PinName scl) { ret_code_t ret; int i; for (i = 0; i < TWI_COUNT; ++i) { if (m_twi_info[i].initialized && m_twi_info[i].pselsda == (uint32_t)sda && m_twi_info[i].pselscl == (uint32_t)scl) { TWI_IDX(obj) = i; TWI_INFO(obj)->frequency = NRF_TWI_FREQ_100K; i2c_reset(obj); return; } } for (i = 0; i < TWI_COUNT; ++i) { if (!m_twi_info[i].initialized) { ret = nrf_drv_common_per_res_acquire(m_twi_instances[i], m_twi_irq_handlers[i]); if (ret != NRF_SUCCESS) { continue; /* the hw resource is busy - test another one */ } TWI_IDX(obj) = i; twi_info_t *twi_info = TWI_INFO(obj); twi_info->initialized = true; twi_info->pselsda = (uint32_t)sda; twi_info->pselscl = (uint32_t)scl; twi_info->frequency = NRF_TWI_FREQ_100K; twi_info->start_twi = false; #if DEVICE_I2C_ASYNCH twi_info->active = false; #endif twi_clear_bus(twi_info); configure_twi_pin(twi_info->pselsda, NRF_GPIO_PIN_DIR_INPUT); configure_twi_pin(twi_info->pselscl, NRF_GPIO_PIN_DIR_INPUT); i2c_reset(obj); #if DEVICE_I2C_ASYNCH NVIC_SetVector(twi_handlers[i].IRQn, twi_handlers[i].vector); nrf_drv_common_irq_enable(twi_handlers[i].IRQn, TWI_IRQ_PRIORITY); #endif return; } } error("No available I2C peripheral\r\n"); }
ret_code_t nrf_drv_spis_init(nrf_drv_spis_t const * const p_instance, nrf_drv_spis_config_t const * p_config, nrf_drv_spis_event_handler_t event_handler) { spis_cb_t * p_cb = &m_cb[p_instance->instance_id]; NRF_SPIS_Type * p_spis = p_instance->p_reg; if (p_cb->state != NRF_DRV_STATE_UNINITIALIZED) { return NRF_ERROR_INVALID_STATE; } if (p_config == NULL) { p_config = &m_default_config[p_instance->instance_id]; } if ((uint32_t)p_config->mode > (uint32_t)NRF_DRV_SPIS_MODE_3) { return NRF_ERROR_INVALID_PARAM; } if (!event_handler) { return NRF_ERROR_NULL; } #if PERIPHERAL_RESOURCE_SHARING_ENABLED if (nrf_drv_common_per_res_acquire(p_spis, m_irq_handlers[p_instance->instance_id]) != NRF_SUCCESS) { return NRF_ERROR_BUSY; } #endif // Configure the SPI pins for input. uint32_t mosi_pin; uint32_t miso_pin; if (p_config->miso_pin != NRF_DRV_SPIS_PIN_NOT_USED) { nrf_gpio_cfg(p_config->miso_pin, NRF_GPIO_PIN_DIR_INPUT, NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_NOPULL, p_config->miso_drive, NRF_GPIO_PIN_NOSENSE); miso_pin = p_config->miso_pin; } else { miso_pin = NRF_SPIS_PIN_NOT_CONNECTED; } if (p_config->mosi_pin != NRF_DRV_SPIS_PIN_NOT_USED) { nrf_gpio_cfg(p_config->mosi_pin, NRF_GPIO_PIN_DIR_INPUT, NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE); mosi_pin = p_config->mosi_pin; } else { mosi_pin = NRF_SPIS_PIN_NOT_CONNECTED; } nrf_gpio_cfg(p_config->csn_pin, NRF_GPIO_PIN_DIR_INPUT, NRF_GPIO_PIN_INPUT_CONNECT, p_config->csn_pullup, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE); nrf_gpio_cfg(p_config->sck_pin, NRF_GPIO_PIN_DIR_INPUT, NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_S0S1, NRF_GPIO_PIN_NOSENSE); nrf_spis_pins_set(p_spis, p_config->sck_pin, mosi_pin, miso_pin, p_config->csn_pin); nrf_spis_rx_buffer_set(p_spis, NULL, 0); nrf_spis_tx_buffer_set(p_spis, NULL, 0); // Configure SPI mode. nrf_spis_configure(p_spis, (nrf_spis_mode_t) p_config->mode, (nrf_spis_bit_order_t) p_config->bit_order); // Configure DEF and ORC characters. nrf_spis_def_set(p_spis, p_config->def); nrf_spis_orc_set(p_spis, p_config->orc); // Clear possible pending events. nrf_spis_event_clear(p_spis, NRF_SPIS_EVENT_END); nrf_spis_event_clear(p_spis, NRF_SPIS_EVENT_ACQUIRED); // Enable END_ACQUIRE shortcut. nrf_spis_shorts_enable(p_spis, NRF_SPIS_SHORT_END_ACQUIRE); m_cb[p_instance->instance_id].spi_state = SPIS_STATE_INIT; m_cb[p_instance->instance_id].handler = event_handler; // Enable IRQ. nrf_spis_int_enable(p_spis, NRF_SPIS_INT_ACQUIRED_MASK | NRF_SPIS_INT_END_MASK); nrf_drv_common_irq_enable(p_instance->irq, p_config->irq_priority); p_cb->state = NRF_DRV_STATE_INITIALIZED; // Enable SPI slave device. nrf_spis_enable(p_spis); 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; }