From bae06952fbe1bf12c105509887a548ecd54b54a1 Mon Sep 17 00:00:00 2001 From: rbaron Date: Sat, 12 Nov 2022 12:45:03 +0100 Subject: [PATCH] Initial commit SHTC3 comm via i2c works --- code/nrf-connect/.gitignore | 1 + code/nrf-connect/CMakeLists.txt | 6 +++ code/nrf-connect/nrf52840dk_nrf52840.overlay | 51 ++++++++++++++++++ code/nrf-connect/prj.conf | 3 ++ code/nrf-connect/src/main.c | 11 ++++ code/nrf-connect/src/prst/shtc3.c | 54 ++++++++++++++++++++ code/nrf-connect/src/prst/shtc3.h | 20 ++++++++ 7 files changed, 146 insertions(+) create mode 100644 code/nrf-connect/.gitignore create mode 100644 code/nrf-connect/CMakeLists.txt create mode 100644 code/nrf-connect/nrf52840dk_nrf52840.overlay create mode 100644 code/nrf-connect/prj.conf create mode 100644 code/nrf-connect/src/main.c create mode 100644 code/nrf-connect/src/prst/shtc3.c create mode 100644 code/nrf-connect/src/prst/shtc3.h diff --git a/code/nrf-connect/.gitignore b/code/nrf-connect/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/code/nrf-connect/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/code/nrf-connect/CMakeLists.txt b/code/nrf-connect/CMakeLists.txt new file mode 100644 index 0000000..8282b27 --- /dev/null +++ b/code/nrf-connect/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(hello_world) + +target_sources(app PRIVATE src/main.c src/prst/shtc3.c) diff --git a/code/nrf-connect/nrf52840dk_nrf52840.overlay b/code/nrf-connect/nrf52840dk_nrf52840.overlay new file mode 100644 index 0000000..63796e9 --- /dev/null +++ b/code/nrf-connect/nrf52840dk_nrf52840.overlay @@ -0,0 +1,51 @@ +// To get started, press Ctrl+Space (or Option+Esc) to bring up the completion menu and view the available nodes. + +// You can also use the buttons in the sidebar to perform actions on nodes. +// Actions currently available include: + +// * Enabling / disabling the node +// * Adding the bus to a bus +// * Removing the node +// * Connecting ADC channels + +// For more help, browse the DeviceTree documentation at https: //docs.zephyrproject.org/latest/guides/dts/index.html +// You can also visit the nRF DeviceTree extension documentation at https: //nrfconnect.github.io/vscode-nrf-connect/devicetree/nrfdevicetree.html + +&pinctrl { + pwm0_default: pwm0_default { + group1 { + psels = ; + nordic,invert; + }; + }; + + pwm0_sleep: pwm0_sleep { + group1 { + psels = ; + low-power-enable; + }; + }; + + i2c0_default: i2c0_default { + group1 { + psels = , + ; + }; + }; + + i2c0_sleep: i2c0_sleep { + group1 { + psels = , + ; + low-power-enable; + }; + }; +}; + +&i2c0 { + shtc3: shtc3@70 { + compatible = "i2c-device"; + reg = <0x70>; + label = "SHTC3"; + }; +}; diff --git a/code/nrf-connect/prj.conf b/code/nrf-connect/prj.conf new file mode 100644 index 0000000..fcb7350 --- /dev/null +++ b/code/nrf-connect/prj.conf @@ -0,0 +1,3 @@ +CONFIG_LOG=y +CONFIG_CBPRINTF_FP_SUPPORT=y +CONFIG_I2C=y \ No newline at end of file diff --git a/code/nrf-connect/src/main.c b/code/nrf-connect/src/main.c new file mode 100644 index 0000000..0fab6a1 --- /dev/null +++ b/code/nrf-connect/src/main.c @@ -0,0 +1,11 @@ +#include +#include +#include + +#include "prst/shtc3.h" + +LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG); + +void main(void) { + prst_shtc3_read_t shtc3_read = prst_shtc3_read(); +} diff --git a/code/nrf-connect/src/prst/shtc3.c b/code/nrf-connect/src/prst/shtc3.c new file mode 100644 index 0000000..1ed3aca --- /dev/null +++ b/code/nrf-connect/src/prst/shtc3.c @@ -0,0 +1,54 @@ +#include "shtc3.h" + +#include +#include +#include + +LOG_MODULE_REGISTER(shtc3, LOG_LEVEL_DBG); + +static const struct i2c_dt_spec shtc3 = I2C_DT_SPEC_GET(DT_NODELABEL(shtc3)); + +static uint8_t buff[6]; + +static void write_cmd(uint16_t command) { + static uint8_t cmd[2]; + cmd[0] = command >> 8; + cmd[1] = command & 0xff; + if (i2c_write_dt(&shtc3, cmd, sizeof(cmd)) != 0) { + LOG_ERR("Error writing command"); + } +} + +prst_shtc3_read_t prst_shtc3_read() { + // Wake the sensor up. + write_cmd(PRST_SHTC3_CMD_WAKEUP); + k_msleep(1); + + // Request measurement. + write_cmd(PRST_SHTC3_CMD_MEASURE_TFIRST_NORMAL); + + // Reading in normal (not low power) mode can take up to 12.1 ms, according to + // the datasheet. + k_msleep(15); + + while (i2c_read_dt(&shtc3, buff, 6) != 0) { + k_msleep(10); + } + + // Put the sensor in sleep mode. + write_cmd(PRST_SHTC3_CMD_SLEEP); + + // TODO: Uninit i2c to save power? + + // TODO: verify the CRC of the measurements. The function is described in the + // datasheet. + + float temp_c = -45 + 175 * ((float)((buff[0] << 8) | buff[1])) / (1 << 16); + float humi = ((float)((buff[3] << 8) | buff[4])) / UINT16_MAX; + + prst_shtc3_read_t ret = {.temp_c = temp_c, .rel_humi = humi}; + + LOG_INF("Read temp: %f oC (%d)", ret.temp_c, (int)temp_c); + LOG_INF("Read humi: %.0f %%", 100.0 * ret.rel_humi); + return ret; +} \ No newline at end of file diff --git a/code/nrf-connect/src/prst/shtc3.h b/code/nrf-connect/src/prst/shtc3.h new file mode 100644 index 0000000..11b12e5 --- /dev/null +++ b/code/nrf-connect/src/prst/shtc3.h @@ -0,0 +1,20 @@ +#ifndef _PRST_SHT3C_H_ +#define _PRST_SHT3C_H_ + +// Values from the SHTC3 datasheet. +#define PRST_SHTC3_ADDR 0x70 +#define PRST_SHTC3_CMD_SLEEP 0xb098 +#define PRST_SHTC3_CMD_WAKEUP 0x3517 +#define PRST_SHTC3_CMD_MEASURE_TFIRST_LOW_POWER 0x609c +#define PRST_SHTC3_CMD_MEASURE_TFIRST_NORMAL 0x7866 + +typedef struct { + // Temperature in Celcius. + float temp_c; + // Relative humidity in [0, 1.0]. + float rel_humi; +} prst_shtc3_read_t; + +prst_shtc3_read_t prst_shtc3_read(); + +#endif // _PRST_SHT3C_H_ \ No newline at end of file