dev_keys.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /***************************************************************************//**
  2. * @file dev_keys.c
  3. * @brief Keys driver of RT-Thread RTOS for EFM32
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. * @author onelife
  6. * @version 0.4 beta
  7. *******************************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file
  10. * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
  11. *******************************************************************************
  12. * @section Change Logs
  13. * Date Author Notes
  14. * 2011-12-29 onelife Initial creation for EFM32GG_DK3750 board
  15. ******************************************************************************/
  16. /***************************************************************************//**
  17. * @addtogroup EFM32GG_DK3750
  18. * @{
  19. ******************************************************************************/
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "board.h"
  22. #include "hdl_interrupt.h"
  23. #include "dev_keys.h"
  24. #if defined(EFM32_USING_KEYS)
  25. #include <rtgui/event.h>
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* Private define ------------------------------------------------------------*/
  28. /* Private macro -------------------------------------------------------------*/
  29. #ifdef EFM32_KEYS_DEBUG
  30. #define keys_debug(format,args...) rt_kprintf(format, ##args)
  31. #else
  32. #define keys_debug(format,args...)
  33. #endif
  34. /* Private function prototypes -----------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. static struct efm32_joy_device joy;
  37. static struct rt_device joy_dev;
  38. static struct rtgui_event_mouse mouse;
  39. static rt_bool_t click;
  40. /* Private functions ---------------------------------------------------------*/
  41. /***************************************************************************//**
  42. * @brief
  43. * Keys interrupt handler
  44. *
  45. * @details
  46. *
  47. * @note
  48. *
  49. * @param[in] device
  50. * Pointer to device descriptor
  51. ******************************************************************************/
  52. static void efm32_keys_isr(rt_device_t dev)
  53. {
  54. rt_uint16_t flag, joystick;
  55. /* Clear DEK interrupt */
  56. flag = DVK_getInterruptFlags();
  57. DVK_clearInterruptFlags(flag);
  58. if (flag & BC_INTFLAG_PB)
  59. {
  60. }
  61. if (flag & BC_INTFLAG_DIP)
  62. {
  63. }
  64. if (flag & BC_INTFLAG_JOYSTICK)
  65. {
  66. joystick = DVK_getJoystick();
  67. keys_debug("Keys: joystick %x\n", joystick);
  68. #ifdef RT_USING_RTGUI
  69. switch (joystick)
  70. {
  71. case BC_UIF_JOYSTICK_RIGHT:
  72. joy.x += 2;
  73. if (joy.x > joy.max_x)
  74. {
  75. joy.x = joy.max_x;
  76. }
  77. break;
  78. case BC_UIF_JOYSTICK_LEFT:
  79. joy.x -= 2;
  80. if (joy.x < joy.min_x)
  81. {
  82. joy.x = joy.min_x;
  83. }
  84. break;
  85. case BC_UIF_JOYSTICK_DOWN:
  86. joy.y += 2;
  87. if (joy.y > joy.max_y)
  88. {
  89. joy.y = joy.max_y;
  90. }
  91. break;
  92. case BC_UIF_JOYSTICK_UP:
  93. joy.y -= 2;
  94. if (joy.y < joy.min_y)
  95. {
  96. joy.y = joy.min_y;
  97. }
  98. break;
  99. case BC_UIF_JOYSTICK_CENTER:
  100. break;
  101. default:
  102. break;
  103. }
  104. #endif
  105. if (joystick)
  106. {
  107. if (joystick != BC_UIF_JOYSTICK_CENTER)
  108. {
  109. mouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  110. mouse.x = joy.x;
  111. mouse.y = joy.y;
  112. rtgui_server_post_event((&mouse.parent), sizeof(mouse));
  113. rt_timer_start(&joy.timer);
  114. }
  115. else
  116. {
  117. click = RT_TRUE;
  118. mouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  119. mouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN;
  120. rtgui_server_post_event((&mouse.parent), sizeof(mouse));
  121. }
  122. }
  123. else
  124. {
  125. if (click)
  126. {
  127. click = RT_FALSE;
  128. mouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  129. mouse.button = RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP;
  130. rtgui_server_post_event((&mouse.parent), sizeof(mouse));
  131. }
  132. else
  133. {
  134. rt_timer_stop(&joy.timer);
  135. }
  136. }
  137. }
  138. if (flag & BC_INTFLAG_AEM)
  139. {
  140. }
  141. }
  142. /***************************************************************************//**
  143. * @brief
  144. * Keys timeout handler
  145. *
  146. * @details
  147. *
  148. * @note
  149. *
  150. * @param[in] param
  151. * Parameter
  152. ******************************************************************************/
  153. static void efm32_keys_timer_isr(void *param)
  154. {
  155. rt_uint16_t joystick;
  156. joystick = DVK_getJoystick();
  157. #ifdef RT_USING_RTGUI
  158. switch (joystick)
  159. {
  160. case BC_UIF_JOYSTICK_RIGHT:
  161. joy.x += 2;
  162. if (joy.x > joy.max_x)
  163. {
  164. joy.x = joy.max_x;
  165. }
  166. break;
  167. case BC_UIF_JOYSTICK_LEFT:
  168. joy.x -= 2;
  169. if (joy.x < joy.min_x)
  170. {
  171. joy.x = joy.min_x;
  172. }
  173. break;
  174. case BC_UIF_JOYSTICK_DOWN:
  175. joy.y += 2;
  176. if (joy.y > joy.max_y)
  177. {
  178. joy.y = joy.max_y;
  179. }
  180. break;
  181. case BC_UIF_JOYSTICK_UP:
  182. joy.y -= 2;
  183. if (joy.y < joy.min_y)
  184. {
  185. joy.y = joy.min_y;
  186. }
  187. break;
  188. }
  189. if (joystick)
  190. {
  191. mouse.parent.type = RTGUI_EVENT_MOUSE_MOTION;
  192. mouse.x = joy.x;
  193. mouse.y = joy.y;
  194. rtgui_server_post_event((&mouse.parent), sizeof(mouse));
  195. }
  196. #endif
  197. }
  198. /***************************************************************************//**
  199. * @brief
  200. * Initialize keys device
  201. *
  202. * @details
  203. *
  204. * @note
  205. *
  206. * @param[in] dev
  207. * Pointer to device descriptor
  208. *
  209. * @return
  210. * Error code
  211. ******************************************************************************/
  212. static rt_err_t efm32_keys_init (rt_device_t dev)
  213. {
  214. struct rt_device_graphic_info lcd_info;
  215. rt_device_t lcd;
  216. lcd = rt_device_find(LCD_DEVICE_NAME);
  217. if (lcd == RT_NULL)
  218. {
  219. keys_debug("Keys err: Can't find LCD\n");
  220. return -RT_ERROR;
  221. }
  222. lcd->control(lcd, RTGRAPHIC_CTRL_GET_INFO, (void *)&lcd_info);
  223. click = RT_FALSE;
  224. joy.x = 0;
  225. joy.y = 0;
  226. joy.min_x = 0;
  227. joy.max_x = lcd_info.width;
  228. joy.min_y = 0;
  229. joy.max_y = lcd_info.height;
  230. mouse.parent.sender = RT_NULL;
  231. mouse.wid = RT_NULL;
  232. mouse.button = 0;
  233. return RT_EOK;
  234. }
  235. /***************************************************************************//**
  236. * @brief
  237. * Initialize keys related hardware and register joystic device to kernel
  238. *
  239. * @details
  240. *
  241. * @note
  242. *
  243. ******************************************************************************/
  244. void efm32_hw_keys_init(void)
  245. {
  246. /* Configure joystick interrupt pin */
  247. GPIO_PinModeSet(KEYS_INT_PORT, KEYS_INT_PIN, gpioModeInputPullFilter, 1);
  248. /* Enable joystick interrupt */
  249. GPIO_IntConfig(KEYS_INT_PORT, KEYS_INT_PIN, true, true, true);
  250. efm32_irq_hook_init_t hook;
  251. hook.type = efm32_irq_type_gpio;
  252. hook.unit = KEYS_INT_PIN;
  253. hook.cbFunc = efm32_keys_isr;
  254. hook.userPtr = RT_NULL;
  255. efm32_irq_hook_register(&hook);
  256. if ((rt_uint8_t)KEYS_INT_PIN % 2)
  257. {
  258. NVIC_ClearPendingIRQ(GPIO_ODD_IRQn);
  259. NVIC_SetPriority(GPIO_ODD_IRQn, EFM32_IRQ_PRI_DEFAULT);
  260. NVIC_EnableIRQ(GPIO_ODD_IRQn);
  261. }
  262. else
  263. {
  264. NVIC_ClearPendingIRQ(GPIO_EVEN_IRQn);
  265. NVIC_SetPriority(GPIO_EVEN_IRQn, EFM32_IRQ_PRI_DEFAULT);
  266. NVIC_EnableIRQ(GPIO_EVEN_IRQn);
  267. }
  268. /* Enable DVK joystick interrupt */
  269. DVK_enableInterrupt(BC_INTEN_JOYSTICK);
  270. rt_timer_init(&joy.timer,
  271. "joy_tmr",
  272. efm32_keys_timer_isr,
  273. RT_NULL,
  274. KEYS_POLL_TIME,
  275. RT_TIMER_FLAG_PERIODIC);
  276. joy_dev.init = efm32_keys_init;
  277. joy_dev.open = RT_NULL;
  278. joy_dev.close = RT_NULL;
  279. joy_dev.read = RT_NULL;
  280. joy_dev.write = RT_NULL;
  281. joy_dev.control = RT_NULL;
  282. joy_dev.user_data = (void *)&joy;
  283. /* register joy stick device */
  284. rt_device_register(&joy_dev, "joy", RT_DEVICE_FLAG_RDWR);
  285. keys_debug("Keys: H/W init OK!\n");
  286. }
  287. #endif /* defined(EFM32_USING_KEYS) */
  288. /***************************************************************************//**
  289. * @}
  290. ******************************************************************************/