1 /*
2 * Copyright (c) 2016, Freescale Semiconductor, Inc.
3 * Copyright 2016 - 2017,2019 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "usb_otg_config.h"
10 #include "usb_otg.h"
11 #include "usb_otg_oci.h"
12 #include "fsl_device_registers.h"
13 #include "usb_otg_khci.h"
14
15 /*******************************************************************************
16 * Definitions
17 ******************************************************************************/
18
19 /* Component ID definition, used by tools. */
20 #ifndef FSL_COMPONENT_ID
21 #define FSL_COMPONENT_ID "middleware.usb.otg_stack"
22 #endif
23
24 /*******************************************************************************
25 * Prototypes
26 ******************************************************************************/
27
28 static void _USB_OtgGetControllerInterface(uint8_t controllerId,
29 const usb_otg_controller_interface_t **controllerTable);
30 static void _USB_OtgStartTimer(usb_otg_instance_t *otgInstance, uint32_t timeValue);
31 static void _USB_OtgCancelTimer(usb_otg_instance_t *otgInstance);
32 static void _USB_OtgEnterStateStart(usb_otg_instance_t *otgInstance);
33 static void _USB_OtgProcessStateStart(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue);
34 static void _USB_OtgEnterStateAIdle(usb_otg_instance_t *otgInstance);
35 static void _USB_OtgProcessStateAIdle(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue);
36 static void _USB_OtgEnterStateAWaitVrise(usb_otg_instance_t *otgInstance);
37 static void _USB_OtgProcessStateAWaitVrise(usb_otg_instance_t *otgInstance,
38 uint32_t otgChangeType,
39 uint32_t changeValue);
40 static void _USB_OtgEnterStateAWaitVfall(usb_otg_instance_t *otgInstance);
41 static void _USB_OtgProcessStateAWaitVfall(usb_otg_instance_t *otgInstance,
42 uint32_t otgChangeType,
43 uint32_t changeValue);
44 static void _USB_OtgEnterStateAWaitBcon(usb_otg_instance_t *otgInstance);
45 static void _USB_OtgProcessStateAWaitBcon(usb_otg_instance_t *otgInstance,
46 uint32_t otgChangeType,
47 uint32_t changeValue);
48 static void _USB_OtgEnterStateAHost(usb_otg_instance_t *otgInstance);
49 static void _USB_OtgProcessStateAHost(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue);
50 static void _USB_OtgEnterStateASuspend(usb_otg_instance_t *otgInstance);
51 static void _USB_OtgProcessStateASuspend(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue);
52 static void _USB_OtgEnterStateAPeripheral(usb_otg_instance_t *otgInstance);
53 static void _USB_OtgProcessStateAPeripheral(usb_otg_instance_t *otgInstance,
54 uint32_t otgChangeType,
55 uint32_t changeValue);
56 static void _USB_OtgEnterStateAVbusErr(usb_otg_instance_t *otgInstance);
57 static void _USB_OtgProcessStateAVbusErr(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue);
58 static void _USB_OtgEnterStateBIdle(usb_otg_instance_t *otgInstance);
59 static void _USB_OtgProcessStateBIdle(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue);
60 static void _USB_OtgEnterStateBSrpInit(usb_otg_instance_t *otgInstance);
61 static void _USB_OtgProcessStateBSrpInit(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue);
62 static void _USB_OtgEnterStateBPeripheral(usb_otg_instance_t *otgInstance);
63 static void _USB_OtgProcessStateBPeripheral(usb_otg_instance_t *otgInstance,
64 uint32_t otgChangeType,
65 uint32_t changeValue);
66 static void _USB_OtgEnterStateBWaitAcon(usb_otg_instance_t *otgInstance);
67 static void _USB_OtgProcessStateBWaitAcon(usb_otg_instance_t *otgInstance,
68 uint32_t otgChangeType,
69 uint32_t changeValue);
70 static void _USB_OtgEnterStateBHost(usb_otg_instance_t *otgInstance);
71 static void _USB_OtgProcessStateBHost(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue);
72
73 /*******************************************************************************
74 * Variables
75 ******************************************************************************/
76
77 #if (USB_OTG_CONFIG_KHCI)
78 static const usb_otg_controller_interface_t s_KhciInterface = {USB_OtgKhciInit, USB_OtgKhciDeinit, USB_OtgKhciControl};
79 #endif
80
81 /*******************************************************************************
82 * Code
83 ******************************************************************************/
84
_USB_OtgGetControllerInterface(uint8_t controllerId,const usb_otg_controller_interface_t ** controllerTable)85 static void _USB_OtgGetControllerInterface(uint8_t controllerId, const usb_otg_controller_interface_t **controllerTable)
86 {
87 #if (USB_OTG_CONFIG_KHCI)
88 if (controllerId == (uint8_t)kUSB_ControllerKhci0)
89 {
90 *controllerTable = &s_KhciInterface;
91 }
92 #endif
93 }
94
_USB_OtgStartTimer(usb_otg_instance_t * otgInstance,uint32_t timeValue)95 static void _USB_OtgStartTimer(usb_otg_instance_t *otgInstance, uint32_t timeValue)
96 {
97 #if (USB_OTG_CONFIG_KHCI)
98 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle, kOtg_ControlSetTimer,
99 timeValue, 0);
100 #endif
101 }
102
103 /* todo: cancel timer in the otg stack level, the timer cannot be canceled timely */
_USB_OtgCancelTimer(usb_otg_instance_t * otgInstance)104 static void _USB_OtgCancelTimer(usb_otg_instance_t *otgInstance)
105 {
106 #if (USB_OTG_CONFIG_KHCI)
107 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle, kOtg_ControlCancelTimer, 0,
108 0);
109 #endif
110 if (otgInstance->hasTimeOutMsg > 0U)
111 {
112 otgInstance->cancelTime = 1U;
113 }
114 }
115
_USB_OtgEnterStateStart(usb_otg_instance_t * otgInstance)116 static void _USB_OtgEnterStateStart(usb_otg_instance_t *otgInstance)
117 {
118 otgInstance->otgDeviceState = (uint8_t)kOtg_State_Start;
119 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle, kOtg_ControlRequestStatus,
120 kOtg_StatusId, kOtg_State_Start);
121
122 (void)USB_OtgNotifyChange(otgInstance, (uint32_t)kOtg_StatusPowerUp, 1);
123 }
124
_USB_OtgProcessStateStart(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)125 static void _USB_OtgProcessStateStart(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue)
126 {
127 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
128 if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop))) /* a_bus_drop/ */
129 {
130 return;
131 }
132
133 switch (statusType)
134 {
135 case kOtg_StatusId:
136 if (0U != changeValue)
137 {
138 /* todo: update controller state */
139 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusId;
140
141 _USB_OtgEnterStateBIdle(otgInstance); /* go to b_idle */
142 }
143 else
144 {
145 /* todo: update controller state */
146 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusId);
147
148 _USB_OtgEnterStateAIdle(otgInstance); /* go to a_idle */
149 }
150 break;
151
152 case kOtg_StatusPowerUp:
153 if (0U != changeValue)
154 {
155 /* todo: update controller state */
156 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusPowerUp;
157 }
158 break;
159
160 default:
161 /*no action*/
162 break;
163 }
164 }
165
_USB_OtgEnterStateAIdle(usb_otg_instance_t * otgInstance)166 static void _USB_OtgEnterStateAIdle(usb_otg_instance_t *otgInstance)
167 {
168 if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusId)))
169 {
170 _USB_OtgEnterStateBIdle(otgInstance);
171 }
172 else if ((0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop))) &&
173 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusPowerUp))))
174 {
175 _USB_OtgEnterStateAWaitVrise(otgInstance);
176 }
177 else
178 {
179 otgInstance->otgDeviceState = (uint8_t)kOtg_State_AIdle;
180 otgInstance->otgControllerStatus &= (~((uint32_t)kOtg_StatusBConn)); /* clear b_conn */
181
182 #if ((defined USB_OTG_ADP_ENABLE) && (USB_OTG_ADP_ENABLE))
183 /* todo: adp */
184 #endif
185
186 (void)otgInstance->controllerInterface->controllerControl(
187 otgInstance->controllerHandle, kOtg_ControlRequestStatus,
188 ((uint32_t)kOtg_StatusId) | ((uint32_t)kOtg_StatusSrpDet) | ((uint32_t)kOtg_StatusAdpChange),
189 (uint32_t)kOtg_State_AIdle);
190
191 otgInstance->otgCallback(otgInstance->otgCallbackParameter, (uint8_t)kOtg_EventStateChange,
192 (uint32_t)kOtg_State_AIdle);
193
194 if (0U != otgInstance->idChangeAsFalse)
195 {
196 otgInstance->idChangeAsFalse = 0U;
197 /* a change in id from TRUE to FALSE causes a_bus_req to be asserted unless the device is ADP capable. */
198 (void)USB_OtgNotifyChange(otgInstance, (uint32_t)kOtg_StatusBusReq, 1);
199 }
200 }
201 }
202
_USB_OtgProcessStateAIdle(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)203 static void _USB_OtgProcessStateAIdle(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue)
204 {
205 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
206 switch (statusType)
207 {
208 case kOtg_StatusBusReq:
209 if (0U != changeValue)
210 {
211 /* todo: update controller state */
212 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBusReq;
213
214 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop)))
215 {
216 _USB_OtgEnterStateAWaitVrise(otgInstance); /* go to a_wait_vrise */
217 }
218 }
219 else
220 {
221 /* todo: update controller state */
222 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBusReq);
223 }
224 break;
225
226 #if (USB_OTG_SRP_ENABLE)
227 case kOtg_StatusSrpDet:
228 if (0U != changeValue)
229 {
230 /* todo: update controller state */
231
232 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop)))
233 {
234 _USB_OtgEnterStateAWaitVrise(otgInstance); /* go to a_wait_vrise */
235 }
236 }
237 break;
238 #endif
239
240 #if ((defined USB_OTG_ADP_ENABLE) && (USB_OTG_ADP_ENABLE))
241 case kOtg_StatusAdpChange:
242 if (0U != changeValue)
243 {
244 /* todo: update controller state */
245
246 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop)))
247 {
248 _USB_OtgEnterStateAWaitVrise(otgInstance); /* go to a_wait_vrise */
249 }
250 }
251 break;
252 #endif
253
254 case kOtg_StatusPowerUp:
255 /* when power up, the kOtg_StatusBusDrop must be false */
256 if (0U != changeValue)
257 {
258 /* todo: update controller state */
259 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusPowerUp);
260
261 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop)))
262 {
263 _USB_OtgEnterStateAWaitVrise(otgInstance); /* go to a_wait_vrise */
264 }
265 }
266 break;
267
268 case kOtg_StatusId:
269 if (0U != changeValue)
270 {
271 /* todo: update controller state */
272 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusId;
273
274 _USB_OtgEnterStateBHost(otgInstance); /* go to b_host */
275 }
276 break;
277
278 case kOtg_StatusBusDrop:
279 if (0U != changeValue)
280 {
281 /* todo: update controller state */
282 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBusDrop;
283 }
284 else
285 {
286 /* todo: update controller state */
287 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBusDrop);
288
289 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusReq))) ||
290 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSrpDet))) ||
291 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusAdpChange))))
292 {
293 _USB_OtgEnterStateAWaitVrise(otgInstance); /* go to a_wait_vrise */
294 }
295 }
296 break;
297
298 default:
299 if (0U != changeValue)
300 {
301 otgInstance->otgControllerStatus |= otgChangeType;
302 }
303 else
304 {
305 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
306 }
307 break;
308 }
309 }
310
_USB_OtgEnterStateAWaitVrise(usb_otg_instance_t * otgInstance)311 static void _USB_OtgEnterStateAWaitVrise(usb_otg_instance_t *otgInstance)
312 {
313 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusId))) ||
314 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop))))
315 {
316 _USB_OtgEnterStateAWaitVfall(otgInstance);
317 }
318 else if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
319 {
320 _USB_OtgEnterStateAWaitBcon(otgInstance);
321 }
322 else
323 {
324 otgInstance->otgDeviceState = (uint8_t)kOtg_State_AWaitVrise;
325
326 /* driver vbus */
327 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
328 {
329 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusVbusVld;
330 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
331 (uint32_t)kOtg_ControlVbus, 1, 0);
332 }
333 (void)otgInstance->controllerInterface->controllerControl(
334 otgInstance->controllerHandle, kOtg_ControlRequestStatus,
335 ((uint32_t)kOtg_StatusId) | ((uint32_t)kOtg_StatusVbusVld), kOtg_State_AWaitVrise);
336
337 otgInstance->otgCallback(otgInstance->otgCallbackParameter, (uint32_t)kOtg_EventStateChange,
338 (uint8_t)kOtg_State_AWaitVrise);
339
340 /* set timer */
341 _USB_OtgStartTimer(otgInstance, USB_OTG_TIMER_A_WAIT_VRISE_TMR);
342 }
343 }
344
_USB_OtgProcessStateAWaitVrise(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)345 static void _USB_OtgProcessStateAWaitVrise(usb_otg_instance_t *otgInstance,
346 uint32_t otgChangeType,
347 uint32_t changeValue)
348 {
349 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
350 switch (statusType)
351 {
352 case kOtg_StatusBusDrop:
353 if (0U != changeValue)
354 {
355 /* todo: update controller state */
356 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBusDrop;
357
358 _USB_OtgCancelTimer(otgInstance);
359 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
360 }
361 break;
362
363 case kOtg_StatusId:
364 if (0U != changeValue)
365 {
366 /* todo: update controller state */
367 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusId;
368
369 _USB_OtgCancelTimer(otgInstance);
370 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
371 }
372 break;
373
374 case kOtg_StatusTimeOut:
375 /* todo: update controller state */
376
377 if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
378 {
379 _USB_OtgEnterStateAWaitBcon(otgInstance); /* go to a_wait_bcon */
380 }
381 else
382 {
383 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
384 }
385 break;
386
387 case kOtg_StatusVbusVld:
388 if (0U != changeValue)
389 {
390 /* todo: update controller state */
391 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusVbusVld;
392
393 _USB_OtgCancelTimer(otgInstance);
394 _USB_OtgEnterStateAWaitBcon(otgInstance); /* go to a_wait_bcon */
395 }
396 break;
397
398 default:
399 if (0U != changeValue)
400 {
401 otgInstance->otgControllerStatus |= (uint32_t)otgChangeType;
402 }
403 else
404 {
405 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
406 }
407 break;
408 }
409 }
410
_USB_OtgEnterStateAWaitVfall(usb_otg_instance_t * otgInstance)411 static void _USB_OtgEnterStateAWaitVfall(usb_otg_instance_t *otgInstance)
412 {
413 otgInstance->otgDeviceState = (uint8_t)kOtg_State_AWaitVfall;
414
415 (void)USB_OtgKhciControl(otgInstance->controllerHandle, (uint32_t)kOtg_ControlPullUp, 0, 0);
416
417 /* if there is bus req, the bus req fail. Or there is bus drop. */
418 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBusReq);
419
420 /* close vbus */
421 if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
422 {
423 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusVbusVld);
424 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
425 (uint32_t)kOtg_ControlVbus, 0, 0);
426 }
427 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle, kOtg_ControlRequestStatus,
428 0, kOtg_State_AWaitVfall);
429
430 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStateChange, kOtg_State_AWaitVfall);
431
432 /* set timer */
433 _USB_OtgStartTimer(otgInstance, USB_OTG_TIMER_A_WAIT_VFALL_TMR);
434 }
435
_USB_OtgProcessStateAWaitVfall(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)436 static void _USB_OtgProcessStateAWaitVfall(usb_otg_instance_t *otgInstance,
437 uint32_t otgChangeType,
438 uint32_t changeValue)
439 {
440 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
441 switch (statusType)
442 {
443 case kOtg_StatusTimeOut:
444 /* todo: update controller state */
445
446 _USB_OtgEnterStateAIdle(otgInstance); /* go to a_idle */
447 break;
448
449 default:
450 if (0U != changeValue)
451 {
452 otgInstance->otgControllerStatus |= (uint32_t)otgChangeType;
453 }
454 else
455 {
456 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
457 }
458 break;
459 }
460 }
461
_USB_OtgEnterStateAWaitBcon(usb_otg_instance_t * otgInstance)462 static void _USB_OtgEnterStateAWaitBcon(usb_otg_instance_t *otgInstance)
463 {
464 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusId))) ||
465 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop))))
466 {
467 _USB_OtgEnterStateAWaitVfall(otgInstance);
468 }
469 else if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBConn)))
470 {
471 _USB_OtgEnterStateAHost(otgInstance);
472 }
473 else if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
474 {
475 _USB_OtgEnterStateAVbusErr(otgInstance);
476 }
477 else
478 {
479 otgInstance->otgDeviceState = (uint8_t)kOtg_State_AWaitBcon;
480
481 /* driver vbus */
482 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
483 {
484 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusVbusVld;
485 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
486 (uint32_t)kOtg_ControlVbus, 1, 0);
487 }
488 (void)otgInstance->controllerInterface->controllerControl(
489 otgInstance->controllerHandle, kOtg_ControlRequestStatus,
490 ((uint32_t)kOtg_StatusId) | ((uint32_t)kOtg_StatusVbusInvld) | ((uint32_t)kOtg_StatusBConn),
491 kOtg_State_AWaitBcon);
492
493 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStateChange, kOtg_State_AWaitBcon);
494
495 /* set timer */
496 #if (USB_OTG_TIMER_A_WAIT_BCON_TMR != 0xFFFFFFFFU)
497 _USB_OtgStartTimer(otgInstance, USB_OTG_TIMER_A_WAIT_BCON_TMR);
498 #endif
499 }
500 }
501
_USB_OtgProcessStateAWaitBcon(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)502 static void _USB_OtgProcessStateAWaitBcon(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue)
503 {
504 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
505 switch (statusType)
506 {
507 case kOtg_StatusTimeOut:
508 /* todo: update controller state */
509
510 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
511 break;
512
513 case kOtg_StatusId:
514 if (0U != changeValue)
515 {
516 /* todo: update controller state */
517 otgInstance->otgControllerStatus |= ((uint32_t)kOtg_StatusId);
518
519 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
520 }
521 break;
522
523 case kOtg_StatusBusDrop:
524 if (0U != changeValue)
525 {
526 /* todo: update controller state */
527 otgInstance->otgControllerStatus |= ((uint32_t)kOtg_StatusBusDrop);
528
529 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
530 }
531 break;
532
533 case kOtg_StatusBConn:
534 if (0U != changeValue)
535 {
536 /* todo: update controller state */
537 otgInstance->otgControllerStatus |= ((uint32_t)kOtg_StatusBConn);
538
539 _USB_OtgEnterStateAHost(otgInstance); /* go to a_host */
540 }
541 break;
542
543 case kOtg_StatusVbusVld:
544 if (0U == changeValue)
545 {
546 /* todo: update controller state */
547 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusVbusVld);
548
549 _USB_OtgEnterStateAVbusErr(otgInstance); /* go to a_vbus_err */
550 }
551 break;
552
553 default:
554 if (0U != changeValue)
555 {
556 otgInstance->otgControllerStatus |= ((uint32_t)otgChangeType);
557 }
558 else
559 {
560 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
561 }
562 break;
563 }
564 }
565
_USB_OtgEnterStateAHost(usb_otg_instance_t * otgInstance)566 static void _USB_OtgEnterStateAHost(usb_otg_instance_t *otgInstance)
567 {
568 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusId))) ||
569 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop))))
570 {
571 _USB_OtgEnterStateAWaitVfall(otgInstance);
572 }
573 else if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBConn)))
574 {
575 _USB_OtgEnterStateAWaitBcon(otgInstance);
576 }
577 /*
578 else if (!(otgInstance->otgControllerStatus & kOtg_StatusBusReq))
579 {
580 _USB_OtgEnterStateASuspend(otgInstance);
581 }
582 */
583 else if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
584 {
585 _USB_OtgEnterStateAVbusErr(otgInstance);
586 }
587 else
588 {
589 otgInstance->otgDeviceState = ((uint8_t)kOtg_State_AHost);
590 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBHNPFeature);
591
592 /* driver vbus */
593 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
594 {
595 otgInstance->otgControllerStatus |= ((uint32_t)kOtg_StatusVbusVld);
596 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
597 (uint32_t)kOtg_ControlVbus, 1, 0);
598 }
599 /* pull down dp and dm */
600 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle, kOtg_ControlPullDown,
601 ((uint32_t)kOtg_PullDp) | ((uint32_t)kOtg_PullDm), 0);
602 (void)otgInstance->controllerInterface->controllerControl(
603 otgInstance->controllerHandle, kOtg_ControlRequestStatus,
604 ((uint32_t)kOtg_StatusId) | ((uint32_t)kOtg_StatusBDisconn) | ((uint32_t)kOtg_StatusVbusInvld),
605 kOtg_State_AHost);
606
607 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStateChange, kOtg_State_AHost);
608
609 #if (USB_OTG_TIME_WAIT_DEVICE_INIT != 0U)
610 otgInstance->waitInit = 1U;
611 _USB_OtgStartTimer(otgInstance, USB_OTG_TIME_WAIT_DEVICE_INIT); /* wait the B-Device init the device stack */
612 #else
613 /* start work as host */
614 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStackInit, kOtg_StackHostInit);
615 #endif
616 }
617 }
618
_USB_OtgExitHost(usb_otg_instance_t * otgInstance)619 static void _USB_OtgExitHost(usb_otg_instance_t *otgInstance)
620 {
621 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStackInit,
622 kOtg_StackHostDeinit); /* host stack de-init */
623 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
624 (uint32_t)kOtg_ControlPullDown, 0, 0);
625 }
626
_USB_OtgProcessStateAHost(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)627 static void _USB_OtgProcessStateAHost(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue)
628 {
629 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
630 switch (statusType)
631 {
632 #if (USB_OTG_TIME_WAIT_DEVICE_INIT != 0U)
633 case kOtg_StatusTimeOut:
634 if (0U != otgInstance->waitInit) /* init host */
635 {
636 /* start work as host */
637 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStackInit,
638 kOtg_StackHostInit); /* host stack init */
639 otgInstance->waitInit = 0U;
640 }
641 break;
642 #endif
643
644 case kOtg_StatusBConn:
645 if (0U == changeValue)
646 {
647 /* todo: update controller state */
648 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBConn);
649
650 _USB_OtgExitHost(otgInstance);
651 _USB_OtgEnterStateAWaitBcon(otgInstance); /* go to a_wait_bcon */
652 }
653 break;
654
655 case kOtg_StatusId:
656 if (0U != changeValue)
657 {
658 /* todo: update controller state */
659 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusId;
660
661 _USB_OtgExitHost(otgInstance);
662 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
663 }
664 break;
665
666 case kOtg_StatusBusDrop:
667 if (0U != changeValue)
668 {
669 /* todo: update controller state */
670 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBusDrop;
671
672 _USB_OtgExitHost(otgInstance);
673 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
674 }
675 break;
676
677 case kOtg_StatusBusReq: /* hnp or host release bus */
678 if (0U == changeValue) /* bus release */
679 {
680 /* todo: update controller state */
681 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBusReq);
682
683 _USB_OtgExitHost(otgInstance);
684 /* USB_HostSuspend(otgInstance->hostHandle); */
685 _USB_OtgEnterStateASuspend(otgInstance); /* go to a_suspend */
686 }
687 else /* HNP bus request */
688 {
689 /* todo: update controller state */
690 /* otgInstance->otgControllerStatus &= (~kOtg_StatusBusReq); */
691
692 _USB_OtgExitHost(otgInstance);
693 /* USB_HostSuspend(otgInstance->hostHandle); */
694 _USB_OtgEnterStateASuspend(otgInstance); /* go to a_suspend */
695 }
696 break;
697
698 case kOtg_StatusVbusVld:
699 if (0U == changeValue)
700 {
701 /* todo: update controller state */
702 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusVbusVld);
703
704 _USB_OtgExitHost(otgInstance);
705 _USB_OtgEnterStateAVbusErr(otgInstance); /* go to a_vbus_err */
706 }
707 break;
708
709 default:
710 if (0U != changeValue)
711 {
712 otgInstance->otgControllerStatus |= (uint32_t)otgChangeType;
713 }
714 else
715 {
716 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
717 }
718 break;
719 }
720 }
721
_USB_OtgEnterStateASuspend(usb_otg_instance_t * otgInstance)722 static void _USB_OtgEnterStateASuspend(usb_otg_instance_t *otgInstance)
723 {
724 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusId))) ||
725 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop))))
726 {
727 _USB_OtgEnterStateAWaitVfall(otgInstance);
728 }
729 else if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBConn)))
730 {
731 if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBHNPFeature)))
732 {
733 _USB_OtgEnterStateAPeripheral(otgInstance);
734 }
735 else
736 {
737 _USB_OtgEnterStateAWaitBcon(otgInstance);
738 }
739 }
740 else if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
741 {
742 _USB_OtgEnterStateAVbusErr(otgInstance);
743 }
744 else
745 {
746 otgInstance->otgDeviceState = (uint8_t)kOtg_State_ASuspend;
747
748 /* driver vbus */
749 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
750 {
751 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusVbusVld;
752 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
753 (uint32_t)kOtg_ControlVbus, 1, 0);
754 }
755 (void)otgInstance->controllerInterface->controllerControl(
756 otgInstance->controllerHandle, kOtg_ControlRequestStatus,
757 ((uint32_t)kOtg_StatusId) | ((uint32_t)kOtg_StatusBDisconn) | ((uint32_t)kOtg_StatusVbusInvld),
758 (uint32_t)kOtg_State_ASuspend);
759
760 otgInstance->otgCallback(otgInstance->otgCallbackParameter, (uint32_t)kOtg_EventStateChange,
761 kOtg_State_ASuspend);
762
763 if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBHNPFeature)))
764 {
765 /* set timer */
766 _USB_OtgStartTimer(otgInstance, USB_OTG_TIMER_A_AIDL_BDIS_TMR);
767 }
768 }
769 }
770
_USB_OtgProcessStateASuspend(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)771 static void _USB_OtgProcessStateASuspend(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue)
772 {
773 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
774 switch (statusType)
775 {
776 case kOtg_StatusBConn:
777 if (0U == changeValue)
778 {
779 /* todo: update controller state */
780 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBConn);
781 _USB_OtgCancelTimer(otgInstance);
782 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBHNPFeature)))
783 {
784 _USB_OtgEnterStateAWaitBcon(otgInstance); /* go to a_wait_bcon */
785 }
786 else
787 {
788 _USB_OtgEnterStateAPeripheral(otgInstance); /* go to a_peripheral */
789 }
790 }
791 break;
792
793 case kOtg_StatusTimeOut:
794 if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBHNPFeature)))
795 {
796 if (0U != changeValue)
797 {
798 /* todo: update controller state */
799
800 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
801 }
802 }
803 break;
804
805 case kOtg_StatusBusReq: /* resume */
806 if (0U != changeValue)
807 {
808 /* todo: update controller state */
809 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBusReq;
810
811 _USB_OtgCancelTimer(otgInstance);
812 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
813 kOtg_ControlResume, 20, 0);
814
815 _USB_OtgEnterStateAHost(otgInstance); /* go to a_host */
816 }
817 break;
818
819 case kOtg_StatusId:
820 if (0U != changeValue)
821 {
822 /* todo: update controller state */
823 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusId;
824
825 _USB_OtgCancelTimer(otgInstance);
826 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
827 }
828 break;
829
830 case kOtg_StatusBusDrop:
831 if (0U != changeValue)
832 {
833 /* todo: update controller state */
834 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBusDrop;
835
836 _USB_OtgCancelTimer(otgInstance);
837 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
838 }
839 break;
840
841 case kOtg_StatusVbusVld:
842 if (0U == changeValue)
843 {
844 /* todo: update controller state */
845 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusVbusVld);
846
847 _USB_OtgCancelTimer(otgInstance);
848 _USB_OtgEnterStateAVbusErr(otgInstance); /* go to a_vbus_err */
849 }
850 break;
851
852 default:
853 if (0U != changeValue)
854 {
855 otgInstance->otgControllerStatus |= (uint32_t)otgChangeType;
856 }
857 else
858 {
859 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
860 }
861 break;
862 }
863 }
864
_USB_OtgEnterStateAPeripheral(usb_otg_instance_t * otgInstance)865 static void _USB_OtgEnterStateAPeripheral(usb_otg_instance_t *otgInstance)
866 {
867 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusId))) ||
868 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop))))
869 {
870 _USB_OtgEnterStateAWaitVfall(otgInstance);
871 }
872 else if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
873 {
874 _USB_OtgEnterStateAVbusErr(otgInstance);
875 }
876 else
877 {
878 otgInstance->otgDeviceState = (uint8_t)kOtg_State_APeripheral;
879 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBConn);
880
881 /* start work as device */
882 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
883 (uint32_t)kOtg_ControlPullUp, kOtg_PullDp, 0);
884 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
885 (uint32_t)kOtg_ControlPullDown, kOtg_PullDm, 0);
886 /* driver vbus */
887 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
888 {
889 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusVbusVld;
890 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
891 (uint32_t)kOtg_ControlVbus, 1, 0);
892 }
893 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStackInit,
894 kOtg_StackDeviceInit); /* device stack init */
895 #if (USB_OTG_TIME_WAIT_BHOST != 0U)
896 (void)otgInstance->controllerInterface->controllerControl(
897 otgInstance->controllerHandle, kOtg_ControlRequestStatus,
898 ((uint32_t)kOtg_StatusId) | ((uint32_t)kOtg_StatusVbusInvld), kOtg_State_APeripheral);
899 #else
900 (void)otgInstance->controllerInterface->controllerControl(
901 otgInstance->controllerHandle, kOtg_ControlRequestStatus,
902 ((uint32_t)kOtg_StatusId) | ((uint32_t)kOtg_StatusVbusInvld) |
903 ((uint32_t)kOtg_StatusCheckIdleInAPeripheral),
904 kOtg_State_APeripheral);
905 #endif
906
907 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStateChange, kOtg_State_APeripheral);
908
909 #if (USB_OTG_TIME_WAIT_BHOST != 0U)
910 /* wait the B-Device init the host stack */
911 otgInstance->waitInit = 1;
912 _USB_OtgStartTimer(otgInstance, USB_OTG_TIME_WAIT_BHOST);
913 #endif
914 }
915 }
916
_USB_OtgExitDevice(usb_otg_instance_t * otgInstance)917 static void _USB_OtgExitDevice(usb_otg_instance_t *otgInstance)
918 {
919 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
920 (uint32_t)kOtg_ControlPullUp, 0, 0);
921 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
922 (uint32_t)kOtg_ControlPullDown, 0, 0);
923 /* otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle, kOtg_ControlPullUp, 0); */
924 otgInstance->otgCallback(otgInstance->otgCallbackParameter, (uint32_t)kOtg_EventStackInit,
925 kOtg_StackDeviceDeinit); /* device stack de-init */
926 }
927
_USB_OtgProcessStateAPeripheral(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)928 static void _USB_OtgProcessStateAPeripheral(usb_otg_instance_t *otgInstance,
929 uint32_t otgChangeType,
930 uint32_t changeValue)
931 {
932 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
933 switch (statusType)
934 {
935 case kOtg_StatusBusReq:
936 if (0U != changeValue)
937 {
938 /* todo: set hnp flag for polling */
939 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBusReq;
940 }
941 break;
942
943 case kOtg_StatusId:
944 if (0U != changeValue)
945 {
946 /* todo: update controller state */
947 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusId;
948
949 _USB_OtgCancelTimer(otgInstance);
950 _USB_OtgExitDevice(otgInstance);
951 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
952 }
953 break;
954
955 case kOtg_StatusBusDrop:
956 if (0U != changeValue)
957 {
958 /* todo: update controller state */
959 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBusDrop;
960
961 _USB_OtgCancelTimer(otgInstance);
962 _USB_OtgExitDevice(otgInstance);
963 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
964 }
965 break;
966
967 case kOtg_StatusTimeOut:
968 #if (USB_OTG_TIME_WAIT_BHOST != 0U)
969 if (0U != otgInstance->waitInit) /* wait init host */
970 {
971 (void)otgInstance->controllerInterface->controllerControl(
972 otgInstance->controllerHandle, kOtg_ControlRequestStatus, kOtg_StatusCheckIdleInAPeripheral,
973 kOtg_State_APeripheral);
974 otgInstance->waitInit = 0;
975 }
976 else
977 {
978 #endif
979 if (0U != changeValue)
980 {
981 /* todo: update controller state */
982
983 _USB_OtgExitDevice(otgInstance);
984 _USB_OtgEnterStateAWaitBcon(otgInstance); /* go to a_wait_bcon */
985 }
986 #if (USB_OTG_TIME_WAIT_BHOST != 0U)
987 }
988 #endif
989 break;
990
991 case kOtg_StatusVbusVld:
992 if (0U == changeValue)
993 {
994 /* todo: update controller state */
995 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusVbusVld);
996
997 _USB_OtgCancelTimer(otgInstance);
998 _USB_OtgExitDevice(otgInstance);
999 _USB_OtgEnterStateAVbusErr(otgInstance); /* go to a_vbus_err */
1000 }
1001 break;
1002
1003 default:
1004 if (0U != changeValue)
1005 {
1006 otgInstance->otgControllerStatus |= otgChangeType;
1007 }
1008 else
1009 {
1010 otgInstance->otgControllerStatus &= (~otgChangeType);
1011 }
1012 break;
1013 }
1014 }
1015
_USB_OtgEnterStateAVbusErr(usb_otg_instance_t * otgInstance)1016 static void _USB_OtgEnterStateAVbusErr(usb_otg_instance_t *otgInstance)
1017 {
1018 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusId))) ||
1019 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusDrop))))
1020 {
1021 _USB_OtgEnterStateAWaitVfall(otgInstance);
1022 }
1023 else
1024 {
1025 otgInstance->otgDeviceState = (uint8_t)kOtg_State_AVbusErr;
1026
1027 /* don't driver vbus */
1028 if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
1029 {
1030 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusVbusVld);
1031 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
1032 (uint32_t)kOtg_ControlVbus, 0, 0);
1033 }
1034 (void)otgInstance->controllerInterface->controllerControl(
1035 otgInstance->controllerHandle, kOtg_ControlRequestStatus, kOtg_StatusId, kOtg_State_AVbusErr);
1036
1037 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStateChange, kOtg_State_AVbusErr);
1038 }
1039 }
1040
_USB_OtgProcessStateAVbusErr(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)1041 static void _USB_OtgProcessStateAVbusErr(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue)
1042 {
1043 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
1044 switch (statusType)
1045 {
1046 case kOtg_StatusId:
1047 if (0U != changeValue)
1048 {
1049 /* todo: update controller state */
1050 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusId;
1051
1052 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
1053 }
1054 break;
1055
1056 case kOtg_StatusBusDrop:
1057 if (0U != changeValue)
1058 {
1059 /* todo: update controller state */
1060 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBusDrop;
1061
1062 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
1063 }
1064 break;
1065
1066 case kOtg_StatusClrErr:
1067 if (0U == changeValue)
1068 {
1069 /* todo: update controller state */
1070
1071 _USB_OtgEnterStateAWaitVfall(otgInstance); /* go to a_wait_vfall */
1072 }
1073 break;
1074
1075 default:
1076 if (0U != changeValue)
1077 {
1078 otgInstance->otgControllerStatus |= (uint32_t)otgChangeType;
1079 }
1080 else
1081 {
1082 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
1083 }
1084 break;
1085 }
1086 }
1087
_USB_OtgEnterStateBIdle(usb_otg_instance_t * otgInstance)1088 static void _USB_OtgEnterStateBIdle(usb_otg_instance_t *otgInstance)
1089 {
1090 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusId)))
1091 {
1092 otgInstance->otgControllerStatus = 0U; /* default controller status */
1093 _USB_OtgEnterStateAIdle(otgInstance);
1094 }
1095 else if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSessVld)))
1096 {
1097 otgInstance->otgControllerStatus = (uint32_t)kOtg_StatusId;
1098 _USB_OtgEnterStateBPeripheral(otgInstance);
1099 }
1100 else
1101 {
1102 otgInstance->otgDeviceState = (uint8_t)kOtg_State_BIdle;
1103 otgInstance->otgControllerStatus = (uint32_t)kOtg_StatusId;
1104
1105 #if ((defined USB_OTG_ADP_ENABLE) && (USB_OTG_ADP_ENABLE))
1106 /* todo: adp */
1107 #endif
1108
1109 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle, kOtg_ControlPullUp, 0,
1110 0); /* disable pull-up */
1111 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle, kOtg_ControlPullDown,
1112 0, 0); /* disable DP&DM pulldown */
1113 (void)otgInstance->controllerInterface->controllerControl(
1114 otgInstance->controllerHandle, kOtg_ControlRequestStatus,
1115 ((uint32_t)kOtg_StatusId) | ((uint32_t)kOtg_StatusAdpChange) | ((uint32_t)kOtg_StatusSessVld) |
1116 ((uint32_t)kOtg_StatusSe0Srp) | ((uint32_t)kOtg_StatusSsendSrp),
1117 kOtg_State_BIdle);
1118
1119 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStateChange, kOtg_State_BIdle);
1120 }
1121 }
1122
_USB_OtgProcessStateBIdle(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)1123 static void _USB_OtgProcessStateBIdle(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue)
1124 {
1125 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
1126 switch (statusType)
1127 {
1128 case kOtg_StatusId:
1129 if (0U == changeValue)
1130 {
1131 /* todo: update controller state */
1132 otgInstance->otgControllerStatus = 0U; /* default controller status */
1133
1134 _USB_OtgEnterStateAIdle(otgInstance); /* go to a_idle */
1135 }
1136 break;
1137
1138 case kOtg_StatusSsendSrp:
1139 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusSsendSrp;
1140 if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSe0Srp)))
1141 {
1142 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusPowerUp))) ||
1143 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusReq))) ||
1144 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusAdpChange))))
1145 {
1146 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusPowerUp);
1147 _USB_OtgEnterStateBSrpInit(otgInstance); /* go to b_srp_init */
1148 }
1149 }
1150 break;
1151
1152 case kOtg_StatusSe0Srp:
1153 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusSe0Srp;
1154 if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSsendSrp)))
1155 {
1156 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusPowerUp))) ||
1157 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusReq))) ||
1158 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusAdpChange))))
1159 {
1160 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusPowerUp);
1161 _USB_OtgEnterStateBSrpInit(otgInstance); /* go to b_srp_init */
1162 }
1163 }
1164 break;
1165
1166 case kOtg_StatusPowerUp:
1167 if (0U != changeValue)
1168 {
1169 /* todo: update controller state */
1170 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusPowerUp;
1171
1172 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSsendSrp))) &&
1173 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSe0Srp))))
1174 {
1175 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusPowerUp);
1176 _USB_OtgEnterStateBSrpInit(otgInstance); /* go to b_srp_init */
1177 }
1178 }
1179 break;
1180
1181 case kOtg_StatusBusReq:
1182 if (0U != changeValue)
1183 {
1184 /* todo: update controller state */
1185 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBusReq;
1186
1187 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSsendSrp))) &&
1188 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSe0Srp))))
1189 {
1190 _USB_OtgEnterStateBSrpInit(otgInstance); /* go to b_srp_init */
1191 }
1192 }
1193 break;
1194
1195 case kOtg_StatusAdpChange:
1196 if (0U != changeValue)
1197 {
1198 /* todo: update controller state */
1199
1200 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSsendSrp))) &&
1201 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSe0Srp))))
1202 {
1203 _USB_OtgEnterStateBSrpInit(otgInstance); /* go to b_srp_init */
1204 }
1205 }
1206 break;
1207
1208 case kOtg_StatusSessVld:
1209 if (0U != changeValue)
1210 {
1211 /* todo: update controller state */
1212 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusSessVld;
1213
1214 _USB_OtgEnterStateBPeripheral(otgInstance); /* go to b_peripheral */
1215 }
1216 break;
1217
1218 default:
1219 if (0U != changeValue)
1220 {
1221 otgInstance->otgControllerStatus |= (uint32_t)otgChangeType;
1222 }
1223 else
1224 {
1225 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
1226 }
1227 break;
1228 }
1229 }
1230
_USB_OtgEnterStateBSrpInit(usb_otg_instance_t * otgInstance)1231 static void _USB_OtgEnterStateBSrpInit(usb_otg_instance_t *otgInstance)
1232 {
1233 otgInstance->otgControllerStatus = ~((uint32_t)kOtg_StatusBusReq); /* clear b_bus_req */
1234
1235 if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusId)))
1236 {
1237 _USB_OtgEnterStateBIdle(otgInstance);
1238 }
1239 else
1240 {
1241 otgInstance->otgDeviceState = (uint8_t)kOtg_State_BSrpInit;
1242
1243 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
1244 (uint32_t)kOtg_ControlDataPulse, 1, 0);
1245
1246 (void)otgInstance->controllerInterface->controllerControl(
1247 otgInstance->controllerHandle, (uint32_t)kOtg_ControlRequestStatus,
1248 ((uint32_t)kOtg_StatusId) | ((uint32_t)kOtg_StatusBSrpDone), (uint32_t)kOtg_State_BSrpInit);
1249 /* todo: check SRP fail */
1250 /* _USB_OtgStartTimer(otgInstance, USB_OTG_TIME_B_SRP_FAIL); */
1251
1252 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStateChange, kOtg_State_BSrpInit);
1253 }
1254 }
1255
_USB_OtgProcessStateBSrpInit(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)1256 static void _USB_OtgProcessStateBSrpInit(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue)
1257 {
1258 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
1259 switch (statusType)
1260 {
1261 case kOtg_StatusId:
1262 if (0U == changeValue)
1263 {
1264 /* todo: update controller state */
1265 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusId);
1266
1267 _USB_OtgEnterStateBIdle(otgInstance); /* go to b_idle */
1268 }
1269 break;
1270
1271 case kOtg_StatusBSrpDone:
1272 if (0U != changeValue)
1273 {
1274 /* todo: update controller state */
1275
1276 _USB_OtgEnterStateBIdle(otgInstance); /* go to b_idle */
1277 }
1278 break;
1279
1280 #if 0
1281 case kOtg_StatusTimeOut: /* b_srp_done and srp fail */
1282 if (0U != changeValue)
1283 {
1284 /* todo: update controller state */
1285
1286 _USB_OtgEnterStateBIdle(otgInstance); /* go to b_idle */
1287 }
1288 break;
1289
1290 case kOtg_StatusSessVld: /* this change is not align with spec state machine */
1291 if (0U != changeValue)
1292 {
1293 /* todo: update controller state */
1294 otgInstance->otgControllerStatus |= kOtg_StatusSessVld;
1295
1296 _USB_OtgCancelTimer(otgInstance);
1297 _USB_OtgEnterStateBIdle(otgInstance); /* go to b_idle */
1298 }
1299 break;
1300 #endif
1301
1302 default:
1303 if (0U != changeValue)
1304 {
1305 otgInstance->otgControllerStatus |= (uint32_t)otgChangeType;
1306 }
1307 else
1308 {
1309 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
1310 }
1311 break;
1312 }
1313 }
1314
_USB_OtgEnterStateBPeripheral(usb_otg_instance_t * otgInstance)1315 static void _USB_OtgEnterStateBPeripheral(usb_otg_instance_t *otgInstance)
1316 {
1317 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusAConn);
1318
1319 if ((0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusId))) ||
1320 (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSessVld))))
1321 {
1322 _USB_OtgEnterStateBIdle(otgInstance);
1323 }
1324 else
1325 {
1326 otgInstance->otgDeviceState = (uint8_t)kOtg_State_BPeripheral;
1327 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBHNPFeature);
1328
1329 /* start work as device */
1330 (void)otgInstance->controllerInterface->controllerControl(
1331 otgInstance->controllerHandle, (uint32_t)kOtg_ControlPullUp, (uint32_t)kOtg_PullDp, 0);
1332 (void)otgInstance->controllerInterface->controllerControl(
1333 otgInstance->controllerHandle, (uint32_t)kOtg_ControlPullDown, (uint32_t)kOtg_PullDm, 0);
1334 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStackInit,
1335 kOtg_StackDeviceInit); /* device stack init */
1336
1337 (void)otgInstance->controllerInterface->controllerControl(
1338 otgInstance->controllerHandle, (uint32_t)kOtg_ControlRequestStatus,
1339 ((uint32_t)kOtg_StatusId) | ((uint32_t)kOtg_StatusSessInvld) | ((uint32_t)kOtg_StatusBusSuspend),
1340 ((uint32_t)kOtg_State_BPeripheral));
1341
1342 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStateChange, kOtg_State_BPeripheral);
1343 }
1344 }
1345
_USB_OtgProcessStateBPeripheral(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)1346 static void _USB_OtgProcessStateBPeripheral(usb_otg_instance_t *otgInstance,
1347 uint32_t otgChangeType,
1348 uint32_t changeValue)
1349 {
1350 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
1351 switch (statusType)
1352 {
1353 case kOtg_StatusId:
1354 if (0U == changeValue)
1355 {
1356 /* todo: update controller state */
1357 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusId);
1358
1359 _USB_OtgExitDevice(otgInstance);
1360 _USB_OtgEnterStateBIdle(otgInstance); /* go to b_idle */
1361 }
1362 break;
1363
1364 case kOtg_StatusSessVld:
1365 if (0U == changeValue)
1366 {
1367 /* todo: update controller state */
1368 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusSessVld);
1369
1370 _USB_OtgExitDevice(otgInstance);
1371 _USB_OtgEnterStateBIdle(otgInstance); /* go to b_idle */
1372 }
1373 break;
1374
1375 case kOtg_StatusVbusVld:
1376 if (0U == changeValue)
1377 {
1378 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusVbusVld);
1379 _USB_OtgExitDevice(otgInstance);
1380 _USB_OtgEnterStateBIdle(otgInstance); /* go to b_idle */
1381 }
1382 else
1383 {
1384 otgInstance->otgControllerStatus |= (uint32_t)(kOtg_StatusVbusVld);
1385 }
1386 break;
1387
1388 case kOtg_StatusBusReq:
1389 if (0U != changeValue)
1390 {
1391 /* todo: update controller state */
1392 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBusReq;
1393 }
1394 break;
1395
1396 case kOtg_StatusBusSuspend:
1397 if (0U != changeValue)
1398 {
1399 /* todo: update controller state */
1400
1401 if ((0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBHNPFeature))) &&
1402 (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusReq))))
1403 {
1404 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
1405 (uint32_t)kOtg_ControlPullUp, 0, 0);
1406 _USB_OtgExitDevice(otgInstance);
1407 _USB_OtgEnterStateBWaitAcon(otgInstance); /* go to b_wait_acon */
1408 }
1409 }
1410 break;
1411
1412 default:
1413 if (0U != changeValue)
1414 {
1415 otgInstance->otgControllerStatus |= (uint32_t)otgChangeType;
1416 }
1417 else
1418 {
1419 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
1420 }
1421 break;
1422 }
1423 }
1424
_USB_OtgEnterStateBWaitAcon(usb_otg_instance_t * otgInstance)1425 static void _USB_OtgEnterStateBWaitAcon(usb_otg_instance_t *otgInstance)
1426 {
1427 if ((0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusId))) ||
1428 (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusSessVld))))
1429 {
1430 _USB_OtgEnterStateBIdle(otgInstance);
1431 }
1432 else if (0U != (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusAConn)))
1433 {
1434 _USB_OtgEnterStateBHost(otgInstance);
1435 }
1436 else
1437 {
1438 otgInstance->otgDeviceState = (uint8_t)kOtg_State_BWaitAcon;
1439
1440 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
1441 (uint32_t)kOtg_ControlPullUp, 0, 0);
1442 (void)otgInstance->controllerInterface->controllerControl(
1443 otgInstance->controllerHandle, (uint32_t)kOtg_ControlRequestStatus,
1444 ((uint32_t)kOtg_StatusBusResume) | ((uint32_t)kOtg_StatusAConn), (uint32_t)kOtg_State_BWaitAcon);
1445
1446 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStateChange, kOtg_State_BWaitAcon);
1447
1448 /* set timer */
1449 _USB_OtgStartTimer(otgInstance, USB_OTG_TIMER_B_ASE0_BRST_TMR);
1450 }
1451 }
1452
_USB_OtgProcessStateBWaitAcon(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)1453 static void _USB_OtgProcessStateBWaitAcon(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue)
1454 {
1455 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
1456 switch (statusType)
1457 {
1458 case kOtg_StatusAConn:
1459 if (0U != changeValue)
1460 {
1461 /* todo: update controller state */
1462 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusAConn;
1463
1464 _USB_OtgCancelTimer(otgInstance);
1465 _USB_OtgEnterStateBHost(otgInstance); /* go to b_host */
1466 }
1467 break;
1468
1469 case kOtg_StatusId:
1470 if (0U == changeValue)
1471 {
1472 /* todo: update controller state */
1473 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusId);
1474
1475 _USB_OtgCancelTimer(otgInstance);
1476 _USB_OtgEnterStateBIdle(otgInstance); /* go to b_idle */
1477 }
1478 break;
1479
1480 case kOtg_StatusSessVld:
1481 if (0U == changeValue)
1482 {
1483 /* todo: update controller state */
1484 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusSessVld);
1485
1486 _USB_OtgCancelTimer(otgInstance);
1487 _USB_OtgEnterStateBIdle(otgInstance); /* go to b_idle */
1488 }
1489 break;
1490
1491 case kOtg_StatusBusResume:
1492 if (0U != changeValue)
1493 {
1494 /* todo: update controller state */
1495
1496 _USB_OtgCancelTimer(otgInstance);
1497 _USB_OtgEnterStateBPeripheral(otgInstance); /* go to b_peripheral */
1498 }
1499 break;
1500
1501 case kOtg_StatusTimeOut:
1502 if (0U != changeValue)
1503 {
1504 /* todo: update controller state */
1505
1506 _USB_OtgEnterStateBPeripheral(otgInstance); /* go to b_peripheral */
1507 }
1508 break;
1509
1510 default:
1511 if (0U != changeValue)
1512 {
1513 otgInstance->otgControllerStatus |= (uint32_t)otgChangeType;
1514 }
1515 else
1516 {
1517 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
1518 }
1519 break;
1520 }
1521 }
1522
_USB_OtgEnterStateBHost(usb_otg_instance_t * otgInstance)1523 static void _USB_OtgEnterStateBHost(usb_otg_instance_t *otgInstance)
1524 {
1525 if ((0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusAConn))) ||
1526 (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusBusReq))))
1527 {
1528 _USB_OtgEnterStateBPeripheral(otgInstance);
1529 }
1530 else if (0U == (otgInstance->otgControllerStatus & ((uint32_t)kOtg_StatusVbusVld)))
1531 {
1532 _USB_OtgEnterStateBPeripheral(otgInstance);
1533 }
1534 else
1535 {
1536 otgInstance->otgDeviceState = (uint8_t)kOtg_State_BHost;
1537 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBusReq);
1538 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBHNPFeature;
1539
1540 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
1541 (uint32_t)kOtg_ControlPullDown,
1542 ((uint32_t)kOtg_PullDp) | ((uint32_t)kOtg_PullDm), 0);
1543
1544 (void)otgInstance->controllerInterface->controllerControl(
1545 otgInstance->controllerHandle, (uint32_t)kOtg_ControlRequestStatus, (uint32_t)kOtg_StatusADisconn,
1546 (uint32_t)kOtg_State_BHost);
1547
1548 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStateChange, kOtg_State_BHost);
1549
1550 #if (USB_OTG_TIME_WAIT_DEVICE_INIT != 0U)
1551 otgInstance->waitInit = 1U;
1552 _USB_OtgStartTimer(otgInstance, USB_OTG_TIME_WAIT_DEVICE_INIT);
1553 #else
1554 /* start work as host */
1555 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStackInit, kOtg_StackHostInit);
1556 #endif
1557 }
1558 }
1559
_USB_OtgProcessStateBHost(usb_otg_instance_t * otgInstance,uint32_t otgChangeType,uint32_t changeValue)1560 static void _USB_OtgProcessStateBHost(usb_otg_instance_t *otgInstance, uint32_t otgChangeType, uint32_t changeValue)
1561 {
1562 usb_otg_status_type_t statusType = (usb_otg_status_type_t)otgChangeType;
1563 switch (statusType)
1564 {
1565 #if (USB_OTG_TIME_WAIT_DEVICE_INIT != 0U)
1566 case kOtg_StatusTimeOut:
1567 if (0U != otgInstance->waitInit) /* wait device init */
1568 {
1569 /* start work as host */
1570 otgInstance->waitInit = 0;
1571 otgInstance->otgCallback(otgInstance->otgCallbackParameter, kOtg_EventStackInit,
1572 kOtg_StackHostInit); /* host stack init */
1573 }
1574 break;
1575 #endif
1576
1577 case kOtg_StatusBusReq:
1578 if (0U == changeValue) /* B release bus */
1579 {
1580 /* todo: update controller state */
1581 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBusReq);
1582
1583 _USB_OtgExitHost(otgInstance);
1584 _USB_OtgEnterStateBPeripheral(otgInstance); /* go to b_peripheral */
1585 }
1586 else /* A request bus */
1587 {
1588 /* todo: update controller state */
1589
1590 _USB_OtgExitHost(otgInstance);
1591 _USB_OtgEnterStateBPeripheral(otgInstance); /* go to b_peripheral */
1592 }
1593 break;
1594
1595 case kOtg_StatusAConn:
1596 if (0U == changeValue)
1597 {
1598 /* todo: update controller state */
1599 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusAConn);
1600
1601 _USB_OtgExitHost(otgInstance);
1602 _USB_OtgEnterStateBPeripheral(otgInstance); /* go to b_peripheral */
1603 }
1604 break;
1605
1606 case kOtg_StatusVbusVld:
1607 if (0U == changeValue)
1608 {
1609 /* todo: update controller state */
1610 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusVbusVld);
1611
1612 _USB_OtgExitHost(otgInstance);
1613 _USB_OtgEnterStateBPeripheral(otgInstance); /* go to b_peripheral */
1614 }
1615 break;
1616
1617 default:
1618 if (0U != changeValue)
1619 {
1620 otgInstance->otgControllerStatus |= (uint32_t)otgChangeType;
1621 }
1622 else
1623 {
1624 otgInstance->otgControllerStatus &= ~((uint32_t)otgChangeType);
1625 }
1626 break;
1627 }
1628 }
1629
USB_OtgInit(uint8_t controllerId,usb_otg_handle * otgHandle,usb_otg_callback_t otgCallbackFn,void * callbackParameter)1630 usb_status_t USB_OtgInit(uint8_t controllerId,
1631 usb_otg_handle *otgHandle,
1632 usb_otg_callback_t otgCallbackFn,
1633 void *callbackParameter)
1634 {
1635 usb_otg_instance_t *otgInstance = NULL;
1636
1637 if (otgHandle == NULL)
1638 {
1639 return kStatus_USB_InvalidHandle;
1640 }
1641
1642 /* 1. initialize otg stack */
1643 otgInstance = (usb_otg_instance_t *)OSA_MemoryAllocate(sizeof(usb_otg_instance_t));
1644 if (otgInstance == NULL)
1645 {
1646 return kStatus_USB_AllocFail;
1647 }
1648 /* initialize msg queue */
1649 otgInstance->otgMsgHandle = (osa_msgq_handle_t)&otgInstance->otgMsgHandleBuffer[0];
1650 if (KOSA_StatusSuccess != OSA_MsgQCreate(otgInstance->otgMsgHandle, USB_OTG_MSG_COUNT, USB_OTG_MESSAGES_SIZE))
1651 {
1652 OSA_MemoryFree(otgInstance);
1653 return kStatus_USB_Error;
1654 }
1655 /* otg instance structure filed initialization */
1656 otgInstance->otgControllerStatus = 0U; /* default controller status */
1657 otgInstance->otgCallback = otgCallbackFn;
1658 otgInstance->otgCallbackParameter = callbackParameter;
1659 otgInstance->hasUpdateMsg = 0U;
1660
1661 /* 2. initialize controller */
1662 _USB_OtgGetControllerInterface(controllerId, &otgInstance->controllerInterface);
1663 if ((otgInstance->controllerInterface == NULL) || (otgInstance->controllerInterface->controllerInit == NULL) ||
1664 (otgInstance->controllerInterface->controllerDeinit == NULL) ||
1665 (otgInstance->controllerInterface->controllerControl == NULL))
1666 {
1667 (void)OSA_MsgQDestroy(otgInstance->otgMsgHandle);
1668 OSA_MemoryFree(otgInstance);
1669 return kStatus_USB_Error;
1670 }
1671 if (otgInstance->controllerInterface->controllerInit(controllerId, otgInstance, &otgInstance->controllerHandle) !=
1672 kStatus_USB_Success)
1673 {
1674 (void)OSA_MsgQDestroy(otgInstance->otgMsgHandle);
1675 OSA_MemoryFree(otgInstance);
1676 return kStatus_USB_Error;
1677 }
1678
1679 _USB_OtgEnterStateStart(otgInstance);
1680
1681 *otgHandle = otgInstance;
1682 return kStatus_USB_Success;
1683 }
1684
USB_OtgDeinit(usb_otg_handle otgHandle)1685 usb_status_t USB_OtgDeinit(usb_otg_handle otgHandle)
1686 {
1687 usb_otg_instance_t *otgInstance = (usb_otg_instance_t *)otgHandle;
1688
1689 if (otgHandle == NULL)
1690 {
1691 return kStatus_USB_InvalidHandle;
1692 }
1693
1694 /* 1. de-initialize controller */
1695 (void)otgInstance->controllerInterface->controllerDeinit(otgInstance->controllerHandle);
1696
1697 /* 2. de-initialize otg stack */
1698 (void)OSA_MsgQDestroy(otgInstance->otgMsgHandle);
1699 OSA_MemoryFree(otgInstance);
1700
1701 return kStatus_USB_Success;
1702 }
1703
USB_OtgTaskFunction(usb_otg_handle otgHandle)1704 void USB_OtgTaskFunction(usb_otg_handle otgHandle)
1705 {
1706 usb_otg_instance_t *otgInstance = (usb_otg_instance_t *)otgHandle;
1707 usb_otg_msg_t otgMsg;
1708 usb_otg_device_state_t deviceState;
1709
1710 if (otgHandle == NULL)
1711 {
1712 return;
1713 }
1714
1715 /* wait forever for one message */
1716 if (OSA_MsgQGet(otgInstance->otgMsgHandle, (osa_msg_handle_t)&otgMsg, USB_OSA_WAIT_TIMEOUT) == KOSA_StatusSuccess)
1717 {
1718 if (otgMsg.otgStatusType == (uint32_t)kOtg_StatusChange)
1719 {
1720 otgInstance->hasUpdateMsg = 0;
1721 (void)otgInstance->controllerInterface->controllerControl(otgInstance->controllerHandle,
1722 kOtg_ControlUpdateStatus, 0, 0);
1723 }
1724 else
1725 {
1726 if (otgMsg.otgStatusType == (uint32_t)kOtg_StatusTimeOut)
1727 {
1728 otgInstance->hasTimeOutMsg--;
1729 if (0U != otgInstance->cancelTime)
1730 {
1731 otgInstance->cancelTime = 0;
1732 return;
1733 }
1734 }
1735
1736 deviceState = (usb_otg_device_state_t)otgInstance->otgDeviceState;
1737 switch (deviceState)
1738 {
1739 case kOtg_State_Start:
1740 _USB_OtgProcessStateStart(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1741 break;
1742
1743 case kOtg_State_AIdle:
1744 _USB_OtgProcessStateAIdle(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1745 break;
1746
1747 case kOtg_State_AWaitVrise:
1748 _USB_OtgProcessStateAWaitVrise(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1749 break;
1750
1751 case kOtg_State_AWaitBcon:
1752 _USB_OtgProcessStateAWaitBcon(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1753 break;
1754
1755 case kOtg_State_AHost:
1756 _USB_OtgProcessStateAHost(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1757 break;
1758
1759 case kOtg_State_AWaitVfall:
1760 _USB_OtgProcessStateAWaitVfall(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1761 break;
1762
1763 case kOtg_State_ASuspend:
1764 _USB_OtgProcessStateASuspend(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1765 break;
1766
1767 case kOtg_State_APeripheral:
1768 _USB_OtgProcessStateAPeripheral(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1769 break;
1770
1771 case kOtg_State_AVbusErr:
1772 _USB_OtgProcessStateAVbusErr(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1773 break;
1774
1775 case kOtg_State_BIdleEh:
1776 /* The device is OTG device */
1777 break;
1778
1779 case kOtg_State_BIdle:
1780 _USB_OtgProcessStateBIdle(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1781 break;
1782
1783 case kOtg_State_BSrpInit:
1784 _USB_OtgProcessStateBSrpInit(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1785 break;
1786
1787 case kOtg_State_BPeripheral:
1788 _USB_OtgProcessStateBPeripheral(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1789 break;
1790
1791 case kOtg_State_BWaitAcon:
1792 _USB_OtgProcessStateBWaitAcon(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1793 break;
1794
1795 case kOtg_State_BHost:
1796 _USB_OtgProcessStateBHost(otgInstance, otgMsg.otgStatusType, otgMsg.otgStatusValue);
1797 break;
1798
1799 default:
1800 /*no action*/
1801 break;
1802 }
1803 }
1804 }
1805 }
1806
USB_OtgBusDrop(usb_otg_handle otgHandle,uint8_t drop)1807 usb_status_t USB_OtgBusDrop(usb_otg_handle otgHandle, uint8_t drop)
1808 {
1809 usb_otg_instance_t *otgInstance = (usb_otg_instance_t *)otgHandle;
1810
1811 if (otgHandle == NULL)
1812 {
1813 return kStatus_USB_InvalidHandle;
1814 }
1815
1816 if ((otgInstance->otgDeviceState >= (uint8_t)kOtg_State_AIdle) &&
1817 (otgInstance->otgDeviceState <= (uint8_t)kOtg_State_AVbusErr))
1818 {
1819 return USB_OtgNotifyChange(otgHandle, (uint32_t)kOtg_StatusBusDrop, drop);
1820 }
1821 else
1822 {
1823 return kStatus_USB_Error;
1824 }
1825 }
1826
USB_OtgBusRequest(usb_otg_handle otgHandle)1827 usb_status_t USB_OtgBusRequest(usb_otg_handle otgHandle)
1828 {
1829 if (otgHandle == NULL)
1830 {
1831 return kStatus_USB_InvalidHandle;
1832 }
1833
1834 return USB_OtgNotifyChange(otgHandle, (uint32_t)kOtg_StatusBusReq, 1);
1835 }
1836
USB_OtgBusRelease(usb_otg_handle otgHandle)1837 usb_status_t USB_OtgBusRelease(usb_otg_handle otgHandle)
1838 {
1839 if (otgHandle == NULL)
1840 {
1841 return kStatus_USB_InvalidHandle;
1842 }
1843
1844 return USB_OtgNotifyChange(otgHandle, (uint32_t)kOtg_StatusBusReq, 0);
1845 }
1846
USB_OtgClearError(usb_otg_handle otgHandle)1847 usb_status_t USB_OtgClearError(usb_otg_handle otgHandle)
1848 {
1849 usb_otg_instance_t *otgInstance = (usb_otg_instance_t *)otgHandle;
1850
1851 if (otgHandle == NULL)
1852 {
1853 return kStatus_USB_InvalidHandle;
1854 }
1855
1856 if (otgInstance->otgDeviceState == (uint8_t)kOtg_State_AVbusErr)
1857 {
1858 return USB_OtgNotifyChange(otgHandle, (uint32_t)kOtg_StatusClrErr, 1);
1859 }
1860 else
1861 {
1862 return kStatus_USB_Error;
1863 }
1864 }
1865
USB_OtgNotifyChange(usb_otg_handle otgHandle,uint32_t statusType,uint32_t statusValue)1866 usb_status_t USB_OtgNotifyChange(usb_otg_handle otgHandle, uint32_t statusType, uint32_t statusValue)
1867 {
1868 usb_otg_msg_t otgMsg;
1869 usb_otg_instance_t *otgInstance = (usb_otg_instance_t *)otgHandle;
1870
1871 if (otgHandle == NULL)
1872 {
1873 return kStatus_USB_InvalidHandle;
1874 }
1875
1876 if (statusType == (uint32_t)kOtg_StatusTimeOut)
1877 {
1878 otgInstance->hasTimeOutMsg++;
1879 }
1880 else if (statusType == (uint32_t)kOtg_StatusChange)
1881 {
1882 if (otgInstance->hasUpdateMsg == 1U)
1883 {
1884 return kStatus_USB_Success;
1885 }
1886 }
1887 else if (statusType == (uint32_t)kOtg_StatusBHNPFeature)
1888 {
1889 if (0U != statusValue)
1890 {
1891 otgInstance->otgControllerStatus |= (uint32_t)kOtg_StatusBHNPFeature;
1892 }
1893 else
1894 {
1895 otgInstance->otgControllerStatus &= ~((uint32_t)kOtg_StatusBHNPFeature);
1896 }
1897 }
1898 else if (statusType == (uint32_t)kOtg_StatusId)
1899 {
1900 if (statusValue == 0U)
1901 {
1902 otgInstance->idChangeAsFalse = 1U;
1903 }
1904 }
1905 else
1906 {
1907 }
1908
1909 otgMsg.otgStatusType = statusType;
1910 otgMsg.otgStatusValue = statusValue;
1911 if (OSA_MsgQPut(otgInstance->otgMsgHandle, (osa_msg_handle_t)&otgMsg) == KOSA_StatusSuccess)
1912 {
1913 if (statusType == (uint32_t)kOtg_StatusChange)
1914 {
1915 otgInstance->hasUpdateMsg = 1;
1916 }
1917 return kStatus_USB_Success;
1918 }
1919
1920 return kStatus_USB_Error;
1921 }
1922