Raspberry Pi 5 IoT In C - I2C with Gpio5 |
Written by Harry Fairhead | |||||||
Wednesday, 16 July 2025 | |||||||
Page 3 of 3
With all of this defined we can now create some end user-callable functions that are the same as the Pico SDK: A blocking 8-bit read int i2c_read_blocking(I2C i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop) { return i2c_read_blocking_internal(i2c, addr, dst, len, nostop, 0xFFFFFFFF); } A blocking 8-bit write int i2c_write_blocking(I2C i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop) { return i2c_write_blocking_internal(i2c, addr, src, An 8-bit write with per character timeout int i2c_write_timeout_per_char_us(I2C i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop, uint32_t timeout_per_char_us) { return i2c_write_blocking_internal(i2c, addr, src, An 8-bit read with per character timeout int i2c_read_timeout_per_char_us(I2C i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop, uint32_t timeout_per_char_us) { return i2c_read_blocking_internal(i2c, addr, dst, These use the internal read/write functions in obvious ways. The Gpio5 approach has the advantage that you can now add features that are supported by the hardware, such as 10-bit addresses and working as a slave, simply by writing to the appropriate registers. HTU1D Using Gpio5With the I2C functions defined in Gpio5 we can now take a Pico program that reads the HTU1D and run it with only minor modifications: #include <stdio.h> #include <stdlib.h> #include <time.h> #include "Gpio5.h" uint8_t crcCheck(uint8_t msb, uint8_t lsb, uint8_t check) { uint32_t data32 = ((uint32_t)msb << 16) | ((uint32_t)lsb << 8) | (uint32_t)check; uint32_t divisor = 0x988000; for (int i = 0; i < 16; i++) { if (data32 & (uint32_t)1 << (23 - i)) data32 ^= divisor; divisor >>= 1; }; return (uint8_t)data32; } int main(int argc, char **argv) { rp1_Init(); gpio_set_function(2, GPIO_FUNC_I2C); gpio_set_function(3, GPIO_FUNC_I2C); i2c_init(I2C1, 100 * 1000); uint8_t buf[4] = {0xE3}; i2c_write_blocking(I2C1, 0x40, buf, 1, true); i2c_read_blocking(I2C1, 0x40, buf, 3, false); uint8_t msb = buf[0]; uint8_t lsb = buf[1]; uint8_t check = buf[2]; printf("msb %d \n\r lsb %d \n\r checksum %d \n\r", msb, lsb, check); unsigned int data16 = ((unsigned int)msb << 8) | (unsigned int)(lsb & 0xFC); printf("crc = %d\n\r", crcCheck(msb, lsb, check)); float temp = (float)(-46.85 + (175.72 * data16 / (float)65536)); printf("Temperature %f C \n\r", temp); buf[0] = 0xF5; i2c_write_blocking(I2C1, 0x40, buf, 1, true); while (i2c_read_blocking(I2C1, 0x40, buf, 3, false) Reading the temperature is performed using clock stretching and works perfectly. Reading the humidity is performed using polling, just as an illustration of how this works. After performing the write, the master simply loops until it reads data without getting a NAK from the slave. Notice that we could simply test for a negative return value or the more specific NAK error indicated by bit one. Summary
You can find the complete Gpio5 at its Github repo: https://github.com/IOPress/Gpio5, at the book’s web page: https://iopress.info/index.php/books/raspberry-pi-5-iot-in-c Raspberry Pi 5 IoT In C
|
Google Androidify Lets You Turn Yourself Into The Android Bot 12/09/2025 Google has introduced Androidify, an app that can be used to build your own Android bot on the web or in an app on Google Play. AI does most of the work - all you need to do is upload a selfie, o [ ... ] |
Astra API Security Platform 15/09/2025 Astra has launched a new API security solution that offers real-time visibility and automated pentesting to secure "zombie" and undocumented APIs before they can be exploited. |
More News
|
Comments
or email your comment to: comments@i-programmer.info