sensor.c 12 KB

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