drv_gpio.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-01-28 flybreak first version
  9. */
  10. #include "drv_gpio.h"
  11. static void pico_pin_mode(struct rt_device *dev, rt_base_t pin, rt_base_t mode)
  12. {
  13. RT_ASSERT((0 <= pin) && (pin < N_GPIOS));
  14. gpio_init(pin);
  15. switch (mode)
  16. {
  17. case PIN_MODE_OUTPUT:
  18. gpio_set_dir(pin, GPIO_OUT);
  19. break;
  20. case PIN_MODE_INPUT:
  21. gpio_set_dir(pin, GPIO_IN);
  22. break;
  23. case PIN_MODE_INPUT_PULLUP:
  24. gpio_pull_up(pin);
  25. break;
  26. case PIN_MODE_INPUT_PULLDOWN:
  27. gpio_pull_down(pin);
  28. break;
  29. case PIN_MODE_OUTPUT_OD:
  30. gpio_disable_pulls(pin);
  31. break;
  32. }
  33. }
  34. static void pico_pin_write(struct rt_device *dev, rt_base_t pin, rt_base_t value)
  35. {
  36. RT_ASSERT((0 <= pin) && (pin < N_GPIOS));
  37. gpio_put(pin, value);
  38. }
  39. static int pico_pin_read(struct rt_device *device, rt_base_t pin)
  40. {
  41. RT_ASSERT((0 <= pin) && (pin < N_GPIOS));
  42. return (gpio_get(pin)? PIN_HIGH : PIN_LOW);
  43. }
  44. static const struct rt_pin_ops ops =
  45. {
  46. pico_pin_mode,
  47. pico_pin_write,
  48. pico_pin_read,
  49. RT_NULL,
  50. RT_NULL,
  51. RT_NULL,
  52. RT_NULL,
  53. };
  54. int rt_hw_gpio_init(void)
  55. {
  56. rt_device_pin_register("gpio", &ops, RT_NULL);
  57. return 0;
  58. }
  59. INIT_DEVICE_EXPORT(rt_hw_gpio_init);