pin.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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/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-Thread Hardware PIN APIs */
  111. void rt_pin_mode(rt_base_t pin, rt_uint8_t mode)
  112. {
  113. RT_ASSERT(_hw_pin.ops != RT_NULL);
  114. _hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode);
  115. }
  116. void rt_pin_write(rt_base_t pin, rt_uint8_t value)
  117. {
  118. RT_ASSERT(_hw_pin.ops != RT_NULL);
  119. _hw_pin.ops->pin_write(&_hw_pin.parent, pin, value);
  120. }
  121. rt_int8_t rt_pin_read(rt_base_t pin)
  122. {
  123. RT_ASSERT(_hw_pin.ops != RT_NULL);
  124. return _hw_pin.ops->pin_read(&_hw_pin.parent, pin);
  125. }
  126. /* Get pin number by name, such as PA.0, P0.12 */
  127. rt_base_t rt_pin_get(const char *name)
  128. {
  129. RT_ASSERT(_hw_pin.ops != RT_NULL);
  130. if (_hw_pin.ops->pin_get == RT_NULL)
  131. {
  132. return -RT_ENOSYS;
  133. }
  134. return _hw_pin.ops->pin_get(name);
  135. }
  136. #ifdef RT_USING_FINSH
  137. #include <string.h>
  138. #include <stdlib.h>
  139. #include <ctype.h>
  140. #include <finsh.h>
  141. #include <msh_parse.h>
  142. /*
  143. * convert function for port name
  144. */
  145. static rt_base_t _pin_cmd_conv(const char *name)
  146. {
  147. return rt_pin_get(name);
  148. }
  149. static void _pin_cmd_print_usage(void)
  150. {
  151. rt_kprintf("pin [option] GPIO\n");
  152. rt_kprintf(" num: get pin number from hardware pin\n");
  153. rt_kprintf(" mode: set pin mode to output/input/input_pullup/input_pulldown/output_od\n");
  154. rt_kprintf(" e.g. MSH >pin mode GPIO output\n");
  155. rt_kprintf(" read: read pin level of hardware pin\n");
  156. rt_kprintf(" e.g. MSH >pin read GPIO\n");
  157. rt_kprintf(" write: write pin level(high/low or on/off) to hardware pin\n");
  158. rt_kprintf(" e.g. MSH >pin write GPIO high\n");
  159. rt_kprintf(" help: this help list\n");
  160. rt_kprintf("GPIO e.g.:");
  161. rt_pin_get(" ");
  162. }
  163. /* e.g. MSH >pin num PA.16 */
  164. static void _pin_cmd_get(int argc, char *argv[])
  165. {
  166. rt_base_t pin;
  167. if (argc < 3)
  168. {
  169. _pin_cmd_print_usage();
  170. return;
  171. }
  172. pin = _pin_cmd_conv(argv[2]);
  173. if (pin < 0)
  174. {
  175. rt_kprintf("Parameter invalid : %s!\n", argv[2]);
  176. _pin_cmd_print_usage();
  177. return ;
  178. }
  179. rt_kprintf("%s : %d\n", argv[2], pin);
  180. }
  181. /* e.g. MSH >pin mode PA.16 output */
  182. static void _pin_cmd_mode(int argc, char *argv[])
  183. {
  184. rt_base_t pin;
  185. rt_base_t mode;
  186. if (argc < 4)
  187. {
  188. _pin_cmd_print_usage();
  189. return;
  190. }
  191. if (!msh_isint(argv[2]))
  192. {
  193. pin = _pin_cmd_conv(argv[2]);
  194. if (pin < 0)
  195. {
  196. rt_kprintf("Parameter invalid : %s!\n", argv[2]);
  197. _pin_cmd_print_usage();
  198. return;
  199. }
  200. }
  201. else
  202. {
  203. pin = atoi(argv[2]);
  204. }
  205. if (0 == rt_strcmp("output", argv[3]))
  206. {
  207. mode = PIN_MODE_OUTPUT;
  208. }
  209. else if (0 == rt_strcmp("input", argv[3]))
  210. {
  211. mode = PIN_MODE_INPUT;
  212. }
  213. else if (0 == rt_strcmp("input_pullup", argv[3]))
  214. {
  215. mode = PIN_MODE_INPUT_PULLUP;
  216. }
  217. else if (0 == rt_strcmp("input_pulldown", argv[3]))
  218. {
  219. mode = PIN_MODE_INPUT_PULLDOWN;
  220. }
  221. else if (0 == rt_strcmp("output_od", argv[3]))
  222. {
  223. mode = PIN_MODE_OUTPUT_OD;
  224. }
  225. else
  226. {
  227. _pin_cmd_print_usage();
  228. return;
  229. }
  230. rt_pin_mode(pin, mode);
  231. }
  232. /* e.g. MSH >pin read PA.16 */
  233. static void _pin_cmd_read(int argc, char *argv[])
  234. {
  235. rt_base_t pin;
  236. rt_uint8_t value;
  237. if (argc < 3)
  238. {
  239. _pin_cmd_print_usage();
  240. return;
  241. }
  242. if (!msh_isint(argv[2]))
  243. {
  244. pin = _pin_cmd_conv(argv[2]);
  245. if (pin < 0)
  246. {
  247. rt_kprintf("Parameter invalid : %s!\n", argv[2]);
  248. _pin_cmd_print_usage();
  249. return;
  250. }
  251. }
  252. else
  253. {
  254. pin = atoi(argv[2]);
  255. }
  256. value = rt_pin_read(pin);
  257. if (value == PIN_HIGH)
  258. {
  259. rt_kprintf("pin[%d] = high\n", pin);
  260. }
  261. else
  262. {
  263. rt_kprintf("pin[%d] = low\n", pin);
  264. }
  265. }
  266. /* e.g. MSH >pin write PA.16 high */
  267. static void _pin_cmd_write(int argc, char *argv[])
  268. {
  269. rt_base_t pin;
  270. rt_uint8_t value;
  271. if (argc < 4)
  272. {
  273. _pin_cmd_print_usage();
  274. return;
  275. }
  276. if (!msh_isint(argv[2]))
  277. {
  278. pin = _pin_cmd_conv(argv[2]);
  279. if (pin < 0)
  280. {
  281. rt_kprintf("Parameter invalid : %s!\n", argv[2]);
  282. _pin_cmd_print_usage();
  283. return;
  284. }
  285. }
  286. else
  287. {
  288. pin = atoi(argv[2]);
  289. }
  290. if ((0 == rt_strcmp("high", argv[3])) || (0 == rt_strcmp("on", argv[3])))
  291. {
  292. value = PIN_HIGH;
  293. }
  294. else if ((0 == rt_strcmp("low", argv[3])) || (0 == rt_strcmp("off", argv[3])))
  295. {
  296. value = PIN_LOW;
  297. }
  298. else
  299. {
  300. _pin_cmd_print_usage();
  301. return;
  302. }
  303. rt_pin_write(pin, value);
  304. }
  305. static void _pin_cmd(int argc, char *argv[])
  306. {
  307. if (argc < 3)
  308. {
  309. _pin_cmd_print_usage();
  310. return ;
  311. }
  312. if (0 == rt_strcmp("num", argv[1]))
  313. {
  314. _pin_cmd_get(argc, argv);
  315. }
  316. else if (0 == rt_strcmp("mode", argv[1]))
  317. {
  318. _pin_cmd_mode(argc, argv);
  319. }
  320. else if (0 == rt_strcmp("read", argv[1]))
  321. {
  322. _pin_cmd_read(argc, argv);
  323. }
  324. else if (0 == rt_strcmp("write", argv[1]))
  325. {
  326. _pin_cmd_write(argc, argv);
  327. }
  328. else
  329. {
  330. _pin_cmd_print_usage();
  331. return;
  332. }
  333. }
  334. MSH_CMD_EXPORT_ALIAS(_pin_cmd, pin, pin [option]);
  335. #endif /* RT_USING_FINSH */