Example #1
0
void i2c_lpc_transfer(i2c_bus_t* const bus,
	const uint_fast8_t slave_address,
	const uint8_t* const data_tx, const size_t count_tx,
	uint8_t* const data_rx, const size_t count_rx
) {
	const uint32_t port = (uint32_t)bus->obj;
	size_t i;
	bool ack = false;
	if (data_tx && (count_tx > 0)) {
		i2c_tx_start(port);
		i2c_tx_byte(port, (slave_address << 1) | I2C_WRITE);
		for(i=0; i<count_tx; i++) {
			i2c_tx_byte(port, data_tx[i]);
		}
	}

	if (data_rx && (count_rx > 0)) {
		i2c_tx_start(port);
		i2c_tx_byte(port, (slave_address << 1) | I2C_READ);
		for(i=0; i<count_rx; i++) {
			/* ACK each byte except the last */
			ack = (i!=count_rx-1);
			data_rx[i] = i2c_rx_byte(port, ack);
		}
	}

	i2c_stop(port);
}
Example #2
0
/*
 * Starts a transmission with the slave by putting the board into master-transmitter mode,
 * sending the slave address and read/write bit.
 * The I2C bus should be idle (i2c_acquire_bus()) before calling this function
 * 
 * @param addr Address of slave
 * @param rw 0x01 for read, 0x00 for write
 * @param delay_us Microseconds to delay between each send
 */
void i2c_tx_addr(uint8_t addr, uint8_t rw, int delay_us) {
	MCF_I2C0_I2CR |= MCF_I2C_I2CR_MTX; // Make board a transmitter
	MCF_I2C0_I2CR |= MCF_I2C_I2CR_MSTA; // Make board a master (which sends the start bit)
	
	// Compound first 7 address bits followed by the rw bit, then send
	uint8_t hello = 0;
	hello |= addr << 1;
	hello |= rw << 0;
	i2c_tx_byte(hello, delay_us);
}
Example #3
0
/*
 * 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();
}
Example #4
0
File: i2c_lpc.c Project: Ttl/vna
void i2c_lpc_transfer(const uint32_t port,
	const uint_fast8_t slave_address,
	const uint8_t* const data_tx, const size_t count_tx,
	uint8_t* const data_rx, const size_t count_rx
) {
	i2c_tx_start(port);
	i2c_tx_byte(port, (slave_address << 1) | I2C_WRITE);
	for(size_t i=0; i<count_tx; i++) {
		i2c_tx_byte(port, data_tx[i]);
	}

	if( data_rx ) {
		i2c_tx_start(port);
		i2c_tx_byte(port, (slave_address << 1) | I2C_READ);
		for(size_t i=0; i<count_rx; i++) {
			data_rx[i] = i2c_rx_byte(port);
		}
	}

	i2c_stop(port);
}