static int i2c_bit_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) { struct nouveau_i2c_port *port = adap->algo_data; struct i2c_msg *msg = msgs; int ret = 0, mcnt = num; if (port->func->acquire) port->func->acquire(port); while (!ret && mcnt--) { u8 remaining = msg->len; u8 *ptr = msg->buf; ret = i2c_start(port); if (ret == 0) ret = i2c_addr(port, msg); if (msg->flags & I2C_M_RD) { while (!ret && remaining--) ret = i2c_get_byte(port, ptr++, !remaining); } else { while (!ret && remaining--) ret = i2c_put_byte(port, *ptr++); } msg++; } i2c_stop(port); return (ret < 0) ? ret : num; }
// not debugged uns8 i2c_rx_multi( uns8 addr, uns8 *data_ptr, uns8 length ) { uns8 dt; i2c_start(); // send START if( i2c_put_byte( addr ) ) // slave did not acknowledge { i2c_stop(); return( I2C_NO_ACK_ADDR ); } while( length ) { length--; dt = i2c_get_byte(); // get a data byte *data_ptr++ = dt; if( length ) i2c_send_ack(); // this is not the last byte } i2c_send_noack(); // this was the last byte i2c_stop(); // send STOP return( I2C_NORMAL ); }
lc_err_t read_byte(e2p_addr_t address, unsigned char *byte) { register lc_err_t err_code; do { do { i2c_start(); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_START && err_code != I2C_MASTER_REPSTART) break; i2c_slaW(0xa0); err_code = i2c_read_state_wait(); } while (err_code == I2C_MASTER_SLAW_NACK); if (err_code != I2C_MASTER_SLAW_ACK) break; i2c_write(address >> 8); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_TXDATA_ACK) break; i2c_write(address); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_TXDATA_ACK) break; i2c_start(); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_REPSTART) break; i2c_slaR(0xa0); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_SLAR_ACK) break; i2c_read_nack(); err_code = i2c_read_state_wait(); if (err_code != I2C_MASTER_RXDATA_NACK) break; *byte = i2c_get_byte(); err_code = 0; } while (0); i2c_stop(); return err_code; }
uns8 i2c_rx( uns8 addr, uns8 *data ) { uns8 dt; i2c_start(); // send START if( i2c_put_byte( addr ) ) // slave did not acknowledge { i2c_stop(); return( I2C_NO_ACK_ADDR ); } dt = i2c_get_byte(); // get a data byte *data = dt; i2c_send_noack(); // this is the last byte i2c_stop(); // send STOP return( I2C_NORMAL ); }
uint8_t ds3231_read(uint8_t addr, uint8_t* data) { uint8_t ecode; do { i2c_start(); ecode = i2c_read_state_wait(); if ((ecode != I2C_MASTER_START) && (ecode != I2C_MASTER_REPSTART)) break; i2c_slaW(_DS3231_ADDR); ecode = i2c_read_state_wait(); if (ecode != I2C_MASTER_SLAW_ACK) break; i2c_write(addr); ecode = i2c_read_state_wait(); if (ecode != I2C_MASTER_TXDATA_ACK) break; i2c_start(); ecode = i2c_read_state_wait(); if (ecode != I2C_MASTER_REPSTART) break; i2c_slaR(_DS3231_ADDR); ecode = i2c_read_state_wait(); if (ecode != I2C_MASTER_SLAR_ACK) break; i2c_read_nack(); ecode = i2c_read_state_wait(); if (ecode != I2C_MASTER_RXDATA_NACK) break; *data = i2c_get_byte(); ecode = 0; } while(0); i2c_stop(); return ecode; }
void main(void) { /* Initialization functions called below */ ecan_init(); ConfigureOscillator(); /* Enable Interrupts */ RCONbits.IPEN = 0; // Enable interrupt priority INTCONbits.GIE = 1; // Enable interrupts INTCONbits.PEIE = 1; // Enable peripheral interrupts. PIE3bits.RXB0IE = 1; // Enable CAN receive buffer 0 interrupt PIE3bits.RXB1IE = 1; // Enable CAN receive buffer 1 interrupt i2c_init(); //Configure accelerometer CONTRL_REG1_XM //char slaveAddr0 = 0x1E; //char destAddr0 = 0x20; //char sendData0 = 0b01100111; //== 0x67 //i2c_writeToReg(slaveAddr0, destAddr0, sendData0); //CONTROL_REG5_XM //slaveAddr0 = 0x1E; //destAddr0 = 0x24; //sendData0 = 0b11110000; //i2c_writeToReg(slaveAddr0, destAddr0, sendData0); i2c_writeToReg(0x1E, 0x1F, 0x00); i2c_writeToReg(0x1E, 0x20, 0x57); i2c_writeToReg(0x1E, 0x21, 0x00); i2c_writeToReg(0x1E, 0x22, 0x00); i2c_writeToReg(0x1E, 0x23, 0x00); i2c_writeToReg(0x1E, 0x24, 0x14); i2c_writeToReg(0x1E, 0x25, 0x00); i2c_writeToReg(0x1E, 0x26, 0x00); i2c_writeToReg(0x6A, 0x20, 0x0F); i2c_writeToReg(0x6A, 0x21, 0x00); i2c_writeToReg(0x6A, 0x23, 0x00); i2c_writeToReg(0x6A, 0x24, 0x00); /* While doing recurring functions */ while(1) { /* short ac1 = i2c_get_data(0x77, 0xAA); short ac2 = i2c_get_data(0x77, 0xAC); short ac3 = i2c_get_data(0x77, 0xAE); unsigned short ac4 = i2c_get_udata(0x77, 0xB0); unsigned short ac5 = i2c_get_udata(0x77, 0xB2); unsigned short ac6 = i2c_get_udata(0x77, 0xB4); short b1 = i2c_get_data(0x77, 0xB6); short b2 = i2c_get_data(0x77, 0xB8); short mb = i2c_get_data(0x77, 0xBA); short mc = i2c_get_data(0x77, 0xBC); short md = i2c_get_data(0x77, 0xBF); i2c_write_raw(0xF4, 0x2E); __delay_ms(5); long ut = i2c_get_data(0x77, 0xF6); long x1 = ((long) ut - (unsigned long) ac6) * (unsigned long) ac5 / 32768; long x2 = (long) mc * 2048 / (x1 + (long) md); long b5 = x1 + x2; long temp = (b5 + 8) / 16; Message tempPack; tempPack.sid = 0x101; tempPack.len = 4; tempPack.data[3] = temp >> 24; tempPack.data[2] = temp >> 16; tempPack.data[1] = temp >> 8; tempPack.data[0] = temp; ecan_send(&tempPack); __delay_ms(50); */ /*short xGyroRaw = i2c_get_byte(0x6A, 0x28); xGyroRaw += i2c_get_byte(0x6A,0x29) << 8; short yGyroRaw = i2c_get_byte(0x6A, 0x2A); yGyroRaw += i2c_get_byte(0x6A,0x2B) << 8; short zGyroRaw = i2c_get_byte(0x6A, 0x2C); zGyroRaw += i2c_get_byte(0x6A,0x2D) << 8; Message gyroPack; gyroPack.sid = 0x000; gyroPack.len = 6; gyroPack.data[5] = xGyroRaw >> 8; gyroPack.data[4] = xGyroRaw; gyroPack.data[3] = yGyroRaw >> 8; gyroPack.data[2] = yGyroRaw; gyroPack.data[1] = zGyroRaw >> 8; gyroPack.data[0] = zGyroRaw; ecan_send(&gyroPack); */ short xAccelRaw = i2c_get_byte(0x1E, 0x28); xAccelRaw += i2c_get_byte(0x1E,0x29) << 8; short yAccelRaw = i2c_get_byte(0x1E, 0x2A); yAccelRaw += i2c_get_byte(0x1E,0x2B) << 8; short zAccelRaw = i2c_get_byte(0x1E, 0x2C); zAccelRaw += i2c_get_byte(0x1E,0x2D) << 8; Message accelPack; accelPack.sid = 0x000; accelPack.len = 6; accelPack.data[5] = xAccelRaw >> 8; accelPack.data[4] = xAccelRaw; accelPack.data[3] = yAccelRaw >> 8; accelPack.data[2] = yAccelRaw; accelPack.data[1] = zAccelRaw >> 8; accelPack.data[0] = zAccelRaw; ecan_send(&accelPack); } }