Advertisement
Tsung_Mao_Fang

Question for Library 34- STM32F4 as USB HID Device by ftm

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