2 ******************************************************************************
4 * @author MCD Application Team
6 * @date 09-November-2015
7 * @brief OTG Core Layer
8 ******************************************************************************
11 * <h2><center>© COPYRIGHT 2015 STMicroelectronics</center></h2>
13 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
14 * You may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at:
17 * http://www.st.com/software_license_agreement_liberty_v2
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 ******************************************************************************
28 /* Includes ------------------------------------------------------------------*/
29 #include "usb_defines.h"
34 /** @addtogroup USB_OTG_DRIVER
39 * @brief This file is the interface between EFSL ans Host mass-storage class
44 /** @defgroup USB_OTG_Private_Defines
52 /** @defgroup USB_OTG_Private_TypesDefinitions
61 /** @defgroup USB_OTG_Private_Macros
69 /** @defgroup USB_OTG_Private_Variables
77 /** @defgroup USB_OTG_Private_FunctionPrototypes
81 uint32_t USB_OTG_HandleOTG_ISR(USB_OTG_CORE_HANDLE
*pdev
);
83 static uint32_t USB_OTG_HandleConnectorIDStatusChange_ISR(USB_OTG_CORE_HANDLE
*pdev
);
84 static uint32_t USB_OTG_HandleSessionRequest_ISR(USB_OTG_CORE_HANDLE
*pdev
);
85 static uint32_t USB_OTG_Read_itr(USB_OTG_CORE_HANDLE
*pdev
);
92 /** @defgroup USB_OTG_Private_Functions
97 /* OTG Interrupt Handler */
101 * @brief STM32_USBO_OTG_ISR_Handler
106 uint32_t STM32_USBO_OTG_ISR_Handler(USB_OTG_CORE_HANDLE
*pdev
)
109 USB_OTG_GINTSTS_TypeDef gintsts
;
112 gintsts
.d32
= USB_OTG_Read_itr(pdev
);
113 if (gintsts
.d32
== 0)
117 if (gintsts
.b
.otgintr
)
119 retval
|= USB_OTG_HandleOTG_ISR(pdev
);
121 if (gintsts
.b
.conidstschng
)
123 retval
|= USB_OTG_HandleConnectorIDStatusChange_ISR(pdev
);
125 if (gintsts
.b
.sessreqintr
)
127 retval
|= USB_OTG_HandleSessionRequest_ISR(pdev
);
134 * @brief USB_OTG_Read_itr
135 * returns the Core Interrupt register
139 static uint32_t USB_OTG_Read_itr(USB_OTG_CORE_HANDLE
*pdev
)
141 USB_OTG_GINTSTS_TypeDef gintsts
;
142 USB_OTG_GINTMSK_TypeDef gintmsk
;
143 USB_OTG_GINTMSK_TypeDef gintmsk_common
;
148 gintmsk_common
.d32
= 0;
151 gintmsk_common
.b
.sessreqintr
= 1;
152 gintmsk_common
.b
.conidstschng
= 1;
153 gintmsk_common
.b
.otgintr
= 1;
155 gintsts
.d32
= USB_OTG_READ_REG32(&pdev
->regs
.GREGS
->GINTSTS
);
156 gintmsk
.d32
= USB_OTG_READ_REG32(&pdev
->regs
.GREGS
->GINTMSK
);
157 return ((gintsts
.d32
& gintmsk
.d32
) & gintmsk_common
.d32
);
162 * @brief USB_OTG_HandleOTG_ISR
163 * handles the OTG Interrupts
167 static uint32_t USB_OTG_HandleOTG_ISR(USB_OTG_CORE_HANDLE
*pdev
)
169 USB_OTG_GOTGINT_TypeDef gotgint
;
170 USB_OTG_GOTGCTL_TypeDef gotgctl
;
176 gotgint
.d32
= USB_OTG_READ_REG32(&pdev
->regs
.GREGS
->GOTGINT
);
177 gotgctl
.d32
= USB_OTG_READ_REG32(&pdev
->regs
.GREGS
->GOTGCTL
);
179 if (gotgint
.b
.sesenddet
)
181 gotgctl
.d32
= USB_OTG_READ_REG32(&pdev
->regs
.GREGS
->GOTGCTL
);
184 if (USB_OTG_IsDeviceMode(pdev
))
188 else if (USB_OTG_IsHostMode(pdev
))
194 /* ----> SRP SUCCESS or FAILURE INTERRUPT <---- */
195 if (gotgint
.b
.sesreqsucstschng
)
197 gotgctl
.d32
= USB_OTG_READ_REG32(&pdev
->regs
.GREGS
->GOTGCTL
);
198 if (gotgctl
.b
.sesreqscs
) /* Session request success */
200 if (USB_OTG_IsDeviceMode(pdev
))
204 /* Clear Session Request */
206 gotgctl
.b
.sesreq
= 1;
207 USB_OTG_MODIFY_REG32(&pdev
->regs
.GREGS
->GOTGCTL
, gotgctl
.d32
, 0);
209 else /* Session request failure */
211 if (USB_OTG_IsDeviceMode(pdev
))
217 /* ----> HNP SUCCESS or FAILURE INTERRUPT <---- */
218 if (gotgint
.b
.hstnegsucstschng
)
220 gotgctl
.d32
= USB_OTG_READ_REG32(&pdev
->regs
.GREGS
->GOTGCTL
);
222 if (gotgctl
.b
.hstnegscs
) /* Host negotiation success */
224 if (USB_OTG_IsHostMode(pdev
)) /* The core AUTOMATICALLY sets the Host mode */
229 else /* Host negotiation failure */
233 gotgint
.b
.hstnegsucstschng
= 1; /* Ack "Host Negotiation Success Status Change" interrupt. */
235 /* ----> HOST NEGOTIATION DETECTED INTERRUPT <---- */
236 if (gotgint
.b
.hstnegdet
)
238 if (USB_OTG_IsDeviceMode(pdev
)) /* The core AUTOMATICALLY sets the Host mode */
247 if (gotgint
.b
.adevtoutchng
)
249 if (gotgint
.b
.debdone
)
251 USB_OTG_ResetPort(pdev
);
254 USB_OTG_WRITE_REG32(&pdev
->regs
.GREGS
->GOTGINT
, gotgint
.d32
);
260 * @brief USB_OTG_HandleConnectorIDStatusChange_ISR
261 * handles the Connector ID Status Change Interrupt
265 static uint32_t USB_OTG_HandleConnectorIDStatusChange_ISR(USB_OTG_CORE_HANDLE
*pdev
)
267 USB_OTG_GINTMSK_TypeDef gintmsk
;
268 USB_OTG_GOTGCTL_TypeDef gotgctl
;
269 USB_OTG_GINTSTS_TypeDef gintsts
;
274 gintmsk
.b
.sofintr
= 1;
276 USB_OTG_MODIFY_REG32(&pdev
->regs
.GREGS
->GINTMSK
, gintmsk
.d32
, 0);
277 gotgctl
.d32
= USB_OTG_READ_REG32(&pdev
->regs
.GREGS
->GOTGCTL
);
279 /* B-Device connector (Device Mode) */
280 if (gotgctl
.b
.conidsts
)
282 USB_OTG_DisableGlobalInt(pdev
);
283 USB_OTG_CoreInitDev(pdev
);
284 USB_OTG_EnableGlobalInt(pdev
);
285 pdev
->otg
.OTG_State
= B_PERIPHERAL
;
289 USB_OTG_DisableGlobalInt(pdev
);
290 USB_OTG_CoreInitHost(pdev
);
291 USB_OTG_EnableGlobalInt(pdev
);
292 pdev
->otg
.OTG_State
= A_HOST
;
294 /* Set flag and clear interrupt */
295 gintsts
.b
.conidstschng
= 1;
296 USB_OTG_WRITE_REG32 (&pdev
->regs
.GREGS
->GINTSTS
, gintsts
.d32
);
302 * @brief USB_OTG_HandleSessionRequest_ISR
303 * Initiating the Session Request Protocol
307 static uint32_t USB_OTG_HandleSessionRequest_ISR(USB_OTG_CORE_HANDLE
*pdev
)
309 USB_OTG_GINTSTS_TypeDef gintsts
;
310 USB_OTG_GOTGCTL_TypeDef gotgctl
;
316 gotgctl
.d32
= USB_OTG_READ_REG32( &pdev
->regs
.GREGS
->GOTGCTL
);
317 if (USB_OTG_IsDeviceMode(pdev
) && (gotgctl
.b
.bsesvld
))
321 else if (gotgctl
.b
.asesvld
)
324 /* Clear interrupt */
326 gintsts
.b
.sessreqintr
= 1;
327 USB_OTG_WRITE_REG32 (&pdev
->regs
.GREGS
->GINTSTS
, gintsts
.d32
);
333 * @brief USB_OTG_InitiateSRP
334 * Initiate an srp session
338 void USB_OTG_InitiateSRP(USB_OTG_CORE_HANDLE
*pdev
)
340 USB_OTG_GOTGCTL_TypeDef otgctl
;
344 otgctl
.d32
= USB_OTG_READ_REG32( &pdev
->regs
.GREGS
->GOTGCTL
);
347 return; /* SRP in progress */
350 USB_OTG_WRITE_REG32(&pdev
->regs
.GREGS
->GOTGCTL
, otgctl
.d32
);
355 * @brief USB_OTG_InitiateHNP
360 void USB_OTG_InitiateHNP(USB_OTG_CORE_HANDLE
*pdev
, uint8_t state
, uint8_t mode
)
362 USB_OTG_GOTGCTL_TypeDef otgctl
;
363 USB_OTG_HPRT0_TypeDef hprt0
;
368 otgctl
.d32
= USB_OTG_READ_REG32( &pdev
->regs
.GREGS
->GOTGCTL
);
374 otgctl
.b
.devhnpen
= 1; /* B-Dev has been enabled to perform HNP */
375 otgctl
.b
.hnpreq
= 1; /* Initiate an HNP req. to the connected USB host*/
376 USB_OTG_WRITE_REG32(&pdev
->regs
.GREGS
->GOTGCTL
, otgctl
.d32
);
383 otgctl
.b
.hstsethnpen
= 1; /* A-Dev has enabled B-device for HNP */
384 USB_OTG_WRITE_REG32(&pdev
->regs
.GREGS
->GOTGCTL
, otgctl
.d32
);
385 /* Suspend the bus so that B-dev will disconnect indicating the initial condition for HNP to DWC_Core */
386 hprt0
.d32
= USB_OTG_ReadHPRT0(pdev
);
387 hprt0
.b
.prtsusp
= 1; /* The core clear this bit when disconnect interrupt generated (GINTSTS.DisconnInt = '1') */
388 USB_OTG_WRITE_REG32(pdev
->regs
.HPRT0
, hprt0
.d32
);
395 * @brief USB_OTG_GetCurrentState
396 * Return current OTG State
400 uint32_t USB_OTG_GetCurrentState (USB_OTG_CORE_HANDLE
*pdev
)
402 return pdev
->otg
.OTG_State
;
418 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/