dev_pin.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015-01-20 Bernard the first version
  9. * 2021-02-06 Meco Man fix RT_ENOSYS code in negative
  10. * 2022-04-29 WangQiang add pin operate command in MSH
  11. */
  12. #include <drivers/dev_pin.h>
  13. static struct rt_device_pin _hw_pin;
  14. static rt_ssize_t _pin_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  15. {
  16. struct rt_device_pin_value *value;
  17. struct rt_device_pin *pin = (struct rt_device_pin *)dev;
  18. /* check parameters */
  19. RT_ASSERT(pin != RT_NULL);
  20. value = (struct rt_device_pin_value *)buffer;
  21. if (value == RT_NULL || size != sizeof(*value))
  22. return 0;
  23. value->value = pin->ops->pin_read(dev, value->pin);
  24. return size;
  25. }
  26. static rt_ssize_t _pin_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  27. {
  28. struct rt_device_pin_value *value;
  29. struct rt_device_pin *pin = (struct rt_device_pin *)dev;
  30. /* check parameters */
  31. RT_ASSERT(pin != RT_NULL);
  32. value = (struct rt_device_pin_value *)buffer;
  33. if (value == RT_NULL || size != sizeof(*value))
  34. return 0;
  35. pin->ops->pin_write(dev, (rt_base_t)value->pin, (rt_base_t)value->value);
  36. return size;
  37. }
  38. static rt_err_t _pin_control(rt_device_t dev, int cmd, void *args)
  39. {
  40. struct rt_device_pin_mode *mode;
  41. struct rt_device_pin *pin = (struct rt_device_pin *)dev;
  42. /* check parameters */
  43. RT_ASSERT(pin != RT_NULL);
  44. mode = (struct rt_device_pin_mode *)args;
  45. if (mode == RT_NULL)
  46. return -RT_ERROR;
  47. pin->ops->pin_mode(dev, (rt_base_t)mode->pin, (rt_base_t)mode->mode);
  48. return 0;
  49. }
  50. #ifdef RT_USING_DEVICE_OPS
  51. const static struct rt_device_ops pin_ops =
  52. {
  53. RT_NULL,
  54. RT_NULL,
  55. RT_NULL,
  56. _pin_read,
  57. _pin_write,
  58. _pin_control
  59. };
  60. #endif
  61. int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data)
  62. {
  63. _hw_pin.parent.type = RT_Device_Class_Pin;
  64. _hw_pin.parent.rx_indicate = RT_NULL;
  65. _hw_pin.parent.tx_complete = RT_NULL;
  66. #ifdef RT_USING_DEVICE_OPS
  67. _hw_pin.parent.ops = &pin_ops;
  68. #else
  69. _hw_pin.parent.init = RT_NULL;
  70. _hw_pin.parent.open = RT_NULL;
  71. _hw_pin.parent.close = RT_NULL;
  72. _hw_pin.parent.read = _pin_read;
  73. _hw_pin.parent.write = _pin_write;
  74. _hw_pin.parent.control = _pin_control;
  75. #endif
  76. _hw_pin.ops = ops;
  77. _hw_pin.parent.user_data = user_data;
  78. /* register a character device */
  79. rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR);
  80. return 0;
  81. }
  82. rt_err_t rt_pin_attach_irq(rt_base_t pin, rt_uint8_t mode,
  83. void (*hdr)(void *args), void *args)
  84. {
  85. RT_ASSERT(_hw_pin.ops != RT_NULL);
  86. if (_hw_pin.ops->pin_attach_irq)
  87. {
  88. return _hw_pin.ops->pin_attach_irq(&_hw_pin.parent, pin, mode, hdr, args);
  89. }
  90. return -RT_ENOSYS;
  91. }
  92. rt_err_t rt_pin_detach_irq(rt_base_t pin)
  93. {
  94. RT_ASSERT(_hw_pin.ops != RT_NULL);
  95. if (_hw_pin.ops->pin_detach_irq)
  96. {
  97. return _hw_pin.ops->pin_detach_irq(&_hw_pin.parent, pin);
  98. }
  99. return -RT_ENOSYS;
  100. }
  101. rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint8_t enabled)
  102. {
  103. RT_ASSERT(_hw_pin.ops != RT_NULL);
  104. if (_hw_pin.ops->pin_irq_enable)
  105. {
  106. return _hw_pin.ops->pin_irq_enable(&_hw_pin.parent, pin, enabled);
  107. }
  108. return -RT_ENOSYS;
  109. }
  110. rt_err_t rt_pin_debounce(rt_base_t pin, rt_uint32_t debounce)
  111. {
  112. RT_ASSERT(_hw_pin.ops != RT_NULL);
  113. if (_hw_pin.ops->pin_debounce)
  114. {
  115. return _hw_pin.ops->pin_debounce(&_hw_pin.parent, pin, debounce);
  116. }
  117. return -RT_ENOSYS;
  118. }
  119. /* RT-Thread Hardware PIN APIs */
  120. void rt_pin_mode(rt_base_t pin, rt_uint8_t mode)
  121. {
  122. RT_ASSERT(_hw_pin.ops != RT_NULL);
  123. _hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode);
  124. }
  125. void rt_pin_write(rt_base_t pin, rt_ssize_t value)
  126. {
  127. RT_ASSERT(_hw_pin.ops != RT_NULL);
  128. _hw_pin.ops->pin_write(&_hw_pin.parent, pin, value);
  129. }
  130. rt_ssize_t rt_pin_read(rt_base_t pin)
  131. {
  132. RT_ASSERT(_hw_pin.ops != RT_NULL);
  133. return _hw_pin.ops->pin_read(&_hw_pin.parent, pin);
  134. }
  135. /* Get pin number by name, such as PA.0, P0.12 */
  136. rt_base_t rt_pin_get(const char *name)
  137. {
  138. RT_ASSERT(_hw_pin.ops != RT_NULL);
  139. if (_hw_pin.ops->pin_get == RT_NULL)
  140. {
  141. return -RT_ENOSYS;
  142. }
  143. return _hw_pin.ops->pin_get(name);
  144. }
  145. #ifdef RT_USING_FINSH
  146. #include <string.h>
  147. #include <stdlib.h>
  148. #include <ctype.h>
  149. #include <finsh.h>
  150. #include <msh_parse.h>
  151. /*
  152. * convert function for port name
  153. */
  154. static rt_base_t _pin_cmd_conv(const char *name)
  155. {
  156. return rt_pin_get(name);
  157. }
  158. static void _pin_cmd_print_usage(void)
  159. {
  160. rt_kprintf("pin [option] GPIO\n");
  161. rt_kprintf(" num: get pin number from hardware pin\n");
  162. rt_kprintf(" mode: set pin mode to output/input/input_pullup/input_pulldown/output_od\n");
  163. rt_kprintf(" e.g. MSH >pin mode GPIO output\n");
  164. rt_kprintf(" read: read pin level of hardware pin\n");
  165. rt_kprintf(" e.g. MSH >pin read GPIO\n");
  166. rt_kprintf(" write: write pin level(high/low or on/off) to hardware pin\n");
  167. rt_kprintf(" e.g. MSH >pin write GPIO high\n");
  168. rt_kprintf(" help: this help list\n");
  169. rt_kprintf("GPIO e.g.:");
  170. rt_pin_get(" ");
  171. }
  172. /* e.g. MSH >pin num PA.16 */
  173. static void _pin_cmd_get(int argc, char *argv[])
  174. {
  175. rt_base_t pin;
  176. if (argc < 3)
  177. {
  178. _pin_cmd_print_usage();
  179. return;
  180. }
  181. pin = _pin_cmd_conv(argv[2]);
  182. if (pin < 0)
  183. {
  184. rt_kprintf("Parameter invalid : %s!\n", argv[2]);
  185. _pin_cmd_print_usage();
  186. return ;
  187. }
  188. rt_kprintf("%s : %d\n", argv[2], pin);
  189. }
  190. /* e.g. MSH >pin mode PA.16 output */
  191. static void _pin_cmd_mode(int argc, char *argv[])
  192. {
  193. rt_base_t pin;
  194. rt_base_t mode;
  195. if (argc < 4)
  196. {
  197. _pin_cmd_print_usage();
  198. return;
  199. }
  200. if (!msh_isint(argv[2]))
  201. {
  202. pin = _pin_cmd_conv(argv[2]);
  203. if (pin < 0)
  204. {
  205. rt_kprintf("Parameter invalid : %s!\n", argv[2]);
  206. _pin_cmd_print_usage();
  207. return;
  208. }
  209. }
  210. else
  211. {
  212. pin = atoi(argv[2]);
  213. }
  214. if (0 == rt_strcmp("output", argv[3]))
  215. {
  216. mode = PIN_MODE_OUTPUT;
  217. }
  218. else if (0 == rt_strcmp("input", argv[3]))
  219. {
  220. mode = PIN_MODE_INPUT;
  221. }
  222. else if (0 == rt_strcmp("input_pullup", argv[3]))
  223. {
  224. mode = PIN_MODE_INPUT_PULLUP;
  225. }
  226. else if (0 == rt_strcmp("input_pulldown", argv[3]))
  227. {
  228. mode = PIN_MODE_INPUT_PULLDOWN;
  229. }
  230. else if (0 == rt_strcmp("output_od", argv[3]))
  231. {
  232. mode = PIN_MODE_OUTPUT_OD;
  233. }
  234. else
  235. {
  236. _pin_cmd_print_usage();
  237. return;
  238. }
  239. rt_pin_mode(pin, mode);
  240. }
  241. /* e.g. MSH >pin read PA.16 */
  242. static void _pin_cmd_read(int argc, char *argv[])
  243. {
  244. rt_base_t pin;
  245. rt_uint8_t value;
  246. if (argc < 3)
  247. {
  248. _pin_cmd_print_usage();
  249. return;
  250. }
  251. if (!msh_isint(argv[2]))
  252. {
  253. pin = _pin_cmd_conv(argv[2]);
  254. if (pin < 0)
  255. {
  256. rt_kprintf("Parameter invalid : %s!\n", argv[2]);
  257. _pin_cmd_print_usage();
  258. return;
  259. }
  260. }
  261. else
  262. {
  263. pin = atoi(argv[2]);
  264. }
  265. value = rt_pin_read(pin);
  266. if (value == PIN_HIGH)
  267. {
  268. rt_kprintf("pin[%d] = high\n", pin);
  269. }
  270. else
  271. {
  272. rt_kprintf("pin[%d] = low\n", pin);
  273. }
  274. }
  275. /* e.g. MSH >pin write PA.16 high */
  276. static void _pin_cmd_write(int argc, char *argv[])
  277. {
  278. rt_base_t pin;
  279. rt_uint8_t value;
  280. if (argc < 4)
  281. {
  282. _pin_cmd_print_usage();
  283. return;
  284. }
  285. if (!msh_isint(argv[2]))
  286. {
  287. pin = _pin_cmd_conv(argv[2]);
  288. if (pin < 0)
  289. {
  290. rt_kprintf("Parameter invalid : %s!\n", argv[2]);
  291. _pin_cmd_print_usage();
  292. return;
  293. }
  294. }
  295. else
  296. {
  297. pin = atoi(argv[2]);
  298. }
  299. if ((0 == rt_strcmp("high", argv[3])) || (0 == rt_strcmp("on", argv[3])))
  300. {
  301. value = PIN_HIGH;
  302. }
  303. else if ((0 == rt_strcmp("low", argv[3])) || (0 == rt_strcmp("off", argv[3])))
  304. {
  305. value = PIN_LOW;
  306. }
  307. else
  308. {
  309. _pin_cmd_print_usage();
  310. return;
  311. }
  312. rt_pin_write(pin, value);
  313. }
  314. static void _pin_cmd(int argc, char *argv[])
  315. {
  316. if (argc < 3)
  317. {
  318. _pin_cmd_print_usage();
  319. return ;
  320. }
  321. if (0 == rt_strcmp("num", argv[1]))
  322. {
  323. _pin_cmd_get(argc, argv);
  324. }
  325. else if (0 == rt_strcmp("mode", argv[1]))
  326. {
  327. _pin_cmd_mode(argc, argv);
  328. }
  329. else if (0 == rt_strcmp("read", argv[1]))
  330. {
  331. _pin_cmd_read(argc, argv);
  332. }
  333. else if (0 == rt_strcmp("write", argv[1]))
  334. {
  335. _pin_cmd_write(argc, argv);
  336. }
  337. else
  338. {
  339. _pin_cmd_print_usage();
  340. return;
  341. }
  342. }
  343. MSH_CMD_EXPORT_ALIAS(_pin_cmd, pin, pin [option]);
  344. #endif /* RT_USING_FINSH */