/** * @brief TWI initialization. */ static ret_code_t twi_init (void) { ret_code_t err_code; const nrf_drv_twi_config_t twi_mcp4725_config = { .scl = ARDUINO_SCL_PIN, .sda = ARDUINO_SDA_PIN, .frequency = NRF_TWI_FREQ_100K, .interrupt_priority = APP_IRQ_PRIORITY_HIGH, .clear_bus_init = false }; err_code = nrf_drv_twi_init(&m_twi, &twi_mcp4725_config, twi_handler, NULL); if (err_code != NRF_SUCCESS) { return err_code; } nrf_drv_twi_enable(&m_twi); return NRF_SUCCESS; } ret_code_t mcp4725_setup(void) { ret_code_t err_code = twi_init(); if (err_code != NRF_SUCCESS) { return err_code; } return NRF_SUCCESS; }
int32_t VL53L0X_comms_initialise(uint8_t comms_type, uint16_t comms_speed_khz){ if(comms_type == SPI){ NRF_LOG_ERROR("SPI not supported. Use I2C.\r\n"); return 1; } else if(comms_type != I2C){ NRF_LOG_ERROR("Invalid communication protocol with VL53L0X. Use I2C.\r\n"); return 1; } uint32_t nrf_speed; if(comms_speed_khz == 400){ nrf_speed = NRF_TWI_FREQ_400K; } else if(comms_speed_khz == 250){ nrf_speed = NRF_TWI_FREQ_250K; } else if(comms_speed_khz == 100){ nrf_speed = NRF_TWI_FREQ_100K; } else { NRF_LOG_ERROR("Invalid TWI comms speed."); return 1; } ret_code_t ret; const nrf_drv_twi_config_t config = { .scl = TWI_SCL_M, .sda = TWI_SDA_M, .frequency = nrf_speed, .interrupt_priority = APP_IRQ_PRIORITY_HIGH, .clear_bus_init = false }; ret = nrf_drv_twi_init(&m_twi_master, &config, NULL, NULL); if (NRF_SUCCESS == ret) { nrf_drv_twi_enable(&m_twi_master); NRF_LOG_DEBUG("TWI init successful\r\n"); } else { NRF_LOG_ERROR("TWI init failed\r\n"); } return ret; }; /** * @brief Close platform comms. * * @return status - status 0 = ok, 1 = error * */ int32_t VL53L0X_comms_close(void){ nrf_drv_twi_disable(&m_twi_master); return 0; }
void i2c_reset(i2c_t *obj) { twi_info_t *twi_info = TWI_INFO(obj); nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; nrf_drv_twi_uninit(twi); nrf_drv_twi_init(twi, &twi_info->config, twi_event_handler, obj); nrf_drv_twi_enable(twi); }
/** * @brief TWI initialization. * Just the usual way. Nothing special here */ void twi_init(void) { ret_code_t err_code; const nrf_drv_twi_config_t twi_mpu_9150_config = { .scl = MPU9150_TWI_SCL_PIN, .sda = MPU9150_TWI_SDA_PIN, .frequency = NRF_TWI_FREQ_400K, .interrupt_priority = APP_IRQ_PRIORITY_HIGH }; err_code = nrf_drv_twi_init(&m_twi_instance, &twi_mpu_9150_config, twi_handler, NULL); APP_ERROR_CHECK(err_code); nrf_drv_twi_enable(&m_twi_instance); }
void twi_rfid_init(void) { const nrf_drv_twi_config_t twi_rfid_config = { .scl = SCL_RFID_PIN, .sda = SDA_RFID_PIN, .frequency = NRF_TWI_FREQ_100K, .interrupt_priority = TWI0_CONFIG_IRQ_PRIORITY }; // Initialize the TWI channel nrf_drv_twi_init(&twi_rfid, &twi_rfid_config, NULL, NULL); // Enable the TWI channel nrf_drv_twi_enable(&twi_rfid); //Initializes the RFID-shield ready_rfid_shield(); };
/** Set up I2C, if pins are -1 they will be guessed */ void jshI2CSetup(IOEventFlags device, JshI2CInfo *inf) { if (!jshIsPinValid(inf->pinSCL) || !jshIsPinValid(inf->pinSDA)) { jsError("SDA and SCL pins must be valid, got %d and %d\n", inf->pinSDA, inf->pinSCL); return; } const nrf_drv_twi_t *twi = jshGetTWI(device); if (!twi) return; // http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk51.v9.0.0%2Fhardware_driver_twi.html&cp=4_1_0_2_10 nrf_drv_twi_config_t p_twi_config; p_twi_config.scl = (uint32_t)pinInfo[inf->pinSCL].pin; p_twi_config.sda = (uint32_t)pinInfo[inf->pinSDA].pin; p_twi_config.frequency = (inf->bitrate<175000) ? NRF_TWI_FREQ_100K : ((inf->bitrate<325000) ? NRF_TWI_FREQ_250K : NRF_TWI_FREQ_400K); p_twi_config.interrupt_priority = APP_IRQ_PRIORITY_LOW; if (twi1Initialised) nrf_drv_twi_uninit(twi); twi1Initialised = true; uint32_t err_code = nrf_drv_twi_init(twi, &p_twi_config, NULL, NULL); if (err_code != NRF_SUCCESS) jsExceptionHere(JSET_INTERNALERROR, "I2C Initialisation Error %d\n", err_code); else nrf_drv_twi_enable(twi); }
void i2c_init(i2c_t *obj, PinName sda, PinName scl) { int i; for (i = 0; i < TWI_COUNT; ++i) { if (m_twi_info[i].initialized && m_twi_info[i].config.sda == (uint32_t)sda && m_twi_info[i].config.scl == (uint32_t)scl) { TWI_IDX(obj) = i; TWI_INFO(obj)->config.frequency = NRF_TWI_FREQ_100K; i2c_reset(obj); return; } } nrf_drv_twi_config_t const config = { .scl = scl, .sda = sda, .frequency = NRF_TWI_FREQ_100K, .interrupt_priority = APP_IRQ_PRIORITY_LOW, }; for (i = 0; i < TWI_COUNT; ++i) { if (!m_twi_info[i].initialized) { nrf_drv_twi_t const *twi = &m_twi_instances[i]; ret_code_t ret_code = nrf_drv_twi_init(twi, &config, twi_event_handler, obj); if (ret_code == NRF_SUCCESS) { TWI_IDX(obj) = i; TWI_INFO(obj)->initialized = true; TWI_INFO(obj)->config = config; nrf_drv_twi_enable(twi); return; } } } // No available peripheral error("No available I2C peripheral\r\n"); }