sensor.c 12 KB

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