sensor.c 15 KB

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