1 /*
2 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3 *
4 *	Edit wanjiang-yan 2018-6-24
5 */
6 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "aos/kernel.h"
11 #include "sensor_drv_api.h"
12 #include "sensor_hal.h"
13 
14 //1.8V offset=0,2.8v offset= -0.6
15 //#define VALUE_OFFSET						//modify by laoyan
16 #ifdef VALUE_OFFSET
17 #define PRESSURE_VALUES_OFFSET		0.6		//modify by laoyan
18 #endif
19 
20 
21 //For Get_Attrib Function
22 #define PRESS_SENSOR_POWER_VAL           (650)
23 #define PRESS_SENSOR_LOW_POWER_VAL       (30)
24 #define PRESS_SENSOR_ACTIVE_TIME         (1300)
25 #define PRESS_SENSOR_DATA_TIME           (147000)
26 #define PRESS_SENSOR_RANGE_MIN           (0)
27 #define PRESS_SENSOR_RANGE_MAX           (1)
28 #define PRESS_SENSOR_ODR_SIZE            (7)
29 #define PRESS_SENSOR_RES_DECIMAL         (11)
30 #define PRESS_SENSOR_RES_INTEGER         (11)
31 #define PRESS_SENSOR_DATA_BYTE           (3)
32 
33 #define PRESS_SENSOR_INIT_ODR            (6)
34 
35 
36 /******************************************************************************
37    define name for IC
38  *****************************************************************************/
39 /* Address */
40 
41 
42 /* Compear ID */
43 #define BM1383A_ID1_VALUE               (0xE0)
44 #define BM1383A_ID2_VALUE               (0x32)
45 
46 /* Power Down */
47 #define BM1383A_POWER_OFF              (0x00)
48 #define BM1383A_POWER_ON               (0x01)
49 
50 /* Reset */
51 #define BM1383A_SW_RESET_VALUE         (1 << 7)
52 #define BM1383A_INT_RESET_VALUE        (1 << 6)
53 
54 /* Sleep value */
55 #define BM1383A_SLEEP_RELEASE          (0x00)
56 #define BM1383A_SLEEP_ACTIVE           (0x01)
57 
58 
59 /* AVE_NUM */
60 
61 #define BM1383A_AVE_NUM_VALUE_1_TIMES   (0x0 << 5) /*000: single */
62 #define BM1383A_AVE_NUM_VALUE_30_TIMES   (0x3 << 5) /*001: average of 30 times */
63 #define BM1383A_AVE_NUM_VALUE_60_TIMES  (0x4 << 5) /*001: average of 60 times*/    //50ms
64 #define BM1383A_AVE_NUM_VALUE_120_TIMES  (0x5 << 5) /*101: average of 120 times*/    //100ms
65 #define BM1383A_AVE_NUM_VALUE_240_TIMES (0x6 << 5) /*110: average of 240 times*/   //200ms
66 
67 
68 
69 #define BM1383A_MODE_VALUE_STANDBY      (0x0)    /* 00 stand by        */
70 #define BM1383A_MODE_VALUE_ONE_SHOT     (0x1)    /* 01 One shot        */
71 #define BM1383A_MODE_VALUE_CONTINOUS    (0x2)    /* 10 Continuous  */
72 #define BM1383A_MODE_VALUE_PROHIBITION  (0x3)    /* 11 Prohibition */
73 
74 #define BM1383A_INT_ENABLE_DREG          (0x1>>4)  //DRDY pin Enable
75 #define BM1383A_INT_DISABLE_DREG         (0x0>>4)  //DRDY pin Disable
76 
77 #define BM1383A_MODE_RECV				  (0x2>>2)  //Refer to Operation mode transition
78 
79 
80 
81 #define BM1383A_INT_CLS_VALUE             (0x1 << 6) /* Clear interrupt */
82 
83 
84 
85 
86 /* ROHM SENSOR REGISTER MAP */
87 #define BM1383A_BIT(x)               ((uint8_t)x)
88 #define BM1383A_REG_ID1                     (0x0F)
89 #define BM1383A_REG_ID2                     (0x10)
90 
91 #define BM1383A_REG_POWER_DOWN             (0x12)
92 #define BM1383A_REG_RESET_CONTROL          (0x13)
93 #define BM1383A_REG_MODE_CONTROL           (0x14)
94 #define BM1383A_REG_STATUS                 (0x19)
95 
96 #define BM1383A_REG_PRESSURE_MSB_H           (0x1A)
97 #define BM1383A_REG_PRESSURE_LSB_M           (0x1B)
98 #define BM1383A_REG_PRESSURE_LSB_L           (0x1C)
99 #define BM1383A_REG_TEMPERATURE_MSB           (0x1D)
100 #define BM1383A_REG_TEMPERATURE_LSB           (0x1E)
101 
102 
103 #define BM1383A_I2C_ADDR            (0x5D)
104 #define BM1383A_I2C_ADDR_TRANS(n)    ((n)<<1)
105 #define BM1383A_I2C_ADDR2             BM1383A_I2C_ADDR_TRANS(BM1383A_I2C_ADDR)
106 #define BM1383A_ODR_MASK             (uint8_t)0x1c
107 
108 typedef enum {
109   BM1383A_ODR_ONE_SHOT        = (uint8_t)0x01,         /*!< Output Data Rate: one shot */
110   BM1383A_ODR_1HZ             = (uint8_t)0xc2,         /*!< Output Data Rate: 1Hz */
111   BM1383A_ODR_5HZ            = (uint8_t)0xa2,         /*!< Output Data Rate: 4Hz */
112   BM1383A_ODR_10HZ            = (uint8_t)0x82,         /*!< Output Data Rate: 8Hz */
113   BM1383A_ODR_20HZ            = (uint8_t)0x62,         /*!< Output Data Rate: 15Hz */
114 } bm1383a_odr_e;
115 
116 
117 
118 typedef enum {
119   BM1383A_BDU_CONTINUOUS_UPDATE     =  (uint8_t)0x00,  /*!< Data updated continuously */
120   BM1383A_BDU_NO_UPDATE             =  (uint8_t)0x02   /*!< Data updated after a read operation */
121 } bm1383a_bdu_e;
122 
123 i2c_dev_t bm1383a_ctx = {
124     .port = 3,
125     .config.address_width = 8,
126     .config.freq = 400000,
127     .config.dev_addr = BM1383A_I2C_ADDR2,
128 };
129 
drv_baro_rohm_bm1383a_hz2odr(int hz)130 static bm1383a_odr_e drv_baro_rohm_bm1383a_hz2odr(int hz)
131 {
132     if(hz > 10)
133         return BM1383A_ODR_20HZ;
134     else if(hz > 5)
135         return BM1383A_ODR_10HZ;
136     else if(hz > 1)
137         return BM1383A_ODR_5HZ;
138     else
139         return BM1383A_ODR_1HZ;
140 }
141 
142 //��֤оƬ�������Ĵ����Ƿ���Ĭ�ϵļĴ�������
drv_baro_rohm_bm1383a_validate_id(i2c_dev_t * drv)143 static int drv_baro_rohm_bm1383a_validate_id(i2c_dev_t* drv)
144 {
145     uint8_t value = 0x00;
146     int ret = 0;
147 
148     if(drv == NULL){
149         return -1;
150     }
151 
152     ret = sensor_i2c_read(drv, BM1383A_REG_ID1, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
153     if(unlikely(ret)){
154         return ret;
155     }
156 
157     if (BM1383A_ID1_VALUE != value){
158         return -1;
159     }
160 
161 	ret = sensor_i2c_read(drv, BM1383A_REG_ID2, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
162 	if(unlikely(ret)){
163 		return ret;
164 	}
165 
166 	if (BM1383A_ID2_VALUE != value){
167 		return -1;
168 	}
169 
170 
171     return 0;
172 }
173 
drv_baro_rohm_bm1383a_set_power_mode(i2c_dev_t * drv,dev_power_mode_e mode)174 static int drv_baro_rohm_bm1383a_set_power_mode(i2c_dev_t* drv, dev_power_mode_e mode)
175 {
176     uint8_t value = 0x00;
177     int ret = 0;
178 
179 	//ret = sensor_i2c_read(drv, BM1383A_RES_CONF_REG, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
180     //if(unlikely(ret)){
181     //    return ret;
182     //}
183 
184     switch(mode){
185         case DEV_POWER_ON:{
186             value = BM1383A_POWER_ON;
187             ret = sensor_i2c_write(drv, BM1383A_REG_POWER_DOWN, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
188             if(unlikely(ret)){
189                 return ret;
190             }
191 			//It must wait 2ms (i2c)
192 			aos_msleep(2);
193 			value = BM1383A_SLEEP_ACTIVE;
194 			ret = sensor_i2c_write(drv, BM1383A_REG_RESET_CONTROL, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
195 			if(unlikely(ret)){
196 				return ret;
197 			}
198 			value = BM1383A_AVE_NUM_VALUE_60_TIMES|BM1383A_MODE_VALUE_CONTINOUS;
199 			ret = sensor_i2c_write(drv, BM1383A_REG_MODE_CONTROL, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
200 			if(unlikely(ret)){
201 			    return ret;
202 			}
203 			aos_msleep(100);
204 
205 
206         }break;
207 
208         case DEV_POWER_OFF:{
209 			//sleep status
210 			value = BM1383A_SLEEP_RELEASE;
211             ret = sensor_i2c_write(drv, BM1383A_REG_RESET_CONTROL, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
212             if(unlikely(ret)){
213                 return ret;
214             }
215 			//power off
216             value = BM1383A_POWER_OFF;
217             ret = sensor_i2c_write(drv, BM1383A_REG_POWER_DOWN, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
218             if(unlikely(ret)){
219                 return ret;
220             }
221         }break;
222 
223        default:break;
224     }
225     return 0;
226 }
227 
drv_baro_rohm_bm1383a_set_odr(i2c_dev_t * drv,bm1383a_odr_e odr)228 static int drv_baro_rohm_bm1383a_set_odr(i2c_dev_t* drv, bm1383a_odr_e odr)
229 {
230     uint8_t value = 0x00;
231     int ret = 0;
232 
233     ret = sensor_i2c_read(drv, BM1383A_REG_MODE_CONTROL, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
234     if(unlikely(ret)){
235         return ret;
236     }
237 	value &= ~BM1383A_ODR_MASK;
238     value |= (uint8_t)odr;
239 
240     ret = sensor_i2c_write(drv, BM1383A_REG_MODE_CONTROL, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
241     if(unlikely(ret)){
242         return ret;
243     }
244     return 0;
245 }
246 
247 //
drv_baro_rohm_bm1383a_set_bdu(i2c_dev_t * drv,bm1383a_bdu_e bdu)248 static int drv_baro_rohm_bm1383a_set_bdu(i2c_dev_t* drv, bm1383a_bdu_e bdu)
249 {
250     /*
251     ret = sensor_i2c_read(drv, BM1383A_REG_MODE_CONTROL, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
252     if(unlikely(ret)){
253         return ret;
254     }
255     value &= ~BM1383A_BDU_MASK;
256     value |= (uint8_t)bdu;
257 
258     ret = sensor_i2c_write(drv, BM1383A_REG_MODE_CONTROL, &value, I2C_DATA_LEN, I2C_OP_RETRIES);
259     if(unlikely(ret)){
260         return ret;
261     }
262     */
263     return 0;
264 }
265 
drv_baro_rohm_bm1383a_set_default_config(i2c_dev_t * drv)266 static int drv_baro_rohm_bm1383a_set_default_config(i2c_dev_t* drv)
267 {
268     int ret = 0;
269     ret = drv_baro_rohm_bm1383a_set_power_mode(drv, DEV_POWER_OFF);
270     if(unlikely(ret)){
271         return ret;
272     }
273     ret = drv_baro_rohm_bm1383a_set_odr(drv, BM1383A_ODR_20HZ);
274     if(unlikely(ret)){
275         return ret;
276     }
277     ret = drv_baro_rohm_bm1383a_set_bdu(drv, BM1383A_BDU_NO_UPDATE);
278     if(unlikely(ret)){
279         return ret;
280     }
281     /* you also can set the low-pass filter and cut off config here */
282     return 0;
283 }
284 
285 
drv_baro_rohm_bm1383a_irq_handle(void)286 static void drv_baro_rohm_bm1383a_irq_handle(void)
287 {
288     /* no handle so far */
289 }
290 
drv_baro_rohm_bm1383a_open(void)291 static int drv_baro_rohm_bm1383a_open(void)
292 {
293     int ret = 0;
294     ret  = drv_baro_rohm_bm1383a_set_power_mode(&bm1383a_ctx, DEV_POWER_ON);
295     if(unlikely(ret)){
296         return -1;
297     }
298     LOG("%s %s successfully \n", SENSOR_STR, __func__);
299     return 0;
300 
301 }
302 
drv_baro_rohm_bm1383a_close(void)303 static int drv_baro_rohm_bm1383a_close(void)
304 {
305     int ret = 0;
306     ret  = drv_baro_rohm_bm1383a_set_power_mode(&bm1383a_ctx, DEV_POWER_OFF);
307     if(unlikely(ret)){
308         return -1;
309     }
310     LOG("%s %s successfully \n", SENSOR_STR, __func__);
311     return 0;
312 }
313 
314 
drv_baro_rohm_bm1383a_read(void * buf,size_t len)315 static int drv_baro_rohm_bm1383a_read(void *buf, size_t len)
316 {
317     int ret = 0;
318     size_t size;
319     uint8_t data[3];
320     barometer_data_t* pdata = (barometer_data_t*)buf;
321     if(buf == NULL){
322         return -1;
323     }
324 
325     size = sizeof(barometer_data_t);
326     if(len < size){
327         return -1;
328     }
329 
330     ret  = sensor_i2c_read(&bm1383a_ctx, BM1383A_REG_PRESSURE_LSB_L,  &data[0], I2C_DATA_LEN, I2C_OP_RETRIES);
331     ret |= sensor_i2c_read(&bm1383a_ctx, BM1383A_REG_PRESSURE_LSB_M,   &data[1], I2C_DATA_LEN, I2C_OP_RETRIES);
332     ret |= sensor_i2c_read(&bm1383a_ctx, BM1383A_REG_PRESSURE_MSB_H,   &data[2], I2C_DATA_LEN, I2C_OP_RETRIES);
333     if(unlikely(ret)){
334         return -1;
335     }
336 
337    /* hatch the baro data here*/
338     for(int i=0; i<3; i++){
339       pdata->p |= (((uint32_t)data[i]) << (8*i));
340     }
341 	// left 2 bit
342     //pdata->p = pdata->p >> 2;
343 
344     /* convert the 2's complement 24 bit to 2's complement 32 bit */
345     if((pdata->p & 0x00800000) != 0){
346         pdata->p |= 0xFF000000;
347     }
348     pdata->p = (pdata->p * 25)/2048;
349     //pdata->p = pdata->p/100;
350 #ifdef VALUE_OFFSET
351 	pdata->p -= PRESSURE_VALUES_OFFSET;
352 #endif
353 
354 
355     pdata->timestamp = aos_now_ms();
356 
357     return (int)size;
358 }
359 
drv_baro_rohm_bm1383a_write(const void * buf,size_t len)360 static int drv_baro_rohm_bm1383a_write(const void *buf, size_t len)
361 {
362     return 0;
363 }
364 
drv_baro_rohm_bm1383a_ioctl(int cmd,unsigned long arg)365 static int drv_baro_rohm_bm1383a_ioctl(int cmd, unsigned long arg)
366 {
367     int ret = 0;
368 
369     switch(cmd){
370         case SENSOR_IOCTL_ODR_SET:{
371             bm1383a_odr_e odr = drv_baro_rohm_bm1383a_hz2odr(arg);
372             ret = drv_baro_rohm_bm1383a_set_odr(&bm1383a_ctx, odr);
373             if(unlikely(ret)){
374                 return -1;
375             }
376         }break;
377         case SENSOR_IOCTL_SET_POWER:{
378             ret = drv_baro_rohm_bm1383a_set_power_mode(&bm1383a_ctx, arg);
379             if(unlikely(ret)){
380                 return -1;
381             }
382         }break;
383         case SENSOR_IOCTL_GET_INFO:{
384             /* fill the dev info here */
385             dev_sensor_info_t *info = (dev_sensor_info_t *)arg;
386             info->model = "BM1383A";
387             info->range_max = 110000;
388             info->range_min = 30000;
389             info->unit = pa;
390 
391         }break;
392 
393        default:break;
394     }
395 
396     LOG("%s %s successfully \n", SENSOR_STR, __func__);
397     return 0;
398 }
399 
drv_baro_rohm_bm1383a_init(void)400 int drv_baro_rohm_bm1383a_init(void){
401     int ret = 0;
402     sensor_obj_t sensor;
403     memset(&sensor, 0, sizeof(sensor));
404 
405     /* fill the sensor obj parameters here */
406     sensor.tag = TAG_DEV_BARO;
407     sensor.path = dev_baro_path;
408     sensor.io_port = I2C_PORT;
409     sensor.open = drv_baro_rohm_bm1383a_open;
410     sensor.close = drv_baro_rohm_bm1383a_close;
411     sensor.read = drv_baro_rohm_bm1383a_read;
412     sensor.write = drv_baro_rohm_bm1383a_write;
413     sensor.ioctl = drv_baro_rohm_bm1383a_ioctl;
414     sensor.irq_handle = drv_baro_rohm_bm1383a_irq_handle;
415 
416 
417     ret = sensor_create_obj(&sensor);
418     if(unlikely(ret)){
419         return -1;
420     }
421 
422     ret = drv_baro_rohm_bm1383a_validate_id(&bm1383a_ctx);
423     if(unlikely(ret)){
424         return -1;
425     }
426     /* set the default config for the sensor here */
427     ret = drv_baro_rohm_bm1383a_set_default_config(&bm1383a_ctx);
428     if(unlikely(ret)){
429         return -1;
430     }
431 
432     LOG("%s %s successfully \n", SENSOR_STR, __func__);
433     return 0;
434 }
435 
436 SENSOR_DRV_ADD(drv_baro_rohm_bm1383a_init);
437 
438