diff --git a/gpio/shtc3/README.md b/gpio/shtc3/README.md new file mode 100644 index 0000000..0ad99d5 --- /dev/null +++ b/gpio/shtc3/README.md @@ -0,0 +1,13 @@ +# SHTC3 + +This is just a simple console application that uses i2c to communicate with an SHTC3 temperature and humidity device. It is assumed that you have debug mode turned on (Settings, System, Debug ON). To see the logs, either connect to serial port or use https://lab.flipper.net/cli and type ``log`` then enter. Once you are viewing the logs, run the application. + +The code does the following: +- The address of the device is hard-coded to 0x70. +- Wake-up (0x3517) is sent to wake up the device. +- Read id (0xefc8) is sent to read the device ID. +- The output is expected to match "(xxxx1xxxxx000111)". + +Resources: +- [I2CDevices.org info](https://i2cdevices.org/devices/shtc3) +- [SHTC3 Datasheet](https://www.waveshare.com/w/upload/3/33/SHTC3_Datasheet.pdf) \ No newline at end of file diff --git a/gpio/shtc3/application.fam b/gpio/shtc3/application.fam new file mode 100644 index 0000000..53de3a1 --- /dev/null +++ b/gpio/shtc3/application.fam @@ -0,0 +1,10 @@ +App( + appid="i2c_read_shtc3", + name="I2C SHTC3 Demo", + apptype=FlipperAppType.EXTERNAL, + entry_point="i2c_read_shtc3_app", + requires=["gui"], + stack_size=2 * 1024, + fap_icon="i2c_read_shtc3.png", + fap_category="GPIO", +) diff --git a/gpio/shtc3/i2c_read_shtc3.png b/gpio/shtc3/i2c_read_shtc3.png new file mode 100644 index 0000000..f893f75 Binary files /dev/null and b/gpio/shtc3/i2c_read_shtc3.png differ diff --git a/gpio/shtc3/i2c_read_shtc3_app.c b/gpio/shtc3/i2c_read_shtc3_app.c new file mode 100644 index 0000000..f9c5c60 --- /dev/null +++ b/gpio/shtc3/i2c_read_shtc3_app.c @@ -0,0 +1,47 @@ +#include +#include + +uint8_t address = 0x70 << 1; +uint32_t timeout = 1000; +FuriHalI2cBusHandle* i2c = &furi_hal_i2c_handle_external; + +void wake_up() { + uint8_t data_wake_up[2] = {0x35, 0x17}; + if(furi_hal_i2c_tx(i2c, address, data_wake_up, 2, timeout)) { + FURI_LOG_I("I2C", "Wake up success"); + } else { + FURI_LOG_I("I2C", "Wake up failed"); + } +} + +void read_id() { + uint8_t data_read_id[2] = {0xef, 0xc8}; + uint8_t data_response[3] = {0}; + if(furi_hal_i2c_trx(i2c, address, data_read_id, 2, data_response, 3, timeout)) { + uint16_t value = (data_response[0] << 8) | data_response[1]; + + // put binary value into buffer and null terminate + char buffer[16]; + for(uint8_t i = 0; i < 16; i++) { + buffer[15 - i] = (value & (1 << i)) ? '1' : '0'; + } + buffer[16] = 0; + + FURI_LOG_I("I2C", "Read ID: (%s) %04x", buffer, value); + FURI_LOG_I("I2C", "Expected ID: (xxxx1xxxxx000111)"); + FURI_LOG_I("I2C", "CRC 8: %02x", data_response[2]); + } else { + FURI_LOG_I("I2C", "Read ID failed"); + } +} + +int32_t i2c_read_shtc3_app(void* p) { + UNUSED(p); + + furi_hal_i2c_acquire(&furi_hal_i2c_handle_external); + wake_up(); + read_id(); + furi_hal_i2c_release(&furi_hal_i2c_handle_external); + + return 0; +} \ No newline at end of file