How to: I²C Bus¶
This is the main bus HARDWARIO TOWER uses to communicate with the most of the sensors and modules. All sensors and modules have their own addres in the HARDWARIO TOWER I²C address space.
Normally you don’t need to use I²C API, because all the sensors have their own libraries that gives you the measured data. You will need I²C APIs in case you implement your own I²C sensor or chip.
I²C buses on the Core Module¶
There are two busses on the Core Module. They are called:
SCL0(17, 18) pins in the bottom right corner of the Core Module
TWR_I2C_I2C1- Using SDA1 and SCL1 (27, 28) pins in the top right corner of the Core Module
There’s even another virtual I2C bus
TWR_I2C_I2C_1W which is encapsulated in 1-Wire protocol and can be used with
Sensor Module and 1-Wire Module to extend the communication distance.
By default the I²C buses are not initialized to save the power.
However when you initialize at least one sensor in the SDK, the bus is initialized with the first sensor in their respective
If you use only your sensor in the project, you have to initialize the I²C bus.
Reading and writing 8 or 16 bits¶
You can use functions to write or read 1 byte, 2 bytes or specific number of bytes which is explained in next chapter below.
1bool twr_i2c_memory_write_8b (twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t data) 2bool twr_i2c_memory_write_16b (twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint16_t data) 3 4bool twr_i2c_memory_read_8b (twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint8_t *data) 5bool twr_i2c_memory_read_16b (twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint16_t *data)
For example to write the data
1twr_i2c_memory_write_8b(TWR_I2C_I2C0, 0x48, 0x01, 0x81); 2twr_i2c_memory_write_16b(TWR_I2C_I2C0, 0x48, 0x01, 0x0180);
To read the data
1uint8_t reg_configuration; 2twr_i2c_memory_read_8b(TWR_I2C_I2C0, 0x48, 0x01, ®_configuration);
Reading and writing more data¶
1bool twr_i2c_write (twr_i2c_channel_t channel, const twr_i2c_transfer_t *transfer) 2bool twr_i2c_read (twr_i2c_channel_t channel, const twr_i2c_transfer_t *transfer)
Example to read more data:
1twr_i2c_memory_transfer_t transfer; 2uint8_t rx_buffer; 3 4transfer.device_address = 0x48; 5transfer.memory_address = 0x28; 6transfer.buffer = rx_buffer; 7transfer.length = sizeof(rx_buffer); 8 9twr_i2c_memory_read(TWR_I2C_I2C0, &transfer);