uint8 TwoWire::process() { itc_msg.xferred = 0; uint8 sla_addr = (itc_msg.addr << 1); if (itc_msg.flags == I2C_MSG_READ) { sla_addr |= I2C_READ; } i2c_start(); // shift out the address we're transmitting to i2c_shift_out(sla_addr); if (!i2c_get_ack()) { return ENACKADDR; } // Recieving if (itc_msg.flags == I2C_MSG_READ) { while (itc_msg.xferred < itc_msg.length) { itc_msg.data[itc_msg.xferred++] = i2c_shift_in(); if (itc_msg.xferred < itc_msg.length) { i2c_send_ack(); } else { i2c_send_nack(); } } } // Sending else { for (uint8 i = 0; i < itc_msg.length; i++) { i2c_shift_out(itc_msg.data[i]); if (!i2c_get_ack()) { return ENACKTRNS; } itc_msg.xferred++; } } i2c_stop(); return SUCCESS; }
uint8 hal_dev_mma8451_read_reg(uint8 addr) { uint8 result; int i; i2c_start(I2C0_B); i2c_write_byte(I2C0_B, MMA8451_I2C_ADDRESS | I2C_WRITE); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_write_byte(I2C0_B, addr); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_repeated_start(I2C0_B); for(i=0;i<1000;i++) for( i=0;i<1000;i++); i2c_write_byte(I2C0_B, MMA8451_I2C_ADDRESS | I2C_READ); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_set_rx_mode(I2C0_B); i2c_give_nack(I2C0_B); result = i2c_read_byte(I2C0_B); i2c_wait(I2C0_B); i2c_stop(I2C0_B); result = i2c_read_byte(I2C0_B); pause(); for(i=0;i<1000;i++) for( i=0;i<1000;i++); return result; }
static int i2c_sendbyte(unsigned char data) { int i; for (i=7; i>=0; i--) { if (data&(1<<i)) { i2c_high(); } else { i2c_low(); } } return i2c_get_ack(); }
uint8_t TwoWire::endTransmission(void) { if (tx_buf_overflow) return EDATA; // Serial.begin(9600); i2c_start(port); i2c_shift_out(port, (tx_addr << 1) | I2C_WRITE); if (!i2c_get_ack(port)) return ENACKADDR; // shift out the address we're transmitting to for (uint8_t i = 0; i < tx_buf_idx; i++) { uint8_t ret = writeOneByte(tx_buf[i]); if (ret) return ret; // SUCCESS is 0 } i2c_stop(port); tx_buf_idx = 0; tx_buf_overflow = false; return SUCCESS; }
uint8 TwoWire::writeOneByte(uint8 byte) { i2c_shift_out(port, byte); if (!i2c_get_ack(port)) return ENACKTRNS; return SUCCESS; }
bit i2c_put_byte( uns8 data ) { SSP1BUF = data; // send data byte SSP1IF = 0; return i2c_get_ack(); // return ACK value }
irom int platform_i2c_send_byte(uint8_t data) { i2c_write_byte(data); // Low-level returns nack (0=acked); we return ack (1=acked). return !i2c_get_ack(); }