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 */
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
5153static uint8_t CMD_CONVERT_D1_ADDR [5 ] = {
5254 0x40 , // OSR=256
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 ;
9699uint32_t _raw_temperature , _raw_pressure ;
97100static ms5611_prom_t _prom ;
98101static uint8_t _ms5611_state ;
@@ -101,38 +104,47 @@ static uint8_t _updated = 0;
101104
102105static 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
113119static 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
127136static 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
301313static 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 ;
0 commit comments