/** Addresses are 7 bit - that is, between 0 and 0x7F. sendStop is whether to send a stop bit or not */ void jshI2CWrite(IOEventFlags device, unsigned char address, int nBytes, const unsigned char *data, bool sendStop) { const nrf_drv_twi_t *twi = jshGetTWI(device); if (!twi) return; uint32_t err_code = nrf_drv_twi_tx(twi, address, data, nBytes, !sendStop); if (err_code != NRF_SUCCESS) jsExceptionHere(JSET_INTERNALERROR, "I2C Write Error %d\n", err_code); }
/** * @brief TWI events handler. */ void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context) { uint32_t err_code; uint8_t reg[2]; NRF_LOG_DEBUG("twi_handler %u\n\r",p_event->type); switch (p_event->type) { case NRF_DRV_TWI_EVT_DONE: NRF_LOG_DEBUG("twi_handler NRF_DRV_TWI_EVT_DONE %u\n\r",p_event->xfer_desc.type); if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_TX) { NRF_LOG_DEBUG("twi_handler TX\n\r"); if (twi_read_mode==SI7021_REG_HUM_READ || twi_read_mode==SI7021_REG_TEMP_READ) { err_code = nrf_drv_twi_rx(&my_twi_0, SI7021_ADDR, m_sample, sizeof(m_sample)); APP_ERROR_CHECK(err_code); } } else if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX) { NRF_LOG_DEBUG("twi_handler RX %d,%d\n\r",m_sample[0],m_sample[1]); if (twi_read_mode==SI7021_REG_HUM_READ) { humRaw=(uint16_t)m_sample[0] << 8 |m_sample[1]; reg[0] = SI7021_REG_TEMP_READ; twi_read_mode = reg[0]; NRF_LOG_DEBUG("nrf_drv_twi_tx %x %u\n\r",SI7021_ADDR,reg[0]); err_code = nrf_drv_twi_tx(&my_twi_0, SI7021_ADDR, reg, 1, false); APP_ERROR_CHECK(err_code); } else if (twi_read_mode==SI7021_REG_TEMP_READ) { humCent = (uint16_t)((12500 * humRaw) >> 16) - 600; tempRaw = (uint16_t)m_sample[0] << 8 |m_sample[1]; tempCent = (uint16_t)((17572 * tempRaw) >> 16) - 4685; NRF_LOG_INFO("Relative Humidity %x %d.%d%% Temp %x %d.%d°C \r",humRaw,humCent/100,humCent%100,tempRaw,tempCent/100,tempCent%100); NRF_LOG_DEBUG("\r\n"); si7021_sensor_update(humRaw,tempRaw); } }
// must only be called after lps331ap_on has been called void lps331ap_readPressure(float *pres) { uint8_t command[1] = {PRESS_OUT_XL | AUTO_INCR}; nrf_drv_twi_tx( m_instance, SENSOR_ADDR, command, sizeof(command), true ); uint8_t pressure_data[3] = {0x00, 0x00, 0x00}; nrf_drv_twi_rx( m_instance, SENSOR_ADDR, pressure_data, sizeof(pressure_data), false ); float pressure = (0x00FFFFFF & (((uint32_t)pressure_data[2] << 16) | ((uint32_t) pressure_data[1] << 8) | ((uint32_t) pressure_data[0]))) / 4096.0; *pres = pressure; }
void si7021_read_RH(float *hum){ uint8_t command[] = {Meas_RH_No_Hold_Master}; ret_code_t ret_code; nrf_drv_twi_tx( m_instance, TEMP_HUM_ADDR_THIS, command, sizeof(command), true ); uint8_t temp_hum_data[3] = {0, 0, 0}; do { ret_code = nrf_drv_twi_rx( m_instance, TEMP_HUM_ADDR_THIS, temp_hum_data, sizeof(temp_hum_data), false ); } while (ret_code != NRF_SUCCESS); float humidity = -6.0 + ((125.0 / (1 << 16)) * (((uint32_t) temp_hum_data[0] << 8) | ((uint32_t) temp_hum_data[1] & 0xf0))); *hum = humidity; }
void lps331ap_interrupt_enable_manual(bool high, bool low, bool latch_request) { uint8_t command[2] = {INT_CFG_REG, 0x00}; if (high) { //enable high command[1] = 0x01; } if (low) { //enable low command[1] = command[1] | 0x02; } if (latch_request) { command[1] = command[1] | 0x04; } nrf_drv_twi_tx( m_instance, SENSOR_ADDR, command, sizeof(command), false ); }
void si7021_heater_off(){ uint8_t reg_status[1] = {0x00}; si7021_read_user_reg(reg_status); uint8_t data[1] = {reg_status[0] & HEATER_OFF}; uint8_t command[] = { Write_User_Reg_1, data[0] }; nrf_drv_twi_tx( m_instance, TEMP_HUM_ADDR_THIS, command, sizeof(command), false ); }
void lps331ap_interrupt_enable(bool latch_request) { uint8_t data[1] = {0x00}; uint8_t command[2] = {INT_CFG_REG, 0x00}; lps331ap_read_controlreg3(data); if (data[0] & 0x09) { //enable high command[1] = 0x01; } if (data[0] & 0x12) { //enable low command[1] = command[1] | 0x02; } if (latch_request) { command[1] = command[1] | 0x04; } nrf_drv_twi_tx( m_instance, SENSOR_ADDR, command, sizeof(command), false ); }
void lps331ap_config_interrupt(interrupt_config interrupt_c_1, interrupt_config interrupt_c_2 , bool active_low) { uint8_t command[2] = { CTRL_REG3, interrupt_c_1 | (interrupt_c_2 << 3) }; if(active_low) { command[1] = command[1] | 0x80; } nrf_drv_twi_tx( m_instance, SENSOR_ADDR, command, sizeof(command), false ); uint8_t data[1] = {0x00}; lps331ap_read_controlreg3(data); }
void si7021_config(si7021_meas_res_t res_mode){ uint8_t res1, res0; uint8_t reg_status; res1 = (res_mode & 0x2) << 6; res0 = res_mode & 0x1; si7021_read_user_reg(®_status); uint8_t data[1] = {reg_status | res1 | res0}; uint8_t command[] = { Write_User_Reg_1, data[0] }; nrf_drv_twi_tx( m_instance, TEMP_HUM_ADDR_THIS, command, sizeof(command), false ); }
//does not return correct temperature void lps331ap_readTemp (float *temp) { uint8_t command[1] = {TEMP_OUT_L | AUTO_INCR}; nrf_drv_twi_tx( m_instance, SENSOR_ADDR, command, sizeof(command), true ); uint8_t temp_data[2]; nrf_drv_twi_rx( m_instance, SENSOR_ADDR, temp_data, sizeof(temp_data), false ); float temperature = (((int16_t)( 0x0000FFFF & ( ( (uint32_t) temp_data[1] << 8) | ((uint32_t) temp_data[0]) ) )) / 480.0 ) + 42.5; *temp = temperature; }
void si7021_read_temp(float *temp){ uint8_t command[] = {Meas_Temp_No_Hold_Master}; ret_code_t ret_code; nrf_drv_twi_tx( m_instance, TEMP_HUM_ADDR_THIS, command, sizeof(command), true ); uint8_t temp_hum_data[3] = {0, 0, 0}; do { ret_code = nrf_drv_twi_rx( m_instance, TEMP_HUM_ADDR_THIS, temp_hum_data, sizeof(temp_hum_data), false ); } while (ret_code != NRF_SUCCESS); float temperature = -46.85 + (175.72 * (((uint32_t) temp_hum_data[0] << 8) | ((uint32_t) temp_hum_data[1] & 0xfc)) / (1 << 16)); *temp = temperature; }
ret_code_t mpu6050_register_read(const nrf_drv_twi_t * twi, uint8_t register_address, uint8_t * destination, uint8_t number_of_bytes) { //bool transfer_succeeded; uint32_t err_code; err_code = nrf_drv_twi_tx(twi, MPU6050_DEFAULT_ADDRESS, ®ister_address, 1, false); err_code = nrf_drv_twi_rx(twi, MPU6050_DEFAULT_ADDRESS, destination, number_of_bytes, false); return err_code; }
ret_code_t mpu6050_register_write(const nrf_drv_twi_t * twi, uint8_t register_address, uint8_t value) { uint8_t w2_data[2]; //uint32_t err_code; w2_data[0] = register_address; w2_data[1] = value; //return twi_master_transfer(m_device_address, w2_data, 2, TWI_ISSUE_STOP); return nrf_drv_twi_tx(twi, MPU6050_DEFAULT_ADDRESS, w2_data, sizeof(w2_data), false); //APP_ERROR_CHECK(err_code); }
void si7021_reset(){ uint8_t data[1] = {RESET}; nrf_drv_twi_tx( m_instance, TEMP_HUM_ADDR_THIS, data, sizeof(data), false ); }
void lps331ap_interrupt_disable_all() { uint8_t command[2] = {INT_CFG_REG, 0x00}; nrf_drv_twi_tx( m_instance, SENSOR_ADDR, command, sizeof(command), false ); }
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { twi_info_t *twi_info = TWI_INFO(obj); nrf_drv_twi_t const *twi = &m_twi_instances[TWI_IDX(obj)]; twi_info->transfer_finished = false; ret_code_t ret_code = nrf_drv_twi_tx(twi, twi_address(address), (uint8_t const *)data, length, (stop == 0)); if (ret_code != NRF_SUCCESS) { return 0; } while (!twi_info->transfer_finished) {} return nrf_drv_twi_data_count_get(twi); }
void ready_rfid_shield(void){ nrf_drv_twi_tx(&twi_rfid, ADR_RFID_SLAVE, &rfid_init_array_1[0], sizeof(rfid_init_array_1), false); nrf_delay_ms(3); nrf_drv_twi_rx(&twi_rfid, ADR_RFID_SLAVE, &dummy_array[0], 7); nrf_delay_ms(3); nrf_drv_twi_rx(&twi_rfid, ADR_RFID_SLAVE, &dummy_array[0], 14); nrf_delay_ms(3); nrf_drv_twi_tx(&twi_rfid, ADR_RFID_SLAVE, &rfid_init_array_2[0], sizeof(rfid_init_array_2), false); nrf_delay_ms(3); nrf_drv_twi_rx(&twi_rfid, ADR_RFID_SLAVE, &dummy_array[0], 7); nrf_delay_ms(3); nrf_drv_twi_rx(&twi_rfid, ADR_RFID_SLAVE, &dummy_array[0], 9); nrf_delay_ms(3); nrf_drv_twi_tx(&twi_rfid, ADR_RFID_SLAVE, &rfid_init_array_3[0], sizeof(rfid_init_array_3), false); nrf_delay_ms(3); nrf_drv_twi_rx(&twi_rfid, ADR_RFID_SLAVE, &dummy_array[0], 7); }
//resets everything void lps331ap_power_off() { uint8_t command[2] = { CTRL_REG1, POWER_OFF }; nrf_drv_twi_tx( m_instance, SENSOR_ADDR, command, sizeof(command), false ); }
void lps331ap_config_data_rate(lps331ap_data_rate data_rate) { uint8_t command[2] = { CTRL_REG1, (data_rate << 4) }; nrf_drv_twi_tx( m_instance, SENSOR_ADDR, command, sizeof(command), false ); }
void lps331ap_sw_reset_disable() { uint8_t command[2] = { CTRL_REG2, 0x00 }; nrf_drv_twi_tx( m_instance, SENSOR_ADDR, command, sizeof(command), false ); }
/** * @brief Function for main application entry. */ int main(void) { ret_code_t ret_code; nrf_gpio_cfg_output(LED_1); nrf_gpio_pin_set(LED_1); nrf_gpio_cfg_output(LED_2); nrf_gpio_pin_set(LED_2); uart_config(); printf("\033[2J\033[;H\r\n* Example to find TWI devices connected to \r\n* the TWI bus and their addresses\r\n\r\n"); twi_init(); uint8_t dummy_data = 0x55; // Itterate through all possible 7-bit TWI addresses for(uint8_t i = 0; i <= 0x7F; i++) { device_address = i; // Send dummy data. If a device is present on this particular address a TWI EVT_DONE event is // received in the twi event handler and a message is printed to UART ret_code = nrf_drv_twi_tx(&twi_instance, i, &dummy_data, 1, false); APP_ERROR_CHECK(ret_code); // Delay 10 ms to allow TWI transfer to complete and UART to print messages before starting new transfer nrf_delay_ms(10); } if(device_found) { // Blinke LED_1 rapidly if device is found while(true) { nrf_gpio_pin_toggle(LED_1); nrf_delay_ms(1000); } } else { // Disable LED_1 if device is NOT found nrf_gpio_cfg_default(LED_1); printf("*****************\n\rNO DEVICES FOUND!\r\n*****************\n\r"); while(true) { nrf_gpio_pin_toggle(LED_2); nrf_delay_ms(100);; } } }
void si7021_read_user_reg(uint8_t *reg_status){ uint8_t command[] = {Read_User_Reg_1}; nrf_drv_twi_tx( m_instance, TEMP_HUM_ADDR_THIS, command, sizeof(command), true ); nrf_drv_twi_rx( m_instance, TEMP_HUM_ADDR_THIS, reg_status, sizeof(reg_status), false ); }
void si7021_read_firmware_rev(uint8_t *buf){ uint8_t command[2] = { Read_Firm_Rev_1, Read_Firm_Rev_2 }; nrf_drv_twi_tx( m_instance, TEMP_HUM_ADDR_THIS, command, sizeof(command), true ); nrf_drv_twi_rx( m_instance, TEMP_HUM_ADDR_THIS, buf, sizeof(buf), false ); }
void si7021_read_temp_after_RH(float *temp){ uint8_t command[] = {Read_Temp_From_Prev_RH}; nrf_drv_twi_tx( m_instance, TEMP_HUM_ADDR_THIS, command, sizeof(command), true ); uint8_t temp_hum_data[3] = {0, 0, 0}; nrf_drv_twi_rx( m_instance, TEMP_HUM_ADDR_THIS, temp_hum_data, sizeof(temp_hum_data), false ); float temperature = -46.85 + (175.72 * (((uint32_t) temp_hum_data[0] << 8) | ((uint32_t) temp_hum_data[1] & 0xfc)) / (1 << 16)); *temp = temperature; }
void si7021_read_temp_hold(float * temp){ uint8_t command[] = {Meas_Temp_Hold_Master}; nrf_drv_twi_tx( m_instance, TEMP_HUM_ADDR_THIS, command, sizeof(command), true ); uint8_t temp_hum_data[3] = {0x00, 0x00, 0x00}; nrf_drv_twi_rx( m_instance, TEMP_HUM_ADDR_THIS, temp_hum_data, sizeof(temp_hum_data), false ); float temperature = -46.85 + (175.72 * (((uint32_t) temp_hum_data[0] << 8) | ((uint32_t) temp_hum_data[1] & 0xfc)) / (1 << 16)); *temp = temperature; }
void lps331ap_amp_control(bool selmain) { uint8_t command[2] = { AMP_CTRL, 0x00 }; if (selmain) { command[2] = 0x01; } nrf_drv_twi_tx( m_instance, SENSOR_ADDR, command, sizeof(command), false ); }
{ return err_code; } return NRF_SUCCESS; } ret_code_t mcp4725_set_voltage(uint16_t val, bool write_eeprom) { /* Shift parameter val to get 2 8-bits values. */ uint8_t reg[3] = {write_eeprom ? MCP4725_EEPROM_ADDRESS : MCP4725_DAC_ADDRESS, (val>>4), (val<<4)}; m_xfer_done = false; ret_code_t err_code = nrf_drv_twi_tx(&m_twi, MCP4725_BASE_ADDRESS, reg, sizeof(reg), false); if (err_code != NRF_SUCCESS) { return err_code; } while (m_xfer_done == false); return NRF_SUCCESS; } bool mcp4725_is_busy(void) { uint8_t busy; m_read_done = false;