/* * Transmit byte array to slave (addr) * * @param addr Address of the slave * @param size Number of bytes in data to transmit * @param data Array of bytes * @param delay_us Microseconds to delay between each send */ void i2c_tx(uint8_t addr, int size, uint8_t *data, int delay_us) { i2c_acquire_bus(); // Waits for bus to become idle i2c_tx_addr(addr, I2C_WRITE, delay_us); // Transmit start bit, slave address, and write bit // Send each byte in data for(int i = 0; i < size; i++) i2c_tx_byte(data[i], delay_us); // Terminate communication with slave i2c_rxtx_end(); }
int hashlet_setup(const char *bus, unsigned int addr) { int fd = i2c_setup(bus); i2c_acquire_bus(fd, addr); wakeup(fd); return fd; }
/* * Reads count bytes of data from the slave (addr) * @param addr Address of the slave to read from * @param size Number of bytes to read from the slave * @param data Array of bytes to transmit * @param delay_us Busy waits for delay_us microseconds following each transferred byte */ void i2c_rx(uint8_t addr, int size, uint8_t *data, int delay_us) { i2c_acquire_bus(); // Wait for bus to become idle i2c_tx_addr(addr, I2C_READ, delay_us); // Send start bit, slave address, and read bit MCF_I2C0_I2CR &= ~(MCF_I2C_I2CR_MTX); // Become a receiver MCF_I2C0_I2CR &= ~(MCF_I2C_I2CR_TXAK); // Configure to ACK each recv'd data byte // Read a dummy byte in order to complete the mode switch uint8_t dummy = i2c_rx_byte(delay_us); // Master-receivers must generate clock pulses on SCL to recv 8 data bytes from slave // Read and ACK up until the last byte (size-2) for(int i = 0; i <= (size-2); i++) data[i] = i2c_rx_byte(delay_us); // NACK to stop transmission and read the last byte MCF_I2C0_I2CR |= MCF_I2C_I2CR_TXAK; data[size-1] = i2c_rx_byte(delay_us); // Terminate communication i2c_rxtx_end(); }