huehuehue
Dependencies: IESE5_merge mbed CCS811 BME680
main.cpp@0:5daf3ccf0e38, 2019-02-19 (annotated)
- Committer:
- etiene32
- Date:
- Tue Feb 19 10:22:33 2019 +0000
- Revision:
- 0:5daf3ccf0e38
huehuehue
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| etiene32 | 0:5daf3ccf0e38 | 1 | #include "mbed.h" |
| etiene32 | 0:5daf3ccf0e38 | 2 | #include "BME680.h" |
| etiene32 | 0:5daf3ccf0e38 | 3 | #include "CCS811.h" |
| etiene32 | 0:5daf3ccf0e38 | 4 | #include "Adafruit_SGP30.h" |
| etiene32 | 0:5daf3ccf0e38 | 5 | |
| etiene32 | 0:5daf3ccf0e38 | 6 | BME680 myBME680 (A4, A5, 400000 ); |
| etiene32 | 0:5daf3ccf0e38 | 7 | Adafruit_SGP30 SGP30(D14, D15); |
| etiene32 | 0:5daf3ccf0e38 | 8 | CCS811 ccs811(D14, D15); |
| etiene32 | 0:5daf3ccf0e38 | 9 | |
| etiene32 | 0:5daf3ccf0e38 | 10 | uint16_t eco2, tvoc; |
| etiene32 | 0:5daf3ccf0e38 | 11 | |
| etiene32 | 0:5daf3ccf0e38 | 12 | Serial pc ( USBTX, USBRX ); |
| etiene32 | 0:5daf3ccf0e38 | 13 | |
| etiene32 | 0:5daf3ccf0e38 | 14 | DigitalOut myled ( LED1 ); |
| etiene32 | 0:5daf3ccf0e38 | 15 | Ticker newReading; |
| etiene32 | 0:5daf3ccf0e38 | 16 | |
| etiene32 | 0:5daf3ccf0e38 | 17 | uint32_t myState = 0; |
| etiene32 | 0:5daf3ccf0e38 | 18 | |
| etiene32 | 0:5daf3ccf0e38 | 19 | |
| etiene32 | 0:5daf3ccf0e38 | 20 | //@brief FUNCTION PROTOTYPES |
| etiene32 | 0:5daf3ccf0e38 | 21 | void changeDATA ( void ); |
| etiene32 | 0:5daf3ccf0e38 | 22 | void user_delay_ms ( uint32_t period ); |
| etiene32 | 0:5daf3ccf0e38 | 23 | int8_t user_i2c_read ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len ); |
| etiene32 | 0:5daf3ccf0e38 | 24 | int8_t user_i2c_write ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len ); |
| etiene32 | 0:5daf3ccf0e38 | 25 | |
| etiene32 | 0:5daf3ccf0e38 | 26 | |
| etiene32 | 0:5daf3ccf0e38 | 27 | //@brief FUNCTION FOR APPLICATION MAIN ENTRY. |
| etiene32 | 0:5daf3ccf0e38 | 28 | int main() |
| etiene32 | 0:5daf3ccf0e38 | 29 | { |
| etiene32 | 0:5daf3ccf0e38 | 30 | pc.baud ( 9600 ); |
| etiene32 | 0:5daf3ccf0e38 | 31 | |
| etiene32 | 0:5daf3ccf0e38 | 32 | myled = 1; |
| etiene32 | 0:5daf3ccf0e38 | 33 | wait(3); |
| etiene32 | 0:5daf3ccf0e38 | 34 | myled = 0; |
| etiene32 | 0:5daf3ccf0e38 | 35 | ccs811.init(); |
| etiene32 | 0:5daf3ccf0e38 | 36 | SGP30.begin(); |
| etiene32 | 0:5daf3ccf0e38 | 37 | SGP30.IAQinit(); |
| etiene32 | 0:5daf3ccf0e38 | 38 | |
| etiene32 | 0:5daf3ccf0e38 | 39 | |
| etiene32 | 0:5daf3ccf0e38 | 40 | struct bme680_dev gas_sensor; |
| etiene32 | 0:5daf3ccf0e38 | 41 | |
| etiene32 | 0:5daf3ccf0e38 | 42 | gas_sensor.dev_id = BME680_I2C_ADDR_PRIMARY; |
| etiene32 | 0:5daf3ccf0e38 | 43 | gas_sensor.intf = BME680_I2C_INTF; |
| etiene32 | 0:5daf3ccf0e38 | 44 | gas_sensor.read = user_i2c_read; |
| etiene32 | 0:5daf3ccf0e38 | 45 | gas_sensor.write = user_i2c_write; |
| etiene32 | 0:5daf3ccf0e38 | 46 | gas_sensor.delay_ms = user_delay_ms; |
| etiene32 | 0:5daf3ccf0e38 | 47 | // amb_temp can be set to 25 prior to configuring the gas sensor |
| etiene32 | 0:5daf3ccf0e38 | 48 | // or by performing a few temperature readings without operating the gas sensor. |
| etiene32 | 0:5daf3ccf0e38 | 49 | gas_sensor.amb_temp = 25; |
| etiene32 | 0:5daf3ccf0e38 | 50 | |
| etiene32 | 0:5daf3ccf0e38 | 51 | |
| etiene32 | 0:5daf3ccf0e38 | 52 | int8_t rslt = BME680_OK; |
| etiene32 | 0:5daf3ccf0e38 | 53 | rslt = myBME680.bme680_init ( &gas_sensor ); |
| etiene32 | 0:5daf3ccf0e38 | 54 | |
| etiene32 | 0:5daf3ccf0e38 | 55 | |
| etiene32 | 0:5daf3ccf0e38 | 56 | uint8_t set_required_settings; |
| etiene32 | 0:5daf3ccf0e38 | 57 | |
| etiene32 | 0:5daf3ccf0e38 | 58 | // Set the temperature, pressure and humidity settings |
| etiene32 | 0:5daf3ccf0e38 | 59 | gas_sensor.tph_sett.os_hum = BME680_OS_2X; |
| etiene32 | 0:5daf3ccf0e38 | 60 | gas_sensor.tph_sett.os_pres = BME680_OS_4X; |
| etiene32 | 0:5daf3ccf0e38 | 61 | gas_sensor.tph_sett.os_temp = BME680_OS_8X; |
| etiene32 | 0:5daf3ccf0e38 | 62 | gas_sensor.tph_sett.filter = BME680_FILTER_SIZE_3; |
| etiene32 | 0:5daf3ccf0e38 | 63 | |
| etiene32 | 0:5daf3ccf0e38 | 64 | // Set the remaining gas sensor settings and link the heating profile |
| etiene32 | 0:5daf3ccf0e38 | 65 | gas_sensor.gas_sett.run_gas = BME680_ENABLE_GAS_MEAS; |
| etiene32 | 0:5daf3ccf0e38 | 66 | // Create a ramp heat waveform in 3 steps |
| etiene32 | 0:5daf3ccf0e38 | 67 | gas_sensor.gas_sett.heatr_temp = 320; // degree Celsius |
| etiene32 | 0:5daf3ccf0e38 | 68 | gas_sensor.gas_sett.heatr_dur = 150; // milliseconds |
| etiene32 | 0:5daf3ccf0e38 | 69 | |
| etiene32 | 0:5daf3ccf0e38 | 70 | // Select the power mode |
| etiene32 | 0:5daf3ccf0e38 | 71 | // Must be set before writing the sensor configuration |
| etiene32 | 0:5daf3ccf0e38 | 72 | gas_sensor.power_mode = BME680_FORCED_MODE; |
| etiene32 | 0:5daf3ccf0e38 | 73 | |
| etiene32 | 0:5daf3ccf0e38 | 74 | // Set the required sensor settings needed |
| etiene32 | 0:5daf3ccf0e38 | 75 | set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_FILTER_SEL | BME680_GAS_SENSOR_SEL; |
| etiene32 | 0:5daf3ccf0e38 | 76 | |
| etiene32 | 0:5daf3ccf0e38 | 77 | // Set the desired sensor configuration |
| etiene32 | 0:5daf3ccf0e38 | 78 | rslt = myBME680.bme680_set_sensor_settings ( set_required_settings, &gas_sensor ); |
| etiene32 | 0:5daf3ccf0e38 | 79 | |
| etiene32 | 0:5daf3ccf0e38 | 80 | // Set the power mode |
| etiene32 | 0:5daf3ccf0e38 | 81 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); |
| etiene32 | 0:5daf3ccf0e38 | 82 | |
| etiene32 | 0:5daf3ccf0e38 | 83 | |
| etiene32 | 0:5daf3ccf0e38 | 84 | // Get the total measurement duration so as to sleep or wait till the measurement is complete |
| etiene32 | 0:5daf3ccf0e38 | 85 | uint16_t meas_period; |
| etiene32 | 0:5daf3ccf0e38 | 86 | myBME680.bme680_get_profile_dur ( &meas_period, &gas_sensor ); |
| etiene32 | 0:5daf3ccf0e38 | 87 | |
| etiene32 | 0:5daf3ccf0e38 | 88 | struct bme680_field_data data; |
| etiene32 | 0:5daf3ccf0e38 | 89 | |
| etiene32 | 0:5daf3ccf0e38 | 90 | |
| etiene32 | 0:5daf3ccf0e38 | 91 | newReading.attach( &changeDATA, 1 ); // the address of the function to be attached ( changeDATA ) and the interval ( 1s ) |
| etiene32 | 0:5daf3ccf0e38 | 92 | |
| etiene32 | 0:5daf3ccf0e38 | 93 | // Let the callbacks take care of everything |
| etiene32 | 0:5daf3ccf0e38 | 94 | while(1) { |
| etiene32 | 0:5daf3ccf0e38 | 95 | sleep(); |
| etiene32 | 0:5daf3ccf0e38 | 96 | |
| etiene32 | 0:5daf3ccf0e38 | 97 | myled = 1; |
| etiene32 | 0:5daf3ccf0e38 | 98 | |
| etiene32 | 0:5daf3ccf0e38 | 99 | if ( myState == 1 ) { |
| etiene32 | 0:5daf3ccf0e38 | 100 | // Delay till the measurement is ready |
| etiene32 | 0:5daf3ccf0e38 | 101 | user_delay_ms ( meas_period ); |
| etiene32 | 0:5daf3ccf0e38 | 102 | SGP30.IAQmeasure(); |
| etiene32 | 0:5daf3ccf0e38 | 103 | //printf("SGP30 %d,%d\r\n",SGP30.eCO2,SGP30.TVOC); |
| etiene32 | 0:5daf3ccf0e38 | 104 | ccs811.readData(&eco2, &tvoc); |
| etiene32 | 0:5daf3ccf0e38 | 105 | //printf("eCO2 reading :%dppm, TVOC reading :%dppb\r\n", eco2, tvoc); |
| etiene32 | 0:5daf3ccf0e38 | 106 | rslt = myBME680.bme680_get_sensor_data ( &data, &gas_sensor ); |
| etiene32 | 0:5daf3ccf0e38 | 107 | |
| etiene32 | 0:5daf3ccf0e38 | 108 | // Prepare the data to be sent through the UART. NOTE: sprintf does NOT allow float numbers, that is why we round the number and plot them as integer |
| etiene32 | 0:5daf3ccf0e38 | 109 | // Avoid using measurements from an unstable heating setup |
| etiene32 | 0:5daf3ccf0e38 | 110 | if ( data.status & BME680_GASM_VALID_MSK ) { |
| etiene32 | 0:5daf3ccf0e38 | 111 | //pc.printf( "T: %.2f degC, P: %.2f hPa, H %.2f %%rH, G: %d ohms\r\n", ( data.temperature/100.0f ), ( data.pressure / 100.0f ), ( data.humidity / 1000.0f ), data.gas_resistance ); |
| etiene32 | 0:5daf3ccf0e38 | 112 | //BME680 : Degres, Pression, Humidité, Voc ; SGO30 : eCO2, TVOC ; CCS811 : eco2, tvoc |
| etiene32 | 0:5daf3ccf0e38 | 113 | pc.printf( "%.2f;%.2f;%.2f;%d;%d;%d;%d;%d\r\n", ( data.temperature/100.0f ), ( data.pressure / 100.0f ), ( data.humidity / 1000.0f ), data.gas_resistance,SGP30.eCO2,SGP30.TVOC, eco2, tvoc); |
| etiene32 | 0:5daf3ccf0e38 | 114 | |
| etiene32 | 0:5daf3ccf0e38 | 115 | } else { |
| etiene32 | 0:5daf3ccf0e38 | 116 | pc.printf( "T: %.2f degC, P: %.2f hPa, H %.2f %%rH\r\n", ( data.temperature/100.0f ), ( data.pressure / 100.0f ), ( data.humidity / 1000.0f ) ); |
| etiene32 | 0:5daf3ccf0e38 | 117 | } |
| etiene32 | 0:5daf3ccf0e38 | 118 | |
| etiene32 | 0:5daf3ccf0e38 | 119 | |
| etiene32 | 0:5daf3ccf0e38 | 120 | // Trigger the next measurement if you would like to read data out continuously |
| etiene32 | 0:5daf3ccf0e38 | 121 | if ( gas_sensor.power_mode == BME680_FORCED_MODE ) { |
| etiene32 | 0:5daf3ccf0e38 | 122 | rslt = myBME680.bme680_set_sensor_mode ( &gas_sensor ); |
| etiene32 | 0:5daf3ccf0e38 | 123 | } |
| etiene32 | 0:5daf3ccf0e38 | 124 | |
| etiene32 | 0:5daf3ccf0e38 | 125 | myState = 0; // Reset the variable |
| etiene32 | 0:5daf3ccf0e38 | 126 | } |
| etiene32 | 0:5daf3ccf0e38 | 127 | |
| etiene32 | 0:5daf3ccf0e38 | 128 | myled = 0; |
| etiene32 | 0:5daf3ccf0e38 | 129 | } |
| etiene32 | 0:5daf3ccf0e38 | 130 | } |
| etiene32 | 0:5daf3ccf0e38 | 131 | |
| etiene32 | 0:5daf3ccf0e38 | 132 | |
| etiene32 | 0:5daf3ccf0e38 | 133 | |
| etiene32 | 0:5daf3ccf0e38 | 134 | |
| etiene32 | 0:5daf3ccf0e38 | 135 | // @brief changeDATA ( void ) |
| etiene32 | 0:5daf3ccf0e38 | 136 | // |
| etiene32 | 0:5daf3ccf0e38 | 137 | // @details It changes myState variable |
| etiene32 | 0:5daf3ccf0e38 | 138 | // |
| etiene32 | 0:5daf3ccf0e38 | 139 | // @param[in] N/A |
| etiene32 | 0:5daf3ccf0e38 | 140 | // |
| etiene32 | 0:5daf3ccf0e38 | 141 | // @param[out] N/A. |
| etiene32 | 0:5daf3ccf0e38 | 142 | // |
| etiene32 | 0:5daf3ccf0e38 | 143 | // |
| etiene32 | 0:5daf3ccf0e38 | 144 | // @return N/A.. |
| etiene32 | 0:5daf3ccf0e38 | 145 | // |
| etiene32 | 0:5daf3ccf0e38 | 146 | // |
| etiene32 | 0:5daf3ccf0e38 | 147 | // @author Manuel Caballero |
| etiene32 | 0:5daf3ccf0e38 | 148 | // @date 21/July/2018 |
| etiene32 | 0:5daf3ccf0e38 | 149 | // @version 21/July/2018 The ORIGIN |
| etiene32 | 0:5daf3ccf0e38 | 150 | // @pre N/A |
| etiene32 | 0:5daf3ccf0e38 | 151 | // @warning N/A. |
| etiene32 | 0:5daf3ccf0e38 | 152 | void changeDATA ( void ) |
| etiene32 | 0:5daf3ccf0e38 | 153 | { |
| etiene32 | 0:5daf3ccf0e38 | 154 | myState = 1; |
| etiene32 | 0:5daf3ccf0e38 | 155 | } |
| etiene32 | 0:5daf3ccf0e38 | 156 | |
| etiene32 | 0:5daf3ccf0e38 | 157 | |
| etiene32 | 0:5daf3ccf0e38 | 158 | |
| etiene32 | 0:5daf3ccf0e38 | 159 | // @brief user_delay_ms ( uint32_t ) |
| etiene32 | 0:5daf3ccf0e38 | 160 | // |
| etiene32 | 0:5daf3ccf0e38 | 161 | // @details Return control or wait, for a period amount of milliseconds |
| etiene32 | 0:5daf3ccf0e38 | 162 | // |
| etiene32 | 0:5daf3ccf0e38 | 163 | // @param[in] period: Delay in milliseconds. |
| etiene32 | 0:5daf3ccf0e38 | 164 | // |
| etiene32 | 0:5daf3ccf0e38 | 165 | // @param[out] N/A. |
| etiene32 | 0:5daf3ccf0e38 | 166 | // |
| etiene32 | 0:5daf3ccf0e38 | 167 | // |
| etiene32 | 0:5daf3ccf0e38 | 168 | // @return N/A.. |
| etiene32 | 0:5daf3ccf0e38 | 169 | // |
| etiene32 | 0:5daf3ccf0e38 | 170 | // |
| etiene32 | 0:5daf3ccf0e38 | 171 | // @author Manuel Caballero |
| etiene32 | 0:5daf3ccf0e38 | 172 | // @date 21/July/2018 |
| etiene32 | 0:5daf3ccf0e38 | 173 | // @version 21/July/2018 The ORIGIN |
| etiene32 | 0:5daf3ccf0e38 | 174 | // @pre This is a Bosh pointer function adapted to our system. |
| etiene32 | 0:5daf3ccf0e38 | 175 | // @warning N/A. |
| etiene32 | 0:5daf3ccf0e38 | 176 | void user_delay_ms ( uint32_t period ) |
| etiene32 | 0:5daf3ccf0e38 | 177 | { |
| etiene32 | 0:5daf3ccf0e38 | 178 | // Return control or wait, |
| etiene32 | 0:5daf3ccf0e38 | 179 | // for a period amount of milliseconds |
| etiene32 | 0:5daf3ccf0e38 | 180 | |
| etiene32 | 0:5daf3ccf0e38 | 181 | wait_ms ( period ); |
| etiene32 | 0:5daf3ccf0e38 | 182 | } |
| etiene32 | 0:5daf3ccf0e38 | 183 | |
| etiene32 | 0:5daf3ccf0e38 | 184 | |
| etiene32 | 0:5daf3ccf0e38 | 185 | |
| etiene32 | 0:5daf3ccf0e38 | 186 | // @brief user_i2c_read ( uint8_t , uint8_t reg_addr, uint8_t *reg_data, uint16_t len ) |
| etiene32 | 0:5daf3ccf0e38 | 187 | // |
| etiene32 | 0:5daf3ccf0e38 | 188 | // @details It adapts I2C reading functionality. |
| etiene32 | 0:5daf3ccf0e38 | 189 | // |
| etiene32 | 0:5daf3ccf0e38 | 190 | // @param[in] dev_id: I2C address. |
| etiene32 | 0:5daf3ccf0e38 | 191 | // @param[in] reg_addr: Register to be read. |
| etiene32 | 0:5daf3ccf0e38 | 192 | // @param[in] len: How many bytes to read. |
| etiene32 | 0:5daf3ccf0e38 | 193 | // |
| etiene32 | 0:5daf3ccf0e38 | 194 | // @param[out] reg_data: Result. |
| etiene32 | 0:5daf3ccf0e38 | 195 | // |
| etiene32 | 0:5daf3ccf0e38 | 196 | // |
| etiene32 | 0:5daf3ccf0e38 | 197 | // @return Status of user_i2c_read. |
| etiene32 | 0:5daf3ccf0e38 | 198 | // |
| etiene32 | 0:5daf3ccf0e38 | 199 | // |
| etiene32 | 0:5daf3ccf0e38 | 200 | // @author Manuel Caballero |
| etiene32 | 0:5daf3ccf0e38 | 201 | // @date 21/July/2018 |
| etiene32 | 0:5daf3ccf0e38 | 202 | // @version 21/July/2018 The ORIGIN |
| etiene32 | 0:5daf3ccf0e38 | 203 | // @pre This is a Bosh pointer function adapted to our system. |
| etiene32 | 0:5daf3ccf0e38 | 204 | // @warning N/A. |
| etiene32 | 0:5daf3ccf0e38 | 205 | int8_t user_i2c_read ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len ) |
| etiene32 | 0:5daf3ccf0e38 | 206 | { |
| etiene32 | 0:5daf3ccf0e38 | 207 | int8_t rslt = 0; // Return 0 for Success, non-zero for failure |
| etiene32 | 0:5daf3ccf0e38 | 208 | |
| etiene32 | 0:5daf3ccf0e38 | 209 | // The parameter dev_id can be used as a variable to store the I2C address of the device |
| etiene32 | 0:5daf3ccf0e38 | 210 | |
| etiene32 | 0:5daf3ccf0e38 | 211 | |
| etiene32 | 0:5daf3ccf0e38 | 212 | // Data on the bus should be like |
| etiene32 | 0:5daf3ccf0e38 | 213 | // |------------+---------------------| |
| etiene32 | 0:5daf3ccf0e38 | 214 | // | I2C action | Data | |
| etiene32 | 0:5daf3ccf0e38 | 215 | // |------------+---------------------| |
| etiene32 | 0:5daf3ccf0e38 | 216 | // | Start | - | |
| etiene32 | 0:5daf3ccf0e38 | 217 | // | Write | (reg_addr) | |
| etiene32 | 0:5daf3ccf0e38 | 218 | // | Stop | - | |
| etiene32 | 0:5daf3ccf0e38 | 219 | // | Start | - | |
| etiene32 | 0:5daf3ccf0e38 | 220 | // | Read | (reg_data[0]) | |
| etiene32 | 0:5daf3ccf0e38 | 221 | // | Read | (....) | |
| etiene32 | 0:5daf3ccf0e38 | 222 | // | Read | (reg_data[len - 1]) | |
| etiene32 | 0:5daf3ccf0e38 | 223 | // | Stop | - | |
| etiene32 | 0:5daf3ccf0e38 | 224 | // |------------+---------------------| |
| etiene32 | 0:5daf3ccf0e38 | 225 | |
| etiene32 | 0:5daf3ccf0e38 | 226 | // Read data |
| etiene32 | 0:5daf3ccf0e38 | 227 | uint32_t aux = 0; |
| etiene32 | 0:5daf3ccf0e38 | 228 | aux = myBME680._i2c.write ( dev_id, (char*)®_addr, 1, true ); |
| etiene32 | 0:5daf3ccf0e38 | 229 | aux = myBME680._i2c.read ( dev_id, (char*)®_data[0], len ); |
| etiene32 | 0:5daf3ccf0e38 | 230 | |
| etiene32 | 0:5daf3ccf0e38 | 231 | |
| etiene32 | 0:5daf3ccf0e38 | 232 | |
| etiene32 | 0:5daf3ccf0e38 | 233 | if ( aux == 0 ) { |
| etiene32 | 0:5daf3ccf0e38 | 234 | rslt = 0; |
| etiene32 | 0:5daf3ccf0e38 | 235 | } else { |
| etiene32 | 0:5daf3ccf0e38 | 236 | rslt = 0xFF; |
| etiene32 | 0:5daf3ccf0e38 | 237 | } |
| etiene32 | 0:5daf3ccf0e38 | 238 | |
| etiene32 | 0:5daf3ccf0e38 | 239 | |
| etiene32 | 0:5daf3ccf0e38 | 240 | return rslt; |
| etiene32 | 0:5daf3ccf0e38 | 241 | } |
| etiene32 | 0:5daf3ccf0e38 | 242 | |
| etiene32 | 0:5daf3ccf0e38 | 243 | |
| etiene32 | 0:5daf3ccf0e38 | 244 | |
| etiene32 | 0:5daf3ccf0e38 | 245 | |
| etiene32 | 0:5daf3ccf0e38 | 246 | // @brief user_i2c_write ( uint8_t , uint8_t reg_addr, uint8_t *reg_data, uint16_t len ) |
| etiene32 | 0:5daf3ccf0e38 | 247 | // |
| etiene32 | 0:5daf3ccf0e38 | 248 | // @details It adapts I2C writing functionality. |
| etiene32 | 0:5daf3ccf0e38 | 249 | // |
| etiene32 | 0:5daf3ccf0e38 | 250 | // @param[in] dev_id: I2C address. |
| etiene32 | 0:5daf3ccf0e38 | 251 | // @param[in] reg_addr: Register to be read. |
| etiene32 | 0:5daf3ccf0e38 | 252 | // @param[out] reg_data: Data to be written. |
| etiene32 | 0:5daf3ccf0e38 | 253 | // @param[in] len: How many bytes to read. |
| etiene32 | 0:5daf3ccf0e38 | 254 | // |
| etiene32 | 0:5daf3ccf0e38 | 255 | // @param[out] N/A. |
| etiene32 | 0:5daf3ccf0e38 | 256 | // |
| etiene32 | 0:5daf3ccf0e38 | 257 | // |
| etiene32 | 0:5daf3ccf0e38 | 258 | // @return Status of user_i2c_write. |
| etiene32 | 0:5daf3ccf0e38 | 259 | // |
| etiene32 | 0:5daf3ccf0e38 | 260 | // |
| etiene32 | 0:5daf3ccf0e38 | 261 | // @author Manuel Caballero |
| etiene32 | 0:5daf3ccf0e38 | 262 | // @date 21/July/2018 |
| etiene32 | 0:5daf3ccf0e38 | 263 | // @version 21/July/2018 The ORIGIN |
| etiene32 | 0:5daf3ccf0e38 | 264 | // @pre This is a Bosh pointer function adapted to our system. |
| etiene32 | 0:5daf3ccf0e38 | 265 | // @warning N/A. |
| etiene32 | 0:5daf3ccf0e38 | 266 | int8_t user_i2c_write ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len ) |
| etiene32 | 0:5daf3ccf0e38 | 267 | { |
| etiene32 | 0:5daf3ccf0e38 | 268 | int8_t rslt = 0; // Return 0 for Success, non-zero for failure |
| etiene32 | 0:5daf3ccf0e38 | 269 | |
| etiene32 | 0:5daf3ccf0e38 | 270 | // The parameter dev_id can be used as a variable to store the I2C address of the device |
| etiene32 | 0:5daf3ccf0e38 | 271 | |
| etiene32 | 0:5daf3ccf0e38 | 272 | |
| etiene32 | 0:5daf3ccf0e38 | 273 | // Data on the bus should be like |
| etiene32 | 0:5daf3ccf0e38 | 274 | // |------------+---------------------| |
| etiene32 | 0:5daf3ccf0e38 | 275 | // | I2C action | Data | |
| etiene32 | 0:5daf3ccf0e38 | 276 | // |------------+---------------------| |
| etiene32 | 0:5daf3ccf0e38 | 277 | // | Start | - | |
| etiene32 | 0:5daf3ccf0e38 | 278 | // | Write | (reg_addr) | |
| etiene32 | 0:5daf3ccf0e38 | 279 | // | Write | (reg_data[0]) | |
| etiene32 | 0:5daf3ccf0e38 | 280 | // | Write | (....) | |
| etiene32 | 0:5daf3ccf0e38 | 281 | // | Write | (reg_data[len - 1]) | |
| etiene32 | 0:5daf3ccf0e38 | 282 | // | Stop | - | |
| etiene32 | 0:5daf3ccf0e38 | 283 | // |------------+---------------------| |
| etiene32 | 0:5daf3ccf0e38 | 284 | |
| etiene32 | 0:5daf3ccf0e38 | 285 | uint32_t aux = 0; |
| etiene32 | 0:5daf3ccf0e38 | 286 | char cmd[16] = { 0 }; |
| etiene32 | 0:5daf3ccf0e38 | 287 | uint32_t i = 0; |
| etiene32 | 0:5daf3ccf0e38 | 288 | |
| etiene32 | 0:5daf3ccf0e38 | 289 | // Prepare the data to be sent |
| etiene32 | 0:5daf3ccf0e38 | 290 | cmd[0] = reg_addr; |
| etiene32 | 0:5daf3ccf0e38 | 291 | for ( i = 1; i <= len; i++ ) { |
| etiene32 | 0:5daf3ccf0e38 | 292 | cmd[i] = reg_data[i - 1]; |
| etiene32 | 0:5daf3ccf0e38 | 293 | } |
| etiene32 | 0:5daf3ccf0e38 | 294 | |
| etiene32 | 0:5daf3ccf0e38 | 295 | // Write data |
| etiene32 | 0:5daf3ccf0e38 | 296 | aux = myBME680._i2c.write ( dev_id, &cmd[0], len + 1, false ); |
| etiene32 | 0:5daf3ccf0e38 | 297 | |
| etiene32 | 0:5daf3ccf0e38 | 298 | |
| etiene32 | 0:5daf3ccf0e38 | 299 | |
| etiene32 | 0:5daf3ccf0e38 | 300 | if ( aux == 0 ) { |
| etiene32 | 0:5daf3ccf0e38 | 301 | rslt = 0; |
| etiene32 | 0:5daf3ccf0e38 | 302 | } else { |
| etiene32 | 0:5daf3ccf0e38 | 303 | rslt = 0xFF; |
| etiene32 | 0:5daf3ccf0e38 | 304 | } |
| etiene32 | 0:5daf3ccf0e38 | 305 | |
| etiene32 | 0:5daf3ccf0e38 | 306 | |
| etiene32 | 0:5daf3ccf0e38 | 307 | return rslt; |
| etiene32 | 0:5daf3ccf0e38 | 308 | } |