dev_keys.c 8.8 KB

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