1010#include "bme688.h"
1111
1212/**
13- * Writes given data to given register(s).
13+ * Writes given register - data pairs to respective register(s).
1414 *
1515 * @param reg register of first register - data pair
1616 * @param data array with data of first register - data pair as first element,
17- * with remaining register - data pair following
17+ * with remaining register - data pairs following
1818 * @param len (number of register - data pairs) - 1
1919 * @param intfPtr
2020 * @return success
@@ -28,15 +28,15 @@ static BME68X_INTF_RET_TYPE bme68xWrite(uint8_t reg,
2828 for (uint32_t i = 0 ; i < len ; i ++ ) {
2929 transmit (data [i ]);
3030 }
31-
3231 PORTC_OUTSET = (1 << PD2 );
3332
3433 // TODO
3534 return 0 ;
3635}
3736
3837/**
39- * Reads given data starting with given register auto-incrementing.
38+ * Reads from register(s) into given data array starting with
39+ * given register auto-incrementing.
4040 *
4141 * @param reg start register
4242 * @param data array with data to be read from consecutive registers
@@ -70,67 +70,64 @@ static void bme68xDelayUs(uint32_t period, void *intfPtr) {
7070 _delay_us (period );
7171}
7272
73- /**
74- * Initializes the BME68x to use SPI interface.
75- *
76- * @param bme bme68x device descriptor
77- * @param pin SPI chip select pin
78- * @return success
79- */
80- uint8_t initBME68xIntf (struct bme68x_dev * bme , uint8_t pin ) {
81- bme -> intf = BME68X_SPI_INTF ;
82- bme -> write = bme68xWrite ;
83- bme -> read = bme68xRead ;
84- bme -> delay_us = bme68xDelayUs ;
85- bme -> intf_ptr = & pin ;
86- bme -> amb_temp = 20 ;
87-
88- return 0 ;
89- }
90-
91- void initBME68x () {
92- struct bme68x_dev bme ;
73+ int8_t initBME68x () {
74+ struct bme68x_dev dev ;
9375 struct bme68x_conf conf ;
9476 struct bme68x_heatr_conf heater_conf ;
9577 struct bme68x_data data ;
9678 int8_t result ;
9779
98- result = initBME68xIntf (& bme , PD2 ); // FIXME pin not used
99- // TODO check result
100- printInt (result );
80+ uint8_t pin = PD2 ;
81+
82+ dev .intf = BME68X_SPI_INTF ;
83+ dev .write = bme68xWrite ;
84+ dev .read = bme68xRead ;
85+ dev .delay_us = bme68xDelayUs ;
86+ dev .intf_ptr = & pin ; // TODO not used
87+ dev .amb_temp = 20 ; // could use temp measured with thermistor ;-)
10188
102- result = bme68x_init (& bme );
103- // TODO check result
104- printInt (result );
89+ result = bme68x_init (& dev );
90+ // TODO properly check result
91+ if (result < 0 ) {
92+ return result ;
93+ }
10594
10695 conf .filter = BME68X_FILTER_OFF ;
10796 conf .odr = BME68X_ODR_NONE ;
10897 conf .os_hum = BME68X_OS_8X ;
10998 conf .os_pres = BME68X_OS_8X ;
11099 conf .os_temp = BME68X_OS_8X ;
111- result = bme68x_set_conf (& conf , & bme );
112- // TODO check result
113- printInt (result );
100+ result = bme68x_set_conf (& conf , & dev );
101+ // TODO properly check result
102+ if (result < 0 ) {
103+ return result ;
104+ }
114105
115106 heater_conf .enable = BME68X_ENABLE ;
116107 heater_conf .heatr_temp = 300 ;
117108 heater_conf .heatr_dur = 100 ;
118- result = bme68x_set_heatr_conf (BME68X_FORCED_MODE , & heater_conf , & bme );
119- // TODO check result
120- printInt (result );
109+ result = bme68x_set_heatr_conf (BME68X_FORCED_MODE , & heater_conf , & dev );
110+ // TODO properly check result
111+ if (result < 0 ) {
112+ return result ;
113+ }
121114
122- result = bme68x_set_op_mode (BME68X_FORCED_MODE , & bme );
123- // TODO check result
124- printInt (result );
115+ result = bme68x_set_op_mode (BME68X_FORCED_MODE , & dev );
116+ // TODO properly check result
117+ if (result < 0 ) {
118+ return result ;
119+ }
125120
126- uint32_t meas_dur = bme68x_get_meas_dur (BME68X_FORCED_MODE , & conf , & bme ) +
121+ uint32_t meas_dur = bme68x_get_meas_dur (BME68X_FORCED_MODE , & conf , & dev ) +
127122 (heater_conf .heatr_dur * 1000 );
128- bme .delay_us (meas_dur , bme .intf_ptr );
123+ dev .delay_us (meas_dur , dev .intf_ptr );
129124
130125 uint8_t n_data ;
131- result = bme68x_get_data (BME68X_FORCED_MODE , & data , & n_data , & bme );
132- // TODO check result
133- printInt (result );
126+ result = bme68x_get_data (BME68X_FORCED_MODE , & data , & n_data , & dev );
127+ // TODO properly check result
128+ if (result < 0 ) {
129+ return result ;
130+ }
134131
135132 div_t tdiv = div (data .temperature , 100 );
136133
@@ -142,4 +139,6 @@ void initBME68x() {
142139 (uint32_t )data .gas_resistance ,
143140 data .status );
144141 printString (buf );
142+
143+ return true;
145144}
0 commit comments