Advertisement
Tsung_Mao_Fang

Untitled

Mar 9th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. /* Include core modules */
  2. #include "stm32f4xx.h"
  3. /* Include my libraries here */
  4. #include "defines.h"
  5. #include "tm_stm32f4_usb_hid_device.h"
  6. #include "tm_stm32f4_delay.h"
  7. #include "tm_stm32f4_disco.h"
  8.  
  9. void Uart3_Init();
  10. TM_USB_HIDDEVICE_Mouse_t Mouse;
  11.  
  12. int main(void) {
  13. uint8_t already = 0;
  14.  
  15. /* Initialize system */
  16. SystemInit();
  17.  
  18. /* Initialize leds */
  19. TM_DISCO_LedInit();
  20.  
  21. /* Initialize button */
  22. TM_DISCO_ButtonInit();
  23.  
  24. /* Initialize delay */
  25. TM_DELAY_Init();
  26.  
  27. /* Initialize USB HID Device */
  28. TM_USB_HIDDEVICE_Init();
  29.  
  30. /*Initialize USART3*/
  31. Uart3_Init();
  32.  
  33. /* Set default values for mouse struct */
  34. TM_USB_HIDDEVICE_MouseStructInit(&Mouse);
  35.  
  36. while (1) {
  37.  
  38. /* If we are connected and drivers are OK */
  39. /* If you pressed button right now and was not already pressed */
  40.  
  41. if (TM_DISCO_ButtonPressed() && already == 0) { /* Button on press */
  42. already = 1;
  43.  
  44. } else if (!TM_DISCO_ButtonPressed() && already == 1) { /* Button on release */
  45. already = 0;
  46.  
  47. /*
  48. Send a signals for dragging cursor.
  49. */
  50. Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Pressed;
  51. Mouse.XAxis = 0;
  52. Mouse.YAxis = 0;
  53. TM_USB_HIDDEVICE_MouseSend(&Mouse);
  54.  
  55. Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Pressed;
  56.  
  57. Mouse.XAxis = 50;
  58. Mouse.YAxis = 50;
  59. TM_USB_HIDDEVICE_MouseSend(&Mouse);
  60.  
  61. Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Released;
  62. Mouse.XAxis = 0;
  63. Mouse.YAxis = 0;
  64. TM_USB_HIDDEVICE_MouseSend(&Mouse);
  65.  
  66. }
  67. else {
  68. /* Turn off green LED */
  69. TM_DISCO_LedOff(LED_GREEN);
  70. }
  71. }
  72. }
  73.  
  74.  
  75. void Uart3_Init(){
  76. /******** ?? USART?GPIO ??? ********/
  77. USART_InitTypeDef USART_InitStructure;
  78. GPIO_InitTypeDef GPIO_InitStructure;
  79.  
  80. /******** ?? GPIOC?USART3 ? RCC ?? ********/
  81. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  82. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  83.  
  84. /******** ? PC10?PC11 ??? USART3 ********/
  85. GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
  86. GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
  87.  
  88. /******** ?? PC10 ? Tx ?? ********/
  89. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  90. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  91. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  92. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  93. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  94. GPIO_Init(GPIOC, &GPIO_InitStructure);
  95.  
  96. /******** ?? PC11 ? Rx ?? ********/
  97. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  98. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  99. GPIO_Init(GPIOC, &GPIO_InitStructure);
  100.  
  101. /******** USART ?????? ********/
  102. USART_InitStructure.USART_BaudRate = 9600;
  103. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  104. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  105. USART_InitStructure.USART_Parity = USART_Parity_No;
  106. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  107. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  108. USART_Init(USART3, &USART_InitStructure);
  109.  
  110. /******** ?? USART3 ********/
  111. USART_Cmd(USART3, ENABLE);
  112.  
  113. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  114. /* ?? USART3 ?? */
  115. NVIC_EnableIRQ(USART3_IRQn);
  116. }
  117.  
  118.  
  119. uint8_t MouseDataBUFF[5];
  120. int buffIndex = 0;
  121.  
  122. void USART3_IRQHandler(){
  123.  
  124. uint8_t mouse_signal;
  125.  
  126. while(USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET){}
  127. /* read 1 byte via usart3 it */
  128. mouse_signal = USART_ReceiveData(USART3);
  129.  
  130. /*MouseDataBuff spec
  131.  
  132. these signals be read from usart3
  133.  
  134. index 0 -> Does left button be clicked? 0 is click, 1 is release
  135. index 1 -> Does right button be clicked? 0 is click, 1 is release
  136. index 2 -> Does middle button be clicked? 0 is click, 1 is release
  137. index 3 -> X's movement
  138. index 4 -> Y's movement
  139. */
  140.  
  141. MouseDataBUFF[buffIndex++] = mouse_signal;
  142.  
  143. /*
  144. When uart3 get 5 bytes mouse signals, I can decide a set of Mouse value.
  145. */
  146. if(buffIndex == 5)
  147. {
  148. if(MouseDataBUFF[0] == 0)
  149. {
  150. Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Pressed;
  151. }
  152. else
  153. {
  154. Mouse.LeftButton = TM_USB_HIDDEVICE_Button_Released;
  155. }
  156.  
  157. Mouse.XAxis = MouseDataBUFF[3];
  158. Mouse.YAxis = MouseDataBUFF[4];
  159.  
  160. buffIndex=0;
  161.  
  162. TM_USB_HIDDEVICE_MouseSend(&Mouse);
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement