12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496 |
- #include "bme280.h"
- #define OVERSAMPLING_SETTINGS UINT8_C(0x07)
- #define FILTER_STANDBY_SETTINGS UINT8_C(0x18)
- static int8_t put_device_to_sleep(const struct bme280_dev *dev);
- static int8_t write_power_mode(uint8_t sensor_mode, const struct bme280_dev *dev);
- static int8_t null_ptr_check(const struct bme280_dev *dev);
- static void interleave_reg_addr(const uint8_t *reg_addr, uint8_t *temp_buff, const uint8_t *reg_data, uint8_t len);
- static int8_t get_calib_data(struct bme280_dev *dev);
- static void parse_temp_press_calib_data(const uint8_t *reg_data, struct bme280_dev *dev);
- static void parse_humidity_calib_data(const uint8_t *reg_data, struct bme280_dev *dev);
- #ifdef BME280_FLOAT_ENABLE
- static double compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
- const struct bme280_calib_data *calib_data);
- static double compensate_humidity(const struct bme280_uncomp_data *uncomp_data,
- const struct bme280_calib_data *calib_data);
- static double compensate_temperature(const struct bme280_uncomp_data *uncomp_data,
- struct bme280_calib_data *calib_data);
- #else
- static int32_t compensate_temperature(const struct bme280_uncomp_data *uncomp_data,
- struct bme280_calib_data *calib_data);
- static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
- const struct bme280_calib_data *calib_data);
- static uint32_t compensate_humidity(const struct bme280_uncomp_data *uncomp_data,
- const struct bme280_calib_data *calib_data);
- #endif
- static uint8_t are_settings_changed(uint8_t sub_settings, uint8_t desired_settings);
- static int8_t set_osr_humidity_settings(const struct bme280_settings *settings, const struct bme280_dev *dev);
- static int8_t set_osr_settings(uint8_t desired_settings,
- const struct bme280_settings *settings,
- const struct bme280_dev *dev);
- static int8_t set_osr_press_temp_settings(uint8_t desired_settings,
- const struct bme280_settings *settings,
- const struct bme280_dev *dev);
- static void fill_osr_press_settings(uint8_t *reg_data, const struct bme280_settings *settings);
- static void fill_osr_temp_settings(uint8_t *reg_data, const struct bme280_settings *settings);
- static int8_t set_filter_standby_settings(uint8_t desired_settings,
- const struct bme280_settings *settings,
- const struct bme280_dev *dev);
- static void fill_filter_settings(uint8_t *reg_data, const struct bme280_settings *settings);
- static void fill_standby_settings(uint8_t *reg_data, const struct bme280_settings *settings);
- static void parse_device_settings(const uint8_t *reg_data, struct bme280_settings *settings);
- static int8_t reload_device_settings(const struct bme280_settings *settings, const struct bme280_dev *dev);
- int8_t bme280_init(struct bme280_dev *dev)
- {
- int8_t rslt;
-
- uint8_t try_count = 5;
- uint8_t chip_id = 0;
-
- rslt = null_ptr_check(dev);
-
- if (rslt == BME280_OK)
- {
- while (try_count)
- {
-
- rslt = bme280_get_regs(BME280_CHIP_ID_ADDR, &chip_id, 1, dev);
-
- if ((rslt == BME280_OK) && (chip_id == BME280_CHIP_ID))
- {
- dev->chip_id = chip_id;
-
- rslt = bme280_soft_reset(dev);
- if (rslt == BME280_OK)
- {
-
- rslt = get_calib_data(dev);
- }
- break;
- }
-
- dev->delay_ms(1);
- --try_count;
- }
-
- if (!try_count)
- {
- rslt = BME280_E_DEV_NOT_FOUND;
- }
- }
- return rslt;
- }
- int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, const struct bme280_dev *dev)
- {
- int8_t rslt;
-
- rslt = null_ptr_check(dev);
-
- if (rslt == BME280_OK)
- {
-
- if (dev->intf != BME280_I2C_INTF)
- {
- reg_addr = reg_addr | 0x80;
- }
-
- rslt = dev->read(dev->dev_id, reg_addr, reg_data, len);
-
- if (rslt != BME280_OK)
- {
- rslt = BME280_E_COMM_FAIL;
- }
- }
- return rslt;
- }
- int8_t bme280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len, const struct bme280_dev *dev)
- {
- int8_t rslt;
- uint8_t temp_buff[20];
- if (len > 10)
- {
- len = 10;
- }
- uint16_t temp_len;
- uint8_t reg_addr_cnt;
-
- rslt = null_ptr_check(dev);
-
- if ((rslt == BME280_OK) && (reg_addr != NULL) && (reg_data != NULL))
- {
- if (len != 0)
- {
- temp_buff[0] = reg_data[0];
-
- if (dev->intf != BME280_I2C_INTF)
- {
- for (reg_addr_cnt = 0; reg_addr_cnt < len; reg_addr_cnt++)
- {
- reg_addr[reg_addr_cnt] = reg_addr[reg_addr_cnt] & 0x7F;
- }
- }
-
- if (len > 1)
- {
-
- interleave_reg_addr(reg_addr, temp_buff, reg_data, len);
- temp_len = ((len * 2) - 1);
- }
- else
- {
- temp_len = len;
- }
- rslt = dev->write(dev->dev_id, reg_addr[0], temp_buff, temp_len);
-
- if (rslt != BME280_OK)
- {
- rslt = BME280_E_COMM_FAIL;
- }
- }
- else
- {
- rslt = BME280_E_INVALID_LEN;
- }
- }
- else
- {
- rslt = BME280_E_NULL_PTR;
- }
- return rslt;
- }
- int8_t bme280_set_sensor_settings(uint8_t desired_settings, const struct bme280_dev *dev)
- {
- int8_t rslt;
- uint8_t sensor_mode;
-
- rslt = null_ptr_check(dev);
-
- if (rslt == BME280_OK)
- {
- rslt = bme280_get_sensor_mode(&sensor_mode, dev);
- if ((rslt == BME280_OK) && (sensor_mode != BME280_SLEEP_MODE))
- {
- rslt = put_device_to_sleep(dev);
- }
- if (rslt == BME280_OK)
- {
-
- if (are_settings_changed(OVERSAMPLING_SETTINGS, desired_settings))
- {
- rslt = set_osr_settings(desired_settings, &dev->settings, dev);
- }
-
- if ((rslt == BME280_OK) && are_settings_changed(FILTER_STANDBY_SETTINGS, desired_settings))
- {
- rslt = set_filter_standby_settings(desired_settings, &dev->settings, dev);
- }
- }
- }
- return rslt;
- }
- int8_t bme280_get_sensor_settings(struct bme280_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_data[4];
-
- rslt = null_ptr_check(dev);
-
- if (rslt == BME280_OK)
- {
- rslt = bme280_get_regs(BME280_CTRL_HUM_ADDR, reg_data, 4, dev);
- if (rslt == BME280_OK)
- {
- parse_device_settings(reg_data, &dev->settings);
- }
- }
- return rslt;
- }
- int8_t bme280_set_sensor_mode(uint8_t sensor_mode, const struct bme280_dev *dev)
- {
- int8_t rslt;
- uint8_t last_set_mode;
-
- rslt = null_ptr_check(dev);
- if (rslt == BME280_OK)
- {
- rslt = bme280_get_sensor_mode(&last_set_mode, dev);
-
- if ((rslt == BME280_OK) && (last_set_mode != BME280_SLEEP_MODE))
- {
- rslt = put_device_to_sleep(dev);
- }
-
- if (rslt == BME280_OK)
- {
- rslt = write_power_mode(sensor_mode, dev);
- }
- }
- return rslt;
- }
- int8_t bme280_get_sensor_mode(uint8_t *sensor_mode, const struct bme280_dev *dev)
- {
- int8_t rslt;
-
- rslt = null_ptr_check(dev);
- if (rslt == BME280_OK)
- {
-
- rslt = bme280_get_regs(BME280_PWR_CTRL_ADDR, sensor_mode, 1, dev);
-
- *sensor_mode = BME280_GET_BITS_POS_0(*sensor_mode, BME280_SENSOR_MODE);
- }
- return rslt;
- }
- int8_t bme280_soft_reset(const struct bme280_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_addr = BME280_RESET_ADDR;
- uint8_t status_reg = 0;
- uint8_t try_run = 5;
-
- uint8_t soft_rst_cmd = BME280_SOFT_RESET_COMMAND;
-
- rslt = null_ptr_check(dev);
-
- if (rslt == BME280_OK)
- {
-
- rslt = bme280_set_regs(®_addr, &soft_rst_cmd, 1, dev);
- if (rslt == BME280_OK)
- {
-
- do
- {
-
- dev->delay_ms(2);
- rslt = bme280_get_regs(BME280_STATUS_REG_ADDR, &status_reg, 1, dev);
- } while ((rslt == BME280_OK) && (try_run--) && (status_reg & BME280_STATUS_IM_UPDATE));
- if (status_reg & BME280_STATUS_IM_UPDATE)
- {
- rslt = BME280_E_NVM_COPY_FAILED;
- }
- }
- }
- return rslt;
- }
- int8_t bme280_get_sensor_data(uint8_t sensor_comp, struct bme280_data *comp_data, struct bme280_dev *dev)
- {
- int8_t rslt;
-
- uint8_t reg_data[BME280_P_T_H_DATA_LEN] = { 0 };
- struct bme280_uncomp_data uncomp_data = { 0 };
-
- rslt = null_ptr_check(dev);
- if ((rslt == BME280_OK) && (comp_data != NULL))
- {
-
- rslt = bme280_get_regs(BME280_DATA_ADDR, reg_data, BME280_P_T_H_DATA_LEN, dev);
- if (rslt == BME280_OK)
- {
-
- bme280_parse_sensor_data(reg_data, &uncomp_data);
-
- rslt = bme280_compensate_data(sensor_comp, &uncomp_data, comp_data, &dev->calib_data);
- }
- }
- else
- {
- rslt = BME280_E_NULL_PTR;
- }
- return rslt;
- }
- void bme280_parse_sensor_data(const uint8_t *reg_data, struct bme280_uncomp_data *uncomp_data)
- {
-
- uint32_t data_xlsb;
- uint32_t data_lsb;
- uint32_t data_msb;
-
- data_msb = (uint32_t)reg_data[0] << 12;
- data_lsb = (uint32_t)reg_data[1] << 4;
- data_xlsb = (uint32_t)reg_data[2] >> 4;
- uncomp_data->pressure = data_msb | data_lsb | data_xlsb;
-
- data_msb = (uint32_t)reg_data[3] << 12;
- data_lsb = (uint32_t)reg_data[4] << 4;
- data_xlsb = (uint32_t)reg_data[5] >> 4;
- uncomp_data->temperature = data_msb | data_lsb | data_xlsb;
-
- data_msb = (uint32_t)reg_data[6] << 8;
- data_lsb = (uint32_t)reg_data[7];
- uncomp_data->humidity = data_msb | data_lsb;
- }
- int8_t bme280_compensate_data(uint8_t sensor_comp,
- const struct bme280_uncomp_data *uncomp_data,
- struct bme280_data *comp_data,
- struct bme280_calib_data *calib_data)
- {
- int8_t rslt = BME280_OK;
- if ((uncomp_data != NULL) && (comp_data != NULL) && (calib_data != NULL))
- {
-
- comp_data->temperature = 0;
- comp_data->pressure = 0;
- comp_data->humidity = 0;
-
- if (sensor_comp & (BME280_PRESS | BME280_TEMP | BME280_HUM))
- {
-
- comp_data->temperature = compensate_temperature(uncomp_data, calib_data);
- }
- if (sensor_comp & BME280_PRESS)
- {
-
- comp_data->pressure = compensate_pressure(uncomp_data, calib_data);
- }
- if (sensor_comp & BME280_HUM)
- {
-
- comp_data->humidity = compensate_humidity(uncomp_data, calib_data);
- }
- }
- else
- {
- rslt = BME280_E_NULL_PTR;
- }
- return rslt;
- }
- uint32_t bme280_cal_meas_delay(const struct bme280_settings *settings)
- {
- uint32_t max_delay;
- uint8_t temp_osr;
- uint8_t pres_osr;
- uint8_t hum_osr;
-
- uint8_t osr_sett_to_act_osr[] = { 0, 1, 2, 4, 8, 16 };
-
- if (settings->osr_t <= 5)
- {
- temp_osr = osr_sett_to_act_osr[settings->osr_t];
- }
- else
- {
- temp_osr = 16;
- }
- if (settings->osr_p <= 5)
- {
- pres_osr = osr_sett_to_act_osr[settings->osr_p];
- }
- else
- {
- pres_osr = 16;
- }
- if (settings->osr_h <= 5)
- {
- hum_osr = osr_sett_to_act_osr[settings->osr_h];
- }
- else
- {
- hum_osr = 16;
- }
- max_delay =
- (uint32_t)((BME280_MEAS_OFFSET + (BME280_MEAS_DUR * temp_osr) +
- ((BME280_MEAS_DUR * pres_osr) + BME280_PRES_HUM_MEAS_OFFSET) +
- ((BME280_MEAS_DUR * hum_osr) + BME280_PRES_HUM_MEAS_OFFSET)) / BME280_MEAS_SCALING_FACTOR);
- return max_delay;
- }
- static int8_t set_osr_settings(uint8_t desired_settings,
- const struct bme280_settings *settings,
- const struct bme280_dev *dev)
- {
- int8_t rslt = BME280_W_INVALID_OSR_MACRO;
- if (desired_settings & BME280_OSR_HUM_SEL)
- {
- rslt = set_osr_humidity_settings(settings, dev);
- }
- if (desired_settings & (BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL))
- {
- rslt = set_osr_press_temp_settings(desired_settings, settings, dev);
- }
- return rslt;
- }
- static int8_t set_osr_humidity_settings(const struct bme280_settings *settings, const struct bme280_dev *dev)
- {
- int8_t rslt;
- uint8_t ctrl_hum;
- uint8_t ctrl_meas;
- uint8_t reg_addr = BME280_CTRL_HUM_ADDR;
- ctrl_hum = settings->osr_h & BME280_CTRL_HUM_MSK;
-
- rslt = bme280_set_regs(®_addr, &ctrl_hum, 1, dev);
-
- if (rslt == BME280_OK)
- {
- reg_addr = BME280_CTRL_MEAS_ADDR;
- rslt = bme280_get_regs(reg_addr, &ctrl_meas, 1, dev);
- if (rslt == BME280_OK)
- {
- rslt = bme280_set_regs(®_addr, &ctrl_meas, 1, dev);
- }
- }
- return rslt;
- }
- static int8_t set_osr_press_temp_settings(uint8_t desired_settings,
- const struct bme280_settings *settings,
- const struct bme280_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_addr = BME280_CTRL_MEAS_ADDR;
- uint8_t reg_data;
- rslt = bme280_get_regs(reg_addr, ®_data, 1, dev);
- if (rslt == BME280_OK)
- {
- if (desired_settings & BME280_OSR_PRESS_SEL)
- {
- fill_osr_press_settings(®_data, settings);
- }
- if (desired_settings & BME280_OSR_TEMP_SEL)
- {
- fill_osr_temp_settings(®_data, settings);
- }
-
- rslt = bme280_set_regs(®_addr, ®_data, 1, dev);
- }
- return rslt;
- }
- static int8_t set_filter_standby_settings(uint8_t desired_settings,
- const struct bme280_settings *settings,
- const struct bme280_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_addr = BME280_CONFIG_ADDR;
- uint8_t reg_data;
- rslt = bme280_get_regs(reg_addr, ®_data, 1, dev);
- if (rslt == BME280_OK)
- {
- if (desired_settings & BME280_FILTER_SEL)
- {
- fill_filter_settings(®_data, settings);
- }
- if (desired_settings & BME280_STANDBY_SEL)
- {
- fill_standby_settings(®_data, settings);
- }
-
- rslt = bme280_set_regs(®_addr, ®_data, 1, dev);
- }
- return rslt;
- }
- static void fill_filter_settings(uint8_t *reg_data, const struct bme280_settings *settings)
- {
- *reg_data = BME280_SET_BITS(*reg_data, BME280_FILTER, settings->filter);
- }
- static void fill_standby_settings(uint8_t *reg_data, const struct bme280_settings *settings)
- {
- *reg_data = BME280_SET_BITS(*reg_data, BME280_STANDBY, settings->standby_time);
- }
- static void fill_osr_press_settings(uint8_t *reg_data, const struct bme280_settings *settings)
- {
- *reg_data = BME280_SET_BITS(*reg_data, BME280_CTRL_PRESS, settings->osr_p);
- }
- static void fill_osr_temp_settings(uint8_t *reg_data, const struct bme280_settings *settings)
- {
- *reg_data = BME280_SET_BITS(*reg_data, BME280_CTRL_TEMP, settings->osr_t);
- }
- static void parse_device_settings(const uint8_t *reg_data, struct bme280_settings *settings)
- {
- settings->osr_h = BME280_GET_BITS_POS_0(reg_data[0], BME280_CTRL_HUM);
- settings->osr_p = BME280_GET_BITS(reg_data[2], BME280_CTRL_PRESS);
- settings->osr_t = BME280_GET_BITS(reg_data[2], BME280_CTRL_TEMP);
- settings->filter = BME280_GET_BITS(reg_data[3], BME280_FILTER);
- settings->standby_time = BME280_GET_BITS(reg_data[3], BME280_STANDBY);
- }
- static int8_t write_power_mode(uint8_t sensor_mode, const struct bme280_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_addr = BME280_PWR_CTRL_ADDR;
-
- uint8_t sensor_mode_reg_val;
-
- rslt = bme280_get_regs(reg_addr, &sensor_mode_reg_val, 1, dev);
-
- if (rslt == BME280_OK)
- {
- sensor_mode_reg_val = BME280_SET_BITS_POS_0(sensor_mode_reg_val, BME280_SENSOR_MODE, sensor_mode);
-
- rslt = bme280_set_regs(®_addr, &sensor_mode_reg_val, 1, dev);
- }
- return rslt;
- }
- static int8_t put_device_to_sleep(const struct bme280_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_data[4];
- struct bme280_settings settings;
- rslt = bme280_get_regs(BME280_CTRL_HUM_ADDR, reg_data, 4, dev);
- if (rslt == BME280_OK)
- {
- parse_device_settings(reg_data, &settings);
- rslt = bme280_soft_reset(dev);
- if (rslt == BME280_OK)
- {
- rslt = reload_device_settings(&settings, dev);
- }
- }
- return rslt;
- }
- static int8_t reload_device_settings(const struct bme280_settings *settings, const struct bme280_dev *dev)
- {
- int8_t rslt;
- rslt = set_osr_settings(BME280_ALL_SETTINGS_SEL, settings, dev);
- if (rslt == BME280_OK)
- {
- rslt = set_filter_standby_settings(BME280_ALL_SETTINGS_SEL, settings, dev);
- }
- return rslt;
- }
- #ifdef BME280_FLOAT_ENABLE
- static double compensate_temperature(const struct bme280_uncomp_data *uncomp_data, struct bme280_calib_data *calib_data)
- {
- double var1;
- double var2;
- double temperature;
- double temperature_min = -40;
- double temperature_max = 85;
- var1 = ((double)uncomp_data->temperature) / 16384.0 - ((double)calib_data->dig_t1) / 1024.0;
- var1 = var1 * ((double)calib_data->dig_t2);
- var2 = (((double)uncomp_data->temperature) / 131072.0 - ((double)calib_data->dig_t1) / 8192.0);
- var2 = (var2 * var2) * ((double)calib_data->dig_t3);
- calib_data->t_fine = (int32_t)(var1 + var2);
- temperature = (var1 + var2) / 5120.0;
- if (temperature < temperature_min)
- {
- temperature = temperature_min;
- }
- else if (temperature > temperature_max)
- {
- temperature = temperature_max;
- }
- return temperature;
- }
- static double compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
- const struct bme280_calib_data *calib_data)
- {
- double var1;
- double var2;
- double var3;
- double pressure;
- double pressure_min = 30000.0;
- double pressure_max = 110000.0;
- var1 = ((double)calib_data->t_fine / 2.0) - 64000.0;
- var2 = var1 * var1 * ((double)calib_data->dig_p6) / 32768.0;
- var2 = var2 + var1 * ((double)calib_data->dig_p5) * 2.0;
- var2 = (var2 / 4.0) + (((double)calib_data->dig_p4) * 65536.0);
- var3 = ((double)calib_data->dig_p3) * var1 * var1 / 524288.0;
- var1 = (var3 + ((double)calib_data->dig_p2) * var1) / 524288.0;
- var1 = (1.0 + var1 / 32768.0) * ((double)calib_data->dig_p1);
-
- if (var1 > (0.0))
- {
- pressure = 1048576.0 - (double) uncomp_data->pressure;
- pressure = (pressure - (var2 / 4096.0)) * 6250.0 / var1;
- var1 = ((double)calib_data->dig_p9) * pressure * pressure / 2147483648.0;
- var2 = pressure * ((double)calib_data->dig_p8) / 32768.0;
- pressure = pressure + (var1 + var2 + ((double)calib_data->dig_p7)) / 16.0;
- if (pressure < pressure_min)
- {
- pressure = pressure_min;
- }
- else if (pressure > pressure_max)
- {
- pressure = pressure_max;
- }
- }
- else
- {
- pressure = pressure_min;
- }
- return pressure;
- }
- static double compensate_humidity(const struct bme280_uncomp_data *uncomp_data,
- const struct bme280_calib_data *calib_data)
- {
- double humidity;
- double humidity_min = 0.0;
- double humidity_max = 100.0;
- double var1;
- double var2;
- double var3;
- double var4;
- double var5;
- double var6;
- var1 = ((double)calib_data->t_fine) - 76800.0;
- var2 = (((double)calib_data->dig_h4) * 64.0 + (((double)calib_data->dig_h5) / 16384.0) * var1);
- var3 = uncomp_data->humidity - var2;
- var4 = ((double)calib_data->dig_h2) / 65536.0;
- var5 = (1.0 + (((double)calib_data->dig_h3) / 67108864.0) * var1);
- var6 = 1.0 + (((double)calib_data->dig_h6) / 67108864.0) * var1 * var5;
- var6 = var3 * var4 * (var5 * var6);
- humidity = var6 * (1.0 - ((double)calib_data->dig_h1) * var6 / 524288.0);
- if (humidity > humidity_max)
- {
- humidity = humidity_max;
- }
- else if (humidity < humidity_min)
- {
- humidity = humidity_min;
- }
- return humidity;
- }
- #else
- static int32_t compensate_temperature(const struct bme280_uncomp_data *uncomp_data,
- struct bme280_calib_data *calib_data)
- {
- int32_t var1;
- int32_t var2;
- int32_t temperature;
- int32_t temperature_min = -4000;
- int32_t temperature_max = 8500;
- var1 = (int32_t)((uncomp_data->temperature / 8) - ((int32_t)calib_data->dig_t1 * 2));
- var1 = (var1 * ((int32_t)calib_data->dig_t2)) / 2048;
- var2 = (int32_t)((uncomp_data->temperature / 16) - ((int32_t)calib_data->dig_t1));
- var2 = (((var2 * var2) / 4096) * ((int32_t)calib_data->dig_t3)) / 16384;
- calib_data->t_fine = var1 + var2;
- temperature = (calib_data->t_fine * 5 + 128) / 256;
- if (temperature < temperature_min)
- {
- temperature = temperature_min;
- }
- else if (temperature > temperature_max)
- {
- temperature = temperature_max;
- }
- return temperature;
- }
- #ifndef BME280_32BIT_ENABLE
- static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
- const struct bme280_calib_data *calib_data)
- {
- int64_t var1;
- int64_t var2;
- int64_t var3;
- int64_t var4;
- uint32_t pressure;
- uint32_t pressure_min = 3000000;
- uint32_t pressure_max = 11000000;
- var1 = ((int64_t)calib_data->t_fine) - 128000;
- var2 = var1 * var1 * (int64_t)calib_data->dig_p6;
- var2 = var2 + ((var1 * (int64_t)calib_data->dig_p5) * 131072);
- var2 = var2 + (((int64_t)calib_data->dig_p4) * 34359738368);
- var1 = ((var1 * var1 * (int64_t)calib_data->dig_p3) / 256) + ((var1 * ((int64_t)calib_data->dig_p2) * 4096));
- var3 = ((int64_t)1) * 140737488355328;
- var1 = (var3 + var1) * ((int64_t)calib_data->dig_p1) / 8589934592;
-
- if (var1 != 0)
- {
- var4 = 1048576 - uncomp_data->pressure;
- var4 = (((var4 * INT64_C(2147483648)) - var2) * 3125) / var1;
- var1 = (((int64_t)calib_data->dig_p9) * (var4 / 8192) * (var4 / 8192)) / 33554432;
- var2 = (((int64_t)calib_data->dig_p8) * var4) / 524288;
- var4 = ((var4 + var1 + var2) / 256) + (((int64_t)calib_data->dig_p7) * 16);
- pressure = (uint32_t)(((var4 / 2) * 100) / 128);
- if (pressure < pressure_min)
- {
- pressure = pressure_min;
- }
- else if (pressure > pressure_max)
- {
- pressure = pressure_max;
- }
- }
- else
- {
- pressure = pressure_min;
- }
- return pressure;
- }
- #else
- static uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data,
- const struct bme280_calib_data *calib_data)
- {
- int32_t var1;
- int32_t var2;
- int32_t var3;
- int32_t var4;
- uint32_t var5;
- uint32_t pressure;
- uint32_t pressure_min = 30000;
- uint32_t pressure_max = 110000;
- var1 = (((int32_t)calib_data->t_fine) / 2) - (int32_t)64000;
- var2 = (((var1 / 4) * (var1 / 4)) / 2048) * ((int32_t)calib_data->dig_p6);
- var2 = var2 + ((var1 * ((int32_t)calib_data->dig_p5)) * 2);
- var2 = (var2 / 4) + (((int32_t)calib_data->dig_p4) * 65536);
- var3 = (calib_data->dig_p3 * (((var1 / 4) * (var1 / 4)) / 8192)) / 8;
- var4 = (((int32_t)calib_data->dig_p2) * var1) / 2;
- var1 = (var3 + var4) / 262144;
- var1 = (((32768 + var1)) * ((int32_t)calib_data->dig_p1)) / 32768;
-
- if (var1)
- {
- var5 = (uint32_t)((uint32_t)1048576) - uncomp_data->pressure;
- pressure = ((uint32_t)(var5 - (uint32_t)(var2 / 4096))) * 3125;
- if (pressure < 0x80000000)
- {
- pressure = (pressure << 1) / ((uint32_t)var1);
- }
- else
- {
- pressure = (pressure / (uint32_t)var1) * 2;
- }
- var1 = (((int32_t)calib_data->dig_p9) * ((int32_t)(((pressure / 8) * (pressure / 8)) / 8192))) / 4096;
- var2 = (((int32_t)(pressure / 4)) * ((int32_t)calib_data->dig_p8)) / 8192;
- pressure = (uint32_t)((int32_t)pressure + ((var1 + var2 + calib_data->dig_p7) / 16));
- if (pressure < pressure_min)
- {
- pressure = pressure_min;
- }
- else if (pressure > pressure_max)
- {
- pressure = pressure_max;
- }
- }
- else
- {
- pressure = pressure_min;
- }
- return pressure;
- }
- #endif
- static uint32_t compensate_humidity(const struct bme280_uncomp_data *uncomp_data,
- const struct bme280_calib_data *calib_data)
- {
- int32_t var1;
- int32_t var2;
- int32_t var3;
- int32_t var4;
- int32_t var5;
- uint32_t humidity;
- uint32_t humidity_max = 102400;
- var1 = calib_data->t_fine - ((int32_t)76800);
- var2 = (int32_t)(uncomp_data->humidity * 16384);
- var3 = (int32_t)(((int32_t)calib_data->dig_h4) * 1048576);
- var4 = ((int32_t)calib_data->dig_h5) * var1;
- var5 = (((var2 - var3) - var4) + (int32_t)16384) / 32768;
- var2 = (var1 * ((int32_t)calib_data->dig_h6)) / 1024;
- var3 = (var1 * ((int32_t)calib_data->dig_h3)) / 2048;
- var4 = ((var2 * (var3 + (int32_t)32768)) / 1024) + (int32_t)2097152;
- var2 = ((var4 * ((int32_t)calib_data->dig_h2)) + 8192) / 16384;
- var3 = var5 * var2;
- var4 = ((var3 / 32768) * (var3 / 32768)) / 128;
- var5 = var3 - ((var4 * ((int32_t)calib_data->dig_h1)) / 16);
- var5 = (var5 < 0 ? 0 : var5);
- var5 = (var5 > 419430400 ? 419430400 : var5);
- humidity = (uint32_t)(var5 / 4096);
- if (humidity > humidity_max)
- {
- humidity = humidity_max;
- }
- return humidity;
- }
- #endif
- static int8_t get_calib_data(struct bme280_dev *dev)
- {
- int8_t rslt;
- uint8_t reg_addr = BME280_TEMP_PRESS_CALIB_DATA_ADDR;
-
- uint8_t calib_data[BME280_TEMP_PRESS_CALIB_DATA_LEN] = { 0 };
-
- rslt = bme280_get_regs(reg_addr, calib_data, BME280_TEMP_PRESS_CALIB_DATA_LEN, dev);
- if (rslt == BME280_OK)
- {
-
- parse_temp_press_calib_data(calib_data, dev);
- reg_addr = BME280_HUMIDITY_CALIB_DATA_ADDR;
-
- rslt = bme280_get_regs(reg_addr, calib_data, BME280_HUMIDITY_CALIB_DATA_LEN, dev);
- if (rslt == BME280_OK)
- {
-
- parse_humidity_calib_data(calib_data, dev);
- }
- }
- return rslt;
- }
- static void interleave_reg_addr(const uint8_t *reg_addr, uint8_t *temp_buff, const uint8_t *reg_data, uint8_t len)
- {
- uint8_t index;
- for (index = 1; index < len; index++)
- {
- temp_buff[(index * 2) - 1] = reg_addr[index];
- temp_buff[index * 2] = reg_data[index];
- }
- }
- static void parse_temp_press_calib_data(const uint8_t *reg_data, struct bme280_dev *dev)
- {
- struct bme280_calib_data *calib_data = &dev->calib_data;
- calib_data->dig_t1 = BME280_CONCAT_BYTES(reg_data[1], reg_data[0]);
- calib_data->dig_t2 = (int16_t)BME280_CONCAT_BYTES(reg_data[3], reg_data[2]);
- calib_data->dig_t3 = (int16_t)BME280_CONCAT_BYTES(reg_data[5], reg_data[4]);
- calib_data->dig_p1 = BME280_CONCAT_BYTES(reg_data[7], reg_data[6]);
- calib_data->dig_p2 = (int16_t)BME280_CONCAT_BYTES(reg_data[9], reg_data[8]);
- calib_data->dig_p3 = (int16_t)BME280_CONCAT_BYTES(reg_data[11], reg_data[10]);
- calib_data->dig_p4 = (int16_t)BME280_CONCAT_BYTES(reg_data[13], reg_data[12]);
- calib_data->dig_p5 = (int16_t)BME280_CONCAT_BYTES(reg_data[15], reg_data[14]);
- calib_data->dig_p6 = (int16_t)BME280_CONCAT_BYTES(reg_data[17], reg_data[16]);
- calib_data->dig_p7 = (int16_t)BME280_CONCAT_BYTES(reg_data[19], reg_data[18]);
- calib_data->dig_p8 = (int16_t)BME280_CONCAT_BYTES(reg_data[21], reg_data[20]);
- calib_data->dig_p9 = (int16_t)BME280_CONCAT_BYTES(reg_data[23], reg_data[22]);
- calib_data->dig_h1 = reg_data[25];
- }
- static void parse_humidity_calib_data(const uint8_t *reg_data, struct bme280_dev *dev)
- {
- struct bme280_calib_data *calib_data = &dev->calib_data;
- int16_t dig_h4_lsb;
- int16_t dig_h4_msb;
- int16_t dig_h5_lsb;
- int16_t dig_h5_msb;
- calib_data->dig_h2 = (int16_t)BME280_CONCAT_BYTES(reg_data[1], reg_data[0]);
- calib_data->dig_h3 = reg_data[2];
- dig_h4_msb = (int16_t)(int8_t)reg_data[3] * 16;
- dig_h4_lsb = (int16_t)(reg_data[4] & 0x0F);
- calib_data->dig_h4 = dig_h4_msb | dig_h4_lsb;
- dig_h5_msb = (int16_t)(int8_t)reg_data[5] * 16;
- dig_h5_lsb = (int16_t)(reg_data[4] >> 4);
- calib_data->dig_h5 = dig_h5_msb | dig_h5_lsb;
- calib_data->dig_h6 = (int8_t)reg_data[6];
- }
- static uint8_t are_settings_changed(uint8_t sub_settings, uint8_t desired_settings)
- {
- uint8_t settings_changed = FALSE;
- if (sub_settings & desired_settings)
- {
-
- settings_changed = TRUE;
- }
- else
- {
-
- settings_changed = FALSE;
- }
- return settings_changed;
- }
- static int8_t null_ptr_check(const struct bme280_dev *dev)
- {
- int8_t rslt;
- if ((dev == NULL) || (dev->read == NULL) || (dev->write == NULL) || (dev->delay_ms == NULL))
- {
-
- rslt = BME280_E_NULL_PTR;
- }
- else
- {
-
- rslt = BME280_OK;
- }
- return rslt;
- }
|