pbtest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Copyright (C) 2009 by Ulrich Prinz, <uprinz2@netscape.net>
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * $Id$
  36. */
  37. /*!
  38. * \example buttons/pbtest.c
  39. *
  40. * Push button driver example.
  41. *
  42. * This sample demonstrates Nut/OS LEd and key handling.
  43. * It is written for the ATMEL SAM7X-EK but can be adapted to any other
  44. * hardware within minutes.
  45. *
  46. */
  47. #include <stdio.h>
  48. #include <io.h>
  49. #include <cfg/arch.h>
  50. #include <cfg/dev.h>
  51. #include <dev/board.h>
  52. #include <sys/timer.h>
  53. #include <sys/event.h>
  54. #include <sys/thread.h>
  55. #include <dev/gpio.h>
  56. #include <dev/keys.h>
  57. #include <dev/led.h>
  58. /* This sample application only supports the following boards */
  59. #if defined(AT91SAM7X_EK) || defined(ENET_SAM7X) || \
  60. defined(EVK1100) || defined(EVK1101) || defined(EVK1105) || \
  61. defined(STM3210C_EVAL) || defined(STM3210E_EVAL)
  62. #if defined(AT91SAM7X_EK) || defined(ENET_SAM7X)
  63. #define LED1_PORT NUTGPIO_PORTB
  64. #define LED1_PIN 19
  65. #define LED2_PORT NUTGPIO_PORTB
  66. #define LED2_PIN 20
  67. #define LED3_PORT NUTGPIO_PORTB
  68. #define LED3_PIN 21
  69. #define LED4_PORT NUTGPIO_PORTB
  70. #define LED4_PIN 22
  71. #define KEY_MI_PORT NUTGPIO_PORTA
  72. #define KEY_MI_PIN 25
  73. #define KEY_DN_PORT NUTGPIO_PORTA
  74. #define KEY_DN_PIN 22
  75. #define KEY_LT_PORT NUTGPIO_PORTA
  76. #define KEY_LT_PIN 23
  77. #define KEY_RT_PORT NUTGPIO_PORTA
  78. #define KEY_RT_PIN 24
  79. #define KEY_UP_PORT NUTGPIO_PORTA
  80. #define KEY_UP_PIN 21
  81. #elif defined(EVK1100)
  82. #define LED1_PORT NUTGPIO_PORTB
  83. #define LED1_PIN 27
  84. #define LED2_PORT NUTGPIO_PORTB
  85. #define LED2_PIN 28
  86. #define LED3_PORT NUTGPIO_PORTB
  87. #define LED3_PIN 29
  88. #define LED4_PORT NUTGPIO_PORTB
  89. #define LED4_PIN 30
  90. #define KEY_MI_PORT NUTGPIO_PORTA
  91. #define KEY_MI_PIN 20
  92. #define KEY_DN_PORT NUTGPIO_PORTA
  93. #define KEY_DN_PIN 27
  94. #define KEY_LT_PORT NUTGPIO_PORTA
  95. #define KEY_LT_PIN 25
  96. #define KEY_RT_PORT NUTGPIO_PORTA
  97. #define KEY_RT_PIN 28
  98. #define KEY_UP_PORT NUTGPIO_PORTA
  99. #define KEY_UP_PIN 26
  100. #elif defined(EVK1101)
  101. #define LED1_PORT NUTGPIO_PORTA
  102. #define LED1_PIN 7
  103. #define LED2_PORT NUTGPIO_PORTA
  104. #define LED2_PIN 8
  105. #define LED3_PORT NUTGPIO_PORTA
  106. #define LED3_PIN 21
  107. #define LED4_PORT NUTGPIO_PORTA
  108. #define LED4_PIN 22
  109. #define KEY_MI_PORT NUTGPIO_PORTA
  110. #define KEY_MI_PIN 13
  111. #define KEY_DN_PORT NUTGPIO_PORTB
  112. #define KEY_DN_PIN 8
  113. #define KEY_LT_PORT NUTGPIO_PORTB
  114. #define KEY_LT_PIN 6
  115. #define KEY_RT_PORT NUTGPIO_PORTB
  116. #define KEY_RT_PIN 9
  117. #define KEY_UP_PORT NUTGPIO_PORTB
  118. #define KEY_UP_PIN 7
  119. #elif defined(EVK1105)
  120. #define LED1_PORT NUTGPIO_PORTB
  121. #define LED1_PIN 27
  122. #define LED2_PORT NUTGPIO_PORTB
  123. #define LED2_PIN 28
  124. #define LED3_PORT NUTGPIO_PORTA
  125. #define LED3_PIN 5
  126. #define LED4_PORT NUTGPIO_PORTA
  127. #define LED4_PIN 6
  128. #define KEY_MI_PORT NUTGPIO_PORTB
  129. #define KEY_MI_PIN 26
  130. #define KEY_DN_PORT NUTGPIO_PORTB
  131. #define KEY_DN_PIN 23
  132. #define KEY_LT_PORT NUTGPIO_PORTB
  133. #define KEY_LT_PIN 25
  134. #define KEY_RT_PORT NUTGPIO_PORTB
  135. #define KEY_RT_PIN 24
  136. #define KEY_UP_PORT NUTGPIO_PORTB
  137. #define KEY_UP_PIN 22
  138. #elif defined(STM3210C_EVAL)
  139. #define LED1_PORT NUTGPIO_PORTD
  140. #define LED1_PIN 7
  141. #define LED2_PORT NUTGPIO_PORTD
  142. #define LED2_PIN 13
  143. #define LED3_PORT NUTGPIO_PORTD
  144. #define LED3_PIN 3
  145. #define LED4_PORT NUTGPIO_PORTD
  146. #define LED4_PIN 4
  147. /* For STM3210C-EVAL Kit the 5-way controller is connected to
  148. * a I2C expander. So we use the few single buttons instead.
  149. */
  150. #define KEY_MI_PORT NUTGPIO_PORTB /* User Button */
  151. #define KEY_MI_PIN 9
  152. #define KEY_DN_PORT NUTGPIO_PORTC /* Tamper Button */
  153. #define KEY_DN_PIN 13
  154. #define KEY_LT_PORT NUTGPIO_PORTA /* Wakeup Button */
  155. #define KEY_LT_PIN 0
  156. //#define KEY_RT_PORT NUTGPIO_PORTB
  157. //#define KEY_RT_PIN 24
  158. //#define KEY_UP_PORT NUTGPIO_PORTB
  159. //#define KEY_UP_PIN 22
  160. #elif defined(STM3210E_EVAL)
  161. #define LED1_PORT NUTGPIO_PORTF
  162. #define LED1_PIN 6
  163. #define LED2_PORT NUTGPIO_PORTF
  164. #define LED2_PIN 7
  165. #define LED3_PORT NUTGPIO_PORTF
  166. #define LED3_PIN 8
  167. #define LED4_PORT NUTGPIO_PORTF
  168. #define LED4_PIN 9
  169. #define KEY_MI_PORT NUTGPIO_PORTG
  170. #define KEY_MI_PIN 7
  171. #define KEY_DN_PORT NUTGPIO_PORTD
  172. #define KEY_DN_PIN 3
  173. #define KEY_LT_PORT NUTGPIO_PORTG
  174. #define KEY_LT_PIN 14
  175. #define KEY_RT_PORT NUTGPIO_PORTG
  176. #define KEY_RT_PIN 13
  177. #define KEY_UP_PORT NUTGPIO_PORTG
  178. #define KEY_UP_PIN 15
  179. #endif
  180. /* Handles for the LEDs */
  181. HANDLE led1, led2, led3, led4;
  182. /* Handles for the keys / joystick of the SAM7X-EK */
  183. HANDLE keyUp, keyDn, keyLt, keyRt, keyMi;
  184. /* Handles for two threads controlled by the keys */
  185. HANDLE keyT1w, keyT2w;
  186. /*!
  187. * \brief First thread responding to pressed keys.
  188. *
  189. * This thread handles some of the keys of this example.
  190. * In response to a key it gives some text on the debug consle
  191. * and alters the behaviour of the assigned LEDs.
  192. *
  193. */
  194. THREAD(Key1Thread, arg)
  195. {
  196. NutThreadSetPriority(60);
  197. for (;;) {
  198. NutEventWait(&keyT1w, NUT_WAIT_INFINITE);
  199. if (NutGetKeyState(keyMi) & KEY_PENDING) {
  200. printf("KEY ENTER pressed\n");
  201. NutSetLed(led2, LED_OFF, 0, 0);
  202. NutSetLed(led3, LED_OFF, 0, 0);
  203. NutSetLed(led4, LED_OFF, 0, 0);
  204. }
  205. if (NutGetKeyState(keyDn) & KEY_PENDING) {
  206. printf("\nKEY DOWN pressed\n");
  207. NutSetLed(led2, LED_BLINK, 100, 900);
  208. NutSetLed(led3, LED_BLINK, 500, 500);
  209. NutSetLed(led4, LED_BLINK, 900, 100);
  210. }
  211. }
  212. }
  213. /*!
  214. * \brief Second0 thread responding to pressed keys.
  215. *
  216. * This thread handles some of the keys of this example.
  217. * In response to a key it gives some text on the debug consle
  218. * and alters the behaviour of the assigned LEDs.
  219. *
  220. */
  221. THREAD(Key2Thread, arg)
  222. {
  223. NutThreadSetPriority(60);
  224. for (;;) {
  225. NutEventWait(&keyT2w, NUT_WAIT_INFINITE);
  226. if (NutGetKeyState(keyLt) & KEY_PENDING) {
  227. printf("\nKEY LEFT pressed\n");
  228. NutSetLed(led2, LED_ON, 200, 0);
  229. NutSetLed(led4, LED_OFF, 0, 0);
  230. }
  231. if (NutGetKeyState(keyRt) & KEY_PENDING) {
  232. printf("\nKEY RIGHT pressed\n");
  233. NutSetLed(led2, LED_OFF, 0, 0);
  234. NutSetLed(led4, LED_ON, 200, 0);
  235. }
  236. if (NutGetKeyState(keyUp) & KEY_PENDING) {
  237. printf("\nKEY UP pressed\n");
  238. NutSetLed(led3, LED_FLIP, 0, 0);
  239. }
  240. }
  241. }
  242. /*
  243. * Main application thread.
  244. */
  245. int main(void)
  246. {
  247. uint32_t baud = 115200;
  248. /*
  249. * Register the UART device, open it, assign stdout to it and set
  250. * the baudrate.
  251. */
  252. NutRegisterDevice(&DEV_DEBUG, 0, 0);
  253. freopen(DEV_DEBUG_NAME, "w", stdout);
  254. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  255. #if defined(EVK1104)
  256. puts("\n*** Sorry, LED and key Test not supported by the EVK1104 ***\n");
  257. fflush(stdout);
  258. #else
  259. puts("\n*** LED and key Test ***\n");
  260. fflush(stdout);
  261. /* Register LED1 as blinking led */
  262. NutRegisterLed(&led1, LED1_PORT, LED1_PIN);
  263. NutSetLed(led1, LED_BLINK, 100, 900);
  264. /* Register LED2 and let it flash once for 200ms */
  265. NutRegisterLed(&led2, LED2_PORT, LED2_PIN);
  266. NutSetLed(led2, LED_ON, 200, 0);
  267. /* Register LED3 and let it flash once for 200ms */
  268. NutRegisterLed(&led3, LED3_PORT, LED3_PIN);
  269. NutSetLed(led3, LED_ON, 200, 0);
  270. /* Register LED4 and let it flash once for 200ms */
  271. NutRegisterLed(&led4, LED4_PORT, LED4_PIN);
  272. NutSetLed(led4, LED_ON, 200, 0);
  273. /* Register keys for thread */
  274. /* First we register middle function of the 5-way control of the SAM7X-EK
  275. * to do something if it is pressed for less than 1s.
  276. */
  277. NutRegisterKey(&keyMi, KEY_MI_PORT, KEY_MI_PIN, KEY_ACTION_SHORT, 1000);
  278. /* Then we assign, what to do if the above event has happened
  279. * Here we assign to release a handle.
  280. */
  281. NutAssignKeyEvt(keyMi, &keyT1w);
  282. /* Second example is to register the down function of the 5-way controller
  283. * to a different functionality, it executes something after beeing pressed
  284. * for at least 2s.
  285. */
  286. #if !defined(EVK1105)
  287. NutRegisterKey(&keyDn, KEY_DN_PORT, KEY_DN_PIN, KEY_ACTION_HOLD, 2000);
  288. #else
  289. /* The EVK1105 have touch keys, therefore we will not wait 2s here */
  290. NutRegisterKey(&keyDn, KEY_DN_PORT, KEY_DN_PIN, KEY_ACTION_UP, 0);
  291. #endif
  292. /* Assign event to keyDn too.
  293. */
  294. NutAssignKeyEvt(keyDn, &keyT1w);
  295. /* Register keys for thread 2 mutex.
  296. * We assign two of the keys to release a mutex.
  297. */
  298. #ifdef KEY_LT_PORT
  299. NutRegisterKey(&keyLt, KEY_LT_PORT, KEY_LT_PIN, KEY_ACTION_UP, 0);
  300. NutAssignKeyEvt(keyLt, &keyT2w);
  301. #endif
  302. #ifdef KEY_RT_PORT
  303. NutRegisterKey(&keyRt, KEY_RT_PORT, KEY_RT_PIN, KEY_ACTION_DOWN, 0);
  304. NutAssignKeyEvt(keyRt, &keyT2w);
  305. #endif
  306. #ifdef KEY_UP_PORT
  307. NutRegisterKey(&keyUp, KEY_UP_PORT, KEY_UP_PIN, KEY_ACTION_DOWN, 0);
  308. NutAssignKeyEvt(keyUp, &keyT2w);
  309. #endif
  310. /* Register threads that wait on keys */
  311. NutThreadCreate("k1", Key1Thread, NULL, 256);
  312. NutThreadCreate("k2", Key2Thread, NULL, 256);
  313. #endif
  314. /*
  315. * Endless loop in main thread.
  316. */
  317. for (;;) {
  318. putchar('.');
  319. NutSleep(2000);
  320. }
  321. return 0;
  322. }
  323. #else /* defined(AT91SAM7X_EK) || defined(ENET_SAM7X) ||
  324. defined(EVK1100) || defined(EVK1101) || defined(EVK1105) */
  325. int main(void)
  326. {
  327. uint32_t baud = 115200;
  328. /*
  329. * Register the UART device, open it, assign stdout to it and set
  330. * the baudrate.
  331. */
  332. NutRegisterDevice(&DEV_DEBUG, 0, 0);
  333. freopen(DEV_DEBUG_NAME, "w", stdout);
  334. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  335. printf("Sorry, your board is not supported \n");
  336. while(1);
  337. }
  338. #endif