45 #if (CCS811_FIRMWARE_UPDATE>0)
46 #pragma message("CCS811 firmware update included")
47 #include "ccs811_firmware.h"
48 static uint32_t CCS811_firmwareVerificationAndUpdate(
void );
50 static uint32_t CCS811_eraseApplication (
void );
51 static bool CCS811_verifyApplication (
void );
53 static void debug_printf(
const char *format, ...);
56 static uint32_t CCS811_setAppStart (
void );
88 #if (CCS811_FIRMWARE_UPDATE>0)
89 status = CCS811_firmwareVerificationAndUpdate();
178 if( ( status ==
CCS811_OK ) && ( ( reg & 0x08 ) == 0x08 ) ) {
199 printf(
"STATUS : %02X\r\n", buffer[0] );
202 printf(
"MEASURE_MODE : %02X\r\n", buffer[0] );
205 printf(
"ALG_DATA : %02X%02X %02X%02X\r\n", buffer[0], buffer[1], buffer[2], buffer[3] );
208 printf(
"RAW_DATA : %02X%02X\r\n", buffer[0], buffer[1] );
211 printf(
"ENV_DATA : %02X%02X %02X%02X\r\n", buffer[0], buffer[1], buffer[2], buffer[3] );
214 printf(
"NTC : %02X%02X %02X%02X\r\n", buffer[0], buffer[1], buffer[2], buffer[3] );
217 printf(
"THRESHOLDS : %02X%02X %02X%02X\r\n", buffer[0], buffer[1], buffer[2], buffer[3] );
220 printf(
"HW_ID : %02X\r\n", buffer[0] );
223 printf(
"HW_VERSION : %02X\r\n", buffer[0] );
226 printf(
"BOOT_VERSION : %d.%d.%d\r\n", ( buffer[0] >> 4 ) & 0xF, buffer[0] & 0xF, buffer[1] );
229 printf(
"APP_VERSION : %d.%d.%d\r\n", ( buffer[0] >> 4 ) & 0xF, buffer[0] & 0xF, buffer[1] );
232 printf(
"ERR_ID : %02X\r\n", buffer[0] );
255 if( ( status & 0x10 ) != 0x10 ) {
260 result += CCS811_setAppStart();
266 if( ( status & 0x90 ) != 0x90 ) {
295 uint8_t i2c_write_data[1];
303 i2c_write_data[0] = id;
308 seq.
buf[0].
data = i2c_write_data;
344 uint8_t i2c_read_data[4];
345 uint8_t i2c_write_data[1];
360 seq.
buf[0].
data = i2c_write_data;
362 seq.
buf[1].
data = i2c_read_data;
370 *eco2 = ( (uint16_t) i2c_read_data[0] << 8 ) + (uint16_t) i2c_read_data[1];
371 *tvoc = ( (uint16_t) i2c_read_data[2] << 8 ) + (uint16_t) i2c_read_data[3];
399 uint8_t i2c_read_data[2];
400 uint8_t i2c_write_data[1];
415 seq.
buf[0].
data = i2c_write_data;
417 seq.
buf[1].
data = i2c_read_data;
425 *current = (uint16_t) ( ( i2c_read_data[0] >> 2 ) & 0x3F );
427 *rawData = (uint16_t) ( ( i2c_read_data[0] & 0x03 ) << 8 ) + (uint16_t) i2c_read_data[1];
448 uint8_t i2c_read_data[4];
449 uint8_t i2c_write_data[5];
459 i2c_write_data[1] = 0x11;
460 i2c_write_data[2] = 0xE5;
461 i2c_write_data[3] = 0x72;
462 i2c_write_data[4] = 0x8A;
466 seq.
buf[0].
data = i2c_write_data;
468 seq.
buf[1].
data = i2c_read_data;
497 uint8_t i2c_read_data[1];
498 uint8_t i2c_write_data[2];
506 measMode = ( measMode & 0x38 );
510 i2c_write_data[1] = measMode;
514 seq.
buf[0].
data = i2c_write_data;
516 seq.
buf[1].
data = i2c_read_data;
539 static uint32_t CCS811_setAppStart(
void )
544 uint8_t i2c_read_data[2];
545 uint8_t i2c_write_data[1];
558 seq.
buf[0].
data = i2c_write_data;
560 seq.
buf[1].
data = i2c_read_data;
574 #if (CCS811_FIRMWARE_UPDATE>0)
583 static uint32_t CCS811_eraseApplication(
void )
588 uint8_t i2c_read_data[4];
589 uint8_t i2c_write_data[5];
599 i2c_write_data[1] = 0xE7;
600 i2c_write_data[2] = 0xA7;
601 i2c_write_data[3] = 0xE6;
602 i2c_write_data[4] = 0x09;
606 seq.
buf[0].
data = i2c_write_data;
608 seq.
buf[1].
data = i2c_read_data;
632 static uint32_t CCS811_programFirmware( uint8_t buffer[] )
637 uint8_t i2c_read_data[2];
638 uint8_t i2c_write_data[9];
647 i2c_write_data[1] = buffer[0];
648 i2c_write_data[2] = buffer[1];
649 i2c_write_data[3] = buffer[2];
650 i2c_write_data[4] = buffer[3];
651 i2c_write_data[5] = buffer[4];
652 i2c_write_data[6] = buffer[5];
653 i2c_write_data[7] = buffer[6];
654 i2c_write_data[8] = buffer[7];
658 seq.
buf[0].
data = i2c_write_data;
660 seq.
buf[1].
data = i2c_read_data;
682 static bool CCS811_verifyApplication(
void )
689 uint8_t i2c_read_data[2];
690 uint8_t i2c_write_data[2];
703 seq.
buf[0].
data = i2c_write_data;
705 seq.
buf[1].
data = i2c_read_data;
716 if( ( status ==
CCS811_OK ) && ( ( reg & 0x10 ) == 0x00 ) ) {
736 static uint32_t CCS811_firmwareVerificationAndUpdate(
void )
745 uint8_t major, minor, patch;
746 int fmajor, fminor, fpatch;
748 char vStr[3 + 1 + 3 + 1 + 3];
757 major = ( buffer[0] >> 4 ) & 0xF;
758 minor = buffer[0] & 0xF;
760 debug_printf(
"BOOT_VERSION : %d.%d.%d\r\n", major, minor, patch );
763 major = ( buffer[0] >> 4 ) & 0xF;
764 minor = buffer[0] & 0xF;
766 debug_printf(
"APP_VERSION : %d.%d.%d\r\n", major, minor, patch );
768 fileName = RFS_getFileNameByIndex( 0 );
769 if( !RFS_fileOpen( &fh, fileName ) ) {
770 debug_printf(
"RFS file open failed!\r\n" );
773 fileLength = RFS_getFileLength( &fh );
775 debug_printf(
"RFS file : %s [%d]\r\n", (
char *) fileName, fileLength );
776 if( ( fileLength % 8 ) != 0 ) {
777 debug_printf(
"RFS file not 8 byte multiple!\r\n" );
784 for( i = 0; i < strlen( (
char const *) fileName ); i++ ) {
785 if( fileName[i] ==
'_' ) {
788 if( fileName[i] ==
'.' ) {
795 memset( vStr, 0,
sizeof( vStr ) );
796 for( i = pus; i <= pi; i++ ) {
797 vStr[i - pus] = fileName[i];
800 n = sscanf( vStr,
"%dv%dp%d", &fmajor, &fminor, &fpatch );
806 upgrade = (fmajor > major) ||
807 ((fmajor == major) && (fminor > minor)) ||
808 ((fmajor == major) && (fminor == minor) && (fpatch > patch));
811 if( ( major == 0xf ) && ( minor == 0xf ) && ( patch == 0xff ) ) {
823 if( ( reg & 0x80 ) == 0x80 ) {
828 if( ( status & 0x80 ) != 0x00 ) {
836 if( ( reg & 0x80 ) != 0x00 ) {
846 if( ( reg & 0x10 ) == 0x10 ) {
850 status = CCS811_eraseApplication();
852 if( ( status & 0x10 ) != 0x00 ) {
860 if( ( reg & 0x10 ) != 0x00 ) {
869 for( i = 0; i < fileLength; i += 8 ) {
872 n = RFS_fileRead( buffer, 1, 8, &fh );
873 debug_printf(
"FW prog [%d] : %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X ",
875 buffer[0], buffer[1], buffer[2], buffer[3],
876 buffer[4], buffer[5], buffer[6], buffer[7]
883 status = CCS811_programFirmware( buffer );
885 debug_printf(
" FAIL! \r\n" );
889 debug_printf(
" OK \r\n" );
896 if( CCS811_verifyApplication() ==
false ) {
903 debug_printf(
"Cycling CCS811 power domain..." );
908 debug_printf(
"OK\r\n" );
914 static void debug_printf(
const char *format, ... )
919 va_start(args, format);
920 vprintf(format, args);
947 uint8_t i2c_read_data[4];
948 uint8_t i2c_write_data[5];
949 uint8_t humidityRegValue;
950 uint8_t temperatureRegValue;
960 if( ( ( rhData % 1000 ) / 100 ) > 7 ) {
961 humidityRegValue = ( rhData / 1000 + 1 ) << 1;
964 humidityRegValue = ( rhData / 1000 ) << 1;
969 if( ( ( rhData % 1000 ) / 100 ) > 2 && ( ( ( rhData % 1000 ) / 100 ) < 8 ) ) {
970 humidityRegValue |= 0x01;
978 if( ( ( tempData % 1000 ) / 100 ) > 7 ) {
979 temperatureRegValue = ( tempData / 1000 + 1 ) << 1;
982 temperatureRegValue = ( tempData / 1000 ) << 1;
987 if( ( ( tempData % 1000 ) / 100 ) > 2 && ( ( ( tempData % 1000 ) / 100 ) < 8 ) ) {
988 temperatureRegValue |= 0x01;
995 i2c_write_data[1] = humidityRegValue;
996 i2c_write_data[2] = 0x00;
997 i2c_write_data[3] = temperatureRegValue;
998 i2c_write_data[4] = 0x00;
1002 seq.
buf[0].
data = i2c_write_data;
1004 seq.
buf[1].
data = i2c_read_data;
#define CCS811_ADDR_APP_START
uint32_t CCS811_setMeasureMode(uint8_t measMode)
Sets the measurement mode of the CSS811 sensor.
uint32_t BOARD_gasSensorWake(bool wake)
Wakes up the Air Quality / Gas Sensor.
I2C_TransferReturn_TypeDef I2CSPM_Transfer(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq)
Perform I2C transfer.
#define CCS811_I2C_DEVICE_BUS_ADDRESS
uint32_t CCS811_setEnvData(int32_t tempData, uint32_t rhData)
Writes temperature and humidity values to the environmental data regs.
bool CCS811_isDataAvailable(void)
Checks if new measurement data available.
Driver for the Cambridge CMOS Sensors CCS811 gas and indoor air quality sensor.
#define CCS811_ERROR_FIRMWARE_UPDATE_FAILED
#define CCS811_ADDR_HW_VERSION
void UTIL_delay(uint32_t ms)
Delays number of msTick Systicks (1 ms)
uint32_t CCS811_getStatus(uint8_t *status)
Reads the status of the CSS811 sensor.
uint32_t CCS811_init(void)
Initializes the chip and performs firmware upgrade if required.
#define CCS811_ADDR_SW_RESET
#define CCS811_ERROR_NOT_IN_APPLICATION_MODE
uint32_t CCS811_softwareReset(void)
Performs software reset on the CCS811.
#define I2C_FLAG_WRITE
Indicate plain write sequence: S+ADDR(W)+DATA0+P.
I2C_TransferReturn_TypeDef
#define CCS811_ERROR_INIT_FAILED
#define CCS811_ERROR_I2C_TRANSACTION_FAILED
#define CCS811_ADDR_ENV_DATA
uint32_t CCS811_getHardwareID(uint8_t *hardwareID)
Reads Hardware ID from the CSS811 sensor.
#define CCS811_ADDR_FW_ERASE
uint32_t CCS811_startApplication(void)
Switches the CSS811 chip from boot to application mode.
I2C simple poll-based master mode driver for the DK/STK.
#define CCS811_ADDR_ERR_ID
Utility Functions for the Thunderboard Sense.
struct I2C_TransferSeq_TypeDef::@0 buf[2]
#define CCS811_ADDR_STATUS
#define CCS811_ERROR_APPLICATION_NOT_PRESENT
uint32_t CCS811_getMeasurement(uint16_t *eco2, uint16_t *tvoc)
Reads measurement data (eCO2 and TVOC) from the CSS811 sensor.
#define CCS811_ADDR_MEASURE_MODE
uint32_t CCS811_readMailbox(uint8_t id, uint8_t length, uint8_t *data)
Reads data from a specific Mailbox address.
#define CCS811_ADDR_THRESHOLDS
Master mode transfer message structure used to define a complete I2C transfer sequence (from start to...
#define CCS811_ADDR_FW_PROGRAM
#define CCS811_ADDR_HW_ID
#define CCS811_ADDR_RAW_DATA
#define CCS811_ADDR_ALG_RESULT_DATA
#define CCS811_ADDR_FW_BOOT_VERSION
#define I2C_FLAG_WRITE_READ
Indicate combined write/read sequence: S+ADDR(W)+DATA0+Sr+ADDR(R)+DATA1+P.
uint32_t CCS811_getRawData(uint16_t *current, uint16_t *rawData)
Gets the latest readings from the sense resistor of the CSS811 sensor.
uint32_t CCS811_deInit(void)
De-initializes the chip.
#define CCS811_ADDR_FW_APP_VERSION
#define CCS811_ADDR_FW_VERIFY
void CCS811_dumpRegisters(void)
Dumps the registers of the CSS811.
uint16_t addr
Address to use after (repeated) start.
BOARD module header file.
#define CCS811_I2C_DEVICE
uint32_t BOARD_gasSensorEnable(bool enable)
Enables or disables the Air Quality / Gas Sensor.