00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00050 #include <stdio.h>
00051 #include <io.h>
00052
00053 #include <cfg/arch.h>
00054 #include <cfg/dev.h>
00055 #include <dev/board.h>
00056 #include <sys/timer.h>
00057 #include <sys/event.h>
00058 #include <sys/thread.h>
00059
00060 #include <dev/gpio.h>
00061
00062 #include <dev/keys.h>
00063 #include <dev/led.h>
00064
00065 #if defined(AT91SAM7X_EK)
00066 #define LED1_PORT NUTGPIO_PORTB
00067 #define LED1_PIN 19
00068 #define LED2_PORT NUTGPIO_PORTB
00069 #define LED2_PIN 20
00070 #define LED3_PORT NUTGPIO_PORTB
00071 #define LED3_PIN 21
00072 #define LED4_PORT NUTGPIO_PORTB
00073 #define LED4_PIN 22
00074
00075 #define KEY_MI_PORT NUTGPIO_PORTA
00076 #define KEY_MI_PIN 25
00077 #define KEY_DN_PORT NUTGPIO_PORTA
00078 #define KEY_DN_PIN 22
00079 #define KEY_LT_PORT NUTGPIO_PORTA
00080 #define KEY_LT_PIN 23
00081 #define KEY_RT_PORT NUTGPIO_PORTA
00082 #define KEY_RT_PIN 24
00083 #define KEY_UP_PORT NUTGPIO_PORTA
00084 #define KEY_UP_PIN 21
00085 #elif defined(EVK1100)
00086 #define LED1_PORT NUTGPIO_PORTB
00087 #define LED1_PIN 27
00088 #define LED2_PORT NUTGPIO_PORTB
00089 #define LED2_PIN 28
00090 #define LED3_PORT NUTGPIO_PORTB
00091 #define LED3_PIN 29
00092 #define LED4_PORT NUTGPIO_PORTB
00093 #define LED4_PIN 30
00094
00095 #define KEY_MI_PORT NUTGPIO_PORTA
00096 #define KEY_MI_PIN 20
00097 #define KEY_DN_PORT NUTGPIO_PORTA
00098 #define KEY_DN_PIN 27
00099 #define KEY_LT_PORT NUTGPIO_PORTA
00100 #define KEY_LT_PIN 25
00101 #define KEY_RT_PORT NUTGPIO_PORTA
00102 #define KEY_RT_PIN 28
00103 #define KEY_UP_PORT NUTGPIO_PORTA
00104 #define KEY_UP_PIN 26
00105
00106 #elif defined(EVK1101)
00107 #define LED1_PORT NUTGPIO_PORTA
00108 #define LED1_PIN 7
00109 #define LED2_PORT NUTGPIO_PORTA
00110 #define LED2_PIN 8
00111 #define LED3_PORT NUTGPIO_PORTA
00112 #define LED3_PIN 21
00113 #define LED4_PORT NUTGPIO_PORTA
00114 #define LED4_PIN 22
00115
00116 #define KEY_MI_PORT NUTGPIO_PORTA
00117 #define KEY_MI_PIN 13
00118 #define KEY_DN_PORT NUTGPIO_PORTB
00119 #define KEY_DN_PIN 8
00120 #define KEY_LT_PORT NUTGPIO_PORTB
00121 #define KEY_LT_PIN 6
00122 #define KEY_RT_PORT NUTGPIO_PORTB
00123 #define KEY_RT_PIN 9
00124 #define KEY_UP_PORT NUTGPIO_PORTB
00125 #define KEY_UP_PIN 7
00126
00127 #elif defined(EVK1105)
00128 #define LED1_PORT NUTGPIO_PORTB
00129 #define LED1_PIN 27
00130 #define LED2_PORT NUTGPIO_PORTB
00131 #define LED2_PIN 28
00132 #define LED3_PORT NUTGPIO_PORTA
00133 #define LED3_PIN 5
00134 #define LED4_PORT NUTGPIO_PORTA
00135 #define LED4_PIN 6
00136
00137 #define KEY_MI_PORT NUTGPIO_PORTB
00138 #define KEY_MI_PIN 26
00139 #define KEY_DN_PORT NUTGPIO_PORTB
00140 #define KEY_DN_PIN 23
00141 #define KEY_LT_PORT NUTGPIO_PORTB
00142 #define KEY_LT_PIN 25
00143 #define KEY_RT_PORT NUTGPIO_PORTB
00144 #define KEY_RT_PIN 24
00145 #define KEY_UP_PORT NUTGPIO_PORTB
00146 #define KEY_UP_PIN 22
00147 #endif
00148
00149
00150
00151
00152
00153
00154 HANDLE led1, led2, led3, led4;
00155
00156 HANDLE keyUp, keyDn, keyLt, keyRt, keyMi;
00157
00158 HANDLE keyT1w, keyT2w;
00159
00168 THREAD(Key1Thread, arg)
00169 {
00170 NutThreadSetPriority(60);
00171 for (;;) {
00172 NutEventWait(&keyT1w, NUT_WAIT_INFINITE);
00173 if (NutGetKeyState(keyMi) & KEY_PENDING) {
00174 printf("KEY ENTER pressed\n");
00175 NutSetLed(led2, LED_OFF, 0, 0);
00176 NutSetLed(led3, LED_OFF, 0, 0);
00177 NutSetLed(led4, LED_OFF, 0, 0);
00178 }
00179 if (NutGetKeyState(keyDn) & KEY_PENDING) {
00180 printf("\nKEY DOWN pressed\n");
00181 NutSetLed(led2, LED_BLINK, 100, 900);
00182 NutSetLed(led3, LED_BLINK, 500, 500);
00183 NutSetLed(led4, LED_BLINK, 900, 100);
00184 }
00185 }
00186 }
00187
00196 THREAD(Key2Thread, arg)
00197 {
00198 NutThreadSetPriority(60);
00199 for (;;) {
00200 NutEventWait(&keyT2w, NUT_WAIT_INFINITE);
00201 if (NutGetKeyState(keyLt) & KEY_PENDING) {
00202 printf("\nKEY LEFT pressed\n");
00203 NutSetLed(led2, LED_ON, 200, 0);
00204 NutSetLed(led4, LED_OFF, 0, 0);
00205 }
00206 if (NutGetKeyState(keyRt) & KEY_PENDING) {
00207 printf("\nKEY RIGHT pressed\n");
00208 NutSetLed(led2, LED_OFF, 0, 0);
00209 NutSetLed(led4, LED_ON, 200, 0);
00210 }
00211 if (NutGetKeyState(keyUp) & KEY_PENDING) {
00212 printf("\nKEY UP pressed\n");
00213 NutSetLed(led3, LED_FLIP, 0, 0);
00214 }
00215 }
00216 }
00217
00218
00219
00220
00221
00222 int main(void)
00223 {
00224 uint32_t baud = 115200;
00225
00226
00227
00228
00229 NutRegisterDevice(&DEV_DEBUG, 0, 0);
00230 freopen(DEV_DEBUG_NAME, "w", stdout);
00231 _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00232
00233 #if defined(EVK1104)
00234 puts("\n*** Sorry, LED and key Test not supported by the EVK1104 ***\n");
00235 fflush(stdout);
00236 #else
00237 puts("\n*** LED and key Test ***\n");
00238 fflush(stdout);
00239
00240
00241 NutRegisterLed(&led1, LED1_PORT, LED1_PIN);
00242 NutSetLed(led1, LED_BLINK, 100, 900);
00243
00244
00245 NutRegisterLed(&led2, LED2_PORT, LED2_PIN);
00246 NutSetLed(led2, LED_ON, 200, 0);
00247
00248
00249 NutRegisterLed(&led3, LED3_PORT, LED3_PIN);
00250 NutSetLed(led3, LED_ON, 200, 0);
00251
00252
00253 NutRegisterLed(&led4, LED4_PORT, LED4_PIN);
00254 NutSetLed(led4, LED_ON, 200, 0);
00255
00256
00257
00258
00259
00260
00261 NutRegisterKey(&keyMi, KEY_MI_PORT, KEY_MI_PIN, KEY_ACTION_SHORT, 1000);
00262
00263
00264
00265
00266 NutAssignKeyEvt(keyMi, &keyT1w);
00267
00268
00269
00270
00271
00272 #if !defined(EVK1105)
00273 NutRegisterKey(&keyDn, KEY_DN_PORT, KEY_DN_PIN, KEY_ACTION_HOLD, 2000);
00274 #else
00275
00276 NutRegisterKey(&keyDn, KEY_DN_PORT, KEY_DN_PIN, KEY_ACTION_UP, 0);
00277 #endif
00278
00279
00280
00281
00282 NutAssignKeyEvt(keyDn, &keyT1w);
00283
00284
00285
00286 NutRegisterKey(&keyLt, KEY_LT_PORT, KEY_LT_PIN, KEY_ACTION_UP, 0);
00287 NutRegisterKey(&keyRt, KEY_RT_PORT, KEY_RT_PIN, KEY_ACTION_DOWN, 0);
00288 NutRegisterKey(&keyUp, KEY_UP_PORT, KEY_UP_PIN, KEY_ACTION_DOWN, 0);
00289
00290
00291
00292 NutAssignKeyEvt(keyLt, &keyT2w);
00293 NutAssignKeyEvt(keyRt, &keyT2w);
00294 NutAssignKeyEvt(keyUp, &keyT2w);
00295
00296
00297 NutThreadCreate("k1", Key1Thread, NULL, 256);
00298 NutThreadCreate("k2", Key2Thread, NULL, 256);
00299 #endif
00300
00301
00302
00303
00304 for (;;) {
00305 putchar('.');
00306 NutSleep(2000);
00307 }
00308 return 0;
00309 }