sensor.c 12 KB

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