Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1066 lines
38 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_pcd.h
  4. * @author MCD Application Team
  5. * @brief Header file of PCD HAL module.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* Define to prevent recursive inclusion -------------------------------------*/
  20. #ifndef STM32F1xx_HAL_PCD_H
  21. #define STM32F1xx_HAL_PCD_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* Includes ------------------------------------------------------------------*/
  26. #include "stm32f1xx_ll_usb.h"
  27. #if defined (USB) || defined (USB_OTG_FS)
  28. /** @addtogroup STM32F1xx_HAL_Driver
  29. * @{
  30. */
  31. /** @addtogroup PCD
  32. * @{
  33. */
  34. /* Exported types ------------------------------------------------------------*/
  35. /** @defgroup PCD_Exported_Types PCD Exported Types
  36. * @{
  37. */
  38. /**
  39. * @brief PCD State structure definition
  40. */
  41. typedef enum
  42. {
  43. HAL_PCD_STATE_RESET = 0x00,
  44. HAL_PCD_STATE_READY = 0x01,
  45. HAL_PCD_STATE_ERROR = 0x02,
  46. HAL_PCD_STATE_BUSY = 0x03,
  47. HAL_PCD_STATE_TIMEOUT = 0x04
  48. } PCD_StateTypeDef;
  49. /* Device LPM suspend state */
  50. typedef enum
  51. {
  52. LPM_L0 = 0x00, /* on */
  53. LPM_L1 = 0x01, /* LPM L1 sleep */
  54. LPM_L2 = 0x02, /* suspend */
  55. LPM_L3 = 0x03, /* off */
  56. } PCD_LPM_StateTypeDef;
  57. typedef enum
  58. {
  59. PCD_LPM_L0_ACTIVE = 0x00, /* on */
  60. PCD_LPM_L1_ACTIVE = 0x01, /* LPM L1 sleep */
  61. } PCD_LPM_MsgTypeDef;
  62. typedef enum
  63. {
  64. PCD_BCD_ERROR = 0xFF,
  65. PCD_BCD_CONTACT_DETECTION = 0xFE,
  66. PCD_BCD_STD_DOWNSTREAM_PORT = 0xFD,
  67. PCD_BCD_CHARGING_DOWNSTREAM_PORT = 0xFC,
  68. PCD_BCD_DEDICATED_CHARGING_PORT = 0xFB,
  69. PCD_BCD_DISCOVERY_COMPLETED = 0x00,
  70. } PCD_BCD_MsgTypeDef;
  71. #if defined (USB)
  72. #endif /* defined (USB) */
  73. #if defined (USB_OTG_FS)
  74. typedef USB_OTG_GlobalTypeDef PCD_TypeDef;
  75. typedef USB_OTG_CfgTypeDef PCD_InitTypeDef;
  76. typedef USB_OTG_EPTypeDef PCD_EPTypeDef;
  77. #endif /* defined (USB_OTG_FS) */
  78. #if defined (USB)
  79. typedef USB_TypeDef PCD_TypeDef;
  80. typedef USB_CfgTypeDef PCD_InitTypeDef;
  81. typedef USB_EPTypeDef PCD_EPTypeDef;
  82. #endif /* defined (USB) */
  83. /**
  84. * @brief PCD Handle Structure definition
  85. */
  86. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  87. typedef struct __PCD_HandleTypeDef
  88. #else
  89. typedef struct
  90. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  91. {
  92. PCD_TypeDef *Instance; /*!< Register base address */
  93. PCD_InitTypeDef Init; /*!< PCD required parameters */
  94. __IO uint8_t USB_Address; /*!< USB Address */
  95. #if defined (USB_OTG_FS)
  96. PCD_EPTypeDef IN_ep[16]; /*!< IN endpoint parameters */
  97. PCD_EPTypeDef OUT_ep[16]; /*!< OUT endpoint parameters */
  98. #endif /* defined (USB_OTG_FS) */
  99. #if defined (USB)
  100. PCD_EPTypeDef IN_ep[8]; /*!< IN endpoint parameters */
  101. PCD_EPTypeDef OUT_ep[8]; /*!< OUT endpoint parameters */
  102. #endif /* defined (USB) */
  103. HAL_LockTypeDef Lock; /*!< PCD peripheral status */
  104. __IO PCD_StateTypeDef State; /*!< PCD communication state */
  105. __IO uint32_t ErrorCode; /*!< PCD Error code */
  106. uint32_t Setup[12]; /*!< Setup packet buffer */
  107. PCD_LPM_StateTypeDef LPM_State; /*!< LPM State */
  108. uint32_t BESL;
  109. void *pData; /*!< Pointer to upper stack Handler */
  110. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  111. void (* SOFCallback)(struct __PCD_HandleTypeDef *hpcd); /*!< USB OTG PCD SOF callback */
  112. void (* SetupStageCallback)(struct __PCD_HandleTypeDef *hpcd); /*!< USB OTG PCD Setup Stage callback */
  113. void (* ResetCallback)(struct __PCD_HandleTypeDef *hpcd); /*!< USB OTG PCD Reset callback */
  114. void (* SuspendCallback)(struct __PCD_HandleTypeDef *hpcd); /*!< USB OTG PCD Suspend callback */
  115. void (* ResumeCallback)(struct __PCD_HandleTypeDef *hpcd); /*!< USB OTG PCD Resume callback */
  116. void (* ConnectCallback)(struct __PCD_HandleTypeDef *hpcd); /*!< USB OTG PCD Connect callback */
  117. void (* DisconnectCallback)(struct __PCD_HandleTypeDef *hpcd); /*!< USB OTG PCD Disconnect callback */
  118. void (* DataOutStageCallback)(struct __PCD_HandleTypeDef *hpcd, uint8_t epnum); /*!< USB OTG PCD Data OUT Stage callback */
  119. void (* DataInStageCallback)(struct __PCD_HandleTypeDef *hpcd, uint8_t epnum); /*!< USB OTG PCD Data IN Stage callback */
  120. void (* ISOOUTIncompleteCallback)(struct __PCD_HandleTypeDef *hpcd, uint8_t epnum); /*!< USB OTG PCD ISO OUT Incomplete callback */
  121. void (* ISOINIncompleteCallback)(struct __PCD_HandleTypeDef *hpcd, uint8_t epnum); /*!< USB OTG PCD ISO IN Incomplete callback */
  122. void (* MspInitCallback)(struct __PCD_HandleTypeDef *hpcd); /*!< USB OTG PCD Msp Init callback */
  123. void (* MspDeInitCallback)(struct __PCD_HandleTypeDef *hpcd); /*!< USB OTG PCD Msp DeInit callback */
  124. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  125. } PCD_HandleTypeDef;
  126. /**
  127. * @}
  128. */
  129. /* Include PCD HAL Extended module */
  130. #include "stm32f1xx_hal_pcd_ex.h"
  131. /* Exported constants --------------------------------------------------------*/
  132. /** @defgroup PCD_Exported_Constants PCD Exported Constants
  133. * @{
  134. */
  135. /** @defgroup PCD_Speed PCD Speed
  136. * @{
  137. */
  138. #define PCD_SPEED_FULL USBD_FS_SPEED
  139. /**
  140. * @}
  141. */
  142. /** @defgroup PCD_PHY_Module PCD PHY Module
  143. * @{
  144. */
  145. #define PCD_PHY_ULPI 1U
  146. #define PCD_PHY_EMBEDDED 2U
  147. #define PCD_PHY_UTMI 3U
  148. /**
  149. * @}
  150. */
  151. /** @defgroup PCD_Error_Code_definition PCD Error Code definition
  152. * @brief PCD Error Code definition
  153. * @{
  154. */
  155. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  156. #define HAL_PCD_ERROR_INVALID_CALLBACK (0x00000010U) /*!< Invalid Callback error */
  157. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  158. /**
  159. * @}
  160. */
  161. /**
  162. * @}
  163. */
  164. /* Exported macros -----------------------------------------------------------*/
  165. /** @defgroup PCD_Exported_Macros PCD Exported Macros
  166. * @brief macros to handle interrupts and specific clock configurations
  167. * @{
  168. */
  169. #if defined (USB_OTG_FS)
  170. #define __HAL_PCD_ENABLE(__HANDLE__) (void)USB_EnableGlobalInt ((__HANDLE__)->Instance)
  171. #define __HAL_PCD_DISABLE(__HANDLE__) (void)USB_DisableGlobalInt ((__HANDLE__)->Instance)
  172. #define __HAL_PCD_GET_FLAG(__HANDLE__, __INTERRUPT__) \
  173. ((USB_ReadInterrupts((__HANDLE__)->Instance) & (__INTERRUPT__)) == (__INTERRUPT__))
  174. #define __HAL_PCD_CLEAR_FLAG(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->GINTSTS) &= (__INTERRUPT__))
  175. #define __HAL_PCD_IS_INVALID_INTERRUPT(__HANDLE__) (USB_ReadInterrupts((__HANDLE__)->Instance) == 0U)
  176. #define __HAL_PCD_UNGATE_PHYCLOCK(__HANDLE__) \
  177. *(__IO uint32_t *)((uint32_t)((__HANDLE__)->Instance) + USB_OTG_PCGCCTL_BASE) &= ~(USB_OTG_PCGCCTL_STOPCLK)
  178. #define __HAL_PCD_GATE_PHYCLOCK(__HANDLE__) \
  179. *(__IO uint32_t *)((uint32_t)((__HANDLE__)->Instance) + USB_OTG_PCGCCTL_BASE) |= USB_OTG_PCGCCTL_STOPCLK
  180. #define __HAL_PCD_IS_PHY_SUSPENDED(__HANDLE__) \
  181. ((*(__IO uint32_t *)((uint32_t)((__HANDLE__)->Instance) + USB_OTG_PCGCCTL_BASE)) & 0x10U)
  182. #define __HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_IT() EXTI->IMR |= USB_OTG_FS_WAKEUP_EXTI_LINE
  183. #define __HAL_USB_OTG_FS_WAKEUP_EXTI_DISABLE_IT() EXTI->IMR &= ~(USB_OTG_FS_WAKEUP_EXTI_LINE)
  184. #define __HAL_USB_OTG_FS_WAKEUP_EXTI_GET_FLAG() EXTI->PR & (USB_OTG_FS_WAKEUP_EXTI_LINE)
  185. #define __HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG() EXTI->PR = USB_OTG_FS_WAKEUP_EXTI_LINE
  186. #define __HAL_USB_OTG_FS_WAKEUP_EXTI_ENABLE_RISING_EDGE() \
  187. do { \
  188. EXTI->FTSR &= ~(USB_OTG_FS_WAKEUP_EXTI_LINE); \
  189. EXTI->RTSR |= USB_OTG_FS_WAKEUP_EXTI_LINE; \
  190. } while(0U)
  191. #endif /* defined (USB_OTG_FS) */
  192. #if defined (USB)
  193. #define __HAL_PCD_ENABLE(__HANDLE__) (void)USB_EnableGlobalInt ((__HANDLE__)->Instance)
  194. #define __HAL_PCD_DISABLE(__HANDLE__) (void)USB_DisableGlobalInt ((__HANDLE__)->Instance)
  195. #define __HAL_PCD_GET_FLAG(__HANDLE__, __INTERRUPT__) ((USB_ReadInterrupts((__HANDLE__)->Instance)\
  196. & (__INTERRUPT__)) == (__INTERRUPT__))
  197. #define __HAL_PCD_CLEAR_FLAG(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->ISTR)\
  198. &= (uint16_t)(~(__INTERRUPT__)))
  199. #define __HAL_USB_WAKEUP_EXTI_ENABLE_IT() EXTI->IMR |= USB_WAKEUP_EXTI_LINE
  200. #define __HAL_USB_WAKEUP_EXTI_DISABLE_IT() EXTI->IMR &= ~(USB_WAKEUP_EXTI_LINE)
  201. #define __HAL_USB_WAKEUP_EXTI_GET_FLAG() EXTI->PR & (USB_WAKEUP_EXTI_LINE)
  202. #define __HAL_USB_WAKEUP_EXTI_CLEAR_FLAG() EXTI->PR = USB_WAKEUP_EXTI_LINE
  203. #define __HAL_USB_WAKEUP_EXTI_ENABLE_RISING_EDGE() \
  204. do { \
  205. EXTI->FTSR &= ~(USB_WAKEUP_EXTI_LINE); \
  206. EXTI->RTSR |= USB_WAKEUP_EXTI_LINE; \
  207. } while(0U)
  208. #endif /* defined (USB) */
  209. /**
  210. * @}
  211. */
  212. /* Exported functions --------------------------------------------------------*/
  213. /** @addtogroup PCD_Exported_Functions PCD Exported Functions
  214. * @{
  215. */
  216. /* Initialization/de-initialization functions ********************************/
  217. /** @addtogroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
  218. * @{
  219. */
  220. HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd);
  221. HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd);
  222. void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd);
  223. void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd);
  224. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  225. /** @defgroup HAL_PCD_Callback_ID_enumeration_definition HAL USB OTG PCD Callback ID enumeration definition
  226. * @brief HAL USB OTG PCD Callback ID enumeration definition
  227. * @{
  228. */
  229. typedef enum
  230. {
  231. HAL_PCD_SOF_CB_ID = 0x01, /*!< USB PCD SOF callback ID */
  232. HAL_PCD_SETUPSTAGE_CB_ID = 0x02, /*!< USB PCD Setup Stage callback ID */
  233. HAL_PCD_RESET_CB_ID = 0x03, /*!< USB PCD Reset callback ID */
  234. HAL_PCD_SUSPEND_CB_ID = 0x04, /*!< USB PCD Suspend callback ID */
  235. HAL_PCD_RESUME_CB_ID = 0x05, /*!< USB PCD Resume callback ID */
  236. HAL_PCD_CONNECT_CB_ID = 0x06, /*!< USB PCD Connect callback ID */
  237. HAL_PCD_DISCONNECT_CB_ID = 0x07, /*!< USB PCD Disconnect callback ID */
  238. HAL_PCD_MSPINIT_CB_ID = 0x08, /*!< USB PCD MspInit callback ID */
  239. HAL_PCD_MSPDEINIT_CB_ID = 0x09 /*!< USB PCD MspDeInit callback ID */
  240. } HAL_PCD_CallbackIDTypeDef;
  241. /**
  242. * @}
  243. */
  244. /** @defgroup HAL_PCD_Callback_pointer_definition HAL USB OTG PCD Callback pointer definition
  245. * @brief HAL USB OTG PCD Callback pointer definition
  246. * @{
  247. */
  248. typedef void (*pPCD_CallbackTypeDef)(PCD_HandleTypeDef *hpcd); /*!< pointer to a common USB OTG PCD callback function */
  249. typedef void (*pPCD_DataOutStageCallbackTypeDef)(PCD_HandleTypeDef *hpcd, uint8_t epnum); /*!< pointer to USB OTG PCD Data OUT Stage callback */
  250. typedef void (*pPCD_DataInStageCallbackTypeDef)(PCD_HandleTypeDef *hpcd, uint8_t epnum); /*!< pointer to USB OTG PCD Data IN Stage callback */
  251. typedef void (*pPCD_IsoOutIncpltCallbackTypeDef)(PCD_HandleTypeDef *hpcd, uint8_t epnum); /*!< pointer to USB OTG PCD ISO OUT Incomplete callback */
  252. typedef void (*pPCD_IsoInIncpltCallbackTypeDef)(PCD_HandleTypeDef *hpcd, uint8_t epnum); /*!< pointer to USB OTG PCD ISO IN Incomplete callback */
  253. /**
  254. * @}
  255. */
  256. HAL_StatusTypeDef HAL_PCD_RegisterCallback(PCD_HandleTypeDef *hpcd,
  257. HAL_PCD_CallbackIDTypeDef CallbackID,
  258. pPCD_CallbackTypeDef pCallback);
  259. HAL_StatusTypeDef HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef *hpcd,
  260. HAL_PCD_CallbackIDTypeDef CallbackID);
  261. HAL_StatusTypeDef HAL_PCD_RegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd,
  262. pPCD_DataOutStageCallbackTypeDef pCallback);
  263. HAL_StatusTypeDef HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd);
  264. HAL_StatusTypeDef HAL_PCD_RegisterDataInStageCallback(PCD_HandleTypeDef *hpcd,
  265. pPCD_DataInStageCallbackTypeDef pCallback);
  266. HAL_StatusTypeDef HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef *hpcd);
  267. HAL_StatusTypeDef HAL_PCD_RegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd,
  268. pPCD_IsoOutIncpltCallbackTypeDef pCallback);
  269. HAL_StatusTypeDef HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd);
  270. HAL_StatusTypeDef HAL_PCD_RegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd,
  271. pPCD_IsoInIncpltCallbackTypeDef pCallback);
  272. HAL_StatusTypeDef HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd);
  273. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  274. /**
  275. * @}
  276. */
  277. /* I/O operation functions ***************************************************/
  278. /* Non-Blocking mode: Interrupt */
  279. /** @addtogroup PCD_Exported_Functions_Group2 Input and Output operation functions
  280. * @{
  281. */
  282. HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd);
  283. HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd);
  284. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd);
  285. void HAL_PCD_WKUP_IRQHandler(PCD_HandleTypeDef *hpcd);
  286. void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd);
  287. void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd);
  288. void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd);
  289. void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd);
  290. void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd);
  291. void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd);
  292. void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd);
  293. void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum);
  294. void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum);
  295. void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum);
  296. void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum);
  297. /**
  298. * @}
  299. */
  300. /* Peripheral Control functions **********************************************/
  301. /** @addtogroup PCD_Exported_Functions_Group3 Peripheral Control functions
  302. * @{
  303. */
  304. HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd);
  305. HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd);
  306. HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address);
  307. HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
  308. uint16_t ep_mps, uint8_t ep_type);
  309. HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr);
  310. HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
  311. uint8_t *pBuf, uint32_t len);
  312. HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
  313. uint8_t *pBuf, uint32_t len);
  314. HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr);
  315. HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr);
  316. HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr);
  317. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd);
  318. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd);
  319. uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr);
  320. /**
  321. * @}
  322. */
  323. /* Peripheral State functions ************************************************/
  324. /** @addtogroup PCD_Exported_Functions_Group4 Peripheral State functions
  325. * @{
  326. */
  327. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd);
  328. /**
  329. * @}
  330. */
  331. /**
  332. * @}
  333. */
  334. /* Private constants ---------------------------------------------------------*/
  335. /** @defgroup PCD_Private_Constants PCD Private Constants
  336. * @{
  337. */
  338. /** @defgroup USB_EXTI_Line_Interrupt USB EXTI line interrupt
  339. * @{
  340. */
  341. #if defined (USB_OTG_FS)
  342. #define USB_OTG_FS_WAKEUP_EXTI_LINE (0x1U << 18) /*!< USB FS EXTI Line WakeUp Interrupt */
  343. #endif /* defined (USB_OTG_FS) */
  344. #if defined (USB)
  345. #define USB_WAKEUP_EXTI_LINE (0x1U << 18) /*!< USB FS EXTI Line WakeUp Interrupt */
  346. #endif /* defined (USB) */
  347. /**
  348. * @}
  349. */
  350. #if defined (USB)
  351. /** @defgroup PCD_EP0_MPS PCD EP0 MPS
  352. * @{
  353. */
  354. #define PCD_EP0MPS_64 EP_MPS_64
  355. #define PCD_EP0MPS_32 EP_MPS_32
  356. #define PCD_EP0MPS_16 EP_MPS_16
  357. #define PCD_EP0MPS_08 EP_MPS_8
  358. /**
  359. * @}
  360. */
  361. /** @defgroup PCD_ENDP PCD ENDP
  362. * @{
  363. */
  364. #define PCD_ENDP0 0U
  365. #define PCD_ENDP1 1U
  366. #define PCD_ENDP2 2U
  367. #define PCD_ENDP3 3U
  368. #define PCD_ENDP4 4U
  369. #define PCD_ENDP5 5U
  370. #define PCD_ENDP6 6U
  371. #define PCD_ENDP7 7U
  372. /**
  373. * @}
  374. */
  375. /** @defgroup PCD_ENDP_Kind PCD Endpoint Kind
  376. * @{
  377. */
  378. #define PCD_SNG_BUF 0U
  379. #define PCD_DBL_BUF 1U
  380. /**
  381. * @}
  382. */
  383. #endif /* defined (USB) */
  384. /**
  385. * @}
  386. */
  387. #if defined (USB_OTG_FS)
  388. #ifndef USB_OTG_DOEPINT_OTEPSPR
  389. #define USB_OTG_DOEPINT_OTEPSPR (0x1UL << 5) /*!< Status Phase Received interrupt */
  390. #endif /* defined USB_OTG_DOEPINT_OTEPSPR */
  391. #ifndef USB_OTG_DOEPMSK_OTEPSPRM
  392. #define USB_OTG_DOEPMSK_OTEPSPRM (0x1UL << 5) /*!< Setup Packet Received interrupt mask */
  393. #endif /* defined USB_OTG_DOEPMSK_OTEPSPRM */
  394. #ifndef USB_OTG_DOEPINT_NAK
  395. #define USB_OTG_DOEPINT_NAK (0x1UL << 13) /*!< NAK interrupt */
  396. #endif /* defined USB_OTG_DOEPINT_NAK */
  397. #ifndef USB_OTG_DOEPMSK_NAKM
  398. #define USB_OTG_DOEPMSK_NAKM (0x1UL << 13) /*!< OUT Packet NAK interrupt mask */
  399. #endif /* defined USB_OTG_DOEPMSK_NAKM */
  400. #ifndef USB_OTG_DOEPINT_STPKTRX
  401. #define USB_OTG_DOEPINT_STPKTRX (0x1UL << 15) /*!< Setup Packet Received interrupt */
  402. #endif /* defined USB_OTG_DOEPINT_STPKTRX */
  403. #ifndef USB_OTG_DOEPMSK_NYETM
  404. #define USB_OTG_DOEPMSK_NYETM (0x1UL << 14) /*!< Setup Packet Received interrupt mask */
  405. #endif /* defined USB_OTG_DOEPMSK_NYETM */
  406. #endif /* defined (USB_OTG_FS) */
  407. /* Private macros ------------------------------------------------------------*/
  408. /** @defgroup PCD_Private_Macros PCD Private Macros
  409. * @{
  410. */
  411. #if defined (USB)
  412. /******************** Bit definition for USB_COUNTn_RX register *************/
  413. #define USB_CNTRX_NBLK_MSK (0x1FU << 10)
  414. #define USB_CNTRX_BLSIZE (0x1U << 15)
  415. /* SetENDPOINT */
  416. #define PCD_SET_ENDPOINT(USBx, bEpNum, wRegValue) (*(__IO uint16_t *)\
  417. (&(USBx)->EP0R + ((bEpNum) * 2U)) = (uint16_t)(wRegValue))
  418. /* GetENDPOINT */
  419. #define PCD_GET_ENDPOINT(USBx, bEpNum) (*(__IO uint16_t *)(&(USBx)->EP0R + ((bEpNum) * 2U)))
  420. /* ENDPOINT transfer */
  421. #define USB_EP0StartXfer USB_EPStartXfer
  422. /**
  423. * @brief sets the type in the endpoint register(bits EP_TYPE[1:0])
  424. * @param USBx USB peripheral instance register address.
  425. * @param bEpNum Endpoint Number.
  426. * @param wType Endpoint Type.
  427. * @retval None
  428. */
  429. #define PCD_SET_EPTYPE(USBx, bEpNum, wType) (PCD_SET_ENDPOINT((USBx), (bEpNum), ((PCD_GET_ENDPOINT((USBx), (bEpNum))\
  430. & USB_EP_T_MASK) | (wType) | USB_EP_CTR_TX | USB_EP_CTR_RX)))
  431. /**
  432. * @brief gets the type in the endpoint register(bits EP_TYPE[1:0])
  433. * @param USBx USB peripheral instance register address.
  434. * @param bEpNum Endpoint Number.
  435. * @retval Endpoint Type
  436. */
  437. #define PCD_GET_EPTYPE(USBx, bEpNum) (PCD_GET_ENDPOINT((USBx), (bEpNum)) & USB_EP_T_FIELD)
  438. /**
  439. * @brief free buffer used from the application realizing it to the line
  440. * toggles bit SW_BUF in the double buffered endpoint register
  441. * @param USBx USB device.
  442. * @param bEpNum, bDir
  443. * @retval None
  444. */
  445. #define PCD_FreeUserBuffer(USBx, bEpNum, bDir) \
  446. do { \
  447. if ((bDir) == 0U) \
  448. { \
  449. /* OUT double buffered endpoint */ \
  450. PCD_TX_DTOG((USBx), (bEpNum)); \
  451. } \
  452. else if ((bDir) == 1U) \
  453. { \
  454. /* IN double buffered endpoint */ \
  455. PCD_RX_DTOG((USBx), (bEpNum)); \
  456. } \
  457. } while(0)
  458. /**
  459. * @brief sets the status for tx transfer (bits STAT_TX[1:0]).
  460. * @param USBx USB peripheral instance register address.
  461. * @param bEpNum Endpoint Number.
  462. * @param wState new state
  463. * @retval None
  464. */
  465. #define PCD_SET_EP_TX_STATUS(USBx, bEpNum, wState) \
  466. do { \
  467. uint16_t _wRegVal; \
  468. \
  469. _wRegVal = PCD_GET_ENDPOINT((USBx), (bEpNum)) & USB_EPTX_DTOGMASK; \
  470. /* toggle first bit ? */ \
  471. if ((USB_EPTX_DTOG1 & (wState))!= 0U) \
  472. { \
  473. _wRegVal ^= USB_EPTX_DTOG1; \
  474. } \
  475. /* toggle second bit ? */ \
  476. if ((USB_EPTX_DTOG2 & (wState))!= 0U) \
  477. { \
  478. _wRegVal ^= USB_EPTX_DTOG2; \
  479. } \
  480. PCD_SET_ENDPOINT((USBx), (bEpNum), (_wRegVal | USB_EP_CTR_RX | USB_EP_CTR_TX)); \
  481. } while(0) /* PCD_SET_EP_TX_STATUS */
  482. /**
  483. * @brief sets the status for rx transfer (bits STAT_TX[1:0])
  484. * @param USBx USB peripheral instance register address.
  485. * @param bEpNum Endpoint Number.
  486. * @param wState new state
  487. * @retval None
  488. */
  489. #define PCD_SET_EP_RX_STATUS(USBx, bEpNum,wState) \
  490. do { \
  491. uint16_t _wRegVal; \
  492. \
  493. _wRegVal = PCD_GET_ENDPOINT((USBx), (bEpNum)) & USB_EPRX_DTOGMASK; \
  494. /* toggle first bit ? */ \
  495. if ((USB_EPRX_DTOG1 & (wState))!= 0U) \
  496. { \
  497. _wRegVal ^= USB_EPRX_DTOG1; \
  498. } \
  499. /* toggle second bit ? */ \
  500. if ((USB_EPRX_DTOG2 & (wState))!= 0U) \
  501. { \
  502. _wRegVal ^= USB_EPRX_DTOG2; \
  503. } \
  504. PCD_SET_ENDPOINT((USBx), (bEpNum), (_wRegVal | USB_EP_CTR_RX | USB_EP_CTR_TX)); \
  505. } while(0) /* PCD_SET_EP_RX_STATUS */
  506. /**
  507. * @brief sets the status for rx & tx (bits STAT_TX[1:0] & STAT_RX[1:0])
  508. * @param USBx USB peripheral instance register address.
  509. * @param bEpNum Endpoint Number.
  510. * @param wStaterx new state.
  511. * @param wStatetx new state.
  512. * @retval None
  513. */
  514. #define PCD_SET_EP_TXRX_STATUS(USBx, bEpNum, wStaterx, wStatetx) \
  515. do { \
  516. uint16_t _wRegVal; \
  517. \
  518. _wRegVal = PCD_GET_ENDPOINT((USBx), (bEpNum)) & (USB_EPRX_DTOGMASK | USB_EPTX_STAT); \
  519. /* toggle first bit ? */ \
  520. if ((USB_EPRX_DTOG1 & (wStaterx))!= 0U) \
  521. { \
  522. _wRegVal ^= USB_EPRX_DTOG1; \
  523. } \
  524. /* toggle second bit ? */ \
  525. if ((USB_EPRX_DTOG2 & (wStaterx))!= 0U) \
  526. { \
  527. _wRegVal ^= USB_EPRX_DTOG2; \
  528. } \
  529. /* toggle first bit ? */ \
  530. if ((USB_EPTX_DTOG1 & (wStatetx))!= 0U) \
  531. { \
  532. _wRegVal ^= USB_EPTX_DTOG1; \
  533. } \
  534. /* toggle second bit ? */ \
  535. if ((USB_EPTX_DTOG2 & (wStatetx))!= 0U) \
  536. { \
  537. _wRegVal ^= USB_EPTX_DTOG2; \
  538. } \
  539. \
  540. PCD_SET_ENDPOINT((USBx), (bEpNum), (_wRegVal | USB_EP_CTR_RX | USB_EP_CTR_TX)); \
  541. } while(0) /* PCD_SET_EP_TXRX_STATUS */
  542. /**
  543. * @brief gets the status for tx/rx transfer (bits STAT_TX[1:0]
  544. * /STAT_RX[1:0])
  545. * @param USBx USB peripheral instance register address.
  546. * @param bEpNum Endpoint Number.
  547. * @retval status
  548. */
  549. #define PCD_GET_EP_TX_STATUS(USBx, bEpNum) ((uint16_t)PCD_GET_ENDPOINT((USBx), (bEpNum)) & USB_EPTX_STAT)
  550. #define PCD_GET_EP_RX_STATUS(USBx, bEpNum) ((uint16_t)PCD_GET_ENDPOINT((USBx), (bEpNum)) & USB_EPRX_STAT)
  551. /**
  552. * @brief sets directly the VALID tx/rx-status into the endpoint register
  553. * @param USBx USB peripheral instance register address.
  554. * @param bEpNum Endpoint Number.
  555. * @retval None
  556. */
  557. #define PCD_SET_EP_TX_VALID(USBx, bEpNum) (PCD_SET_EP_TX_STATUS((USBx), (bEpNum), USB_EP_TX_VALID))
  558. #define PCD_SET_EP_RX_VALID(USBx, bEpNum) (PCD_SET_EP_RX_STATUS((USBx), (bEpNum), USB_EP_RX_VALID))
  559. /**
  560. * @brief checks stall condition in an endpoint.
  561. * @param USBx USB peripheral instance register address.
  562. * @param bEpNum Endpoint Number.
  563. * @retval TRUE = endpoint in stall condition.
  564. */
  565. #define PCD_GET_EP_TX_STALL_STATUS(USBx, bEpNum) (PCD_GET_EP_TX_STATUS((USBx), (bEpNum)) == USB_EP_TX_STALL)
  566. #define PCD_GET_EP_RX_STALL_STATUS(USBx, bEpNum) (PCD_GET_EP_RX_STATUS((USBx), (bEpNum)) == USB_EP_RX_STALL)
  567. /**
  568. * @brief set & clear EP_KIND bit.
  569. * @param USBx USB peripheral instance register address.
  570. * @param bEpNum Endpoint Number.
  571. * @retval None
  572. */
  573. #define PCD_SET_EP_KIND(USBx, bEpNum) \
  574. do { \
  575. uint16_t _wRegVal; \
  576. \
  577. _wRegVal = PCD_GET_ENDPOINT((USBx), (bEpNum)) & USB_EPREG_MASK; \
  578. \
  579. PCD_SET_ENDPOINT((USBx), (bEpNum), (_wRegVal | USB_EP_CTR_RX | USB_EP_CTR_TX | USB_EP_KIND)); \
  580. } while(0) /* PCD_SET_EP_KIND */
  581. #define PCD_CLEAR_EP_KIND(USBx, bEpNum) \
  582. do { \
  583. uint16_t _wRegVal; \
  584. \
  585. _wRegVal = PCD_GET_ENDPOINT((USBx), (bEpNum)) & USB_EPKIND_MASK; \
  586. \
  587. PCD_SET_ENDPOINT((USBx), (bEpNum), (_wRegVal | USB_EP_CTR_RX | USB_EP_CTR_TX)); \
  588. } while(0) /* PCD_CLEAR_EP_KIND */
  589. /**
  590. * @brief Sets/clears directly STATUS_OUT bit in the endpoint register.
  591. * @param USBx USB peripheral instance register address.
  592. * @param bEpNum Endpoint Number.
  593. * @retval None
  594. */
  595. #define PCD_SET_OUT_STATUS(USBx, bEpNum) PCD_SET_EP_KIND((USBx), (bEpNum))
  596. #define PCD_CLEAR_OUT_STATUS(USBx, bEpNum) PCD_CLEAR_EP_KIND((USBx), (bEpNum))
  597. /**
  598. * @brief Sets/clears directly EP_KIND bit in the endpoint register.
  599. * @param USBx USB peripheral instance register address.
  600. * @param bEpNum Endpoint Number.
  601. * @retval None
  602. */
  603. #define PCD_SET_EP_DBUF(USBx, bEpNum) PCD_SET_EP_KIND((USBx), (bEpNum))
  604. #define PCD_CLEAR_EP_DBUF(USBx, bEpNum) PCD_CLEAR_EP_KIND((USBx), (bEpNum))
  605. /**
  606. * @brief Clears bit CTR_RX / CTR_TX in the endpoint register.
  607. * @param USBx USB peripheral instance register address.
  608. * @param bEpNum Endpoint Number.
  609. * @retval None
  610. */
  611. #define PCD_CLEAR_RX_EP_CTR(USBx, bEpNum) \
  612. do { \
  613. uint16_t _wRegVal; \
  614. \
  615. _wRegVal = PCD_GET_ENDPOINT((USBx), (bEpNum)) & (0x7FFFU & USB_EPREG_MASK); \
  616. \
  617. PCD_SET_ENDPOINT((USBx), (bEpNum), (_wRegVal | USB_EP_CTR_TX)); \
  618. } while(0) /* PCD_CLEAR_RX_EP_CTR */
  619. #define PCD_CLEAR_TX_EP_CTR(USBx, bEpNum) \
  620. do { \
  621. uint16_t _wRegVal; \
  622. \
  623. _wRegVal = PCD_GET_ENDPOINT((USBx), (bEpNum)) & (0xFF7FU & USB_EPREG_MASK); \
  624. \
  625. PCD_SET_ENDPOINT((USBx), (bEpNum), (_wRegVal | USB_EP_CTR_RX)); \
  626. } while(0) /* PCD_CLEAR_TX_EP_CTR */
  627. /**
  628. * @brief Toggles DTOG_RX / DTOG_TX bit in the endpoint register.
  629. * @param USBx USB peripheral instance register address.
  630. * @param bEpNum Endpoint Number.
  631. * @retval None
  632. */
  633. #define PCD_RX_DTOG(USBx, bEpNum) \
  634. do { \
  635. uint16_t _wEPVal; \
  636. \
  637. _wEPVal = PCD_GET_ENDPOINT((USBx), (bEpNum)) & USB_EPREG_MASK; \
  638. \
  639. PCD_SET_ENDPOINT((USBx), (bEpNum), (_wEPVal | USB_EP_CTR_RX | USB_EP_CTR_TX | USB_EP_DTOG_RX)); \
  640. } while(0) /* PCD_RX_DTOG */
  641. #define PCD_TX_DTOG(USBx, bEpNum) \
  642. do { \
  643. uint16_t _wEPVal; \
  644. \
  645. _wEPVal = PCD_GET_ENDPOINT((USBx), (bEpNum)) & USB_EPREG_MASK; \
  646. \
  647. PCD_SET_ENDPOINT((USBx), (bEpNum), (_wEPVal | USB_EP_CTR_RX | USB_EP_CTR_TX | USB_EP_DTOG_TX)); \
  648. } while(0) /* PCD_TX_DTOG */
  649. /**
  650. * @brief Clears DTOG_RX / DTOG_TX bit in the endpoint register.
  651. * @param USBx USB peripheral instance register address.
  652. * @param bEpNum Endpoint Number.
  653. * @retval None
  654. */
  655. #define PCD_CLEAR_RX_DTOG(USBx, bEpNum) \
  656. do { \
  657. uint16_t _wRegVal; \
  658. \
  659. _wRegVal = PCD_GET_ENDPOINT((USBx), (bEpNum)); \
  660. \
  661. if ((_wRegVal & USB_EP_DTOG_RX) != 0U)\
  662. { \
  663. PCD_RX_DTOG((USBx), (bEpNum)); \
  664. } \
  665. } while(0) /* PCD_CLEAR_RX_DTOG */
  666. #define PCD_CLEAR_TX_DTOG(USBx, bEpNum) \
  667. do { \
  668. uint16_t _wRegVal; \
  669. \
  670. _wRegVal = PCD_GET_ENDPOINT((USBx), (bEpNum)); \
  671. \
  672. if ((_wRegVal & USB_EP_DTOG_TX) != 0U)\
  673. { \
  674. PCD_TX_DTOG((USBx), (bEpNum)); \
  675. } \
  676. } while(0) /* PCD_CLEAR_TX_DTOG */
  677. /**
  678. * @brief Sets address in an endpoint register.
  679. * @param USBx USB peripheral instance register address.
  680. * @param bEpNum Endpoint Number.
  681. * @param bAddr Address.
  682. * @retval None
  683. */
  684. #define PCD_SET_EP_ADDRESS(USBx, bEpNum, bAddr) \
  685. do { \
  686. uint16_t _wRegVal; \
  687. \
  688. _wRegVal = (PCD_GET_ENDPOINT((USBx), (bEpNum)) & USB_EPREG_MASK) | (bAddr); \
  689. \
  690. PCD_SET_ENDPOINT((USBx), (bEpNum), (_wRegVal | USB_EP_CTR_RX | USB_EP_CTR_TX)); \
  691. } while(0) /* PCD_SET_EP_ADDRESS */
  692. /**
  693. * @brief Gets address in an endpoint register.
  694. * @param USBx USB peripheral instance register address.
  695. * @param bEpNum Endpoint Number.
  696. * @retval None
  697. */
  698. #define PCD_GET_EP_ADDRESS(USBx, bEpNum) ((uint8_t)(PCD_GET_ENDPOINT((USBx), (bEpNum)) & USB_EPADDR_FIELD))
  699. #define PCD_EP_TX_CNT(USBx, bEpNum) ((uint16_t *)((((uint32_t)(USBx)->BTABLE\
  700. + ((uint32_t)(bEpNum) * 8U) + 2U) * PMA_ACCESS) + ((uint32_t)(USBx) + 0x400U)))
  701. #define PCD_EP_RX_CNT(USBx, bEpNum) ((uint16_t *)((((uint32_t)(USBx)->BTABLE\
  702. + ((uint32_t)(bEpNum) * 8U) + 6U) * PMA_ACCESS) + ((uint32_t)(USBx) + 0x400U)))
  703. /**
  704. * @brief sets address of the tx/rx buffer.
  705. * @param USBx USB peripheral instance register address.
  706. * @param bEpNum Endpoint Number.
  707. * @param wAddr address to be set (must be word aligned).
  708. * @retval None
  709. */
  710. #define PCD_SET_EP_TX_ADDRESS(USBx, bEpNum, wAddr) \
  711. do { \
  712. __IO uint16_t *_wRegVal; \
  713. uint32_t _wRegBase = (uint32_t)USBx; \
  714. \
  715. _wRegBase += (uint32_t)(USBx)->BTABLE; \
  716. _wRegVal = (__IO uint16_t *)(_wRegBase + 0x400U + (((uint32_t)(bEpNum) * 8U) * PMA_ACCESS)); \
  717. *_wRegVal = ((wAddr) >> 1) << 1; \
  718. } while(0) /* PCD_SET_EP_TX_ADDRESS */
  719. #define PCD_SET_EP_RX_ADDRESS(USBx, bEpNum, wAddr) \
  720. do { \
  721. __IO uint16_t *_wRegVal; \
  722. uint32_t _wRegBase = (uint32_t)USBx; \
  723. \
  724. _wRegBase += (uint32_t)(USBx)->BTABLE; \
  725. _wRegVal = (__IO uint16_t *)(_wRegBase + 0x400U + ((((uint32_t)(bEpNum) * 8U) + 4U) * PMA_ACCESS)); \
  726. *_wRegVal = ((wAddr) >> 1) << 1; \
  727. } while(0) /* PCD_SET_EP_RX_ADDRESS */
  728. /**
  729. * @brief Gets address of the tx/rx buffer.
  730. * @param USBx USB peripheral instance register address.
  731. * @param bEpNum Endpoint Number.
  732. * @retval address of the buffer.
  733. */
  734. #define PCD_GET_EP_TX_ADDRESS(USBx, bEpNum) ((uint16_t)*PCD_EP_TX_ADDRESS((USBx), (bEpNum)))
  735. #define PCD_GET_EP_RX_ADDRESS(USBx, bEpNum) ((uint16_t)*PCD_EP_RX_ADDRESS((USBx), (bEpNum)))
  736. /**
  737. * @brief Sets counter of rx buffer with no. of blocks.
  738. * @param pdwReg Register pointer
  739. * @param wCount Counter.
  740. * @param wNBlocks no. of Blocks.
  741. * @retval None
  742. */
  743. #define PCD_CALC_BLK32(pdwReg, wCount, wNBlocks) \
  744. do { \
  745. (wNBlocks) = (wCount) >> 5; \
  746. if (((wCount) & 0x1fU) == 0U) \
  747. { \
  748. (wNBlocks)--; \
  749. } \
  750. *(pdwReg) = (uint16_t)(((wNBlocks) << 10) | USB_CNTRX_BLSIZE); \
  751. } while(0) /* PCD_CALC_BLK32 */
  752. #define PCD_CALC_BLK2(pdwReg, wCount, wNBlocks) \
  753. do { \
  754. (wNBlocks) = (wCount) >> 1; \
  755. if (((wCount) & 0x1U) != 0U) \
  756. { \
  757. (wNBlocks)++; \
  758. } \
  759. *(pdwReg) = (uint16_t)((wNBlocks) << 10); \
  760. } while(0) /* PCD_CALC_BLK2 */
  761. #define PCD_SET_EP_CNT_RX_REG(pdwReg, wCount) \
  762. do { \
  763. uint32_t wNBlocks; \
  764. if ((wCount) == 0U) \
  765. { \
  766. *(pdwReg) &= (uint16_t)~USB_CNTRX_NBLK_MSK; \
  767. *(pdwReg) |= USB_CNTRX_BLSIZE; \
  768. } \
  769. else if((wCount) <= 62U) \
  770. { \
  771. PCD_CALC_BLK2((pdwReg), (wCount), wNBlocks); \
  772. } \
  773. else \
  774. { \
  775. PCD_CALC_BLK32((pdwReg), (wCount), wNBlocks); \
  776. } \
  777. } while(0) /* PCD_SET_EP_CNT_RX_REG */
  778. #define PCD_SET_EP_RX_DBUF0_CNT(USBx, bEpNum, wCount) \
  779. do { \
  780. uint32_t _wRegBase = (uint32_t)(USBx); \
  781. __IO uint16_t *pdwReg; \
  782. \
  783. _wRegBase += (uint32_t)(USBx)->BTABLE; \
  784. pdwReg = (__IO uint16_t *)(_wRegBase + 0x400U + ((((uint32_t)(bEpNum) * 8U) + 2U) * PMA_ACCESS)); \
  785. PCD_SET_EP_CNT_RX_REG(pdwReg, (wCount)); \
  786. } while(0)
  787. /**
  788. * @brief sets counter for the tx/rx buffer.
  789. * @param USBx USB peripheral instance register address.
  790. * @param bEpNum Endpoint Number.
  791. * @param wCount Counter value.
  792. * @retval None
  793. */
  794. #define PCD_SET_EP_TX_CNT(USBx, bEpNum, wCount) \
  795. do { \
  796. uint32_t _wRegBase = (uint32_t)(USBx); \
  797. __IO uint16_t *_wRegVal; \
  798. \
  799. _wRegBase += (uint32_t)(USBx)->BTABLE; \
  800. _wRegVal = (__IO uint16_t *)(_wRegBase + 0x400U + ((((uint32_t)(bEpNum) * 8U) + 2U) * PMA_ACCESS)); \
  801. *_wRegVal = (uint16_t)(wCount); \
  802. } while(0)
  803. #define PCD_SET_EP_RX_CNT(USBx, bEpNum, wCount) \
  804. do { \
  805. uint32_t _wRegBase = (uint32_t)(USBx); \
  806. __IO uint16_t *_wRegVal; \
  807. \
  808. _wRegBase += (uint32_t)(USBx)->BTABLE; \
  809. _wRegVal = (__IO uint16_t *)(_wRegBase + 0x400U + ((((uint32_t)(bEpNum) * 8U) + 6U) * PMA_ACCESS)); \
  810. PCD_SET_EP_CNT_RX_REG(_wRegVal, (wCount)); \
  811. } while(0)
  812. /**
  813. * @brief gets counter of the tx buffer.
  814. * @param USBx USB peripheral instance register address.
  815. * @param bEpNum Endpoint Number.
  816. * @retval Counter value
  817. */
  818. #define PCD_GET_EP_TX_CNT(USBx, bEpNum) ((uint32_t)(*PCD_EP_TX_CNT((USBx), (bEpNum))) & 0x3ffU)
  819. #define PCD_GET_EP_RX_CNT(USBx, bEpNum) ((uint32_t)(*PCD_EP_RX_CNT((USBx), (bEpNum))) & 0x3ffU)
  820. /**
  821. * @brief Sets buffer 0/1 address in a double buffer endpoint.
  822. * @param USBx USB peripheral instance register address.
  823. * @param bEpNum Endpoint Number.
  824. * @param wBuf0Addr buffer 0 address.
  825. * @retval Counter value
  826. */
  827. #define PCD_SET_EP_DBUF0_ADDR(USBx, bEpNum, wBuf0Addr) \
  828. do { \
  829. PCD_SET_EP_TX_ADDRESS((USBx), (bEpNum), (wBuf0Addr)); \
  830. } while(0) /* PCD_SET_EP_DBUF0_ADDR */
  831. #define PCD_SET_EP_DBUF1_ADDR(USBx, bEpNum, wBuf1Addr) \
  832. do { \
  833. PCD_SET_EP_RX_ADDRESS((USBx), (bEpNum), (wBuf1Addr)); \
  834. } while(0) /* PCD_SET_EP_DBUF1_ADDR */
  835. /**
  836. * @brief Sets addresses in a double buffer endpoint.
  837. * @param USBx USB peripheral instance register address.
  838. * @param bEpNum Endpoint Number.
  839. * @param wBuf0Addr: buffer 0 address.
  840. * @param wBuf1Addr = buffer 1 address.
  841. * @retval None
  842. */
  843. #define PCD_SET_EP_DBUF_ADDR(USBx, bEpNum, wBuf0Addr, wBuf1Addr) \
  844. do { \
  845. PCD_SET_EP_DBUF0_ADDR((USBx), (bEpNum), (wBuf0Addr)); \
  846. PCD_SET_EP_DBUF1_ADDR((USBx), (bEpNum), (wBuf1Addr)); \
  847. } while(0) /* PCD_SET_EP_DBUF_ADDR */
  848. /**
  849. * @brief Gets buffer 0/1 address of a double buffer endpoint.
  850. * @param USBx USB peripheral instance register address.
  851. * @param bEpNum Endpoint Number.
  852. * @retval None
  853. */
  854. #define PCD_GET_EP_DBUF0_ADDR(USBx, bEpNum) (PCD_GET_EP_TX_ADDRESS((USBx), (bEpNum)))
  855. #define PCD_GET_EP_DBUF1_ADDR(USBx, bEpNum) (PCD_GET_EP_RX_ADDRESS((USBx), (bEpNum)))
  856. /**
  857. * @brief Gets buffer 0/1 address of a double buffer endpoint.
  858. * @param USBx USB peripheral instance register address.
  859. * @param bEpNum Endpoint Number.
  860. * @param bDir endpoint dir EP_DBUF_OUT = OUT
  861. * EP_DBUF_IN = IN
  862. * @param wCount: Counter value
  863. * @retval None
  864. */
  865. #define PCD_SET_EP_DBUF0_CNT(USBx, bEpNum, bDir, wCount) \
  866. do { \
  867. if ((bDir) == 0U) \
  868. /* OUT endpoint */ \
  869. { \
  870. PCD_SET_EP_RX_DBUF0_CNT((USBx), (bEpNum), (wCount)); \
  871. } \
  872. else \
  873. { \
  874. if ((bDir) == 1U) \
  875. { \
  876. /* IN endpoint */ \
  877. PCD_SET_EP_TX_CNT((USBx), (bEpNum), (wCount)); \
  878. } \
  879. } \
  880. } while(0) /* SetEPDblBuf0Count*/
  881. #define PCD_SET_EP_DBUF1_CNT(USBx, bEpNum, bDir, wCount) \
  882. do { \
  883. uint32_t _wBase = (uint32_t)(USBx); \
  884. __IO uint16_t *_wEPRegVal; \
  885. \
  886. if ((bDir) == 0U) \
  887. { \
  888. /* OUT endpoint */ \
  889. PCD_SET_EP_RX_CNT((USBx), (bEpNum), (wCount)); \
  890. } \
  891. else \
  892. { \
  893. if ((bDir) == 1U) \
  894. { \
  895. /* IN endpoint */ \
  896. _wBase += (uint32_t)(USBx)->BTABLE; \
  897. _wEPRegVal = (__IO uint16_t *)(_wBase + 0x400U + ((((uint32_t)(bEpNum) * 8U) + 6U) * PMA_ACCESS)); \
  898. *_wEPRegVal = (uint16_t)(wCount); \
  899. } \
  900. } \
  901. } while(0) /* SetEPDblBuf1Count */
  902. #define PCD_SET_EP_DBUF_CNT(USBx, bEpNum, bDir, wCount) \
  903. do { \
  904. PCD_SET_EP_DBUF0_CNT((USBx), (bEpNum), (bDir), (wCount)); \
  905. PCD_SET_EP_DBUF1_CNT((USBx), (bEpNum), (bDir), (wCount)); \
  906. } while(0) /* PCD_SET_EP_DBUF_CNT */
  907. /**
  908. * @brief Gets buffer 0/1 rx/tx counter for double buffering.
  909. * @param USBx USB peripheral instance register address.
  910. * @param bEpNum Endpoint Number.
  911. * @retval None
  912. */
  913. #define PCD_GET_EP_DBUF0_CNT(USBx, bEpNum) (PCD_GET_EP_TX_CNT((USBx), (bEpNum)))
  914. #define PCD_GET_EP_DBUF1_CNT(USBx, bEpNum) (PCD_GET_EP_RX_CNT((USBx), (bEpNum)))
  915. #endif /* defined (USB) */
  916. /**
  917. * @}
  918. */
  919. /**
  920. * @}
  921. */
  922. /**
  923. * @}
  924. */
  925. #endif /* defined (USB) || defined (USB_OTG_FS) */
  926. #ifdef __cplusplus
  927. }
  928. #endif
  929. #endif /* STM32F1xx_HAL_PCD_H */
  930. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/