gpio_sample.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Email: opensource_embedded@phytium.com.cn
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2022-12-10 liqiaozhong first commit
  11. * 2025-04-08 liqiaozhong
  12. *
  13. */
  14. #include "rtconfig.h"
  15. #ifdef BSP_USING_GPIO
  16. #include <rtthread.h>
  17. #include <rtdevice.h>
  18. #include <string.h>
  19. #include "ftypes.h"
  20. #include "fkernel.h"
  21. #include "board.h"
  22. #include "fgpio.h"
  23. /**************************** Type Definitions *******************************/
  24. /************************** Variable Definitions *****************************/
  25. static rt_base_t input_pin_index = 0U;
  26. static rt_base_t output_pin_index = 0U;
  27. /***************** Macros (Inline Functions) Definitions *********************/
  28. #define FGPIO_OPS_PIN_PORT_A 0U
  29. #define FGPIO_OPS_PIN_PORT_B 1U
  30. #define FGPIO_OPS_LEVEL_LOW 0U
  31. #define FGPIO_OPS_LEVEL_HIGH 1U
  32. /* Extract the index number from input_pin_index */
  33. #define FGPIO_GET_CTRL(index) (((index) - FGPIO_GET_PIN(index) - (FGPIO_PORT_A * FGPIO_PIN_NUM)) / (FGPIO_PORT_NUM * FGPIO_PIN_NUM))
  34. /* Extract the pin number from input_pin_index */
  35. #define FGPIO_GET_PIN(index) ((index) % FGPIO_PIN_NUM)
  36. /***************************** main function *********************************/
  37. static void gpio_irq_test(s32 vector, void *param)
  38. {
  39. FGpio *gpio_instance = (FGpio *)param;
  40. rt_kprintf("GPIO-%d-%d, PIN_IRQ_MODE_RISING Interrupt Asserted!!! \r\n",
  41. gpio_instance->config.ctrl,
  42. gpio_instance->config.pin);
  43. }
  44. /* this function will toggle output pin and test intr of input pin */
  45. rt_err_t gpio_toggle_sample()
  46. {
  47. rt_err_t res = RT_EOK;
  48. static u32 set_level = FGPIO_OPS_LEVEL_LOW;
  49. u32 get_level;
  50. #if defined(FIREFLY_DEMO_BOARD)
  51. /* pin init */
  52. input_pin_index = FGPIO_ID(FGPIO_CTRL_0, FGPIO_PIN_0);
  53. rt_pin_mode(input_pin_index, PIN_MODE_INPUT);
  54. rt_pin_attach_irq(input_pin_index, PIN_IRQ_MODE_RISING, gpio_irq_test, RT_NULL);
  55. output_pin_index = FGPIO_ID(FGPIO_CTRL_4, FGPIO_PIN_13);
  56. rt_pin_mode(output_pin_index, PIN_MODE_OUTPUT);
  57. #endif
  58. #if defined(E2000D_DEMO_BOARD)||defined(E2000Q_DEMO_BOARD)
  59. /* pin init */
  60. input_pin_index = FGPIO_ID(FGPIO_CTRL_4, FGPIO_PIN_13);
  61. rt_pin_mode(input_pin_index, PIN_MODE_INPUT);
  62. rt_pin_attach_irq(input_pin_index, PIN_IRQ_MODE_RISING, gpio_irq_test, RT_NULL);
  63. output_pin_index = FGPIO_ID(FGPIO_CTRL_4, FGPIO_PIN_10);
  64. rt_pin_mode(output_pin_index, PIN_MODE_OUTPUT);
  65. #endif
  66. /* toggle operation */
  67. rt_pin_write(output_pin_index, set_level);
  68. set_level = (FGPIO_OPS_LEVEL_LOW == set_level) ? FGPIO_OPS_LEVEL_HIGH : FGPIO_OPS_LEVEL_LOW;
  69. rt_pin_irq_enable(input_pin_index, PIN_IRQ_ENABLE);
  70. rt_pin_write(output_pin_index, set_level);
  71. get_level = rt_pin_read(input_pin_index);
  72. if (set_level != get_level)
  73. {
  74. rt_kprintf(" input level not equals to output level!!!\r\n");
  75. res = RT_ERROR;
  76. goto exit;
  77. }
  78. else
  79. {
  80. rt_kprintf(" ==> Set GPIO-%d-%d to %s \r\n",
  81. FGPIO_GET_CTRL(output_pin_index),
  82. FGPIO_GET_PIN(output_pin_index),
  83. (set_level == FGPIO_PIN_LOW) ? "low" : "high");
  84. rt_kprintf(" <== Get GPIO-%d-%d as %s \r\n",
  85. FGPIO_GET_CTRL(input_pin_index),
  86. FGPIO_GET_PIN(input_pin_index),
  87. (get_level == FGPIO_PIN_LOW) ? "low" : "high");
  88. }
  89. exit:
  90. /* print message on example run result */
  91. if (res == RT_EOK)
  92. {
  93. rt_kprintf("%s@%d:rtthread gpio test example [success].\r\n", __func__, __LINE__);
  94. }
  95. else
  96. {
  97. rt_kprintf("%s@%d:rtthread gpio test example [failure], res = %d\r\n", __func__, __LINE__, res);
  98. }
  99. return res;
  100. }
  101. MSH_CMD_EXPORT(gpio_toggle_sample, FT GPIO toggle sample.);
  102. #endif