2 ******************************************************************************
3 * @file stm32f3xx_hal_pcd_ex.c
4 * @author MCD Application Team
7 * @brief Extended PCD HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the USB Peripheral Controller:
10 * + Configuration of the PMA for EP
12 ******************************************************************************
15 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
17 * Redistribution and use in source and binary forms, with or without modification,
18 * are permitted provided that the following conditions are met:
19 * 1. Redistributions of source code must retain the above copyright notice,
20 * this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
23 * and/or other materials provided with the distribution.
24 * 3. Neither the name of STMicroelectronics nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ******************************************************************************
42 /* Includes ------------------------------------------------------------------*/
43 #include "stm32f3xx_hal.h"
45 /** @addtogroup STM32F3xx_HAL_Driver
49 /** @defgroup PCDEx PCD Extended HAL module driver
50 * @brief PCDEx PCDEx Extended HAL module driver
54 #ifdef HAL_PCD_MODULE_ENABLED
56 #if defined(STM32F302xE) || defined(STM32F303xE) || \
57 defined(STM32F302xC) || defined(STM32F303xC) || \
58 defined(STM32F302x8) || \
61 /* Private typedef -----------------------------------------------------------*/
62 /* Private define ------------------------------------------------------------*/
63 /* Private macro -------------------------------------------------------------*/
64 /* Private variables ---------------------------------------------------------*/
65 /* Private function prototypes -----------------------------------------------*/
66 /* Exported functions ---------------------------------------------------------*/
68 /** @defgroup PCDEx_Exported_Functions PCD Extended Exported Functions
72 /** @defgroup PCDEx_Exported_Functions_Group1 Extended Initialization and de-initialization functions
73 * @brief Initialization and Configuration functions
76 ===============================================================================
77 ##### Peripheral extended features methods #####
78 ===============================================================================
84 * @brief Configure PMA for EP
85 * @param hpcd: PCD handle
86 * @param ep_addr: endpoint address
87 * @param ep_kind: endpoint Kind
88 * @arg USB_SNG_BUF: Single Buffer used
89 * @arg USB_DBL_BUF: Double Buffer used
90 * @param pmaadress: EP address in The PMA: In case of single buffer endpoint
91 * this parameter is 16-bit value providing the address
92 * in PMA allocated to endpoint.
93 * In case of double buffer endpoint this parameter
94 * is a 32-bit value providing the endpoint buffer 0 address
95 * in the LSB part of 32-bit value and endpoint buffer 1 address
96 * in the MSB part of 32-bit value.
100 HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd,
108 /* initialize ep structure*/
109 if ((0x80 & ep_addr) == 0x80)
111 ep = &hpcd->IN_ep[ep_addr & 0x7F];
115 ep = &hpcd->OUT_ep[ep_addr];
118 /* Here we check if the endpoint is single or double Buffer*/
119 if (ep_kind == PCD_SNG_BUF)
122 ep->doublebuffer = 0;
124 ep->pmaadress = (uint16_t)pmaadress;
128 /*Double Buffer Endpoint*/
129 ep->doublebuffer = 1;
130 /*Configure the PMA*/
131 ep->pmaaddr0 = pmaadress & 0xFFFF;
132 ep->pmaaddr1 = (pmaadress & 0xFFFF0000) >> 16;
145 /** @defgroup PCDEx_Private_Functions PCD Extended Private Functions
148 #if defined(STM32F303xC) || \
149 defined(STM32F303x8) || defined(STM32F334x8) || \
150 defined(STM32F301x8) || \
151 defined(STM32F373xC) || defined(STM32F378xx)
155 * @brief Copy a buffer from user memory area to packet memory area (PMA)
156 * @param USBx: USB peripheral instance register address.
157 * @param pbUsrBuf: pointer to user memory area.
158 * @param wPMABufAddr: address into PMA.
159 * @param wNBytes: no. of bytes to be copied.
162 void PCD_WritePMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
164 uint32_t n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */
165 uint32_t i, temp1, temp2;
167 pdwVal = (uint16_t *)(wPMABufAddr * 2 + (uint32_t)USBx + 0x400);
168 for (i = n; i != 0; i--)
170 temp1 = (uint16_t) * pbUsrBuf;
172 temp2 = temp1 | (uint16_t) * pbUsrBuf << 8;
180 * @brief Copy a buffer from user memory area to packet memory area (PMA)
181 * @param USBx: USB peripheral instance register address.
182 * @param pbUsrBuf: pointer to user memory area.
183 * @param wPMABufAddr: address into PMA.
184 * @param wNBytes: no. of bytes to be copied.
187 void PCD_ReadPMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
189 uint32_t n = (wNBytes + 1) >> 1;/* /2*/
192 pdwVal = (uint32_t *)(wPMABufAddr * 2 + (uint32_t)USBx + 0x400);
193 for (i = n; i != 0; i--)
195 *(uint16_t*)pbUsrBuf++ = *pdwVal++;
199 #endif /* STM32F303xC || */
200 /* STM32F303x8 || STM32F334x8 || */
202 /* STM32F373xC || STM32F378xx */
204 #if defined(STM32F302xE) || defined(STM32F303xE) || \
205 defined(STM32F302xC) || \
208 * @brief Copy a buffer from user memory area to packet memory area (PMA)
209 * @param USBx: USB peripheral instance register address.
210 * @param pbUsrBuf: pointer to user memory area.
211 * @param wPMABufAddr: address into PMA.
212 * @param wNBytes: no. of bytes to be copied.
215 void PCD_WritePMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
217 uint32_t n = (wNBytes + 1) >> 1;
219 uint16_t temp1, temp2;
221 pdwVal = (uint16_t *)(wPMABufAddr + (uint32_t)USBx + 0x400);
223 for (i = n; i != 0; i--)
225 temp1 = (uint16_t) * pbUsrBuf;
227 temp2 = temp1 | (uint16_t) * pbUsrBuf << 8;
234 * @brief Copy a buffer from user memory area to packet memory area (PMA)
235 * @param USBx: USB peripheral instance register address.
236 * @param pbUsrBuf: pointer to user memory area.
237 * @param wPMABufAddr: address into PMA.
238 * @param wNBytes: no. of bytes to be copied.
241 void PCD_ReadPMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
243 uint32_t n = (wNBytes + 1) >> 1;
246 pdwVal = (uint16_t *)(wPMABufAddr + (uint32_t)USBx + 0x400);
247 for (i = n; i != 0; i--)
249 *(uint16_t*)pbUsrBuf++ = *pdwVal++;
253 #endif /* STM32F302xE || STM32F303xE || */
261 /** @addtogroup PCDEx_Exported_Functions PCD Extended Exported Functions
265 /** @addtogroup PCDEx_Exported_Functions_Group1 Extended Initialization and de-initialization functions
269 * @brief Software Device Connection
270 * @param hpcd: PCD handle
271 * @param state: Device state
274 __weak void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
276 /* NOTE : This function Should not be modified, when the callback is needed,
277 the HAL_PCDEx_SetConnectionState could be implenetd in the user file
287 #endif /* STM32F302xE || STM32F303xE || */
288 /* STM32F302xC || STM32F303xC || */
292 #endif /* HAL_PCD_MODULE_ENABLED */
301 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/