key.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * File : touch.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2010, RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-10-01 Yi.Qiu first version
  13. */
  14. /*
  15. * Warning, this keypad driver can only work on QEMU emulator
  16. */
  17. #include <rtthread.h>
  18. #include <s3c24x0.h>
  19. #include <rthw.h>
  20. #define KEY_RX_BUFFER_SIZE 32
  21. struct rt_key_device
  22. {
  23. struct rt_device parent;
  24. rt_uint32_t rx_buffer[KEY_RX_BUFFER_SIZE];
  25. rt_uint32_t read_index, save_index;
  26. };
  27. static struct rt_key_device *key_device = RT_NULL;
  28. /* save a char to serial buffer */
  29. static void rt_key_savechar(char ch)
  30. {
  31. rt_base_t level;
  32. /* disable interrupt */
  33. level = rt_hw_interrupt_disable();
  34. key_device->rx_buffer[key_device->save_index] = ch;
  35. key_device->save_index ++;
  36. if (key_device->save_index >= KEY_RX_BUFFER_SIZE)
  37. key_device->save_index = 0;
  38. /* if the next position is read index, discard this 'read char' */
  39. if (key_device->save_index == key_device->read_index)
  40. {
  41. key_device->read_index ++;
  42. if (key_device->read_index >= KEY_RX_BUFFER_SIZE)
  43. key_device->read_index = 0;
  44. }
  45. /* enable interrupt */
  46. rt_hw_interrupt_enable(level);
  47. }
  48. /* ISR for serial interrupt */
  49. static void rt_hw_key_isr(void)
  50. {
  51. /* save on rx buffer */
  52. rt_key_savechar(URXH1 & 0xff);
  53. /* invoke callback */
  54. if (key_device->parent.rx_indicate != RT_NULL)
  55. {
  56. rt_size_t rx_length;
  57. /* get rx length */
  58. rx_length = key_device->read_index > key_device->save_index ?
  59. KEY_RX_BUFFER_SIZE - key_device->read_index + key_device->save_index :
  60. key_device->save_index - key_device->read_index;
  61. key_device->parent.rx_indicate(&key_device->parent, rx_length);
  62. }
  63. }
  64. /**
  65. * This function is only for QEMU emulation
  66. */
  67. static void rt_key_handler(int vector)
  68. {
  69. INTSUBMSK |= (BIT_SUB_RXD1);
  70. rt_hw_key_isr();
  71. SUBSRCPND |= BIT_SUB_RXD1;
  72. /*Unmask sub interrupt (RXD0)*/
  73. INTSUBMSK &=~(BIT_SUB_RXD1);
  74. }
  75. /**
  76. * This function is only for QEMU emulation
  77. */
  78. static void key_init(void)
  79. {
  80. int i = 0;
  81. GPHCON |= 0xa0;
  82. /*PULLUP is enable */
  83. GPHUP |= 0x0c;
  84. /* FIFO enable, Tx/Rx FIFO clear */
  85. UFCON1 = 0x0;
  86. /* disable the flow control */
  87. UMCON1= 0x0;
  88. /* Normal,No parity,1 stop,8 bit */
  89. ULCON1 = 0x3;
  90. /*
  91. * tx=level,rx=edge,disable timeout int.,enable rx error int.,
  92. * normal,interrupt or polling
  93. */
  94. UCON1 = 0x245;
  95. //UBRD0 = div;
  96. // UBRD0 = 0x500; /* baudrate = 19200bps */
  97. UBRD1 = 0x1a;
  98. UTXH1 = 0x2;
  99. URXH1 = 0x1;
  100. /* output PCLK to UART0/1, PWMTIMER */
  101. CLKCON |= 0x0D00;
  102. for (i = 0; i < 100; i++);
  103. /* install key isr */
  104. INTSUBMSK &= ~(BIT_SUB_RXD1);
  105. rt_hw_interrupt_install(INTUART1, rt_key_handler, RT_NULL);
  106. rt_hw_interrupt_umask(INTUART1);
  107. }
  108. static rt_err_t rt_key_init(rt_device_t dev)
  109. {
  110. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  111. {
  112. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  113. {
  114. rt_memset(key_device->rx_buffer, 0,
  115. sizeof(key_device->rx_buffer));
  116. key_device->read_index = key_device->save_index = 0;
  117. }
  118. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  119. }
  120. return RT_EOK;
  121. }
  122. static rt_err_t rt_key_open(rt_device_t dev, rt_uint16_t oflag)
  123. {
  124. return RT_EOK;
  125. }
  126. static rt_err_t rt_key_close(rt_device_t dev)
  127. {
  128. return RT_EOK;
  129. }
  130. static rt_size_t rt_key_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  131. {
  132. rt_uint8_t* ptr;
  133. rt_err_t err_code;
  134. rt_base_t level;
  135. ptr = buffer;
  136. err_code = RT_EOK;
  137. /* interrupt mode Rx */
  138. while (size)
  139. {
  140. if (key_device->read_index != key_device->save_index)
  141. {
  142. *ptr++ = key_device->rx_buffer[key_device->read_index];
  143. size --;
  144. /* disable interrupt */
  145. level = rt_hw_interrupt_disable();
  146. key_device->read_index ++;
  147. if (key_device->read_index >= KEY_RX_BUFFER_SIZE)
  148. key_device->read_index = 0;
  149. /* enable interrupt */
  150. rt_hw_interrupt_enable(level);
  151. }
  152. else
  153. {
  154. /* set error code */
  155. err_code = -RT_EEMPTY;
  156. break;
  157. }
  158. }
  159. /* set error code */
  160. rt_set_errno(err_code);
  161. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  162. }
  163. static rt_err_t rt_key_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  164. {
  165. return RT_EOK;
  166. }
  167. #ifdef RT_USING_RTGUI
  168. #include <rtgui/event.h>
  169. #include <rtgui/rtgui_server.h>
  170. #include <rtgui/kbddef.h>
  171. static int s_key_map[0xff] = {0};
  172. static void rt_keymap_init(void)
  173. {
  174. s_key_map[0x1] = RTGUIK_ESCAPE;
  175. s_key_map[0xc] = RTGUIK_MINUS;
  176. s_key_map[0x39] = RTGUIK_SPACE;
  177. s_key_map[0xd] = RTGUIK_KP_EQUALS;
  178. s_key_map[0xe] = RTGUIK_BACKSPACE;
  179. s_key_map[0xf] = RTGUIK_TAB;
  180. s_key_map[0x1c] = RTGUIK_KP_ENTER;
  181. s_key_map[0xb] = RTGUIK_0;
  182. s_key_map[0x2] = RTGUIK_1;
  183. s_key_map[0x3] = RTGUIK_2;
  184. s_key_map[0x4] = RTGUIK_3;
  185. s_key_map[0x5] = RTGUIK_4;
  186. s_key_map[0x6] = RTGUIK_5;
  187. s_key_map[0x7] = RTGUIK_6;
  188. s_key_map[0x8] = RTGUIK_7;
  189. s_key_map[0x9] = RTGUIK_8;
  190. s_key_map[0xa] = RTGUIK_9;
  191. s_key_map[0x3b] = RTGUIK_F1;
  192. s_key_map[0x3c] = RTGUIK_F2;
  193. s_key_map[0x3d] = RTGUIK_F3;
  194. s_key_map[0x3e] = RTGUIK_F4;
  195. s_key_map[0xef] = RTGUIK_F5;
  196. s_key_map[0x40] = RTGUIK_F6;
  197. s_key_map[0x41] = RTGUIK_F7;
  198. s_key_map[0x42] = RTGUIK_F8;
  199. s_key_map[0x43] = RTGUIK_F9;
  200. s_key_map[0x1e] = RTGUIK_a;
  201. s_key_map[0x30] = RTGUIK_b;
  202. s_key_map[0x2c] = RTGUIK_c;
  203. s_key_map[0x20] = RTGUIK_d;
  204. s_key_map[0x12] = RTGUIK_e;
  205. s_key_map[0x21] = RTGUIK_f;
  206. s_key_map[0x22] = RTGUIK_g;
  207. s_key_map[0x23] = RTGUIK_h;
  208. s_key_map[0x17] = RTGUIK_i;
  209. s_key_map[0x24] = RTGUIK_j;
  210. s_key_map[0x25] = RTGUIK_k;
  211. s_key_map[0x26] = RTGUIK_l;
  212. s_key_map[0x32] = RTGUIK_m;
  213. s_key_map[0x31] = RTGUIK_n;
  214. s_key_map[0x18] = RTGUIK_o;
  215. s_key_map[0x19] = RTGUIK_p;
  216. s_key_map[0x10] = RTGUIK_q;
  217. s_key_map[0x13] = RTGUIK_r;
  218. s_key_map[0x1f] = RTGUIK_s;
  219. s_key_map[0x14] = RTGUIK_t;
  220. s_key_map[0x16] = RTGUIK_u;
  221. s_key_map[0x2f] = RTGUIK_v;
  222. s_key_map[0x11] = RTGUIK_w;
  223. s_key_map[0x2d] = RTGUIK_x;
  224. s_key_map[0x15] = RTGUIK_y;
  225. s_key_map[0x2c] = RTGUIK_z;
  226. s_key_map[0x4b] = RTGUIK_LEFT;
  227. s_key_map[0x4d] = RTGUIK_RIGHT;
  228. s_key_map[0x50] = RTGUIK_DOWN;
  229. s_key_map[0x2e] = RTGUIK_DELETE;
  230. s_key_map[0x48] = RTGUIK_UP;
  231. }
  232. static rt_err_t rtgui_key_rx(rt_device_t dev, rt_size_t size)
  233. {
  234. struct rtgui_event_kbd kbd_event;
  235. char key_value;
  236. while(rt_device_read(dev, 0, &key_value, 1) == 1)
  237. {
  238. /* init keyboard event */
  239. RTGUI_EVENT_KBD_INIT(&kbd_event);
  240. kbd_event.mod = RTGUI_KMOD_NONE;
  241. kbd_event.unicode = 0;
  242. kbd_event.key = RTGUIK_UNKNOWN;
  243. if(key_value & 0x80)
  244. {
  245. kbd_event.type = RTGUI_KEYUP;
  246. }
  247. else
  248. {
  249. kbd_event.type = RTGUI_KEYDOWN;
  250. }
  251. kbd_event.key = s_key_map[key_value & 0x7F];
  252. }
  253. if (kbd_event.key != RTGUIK_UNKNOWN)
  254. {
  255. /* post down event */
  256. rtgui_server_post_event(&(kbd_event.parent), sizeof(kbd_event));
  257. }
  258. return RT_EOK;
  259. }
  260. #endif
  261. /*
  262. * key driver register
  263. */
  264. void rt_hw_key_init(void)
  265. {
  266. /* hardware init */
  267. key_init();
  268. key_device = (struct rt_key_device*)rt_malloc (sizeof(struct rt_key_device));
  269. if (key_device == RT_NULL) return; /* no memory yet */
  270. /* clear device structure */
  271. rt_memset(&(key_device->parent), 0, sizeof(struct rt_device));
  272. key_device->parent.type = RT_Device_Class_Char;
  273. key_device->parent.tx_complete = RT_NULL;
  274. key_device->parent.init = rt_key_init;
  275. key_device->parent.open = rt_key_open;
  276. key_device->parent.close = rt_key_close;
  277. key_device->parent.read = rt_key_read;
  278. key_device->parent.write = RT_NULL;
  279. key_device->parent.control = rt_key_control;
  280. key_device->parent.user_data = RT_NULL;
  281. #ifdef RT_USING_RTGUI
  282. key_device->parent.rx_indicate = rtgui_key_rx;
  283. /* init keymap */
  284. rt_keymap_init();
  285. #endif
  286. /* register key device to RT-Thread */
  287. rt_device_register(&(key_device->parent), "key", RT_DEVICE_FLAG_RDONLY | RT_DEVICE_FLAG_INT_RX);
  288. }