test_gpio.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (C) 2022, Canaan Bright Sight Co., Ltd
  3. *
  4. * All enquiries to https://www.canaan-creative.com/
  5. */
  6. /*
  7. * Copyright (c) 2006-2025 RT-Thread Development Team
  8. *
  9. * SPDX-License-Identifier: Apache-2.0
  10. */
  11. /*
  12. * 本测试用例演示基于 01Studio Canmv-K230 开发板测试 gpio 的基本输入和输出功能。其中
  13. * gpio_led_flashing 演示 LED 的跑马灯功能。LED 采用开发板上的 LED2(连接 LED_GPIO52)。
  14. * gpio_io 演示用杜邦线将 40Pin GPIO 中的 GPIO33 和 GPIO32 连接起来,GPIO33 配
  15. * 置为输出,GPIO32 配置为输入。分别用 GPIO33 产生输出给 GPIO32 读取并检查是否
  16. * 和预期的输出一致。
  17. */
  18. #include <unistd.h>
  19. #include <stdio.h>
  20. #include <rtthread.h>
  21. #include <rtdevice.h>
  22. #include <ioremap.h>
  23. #include "board.h"
  24. #include "drv_pinctrl.h"
  25. #include "drv_gpio.h"
  26. #include "utest.h"
  27. #define LED_PIN_NUM 52
  28. #define OUT_PIN_NUM 33
  29. #define IN_PIN_NUM 32
  30. static void gpio_led_flashing(void)
  31. {
  32. int cnt = 5;
  33. /* Use default pinmux function */
  34. kd_pin_mode(LED_PIN_NUM, GPIO_DM_OUTPUT);
  35. while(cnt--)
  36. {
  37. LOG_I("led ON");
  38. kd_pin_write(LED_PIN_NUM, GPIO_PV_HIGH);
  39. rt_thread_mdelay(1000);
  40. LOG_I("led OFF");
  41. kd_pin_write(LED_PIN_NUM, GPIO_PV_LOW);
  42. rt_thread_mdelay(1000);
  43. }
  44. }
  45. static void gpio_io(void)
  46. {
  47. int cnt = 5;
  48. int level = 0xff;
  49. rt_uint32_t val;
  50. /* Set pinmux function */
  51. k230_pinctrl_set_function(IN_PIN_NUM, IOMUX_FUNC1);
  52. k230_pinctrl_set_ie(IN_PIN_NUM, 1);
  53. k230_pinctrl_set_oe(IN_PIN_NUM, 0);
  54. k230_pinctrl_set_function(OUT_PIN_NUM, IOMUX_FUNC1);
  55. k230_pinctrl_set_ie(OUT_PIN_NUM, 0);
  56. k230_pinctrl_set_oe(OUT_PIN_NUM, 1);
  57. /* Set GPIO mode */
  58. kd_pin_mode(OUT_PIN_NUM, GPIO_DM_OUTPUT);
  59. kd_pin_mode(IN_PIN_NUM, GPIO_DM_INPUT);
  60. while(cnt--)
  61. {
  62. kd_pin_write(OUT_PIN_NUM, GPIO_PV_LOW);
  63. level = kd_pin_read(IN_PIN_NUM);
  64. LOG_I("--> %d", level);
  65. uassert_int_equal(level, GPIO_PV_LOW);
  66. kd_pin_write(OUT_PIN_NUM, GPIO_PV_HIGH);
  67. level = kd_pin_read(IN_PIN_NUM);
  68. LOG_I("--> %d", level);
  69. uassert_int_equal(level, GPIO_PV_HIGH);
  70. rt_thread_mdelay(500);
  71. }
  72. }
  73. static void testcase(void)
  74. {
  75. UTEST_UNIT_RUN(gpio_led_flashing);
  76. UTEST_UNIT_RUN(gpio_io);
  77. }
  78. static rt_err_t utest_tc_init(void)
  79. {
  80. return RT_EOK;
  81. }
  82. static rt_err_t utest_tc_cleanup(void)
  83. {
  84. return RT_EOK;
  85. }
  86. UTEST_TC_EXPORT(testcase, "gpio", utest_tc_init, utest_tc_cleanup, 100);