sensor.c 13 KB

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