Advertisement
winston1777

Untitled

Oct 1st, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.44 KB | None | 0 0
  1. /**
  2. * @author Tilen Majerle
  3. * @email tilen@majerle.eu
  4. * @website http://stm32f4-discovery.com
  5. * @link http://stm32f4-discovery.com/2014/09/library-34-stm32f4-usb-hid-device
  6. * @version v1.0
  7. * @ide Keil uVision
  8. * @license GNU GPL v3
  9. * @brief USB HID Device library for STM32F4
  10. *
  11. @verbatim
  12. ----------------------------------------------------------------------
  13. Copyright (C) Tilen Majerle, 2015
  14.  
  15. This program is free software: you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation, either version 3 of the License, or
  18. any later version.
  19.  
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. ----------------------------------------------------------------------
  28. @endverbatim
  29. */
  30. #ifndef TM_USB_HIDDEVICE_H
  31. #define TM_USB_HIDDEVICE_H 100
  32. /**
  33. * @addtogroup TM_STM32F4xx_Libraries
  34. * @{
  35. */
  36.  
  37. /**
  38. * @defgroup TM_USB_HID_DEVICE
  39. * @brief USB HID Device library for STM32F4xx - http://stm32f4-discovery.com/2014/09/library-34-stm32f4-usb-hid-device
  40. * @{
  41. *
  42. * Library is designed to operate as HID device. This means that STM32F4xx device will be shown to your computer
  43. * as USB Keyboard, Mouse and 2 game pads at the same time.
  44. *
  45. * It works in USB FS or USB HS in FS mode.
  46. *
  47. * By default, library works in USB FS mode (for STM32F4-Discovery board).
  48. * If you want to use this on STM32F429-Discovery board, you have to activate USB HS in FS mode.
  49. * Activate this with lines below in your defines.h file:
  50. *
  51. @verbatim
  52. //Activate USB HS in FS mode
  53. #define USE_USB_OTG_HS
  54. @endverbatim
  55. *
  56. * \par Pinout
  57. *
  58. @verbatim
  59. USB |STM32F4xx FS mode |STM32F4xx HS in FS mode |Notes
  60. |STM32F4-Discovery |STM32F429-Discovery
  61.  
  62. Data + PA12 PB15 Data+ for USB, standard and used pin
  63. Data - PA11 PB14 Data- for USB, standard and used pin
  64. ID PA10 PB12 ID pin, used on F4 and F429 discovery boards, not needed if you don't like it
  65. VBUS PA9 PB13 VBUS pin, used on F4 and F429 discovery board for activating USB chip.
  66.  
  67. @endverbatim
  68. *
  69. * You have to use VBUS on discovery boards, but on nucleo, it's ok with only Data+ and Data- pins
  70. * Disable necessary pins
  71. *
  72. * USB technically needs only Data+ and Data- pins.
  73. * Also, ID pin can be used, but it is not needed.
  74. *
  75. * \par Disable ID PIN
  76. *
  77. * If you need pin for something else, where ID is located, you can disable this pin for USB.
  78. * Add lines below in your defines.h file:
  79. *
  80. @verbatim
  81. //Disable ID pin
  82. #define USB_HID_HOST_DISABLE_ID
  83. @endverbatim
  84. *
  85. * \par Disable VBUS PIN
  86. *
  87. * VBUS pin is located on Discovery boards, to activate USB chip on board.
  88. * If you are working with Discovery boards, then you need this pin, otherise USB will not work.
  89. * But if you are working on other application (or Nucleo board), you only need Data+ and Data- pins.
  90. * To disable VBUS pin, add lines below in your defines.h file:
  91. *
  92. @verbatim
  93. //Disable VBUS pin
  94. #define USB_HID_HOST_DISABLE_VBUS
  95. @endverbatim
  96. *
  97. * \par Changelog
  98. *
  99. @verbatim
  100. Version 1.0
  101. - First release
  102. @endverbatim
  103. *
  104. * \par Dependencies
  105. *
  106. @verbatim
  107. - STM32F4xx
  108. - STM32F4xx RCC
  109. - STM32F4xx GPIO
  110. - STM32F4xx EXTI
  111. - misc.h
  112. - defines.h
  113. - USB HID DEVICE
  114. @endverbatim
  115. */
  116.  
  117. #include "stm32f4xx.h"
  118. #include "stm32f4xx_rcc.h"
  119. #include "stm32f4xx_gpio.h"
  120. #include "stm32f4xx_exti.h"
  121. #include "misc.h"
  122. #include "defines.h"
  123.  
  124. #include "usbd_hid_core.h"
  125. #include "usbd_usr.h"
  126. #include "usbd_desc.h"
  127.  
  128. /**
  129. * @defgroup TM_USB_HID_DEVICE_Typedefs
  130. * @brief Library Typedefs
  131. * @{
  132. */
  133.  
  134. /**
  135. * @brief USB HID device enumeration
  136. */
  137. typedef enum {
  138. TM_USB_HIDDEVICE_Status_LibraryNotInitialized = 0x00, /*!< Library is not initialized yet */
  139. TM_USB_HIDDEVICE_Status_Connected, /*!< Device is connected and ready to use */
  140. TM_USB_HIDDEVICE_Status_Disconnected, /*!< Device is not connected */
  141. TM_USB_HIDDEVICE_Status_IdleMode, /*!< Device is is IDLE mode */
  142. TM_USB_HIDDEVICE_Status_SuspendMode /*!< Device is in suspend mode */
  143. } TM_USB_HIDDEVICE_Status_t;
  144.  
  145. /**
  146. * @brief Button status enumeration
  147. */
  148. typedef enum {
  149. TM_USB_HIDDEVICE_Button_Released = 0x00, /*!< Button is not pressed */
  150. TM_USB_HIDDEVICE_Button_Pressed = 0x01 /*!< Button is pressed */
  151. } TM_USB_HIDDEVICE_Button_t;
  152.  
  153. /**
  154. * @brief 2 Game pads are supported simultaneously to work with
  155. */
  156. typedef enum {
  157. TM_USB_HIDDEVICE_Gamepad_Number_1 = 0x03, /*!< Send data to computer for game pad 1 */
  158. TM_USB_HIDDEVICE_Gamepad_Number_2 = 0x04 /*!< Send data to computer for game pad 2 */
  159. } TM_USB_HIDDEVICE_Gamepad_Number_t;
  160.  
  161. /**
  162. * @brief Mouse structure, to work with mouse
  163. */
  164. typedef struct {
  165. TM_USB_HIDDEVICE_Button_t LeftButton; /*!< Detect if left button is pressed and set this to send command to computer, This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  166. TM_USB_HIDDEVICE_Button_t RightButton; /*!< Detect if right button is pressed and set this to send command to computer, This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  167. TM_USB_HIDDEVICE_Button_t MiddleButton; /*!< Detect if middle button is pressed and set this to send command to computer, This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  168. int8_t XAxis; /*!< Mouse X axis */
  169. int8_t YAxis; /*!< Mouse Y axis */
  170. int8_t Wheel; /*!< Mouse wheel rotation */
  171. } TM_USB_HIDDEVICE_Mouse_t;
  172.  
  173. /**
  174. * @brief Game pad structure for 2 game pads available
  175. */
  176. typedef struct {
  177. TM_USB_HIDDEVICE_Button_t Button1; /*!< Game pad button 1 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  178. TM_USB_HIDDEVICE_Button_t Button2; /*!< Game pad button 2 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  179. TM_USB_HIDDEVICE_Button_t Button3; /*!< Game pad button 3 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  180. TM_USB_HIDDEVICE_Button_t Button4; /*!< Game pad button 4 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  181. TM_USB_HIDDEVICE_Button_t Button5; /*!< Game pad button 5 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  182. TM_USB_HIDDEVICE_Button_t Button6; /*!< Game pad button 6 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  183. TM_USB_HIDDEVICE_Button_t Button7; /*!< Game pad button 7 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  184. TM_USB_HIDDEVICE_Button_t Button8; /*!< Game pad button 8 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  185. TM_USB_HIDDEVICE_Button_t Button9; /*!< Game pad button 9 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  186. TM_USB_HIDDEVICE_Button_t Button10; /*!< Game pad button 10 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  187. TM_USB_HIDDEVICE_Button_t Button11; /*!< Game pad button 11 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  188. TM_USB_HIDDEVICE_Button_t Button12; /*!< Game pad button 12 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  189. TM_USB_HIDDEVICE_Button_t Button13; /*!< Game pad button 13 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  190. TM_USB_HIDDEVICE_Button_t Button14; /*!< Game pad button 14 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  191. TM_USB_HIDDEVICE_Button_t Button15; /*!< Game pad button 15 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  192. TM_USB_HIDDEVICE_Button_t Button16; /*!< Game pad button 16 status. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  193. int8_t LeftXAxis; /*!< Left joystick X axis */
  194. int8_t LeftYAxis; /*!< Left joystick Y axis */
  195. int8_t RightXAxis; /*!< Right joystick X axis */
  196. int8_t RightYAxis; /*!< Right joystick Y axis */
  197. } TM_USB_HIDDEVICE_Gamepad_t;
  198.  
  199. /**
  200. * @brief Keyboard structure
  201. * @note Keyboard has special 8 buttons (CTRL, ALT, SHIFT, GUI (or WIN), all are left and right).
  202. * It also supports 6 others keys to be pressed at same time, let's say Key1 = 'a', Key2 = 'b', and you will get "ab" result on the screen.
  203. * If key is not used, then 0x00 value should be set!
  204. */
  205. typedef struct {
  206. TM_USB_HIDDEVICE_Button_t L_CTRL; /*!< Left CTRL button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  207. TM_USB_HIDDEVICE_Button_t L_ALT; /*!< Left ALT button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  208. TM_USB_HIDDEVICE_Button_t L_SHIFT; /*!< Left SHIFT button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  209. TM_USB_HIDDEVICE_Button_t L_GUI; /*!< Left GUI (Win) button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  210. TM_USB_HIDDEVICE_Button_t R_CTRL; /*!< Right CTRL button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  211. TM_USB_HIDDEVICE_Button_t R_ALT; /*!< Right ALT button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  212. TM_USB_HIDDEVICE_Button_t R_SHIFT; /*!< Right SHIFT button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  213. TM_USB_HIDDEVICE_Button_t R_GUI; /*!< Right GUI (Win) button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  214.  
  215.  
  216. uint8_t Key1; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  217. uint8_t Key2; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  218. uint8_t Key3; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  219. uint8_t Key4; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  220. uint8_t Key5; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  221. uint8_t Key6; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  222. uint8_t Key7;
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. } TM_USB_HIDDEVICE_Keyboard_t;
  230.  
  231.  
  232.  
  233.  
  234.  
  235. typedef struct {
  236. TM_USB_HIDDEVICE_Button_t Mute; /*!< Left CTRL button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  237. TM_USB_HIDDEVICE_Button_t VolumeUP; /*!< Left ALT button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  238. TM_USB_HIDDEVICE_Button_t VolumeDown; /*!< Left SHIFT button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  239. // TM_USB_HIDDEVICE_Button_t L_GUI; /*!< Left GUI (Win) button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  240. // TM_USB_HIDDEVICE_Button_t R_CTRL; /*!< Right CTRL button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  241. // TM_USB_HIDDEVICE_Button_t R_ALT; /*!< Right ALT button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  242. // TM_USB_HIDDEVICE_Button_t R_SHIFT; /*!< Right SHIFT button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  243. // TM_USB_HIDDEVICE_Button_t R_GUI; /*!< Right GUI (Win) button. This parameter can be a value of @ref TM_USB_HIDDEVICE_Button_t enumeration */
  244. //
  245. //
  246. // uint8_t Key1; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  247. // uint8_t Key2; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  248. // uint8_t Key3; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  249. // uint8_t Key4; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  250. // uint8_t Key5; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  251. // uint8_t Key6; /*!< Key used with keyboard. This can be whatever. Like numbers, letters, everything. */
  252. // uint8_t Key7;
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259. } TM_USB_HIDDEVICE_Consumer_t;
  260.  
  261.  
  262.  
  263. /**
  264. * @}
  265. */
  266.  
  267. /**
  268. * @defgroup TM_USB_HID_DEVICE_Functions
  269. * @brief Library Functions
  270. * @{
  271. */
  272.  
  273. /**
  274. * @brief Initializes USB HID Device library
  275. * @param None
  276. * @retval TM_USB_HIDDEVICE_Status_Disconnected
  277. */
  278. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_Init(void);
  279.  
  280. /**
  281. * @brief Gets USB status
  282. * @param None
  283. * @retval Library usage status:
  284. * - TM_USB_HIDDEVICE_Status_Connected: Library is ready to use with USB
  285. * - Others: Look for @ref TM_USB_HIDDEVICE_Status_t enumeration
  286. */
  287. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GetStatus(void);
  288.  
  289. /**
  290. * @brief Initializes structure for mouse
  291. * Sets default values, before you start working
  292. * @param *Mouse_Data: Pointer to empty @ref TM_USB_HIDDEVICE_Mouse_t structure
  293. * @retval Member of @ref TM_USB_HIDDEVICE_Status_t enumeration
  294. */
  295. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_MouseStructInit(TM_USB_HIDDEVICE_Mouse_t* Mouse_Data);
  296.  
  297. /**
  298. * @brief Sends mouse data over USB
  299. * @param *Mouse_Data: Pointer to empty @ref TM_USB_HIDDEVICE_Mouse_t structure to get data from
  300. * @retval Member of @ref TM_USB_HIDDEVICE_Status_t enumeration
  301. */
  302. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_MouseSend(TM_USB_HIDDEVICE_Mouse_t* Mouse_Data);
  303.  
  304. /**
  305. * @brief Sends command to release all mouse data in computer.
  306. * This will release all button in computer for mouse and cursor will stop
  307. * @param None
  308. * @retval Member of @ref TM_USB_HIDDEVICE_Status_t enumeration
  309. */
  310. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_MouseReleaseAll(void);
  311.  
  312. /**
  313. * @brief Initializes structure for game pad
  314. * Sets default values, before you start working with game pads
  315. * @param *Gamepad_Data: Pointer to empty @ref TM_USB_HIDDEVICE_Gamepad_t structure
  316. * @retval Member of @ref TM_USB_HIDDEVICE_Status_t enumeration
  317. */
  318. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GamepadStructInit(TM_USB_HIDDEVICE_Gamepad_t* Gamepad_Data);
  319.  
  320.  
  321.  
  322. /**
  323. * @brief Sends game pad report over USB
  324. * @param gamepad_id: Game pad number to work with. This parameter can be a value of @ref TM_USB_HIDDEVICE_Gamepad_Number_t enumeration
  325. * @param *Gamepad_Data: Pointer to working @ref TM_USB_HIDDEVICE_Gamepad_t structure to get data from
  326. * @retval Member of @ref TM_USB_HIDDEVICE_Status_t enumeration
  327. */
  328. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GamepadSend(TM_USB_HIDDEVICE_Gamepad_Number_t gamepad_id, TM_USB_HIDDEVICE_Gamepad_t* Gamepad_Data);
  329.  
  330. /**
  331. * @brief Releases all buttons and joysticks over USB
  332. * @note If you press a button, and don't release it, computer will recognize as long pressed button
  333. * @param gamepad_id: Game pad number to work with. This parameter can be a value of @ref TM_USB_HIDDEVICE_Gamepad_Number_t enumeration
  334. * @retval Member of @ref TM_USB_HIDDEVICE_Status_t enumeration
  335. */
  336. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_GamepadReleaseAll(TM_USB_HIDDEVICE_Gamepad_Number_t gamepad_id);
  337.  
  338. /**
  339. * @brief Sets default values to work with keyboard
  340. * @param *Keyboard_Data: Pointer to empty @ref TM_USB_HIDDEVICE_Keyboard_t structure
  341. * @retval Member of @ref TM_USB_HIDDEVICE_Status_t enumeration
  342. */
  343. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_KeyboardStructInit(TM_USB_HIDDEVICE_Keyboard_t* Keyboard_Data);
  344.  
  345.  
  346.  
  347.  
  348. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_ConsumerStructInit(TM_USB_HIDDEVICE_Consumer_t* Consumer_Data);
  349.  
  350.  
  351.  
  352.  
  353. /**
  354. * @brief Sends keyboard report over USB
  355. * @param *Keyboard_Data: Pointer to @ref TM_USB_HIDDEVICE_Keyboard_t structure to get data from
  356. * @retval Member of @ref TM_USB_HIDDEVICE_Status_t enumeration
  357. */
  358. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_KeyboardSend(TM_USB_HIDDEVICE_Keyboard_t* Keyboard_Data);
  359.  
  360.  
  361.  
  362.  
  363. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_ConsumerSend(TM_USB_HIDDEVICE_Consumer_t* Consumer_Data);
  364.  
  365.  
  366.  
  367. /**
  368. * @brief Release all buttons from keyboard
  369. * @note If you press a button and don't release it, computer will detect like long pressed button
  370. * @param None
  371. * @retval Member of @ref TM_USB_HIDDEVICE_Status_t enumeration
  372. */
  373. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_KeyboardReleaseAll(void);
  374.  
  375.  
  376.  
  377.  
  378. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_ConsumerReleaseAll(void);
  379.  
  380.  
  381.  
  382. /**
  383. * @brief Sends custom HID report over USB
  384. * @param *buff: Pointer to data to be sent
  385. * @param count: Number of bytes to be sent
  386. * @retval Member of @ref TM_USB_HIDDEVICE_Status_t enumeration
  387. */
  388. TM_USB_HIDDEVICE_Status_t TM_USB_HIDDEVICE_SendCustom(uint8_t* buff, uint8_t count);
  389.  
  390. /**
  391. * @}
  392. */
  393.  
  394. /**
  395. * @}
  396. */
  397.  
  398. /**
  399. * @}
  400. */
  401.  
  402. /* C++ detection */
  403. #ifdef __cplusplus
  404. }
  405. #endif
  406.  
  407. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement