Initial commit
SHTC3 comm via i2c works
This commit is contained in:
parent
baec1c2a80
commit
bae06952fb
7 changed files with 146 additions and 0 deletions
1
code/nrf-connect/.gitignore
vendored
Normal file
1
code/nrf-connect/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
build
|
||||
6
code/nrf-connect/CMakeLists.txt
Normal file
6
code/nrf-connect/CMakeLists.txt
Normal file
|
|
@ -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)
|
||||
51
code/nrf-connect/nrf52840dk_nrf52840.overlay
Normal file
51
code/nrf-connect/nrf52840dk_nrf52840.overlay
Normal file
|
|
@ -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 = <NRF_PSEL(PWM_OUT0, 0, 5)>;
|
||||
nordic,invert;
|
||||
};
|
||||
};
|
||||
|
||||
pwm0_sleep: pwm0_sleep {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(PWM_OUT0, 0, 5)>;
|
||||
low-power-enable;
|
||||
};
|
||||
};
|
||||
|
||||
i2c0_default: i2c0_default {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(TWIM_SDA, 0, 24)>,
|
||||
<NRF_PSEL(TWIM_SCL, 0, 13)>;
|
||||
};
|
||||
};
|
||||
|
||||
i2c0_sleep: i2c0_sleep {
|
||||
group1 {
|
||||
psels = <NRF_PSEL(TWIM_SDA, 0, 24)>,
|
||||
<NRF_PSEL(TWIM_SCL, 0, 13)>;
|
||||
low-power-enable;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
&i2c0 {
|
||||
shtc3: shtc3@70 {
|
||||
compatible = "i2c-device";
|
||||
reg = <0x70>;
|
||||
label = "SHTC3";
|
||||
};
|
||||
};
|
||||
3
code/nrf-connect/prj.conf
Normal file
3
code/nrf-connect/prj.conf
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
CONFIG_LOG=y
|
||||
CONFIG_CBPRINTF_FP_SUPPORT=y
|
||||
CONFIG_I2C=y
|
||||
11
code/nrf-connect/src/main.c
Normal file
11
code/nrf-connect/src/main.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include <drivers/i2c.h>
|
||||
#include <logging/log.h>
|
||||
#include <zephyr/zephyr.h>
|
||||
|
||||
#include "prst/shtc3.h"
|
||||
|
||||
LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);
|
||||
|
||||
void main(void) {
|
||||
prst_shtc3_read_t shtc3_read = prst_shtc3_read();
|
||||
}
|
||||
54
code/nrf-connect/src/prst/shtc3.c
Normal file
54
code/nrf-connect/src/prst/shtc3.c
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
#include "shtc3.h"
|
||||
|
||||
#include <drivers/i2c.h>
|
||||
#include <logging/log.h>
|
||||
#include <zephyr/zephyr.h>
|
||||
|
||||
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;
|
||||
}
|
||||
20
code/nrf-connect/src/prst/shtc3.h
Normal file
20
code/nrf-connect/src/prst/shtc3.h
Normal file
|
|
@ -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_
|
||||
Loading…
Add table
Reference in a new issue