Advertisement
Tsung_Mao_Fang

Question for Library 31- USB HID Host for STM32F4 by ftm

Mar 7th, 2015
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 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_disco.h"
  6. #include "tm_stm32f4_delay.h"
  7. #include "tm_stm32f4_usb_hid_host.h"
  8. #include <stm32f4xx_usart.h>
  9.  
  10. #include <stdio.h>
  11.  
  12. void init_usart2(void);
  13. int diff(int16_t *prevBuff, int16_t *nowBuff);
  14.  
  15. int main(void) {
  16. TM_USB_HIDHOST_Result_t USB_HID_Status; /* USB HID Host status */
  17. TM_USB_HIDHOST_Keyboard_t Keyboard_Data; /* Keyboard handler */
  18. TM_USB_HIDHOST_Mouse_t Mouse_Data; /* Mouse handler */
  19.  
  20. int16_t nowBuff[5];
  21. int16_t prevBuff[5];
  22. const int mouseDataSize = 5;
  23.  
  24. /* Initialize system */
  25. SystemInit();
  26.  
  27. /* Leds init */
  28. TM_DISCO_LedInit();
  29.  
  30. /* Delay init */
  31. TM_DELAY_Init();
  32.  
  33. /* Init USB HID */
  34. TM_USB_HIDHOST_Init();
  35.  
  36. init_usart2();
  37.  
  38. while (1) {
  39. /* Process USB HID */
  40. /* This must be called periodically */
  41.  
  42. int change_index = 0, transfer_index = 0;
  43.  
  44. TM_USB_HIDHOST_Process();
  45.  
  46. /* Get connected device */
  47. USB_HID_Status = TM_USB_HIDHOST_Device();
  48.  
  49. /* Switch status */
  50. switch (USB_HID_Status) {
  51. /* Keyboard connected */
  52. case TM_USB_HIDHOST_Result_KeyboardConnected:
  53.  
  54. /* GREEN led ON */
  55. TM_DISCO_LedOn(LED_GREEN);
  56.  
  57. /* Get keyboard data */
  58. TM_USB_HIDHOST_ReadKeyboard(&Keyboard_Data);
  59. /* Check if keyboard button is pressed */
  60. if (Keyboard_Data.ButtonStatus == TM_USB_HIDHOST_Button_Pressed) {
  61. if (Keyboard_Data.Button == 'b') { /* Turn on RED led on 'b' button */
  62. TM_DISCO_LedOn(LED_RED);
  63. } else if (Keyboard_Data.Button == 'v') { /* Turn off RED led on 'v' button */
  64. TM_DISCO_LedOff(LED_RED);
  65. }
  66. }
  67.  
  68. break;
  69.  
  70. /* Mouse connected */
  71. case TM_USB_HIDHOST_Result_MouseConnected:
  72.  
  73. /* RED led ON */
  74. TM_DISCO_LedOn(LED_RED);
  75.  
  76. /* Get mouse data */
  77. TM_USB_HIDHOST_ReadMouse(&Mouse_Data);
  78.  
  79. nowBuff[0] = Mouse_Data.LeftButton;
  80. nowBuff[1] = Mouse_Data.RightButton;
  81. nowBuff[2] = Mouse_Data.MiddleButton;
  82. nowBuff[3] = Mouse_Data.DiffX;
  83. nowBuff[4] = Mouse_Data.DiffY;
  84.  
  85.  
  86. //check this mouse signals is different from previous
  87. if( !diff(prevBuff,nowBuff) )
  88. {
  89. //Recode now mouse signals.
  90. for (change_index = 0; change_index<mouseDataSize; change_index++)
  91. {
  92. prevBuff[change_index] = nowBuff[change_index];
  93. }
  94.  
  95. //transfer mouse data to usart
  96. for ( transfer_index=0; transfer_index <mouseDataSize; transfer_index++)
  97. {
  98. USART_SendData(USART2, nowBuff[transfer_index]);
  99. while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
  100. }
  101. }
  102.  
  103. break;
  104.  
  105. /* No device connected */
  106. case TM_USB_HIDHOST_Result_Disconnected:
  107.  
  108. /* Turn off leds */
  109. TM_DISCO_LedOff(LED_RED | LED_GREEN);
  110.  
  111. break;
  112.  
  113. /* Device is not supported */
  114. case TM_USB_HIDHOST_Result_DeviceNotSupported:
  115.  
  116. /* Device is not supported */
  117. /* Toggle leds forever */
  118. TM_DISCO_LedToggle(LED_RED | LED_GREEN);
  119. /* Delay */
  120. Delayms(50);
  121.  
  122. break;
  123.  
  124. /* Error occurred somewhere */
  125. case TM_USB_HIDHOST_Result_Error:
  126.  
  127. /* Error occurred */
  128. /* Toggle RED LED forever */
  129. TM_DISCO_LedToggle(LED_RED);
  130. /* Delay */
  131. Delayms(50);
  132.  
  133. break;
  134.  
  135. /* Library is not initialized */
  136. case TM_USB_HIDHOST_Result_LibraryNotInitialized:
  137.  
  138. /* Library is not initialized */
  139. /* Toggle GREEN LED */
  140. TM_DISCO_LedToggle(LED_GREEN);
  141. /* Delay */
  142. Delayms(50);
  143.  
  144. break;
  145. }
  146. }
  147. }
  148.  
  149. void init_usart2(void){
  150.  
  151. GPIO_InitTypeDef GPIO_InitStructure;
  152. USART_InitTypeDef USART_InitStructure;
  153.  
  154. /* enable peripheral clock for USART2 */
  155. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  156.  
  157.  
  158. /* GPIOA clock enable */
  159. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  160.  
  161. /* GPIOA Configuration: USART2 TX on PA2 */
  162. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  163. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  164. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  165. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  166. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
  167. GPIO_Init(GPIOA, &GPIO_InitStructure);
  168.  
  169. /* Connect USART2 pins to AF2 */
  170. // TX = PA2
  171. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
  172.  
  173. USART_InitStructure.USART_BaudRate = 9600;
  174. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  175. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  176. USART_InitStructure.USART_Parity = USART_Parity_No;
  177. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  178. USART_InitStructure.USART_Mode = USART_Mode_Tx;
  179. USART_Init(USART2, &USART_InitStructure);
  180.  
  181. USART_Cmd(USART2, ENABLE); // enable USART2
  182.  
  183. }
  184.  
  185. int diff(int16_t *prevBuff, int16_t *nowBuff)
  186. {
  187. int buffSize = 5, index;
  188.  
  189. for ( index = 0 ; index < buffSize ; index++)
  190. {
  191. if (prevBuff[index] != nowBuff[index])
  192. return 0;
  193. }
  194. return 1;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement