sensor.c 16 KB

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