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
13 #include <rtthread.h>
14 #include <rtdevice.h>
15 #include "drv_common.h"
16
17 #if defined(RT_USING_PIN)
18 #include "drv_gpio.h"
19
20 /* defined the LED_0 pin: PN6 */
21 #define LED_0 NU_GET_PININDEX(NU_PN, 6)
22
23 /* defined the LED_1 pin: PN7 */
24 #define LED_1 NU_GET_PININDEX(NU_PN, 7)
25
26 /* defined the LED_2 pin: PN10 */
27 #define LED_2 NU_GET_PININDEX(NU_PN, 10)
28
29 /* defined the KEY_0 pin: PN2 */
30 #define KEY_0 NU_GET_PININDEX(NU_PN, 2)
31
32 /* defined the KEY_1 pin: PN3 */
33 #define KEY_1 NU_GET_PININDEX(NU_PN, 3)
34
35 /* defined the KEY_2 pin: PN12 */
36 #define KEY_2 NU_GET_PININDEX(NU_PN, 12)
37
38 /* defined the KEY_3 pin: PN13 */
39 #define KEY_3 NU_GET_PININDEX(NU_PN, 13)
40
nu_button_cb(void * args)41 void nu_button_cb(void *args)
42 {
43 uint32_t u32Key = (uint32_t)args;
44
45 switch (u32Key)
46 {
47 case KEY_0:
48 rt_kprintf("KEY_0 pressed\n");
49 rt_pin_write(LED_1, PIN_HIGH);
50 break;
51 case KEY_1:
52 rt_kprintf("KEY_1 pressed\n");
53 rt_pin_write(LED_1, PIN_LOW);
54 break;
55 case KEY_2:
56 rt_kprintf("KEY_2 pressed\n");
57 rt_pin_write(LED_2, PIN_HIGH);
58 break;
59 case KEY_3:
60 rt_kprintf("KEY_3 pressed\n");
61 rt_pin_write(LED_2, PIN_LOW);
62 break;
63 }
64 }
65
nu_button_init(void)66 static void nu_button_init(void)
67 {
68 /* set KEY_0 pin mode to input, pull-up, falling edge and enable interrupt. */
69 rt_pin_mode(KEY_0, PIN_MODE_INPUT_PULLUP);
70 rt_pin_attach_irq(KEY_0, PIN_IRQ_MODE_FALLING, nu_button_cb, (void *)KEY_0);
71 rt_pin_irq_enable(KEY_0, PIN_IRQ_ENABLE);
72
73 /* set KEY_1 pin mode to input, pull-up, falling edge and enable interrupt. */
74 rt_pin_mode(KEY_1, PIN_MODE_INPUT_PULLUP);
75 rt_pin_attach_irq(KEY_1, PIN_IRQ_MODE_FALLING, nu_button_cb, (void *)KEY_1);
76 rt_pin_irq_enable(KEY_1, PIN_IRQ_ENABLE);
77
78 /* set KEY_2 pin mode to input, pull-up, falling edge and enable interrupt. */
79 rt_pin_mode(KEY_2, PIN_MODE_INPUT_PULLUP);
80 rt_pin_attach_irq(KEY_2, PIN_IRQ_MODE_FALLING, nu_button_cb, (void *)KEY_2);
81 rt_pin_irq_enable(KEY_2, PIN_IRQ_ENABLE);
82
83 /* set KEY_3 pin mode to input, pull-up, falling edge and enable interrupt. */
84 rt_pin_mode(KEY_3, PIN_MODE_INPUT_PULLUP);
85 rt_pin_attach_irq(KEY_3, PIN_IRQ_MODE_FALLING, nu_button_cb, (void *)KEY_3);
86 rt_pin_irq_enable(KEY_3, PIN_IRQ_ENABLE);
87 }
88
89
main(int argc,char ** argv)90 int main(int argc, char **argv)
91 {
92 int counter = 100000;
93
94 /* set on-board buttons */
95 nu_button_init();
96
97 /* set LED_0, LED_1 and LED_2 pin mode to output */
98 rt_pin_mode(LED_0, PIN_MODE_OUTPUT);
99 rt_pin_mode(LED_1, PIN_MODE_OUTPUT);
100 rt_pin_mode(LED_2, PIN_MODE_OUTPUT);
101
102 while (counter--)
103 {
104 rt_pin_write(LED_0, PIN_HIGH);
105 rt_thread_mdelay(100);
106 rt_pin_write(LED_0, PIN_LOW);
107 rt_thread_mdelay(100);
108 }
109
110 return 0;
111 }
112
113 #else
114
main(int argc,char ** argv)115 int main(int argc, char **argv)
116 {
117 rt_kprintf("cpu-%d %d\r\n", rt_hw_cpu_id(), nu_cpu_dcache_line_size());
118 return 0;
119 }
120
121 #endif
122