Skip to content

Commit c827da8

Browse files
committed
2 parents 5f6e8c2 + 7b80af0 commit c827da8

File tree

6 files changed

+140
-90
lines changed

6 files changed

+140
-90
lines changed

src/driver/barometer/bmp581.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#include "hal/barometer/barometer.h"
2020
#include "hal/spi/spi.h"
2121

22-
#define DRV_DBG(...) printf(__VA_ARGS__)
22+
#define DRV_DBG(...)
23+
// #define DRV_DBG(...) printf(__VA_ARGS__)
2324

2425
#define BMP581_ID 0x50
2526

@@ -166,7 +167,8 @@ static struct baro_ops _baro_ops = {
166167
rt_err_t drv_bmp581_init(const char* spi_device_name, const char* baro_device_name)
167168
{
168169
static struct baro_device baro_dev = {
169-
.ops = &_baro_ops
170+
.ops = &_baro_ops,
171+
.bus_type = BARO_SPI_BUS_TYPE
170172
};
171173

172174
baro_spi_dev = rt_device_find(spi_device_name);

src/driver/barometer/ms5611.c

Lines changed: 79 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,18 @@
1818

1919
#include "driver/barometer/ms5611.h"
2020
#include "hal/barometer/barometer.h"
21+
#include "hal/i2c/i2c.h"
2122
#include "hal/spi/spi.h"
2223
#include "module/math/conversion.h"
2324
#include "module/workqueue/workqueue_manager.h"
2425

25-
#define DRV_DBG(...) printf(__VA_ARGS__)
26-
#define POW2(_x) ((_x) * (_x))
26+
#define DRV_DBG(...)
27+
// #define DRV_DBG(...) printf(__VA_ARGS__)
28+
#define POW2(_x) ((_x) * (_x))
2729

2830
/* SPI protocol address bits */
29-
#define DIR_READ (1 << 7)
30-
#define DIR_WRITE (0 << 7)
31+
#define DIR_READ (1 << 7)
32+
#define DIR_WRITE (0 << 7)
3133

3234
#define ADDR_RESET_CMD 0x1E /* write to this address to reset chip */
3335
#define ADDR_ADC 0x00 /* address of 3 bytes / 32bit pressure data */
@@ -40,13 +42,13 @@
4042
#define ADDR_PROM_C6 0xAC
4143
#define ADDR_PROM_CRC 0xAE
4244

43-
#define BARO_OSR_256 0
44-
#define BARO_OSR_512 1
45-
#define BARO_OSR_1024 2
46-
#define BARO_OSR_2048 3
47-
#define BARO_OSR_4096 4
45+
#define BARO_OSR_256 0
46+
#define BARO_OSR_512 1
47+
#define BARO_OSR_1024 2
48+
#define BARO_OSR_2048 3
49+
#define BARO_OSR_4096 4
4850

49-
#define DEFAULT_OSR BARO_OSR_2048
51+
#define DEFAULT_OSR BARO_OSR_2048
5052

5153
static uint8_t CMD_CONVERT_D1_ADDR[5] = {
5254
0x40, // OSR=256
@@ -92,7 +94,8 @@ enum {
9294
S_COLLECT_REPORT
9395
};
9496

95-
static rt_device_t spi_device;
97+
static rt_device_t dev;
98+
static int use_spi = 0;
9699
uint32_t _raw_temperature, _raw_pressure;
97100
static ms5611_prom_t _prom;
98101
static uint8_t _ms5611_state;
@@ -101,38 +104,47 @@ static uint8_t _updated = 0;
101104

102105
static rt_err_t write_cmd(rt_uint8_t cmd)
103106
{
104-
rt_uint8_t send_buffer;
105-
rt_size_t w_byte;
106-
107-
send_buffer = DIR_WRITE | cmd;
108-
w_byte = rt_device_write(spi_device, 0, &send_buffer, sizeof(send_buffer));
109-
110-
return w_byte == sizeof(send_buffer) ? RT_EOK : RT_ERROR;
107+
if (use_spi) {
108+
rt_uint8_t send_buffer = DIR_WRITE | cmd;
109+
rt_size_t w_byte = rt_device_write(dev, 0, &send_buffer, sizeof(send_buffer));
110+
return w_byte == sizeof(send_buffer) ? RT_EOK : RT_ERROR;
111+
} else {
112+
/* I2C: send single command byte */
113+
struct rt_i2c_device* i2c_dev = (struct rt_i2c_device*)dev;
114+
rt_size_t sent = rt_i2c_master_send(i2c_dev->bus, i2c_dev->slave_addr, i2c_dev->flags, &cmd, 1);
115+
return sent == 1 ? RT_EOK : RT_ERROR;
116+
}
111117
}
112118

113119
static rt_err_t read_adc(uint32_t* buff)
114120
{
115-
rt_uint8_t send_val;
116-
rt_err_t res;
121+
rt_uint8_t send_val = ADDR_ADC;
122+
rt_err_t res = RT_ERROR;
117123

118-
send_val = ADDR_ADC;
124+
if (use_spi) {
125+
res = rt_spi_send_then_recv((struct rt_spi_device*)dev, (void*)&send_val, 1, (void*)buff, 3);
126+
} else {
127+
res = i2c_read_regs(dev, ADDR_ADC, (uint8_t*)buff, 3);
128+
}
119129

120-
res = rt_spi_send_then_recv((struct rt_spi_device*)spi_device, (void*)&send_val, 1, (void*)buff, 3);
121-
//big-endian to little-endian
130+
/* big-endian to little-endian */
122131
Msb2Lsb((uint8_t*)buff, 3);
123132

124133
return res;
125134
}
126135

127136
static rt_err_t read_prom_reg(rt_uint8_t cmd, uint16_t* buff)
128137
{
129-
rt_uint8_t send_val;
130-
rt_err_t res;
138+
rt_err_t res = RT_ERROR;
131139

132-
send_val = DIR_READ | cmd;
140+
if (use_spi) {
141+
rt_uint8_t send_val = DIR_READ | cmd;
142+
res = rt_spi_send_then_recv((struct rt_spi_device*)dev, (void*)&send_val, 1, (void*)buff, 2);
143+
} else {
144+
res = i2c_read_regs(dev, cmd, (uint8_t*)buff, 2);
145+
}
133146

134-
res = rt_spi_send_then_recv((struct rt_spi_device*)spi_device, (void*)&send_val, 1, (void*)buff, 2);
135-
//big-endian to little-endian
147+
/* big-endian to little-endian */
136148
Msb2Lsb((uint8_t*)buff, 2);
137149

138150
return res;
@@ -238,15 +250,15 @@ static rt_err_t collect_data(baro_report_t* report)
238250
double p = _pressure / 1000.0;
239251

240252
/*
241-
* Solve:
242-
*
243-
* / -(aR / g) \
244-
* | (p / p1) . T1 | - T1
245-
* \ /
246-
* h = ------------------------------- + h1
247-
* a
248-
*/
249-
//report->altitude = (((exp((-(a * R) / g) * log((p / p1)))) * T1) - T1) / a;
253+
* Solve:
254+
*
255+
* / -(aR / g) \
256+
* | (p / p1) . T1 | - T1
257+
* \ /
258+
* h = ------------------------------- + h1
259+
* a
260+
*/
261+
// report->altitude = (((exp((-(a * R) / g) * log((p / p1)))) * T1) - T1) / a;
250262
report->altitude_m = (((pow((p / p1), (-(a * R) / g))) * T1) - T1) / a;
251263

252264
report->timestamp_ms = systime_now_ms();
@@ -300,8 +312,6 @@ static void ms5611_measure(void* parameter)
300312

301313
static rt_err_t lowlevel_init(void)
302314
{
303-
RT_TRY(rt_device_open(spi_device, RT_DEVICE_OFLAG_RDWR));
304-
305315
/* reset first */
306316
RT_TRY(write_cmd(ADDR_RESET_CMD));
307317

@@ -357,28 +367,49 @@ static struct WorkItem ms5611_work = {
357367
.run = ms5611_measure
358368
};
359369

360-
rt_err_t drv_ms5611_init(const char* spi_device_name, const char* baro_device_name)
370+
rt_err_t drv_ms5611_init(const char* device_name, const char* baro_device_name)
361371
{
362372
static struct baro_device baro_device = {
363-
.ops = &_baro_ops
373+
.ops = &_baro_ops,
374+
.bus_type = BARO_SPI_BUS_TYPE
364375
};
365376

366-
spi_device = rt_device_find(spi_device_name);
367-
RT_ASSERT(spi_device != NULL);
377+
dev = rt_device_find(device_name);
378+
RT_ASSERT(dev != NULL);
379+
380+
if (dev->type == RT_Device_Class_SPIDevice) {
381+
use_spi = 1;
368382

369-
/* config spi */
370-
{
383+
/* config spi */
371384
struct rt_spi_configuration cfg;
372385
cfg.data_width = 8;
373386
cfg.mode = RT_SPI_MODE_3 | RT_SPI_MSB; /* SPI Compatible Modes 3 */
374387
cfg.max_hz = 7000000;
375388

376-
struct rt_spi_device* spi_device_t = (struct rt_spi_device*)spi_device;
389+
struct rt_spi_device* spi_device_t = (struct rt_spi_device*)dev;
377390

378391
spi_device_t->config.data_width = cfg.data_width;
379392
spi_device_t->config.mode = cfg.mode & RT_SPI_MODE_MASK;
380393
spi_device_t->config.max_hz = cfg.max_hz;
381394
RT_TRY(rt_spi_configure(spi_device_t, &cfg));
395+
RT_TRY(rt_device_open(dev, RT_DEVICE_OFLAG_RDWR));
396+
} else if (dev->type == RT_Device_Class_I2CDevice) {
397+
use_spi = 0;
398+
{
399+
struct rt_i2c_device* i2c_dev = (struct rt_i2c_device*)dev;
400+
if (i2c_dev->bus == RT_NULL) {
401+
DRV_DBG("ms5611: i2c device '%s' has no bus attached\n", device_name);
402+
return RT_ERROR;
403+
}
404+
if (i2c_dev->slave_addr == 0) {
405+
DRV_DBG("ms5611: i2c device '%s' has invalid slave addr 0\n", device_name);
406+
return RT_ERROR;
407+
}
408+
}
409+
/* open i2c device for read/write */
410+
RT_TRY(rt_device_open(dev, RT_DEVICE_OFLAG_RDWR));
411+
} else {
412+
return RT_ERROR;
382413
}
383414

384415
/* driver internal init */
@@ -391,6 +422,8 @@ rt_err_t drv_ms5611_init(const char* spi_device_name, const char* baro_device_na
391422
/* schedule the work */
392423
FMT_CHECK(workqueue_schedule_work(hp_wq, &ms5611_work));
393424

425+
/* indicate underlying bus type to hal layer so device type is correct */
426+
baro_device.bus_type = use_spi ? BARO_SPI_BUS_TYPE : BARO_I2C_BUS_TYPE;
394427
RT_TRY(hal_baro_register(&baro_device, baro_device_name, RT_DEVICE_FLAG_RDWR, RT_NULL));
395428

396429
return RT_EOK;

src/driver/barometer/ms5611.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3838
extern "C" {
3939
#endif
4040

41-
rt_err_t drv_ms5611_init(const char* spi_device_name, const char* baro_device_name);
41+
rt_err_t drv_ms5611_init(const char* device_name, const char* baro_device_name);
4242

4343
#ifdef __cplusplus
4444
}

src/driver/barometer/spl06.c

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
#include "hal/barometer/barometer.h"
2121
#include "hal/spi/spi.h"
2222

23-
#define DRV_DBG(...) printf(__VA_ARGS__)
23+
#define DRV_DBG(...)
24+
// #define DRV_DBG(...) printf(__VA_ARGS__)
2425

2526
#ifdef BIT
2627
#undef BIT
2728
#endif
28-
#define BIT(_idx) (1 << _idx)
29+
#define BIT(_idx) (1 << _idx)
2930

3031
#define SPL06_PRS_B2 0x00
3132
#define SPL06_PRS_B1 0x01
@@ -43,41 +44,41 @@
4344
#define SPL06_ID 0x0D
4445
#define SPL06_COEF 0x10
4546

46-
#define PM_RATE_1 (0)
47-
#define PM_RATE_2 (BIT(4))
48-
#define PM_RATE_4 (BIT(5))
49-
#define PM_RATE_8 (BIT(4) | BIT(5))
50-
#define PM_RATE_16 (BIT(6))
51-
#define PM_RATE_32 (BIT(4) | BIT(6))
52-
#define PM_RATE_64 (BIT(5) | BIT(6))
53-
#define PM_RATE_128 (BIT(4) | BIT(5) | BIT(6))
54-
55-
#define PM_PRC_1 (0)
56-
#define PM_PRC_2 (BIT(0))
57-
#define PM_PRC_4 (BIT(1))
58-
#define PM_PRC_8 (BIT(0) | BIT(1))
59-
#define PM_PRC_16 (BIT(2))
60-
#define PM_PRC_32 (BIT(0) | BIT(2))
61-
#define PM_PRC_64 (BIT(1) | BIT(2))
62-
#define PM_PRC_128 (BIT(0) | BIT(1) | BIT(2))
63-
64-
#define TMP_RATE_1 (0)
65-
#define TMP_RATE_2 (BIT(4))
66-
#define TMP_RATE_4 (BIT(5))
67-
#define TMP_RATE_8 (BIT(4) | BIT(5))
68-
#define TMP_RATE_16 (BIT(6))
69-
#define TMP_RATE_32 (BIT(4) | BIT(6))
70-
#define TMP_RATE_64 (BIT(5) | BIT(6))
71-
#define TMP_RATE_128 (BIT(4) | BIT(5) | BIT(6))
72-
73-
#define TMP_PRC_1 (0)
74-
#define TMP_PRC_2 (BIT(0))
75-
#define TMP_PRC_4 (BIT(1))
76-
#define TMP_PRC_8 (BIT(0) | BIT(1))
77-
#define TMP_PRC_16 (BIT(2))
78-
#define TMP_PRC_32 (BIT(0) | BIT(2))
79-
#define TMP_PRC_64 (BIT(1) | BIT(2))
80-
#define TMP_PRC_128 (BIT(0) | BIT(1) | BIT(2))
47+
#define PM_RATE_1 (0)
48+
#define PM_RATE_2 (BIT(4))
49+
#define PM_RATE_4 (BIT(5))
50+
#define PM_RATE_8 (BIT(4) | BIT(5))
51+
#define PM_RATE_16 (BIT(6))
52+
#define PM_RATE_32 (BIT(4) | BIT(6))
53+
#define PM_RATE_64 (BIT(5) | BIT(6))
54+
#define PM_RATE_128 (BIT(4) | BIT(5) | BIT(6))
55+
56+
#define PM_PRC_1 (0)
57+
#define PM_PRC_2 (BIT(0))
58+
#define PM_PRC_4 (BIT(1))
59+
#define PM_PRC_8 (BIT(0) | BIT(1))
60+
#define PM_PRC_16 (BIT(2))
61+
#define PM_PRC_32 (BIT(0) | BIT(2))
62+
#define PM_PRC_64 (BIT(1) | BIT(2))
63+
#define PM_PRC_128 (BIT(0) | BIT(1) | BIT(2))
64+
65+
#define TMP_RATE_1 (0)
66+
#define TMP_RATE_2 (BIT(4))
67+
#define TMP_RATE_4 (BIT(5))
68+
#define TMP_RATE_8 (BIT(4) | BIT(5))
69+
#define TMP_RATE_16 (BIT(6))
70+
#define TMP_RATE_32 (BIT(4) | BIT(6))
71+
#define TMP_RATE_64 (BIT(5) | BIT(6))
72+
#define TMP_RATE_128 (BIT(4) | BIT(5) | BIT(6))
73+
74+
#define TMP_PRC_1 (0)
75+
#define TMP_PRC_2 (BIT(0))
76+
#define TMP_PRC_4 (BIT(1))
77+
#define TMP_PRC_8 (BIT(0) | BIT(1))
78+
#define TMP_PRC_16 (BIT(2))
79+
#define TMP_PRC_32 (BIT(0) | BIT(2))
80+
#define TMP_PRC_64 (BIT(1) | BIT(2))
81+
#define TMP_PRC_128 (BIT(0) | BIT(1) | BIT(2))
8182

8283
typedef struct {
8384
uint8_t setbits;
@@ -302,7 +303,8 @@ static struct baro_ops _baro_ops = {
302303
rt_err_t drv_spl06_init(const char* spi_device_name, const char* baro_device_name)
303304
{
304305
static struct baro_device baro_dev = {
305-
.ops = &_baro_ops
306+
.ops = &_baro_ops,
307+
.bus_type = BARO_SPI_BUS_TYPE
306308
};
307309

308310
baro_spi_dev = rt_device_find(spi_device_name);

src/hal/barometer/barometer.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static rt_err_t hal_baro_control(struct rt_device* dev,
5656

5757
/**
5858
* @brief register a barometer device
59-
*
59+
*
6060
* @param baro barometer device
6161
* @param name device name
6262
* @param flag device flag
@@ -72,7 +72,15 @@ rt_err_t hal_baro_register(baro_dev_t baro, const char* name, rt_uint32_t flag,
7272

7373
device = &(baro->parent);
7474

75-
device->type = RT_Device_Class_SPIDevice;
75+
/* Prefer explicit bus_type in baro struct (set by driver). Fall back
76+
* to legacy `data` string check for backward compatibility. */
77+
if (baro->bus_type == BARO_SPI_BUS_TYPE) {
78+
device->type = RT_Device_Class_SPIDevice;
79+
} else if (baro->bus_type == BARO_I2C_BUS_TYPE) {
80+
device->type = RT_Device_Class_I2CDevice;
81+
} else {
82+
return RT_EINVAL;
83+
}
7684
device->ref_count = 0;
7785
device->rx_indicate = RT_NULL;
7886
device->tx_complete = RT_NULL;

src/hal/barometer/barometer.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C" {
2424
#endif
2525

2626
/* baro read pos */
27-
#define BARO_RD_REPORT 1
27+
#define BARO_RD_REPORT 1
2828

2929
/* baro command */
3030
#define BARO_CMD_CHECK_READY 0x20
@@ -43,9 +43,14 @@ struct baro_configure {
4343
struct baro_device {
4444
struct rt_device parent;
4545
const struct baro_ops* ops;
46+
rt_uint8_t bus_type;
4647
};
4748
typedef struct baro_device* baro_dev_t;
4849

50+
/* baro device bus type */
51+
#define BARO_SPI_BUS_TYPE 1
52+
#define BARO_I2C_BUS_TYPE 2
53+
4954
/* baro driver opeations */
5055
struct baro_ops {
5156
/**

0 commit comments

Comments
 (0)