Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions kitsune/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,14 @@ void sample_sensor_data(periodic_data* data,NetStats_t * keyword_net_stats)
data->tvoc = tvoc;
data->has_co2 = true;
data->co2 = eco2;

data->has_tvoc_version = true;
data->tvoc_version.has_tvoc_app_fw_version = true;
data->tvoc_version.tvoc_app_fw_version = tvoc_get_fw_app_version();
data->tvoc_version.has_tvoc_boot_fw_version = true;
data->tvoc_version.tvoc_boot_fw_version = tvoc_get_fw_boot_version();
data->tvoc_version.has_tvoc_hw_version = true;
data->tvoc_version.tvoc_hw_version = tvoc_get_hw_version();
}
}
}
Expand Down Expand Up @@ -2057,6 +2065,9 @@ tCmdLineEntry g_sCmdTable[] = {
{ "thp", Cmd_read_temp_hum_press, "" },
{ "tv", Cmd_meas_TVOC, "" },
{"tvenv", Cmd_set_tvenv, ""},
{"tvver",cmd_tvoc_get_ver, ""},
{"tvgets",cmd_tvoc_status, ""},
{"tverr",cmd_tvoc_errid, ""},

{ "uv", Cmd_read_uv, "" },
{ "light", Cmd_readlight, "" },
Expand Down
166 changes: 152 additions & 14 deletions kitsune/i2c_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,18 @@ int Cmd_read_temp_hum_press(int argc, char *argv[]) {
return SUCCESS;
}

/********************************************************************************
* GAS SENSOR CCS811 - BEGIN
********************************************************************************/
bool tvoc_wa = false;
uint8_t tvoc_i2c_addr = 0x5A;
int init_tvoc(int measmode) {
unsigned char b[2];
assert(xSemaphoreTakeRecursive(i2c_smphr, 30000));

b[0] = 0;
(I2C_IF_Write(0x5a, b, 1, 1));
(I2C_IF_Read(0x5a, b, 1));
(I2C_IF_Write(tvoc_i2c_addr, b, 1, 1));
(I2C_IF_Read(tvoc_i2c_addr, b, 1));

if( !(b[0] & 0x10) ) {
LOGE("no valid fw for TVOC\n");
Expand All @@ -260,29 +264,32 @@ int init_tvoc(int measmode) {
}
//boot
b[0] = 0xf4;
(I2C_IF_Write(0x5a, b, 1, 1));
(I2C_IF_Write(tvoc_i2c_addr, b, 1, 1));
vTaskDelay(100);
b[0] = 0;
(I2C_IF_Write(0x5a, b, 1, 1));
(I2C_IF_Read(0x5a, b, 1));
(I2C_IF_Write(tvoc_i2c_addr, b, 1, 1));
(I2C_IF_Read(tvoc_i2c_addr, b, 1));
if( !(b[0] & 0x90) ) {
LOGE("fail to boot TVOC\n");
xSemaphoreGiveRecursive(i2c_smphr);
return -1;
}
b[0] = 1;
b[1] = measmode;
(I2C_IF_Write(0x5a, b, 2, 1));
(I2C_IF_Write(tvoc_i2c_addr, b, 2, 1));

b[0] = 0x24;
(I2C_IF_Write(0x5a, b, 1, 1));
(I2C_IF_Read(0x5a, b, 2));
(I2C_IF_Write(tvoc_i2c_addr, b, 1, 1));
(I2C_IF_Read(tvoc_i2c_addr, b, 2));

LOGE("TVOC FW %d.%d.%d\n",(b[0]>>4) & 0xf,b[0] & 0xf, b[1]);
LOGI("TVOC FW %d.%d.%d\n",(b[0]>>4) & 0xf,b[0] & 0xf, b[1]);
if (b[0] == 0x02 && b[1] == 0x4) {
LOGE("apply TVOC FW 0.2.4 workaround\n");
tvoc_wa = true;
}
else {
tvoc_wa = false;
}

xSemaphoreGiveRecursive(i2c_smphr);
return 0;
Expand All @@ -309,7 +316,7 @@ static int set_tvoc_env(int temp, unsigned int humid){
b[3] |= 1;
}
b[4] = 0;
(I2C_IF_Write(0x5a, b, 5, 1));
(I2C_IF_Write(tvoc_i2c_addr, b, 5, 1));
vTaskDelay(10);
xSemaphoreGiveRecursive(i2c_smphr);
return 0;
Expand Down Expand Up @@ -349,9 +356,9 @@ int get_tvoc(int * tvoc, int * eco2, int * current, int * voltage, int temp, uns
* read alg_result_data
*/
b[0] = 2;
(I2C_IF_Write(0x5a, b, 1, 1));
(I2C_IF_Write(tvoc_i2c_addr, b, 1, 1));
memset(b,0, sizeof(b));
(I2C_IF_Read(0x5a, b, 8));
(I2C_IF_Read(tvoc_i2c_addr, b, 8));

DBG_TVOC("%x:%x:%x:%x:%x:%x:%x:%x\n",
b[0],b[1],b[2],b[3],b[4],b[5],
Expand All @@ -368,8 +375,8 @@ int get_tvoc(int * tvoc, int * eco2, int * current, int * voltage, int temp, uns
if( b[4] & 0x01 ) {
LOGE("TVOC error %x ", b[5] );
b[0] = 0xe0;
(I2C_IF_Write(0x5a, b, 1, 1));
(I2C_IF_Read(0x5a, b, 1));
(I2C_IF_Write(tvoc_i2c_addr, b, 1, 1));
(I2C_IF_Read(tvoc_i2c_addr, b, 1));
LOGE("%x\n", b[0]);
xSemaphoreGiveRecursive(i2c_smphr);
return -1;
Expand Down Expand Up @@ -401,6 +408,137 @@ int Cmd_meas_TVOC(int argc, char *argv[]) {
}
return -1;
}

#define APP_VALID_MASK (0x1 << 4)
inline static uint8_t _tvoc_read_status_reg(void){

uint8_t status_reg_cmd[2] = {0x00, 0xFF};

if(xSemaphoreTakeRecursive(i2c_smphr, 30000)){

(I2C_IF_Write(tvoc_i2c_addr, status_reg_cmd, 1, 1));
(I2C_IF_Read(tvoc_i2c_addr, &status_reg_cmd[1], 1));

xSemaphoreGiveRecursive(i2c_smphr);
}

DBG_TVOC("TV: status reg: %x\n",status_reg_cmd[1]);

return status_reg_cmd[1];

}

inline static uint8_t _tvoc_read_err_id(void){

uint8_t err_id_cmd[2] = {0xE0, 0xFF};

if(xSemaphoreTakeRecursive(i2c_smphr, 30000)){

(I2C_IF_Write(tvoc_i2c_addr, err_id_cmd, 1, 1));
(I2C_IF_Read(tvoc_i2c_addr, &err_id_cmd[1], 1));

xSemaphoreGiveRecursive(i2c_smphr);
}

DBG_TVOC("TV: err id: %x\n",err_id_cmd[1]);

return err_id_cmd[1];

}

inline static int _tvoc_reset(void){

uint8_t sw_reset_cmd[5] = {0xFF, 0x11, 0xE5, 0x72, 0x8A };

assert(xSemaphoreTakeRecursive(i2c_smphr, 30000));

(I2C_IF_Write(tvoc_i2c_addr, sw_reset_cmd, 5, 1));
vTaskDelay(10);
xSemaphoreGiveRecursive(i2c_smphr);

return 0;
}

uint8_t tvoc_get_hw_version(void){

uint8_t hw_ver_cmd[2] = {0x21, 0xFF};

if(xSemaphoreTakeRecursive(i2c_smphr, 30000)){

(I2C_IF_Write(tvoc_i2c_addr, hw_ver_cmd, 1, 1));
(I2C_IF_Read(tvoc_i2c_addr, &hw_ver_cmd[1], 1));

xSemaphoreGiveRecursive(i2c_smphr);
}

return hw_ver_cmd[1];
}

uint16_t tvoc_get_fw_boot_version(void){

uint8_t fw_boot_ver_cmd[3] = {0x23, 0xFF, 0xFF};

if(xSemaphoreTakeRecursive(i2c_smphr, 30000)){

(I2C_IF_Write(tvoc_i2c_addr, fw_boot_ver_cmd, 1, 1));
(I2C_IF_Read(tvoc_i2c_addr, &fw_boot_ver_cmd[1], 2));

xSemaphoreGiveRecursive(i2c_smphr);
}

DBG_TVOC("FW Boot: 0x%x\n",fw_boot_ver_cmd[1]<<8 | fw_boot_ver_cmd[2] );

return (fw_boot_ver_cmd[1]<<8) | fw_boot_ver_cmd[2];

}

uint16_t tvoc_get_fw_app_version(void){

uint8_t fw_app_ver_cmd[3] = {0x24, 0xFF, 0xFF};

if(xSemaphoreTakeRecursive(i2c_smphr, 30000)){

(I2C_IF_Write(tvoc_i2c_addr, fw_app_ver_cmd, 1, 1));
(I2C_IF_Read(tvoc_i2c_addr, &fw_app_ver_cmd[1], 2));

xSemaphoreGiveRecursive(i2c_smphr);
}

DBG_TVOC("FW APP: 0x%x\n",(fw_app_ver_cmd[1] << 8) | fw_app_ver_cmd[2] );

return (fw_app_ver_cmd[1] << 8) | fw_app_ver_cmd[2];

}

// Cmd to get hardware and fw version of CCS811
int cmd_tvoc_get_ver(int argc, char *argv[]) {

LOGI("*TVOC VERSION* \n -Current HW version: 0x%x. FW Boot Version: 0x%x, FW App Version: 0x%x- \n",
tvoc_get_hw_version(), tvoc_get_fw_boot_version(), tvoc_get_fw_app_version());

return 0;
}

// Cmd to get hardware and fw version of CCS811
int cmd_tvoc_status(int argc, char *argv[]) {

LOGI("*TVOC STATUS REG* %x\n", _tvoc_read_status_reg());

return 0;
}

// Cmd to get TVOC error id
int cmd_tvoc_errid(int argc, char *argv[]) {

LOGI("*TVOC ERR ID* %x\n", _tvoc_read_err_id());

return 0;
}

/********************************************************************************
* GAS SENSOR CCS811 - END
********************************************************************************/

static bool haz_tmg4903() {
unsigned char b[2]={0};
b[0] = 0x92;
Expand Down
7 changes: 7 additions & 0 deletions kitsune/i2c_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ int get_ir( int * ir );
int get_rgb_prox( int * w, int * r, int * g, int * bl, int * p );
int get_tvoc(int * tvoc, int * eco2, int * current, int * voltage, int temp, unsigned int humid );

uint8_t tvoc_get_hw_version(void);
uint16_t tvoc_get_fw_boot_version(void);
uint16_t tvoc_get_fw_app_version(void);

typedef enum {
ZOPT_UV = 0,
ZOPT_ALS = 1,
Expand All @@ -32,6 +36,9 @@ int Cmd_read_uv(int argc, char *argv[]);
int Cmd_uvr(int argc, char *argv[]);
int Cmd_uvw(int argc, char *argv[]);
int Cmd_set_tvenv(int argc, char * argv[]);
int cmd_tvoc_get_ver(int argc, char *argv[]);
int cmd_tvoc_status(int argc, char *argv[]);
int cmd_tvoc_errid(int argc, char *argv[]);
int init_humid_sensor();
int init_temp_sensor();

Expand Down
16 changes: 12 additions & 4 deletions kitsune/protobuf/periodic.pb.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.3.1 at Wed Oct 19 09:11:39 2016. */
/* Generated by nanopb-0.3.1 at Mon Feb 06 11:39:23 2017. */

#include "periodic.pb.h"

Expand All @@ -9,7 +9,7 @@



const pb_field_t periodic_data_fields[23] = {
const pb_field_t periodic_data_fields[24] = {
PB_FIELD( 1, INT32 , OPTIONAL, STATIC , FIRST, periodic_data, unix_time, unix_time, 0),
PB_FIELD( 2, INT32 , OPTIONAL, STATIC , OTHER, periodic_data, light, unix_time, 0),
PB_FIELD( 3, INT32 , OPTIONAL, STATIC , OTHER, periodic_data, temperature, light, 0),
Expand All @@ -32,6 +32,7 @@ const pb_field_t periodic_data_fields[23] = {
PB_FIELD( 25, MESSAGE , OPTIONAL, STATIC , OTHER, periodic_data, light_sensor, co2, &periodic_data_light_data_fields),
PB_FIELD( 26, INT32 , OPTIONAL, STATIC , OTHER, periodic_data, light_duration_ms, light_sensor, 0),
PB_FIELD( 27, MESSAGE , OPTIONAL, STATIC , OTHER, periodic_data, keyword_stats, light_duration_ms, &KeywordStats_fields),
PB_FIELD( 28, MESSAGE , OPTIONAL, STATIC , OTHER, periodic_data, tvoc_version, keyword_stats, &periodic_data_TvocVersion_fields),
PB_LAST_FIELD
};

Expand All @@ -46,6 +47,13 @@ const pb_field_t periodic_data_light_data_fields[8] = {
PB_LAST_FIELD
};

const pb_field_t periodic_data_TvocVersion_fields[4] = {
PB_FIELD( 1, UINT32 , OPTIONAL, STATIC , FIRST, periodic_data_TvocVersion, tvoc_app_fw_version, tvoc_app_fw_version, 0),
PB_FIELD( 2, UINT32 , OPTIONAL, STATIC , OTHER, periodic_data_TvocVersion, tvoc_boot_fw_version, tvoc_app_fw_version, 0),
PB_FIELD( 3, UINT32 , OPTIONAL, STATIC , OTHER, periodic_data_TvocVersion, tvoc_hw_version, tvoc_boot_fw_version, 0),
PB_LAST_FIELD
};

const pb_field_t batched_periodic_data_fields[9] = {
PB_FIELD( 1, MESSAGE , REPEATED, CALLBACK, FIRST, batched_periodic_data, data, data, &periodic_data_fields),
PB_FIELD( 2, STRING , REQUIRED, CALLBACK, OTHER, batched_periodic_data, device_id, data, 0),
Expand Down Expand Up @@ -75,7 +83,7 @@ const pb_field_t batched_periodic_data_wifi_access_point_fields[4] = {
* numbers or field sizes that are larger than what can fit in 8 or 16 bit
* field descriptors.
*/
PB_STATIC_ASSERT((pb_membersize(periodic_data, light_sensor) < 65536 && pb_membersize(periodic_data, keyword_stats) < 65536 && pb_membersize(batched_periodic_data, data) < 65536 && pb_membersize(batched_periodic_data, scan) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_periodic_data_periodic_data_light_data_batched_periodic_data_batched_periodic_data_wifi_access_point)
PB_STATIC_ASSERT((pb_membersize(periodic_data, light_sensor) < 65536 && pb_membersize(periodic_data, keyword_stats) < 65536 && pb_membersize(periodic_data, tvoc_version) < 65536 && pb_membersize(batched_periodic_data, data) < 65536 && pb_membersize(batched_periodic_data, scan) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_periodic_data_periodic_data_light_data_periodic_data_TvocVersion_batched_periodic_data_batched_periodic_data_wifi_access_point)
#endif

#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
Expand All @@ -86,7 +94,7 @@ PB_STATIC_ASSERT((pb_membersize(periodic_data, light_sensor) < 65536 && pb_membe
* numbers or field sizes that are larger than what can fit in the default
* 8 bit descriptors.
*/
PB_STATIC_ASSERT((pb_membersize(periodic_data, light_sensor) < 256 && pb_membersize(periodic_data, keyword_stats) < 256 && pb_membersize(batched_periodic_data, data) < 256 && pb_membersize(batched_periodic_data, scan) < 256), YOU_MUST_DEFINE_PB_FIELD_16BIT_FOR_MESSAGES_periodic_data_periodic_data_light_data_batched_periodic_data_batched_periodic_data_wifi_access_point)
PB_STATIC_ASSERT((pb_membersize(periodic_data, light_sensor) < 256 && pb_membersize(periodic_data, keyword_stats) < 256 && pb_membersize(periodic_data, tvoc_version) < 256 && pb_membersize(batched_periodic_data, data) < 256 && pb_membersize(batched_periodic_data, scan) < 256), YOU_MUST_DEFINE_PB_FIELD_16BIT_FOR_MESSAGES_periodic_data_periodic_data_light_data_periodic_data_TvocVersion_batched_periodic_data_batched_periodic_data_wifi_access_point)
#endif


Loading