ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_interface_i2c_bme280.c
Go to the documentation of this file.
2 #if RI_BME280_ENABLED && RI_BME280_I2C_ENABLED
16 #include <stdint.h>
17 #include <string.h>
18 
19 #include "ruuvi_driver_error.h"
20 #include "ruuvi_interface_gpio.h"
21 #include "ruuvi_interface_i2c.h"
22 #include "ruuvi_interface_yield.h"
23 
24 /*
25  * Data on the bus should be like
26  * |------------+---------------------|
27  * | I2C action | Data |
28  * |------------+---------------------|
29  * | Start | - |
30  * | Write | (reg_addr) |
31  * | Write | (reg_data[0]) |
32  * | Write | (....) |
33  * | Write | (reg_data[len - 1]) |
34  * | Stop | - |
35  * |------------+---------------------|
36  */
37 int8_t ri_i2c_bme280_write (uint8_t dev_id, uint8_t reg_addr,
38  uint8_t * reg_data, uint16_t len)
39 {
40  rd_status_t err_code = RD_SUCCESS;
41 
42  // Support only 1-byte writes
43  if (1 > len || 2 < len) { return -1; }
44 
45  uint8_t wbuf[2] = {0};
46  wbuf[0] = reg_addr;
47  wbuf[1] = reg_data[0];
48  err_code |= ri_i2c_write_blocking (dev_id, wbuf, 2, true);
49  return (RD_SUCCESS == err_code) ? 0 : -1;
50 }
51 
52 /*
53  * Data on the bus should be like
54  * |------------+---------------------|
55  * | I2C action | Data |
56  * |------------+---------------------|
57  * | Start | - |
58  * | Write | (reg_addr) |
59  * | Stop | - |
60  * | Start | - |
61  * | Read | (reg_data[0]) |
62  * | Read | (....) |
63  * | Read | (reg_data[len - 1]) |
64  * | Stop | - |
65  * |------------+---------------------|
66  */
67 
68 int8_t ri_i2c_bme280_read (uint8_t dev_id, uint8_t reg_addr,
69  uint8_t * reg_data, uint16_t len)
70 {
71  rd_status_t err_code = RD_SUCCESS;
72  err_code |= ri_i2c_write_blocking (dev_id, &reg_addr, 1, true);
73  err_code |= ri_i2c_read_blocking (dev_id, reg_data, len);
74  return (RD_SUCCESS == err_code) ? 0 : -1;
75 }
76 #endif
uint32_t rd_status_t
bitfield for representing errors
#define RD_SUCCESS
Internal Error.
int8_t ri_i2c_bme280_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *p_reg_data, uint16_t len)
I2C Read function for BME280.
int8_t ri_i2c_bme280_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *p_reg_data, uint16_t len)
I2C write function for BME280.
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
Interface for I2C operations.
rd_status_t ri_i2c_write_blocking(const uint8_t address, uint8_t *const p_tx, const size_t tx_len, const bool stop)
I2C read function.
rd_status_t ri_i2c_read_blocking(const uint8_t address, uint8_t *const p_rx, const size_t rx_len)
I2C read function.