From 7a0540971eb27478e5d4cbab38cd8f11ca349e70 Mon Sep 17 00:00:00 2001 From: Derek Jamison Date: Sun, 11 Jun 2023 17:15:32 -0500 Subject: [PATCH] SHTC3 app to print id. --- gpio/shtc3/README.md | 13 +++++++++ gpio/shtc3/application.fam | 10 +++++++ gpio/shtc3/i2c_read_shtc3.png | Bin 0 -> 1816 bytes gpio/shtc3/i2c_read_shtc3_app.c | 47 ++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 gpio/shtc3/README.md create mode 100644 gpio/shtc3/application.fam create mode 100644 gpio/shtc3/i2c_read_shtc3.png create mode 100644 gpio/shtc3/i2c_read_shtc3_app.c 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 0000000000000000000000000000000000000000..f893f75e6fa6e869cc2c153d91a569c1ccead892 GIT binary patch literal 1816 zcma)6eM}Q)7(XO3fuPyOCK9q-4xBn{?|M*>3$=l^LN^X8-|Ry?dT;4T+q>OeY552- z!sb^L*k%SuHW@=VG%RDX%qa+Nj))4&SeDE!V%#4ti*XW|&V_}&*Oreu_R-{e@B2Kz z=lA}e=Y8%uV9CvjiinLs5G2Z+ZOnsjWgW396#jpHN8f}XA+?;rU@;pEs72&$oC6@p z#_^*q>dr>-)Air}tf|Nr-E{-rjh0X3degUD~eOjospP>3h&W`^>2) zJfuEJHAojFhB04FbrF|iLh5FcHh-6eht6C+qQ5mTIhDWJeEPYfuKi;eM@IkrxVyA( z!=;nP?W0%o&ipZV?xStHg`u&#_YKz?xIuYOW$4xyPCOjs+uwcc=J|b})UB`Q*1W#c zgcqK<|BC(PhBv1VK0Ud2!fii0QC@TBC2_Rp`RhY zSLKr8{UiYzBao~jC&(P{LKRx3oOjDQ3@Y$R172>C+$9AH084=@9$cd)6tO<2VCQ9C zvh%`%m>~RuJ19uzZ2!Qoc!Hp?X3H~s8R1bNP5(R8>0FR4$)+lpaG-uclIMw~f@%Ta zc|hXbq7|5`fJ=_YP^AErfp_p?KI{UZ!!!Y#h4TVOiIH;xmjufS9mG;k=_8(n!uZK( zo`qaV7`kYOvm!7mbeNQvm5G{7h7?U&N{Wh9!$XxyQD!Go0djz=Lbm%4T(lN*Y>-h& z#Nk$MywJtL)X4xv$)ZDtrIPqOb?M*j@GdACb}GxrjFFL{V3L4+rXf`%Q9x*^WRlWo z6Nxm6fT{byJj>at{|oBtZWR8~VGK=E*_8wVEWalMd@2qqt+1WlzFVj4j@BtPMTzj*fO6J zzN`RWFD)a$Z~uP+{Mh9L1Qm6ufOK5>a+g*m9bc}1e%z8T-j{^m3dafD!MOR^r0f>v z8*ss)Cc^2pVh +#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