main.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2021-6-1 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include "drv_common.h"
  15. #if defined(RT_USING_PIN)
  16. #include "drv_gpio.h"
  17. /* defined the LED_0 pin: PN6 */
  18. #define LED_0 NU_GET_PININDEX(NU_PN, 6)
  19. /* defined the LED_1 pin: PN7 */
  20. #define LED_1 NU_GET_PININDEX(NU_PN, 7)
  21. /* defined the LED_2 pin: PN10 */
  22. #define LED_2 NU_GET_PININDEX(NU_PN, 10)
  23. /* defined the KEY_0 pin: PN2 */
  24. #define KEY_0 NU_GET_PININDEX(NU_PN, 2)
  25. /* defined the KEY_1 pin: PN3 */
  26. #define KEY_1 NU_GET_PININDEX(NU_PN, 3)
  27. /* defined the KEY_2 pin: PN12 */
  28. #define KEY_2 NU_GET_PININDEX(NU_PN, 12)
  29. /* defined the KEY_3 pin: PN13 */
  30. #define KEY_3 NU_GET_PININDEX(NU_PN, 13)
  31. void nu_button_cb(void *args)
  32. {
  33. uint32_t u32Key = (uint32_t)args;
  34. switch (u32Key)
  35. {
  36. case KEY_0:
  37. rt_kprintf("KEY_0 pressed\n");
  38. rt_pin_write(LED_1, PIN_HIGH);
  39. break;
  40. case KEY_1:
  41. rt_kprintf("KEY_1 pressed\n");
  42. rt_pin_write(LED_1, PIN_LOW);
  43. break;
  44. case KEY_2:
  45. rt_kprintf("KEY_2 pressed\n");
  46. rt_pin_write(LED_2, PIN_HIGH);
  47. break;
  48. case KEY_3:
  49. rt_kprintf("KEY_3 pressed\n");
  50. rt_pin_write(LED_2, PIN_LOW);
  51. break;
  52. }
  53. }
  54. static void nu_button_init(void)
  55. {
  56. /* set KEY_0 pin mode to input, pull-up, falling edge and enable interrupt. */
  57. rt_pin_mode(KEY_0, PIN_MODE_INPUT_PULLUP);
  58. rt_pin_attach_irq(KEY_0, PIN_IRQ_MODE_FALLING, nu_button_cb, (void *)KEY_0);
  59. rt_pin_irq_enable(KEY_0, PIN_IRQ_ENABLE);
  60. /* set KEY_1 pin mode to input, pull-up, falling edge and enable interrupt. */
  61. rt_pin_mode(KEY_1, PIN_MODE_INPUT_PULLUP);
  62. rt_pin_attach_irq(KEY_1, PIN_IRQ_MODE_FALLING, nu_button_cb, (void *)KEY_1);
  63. rt_pin_irq_enable(KEY_1, PIN_IRQ_ENABLE);
  64. /* set KEY_2 pin mode to input, pull-up, falling edge and enable interrupt. */
  65. rt_pin_mode(KEY_2, PIN_MODE_INPUT_PULLUP);
  66. rt_pin_attach_irq(KEY_2, PIN_IRQ_MODE_FALLING, nu_button_cb, (void *)KEY_2);
  67. rt_pin_irq_enable(KEY_2, PIN_IRQ_ENABLE);
  68. /* set KEY_3 pin mode to input, pull-up, falling edge and enable interrupt. */
  69. rt_pin_mode(KEY_3, PIN_MODE_INPUT_PULLUP);
  70. rt_pin_attach_irq(KEY_3, PIN_IRQ_MODE_FALLING, nu_button_cb, (void *)KEY_3);
  71. rt_pin_irq_enable(KEY_3, PIN_IRQ_ENABLE);
  72. }
  73. int main(int argc, char **argv)
  74. {
  75. int counter = 100000;
  76. /* set on-board buttons */
  77. nu_button_init();
  78. /* set LED_0, LED_1 and LED_2 pin mode to output */
  79. rt_pin_mode(LED_0, PIN_MODE_OUTPUT);
  80. rt_pin_mode(LED_1, PIN_MODE_OUTPUT);
  81. rt_pin_mode(LED_2, PIN_MODE_OUTPUT);
  82. while (counter--)
  83. {
  84. rt_pin_write(LED_0, PIN_HIGH);
  85. rt_thread_mdelay(100);
  86. rt_pin_write(LED_0, PIN_LOW);
  87. rt_thread_mdelay(100);
  88. }
  89. return 0;
  90. }
  91. #else
  92. int main(int argc, char **argv)
  93. {
  94. rt_kprintf("cpu-%d %d\r\n", rt_hw_cpu_id(), nu_cpu_dcache_line_size());
  95. return 0;
  96. }
  97. #endif