1 /**
2 ******************************************************************************
3 * @file stm32f1xx_hal_pcd_ex.c
4 * @author MCD Application Team
5 * @brief PCD Extended HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the USB Peripheral Controller:
8 * + Extended features functions
9 *
10 ******************************************************************************
11 * @attention
12 *
13 * <h2><center>© Copyright (c) 2016 STMicroelectronics.
14 * All rights reserved.</center></h2>
15 *
16 * This software component is licensed by ST under BSD 3-Clause license,
17 * the "License"; You may not use this file except in compliance with the
18 * License. You may obtain a copy of the License at:
19 * opensource.org/licenses/BSD-3-Clause
20 *
21 ******************************************************************************
22 */
23
24 /* Includes ------------------------------------------------------------------*/
25 #include "stm32f1xx_hal.h"
26
27 /** @addtogroup STM32F1xx_HAL_Driver
28 * @{
29 */
30
31 /** @defgroup PCDEx PCDEx
32 * @brief PCD Extended HAL module driver
33 * @{
34 */
35
36 #ifdef HAL_PCD_MODULE_ENABLED
37
38 #if defined (USB) || defined (USB_OTG_FS)
39 /* Private types -------------------------------------------------------------*/
40 /* Private variables ---------------------------------------------------------*/
41 /* Private constants ---------------------------------------------------------*/
42 /* Private macros ------------------------------------------------------------*/
43 /* Private functions ---------------------------------------------------------*/
44 /* Exported functions --------------------------------------------------------*/
45
46 /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions
47 * @{
48 */
49
50 /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions
51 * @brief PCDEx control functions
52 *
53 @verbatim
54 ===============================================================================
55 ##### Extended features functions #####
56 ===============================================================================
57 [..] This section provides functions allowing to:
58 (+) Update FIFO configuration
59
60 @endverbatim
61 * @{
62 */
63 #if defined (USB_OTG_FS)
64 /**
65 * @brief Set Tx FIFO
66 * @param hpcd PCD handle
67 * @param fifo The number of Tx fifo
68 * @param size Fifo size
69 * @retval HAL status
70 */
HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef * hpcd,uint8_t fifo,uint16_t size)71 HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size)
72 {
73 uint8_t i;
74 uint32_t Tx_Offset;
75
76 /* TXn min size = 16 words. (n : Transmit FIFO index)
77 When a TxFIFO is not used, the Configuration should be as follows:
78 case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes)
79 --> Txm can use the space allocated for Txn.
80 case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes)
81 --> Txn should be configured with the minimum space of 16 words
82 The FIFO is used optimally when used TxFIFOs are allocated in the top
83 of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones.
84 When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */
85
86 Tx_Offset = hpcd->Instance->GRXFSIZ;
87
88 if (fifo == 0U)
89 {
90 hpcd->Instance->DIEPTXF0_HNPTXFSIZ = ((uint32_t)size << 16) | Tx_Offset;
91 }
92 else
93 {
94 Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16;
95 for (i = 0U; i < (fifo - 1U); i++)
96 {
97 Tx_Offset += (hpcd->Instance->DIEPTXF[i] >> 16);
98 }
99
100 /* Multiply Tx_Size by 2 to get higher performance */
101 hpcd->Instance->DIEPTXF[fifo - 1U] = ((uint32_t)size << 16) | Tx_Offset;
102 }
103
104 return HAL_OK;
105 }
106
107 /**
108 * @brief Set Rx FIFO
109 * @param hpcd PCD handle
110 * @param size Size of Rx fifo
111 * @retval HAL status
112 */
HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef * hpcd,uint16_t size)113 HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size)
114 {
115 hpcd->Instance->GRXFSIZ = size;
116
117 return HAL_OK;
118 }
119 #endif /* defined (USB_OTG_FS) */
120 #if defined (USB)
121 /**
122 * @brief Configure PMA for EP
123 * @param hpcd Device instance
124 * @param ep_addr endpoint address
125 * @param ep_kind endpoint Kind
126 * USB_SNG_BUF: Single Buffer used
127 * USB_DBL_BUF: Double Buffer used
128 * @param pmaadress: EP address in The PMA: In case of single buffer endpoint
129 * this parameter is 16-bit value providing the address
130 * in PMA allocated to endpoint.
131 * In case of double buffer endpoint this parameter
132 * is a 32-bit value providing the endpoint buffer 0 address
133 * in the LSB part of 32-bit value and endpoint buffer 1 address
134 * in the MSB part of 32-bit value.
135 * @retval HAL status
136 */
137
HAL_PCDEx_PMAConfig(PCD_HandleTypeDef * hpcd,uint16_t ep_addr,uint16_t ep_kind,uint32_t pmaadress)138 HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd, uint16_t ep_addr,
139 uint16_t ep_kind, uint32_t pmaadress)
140 {
141 PCD_EPTypeDef *ep;
142
143 /* initialize ep structure*/
144 if ((0x80U & ep_addr) == 0x80U)
145 {
146 ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
147 }
148 else
149 {
150 ep = &hpcd->OUT_ep[ep_addr];
151 }
152
153 /* Here we check if the endpoint is single or double Buffer*/
154 if (ep_kind == PCD_SNG_BUF)
155 {
156 /* Single Buffer */
157 ep->doublebuffer = 0U;
158 /* Configure the PMA */
159 ep->pmaadress = (uint16_t)pmaadress;
160 }
161 else /* USB_DBL_BUF */
162 {
163 /* Double Buffer Endpoint */
164 ep->doublebuffer = 1U;
165 /* Configure the PMA */
166 ep->pmaaddr0 = (uint16_t)(pmaadress & 0xFFFFU);
167 ep->pmaaddr1 = (uint16_t)((pmaadress & 0xFFFF0000U) >> 16);
168 }
169
170 return HAL_OK;
171 }
172
173 /**
174 * @brief Software Device Connection,
175 * this function is not required by USB OTG FS peripheral, it is used
176 * only by USB Device FS peripheral.
177 * @param hpcd PCD handle
178 * @param state connection state (0 : disconnected / 1: connected)
179 * @retval None
180 */
HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef * hpcd,uint8_t state)181 __weak void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
182 {
183 /* Prevent unused argument(s) compilation warning */
184 UNUSED(hpcd);
185 UNUSED(state);
186 /* NOTE : This function Should not be modified, when the callback is needed,
187 the HAL_PCDEx_SetConnectionState could be implemented in the user file
188 */
189 }
190 #endif /* defined (USB) */
191
192 /**
193 * @brief Send LPM message to user layer callback.
194 * @param hpcd PCD handle
195 * @param msg LPM message
196 * @retval HAL status
197 */
HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef * hpcd,PCD_LPM_MsgTypeDef msg)198 __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
199 {
200 /* Prevent unused argument(s) compilation warning */
201 UNUSED(hpcd);
202 UNUSED(msg);
203
204 /* NOTE : This function should not be modified, when the callback is needed,
205 the HAL_PCDEx_LPM_Callback could be implemented in the user file
206 */
207 }
208
209 /**
210 * @brief Send BatteryCharging message to user layer callback.
211 * @param hpcd PCD handle
212 * @param msg LPM message
213 * @retval HAL status
214 */
HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef * hpcd,PCD_BCD_MsgTypeDef msg)215 __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
216 {
217 /* Prevent unused argument(s) compilation warning */
218 UNUSED(hpcd);
219 UNUSED(msg);
220
221 /* NOTE : This function should not be modified, when the callback is needed,
222 the HAL_PCDEx_BCD_Callback could be implemented in the user file
223 */
224 }
225
226 /**
227 * @}
228 */
229
230 /**
231 * @}
232 */
233 #endif /* defined (USB) || defined (USB_OTG_FS) */
234 #endif /* HAL_PCD_MODULE_ENABLED */
235
236 /**
237 * @}
238 */
239
240 /**
241 * @}
242 */
243
244 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
245