touch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #include <stdbool.h>
  2. #include "stm32f10x.h"
  3. #include "board.h"
  4. #include "touch.h"
  5. #include <rtthread.h>
  6. #include <rtgui/event.h>
  7. #include <rtgui/kbddef.h>
  8. #include <rtgui/rtgui_server.h>
  9. #include <rtgui/rtgui_system.h>
  10. /*
  11. MISO PA6
  12. MOSI PA7
  13. CLK PA5
  14. CS PC4
  15. */
  16. #define CS_0() GPIO_ResetBits(GPIOC,GPIO_Pin_4)
  17. #define CS_1() GPIO_SetBits(GPIOC,GPIO_Pin_4)
  18. /*
  19. 7 6 - 4 3 2 1-0
  20. s A2-A0 MODE SER/DFR PD1-PD0
  21. */
  22. #define TOUCH_MSR_Y 0x90 //读X轴坐标指令 addr:1
  23. #define TOUCH_MSR_X 0xD0 //读Y轴坐标指令 addr:3
  24. struct rtgui_touch_device
  25. {
  26. struct rt_device parent;
  27. rt_timer_t poll_timer;
  28. rt_uint16_t x, y;
  29. rt_bool_t calibrating;
  30. rt_touch_calibration_func_t calibration_func;
  31. rt_uint16_t min_x, max_x;
  32. rt_uint16_t min_y, max_y;
  33. };
  34. static struct rtgui_touch_device *touch = RT_NULL;
  35. extern unsigned char SPI_WriteByte(unsigned char data);
  36. rt_inline void EXTI_Enable(rt_uint32_t enable);
  37. struct rt_semaphore spi1_lock;
  38. void rt_hw_spi1_baud_rate(uint16_t SPI_BaudRatePrescaler)
  39. {
  40. SPI1->CR1 &= ~SPI_BaudRatePrescaler_256;
  41. SPI1->CR1 |= SPI_BaudRatePrescaler;
  42. }
  43. uint8_t SPI_WriteByte(unsigned char data)
  44. {
  45. //Wait until the transmit buffer is empty
  46. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
  47. // Send the byte
  48. SPI_I2S_SendData(SPI1, data);
  49. //Wait until a data is received
  50. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
  51. // Get the received data
  52. data = SPI_I2S_ReceiveData(SPI1);
  53. // Return the shifted data
  54. return data;
  55. }
  56. //SPI写数据
  57. static void WriteDataTo7843(unsigned char num)
  58. {
  59. SPI_WriteByte(num);
  60. }
  61. #define X_WIDTH 240
  62. #define Y_WIDTH 320
  63. static void rtgui_touch_calculate()
  64. {
  65. if (touch != RT_NULL)
  66. {
  67. rt_sem_take(&spi1_lock, RT_WAITING_FOREVER);
  68. /* SPI1 configure */
  69. rt_hw_spi1_baud_rate(SPI_BaudRatePrescaler_64);/* 72M/64=1.125M */
  70. //读取触摸值
  71. {
  72. rt_uint16_t tmpx[10];
  73. rt_uint16_t tmpy[10];
  74. unsigned int i;
  75. for(i=0; i<10; i++)
  76. {
  77. CS_0();
  78. WriteDataTo7843(TOUCH_MSR_X); /* read X */
  79. tmpx[i] = SPI_WriteByte(0x00)<<4; /* read MSB bit[11:8] */
  80. tmpx[i] |= ((SPI_WriteByte(TOUCH_MSR_Y)>>4)&0x0F ); /* read LSB bit[7:0] */
  81. tmpy[i] = SPI_WriteByte(0x00)<<4; /* read MSB bit[11:8] */
  82. tmpy[i] |= ((SPI_WriteByte(0x00)>>4)&0x0F ); /* read LSB bit[7:0] */
  83. WriteDataTo7843( 1<<7 ); /* 打开中断 */
  84. CS_1();
  85. }
  86. //去最高值与最低值,再取平均值
  87. {
  88. rt_uint32_t min_x = 0xFFFF,min_y = 0xFFFF;
  89. rt_uint32_t max_x = 0,max_y = 0;
  90. rt_uint32_t total_x = 0;
  91. rt_uint32_t total_y = 0;
  92. unsigned int i;
  93. for(i=0;i<10;i++)
  94. {
  95. if( tmpx[i] < min_x )
  96. {
  97. min_x = tmpx[i];
  98. }
  99. if( tmpx[i] > max_x )
  100. {
  101. max_x = tmpx[i];
  102. }
  103. total_x += tmpx[i];
  104. if( tmpy[i] < min_y )
  105. {
  106. min_y = tmpy[i];
  107. }
  108. if( tmpy[i] > max_y )
  109. {
  110. max_y = tmpy[i];
  111. }
  112. total_y += tmpy[i];
  113. }
  114. total_x = total_x - min_x - max_x;
  115. total_y = total_y - min_y - max_y;
  116. touch->x = total_x / 8;
  117. touch->y = total_y / 8;
  118. }//去最高值与最低值,再取平均值
  119. }//读取触摸值
  120. rt_sem_release(&spi1_lock);
  121. /* if it's not in calibration status */
  122. if (touch->calibrating != RT_TRUE)
  123. {
  124. if (touch->max_x > touch->min_x)
  125. {
  126. touch->x = (touch->x - touch->min_x) * X_WIDTH/(touch->max_x - touch->min_x);
  127. }
  128. else
  129. {
  130. touch->x = (touch->min_x - touch->x) * X_WIDTH/(touch->min_x - touch->max_x);
  131. }
  132. if (touch->max_y > touch->min_y)
  133. {
  134. touch->y = (touch->y - touch->min_y) * Y_WIDTH /(touch->max_y - touch->min_y);
  135. }
  136. else
  137. {
  138. touch->y = (touch->min_y - touch->y) * Y_WIDTH /(touch->min_y - touch->max_y);
  139. }
  140. }
  141. }
  142. }
  143. static unsigned int flag = 0;
  144. void touch_timeout(void* parameter)
  145. {
  146. struct rtgui_event_mouse emouse;
  147. static struct _touch_previous
  148. {
  149. rt_uint32_t x;
  150. rt_uint32_t y;
  151. } touch_previous;
  152. if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) != 0)
  153. {
  154. int tmer = RT_TICK_PER_SECOND/8 ;
  155. EXTI_Enable(1);
  156. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  157. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_UP);
  158. /* use old value */
  159. emouse.x = touch->x;
  160. emouse.y = touch->y;
  161. /* stop timer */
  162. rt_timer_stop(touch->poll_timer);
  163. rt_kprintf("touch up: (%d, %d)\n", emouse.x, emouse.y);
  164. flag = 0;
  165. if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
  166. {
  167. /* callback function */
  168. touch->calibration_func(emouse.x, emouse.y);
  169. }
  170. rt_timer_control(touch->poll_timer , RT_TIMER_CTRL_SET_TIME , &tmer);
  171. }
  172. else
  173. {
  174. if(flag == 0)
  175. {
  176. int tmer = RT_TICK_PER_SECOND/20 ;
  177. /* calculation */
  178. rtgui_touch_calculate();
  179. /* send mouse event */
  180. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  181. emouse.parent.sender = RT_NULL;
  182. emouse.x = touch->x;
  183. emouse.y = touch->y;
  184. touch_previous.x = touch->x;
  185. touch_previous.y = touch->y;
  186. /* init mouse button */
  187. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_DOWN);
  188. // rt_kprintf("touch down: (%d, %d)\n", emouse.x, emouse.y);
  189. flag = 1;
  190. rt_timer_control(touch->poll_timer , RT_TIMER_CTRL_SET_TIME , &tmer);
  191. }
  192. else
  193. {
  194. /* calculation */
  195. rtgui_touch_calculate();
  196. #define previous_keep 8
  197. //判断移动距离是否小于previous_keep,减少误动作.
  198. if(
  199. (touch_previous.x<touch->x+previous_keep)
  200. && (touch_previous.x>touch->x-previous_keep)
  201. && (touch_previous.y<touch->y+previous_keep)
  202. && (touch_previous.y>touch->y-previous_keep) )
  203. {
  204. return;
  205. }
  206. touch_previous.x = touch->x;
  207. touch_previous.y = touch->y;
  208. /* send mouse event */
  209. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON ;
  210. emouse.parent.sender = RT_NULL;
  211. emouse.x = touch->x;
  212. emouse.y = touch->y;
  213. /* init mouse button */
  214. emouse.button = (RTGUI_MOUSE_BUTTON_RIGHT |RTGUI_MOUSE_BUTTON_DOWN);
  215. // rt_kprintf("touch motion: (%d, %d)\n", emouse.x, emouse.y);
  216. }
  217. }
  218. /* send event to server */
  219. if (touch->calibrating != RT_TRUE)
  220. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  221. }
  222. static void NVIC_Configuration(void)
  223. {
  224. NVIC_InitTypeDef NVIC_InitStructure;
  225. /* Enable the EXTI0 Interrupt */
  226. NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
  227. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  228. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  229. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  230. NVIC_Init(&NVIC_InitStructure);
  231. }
  232. rt_inline void EXTI_Enable(rt_uint32_t enable)
  233. {
  234. EXTI_InitTypeDef EXTI_InitStructure;
  235. /* Configure EXTI */
  236. EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  237. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  238. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//Falling下降沿 Rising上升
  239. if (enable)
  240. {
  241. /* enable */
  242. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  243. }
  244. else
  245. {
  246. /* disable */
  247. EXTI_InitStructure.EXTI_LineCmd = DISABLE;
  248. }
  249. EXTI_Init(&EXTI_InitStructure);
  250. EXTI_ClearITPendingBit(EXTI_Line1);
  251. }
  252. static void EXTI_Configuration(void)
  253. {
  254. /* PB1 touch INT */
  255. {
  256. GPIO_InitTypeDef GPIO_InitStructure;
  257. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  258. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  259. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  260. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  261. GPIO_Init(GPIOB,&GPIO_InitStructure);
  262. }
  263. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
  264. /* Configure EXTI */
  265. EXTI_Enable(1);
  266. }
  267. /* RT-Thread Device Interface */
  268. static rt_err_t rtgui_touch_init (rt_device_t dev)
  269. {
  270. NVIC_Configuration();
  271. EXTI_Configuration();
  272. /* PC4 touch CS */
  273. {
  274. GPIO_InitTypeDef GPIO_InitStructure;
  275. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  276. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  277. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  278. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  279. GPIO_Init(GPIOC,&GPIO_InitStructure);
  280. CS_1();
  281. }
  282. CS_0();
  283. WriteDataTo7843( 1<<7 ); /* 打开中断 */
  284. CS_1();
  285. return RT_EOK;
  286. }
  287. static rt_err_t rtgui_touch_control (rt_device_t dev, rt_uint8_t cmd, void *args)
  288. {
  289. switch (cmd)
  290. {
  291. case RT_TOUCH_CALIBRATION:
  292. touch->calibrating = RT_TRUE;
  293. touch->calibration_func = (rt_touch_calibration_func_t)args;
  294. break;
  295. case RT_TOUCH_NORMAL:
  296. touch->calibrating = RT_FALSE;
  297. break;
  298. case RT_TOUCH_CALIBRATION_DATA:
  299. {
  300. struct calibration_data* data;
  301. data = (struct calibration_data*) args;
  302. //update
  303. touch->min_x = data->min_x;
  304. touch->max_x = data->max_x;
  305. touch->min_y = data->min_y;
  306. touch->max_y = data->max_y;
  307. }
  308. break;
  309. }
  310. return RT_EOK;
  311. }
  312. void EXTI1_IRQHandler(void)
  313. {
  314. /* disable interrupt */
  315. EXTI_Enable(0);
  316. /* start timer */
  317. rt_timer_start(touch->poll_timer);
  318. EXTI_ClearITPendingBit(EXTI_Line1);
  319. }
  320. void rtgui_touch_hw_init(void)
  321. {
  322. /* SPI1 config */
  323. {
  324. GPIO_InitTypeDef GPIO_InitStructure;
  325. SPI_InitTypeDef SPI_InitStructure;
  326. /* Enable SPI1 Periph clock */
  327. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
  328. | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1,
  329. ENABLE);
  330. /* Configure SPI1 pins: PA5-SCK, PA6-MISO and PA7-MOSI */
  331. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  332. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  333. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  334. GPIO_Init(GPIOA, &GPIO_InitStructure);
  335. /*------------------------ SPI1 configuration ------------------------*/
  336. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//SPI_Direction_1Line_Tx;
  337. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  338. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  339. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  340. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  341. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  342. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;/* 72M/64=1.125M */
  343. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  344. SPI_InitStructure.SPI_CRCPolynomial = 7;
  345. SPI_I2S_DeInit(SPI1);
  346. SPI_Init(SPI1, &SPI_InitStructure);
  347. /* Enable SPI_MASTER */
  348. SPI_Cmd(SPI1, ENABLE);
  349. SPI_CalculateCRC(SPI1, DISABLE);
  350. if (rt_sem_init(&spi1_lock, "spi1lock", 1, RT_IPC_FLAG_FIFO) != RT_EOK)
  351. {
  352. rt_kprintf("init spi1 lock semaphore failed\n");
  353. }
  354. } /* SPI1 config */
  355. touch = (struct rtgui_touch_device*)rt_malloc (sizeof(struct rtgui_touch_device));
  356. if (touch == RT_NULL) return; /* no memory yet */
  357. /* clear device structure */
  358. rt_memset(&(touch->parent), 0, sizeof(struct rt_device));
  359. touch->calibrating = false;
  360. /* init device structure */
  361. touch->parent.type = RT_Device_Class_Unknown;
  362. touch->parent.init = rtgui_touch_init;
  363. touch->parent.control = rtgui_touch_control;
  364. touch->parent.user_data = RT_NULL;
  365. /* create 1/8 second timer */
  366. touch->poll_timer = rt_timer_create("touch", touch_timeout, RT_NULL,
  367. RT_TICK_PER_SECOND/8, RT_TIMER_FLAG_PERIODIC);
  368. /* register touch device to RT-Thread */
  369. rt_device_register(&(touch->parent), "touch", RT_DEVICE_FLAG_RDWR);
  370. }
  371. #include <finsh.h>
  372. void touch_t( rt_uint16_t x , rt_uint16_t y )
  373. {
  374. struct rtgui_event_mouse emouse ;
  375. emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
  376. emouse.parent.sender = RT_NULL;
  377. emouse.x = x ;
  378. emouse.y = y ;
  379. /* init mouse button */
  380. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_DOWN );
  381. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  382. rt_thread_delay(2) ;
  383. emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_UP );
  384. rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
  385. }
  386. FINSH_FUNCTION_EXPORT(touch_t, x & y ) ;