sensor.c 12 KB

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