void i2c_write_register(uint8_t bus_num, uint8_t slave_addr, uint8_t reg_addr, uint8_t reg_setting) { uint8_t i2c_packet[2] = {reg_addr, reg_setting}; 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); }
void accm_write_stream(u8_t len, u8_t *data) { i2c_transmitinit(ADXL345_ADDR); while (i2c_busy()); PRINTFDEBUG("I2C Ready to TX(stream)\n"); i2c_transmit_n(len, data); // start tx and send conf reg while (i2c_busy()); PRINTFDEBUG("WRITE_STR %u B to 0x%02X\n", len, data[0]); }
/** data contains the register as a first element if needed. Note that i2c is a transport mechanism and whether there is a register as a first param or not is app/device dependent */ void i2c_write_register(uint8_t bus_num, uint8_t slave_addr, uint8_t length, uint8_t* data) { i2c_init_transmit(bus_num,slave_addr); while( i2c_busy(bus_num) ); i2c_transmit(bus_num,length,data); while( i2c_busy(bus_num) ); __delay_cycles(I2C_BUS_FREE_TIME); }
void tlc59116_write_stream(uint8_t len, uint8_t *data) { i2c_transmitinit(TLC59116_ADDR); while(i2c_busy()); PRINTFDEBUG("I2C Ready to TX(stream)\n"); i2c_transmit_n(len, data); /* start tx and send conf reg */ while(i2c_busy()); PRINTFDEBUG("WRITE_STR %u B to 0x%02X\n", len, data[0]); }
void accm_write_reg(u8_t reg, u8_t val) { u8_t tx_buf[] = {reg, val}; i2c_transmitinit(ADXL345_ADDR); while (i2c_busy()); PRINTFDEBUG("I2C Ready to TX\n"); i2c_transmit_n(2, tx_buf); while (i2c_busy()); PRINTFDEBUG("WRITE_REG 0x%02X @ reg 0x%02X\n", val, reg); }
void tlc59116_write_reg(uint8_t reg, uint8_t val) { uint8_t tx_buf[] = { reg, val }; i2c_transmitinit(TLC59116_ADDR); while(i2c_busy()); PRINTFDEBUG("I2C Ready to TX\n"); i2c_transmit_n(2, tx_buf); while(i2c_busy()); PRINTFDEBUG("WRITE_REG 0x%02X @ reg 0x%02X\n", val, reg); }
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); }
//------------------------------------------------------------------------------ // 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 }
void write_i2c(uint32_t i2c, uint8_t i2c_addr, uint8_t reg, uint8_t size, uint8_t *data) { int wait; int i; while (i2c_busy(i2c) == 1); while (i2c_is_start(i2c) == 1); /*Setting transfer properties*/ i2c_set_bytes_to_transfer(i2c, size + 1); i2c_set_7bit_address(i2c, (i2c_addr & 0x7F)); i2c_set_write_transfer_dir(i2c); i2c_enable_autoend(i2c); /*start transfer*/ i2c_send_start(i2c); wait = true; while (wait) { if (i2c_transmit_int_status(i2c)) { wait = false; } while (i2c_nack(i2c)); } i2c_send_data(i2c, reg); for (i = 0; i < size; i++) { wait = true; while (wait) { if (i2c_transmit_int_status(i2c)) { wait = false; } while (i2c_nack(i2c)); } i2c_send_data(i2c, data[i]); } }
int i2c_read_bytes(i2c_t dev, uint8_t address, void *data, int length) { switch (dev) { #if I2C_0_EN case I2C_0: break; #endif default: return -1; } WARN_IF(I2CM_STAT & BUSY); if ( (length <= 0) || i2c_busy() ) { return 0; } WARN_IF(I2CM_STAT & BUSBSY); if (I2CM_STAT & BUSBSY) { recover_i2c_bus(); if (I2CM_STAT & BUSBSY) { return 0; } } return i2c_read_bytes_dumb(address, data, length); }
// Write address void LED_writeReg( uint8_t bus, uint8_t addr, uint8_t reg, uint8_t val, uint8_t page ) { /* info_msg("I2C Write bus("); printHex( bus ); print(")addr("); printHex( addr ); print(")reg("); printHex( reg ); print(")val("); printHex( val ); print(")page("); printHex( page ); print(")" NL); */ // Reg Write Setup uint16_t writeData[] = { addr, reg, val }; // Setup page LED_setupPage( bus, addr, page ); // Write register while ( i2c_send( bus, writeData, sizeof( writeData ) / 2 ) == -1 ) delay_us( ISSI_SendDelay ); // Delay until written while ( i2c_busy( bus ) ) delay_us( ISSI_SendDelay ); }
//----------------------------------------------------------------------------- // unsigned char i2c_slave_present(unsigned char slave_address) // // This function is used to look for a slave address on the I2C bus. // It sends a START and STOP condition back-to-back and wait for an ACK from the slave // // IN: unsigned char slave_address => Slave Address // OUT: unsigned char => 0: address was not found, // 1: address found //----------------------------------------------------------------------------- unsigned char i2c_slave_present(int bus_num, unsigned char slave_address) { unsigned char uc1ie_bak, slaveadr_bak, ucb1i2cie_bak, returnValue; //store state ucb1i2cie_bak = *i2c_control.i2cie[bus_num]; // store *i2c_control.i2cie[bus_num] register uc1ie_bak = *i2c_control.ie[bus_num]; // store UC1IE register slaveadr_bak = *i2c_control.i2csa[bus_num]; // store old slave address //start i2c while ( i2c_busy(bus_num) ); i2c_init_transmit(bus_num,slave_address); // *i2c_control.i2cie[bus_num] &= ~UCNACKIE; // no NACK interrupt *i2c_control.i2csa[bus_num] = slave_address; // set slave address *i2c_control.ie[bus_num] &= ~(i2c_control.ietx[bus_num] + i2c_control.ierx[bus_num]); // no RX or TX interrupts __disable_interrupt(); *i2c_control.ctl1[bus_num] |= UCTR + UCTXSTT + UCTXSTP; // transmitter, start and stop condition while (*i2c_control.ctl1[bus_num] & UCTXSTP); // wait for STOP condition returnValue = !(*i2c_control.stat[bus_num] & UCNACKIFG); // 0 if an ACK was received __enable_interrupt(); //restore state *i2c_control.ie[bus_num] = uc1ie_bak; // restore IE2 *i2c_control.i2csa[bus_num] = slaveadr_bak; // restore old slave address *i2c_control.i2cie[bus_num] = ucb1i2cie_bak; // restore old UCB0CTL1 return returnValue; // return whether or not }
void accm_read_stream(u8_t reg, u8_t len, u8_t *whereto) { u8_t rtx = reg; PRINTFDEBUG("READ_STR %u B from 0x%02X\n", len, reg); /* transmit the register to start reading from */ i2c_transmitinit(ADXL345_ADDR); while (i2c_busy()); i2c_transmit_n(1, &rtx); while (i2c_busy()); /* receive the data */ i2c_receiveinit(ADXL345_ADDR); while (i2c_busy()); i2c_receive_n(len, whereto); while (i2c_busy()); }
uint8_t i2c_any_busy() { for ( uint8_t ch = ISSI_I2C_FirstBus_define; ch < ISSI_I2C_Buses_define + ISSI_I2C_FirstBus_define; ch++ ) { if ( i2c_busy( ch ) ) return 1; } return 0; }
uint8_t I2CBusy(uint8_t address) { uint8_t result; lockset(lock); result = i2c_busy(i2c_bus, address); lockclr(lock); return result; }
uint8_t I2C_Ready(uint8_t addr) { print("i2c_ready enter\n"); while (i2c_busy(i2c_bus, addr)) { ; } print("i2c_ready exit\n"); }
SINT send_nxtcam_command(U8 port_id, U8 command) { while (i2c_busy(port_id) != 0) ; nxtcambuffer[0] = command; /* write Single shot command */ SINT ret = i2c_start_transaction(port_id, ADDRESS, 0x41, 1, nxtcambuffer, 1, 1); return ret; }
u8_t accm_read_reg(u8_t reg) { u8_t retVal = 0; u8_t rtx = reg; PRINTFDEBUG("READ_REG 0x%02X\n", reg); /* transmit the register to read */ i2c_transmitinit(ADXL345_ADDR); while (i2c_busy()); i2c_transmit_n(1, &rtx); while (i2c_busy()); /* receive the data */ i2c_receiveinit(ADXL345_ADDR); while (i2c_busy()); i2c_receive_n(1, &retVal); while (i2c_busy()); return retVal; }
/*---------------------------------------------------------------------------*/ static uint16_t sht25_read_reg(uint8_t reg) { uint8_t buf[] = { 0x00, 0x00 }; uint16_t retval; uint8_t rtx = reg; /* transmit the register to read */ i2c_transmitinit(SHT25_ADDR); while(i2c_busy()); i2c_transmit_n(1, &rtx); while(i2c_busy()); /* receive the data */ i2c_receiveinit(SHT25_ADDR); while(i2c_busy()); i2c_receive_n(2, &buf[0]); while(i2c_busy()); retval = (uint16_t)(buf[0] << 8 | (buf[1])); return retval; }
uint8_t tlc59116_read_reg(uint8_t reg) { uint8_t retVal = 0; uint8_t rtx = reg; PRINTFDEBUG("READ_REG 0x%02X\n", reg); /* transmit the register to read */ i2c_transmitinit(TLC59116_ADDR); while(i2c_busy()); i2c_transmit_n(1, &rtx); while(i2c_busy()); /* receive the data */ i2c_receiveinit(TLC59116_ADDR); while(i2c_busy()); i2c_receive_n(1, &retVal); while(i2c_busy()); return retVal; }
void write_ti(uint8_t reg, uint8_t value) { uint8_t temp; while(i2c_busy()) /* wait */; *I2CCR = 0; // assume being master, thus no addres has to be set *I2CCR = I2C_MEN | I2C_MSTA | I2C_MTX; // start condition is triggered // write out address of slave *I2CDR = 0x68 << 1; while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/; if (*I2CSR & I2C_RXAK ) { // NO acknoledge byte received printf("*** ERROR I2C: No ack received 1\n"); } if (*I2CSR & I2C_MCF) { *I2CDR = (reg & 0xFF); // clear MIF *I2CSR &= ~I2C_MIF; while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/; if (*I2CSR & I2C_RXAK) { // NO acknoledge byte received printf("*** ERROR I2C: No ack received 2\n"); } } if (*I2CSR & I2C_MCF) { *I2CDR = (value & 0xFF); // clear MIF *I2CSR &= ~I2C_MIF; while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/; if (*I2CSR & I2C_RXAK) { // NO acknoledge byte received printf("*** ERROR I2C: No ack received 2\n"); } } *I2CCR &= ~I2C_MSTA; // stop condition //*I2CCR = 0; }
int nxtrike_get_sonic_sensor(void) { return ev3_ultrasonic_sensor_get_distance(SONIC_SENSOR); #if 0 U8 distance; if (!i2c_busy(SONIC_SENSOR)) { i2c_start_transaction(SONIC_SENSOR, 1, 0x42, 1, &distance, 1, 0); return (int)distance; } return NXTRIKE_ERROR; #endif }
/* * This is an implementation example of I2C sensor data acquisition for * mindsensor acceleration sensor. mindsensor acceleration sensor returns * tilt data and acceleration data in three axes. * * This API implementation for I2C communication might be different from * I2C sensor communication examples in other NXT programming languages. * Others use a wait until data transaction is finished after sending a request. * However, it might not be acceptable for real-time control application. So we * introduce one sampling delay to avoid waiting for the completion of the data acqusition. */ void get_mindsensor_accel_sensor(U8 port_id, S16 *buf) { int i,j; static S16 tilt_state[3]; static S16 accel_state[3]; static U8 data[9] = {0,0,0,0,0,0,0,0,0}; /* * Data spec. of mindsensor acceleration sensor * * 0x42 data[0]: X axis Tilt data * 0x43 data[1]: Y axis Tilt data * 0x44 data[2]: Z axis Tilt data * 0x45 data[3]: X axis Accel LSB * 0x46 data[4]: X axis Accel MSB * 0x47 data[5]: Y axis Accel LSB * 0x48 data[6]: Y axis Accel MSB * 0x49 data[7]: Z axis Accel LSB * 0x4A data[8]: Z axis Accel MSB */ if (i2c_busy(port_id) == 0) /* check the status of I2C comm. */ { j=0; for (i=0; i<8; i++) { if (i<3) { tilt_state[i] = (S16)data[i]; } else { accel_state[j++] = ((S16)data[i+1]<<8) + (S16)data[i]; i++; } } /* i2c_start_transaction just triggers an I2C transaction, * actual data transaction between ARM7 and an Acceleration * Sensor is done by an ISR after this, so there is one execution cycle * delay for consistent data acquistion */ i2c_start_transaction(port_id,1,0x42,1,data,9,0); } for (i=0; i<3; i++) { buf[i] = tilt_state[i]; buf[i+3] = accel_state[i]; } }
//------------------------------------------------------------------------------ // 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 */; // Jagun@UCL - now using a timeout version uint16_t timeout = I2C_TIMEOUT; while(i2c_busy() && --timeout) ; // Wait if(!timeout) { //printf("\nDEBUG: I2C Timeout Detected in recvinit!!\n"); i2c_force_reset(); clock_delay_msec(6); //i2c_disable(); i2c_enable(); clock_delay_msec(6); *I2CSR &= ~I2C_MAL; // should be cleared in software // Arbitration lost; reset.. rx_byte_ctr = tx_byte_ctr = 0; *I2CCR &= ~I2C_MSTA; // generate stop condition // clear MIF *I2CSR &= ~I2C_MIF; *rx_buf = 0; return; // TODO; return some meaningful value in rx_buf! } // 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 }
int request(U8 port_id) { if (i2c_busy(port_id) == 0) /* check the status of I2C comm. */ { /* i2c_start_transaction just triggers an I2C transaction, * actual data transaction between ARM7 and an NxtCam is done * by an ISR after this, so there is one execution cycle * delay for consistent data acquistion */ SINT ret = i2c_start_transaction(port_id, ADDRESS, 0x42, 1, nxtcamdata, 41, 0); if (ret == 0) nxtcamtransaccounter++; } return nxtcamtransaccounter; }
void read_ti(uint8_t reg) { uint8_t temp; while(i2c_busy()) /* wait */; // assume being master, thus no addres has to be set *I2CCR |= I2C_MSTA | I2C_MTX | I2C_TXAK; // start condition is triggered // write out address of slave *I2CDR = 0x68 <<1; while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/; *I2CSR &= ~I2C_MIF; if (*I2CSR & I2C_RXAK ) { // NO acknoledge byte received printf("*** ERROR I2C: No ack received 1\n"); } *I2CDR = (reg & 0xFF); while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/; *I2CSR &= ~I2C_MIF; if (*I2CSR & I2C_RXAK) { // NO acknoledge byte received printf("*** ERROR I2C: No ack received 2\n"); } *I2CCR |= I2C_RSTA; // write out address of slave *I2CDR = ((0x68) <<1) | 0x01; while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/; *I2CSR &= ~I2C_MIF; *I2CCR &= ~I2C_MTX; //*I2CCR &= ~I2C_TXAK; temp = *I2CDR; while(!(*I2CSR & I2C_MCF) || !(*I2CSR & I2C_MIF)) /*wait*/; *I2CSR &= ~I2C_MIF; *I2CCR &= ~I2C_MSTA; // stop condition temp = *I2CDR; //printf("0x%x\n",temp); }
// Enables given page // IS31FL3733 requires unlocking the 0xFD register void LED_setupPage( uint8_t bus, uint8_t addr, uint8_t page ) { #if ISSI_Chip_31FL3733_define == 1 // See http://www.issi.com/WW/pdf/31FL3733.pdf Table 3 Page 12 uint16_t pageEnable[] = { addr, 0xFE, 0xC5 }; while ( i2c_send( bus, pageEnable, sizeof( pageEnable ) / 2 ) == -1 ) delay_us( ISSI_SendDelay ); #endif // Setup page uint16_t pageSetup[] = { addr, 0xFD, page }; while ( i2c_send( bus, pageSetup, sizeof( pageSetup ) / 2 ) == -1 ) delay_us( ISSI_SendDelay ); // Delay until written while ( i2c_busy( bus ) ) delay_us( ISSI_SendDelay ); }
inline std_return RTE_Output_SetOutput_OSPort_Out(const char* message) { #ifdef RTE_Output_SetOutput_OSPort_Out_DISPLAY print(message); #endif #ifdef RTE_Output_SetOutput_OSPort_Out_IIC uint8_t data_IIC = 0x5F; /* wenn 0 auf ein LED ausgegeben wird, dann leuchtet eine LED, wobei die ersten 4 bit entscheiden */ // static uint8_t IIC_Initialized = 0; // if(!IIC_Initialized) // { // IIC_Initialized = 1; // i2c_enable(RTE_Output_SetOutput_OSPort_Out_PORT); // } while(i2c_busy(RTE_Output_SetOutput_OSPort_Out_PORT) != 0); ecrobot_send_i2c(RTE_Output_SetOutput_OSPort_Out_PORT, 0x20, data_IIC, &data_IIC, 0); #endif return 0; }
/* Start a transaction. */ int i2c_start(int port, U32 address, int internal_address, int n_internal_address_bytes, U8 *data, U32 nbytes, int write) { i2c_port *p; struct i2c_partial_transaction *pt; if(port < 0 || port >= I2C_N_PORTS || !i2c_ports[port]) return -1; if(i2c_busy(port)) return -2; if (nbytes > I2C_BUF_SIZE) return -4; if (n_internal_address_bytes > I2C_ADDRESS_SIZE) return -5; p = i2c_ports[port]; pt = p->partial_transaction; p->current_pt = pt; if(n_internal_address_bytes > 0){ int addrlen = n_internal_address_bytes; // Set up command to write the internal address to the device p->addr_int[0] = (address << 1); // This is a write pt->nbits = (n_internal_address_bytes + 1)*8; // copy internal address bytes, high bytes first while (addrlen > 0) { p->addr_int[addrlen--] = internal_address; internal_address >>= 8; } pt->data = p->addr_int; pt->state = I2C_NEWSTART; // We add an extra stop for the odd Lego i2c sensor, but only on a read if (!write && p->lego_mode){ pt++; pt->state = I2C_LEGOSTOP1; } pt++; }
void I2C_ByteRead(I2C_ADDR_t slaveAddress, uint8_t reg, uint8_t* data) { uint8_t num_bytes; uint8_t size = 0; if (reg > 0) { size = sizeof(reg); } lockset(lock); while (i2c_busy(i2c_bus, slaveAddress)) { ; } num_bytes = i2c_in(i2c_bus, slaveAddress, reg, size, data, 1); lockclr(lock); print("\t\tRead addr %02x, reg %02x, data %02x, total %d\n", slaveAddress, reg, *data, num_bytes); }