Make Iterm Lock configurable
[inav.git] / lib / main / STM32_USB_OTG_Driver / src / usb_otg.c
bloba3b9eabfdaa485a651c3263424779a0f36fcf476
1 /**
2 ******************************************************************************
3 * @file usb_otg.c
4 * @author MCD Application Team
5 * @version V2.2.0
6 * @date 09-November-2015
7 * @brief OTG Core Layer
8 ******************************************************************************
9 * @attention
11 * <h2><center>&copy; 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"
30 #include "usb_regs.h"
31 #include "usb_core.h"
32 #include "usb_otg.h"
34 /** @addtogroup USB_OTG_DRIVER
35 * @{
38 /** @defgroup USB_OTG
39 * @brief This file is the interface between EFSL ans Host mass-storage class
40 * @{
44 /** @defgroup USB_OTG_Private_Defines
45 * @{
46 */
47 /**
48 * @}
49 */
52 /** @defgroup USB_OTG_Private_TypesDefinitions
53 * @{
54 */
55 /**
56 * @}
57 */
61 /** @defgroup USB_OTG_Private_Macros
62 * @{
63 */
64 /**
65 * @}
66 */
69 /** @defgroup USB_OTG_Private_Variables
70 * @{
71 */
72 /**
73 * @}
74 */
77 /** @defgroup USB_OTG_Private_FunctionPrototypes
78 * @{
79 */
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);
87 /**
88 * @}
89 */
92 /** @defgroup USB_OTG_Private_Functions
93 * @{
94 */
97 /* OTG Interrupt Handler */
101 * @brief STM32_USBO_OTG_ISR_Handler
103 * @param None
104 * @retval : None
106 uint32_t STM32_USBO_OTG_ISR_Handler(USB_OTG_CORE_HANDLE *pdev)
108 uint32_t retval = 0;
109 USB_OTG_GINTSTS_TypeDef gintsts ;
110 gintsts.d32 = 0;
112 gintsts.d32 = USB_OTG_Read_itr(pdev);
113 if (gintsts.d32 == 0)
115 return 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);
129 return retval;
134 * @brief USB_OTG_Read_itr
135 * returns the Core Interrupt register
136 * @param None
137 * @retval : status
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;
146 gintsts.d32 = 0;
147 gintmsk.d32 = 0;
148 gintmsk_common.d32 = 0;
150 /* OTG interrupts */
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
164 * @param None
165 * @retval : status
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;
173 gotgint.d32 = 0;
174 gotgctl.d32 = 0;
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 */
205 gotgctl.d32 = 0;
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 */
242 else
247 if (gotgint.b.adevtoutchng)
249 if (gotgint.b.debdone)
251 USB_OTG_ResetPort(pdev);
253 /* Clear OTG INT */
254 USB_OTG_WRITE_REG32(&pdev->regs.GREGS->GOTGINT, gotgint.d32);
255 return 1;
260 * @brief USB_OTG_HandleConnectorIDStatusChange_ISR
261 * handles the Connector ID Status Change Interrupt
262 * @param None
263 * @retval : status
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;
271 gintsts.d32 = 0 ;
272 gintmsk.d32 = 0 ;
273 gotgctl.d32 = 0 ;
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;
287 else
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);
297 return 1;
302 * @brief USB_OTG_HandleSessionRequest_ISR
303 * Initiating the Session Request Protocol
304 * @param None
305 * @retval : status
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;
313 gotgctl.d32 = 0;
314 gintsts.d32 = 0;
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 */
325 gintsts.d32 = 0;
326 gintsts.b.sessreqintr = 1;
327 USB_OTG_WRITE_REG32 (&pdev->regs.GREGS->GINTSTS, gintsts.d32);
328 return 1;
333 * @brief USB_OTG_InitiateSRP
334 * Initiate an srp session
335 * @param None
336 * @retval : None
338 void USB_OTG_InitiateSRP(USB_OTG_CORE_HANDLE *pdev)
340 USB_OTG_GOTGCTL_TypeDef otgctl;
342 otgctl.d32 = 0;
344 otgctl.d32 = USB_OTG_READ_REG32( &pdev->regs.GREGS->GOTGCTL );
345 if (otgctl.b.sesreq)
347 return; /* SRP in progress */
349 otgctl.b.sesreq = 1;
350 USB_OTG_WRITE_REG32(&pdev->regs.GREGS->GOTGCTL, otgctl.d32);
355 * @brief USB_OTG_InitiateHNP
356 * Initiate HNP
357 * @param None
358 * @retval : None
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;
365 otgctl.d32 = 0;
366 hprt0.d32 = 0;
368 otgctl.d32 = USB_OTG_READ_REG32( &pdev->regs.GREGS->GOTGCTL );
369 if (mode)
370 { /* Device mode */
371 if (state)
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);
379 else
380 { /* Host mode */
381 if (state)
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
397 * @param None
398 * @retval : None
400 uint32_t USB_OTG_GetCurrentState (USB_OTG_CORE_HANDLE *pdev)
402 return pdev->otg.OTG_State;
407 * @}
411 * @}
415 * @}
418 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/