sensor_bosch_bma400.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include "sensor_bosch_bma400.h"
  2. #define DBG_ENABLE
  3. #define DBG_LEVEL DBG_LOG
  4. #define DBG_SECTION_NAME "sensor.bosch.bma400"
  5. #define DBG_COLOR
  6. #include <rtdbg.h>
  7. #define GRAVITY_EARTH (9.80665f)
  8. static void rt_delay_ms(uint32_t period)
  9. {
  10. rt_thread_mdelay(period);
  11. }
  12. static int8_t rt_i2c_write_reg(void *intf_ptr, uint8_t addr, uint8_t reg, uint8_t *data, uint16_t len)
  13. {
  14. rt_uint8_t tmp = reg;
  15. struct rt_i2c_msg msgs[2];
  16. msgs[0].addr = addr; /* Slave address */
  17. msgs[0].flags = RT_I2C_WR; /* Write flag */
  18. msgs[0].buf = &tmp; /* Slave register address */
  19. msgs[0].len = 1; /* Number of bytes sent */
  20. msgs[1].addr = addr; /* Slave address */
  21. msgs[1].flags = RT_I2C_WR | RT_I2C_NO_START; /* Read flag */
  22. msgs[1].buf = data; /* Read data pointer */
  23. msgs[1].len = len; /* Number of bytes read */
  24. if (rt_i2c_transfer(intf_ptr, msgs, 2) != 2)
  25. {
  26. return (int8_t)-RT_ERROR;
  27. }
  28. return RT_EOK;
  29. }
  30. static int8_t rt_i2c_read_reg(void *intf_ptr, uint8_t addr, uint8_t reg, uint8_t *data, uint16_t len)
  31. {
  32. rt_uint8_t tmp = reg;
  33. struct rt_i2c_msg msgs[2];
  34. msgs[0].addr = addr; /* Slave address */
  35. msgs[0].flags = RT_I2C_WR; /* Write flag */
  36. msgs[0].buf = &tmp; /* Slave register address */
  37. msgs[0].len = 1; /* Number of bytes sent */
  38. msgs[1].addr = addr; /* Slave address */
  39. msgs[1].flags = RT_I2C_RD; /* Read flag */
  40. msgs[1].buf = data; /* Read data pointer */
  41. msgs[1].len = len; /* Number of bytes read */
  42. if (rt_i2c_transfer(intf_ptr, msgs, 2) != 2)
  43. {
  44. return (int8_t)-RT_ERROR;
  45. }
  46. return RT_EOK;
  47. }
  48. static struct bma400_dev *_bma400_create(struct rt_sensor_intf *intf)
  49. {
  50. struct bma400_dev *_bma400_dev = RT_NULL;
  51. struct rt_i2c_bus_device *i2c_bus_dev = RT_NULL;
  52. struct bma400_int_enable step_int;
  53. int8_t rslt = BMA400_OK;
  54. struct bma400_sensor_conf conf;
  55. i2c_bus_dev = (struct rt_i2c_bus_device *)rt_device_find(intf->dev_name);
  56. if (i2c_bus_dev == RT_NULL)
  57. {
  58. LOG_E("can not find device %s", intf->dev_name);
  59. return RT_NULL;
  60. }
  61. _bma400_dev = rt_calloc(1, sizeof(struct bma400_dev));
  62. if (_bma400_dev == RT_NULL)
  63. {
  64. LOG_E("bma400 dev memory allocation failed");
  65. return RT_NULL;
  66. }
  67. _bma400_dev->dev_id = (rt_uint32_t)(intf->user_data) & 0xff;
  68. _bma400_dev->intf = BMA400_I2C_INTF;
  69. _bma400_dev->intf_ptr = i2c_bus_dev;
  70. _bma400_dev->read = rt_i2c_read_reg;
  71. _bma400_dev->write = rt_i2c_write_reg;
  72. _bma400_dev->delay_ms = rt_delay_ms;
  73. rslt = bma400_init(_bma400_dev);
  74. if (rslt == BMA400_OK)
  75. {
  76. rslt = bma400_soft_reset(_bma400_dev);
  77. /* Select the type of configuration to be modified */
  78. conf.type = BMA400_ACCEL;
  79. /* Get the accelerometer configurations which are set in the sensor */
  80. rslt = bma400_get_sensor_conf(&conf, 1, _bma400_dev);
  81. /* Modify the desired configurations as per macros
  82. * available in bma400_defs.h file */
  83. conf.param.accel.odr = BMA400_ODR_100HZ;
  84. conf.param.accel.range = BMA400_2G_RANGE;
  85. conf.param.accel.data_src = BMA400_DATA_SRC_ACCEL_FILT_1;
  86. /* Set the desired configurations to the sensor */
  87. rslt = bma400_set_sensor_conf(&conf, 1, _bma400_dev);
  88. step_int.type = BMA400_STEP_COUNTER_INT_EN;
  89. step_int.conf = BMA400_ENABLE;
  90. rslt = bma400_enable_interrupt(&step_int, 1, _bma400_dev);
  91. bma400_set_power_mode(BMA400_SLEEP_MODE, _bma400_dev);
  92. return _bma400_dev;
  93. }
  94. else
  95. {
  96. LOG_E("bma400 init failed");
  97. rt_free(_bma400_dev);
  98. return RT_NULL;
  99. }
  100. }
  101. static rt_err_t _bma400_set_odr(rt_sensor_t sensor, rt_uint16_t odr)
  102. {
  103. struct bma400_dev *_bma400_dev = sensor->parent.user_data;
  104. struct bma400_sensor_conf conf;
  105. uint8_t odr_ctr;
  106. if (odr < 13)
  107. odr_ctr = BMA400_ODR_12_5HZ;
  108. else if (odr < 25)
  109. odr_ctr = BMA400_ODR_25HZ;
  110. else if (odr < 50)
  111. odr_ctr = BMA400_ODR_50HZ;
  112. else if (odr < 100)
  113. odr_ctr = BMA400_ODR_100HZ;
  114. else if (odr < 200)
  115. odr_ctr = BMA400_ODR_200HZ;
  116. else if (odr < 400)
  117. odr_ctr = BMA400_ODR_400HZ;
  118. else
  119. odr_ctr = BMA400_ODR_800HZ;
  120. if (sensor->info.type == RT_SENSOR_CLASS_ACCE)
  121. {
  122. conf.type = BMA400_ACCEL;
  123. /* Get the accelerometer configurations which are set in the sensor */
  124. bma400_get_sensor_conf(&conf, 1, _bma400_dev);
  125. conf.param.accel.odr = odr_ctr;
  126. /* Set the desired configurations to the sensor */
  127. bma400_set_sensor_conf(&conf, 1, _bma400_dev);
  128. return RT_EOK;
  129. }
  130. return RT_EOK;
  131. }
  132. static rt_err_t _bma400_set_range(rt_sensor_t sensor, rt_uint16_t range)
  133. {
  134. struct bma400_dev *_bma400_dev = sensor->parent.user_data;
  135. if (sensor->info.type == RT_SENSOR_CLASS_ACCE)
  136. {
  137. struct bma400_sensor_conf conf;
  138. uint8_t range_ctr;
  139. if (range < 2000)
  140. range_ctr = BMA400_2G_RANGE;
  141. else if (range < 4000)
  142. range_ctr = BMA400_4G_RANGE;
  143. else if (range < 8000)
  144. range_ctr = BMA400_8G_RANGE;
  145. else
  146. range_ctr = BMA400_16G_RANGE;
  147. conf.type = BMA400_ACCEL;
  148. /* Get the accelerometer configurations which are set in the sensor */
  149. bma400_get_sensor_conf(&conf, 1, _bma400_dev);
  150. conf.param.accel.range = range_ctr;
  151. /* Set the desired configurations to the sensor */
  152. bma400_set_sensor_conf(&conf, 1, _bma400_dev);
  153. return RT_EOK;
  154. }
  155. return RT_EOK;
  156. }
  157. static rt_err_t _bma400_set_power(rt_sensor_t sensor, rt_uint8_t power)
  158. {
  159. struct bma400_dev *_bma400_dev = sensor->parent.user_data;
  160. int8_t rslt = 0;
  161. if (power == RT_SENSOR_POWER_DOWN)
  162. {
  163. rslt = bma400_set_power_mode(BMA400_SLEEP_MODE, _bma400_dev);
  164. }
  165. else if (power == RT_SENSOR_POWER_NORMAL)
  166. {
  167. rslt = bma400_set_power_mode(BMA400_NORMAL_MODE, _bma400_dev);
  168. }
  169. else if (power == RT_SENSOR_POWER_LOW)
  170. {
  171. rslt = bma400_set_power_mode(BMA400_LOW_POWER_MODE, _bma400_dev);
  172. }
  173. else
  174. {
  175. LOG_W("Unsupported mode, code is %d", power);
  176. return -RT_ERROR;
  177. }
  178. return rslt;
  179. }
  180. static RT_SIZE_TYPE bma400_fetch_data(struct rt_sensor_device *sensor, void *buf, rt_size_t len)
  181. {
  182. struct bma400_dev *_bma400_dev = sensor->parent.user_data;
  183. struct rt_sensor_data *data = buf;
  184. if (sensor->info.type == RT_SENSOR_CLASS_ACCE)
  185. {
  186. struct bma400_sensor_data comp_data;
  187. bma400_get_accel_data(BMA400_DATA_SENSOR_TIME, &comp_data, _bma400_dev);
  188. data->type = RT_SENSOR_CLASS_ACCE;
  189. data->data.acce.x = comp_data.x;
  190. data->data.acce.y = comp_data.y;
  191. data->data.acce.z = comp_data.z;
  192. data->timestamp = rt_sensor_get_ts();
  193. }
  194. if (sensor->info.type == RT_SENSOR_CLASS_STEP)
  195. {
  196. uint32_t step_count;
  197. uint8_t activity;
  198. bma400_get_steps_counted(&step_count, &activity, _bma400_dev);
  199. data->type = RT_SENSOR_CLASS_STEP;
  200. data->data.step = step_count;
  201. data->timestamp = rt_sensor_get_ts();
  202. }
  203. return 1;
  204. }
  205. static rt_err_t bma400_control(struct rt_sensor_device *sensor, int cmd, void *args)
  206. {
  207. struct bma400_dev *_bma400_dev = sensor->parent.user_data;
  208. rt_err_t result = RT_EOK;
  209. switch (cmd)
  210. {
  211. case RT_SENSOR_CTRL_GET_ID:
  212. *(rt_uint8_t *)args = _bma400_dev->chip_id;
  213. break;
  214. case RT_SENSOR_CTRL_SET_ODR:
  215. result = _bma400_set_odr(sensor, (rt_uint32_t)args & 0xffff);
  216. break;
  217. case RT_SENSOR_CTRL_SET_RANGE:
  218. result = _bma400_set_range(sensor, (rt_uint32_t)args);
  219. break;
  220. case RT_SENSOR_CTRL_SET_POWER:
  221. result = _bma400_set_power(sensor, (rt_uint32_t)args & 0xff);
  222. break;
  223. case RT_SENSOR_CTRL_SELF_TEST:
  224. /* TODO */
  225. result = -RT_EINVAL;
  226. break;
  227. default:
  228. return -RT_EINVAL;
  229. }
  230. return result;
  231. }
  232. static struct rt_sensor_ops sensor_ops =
  233. {
  234. bma400_fetch_data,
  235. bma400_control
  236. };
  237. int rt_hw_bma400_init(const char *name, struct rt_sensor_config *cfg)
  238. {
  239. rt_int8_t result;
  240. rt_sensor_t sensor_acce = RT_NULL, sensor_step = RT_NULL;
  241. struct bma400_dev *_bma400_dev = RT_NULL;
  242. _bma400_dev = _bma400_create(&cfg->intf);
  243. if (_bma400_dev == RT_NULL)
  244. {
  245. LOG_E("sensor create failed");
  246. return -RT_ERROR;
  247. }
  248. #ifdef PKG_USING_BMA400_ACCE
  249. /* accelerometer sensor register */
  250. {
  251. sensor_acce = rt_calloc(1, sizeof(struct rt_sensor_device));
  252. if (sensor_acce == RT_NULL)
  253. return -RT_ERROR;
  254. sensor_acce->info.type = RT_SENSOR_CLASS_ACCE;
  255. sensor_acce->info.vendor = RT_SENSOR_VENDOR_BOSCH;
  256. sensor_acce->info.model = "bma400_acce";
  257. sensor_acce->info.unit = RT_SENSOR_UNIT_PA;
  258. sensor_acce->info.intf_type = RT_SENSOR_INTF_I2C;
  259. sensor_acce->info.range_max = 110000;
  260. sensor_acce->info.range_min = 30000;
  261. sensor_acce->info.period_min = 100;
  262. rt_memcpy(&sensor_acce->config, cfg, sizeof(struct rt_sensor_config));
  263. sensor_acce->ops = &sensor_ops;
  264. result = rt_hw_sensor_register(sensor_acce, name, RT_DEVICE_FLAG_RDWR, _bma400_dev);
  265. if (result != RT_EOK)
  266. {
  267. LOG_E("device register err code: %d", result);
  268. rt_free(sensor_acce);
  269. return -RT_ERROR;
  270. }
  271. }
  272. #endif
  273. #ifdef PKG_USING_BMA400_STEP
  274. /* step sensor register */
  275. {
  276. sensor_step = rt_calloc(1, sizeof(struct rt_sensor_device));
  277. if (sensor_step == RT_NULL)
  278. goto __exit;
  279. sensor_step->info.type = RT_SENSOR_CLASS_STEP;
  280. sensor_step->info.vendor = RT_SENSOR_VENDOR_BOSCH;
  281. sensor_step->info.model = "bma400_step";
  282. sensor_step->info.unit = RT_SENSOR_UNIT_ONE;
  283. sensor_step->info.intf_type = RT_SENSOR_INTF_I2C;
  284. sensor_step->info.period_min = 100;
  285. rt_memcpy(&sensor_step->config, cfg, sizeof(struct rt_sensor_config));
  286. sensor_step->ops = &sensor_ops;
  287. result = rt_hw_sensor_register(sensor_step, name, RT_DEVICE_FLAG_RDWR, _bma400_dev);
  288. if (result != RT_EOK)
  289. {
  290. LOG_E("device register err code: %d", result);
  291. goto __exit;
  292. }
  293. }
  294. #endif
  295. return RT_EOK;
  296. __exit:
  297. rt_free(sensor_acce);
  298. rt_device_unregister(&sensor_acce->parent);
  299. if (sensor_step)
  300. rt_free(sensor_step);
  301. return -RT_ERROR;
  302. }