Handles SHT3C sensor
...And this is the first time I'm flashing the firmware into a real b-parasite board!
This commit is contained in:
parent
25772def7e
commit
81b33fbd8c
5 changed files with 44 additions and 38 deletions
|
|
@ -4,7 +4,7 @@
|
|||
#include "nrf_gpio.h"
|
||||
|
||||
// Built-in LED.
|
||||
#define PRST_LED_PIN NRF_GPIO_PIN_MAP(1, 11)
|
||||
#define PRST_LED_PIN NRF_GPIO_PIN_MAP(0, 28)
|
||||
|
||||
// Deep sleep.
|
||||
#define PRST_DEEP_SLEEP_IN_SECONDS 2
|
||||
|
|
|
|||
|
|
@ -72,11 +72,13 @@ static void rtc_callback() {
|
|||
prst_adv_stop();
|
||||
nrf_gpio_pin_clear(PRST_LED_PIN);
|
||||
UNUSED_VARIABLE(batt_read);
|
||||
UNUSED_VARIABLE(temp_humi);
|
||||
// UNUSED_VARIABLE(temp_humi);
|
||||
NRF_LOG_INFO("Read batt: " NRF_LOG_FLOAT_MARKER " V (%d), %u mV",
|
||||
NRF_LOG_FLOAT(batt_read.voltage), batt_read.raw, batt_read.millivolts);
|
||||
NRF_LOG_INFO("Read temp: " NRF_LOG_FLOAT_MARKER " oC",
|
||||
NRF_LOG_FLOAT(temp_humi.temp_c));
|
||||
NRF_LOG_INFO("Read humi: " NRF_LOG_FLOAT_MARKER " %%",
|
||||
NRF_LOG_FLOAT(temp_humi.humidity));
|
||||
NRF_LOG_FLUSH();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,9 @@
|
|||
|
||||
#include "prst_config.h"
|
||||
|
||||
// TODO(rbaron): More info in the adv packet:
|
||||
// - Software version
|
||||
|
||||
// We need to pick a service UUID for broadcasting our sensor data.
|
||||
// 0x181a is defined as "environmental sensing", which seems appopriate.
|
||||
#define SERVICE_UUID 0x181a
|
||||
|
|
|
|||
|
|
@ -3,62 +3,52 @@
|
|||
#include <app_error.h>
|
||||
#include <nrf_delay.h>
|
||||
#include <nrf_drv_twi.h>
|
||||
#include <nrf_gpio.h>
|
||||
#include <nrf_log.h>
|
||||
#include <nrf_log_ctrl.h>
|
||||
|
||||
// #define PRST_SHT3C_DEFAULT_ADDR 0x70
|
||||
// I'm using a sht30 to test while I wait for the new PCBs.
|
||||
#define PRST_SHT3C_DEFAULT_ADDR 0x44
|
||||
|
||||
static const nrf_drv_twi_t twi_ = NRF_DRV_TWI_INSTANCE(0);
|
||||
static nrf_drv_twi_config_t twi_config_ = NRF_DRV_TWI_DEFAULT_CONFIG;
|
||||
|
||||
static uint8_t buff[6];
|
||||
|
||||
static void write_cmd(uint16_t command) {
|
||||
uint8_t cmd[2];
|
||||
cmd[0] = command >> 8;
|
||||
cmd[1] = command & 0xff;
|
||||
APP_ERROR_CHECK(nrf_drv_twi_tx(&twi_, PRST_SHTC3_ADDR, cmd, 2,
|
||||
/*no_stop=*/false));
|
||||
}
|
||||
|
||||
void prst_shtc3_init() {
|
||||
twi_config_.scl = NRF_GPIO_PIN_MAP(0, 3);
|
||||
twi_config_.sda = NRF_GPIO_PIN_MAP(0, 2);
|
||||
// twi_config.clear_bus_init = true;
|
||||
twi_config_.scl = PRST_SHT3C_SCL_PIN;
|
||||
twi_config_.sda = PRST_SHT3C_SDA_PIN;
|
||||
twi_config_.frequency = NRF_TWI_FREQ_100K;
|
||||
}
|
||||
|
||||
prst_shtc3_read_t prst_shtc3_read() {
|
||||
uint32_t err_code;
|
||||
// Sends request for data with clock stretching. Currently not working
|
||||
// very well - sometimes it works, sometimes the read blocks forever.
|
||||
// uint8_t tx_data[] = {0x2c, 0x06};
|
||||
// Request for data withour clock stretching. If no data is available yet,
|
||||
// the result will be a NACK.
|
||||
uint8_t tx_data[] = {0x24, 0x00};
|
||||
|
||||
err_code = nrf_drv_twi_init(&twi_, &twi_config_, NULL, NULL);
|
||||
|
||||
APP_ERROR_CHECK(err_code);
|
||||
nrf_delay_ms(10);
|
||||
APP_ERROR_CHECK(nrf_drv_twi_init(&twi_, &twi_config_, NULL, NULL));
|
||||
nrf_drv_twi_enable(&twi_);
|
||||
nrf_delay_ms(10);
|
||||
err_code = nrf_drv_twi_tx(&twi_, PRST_SHT3C_DEFAULT_ADDR, tx_data,
|
||||
sizeof(tx_data), false);
|
||||
APP_ERROR_CHECK(err_code);
|
||||
|
||||
// TODO(rbaron): timeout.
|
||||
while (true) {
|
||||
err_code = nrf_drv_twi_rx(&twi_, PRST_SHT3C_DEFAULT_ADDR, buff, 6);
|
||||
if (err_code == NRF_ERROR_DRV_TWI_ERR_ANACK) {
|
||||
nrf_delay_ms(10);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
// Wake the sensor up.
|
||||
write_cmd(PRST_SHTC3_CMD_WAKEUP);
|
||||
nrf_delay_ms(1);
|
||||
// Request measurement.
|
||||
write_cmd(PRST_SHTC3_CMD_MEASURE_TFIRST_LOW_POWER);
|
||||
// Read temp and humidity.
|
||||
while (nrf_drv_twi_rx(&twi_, PRST_SHTC3_ADDR, buff, 6) != 0) {
|
||||
nrf_delay_ms(10);
|
||||
}
|
||||
// Put the sensor in sleep mode.
|
||||
write_cmd(PRST_SHTC3_CMD_SLEEP);
|
||||
|
||||
// TODO(rbaron): put the sensor to sleep & save power.
|
||||
|
||||
// Uninit i2c.
|
||||
nrf_drv_twi_uninit(&twi_);
|
||||
|
||||
NRF_LOG_INFO("Computing...");
|
||||
double temp_c =
|
||||
-45 + 175 * ((double)((buff[0] << 8) | buff[1])) / ((1 << 16) - 1);
|
||||
// double humi = 100 * ( (double) ((buff[0] << 8) | buff[1])) / ((1<<16) - 1);
|
||||
prst_shtc3_read_t ret = {.temp_c = temp_c, .humidity = 0};
|
||||
double humi = 100 * ((double)((buff[3] << 8) | buff[4])) / ((1 << 16) - 1);
|
||||
|
||||
prst_shtc3_read_t ret = {.temp_c = temp_c, .humidity = humi};
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -1,6 +1,17 @@
|
|||
#ifndef _PRST_SHT3C_H_
|
||||
#define _PRST_SHT3C_H_
|
||||
|
||||
#include <nrf_gpio.h>
|
||||
|
||||
#define PRST_SHT3C_SDA_PIN NRF_GPIO_PIN_MAP(0, 24)
|
||||
#define PRST_SHT3C_SCL_PIN NRF_GPIO_PIN_MAP(0, 13)
|
||||
|
||||
// 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
|
||||
|
||||
typedef struct prst_shtc3_values {
|
||||
double temp_c;
|
||||
double humidity;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue