sensor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-01-31 flybreak first version
  9. * 2020-02-22 luhuadong support custom commands
  10. */
  11. #include <drivers/sensor.h>
  12. #define DBG_TAG "sensor"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. #include <string.h>
  16. static char *const sensor_name_str[] =
  17. {
  18. "None",
  19. "ac-", /* Accelerometer */
  20. "gy-", /* Gyroscope */
  21. "ma-", /* Magnetometer */
  22. "tm-", /* Temperature */
  23. "hm-", /* Relative Humidity */
  24. "br-", /* Barometer */
  25. "li-", /* Ambient light */
  26. "pr-", /* Proximity */
  27. "hr-", /* Heart Rate */
  28. "tv-", /* TVOC Level */
  29. "ni-", /* Noise Loudness */
  30. "st-", /* Step sensor */
  31. "fr-", /* Force sensor */
  32. "du-", /* Dust sensor */
  33. "ec-", /* eCO2 sensor */
  34. "gn-", /* GPS/GNSS sensor */
  35. "tf-", /* TOF sensor */
  36. "sp-", /* SpO2 sensor */
  37. "ia-", /* IAQ sensor */
  38. "et-", /* EtOH sensor */
  39. "bp-" /* Blood Pressure */
  40. };
  41. /* sensor interrupt handler function */
  42. static void _sensor_cb(rt_sensor_t sen)
  43. {
  44. if (sen->parent.rx_indicate == RT_NULL)
  45. {
  46. return;
  47. }
  48. if (sen->irq_handle != RT_NULL)
  49. {
  50. sen->irq_handle(sen);
  51. }
  52. /* The buffer is not empty. Read the data in the buffer first */
  53. if (sen->data_len > 0)
  54. {
  55. sen->parent.rx_indicate(&sen->parent, sen->data_len / sizeof(struct rt_sensor_data));
  56. }
  57. else if (sen->config.mode == RT_SENSOR_MODE_INT)
  58. {
  59. /* The interrupt mode only produces one data at a time */
  60. sen->parent.rx_indicate(&sen->parent, 1);
  61. }
  62. else if (sen->config.mode == RT_SENSOR_MODE_FIFO)
  63. {
  64. sen->parent.rx_indicate(&sen->parent, sen->info.fifo_max);
  65. }
  66. }
  67. /* ISR for sensor interrupt */
  68. static void _irq_callback(void *args)
  69. {
  70. rt_sensor_t sensor = (rt_sensor_t)args;
  71. rt_uint8_t i;
  72. if (sensor->module)
  73. {
  74. /* Invoke a callback for all sensors in the module */
  75. for (i = 0; i < sensor->module->sen_num; i++)
  76. {
  77. _sensor_cb(sensor->module->sen[i]);
  78. }
  79. }
  80. else
  81. {
  82. _sensor_cb(sensor);
  83. }
  84. }
  85. /* Sensor interrupt initialization function */
  86. static rt_err_t _sensor_irq_init(rt_sensor_t sensor)
  87. {
  88. if (sensor->config.irq_pin.pin == RT_PIN_NONE)
  89. {
  90. return -RT_EINVAL;
  91. }
  92. rt_pin_mode(sensor->config.irq_pin.pin, sensor->config.irq_pin.mode);
  93. if (sensor->config.irq_pin.mode == PIN_MODE_INPUT_PULLDOWN)
  94. {
  95. rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING, _irq_callback, (void *)sensor);
  96. }
  97. else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT_PULLUP)
  98. {
  99. rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_FALLING, _irq_callback, (void *)sensor);
  100. }
  101. else if (sensor->config.irq_pin.mode == PIN_MODE_INPUT)
  102. {
  103. rt_pin_attach_irq(sensor->config.irq_pin.pin, PIN_IRQ_MODE_RISING_FALLING, _irq_callback, (void *)sensor);
  104. }
  105. rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_TRUE);
  106. LOG_I("interrupt init success");
  107. return 0;
  108. }
  109. /* sensor local ops */
  110. static rt_ssize_t _local_fetch_data(rt_sensor_t sensor, rt_sensor_data_t buf, rt_size_t len)
  111. {
  112. LOG_D("Undefined fetch_data");
  113. return -RT_EINVAL;
  114. }
  115. static rt_err_t _local_control(rt_sensor_t sensor, int cmd, void *arg)
  116. {
  117. LOG_D("Undefined control");
  118. return -RT_EINVAL;
  119. }
  120. static struct rt_sensor_ops local_ops =
  121. {
  122. .fetch_data = _local_fetch_data,
  123. .control = _local_control
  124. };
  125. /* RT-Thread Device Interface */
  126. static rt_err_t _sensor_open(rt_device_t dev, rt_uint16_t oflag)
  127. {
  128. rt_sensor_t sensor = (rt_sensor_t)dev;
  129. RT_ASSERT(dev != RT_NULL);
  130. rt_err_t res = RT_EOK;
  131. rt_err_t (*local_ctrl)(rt_sensor_t sensor, int cmd, void *arg) = _local_control;
  132. if (sensor->module)
  133. {
  134. /* take the module mutex */
  135. rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
  136. }
  137. if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf == RT_NULL)
  138. {
  139. /* Allocate memory for the sensor buffer */
  140. sensor->data_buf = rt_malloc(sizeof(struct rt_sensor_data) * sensor->info.fifo_max);
  141. if (sensor->data_buf == RT_NULL)
  142. {
  143. res = -RT_ENOMEM;
  144. goto __exit;
  145. }
  146. }
  147. if (sensor->ops->control != RT_NULL)
  148. {
  149. local_ctrl = sensor->ops->control;
  150. }
  151. sensor->config.mode = RT_SENSOR_MODE_POLLING;
  152. if (oflag & RT_DEVICE_FLAG_RDONLY && dev->flag & RT_DEVICE_FLAG_RDONLY)
  153. {
  154. /* If polling mode is supported, configure it to polling mode */
  155. local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_POLLING);
  156. }
  157. else if (oflag & RT_DEVICE_FLAG_INT_RX && dev->flag & RT_DEVICE_FLAG_INT_RX)
  158. {
  159. /* If interrupt mode is supported, configure it to interrupt mode */
  160. if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_INT) == RT_EOK)
  161. {
  162. /* Initialization sensor interrupt */
  163. _sensor_irq_init(sensor);
  164. sensor->config.mode = RT_SENSOR_MODE_INT;
  165. }
  166. }
  167. else if (oflag & RT_DEVICE_FLAG_FIFO_RX && dev->flag & RT_DEVICE_FLAG_FIFO_RX)
  168. {
  169. /* If fifo mode is supported, configure it to fifo mode */
  170. if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_MODE, (void *)RT_SENSOR_MODE_FIFO) == RT_EOK)
  171. {
  172. /* Initialization sensor interrupt */
  173. _sensor_irq_init(sensor);
  174. sensor->config.mode = RT_SENSOR_MODE_FIFO;
  175. }
  176. }
  177. else
  178. {
  179. res = -RT_EINVAL;
  180. goto __exit;
  181. }
  182. /* Configure power mode to normal mode */
  183. if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_NORMAL) == RT_EOK)
  184. {
  185. sensor->config.power = RT_SENSOR_POWER_NORMAL;
  186. }
  187. __exit:
  188. if (sensor->module)
  189. {
  190. /* release the module mutex */
  191. rt_mutex_release(sensor->module->lock);
  192. }
  193. return res;
  194. }
  195. static rt_err_t _sensor_close(rt_device_t dev)
  196. {
  197. rt_sensor_t sensor = (rt_sensor_t)dev;
  198. int i;
  199. rt_err_t (*local_ctrl)(rt_sensor_t sensor, int cmd, void *arg) = _local_control;
  200. RT_ASSERT(dev != RT_NULL);
  201. if (sensor->module)
  202. {
  203. rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
  204. }
  205. if (sensor->ops->control != RT_NULL)
  206. {
  207. local_ctrl = sensor->ops->control;
  208. }
  209. /* Configure power mode to power down mode */
  210. if (local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, (void *)RT_SENSOR_POWER_DOWN) == RT_EOK)
  211. {
  212. sensor->config.power = RT_SENSOR_POWER_DOWN;
  213. }
  214. if (sensor->module != RT_NULL && sensor->info.fifo_max > 0 && sensor->data_buf != RT_NULL)
  215. {
  216. for (i = 0; i < sensor->module->sen_num; i ++)
  217. {
  218. if (sensor->module->sen[i]->parent.ref_count > 0)
  219. goto __exit;
  220. }
  221. /* Free memory for the sensor buffer */
  222. for (i = 0; i < sensor->module->sen_num; i ++)
  223. {
  224. if (sensor->module->sen[i]->data_buf)
  225. {
  226. rt_free(sensor->module->sen[i]->data_buf);
  227. sensor->module->sen[i]->data_buf = RT_NULL;
  228. }
  229. }
  230. }
  231. if (sensor->config.mode != RT_SENSOR_MODE_POLLING)
  232. {
  233. /* Sensor disable interrupt */
  234. if (sensor->config.irq_pin.pin != RT_PIN_NONE)
  235. {
  236. rt_pin_irq_enable(sensor->config.irq_pin.pin, RT_FALSE);
  237. }
  238. }
  239. __exit:
  240. if (sensor->module)
  241. {
  242. rt_mutex_release(sensor->module->lock);
  243. }
  244. return RT_EOK;
  245. }
  246. static rt_size_t _sensor_read(rt_device_t dev, rt_off_t pos, void *buf, rt_size_t len)
  247. {
  248. rt_sensor_t sensor = (rt_sensor_t)dev;
  249. rt_size_t result = 0;
  250. RT_ASSERT(dev != RT_NULL);
  251. if (buf == NULL || len == 0)
  252. {
  253. return 0;
  254. }
  255. if (sensor->module)
  256. {
  257. rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
  258. }
  259. /* The buffer is not empty. Read the data in the buffer first */
  260. if (sensor->data_len > 0)
  261. {
  262. if (len > sensor->data_len / sizeof(struct rt_sensor_data))
  263. {
  264. len = sensor->data_len / sizeof(struct rt_sensor_data);
  265. }
  266. rt_memcpy(buf, sensor->data_buf, len * sizeof(struct rt_sensor_data));
  267. /* Clear the buffer */
  268. sensor->data_len = 0;
  269. result = len;
  270. }
  271. else
  272. {
  273. /* If the buffer is empty, read the data */
  274. if (sensor->ops->fetch_data)
  275. {
  276. result = sensor->ops->fetch_data(sensor, buf, len);
  277. }
  278. }
  279. if (sensor->module)
  280. {
  281. rt_mutex_release(sensor->module->lock);
  282. }
  283. return result;
  284. }
  285. static rt_err_t _sensor_control(rt_device_t dev, int cmd, void *args)
  286. {
  287. rt_sensor_t sensor = (rt_sensor_t)dev;
  288. rt_err_t result = RT_EOK;
  289. RT_ASSERT(dev != RT_NULL);
  290. rt_err_t (*local_ctrl)(rt_sensor_t sensor, int cmd, void *arg) = _local_control;
  291. if (sensor->module)
  292. {
  293. rt_mutex_take(sensor->module->lock, RT_WAITING_FOREVER);
  294. }
  295. if (sensor->ops->control != RT_NULL)
  296. {
  297. local_ctrl = sensor->ops->control;
  298. }
  299. switch (cmd)
  300. {
  301. case RT_SENSOR_CTRL_GET_ID:
  302. if (args)
  303. {
  304. result = local_ctrl(sensor, RT_SENSOR_CTRL_GET_ID, args);
  305. }
  306. break;
  307. case RT_SENSOR_CTRL_GET_INFO:
  308. if (args)
  309. {
  310. rt_memcpy(args, &sensor->info, sizeof(struct rt_sensor_info));
  311. }
  312. break;
  313. case RT_SENSOR_CTRL_SET_RANGE:
  314. /* Configuration measurement range */
  315. result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_RANGE, args);
  316. if (result == RT_EOK)
  317. {
  318. sensor->config.range = (rt_int32_t)args;
  319. LOG_D("set range %d", sensor->config.range);
  320. }
  321. break;
  322. case RT_SENSOR_CTRL_SET_ODR:
  323. /* Configuration data output rate */
  324. result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_ODR, args);
  325. if (result == RT_EOK)
  326. {
  327. sensor->config.odr = (rt_uint32_t)args & 0xFFFF;
  328. LOG_D("set odr %d", sensor->config.odr);
  329. }
  330. break;
  331. case RT_SENSOR_CTRL_SET_POWER:
  332. /* Configuration sensor power mode */
  333. result = local_ctrl(sensor, RT_SENSOR_CTRL_SET_POWER, args);
  334. if (result == RT_EOK)
  335. {
  336. sensor->config.power = (rt_uint32_t)args & 0xFF;
  337. LOG_D("set power mode code:", sensor->config.power);
  338. }
  339. break;
  340. case RT_SENSOR_CTRL_SELF_TEST:
  341. /* Device self-test */
  342. result = local_ctrl(sensor, RT_SENSOR_CTRL_SELF_TEST, args);
  343. break;
  344. default:
  345. if (cmd > RT_SENSOR_CTRL_USER_CMD_START)
  346. {
  347. /* Custom commands */
  348. result = local_ctrl(sensor, cmd, args);
  349. }
  350. else
  351. {
  352. result = -RT_ERROR;
  353. }
  354. break;
  355. }
  356. if (sensor->module)
  357. {
  358. rt_mutex_release(sensor->module->lock);
  359. }
  360. return result;
  361. }
  362. #ifdef RT_USING_DEVICE_OPS
  363. const static struct rt_device_ops rt_sensor_ops =
  364. {
  365. RT_NULL,
  366. _sensor_open,
  367. _sensor_close,
  368. _sensor_read,
  369. RT_NULL,
  370. _sensor_control
  371. };
  372. #endif
  373. /*
  374. * sensor register
  375. */
  376. int rt_hw_sensor_register(rt_sensor_t sensor,
  377. const char *name,
  378. rt_uint32_t flag,
  379. void *data)
  380. {
  381. rt_int8_t result;
  382. rt_device_t device;
  383. RT_ASSERT(sensor != RT_NULL);
  384. char *sensor_name = RT_NULL, *device_name = RT_NULL;
  385. if (sensor->ops == RT_NULL)
  386. {
  387. sensor->ops = &local_ops;
  388. }
  389. /* Add a type name for the sensor device */
  390. sensor_name = sensor_name_str[sensor->info.type];
  391. device_name = (char *)rt_calloc(1, rt_strlen(sensor_name) + 1 + rt_strlen(name));
  392. if (device_name == RT_NULL)
  393. {
  394. LOG_E("device_name calloc failed!");
  395. return -RT_ERROR;
  396. }
  397. rt_memcpy(device_name, sensor_name, rt_strlen(sensor_name) + 1);
  398. strcat(device_name, name);
  399. if (sensor->module != RT_NULL && sensor->module->lock == RT_NULL)
  400. {
  401. /* Create a mutex lock for the module */
  402. sensor->module->lock = rt_mutex_create(name, RT_IPC_FLAG_PRIO);
  403. if (sensor->module->lock == RT_NULL)
  404. {
  405. rt_free(device_name);
  406. return -RT_ERROR;
  407. }
  408. }
  409. device = &sensor->parent;
  410. #ifdef RT_USING_DEVICE_OPS
  411. device->ops = &rt_sensor_ops;
  412. #else
  413. device->init = RT_NULL;
  414. device->open = _sensor_open;
  415. device->close = _sensor_close;
  416. device->read = _sensor_read;
  417. device->write = RT_NULL;
  418. device->control = _sensor_control;
  419. #endif
  420. device->type = RT_Device_Class_Sensor;
  421. device->rx_indicate = RT_NULL;
  422. device->tx_complete = RT_NULL;
  423. device->user_data = data;
  424. result = rt_device_register(device, device_name, flag | RT_DEVICE_FLAG_STANDALONE);
  425. if (result != RT_EOK)
  426. {
  427. LOG_E("sensor[%s] register err code: %d", device_name, result);
  428. rt_free(device_name);
  429. return result;
  430. }
  431. LOG_I("sensor[%s] init success", device_name);
  432. rt_free(device_name);
  433. return RT_EOK;
  434. }