drv_usb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * File : drv_usb.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-10-30 ZYH the first version
  13. * 2017-11-15 ZYH update to 3.0.0
  14. */
  15. #include "drv_usb.h"
  16. #include <rtthread.h>
  17. #include <rtdevice.h>
  18. #include "board.h"
  19. #define USB_DISCONNECT_PIN 30 //PA9
  20. static PCD_HandleTypeDef _stm_pcd;
  21. static struct udcd _stm_udc;
  22. static struct ep_id _ep_pool[] =
  23. {
  24. {0x0, USB_EP_ATTR_CONTROL, USB_DIR_INOUT, 64, ID_ASSIGNED },
  25. {0x1, USB_EP_ATTR_BULK, USB_DIR_IN, 64, ID_UNASSIGNED},
  26. {0x1, USB_EP_ATTR_BULK, USB_DIR_OUT, 64, ID_UNASSIGNED},
  27. {0x2, USB_EP_ATTR_INT, USB_DIR_OUT, 64, ID_UNASSIGNED},
  28. {0x2, USB_EP_ATTR_INT, USB_DIR_IN, 64, ID_UNASSIGNED},
  29. {0xFF, USB_EP_ATTR_TYPE_MASK, USB_DIR_MASK, 0, ID_ASSIGNED },
  30. };
  31. void USB_LP_CAN1_RX0_IRQHandler(void)
  32. {
  33. rt_interrupt_enter();
  34. HAL_PCD_IRQHandler(&_stm_pcd);
  35. rt_interrupt_leave();
  36. }
  37. void HAL_PCD_ResetCallback(PCD_HandleTypeDef *pcd)
  38. {
  39. /* open ep0 OUT and IN */
  40. HAL_PCD_EP_Open(pcd, 0x00, 0x40, EP_TYPE_CTRL);
  41. HAL_PCD_EP_Open(pcd, 0x80, 0x40, EP_TYPE_CTRL);
  42. rt_usbd_reset_handler(&_stm_udc);
  43. }
  44. void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  45. {
  46. rt_usbd_ep0_setup_handler(&_stm_udc, (struct urequest*)hpcd->Setup);
  47. }
  48. void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  49. {
  50. if (epnum == 0)
  51. {
  52. rt_usbd_ep0_in_handler(&_stm_udc);
  53. }
  54. else
  55. {
  56. rt_usbd_ep_in_handler(&_stm_udc, 0x80|epnum,hpcd->IN_ep[epnum].xfer_count);
  57. }
  58. }
  59. void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  60. {
  61. rt_usbd_connect_handler(&_stm_udc);
  62. }
  63. void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  64. {
  65. // rt_usbd_sof_handler(&_stm_udc);
  66. }
  67. void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  68. {
  69. rt_usbd_disconnect_handler(&_stm_udc);
  70. }
  71. void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  72. {
  73. if (epnum != 0)
  74. {
  75. rt_usbd_ep_out_handler(&_stm_udc, epnum, hpcd->OUT_ep[epnum].xfer_count);
  76. }
  77. else
  78. {
  79. rt_usbd_ep0_out_handler(&_stm_udc,hpcd->OUT_ep[0].xfer_count);
  80. }
  81. }
  82. void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
  83. {
  84. if(state == 1)
  85. {
  86. rt_pin_write(USB_DISCONNECT_PIN,PIN_HIGH);
  87. }
  88. else
  89. {
  90. rt_pin_write(USB_DISCONNECT_PIN,PIN_LOW);
  91. }
  92. }
  93. void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
  94. {
  95. if(pcdHandle->Instance==USB)
  96. {
  97. __HAL_RCC_GPIOA_CLK_ENABLE();
  98. rt_pin_mode(USB_DISCONNECT_PIN,PIN_MODE_OUTPUT);
  99. rt_pin_write(USB_DISCONNECT_PIN,PIN_LOW);
  100. /* Peripheral clock enable */
  101. __HAL_RCC_USB_CLK_ENABLE();
  102. /* Peripheral interrupt init */
  103. HAL_NVIC_SetPriority(USB_LP_CAN1_RX0_IRQn, 5, 0);
  104. HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
  105. }
  106. }
  107. void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
  108. {
  109. if(pcdHandle->Instance==USB)
  110. {
  111. /* Peripheral clock disable */
  112. __HAL_RCC_USB_CLK_DISABLE();
  113. /* Peripheral interrupt Deinit*/
  114. HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
  115. }
  116. }
  117. static rt_err_t _ep_set_stall(rt_uint8_t address)
  118. {
  119. HAL_PCD_EP_SetStall(&_stm_pcd, address);
  120. return RT_EOK;
  121. }
  122. static rt_err_t _ep_clear_stall(rt_uint8_t address)
  123. {
  124. HAL_PCD_EP_ClrStall(&_stm_pcd, address);
  125. return RT_EOK;
  126. }
  127. static rt_err_t _set_address(rt_uint8_t address)
  128. {
  129. HAL_PCD_SetAddress(&_stm_pcd, address);
  130. return RT_EOK;
  131. }
  132. static rt_err_t _set_config(rt_uint8_t address)
  133. {
  134. return RT_EOK;
  135. }
  136. static rt_err_t _ep_enable(uep_t ep)
  137. {
  138. RT_ASSERT(ep != RT_NULL);
  139. RT_ASSERT(ep->ep_desc != RT_NULL);
  140. HAL_PCD_EP_Open(&_stm_pcd, ep->ep_desc->bEndpointAddress,
  141. ep->ep_desc->wMaxPacketSize, ep->ep_desc->bmAttributes);
  142. return RT_EOK;
  143. }
  144. static rt_err_t _ep_disable(uep_t ep)
  145. {
  146. RT_ASSERT(ep != RT_NULL);
  147. RT_ASSERT(ep->ep_desc != RT_NULL);
  148. HAL_PCD_EP_Close(&_stm_pcd, ep->ep_desc->bEndpointAddress);
  149. return RT_EOK;
  150. }
  151. static rt_size_t _ep_read(rt_uint8_t address, void *buffer)
  152. {
  153. rt_size_t size = 0;
  154. RT_ASSERT(buffer != RT_NULL);
  155. return size;
  156. }
  157. static rt_size_t _ep_read_prepare(rt_uint8_t address, void *buffer, rt_size_t size)
  158. {
  159. HAL_PCD_EP_Receive(&_stm_pcd, address, buffer, size);
  160. return size;
  161. }
  162. static rt_size_t _ep_write(rt_uint8_t address, void *buffer, rt_size_t size)
  163. {
  164. HAL_PCD_EP_Transmit(&_stm_pcd, address, buffer, size);
  165. return size;
  166. }
  167. static rt_err_t _ep0_send_status(void)
  168. {
  169. HAL_PCD_EP_Transmit(&_stm_pcd, 0x00, NULL, 0);
  170. return RT_EOK;
  171. }
  172. static rt_err_t _suspend(void)
  173. {
  174. return RT_EOK;
  175. }
  176. static rt_err_t _wakeup(void)
  177. {
  178. return RT_EOK;
  179. }
  180. static rt_err_t _init(rt_device_t device)
  181. {
  182. PCD_HandleTypeDef *pcd;
  183. /* Set LL Driver parameters */
  184. pcd = (PCD_HandleTypeDef*)device->user_data;
  185. pcd->Instance = USB;
  186. pcd->Init.dev_endpoints = 8;
  187. pcd->Init.speed = PCD_SPEED_FULL;
  188. pcd->Init.ep0_mps = DEP0CTL_MPS_8;
  189. pcd->Init.low_power_enable = DISABLE;
  190. pcd->Init.lpm_enable = DISABLE;
  191. pcd->Init.battery_charging_enable = DISABLE;
  192. /* Initialize LL Driver */
  193. HAL_PCD_Init(pcd);
  194. HAL_PCDEx_PMAConfig(pcd , 0x00 , PCD_SNG_BUF, 0x18);
  195. HAL_PCDEx_PMAConfig(pcd , 0x80 , PCD_SNG_BUF, 0x58);
  196. HAL_PCDEx_PMAConfig(pcd , 0x81 , PCD_SNG_BUF, 0x98);
  197. HAL_PCDEx_PMAConfig(pcd , 0x01 , PCD_SNG_BUF, 0x118);
  198. HAL_PCDEx_PMAConfig(pcd , 0x82 , PCD_SNG_BUF, 0xD8);
  199. HAL_PCDEx_PMAConfig(pcd , 0x02 , PCD_SNG_BUF, 0x158);
  200. HAL_PCD_Start(pcd);
  201. return RT_EOK;
  202. }
  203. const static struct udcd_ops _udc_ops =
  204. {
  205. _set_address,
  206. _set_config,
  207. _ep_set_stall,
  208. _ep_clear_stall,
  209. _ep_enable,
  210. _ep_disable,
  211. _ep_read_prepare,
  212. _ep_read,
  213. _ep_write,
  214. _ep0_send_status,
  215. _suspend,
  216. _wakeup,
  217. };
  218. int stm_usbd_register(void)
  219. {
  220. rt_memset((void *)&_stm_udc, 0, sizeof(struct udcd));
  221. _stm_udc.parent.type = RT_Device_Class_USBDevice;
  222. _stm_udc.parent.init = _init;
  223. _stm_udc.parent.user_data = &_stm_pcd;
  224. _stm_udc.ops = &_udc_ops;
  225. /* Register endpoint infomation */
  226. _stm_udc.ep_pool = _ep_pool;
  227. _stm_udc.ep0.id = &_ep_pool[0];
  228. rt_device_register((rt_device_t)&_stm_udc, "usbd", 0);
  229. rt_usb_device_init();
  230. return 0;
  231. }
  232. INIT_DEVICE_EXPORT(stm_usbd_register);