unsigned int srf08_ping(unsigned char range_unit) { union i2c_union { unsigned int rx_word; unsigned char rx_byte[2]; } i2c; I2C_START_TX(address); i2c_transmit(SRF08_COMMAND); i2c_transmit(range_unit); i2c_stop(); /* INSTEAD OF WAITING USING A DELAY LOOP, POLL SRF08 FOR COMPLETION OF RANGING */ do{ I2C_START_TX(address); i2c_transmit(SRF08_ECHO_1); I2C_START_RX(address); i2c.rx_byte[1]=i2c_receive(I2C_CONTINUE); /* get high byte msb first */ i2c.rx_byte[0]=i2c_receive(I2C_QUIT); i2c_stop(); }while(i2c.rx_byte[0] >= 0xFF); I2C_START_TX(address); i2c_transmit(SRF08_ECHO_1); I2C_START_RX(address); i2c.rx_byte[1]=i2c_receive(I2C_CONTINUE); /* get high byte msb first */ i2c.rx_byte[0]=i2c_receive(I2C_QUIT); /* get low byte msb first */ i2c_stop(); return(i2c.rx_word); }
void airspeed_amsys_read_periodic( void ) { #ifndef SITL if (airspeed_amsys_i2c_trans.status == I2CTransDone) #ifndef MEASURE_AMSYS_TEMPERATURE i2c_receive(&AIRSPEED_AMSYS_I2C_DEV, &airspeed_amsys_i2c_trans, AIRSPEED_AMSYS_ADDR, 2); #else i2c_receive(&AIRSPEED_AMSYS_I2C_DEV, &airspeed_amsys_i2c_trans, AIRSPEED_AMSYS_ADDR, 4); #endif #else // SITL extern float sim_air_speed; stateSetAirspeed_f(&sim_air_speed); #endif //SITL }
void ms5607_init( ms5607_conf_t *conf){ uint8_t address = 0xEE; uint16_t value = 0; for( uint8_t i = 0; i < 6 ; ++i){ i2c_transmit(address, 1, 0 ); i2c_transmit(0xA2 + 2*i, 0, 1); i2c_transmit(address | 0x01, 1, 0 ); value = i2c_receive(0,0); value <<= 8; value |= i2c_receive(1,1); conf->coeffs[i] = value; } }
int eps_slave_ping(struct command_context *ctx) { i2c_frame_t * frame; frame = csp_buffer_get(I2C_MTU); frame->dest = slave_node; frame->data[0] = CSP_PING; // Ping port frame->data[1] = 0x55; frame->len = 2; frame->len_rx = 3; frame->retries = 0; if (i2c_send(0, frame, 0) != E_NO_ERR) { csp_buffer_free(frame); return CMD_ERROR_NOMEM; } if (i2c_receive(0, &frame, 20) == E_NO_ERR) { printf("Received a reply from EPS!\r\n"); } else { printf("No reply from EPS\r\n"); } csp_buffer_free(frame); return CMD_ERROR_NONE; }
/******************************************************************************* * error_t hmc5883l_rawdata_read(vector_int16_t* magnetic_field) * * Description: * Read HMC5883L raw magnetic field data. Magnetic field data is stored as a * 3-dimensional 16-bit unsigned integer vector where only the lowest 12-bits * contain data. Possible errors are reported to the calling function. * * Parameters: * magnetic_field memory address of 3-dimensional magnetic field vector * * Return: * error ERR_OK - completed without error * ERR_FAIL - failed to write register * * Example: * error = hmc5883l_rawdata_read(&magnetic_field); * * Note: * * History: * pka, 03/SEP/2015, initial code ******************************************************************************/ error_t hmc5883l_rawdata_read(vector_int16_t* magnetic_field) { uint8_t tmp[6]; tmp[0] = HMC5883L_DATA_X_MSB; /* Transmit START condition and slave address + WRITE. Write register * address and generate STOP condition. */ if (i2c_transmit(HMC5883L_SLAVE_ADDR, tmp, 1) != ERR_OK) { return ERR_FAIL; } /* Transmit START condition and slave address + READ. Read register value * and generate STOP condition. */ if (i2c_receive(HMC5883L_SLAVE_ADDR, tmp, 6) != ERR_OK) { return ERR_FAIL; } /* Create 3-dimensional magnetic field vector. Note: The HMC5883L provides * raw data in the order X, Z, Y. */ magnetic_field->x = (tmp[0] << 8) | tmp[1]; magnetic_field->y = (tmp[4] << 8) | tmp[5]; magnetic_field->z = (tmp[2] << 8) | tmp[3]; return ERR_OK; }
int eps_slave_hk2(struct command_context *ctx) { printf("Requesting EPS HK2 data\r\n"); eps_hk_t * chkparam; i2c_frame_t * frame; frame = csp_buffer_get(I2C_MTU); frame->dest = slave_node; frame->data[0] = EPS_PORT_HK; frame->data[1] = 0; frame->len = 2; frame->len_rx = 2 + (uint8_t) sizeof(eps_hk_t); frame->retries = 0; if (i2c_send(0, frame, 0) != E_NO_ERR) { csp_buffer_free(frame); return CMD_ERROR_NOMEM; } if (i2c_receive(0, &frame, 20) != E_NO_ERR) return CMD_ERROR_FAIL; chkparam = (eps_hk_t *)&frame->data[2]; eps_hk_unpack(chkparam); eps_hk_print(chkparam); csp_buffer_free(frame); return CMD_ERROR_NONE; }
void ms45xx_i2c_periodic(void) { // Initiate next read if (ms45xx_trans.status == I2CTransDone) { i2c_receive(&MS45XX_I2C_DEV, &ms45xx_trans, MS45XX_I2C_ADDR, 4); } }
/* needs 29ms delay from humid trigger measurement */ void humid_sht_p_humid( void ) { if (sht_status == SHT2_GET_HUMID) { /* read humid */ sht_status = SHT2_READ_HUMID; i2c_receive(&SHT_I2C_DEV, &sht_trans, SHT_SLAVE_ADDR, 3); } }
/* needs 85ms delay from temp trigger measurement */ void humid_sht_p_temp( void ) { if (sht_status == SHT2_GET_TEMP) { /* get temp */ sht_status = SHT2_READ_TEMP; i2c_receive(&SHT_I2C_DEV, &sht_trans, SHT_SLAVE_ADDR, 3); } }
irom static io_error_t read_register(string_t *error_message, int address, int reg, int *value) { uint8_t i2cbuffer[2]; i2c_error_t error; i2cbuffer[0] = reg; if((error = i2c_send(address, 1, &i2cbuffer[0])) != i2c_error_ok) { if(error_message) i2c_error_format_string(error_message, error); return(io_error); } if((error = i2c_receive(address, 1, &i2cbuffer[1])) != i2c_error_ok) { if(error_message) i2c_error_format_string(error_message, error); return(io_error); } *value = i2cbuffer[1]; return(io_ok); }
void i2c_init_early(void) { LTRACE_ENTRY; /* enable clocks on i2c 0-2 */ RMWREG32(CM_FCLKEN1_CORE, 15, 3, 0x7), RMWREG32(CM_ICLKEN1_CORE, 15, 3, 0x7), i2c_reset_bus(0); i2c_reset_bus(1); i2c_reset_bus(2); #if 0 // write something into a reg char buf[2]; i2c_write_reg(0, 0x4b, 0x14, 0x99); i2c_write_reg(0, 0x4b, 0x15, 0x98); i2c_read_reg(0, 0x4b, 0x15, buf); printf("0x%hhx\n", buf[0]); i2c_read_reg(0, 0x4b, 0x14, buf); printf("0x%hhx\n", buf[0]); int i; for (i=0; i < 255; i++) { char buf[1]; buf[0] = i; i2c_transmit(0, 0x4b, buf, 1); i2c_receive(0, 0x4b, buf, sizeof(buf)); printf("0x%hhx\n", buf[0]); } #endif LTRACE_EXIT; }
//------------------------------------------------------------------------------ // void i2c_receiveinit(uint8_t slave_address, // uint8_t prescale) // // This function initializes the USCI module for master-receive operation. // // IN: uint8_t slave_address => Slave Address // uint8_t prescale => SCL clock adjustment //----------------------------------------------------------------------------- //static volatile uint8_t rx_byte_tot = 0; void i2c_receiveinit(uint8_t slave_address, uint8_t byte_ctr, uint8_t *rx_buf) { // wait for bus to be free before setting MSTA (assuming we're in a multi-master environment or still sending something else) while(i2c_busy()) /* wait */; // assert: rx_byte_ctr <= 0 // assert: tx_byte_ctr <= 0 tx_buf_ptr = 0; tx_byte_ctr = 0; // indicate that nothing is to be received rx_byte_ctr = byte_ctr; rx_buf_ptr = rx_buf; // clockdiv //*I2CFDR = 0x20; // 150 khz for redbee econotag // assume being master, thus no addres has to be set *I2CCR = I2C_MEN | #ifdef I2C_NON_BLOCKING I2C_MIEN | #endif I2C_MSTA | I2C_MTX | I2C_RXAK; // start condition is triggered // write out address of slave *I2CDR = (slave_address & 0x7f) <<1 | 0x01; #ifndef I2C_NON_BLOCKING i2c_receive(); #endif }
// // Receive from a slave I2C device // Assumes first byte is the address. // Length is the address, register to read // and the value to be read // static int i2c_receive_flash( BYTE i2c_a, int mem_a, BYTE *b, BYTE len ) { BYTE c; // Wait for stop while(I2CS & bmSTOP); // Send address field I2CS = bmSTART; I2DAT = i2c_a&0xFE;// LSB = 0 // Wait for ACK while(!((c=I2CS)&bmDONE));//Wait done //check ACK if((c&bmBERR)||(!(c&bmACK))) goto leave; // MSB of Address I2DAT = mem_a>>8; // Wait for Done while(!((c=I2CS)&1)); // LSB of Address I2DAT = mem_a&0xFF; // Wait for Done while(!((c=I2CS)&bmDONE)); // // The address is set up so we now need // to read its contents // i2c_receive( i2c_a, b, len ); return 0; leave: I2CS |= bmSTOP; return -1; }
status_t i2c_read_reg_bytes(int bus, uint8_t address, uint8_t reg, uint8_t *val, size_t cnt) { int err = i2c_transmit(bus, address, ®, 1); if (err < 0) return err; return i2c_receive(bus, address, val, cnt); }
int i2c_read_reg(int bus, uint8_t address, uint8_t reg, uint8_t *val) { int err = i2c_transmit(bus, address, ®, 1); if (err < 0) return err; return i2c_receive(bus, address, val, 1); }
void dust_gp2y_periodic(void) { if (dust_gp2y_status == DUST_GP2Y_IDLE) { i2c_receive(&GP2Y_I2C_DEV, &gp2y_trans, GP2Y_SLAVE_ADDR, 2); } else if (dust_gp2y_status == DUST_GP2Y_UNINIT && sys_time.nb_sec > 1) { dust_gp2y_status = DUST_GP2Y_IDLE; } }
void imu_read_data(struct IMU* data) { uint8_t values[6]; uint8_t reg = GYRO_OUT_X_L | 0x80; //Set high bit for auto address increment i2c_transmit(GYRO_ADDR, ®, 1); i2c_receive(GYRO_ADDR, &values, 6); data->gyro.x = (int16_t)(values[0] | (values[1]<<8)); data->gyro.y = (int16_t)(values[2] | (values[3]<<8)); data->gyro.z = (int16_t)(values[4] | (values[5]<<8)); reg = ACCEL_OUT_X_L_A | 0x80; //Set high bit for auto address increment i2c_transmit(ACCEL_ADDR, ®, 1); i2c_receive(ACCEL_ADDR, &values, 6); data->accel.x = (int16_t)(values[0] | (values[1]<<8)); data->accel.y = (int16_t)(values[2] | (values[3]<<8)); data->accel.z = (int16_t)(values[4] | (values[5]<<8)); }
void ArduIMU_periodic(void) { //Frequence defined in conf/modules/ins_arduimu.xml if (ardu_ins_trans.status == I2CTransDone) { i2c_receive(&ARDUIMU_I2C_DEV, &ardu_ins_trans, ArduIMU_SLAVE_ADDR, NB_DATA * 2); } }
void airspeed_ets_read_periodic( void ) { #ifndef SITL if (airspeed_ets_i2c_trans.status == I2CTransDone) i2c_receive(&AIRSPEED_ETS_I2C_DEV, &airspeed_ets_i2c_trans, AIRSPEED_ETS_ADDR, 2); #else // SITL extern float sim_air_speed; stateSetAirspeed_f(&sim_air_speed); #endif //SITL }
void baro_amsys_read_periodic(void) { // Initiate next read if (baro_amsys_i2c_trans.status == I2CTransDone) { #ifndef MEASURE_AMSYS_TEMPERATURE i2c_receive(&BARO_AMSYS_I2C_DEV, &baro_amsys_i2c_trans, BARO_AMSYS_ADDR, 2); #else i2c_receive(&BARO_AMSYS_I2C_DEV, &baro_amsys_i2c_trans, BARO_AMSYS_ADDR, 4); #endif } #ifdef BARO_AMSYS_SYNC_SEND DOWNLINK_SEND_AMSYS_BARO(DefaultChannel, DefaultDevice, &pBaroRaw, &baro_amsys_p, &baro_amsys_offset, &ref_alt_init, &baro_amsys_abs_altitude, &baro_amsys_altitude, &baro_amsys_temp); #else RunOnceEvery(10, DOWNLINK_SEND_AMSYS_BARO(DefaultChannel, DefaultDevice, &pBaroRaw, &baro_amsys_p, &baro_amsys_offset, &ref_alt_init, &baro_amsys_abs_altitude, &baro_amsys_altitude, &baro_amsys_temp)); #endif }
void baro_ets_read_periodic( void ) { // Initiate next read if (!baro_ets_delay_done) { if (SysTimeTimer(baro_ets_delay_time) < USEC_OF_SEC(BARO_ETS_START_DELAY)) return; else baro_ets_delay_done = TRUE; } if (baro_ets_i2c_trans.status == I2CTransDone) { i2c_receive(&BARO_ETS_I2C_DEV, &baro_ets_i2c_trans, BARO_ETS_ADDR, 2); } }
void eos_remote_handle_message(unsigned char* message) { unsigned char slave_address = message[0]; unsigned char msg_datalen = message[3]; if (EOS_DISABLE_REMOTE) return; if (slave_address > 127) return; if (!TI_USCI_I2C_slave_present(slave_address)) { return; } static unsigned char leadin_bytes[] = {0xbb, 0x88}; i2c_transmit(slave_address, &leadin_bytes[0], 2); i2c_transmit(slave_address, message, 5 + msg_datalen); unsigned char response[40]; unsigned char response_length[1]; unsigned char count = 0; while (++count) { i2c_receive(slave_address, &response_length[0], 1); if (response_length[0] != 0x01) { break; // 0x01 means the slave has an unprocessed packet // in its queue, so keep trying } if (count == 20) return; // cancel after "timeout" } if (response_length[0] > 0 && response_length[0] < 40) { i2c_receive(slave_address, &response[0], response_length[0]); eosprotocol_send_message(response, response_length[0]); } }
uint8_t I2CWire::receiveFrom(uint8_t address, uint16 quantity) { // clamp to buffer length if(quantity > BUFFER_LENGTH) quantity = BUFFER_LENGTH; rxposition = 0; if ( i2c_receive(&i2cbuf, rxbuffer, quantity) ) { return (rxlength = quantity); } return 0; }
void lm75_getTemp(struct lm75 *s) { uint8_t data[2]; // retrieve temp data from LM75 data[0] = (uint8_t)TEMP_ADDR; i2c_transmit(s->i2cAddr, data, 1); i2c_receive(s->i2cAddr, data, 2); // discard lower 0.5°C bit s->temperature = (int8_t)data[0]; }
void wind_gfi_event(void) { if (pcf_trans.status == I2CTransSuccess) { if (pcf_status == PCF_SET_OE_LSB) { pcf_status = PCF_READ_LSB; i2c_receive(&PCF_I2C_DEV, &pcf_trans, PCF_SLAVE_ADDR, 2); } else if (pcf_status == PCF_READ_LSB) { /* read lower byte direction info */ pcf_direction = pcf_trans.buf[0]; /* OE low, SEL low (for high data) */ pcf_trans.buf[0] = 0xFF; pcf_trans.buf[1] = 0x3F; pcf_status = PCF_SET_OE_MSB; i2c_transmit(&PCF_I2C_DEV, &pcf_trans, PCF_SLAVE_ADDR, 2); } else if (pcf_status == PCF_SET_OE_MSB) { pcf_status = PCF_READ_MSB; i2c_receive(&PCF_I2C_DEV, &pcf_trans, PCF_SLAVE_ADDR, 2); } else if (pcf_status == PCF_READ_MSB) { float fpcf_direction; /* read higher byte direction info */ pcf_direction |= pcf_trans.buf[0] << 8; /* OE high, SEL high */ pcf_trans.buf[0] = 0xFF; pcf_trans.buf[1] = 0xFF; pcf_status = PCF_IDLE; i2c_transmit(&PCF_I2C_DEV, &pcf_trans, PCF_SLAVE_ADDR, 2); /* 2048 digits per 360 degrees */ fpcf_direction = fmod((pcf_direction * (360. / 2048.)) + ZERO_OFFSET_DEGREES, 360.); DOWNLINK_SEND_TMP_STATUS(DefaultChannel, DefaultDevice, &pcf_direction, &fpcf_direction); } else if (pcf_status == PCF_IDLE) { pcf_trans.status = I2CTransDone; } } }
uint32_t ms5607_read_d1(){ uint8_t address = 0xEE; uint32_t value = 0; i2c_transmit(address, 1, 0); i2c_transmit(0x42, 0, 1); i2c_transmit(address, 1, 0); i2c_transmit(0x00, 0, 1); _delay_ms(10); i2c_transmit(address | 0x01, 1, 0); value = i2c_receive(0, 0); value <<= 8; value |= i2c_receive(0, 0); value <<= 8; value |= i2c_receive(1, 1); return value; }
unsigned int srf08_read_register(unsigned char srf08_register) { union i2c_union { unsigned int rx_word; unsigned char rx_byte[2]; } i2c; I2C_START_TX(address); i2c_transmit(srf08_register); I2C_START_RX(address); /* get high byte msb first */ if(srf08_register>=2) { i2c.rx_byte[1]=i2c_receive(I2C_CONTINUE); } /* get low byte msb first */ i2c.rx_byte[0]=i2c_receive(I2C_QUIT); i2c_stop(); return(i2c.rx_word); }
void airspeed_ets_read_periodic( void ) { #ifndef SITL if (!airspeed_ets_delay_done) { if (SysTimeTimer(airspeed_ets_delay_time) < USEC_OF_SEC(AIRSPEED_ETS_START_DELAY)) return; else airspeed_ets_delay_done = TRUE; } if (airspeed_ets_i2c_trans.status == I2CTransDone) i2c_receive(&AIRSPEED_ETS_I2C_DEV, &airspeed_ets_i2c_trans, AIRSPEED_ETS_ADDR, 2); #elif !defined USE_NPS extern float sim_air_speed; stateSetAirspeed_f(&sim_air_speed); #endif //SITL }
static int i2c_read_hand(int hand) { uint8_t buf[5]; i2c_status_t ret = i2c_receive(I2C_ADDR(hand), buf, sizeof(buf), I2C_TIMEOUT); if (ret != I2C_STATUS_SUCCESS) return 1; if (buf[0] != TWI_REPLY_KEYDATA) return 2; int start_row = hand ? ROWS_PER_HAND : 0; uint8_t *out = &rows[start_row]; memcpy(out, &buf[1], 4); return 0; }
void i2c_read_registers(uint8_t bus_num, uint8_t slave_addr, uint8_t reg_addr, uint8_t numBytes, uint8_t* spaceToWrite) { uint8_t i2c_packet[1] = {reg_addr}; //transmit reg address to gyro i2c_init_transmit(bus_num,slave_addr); while ( i2c_busy(bus_num) ); i2c_transmit(bus_num,sizeof(i2c_packet),i2c_packet); while ( i2c_busy(bus_num) ); __delay_cycles(I2C_BUS_FREE_TIME); //read what gyro has to say i2c_init_receive(bus_num,slave_addr); while ( i2c_busy(bus_num) ); i2c_receive(bus_num,numBytes,spaceToWrite); while ( i2c_busy(bus_num) ); __delay_cycles(I2C_BUS_FREE_TIME); }