1 /*
2 * SPDX-FileCopyrightText: 2016 STMicroelectronics
3 * SPDX-FileCopyrightText: 2019-2025 SiFli Technologies(Nanjing) Co., Ltd
4 *
5 * SPDX-License-Identifier: BSD-3-Clause AND Apache-2.0
6 */
7
8 /* Includes ------------------------------------------------------------------*/
9 #include "bf0_hal.h"
10
11 /** @addtogroup BF0_HAL_Driver
12 * @{
13 */
14
15 #ifdef HAL_HCD_MODULE_ENABLED
16
17 #include "bf0_hal_usb_common.h"
18
19
20 /** @defgroup HCD HCD
21 * @brief HCD HAL module driver
22 * @{
23 */
24 #ifndef SF32LB55X
25 #define USB_TX_DMA_ENABLED 1
26 //#define USB_RX_DMA_ENABLED 1
27 #define USB_RX_CONT_DMA_ENABLED 1
28 #endif /*SF32LB55X*/
29 #define USB_DATA_DEBUG 0
30 #if USB_DATA_DEBUG
31 #include "rtthread.h"
32 #endif /*USB_DATA_DEBUG*/
33
34 static uint8_t tx_packet_max = 0, rx_packet_max = 0;
35
36 /* Private typedef -----------------------------------------------------------*/
37 /* Private define ------------------------------------------------------------*/
38 /* Private macro -------------------------------------------------------------*/
39 /* Private variables ---------------------------------------------------------*/
40 /* Private function prototypes -----------------------------------------------*/
41 /** @defgroup HCD_Private_Functions HCD Private Functions
42 * @{
43 */
44 static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum);
45 static void HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum);
46 static void HCD_HC_EP0_IRQHandler(HCD_HandleTypeDef *hhcd);
47 #if 0
48 static void HCD_RXQLVL_IRQHandler(HCD_HandleTypeDef *hhcd);
49 #endif
50 /**
51 * @}
52 */
53
54 /* Exported functions --------------------------------------------------------*/
55 /** @defgroup HCD_Exported_Functions HCD Exported Functions
56 * @{
57 */
58
59 /** @defgroup HCD_Exported_Functions_Group1 Initialization and de-initialization functions
60 * @brief Initialization and Configuration functions
61 *
62 @verbatim
63 ===============================================================================
64 ##### Initialization and de-initialization functions #####
65 ===============================================================================
66 [..] This section provides functions allowing to:
67
68 @endverbatim
69 * @{
70 */
71
__HAL_HCD_ENABLE(HCD_HandleTypeDef * hhcd)72 void __HAL_HCD_ENABLE(HCD_HandleTypeDef *hhcd)
73 {
74 HCD_TypeDef *mbase = hhcd->Instance;
75
76 #if defined(SF32LB52X) || defined(SF32LB56X)
77 uint16_t irq = 0x00E1;
78 mbase->intrtxe = irq;
79 mbase->intrrxe = 0x001E;
80 #else
81 mbase->intrtxe = hhcd->epmask;
82 mbase->intrrxe = hhcd->epmask & 0xfffe;
83 #endif /*defined(SF32LB52X) || defined(SF32LB56X)*/
84
85 mbase->intrusbe = 0xf7;
86 }
87
__HAL_HCD_DISABLE(HCD_HandleTypeDef * hhcd)88 void __HAL_HCD_DISABLE(HCD_HandleTypeDef *hhcd)
89 {
90 HCD_TypeDef *mbase = hhcd->Instance;
91
92 /* disable interrupts */
93 mbase->intrusbe = 0x10; // Keep connect enable
94 mbase->intrtxe = 0;
95 mbase->intrrxe = 0;
96
97 /* flush pending interrupts, w1c */
98 mbase->intrusb = mbase->intrusb;
99 mbase->intrtx = mbase->intrtx;
100 mbase->intrrx = mbase->intrrx;
101 }
102
ep_2_ch(HCD_HandleTypeDef * hhcd,int ep_num)103 static int ep_2_ch(HCD_HandleTypeDef *hhcd, int ep_num)
104 {
105 int chnum;
106
107 for (chnum = 0; chnum < 16; chnum++)
108 {
109 if (((ep_num & USB_DIR_IN) ? 1 : 0) != hhcd->hc[chnum].ep_is_in)
110 continue;
111 if (hhcd->hc[chnum].ep_num == (ep_num & 0x7f) && hhcd->hc[chnum].max_packet > 0)
112 {
113 ep_num = hhcd->hc[chnum].ep_num;
114 break;
115 }
116 }
117
118 if (chnum == 16)
119 {
120 chnum = -1;
121 }
122 return chnum;
123 }
124
125 /**
126 * @brief Initialize the host driver.
127 * @param hhcd HCD handle
128 * @retval HAL status
129 */
HAL_HCD_Init(HCD_HandleTypeDef * hhcd)130 HAL_StatusTypeDef HAL_HCD_Init(HCD_HandleTypeDef *hhcd)
131 {
132 USB_OTG_GlobalTypeDef *USBx;
133
134 /* Check the HCD handle allocation */
135 if (hhcd == NULL)
136 {
137 return HAL_ERROR;
138 }
139
140 /* Check the parameters */
141
142 USBx = hhcd->Instance;
143
144 if (hhcd->State == HAL_HCD_STATE_RESET)
145 {
146 /* Allocate lock resource and initialize it */
147 hhcd->Lock = HAL_UNLOCKED;
148
149 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
150 hhcd->SOFCallback = HAL_HCD_SOF_Callback;
151 hhcd->ConnectCallback = HAL_HCD_Connect_Callback;
152 hhcd->DisconnectCallback = HAL_HCD_Disconnect_Callback;
153 hhcd->PortEnabledCallback = HAL_HCD_PortEnabled_Callback;
154 hhcd->PortDisabledCallback = HAL_HCD_PortDisabled_Callback;
155 hhcd->HC_NotifyURBChangeCallback = HAL_HCD_HC_NotifyURBChange_Callback;
156
157 if (hhcd->MspInitCallback == NULL)
158 {
159 hhcd->MspInitCallback = HAL_HCD_MspInit;
160 }
161
162 /* Init the low level hardware */
163 hhcd->MspInitCallback(hhcd);
164 #else
165 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
166 HAL_HCD_MspInit(hhcd);
167 #endif /* (USE_HAL_HCD_REGISTER_CALLBACKS) */
168 }
169
170 hhcd->State = HAL_HCD_STATE_BUSY;
171 __HAL_HCD_DISABLE(hhcd);
172
173 hhcd->State = HAL_HCD_STATE_READY;
174
175 return HAL_OK;
176 }
177
178 /**
179 * @brief Initialize a host channel.
180 * @param hhcd HCD handle
181 * @param ch_num Channel number.
182 * This parameter can be a value from 1 to 15
183 * @param epnum Endpoint number.
184 * This parameter can be a value from 1 to 15
185 * @param dev_address Current device address
186 * This parameter can be a value from 0 to 255
187 * @param speed Current device speed.
188 * This parameter can be one of these values:
189 * HCD_DEVICE_SPEED_HIGH: High speed mode,
190 * HCD_DEVICE_SPEED_FULL: Full speed mode,
191 * HCD_DEVICE_SPEED_LOW: Low speed mode
192 * @param ep_type Endpoint Type.
193 * This parameter can be one of these values:
194 * EP_TYPE_CTRL: Control type,
195 * EP_TYPE_ISOC: Isochronous type,
196 * EP_TYPE_BULK: Bulk type,
197 * EP_TYPE_INTR: Interrupt type
198 * @param mps Max Packet Size.
199 * This parameter can be a value from 0 to32K
200 * @retval HAL status
201 */
HAL_HCD_HC_Init(HCD_HandleTypeDef * hhcd,uint8_t ch_num,uint8_t epnum,uint8_t dev_address,uint8_t speed,uint8_t ep_type,uint16_t mps)202 HAL_StatusTypeDef HAL_HCD_HC_Init(HCD_HandleTypeDef *hhcd,
203 uint8_t ch_num,
204 uint8_t epnum,
205 uint8_t dev_address,
206 uint8_t speed,
207 uint8_t ep_type,
208 uint16_t mps)
209 {
210 HAL_StatusTypeDef status;
211
212 __HAL_LOCK(hhcd);
213 hhcd->hc[ch_num].do_ping = 0U;
214 hhcd->hc[ch_num].dev_addr = dev_address;
215 hhcd->hc[ch_num].max_packet = mps;
216 hhcd->hc[ch_num].ch_num = ch_num;
217 hhcd->hc[ch_num].ep_type = ep_type;
218 hhcd->hc[ch_num].ep_num = epnum & 0x7FU;
219
220 if ((epnum & 0x80U) == 0x80U)
221 {
222 hhcd->hc[ch_num].ep_is_in = 1U;//rx
223 }
224 else
225 {
226 hhcd->hc[ch_num].ep_is_in = 0U;//tx
227 }
228
229 hhcd->hc[ch_num].speed = speed;
230 hhcd->epmask |= (1 << hhcd->hc[ch_num].ep_num);
231 __HAL_HCD_ENABLE(hhcd);
232
233 __HAL_UNLOCK(hhcd);
234
235 return status;
236 }
237
238 /**
239 * @brief Halt a host channel.
240 * @param hhcd HCD handle
241 * @param ch_num Channel number.
242 * This parameter can be a value from 1 to 15
243 * @retval HAL status
244 */
HAL_HCD_HC_Halt(HCD_HandleTypeDef * hhcd,uint8_t ch_num)245 HAL_StatusTypeDef HAL_HCD_HC_Halt(HCD_HandleTypeDef *hhcd, uint8_t ch_num)
246 {
247 HAL_StatusTypeDef status = HAL_OK;
248 HCD_TypeDef *mbase = hhcd->Instance;
249
250 __HAL_LOCK(hhcd);
251 #if defined(SF32LB52X) || defined(SF32LB56X)
252 int ep_num = (hhcd->hc[ch_num].ep_num & 0x00E3);
253 #else
254 int ep_num = (hhcd->hc[ch_num].ep_num & 0x7f);
255 #endif/*defined(SF32LB52X) || defined(SF32LB56X) */
256
257
258 hhcd->epmask &= ~(1 << ep_num);
259
260
261 /* disable interrupts */
262 mbase->intrtxe &= ~(1 << ep_num);
263 mbase->intrrxe &= ~(1 << ep_num);
264
265 /* flush pending interrupts, w1c */
266 mbase->intrtx = (1 << ep_num);
267 mbase->intrrx = (1 << ep_num);
268
269 __HAL_UNLOCK(hhcd);
270
271 return status;
272 }
273
274 /**
275 * @brief DeInitialize the host driver.
276 * @param hhcd HCD handle
277 * @retval HAL status
278 */
HAL_HCD_DeInit(HCD_HandleTypeDef * hhcd)279 HAL_StatusTypeDef HAL_HCD_DeInit(HCD_HandleTypeDef *hhcd)
280 {
281 /* Check the HCD handle allocation */
282 if (hhcd == NULL)
283 {
284 return HAL_ERROR;
285 }
286
287 hhcd->State = HAL_HCD_STATE_BUSY;
288
289 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
290 if (hhcd->MspDeInitCallback == NULL)
291 {
292 hhcd->MspDeInitCallback = HAL_HCD_MspDeInit; /* Legacy weak MspDeInit */
293 }
294
295 /* DeInit the low level hardware */
296 hhcd->MspDeInitCallback(hhcd);
297 #else
298 /* DeInit the low level hardware: CLOCK, NVIC.*/
299 HAL_HCD_MspDeInit(hhcd);
300 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
301
302 __HAL_HCD_DISABLE(hhcd);
303
304 hhcd->State = HAL_HCD_STATE_RESET;
305
306 return HAL_OK;
307 }
308
309 /**
310 * @brief Initialize the HCD MSP.
311 * @param hhcd HCD handle
312 * @retval None
313 */
HAL_HCD_MspInit(HCD_HandleTypeDef * hhcd)314 __weak void HAL_HCD_MspInit(HCD_HandleTypeDef *hhcd)
315 {
316 HAL_RCC_EnableModule(RCC_MOD_USBC);
317 #ifdef SF32LB58X
318 //hwp_usbc->utmicfg12 = hwp_usbc->utmicfg12 | 0x3; //set xo_clk_sel
319 hwp_usbc->utmicfg23 = 0xd8;
320 hwp_usbc->ldo25 = hwp_usbc->ldo25 | 0xa; //set psw_en and ldo25_en
321 HAL_Delay(1);
322 hwp_usbc->swcntl3 = 0x1; //set utmi_en for USB2.0
323 hwp_usbc->usbcfg = hwp_usbc->usbcfg | 0x40; //enable usb PLL.
324 hwp_usbc->dpbrxdisl = 0xff;
325 hwp_usbc->dpbtxdisl = 0xff;
326 hwp_usbc->utmicfg25 = hwp_usbc->utmicfg25 | 0xc0;
327 #elif defined(SF32LB56X)||defined(SF32LB52X)
328 hwp_hpsys_cfg->USBCR |= HPSYS_CFG_USBCR_DM_PD | HPSYS_CFG_USBCR_DP_EN | HPSYS_CFG_USBCR_USB_EN;
329 #elif defined(SF32LB55X)
330 hwp_hpsys_cfg->USBCR |= HPSYS_CFG_USBCR_DM_PD | HPSYS_CFG_USBCR_USB_EN;
331 #endif /*SF32LB58X*/
332 }
333
334 /**
335 * @brief DeInitialize the HCD MSP.
336 * @param hhcd HCD handle
337 * @retval None
338 */
HAL_HCD_MspDeInit(HCD_HandleTypeDef * hhcd)339 __weak void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hhcd)
340 {
341 #ifdef SF32LB58X
342 hwp_usbc->usbcfg &= ~0x40; // Disable usb PLL.
343 hwp_usbc->swcntl3 = 0x0;
344 hwp_usbc->ldo25 &= ~0xa; // Disable psw_en and ldo25_en
345 #elif defined(SF32LB56X)||defined(SF32LB52X)
346 hwp_hpsys_cfg->USBCR &= ~(HPSYS_CFG_USBCR_DM_PD | HPSYS_CFG_USBCR_DP_EN | HPSYS_CFG_USBCR_USB_EN);
347 #elif defined(SF32LB55X)
348 hwp_hpsys_cfg->USBCR &= ~(HPSYS_CFG_USBCR_DM_PD | HPSYS_CFG_USBCR_USB_EN);
349 #endif/*SF32LB58X*/
350 HAL_RCC_DisableModule(RCC_MOD_USBC);
351 }
352
353 /**
354 * @}
355 */
356
357 /** @defgroup HCD_Exported_Functions_Group2 Input and Output operation functions
358 * @brief HCD IO operation functions
359 *
360 @verbatim
361 ===============================================================================
362 ##### IO operation functions #####
363 ===============================================================================
364 [..] This subsection provides a set of functions allowing to manage the USB Host Data
365 Transfer
366
367 @endverbatim
368 * @{
369 */
370
371 /**
372 * @brief Submit a new URB for processing.
373 * @param hhcd HCD handle
374 * @param ch_num Channel number.
375 * This parameter can be a value from 1 to 15
376 * @param direction Channel number.
377 * This parameter can be one of these values:
378 * 0 : Output / 1 : Input
379 * @param ep_type Endpoint Type.
380 * This parameter can be one of these values:
381 * EP_TYPE_CTRL: Control type/
382 * EP_TYPE_ISOC: Isochronous type/
383 * EP_TYPE_BULK: Bulk type/
384 * EP_TYPE_INTR: Interrupt type/
385 * @param token Endpoint Type.
386 * This parameter can be one of these values:
387 * 0: HC_PID_SETUP / 1: HC_PID_DATA1
388 * @param pbuff pointer to URB data
389 * @param length Length of URB data
390 * @param do_ping activate do ping protocol (for high speed only).
391 * This parameter can be one of these values:
392 * 0 : do ping inactive / 1 : do ping active
393 * @retval HAL status
394 */
395
HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef * hhcd,uint8_t ch_num,uint8_t direction,uint8_t ep_type,uint8_t token,uint8_t * pbuff,uint16_t length,uint8_t do_ping)396 HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
397 uint8_t ch_num,
398 uint8_t direction,
399 uint8_t ep_type,
400 uint8_t token,
401 uint8_t *pbuff,
402 uint16_t length,
403 uint8_t do_ping)
404 {
405 HCD_TypeDef *mbase = hhcd->Instance;
406 uint8_t ep_num = hhcd->hc[ch_num].ep_num;
407
408 hhcd->hc[ch_num].ep_is_in = direction;
409 hhcd->hc[ch_num].ep_type = ep_type;
410
411 mbase->faddr = hhcd->hc[ch_num].dev_addr;
412 #ifdef SF32LB58X
413 hhcd->Instance->swcntl1 = 0x40;
414 #endif
415 if (direction == 0) // TX
416 {
417 if (ep_num == 0)
418 {
419 int i;
420 uint16_t csr;
421 __IO uint8_t *fifox = (__IO uint8_t *) & (hhcd->Instance->fifox[ep_num]);
422 __IO struct musb_ep0_regs *ep0 = &(hhcd->Instance->ep[ep_num].ep0);
423
424 //ep0->csr0 = USB_CSR0_FLUSHFIFO;
425 //HAL_DBG_printf("tx ep0: fifox=%p, length=%d\n", fifox, length);
426 for (i = 0; i < length; i++) // REVISIT: Use 16bits/32bits FIFO to speed up
427 *fifox = *(pbuff + i);
428 #if USB_DATA_DEBUG
429 rt_kprintf("%s %d TX ep=%d usb chnum=%d tx\n", __func__, __LINE__, ep_num, ch_num);
430 for (i = 0; i < length; i++)
431 rt_kprintf("%x ", pbuff[i]);
432 rt_kprintf("\n");
433 #endif/*USB_DATA_DEBUG */
434 csr = USB_CSR0_TXPKTRDY;
435 //HAL_DBG_print_data((char *)pBuf, 0, len);
436
437 if (token == USB_PID_SETUP)
438 {
439 hhcd->ep0_state = HAL_HCD_EP0_SETUP;
440 csr |= USB_CSR0_H_SETUPPKT;
441 }
442 else
443 {
444 if (length == 0)
445 csr |= USB_CSR0_H_STATUSPKT;
446 hhcd->ep0_state = HAL_HCD_EP0_TX;
447 }
448 ep0->host_naklimit0 = (HAL_HCD_GetCurrentSpeed(hhcd) == HCD_SPEED_HIGH ? 8 : 4);
449 //HAL_DBG_printf("tx ep0: csr=0x%x, token=%x, %d, %d\n", csr, token, length,ep0->host_naklimit0);
450 hhcd->hc[ch_num].xfer_count = length;
451 ep0->csr0 = csr;
452 }
453 else
454 {
455 int i;
456 uint16_t csr;
457 #if USB_DATA_DEBUG
458 //pbuff = 0x20004ac4;
459 rt_kprintf("%s %d TX ep=%d usb chnum=%d tx pbuff=0x%p\n", __func__, __LINE__, ep_num, ch_num, pbuff);
460 for (i = 0; i < length; i++)
461 rt_kprintf("%x ", pbuff[i]);
462 rt_kprintf("\n");
463 #endif /*USB_DATA_DEBUG */
464 #if defined(SF32LB52X) || defined(SF32LB56X)
465 __IO struct musb_epN_regs *epn = &(hhcd->Instance->ep[ch_num].epN);
466 #else
467 __IO struct musb_epN_regs *epn = &(hhcd->Instance->ep[ep_num].epN);
468 #endif /*defined(SF32LB52X) || defined(SF32LB56X)*/
469 uint8_t num = ch_num;
470 epn->txcsr = USB_TXCSR_FLUSHFIFO;
471 csr = epn->txcsr;
472 csr &= ~(USB_TXCSR_H_NAKTIMEOUT
473 | USB_TXCSR_AUTOSET
474 | USB_TXCSR_DMAENAB
475 | USB_TXCSR_FRCDATATOG
476 | USB_TXCSR_H_RXSTALL
477 | USB_TXCSR_H_ERROR
478 | USB_TXCSR_TXPKTRDY);
479 csr |= USB_TXCSR_MODE;
480
481 #ifdef USB_TX_DMA_ENABLED
482 if (IS_DCACHED_RAM((uint32_t)pbuff))
483 mpu_dcache_clean((void *)pbuff, length);
484 csr |= USB_TXCSR_DMAENAB | USB_TXCSR_AUTOSET;
485 epn->txcsr = csr;
486 epn->txmaxp = hhcd->hc[num].max_packet;
487 //epn->txinterval = (HAL_HCD_GetCurrentSpeed(hhcd) == HCD_SPEED_HIGH ? 16 : 2);
488 #if defined(SF32LB52X) || defined(SF32LB56X)
489 epn->txtype = (hhcd->hc[ep_num].ep_type << 4) + ep_num;
490 #else
491 epn->txtype = (hhcd->hc[ch_num].ep_type << 4) + ep_num;
492 #endif /*defined(SF32LB52X) || defined(SF32LB56X)*/
493 struct musb_dma_regs *dma = (struct musb_dma_regs *) & (hhcd->Instance->dma[num]);
494 dma->addr = (REG32)pbuff;
495 dma->count = length;
496 dma->cntl = (1 << USB_DMACTRL_TRANSMIT_SHIFT) |
497 #if defined(SF32LB52X) || defined(SF32LB56X)
498 ((num) << USB_DMACTRL_ENDPOINT_SHIFT) |
499 #else
500 ((ep_num) << USB_DMACTRL_ENDPOINT_SHIFT) |
501 #endif /*defined(SF32LB52X) || defined(SF32LB56X) */
502 (1 << USB_DMACTRL_IRQENABLE_SHIFT) | USB_DMACTRL_BURSTMODE;
503 if (length / hhcd->hc[num].max_packet)
504 {
505 dma->cntl |= (1 << USB_DMACTRL_ENABLE_SHIFT) | (1 << USB_DMACTRL_MODE1_SHIFT);
506 tx_packet_max = 1;
507 }
508 else
509 {
510 dma->cntl |= (1 << USB_DMACTRL_ENABLE_SHIFT) | (0 << USB_DMACTRL_MODE1_SHIFT);
511 tx_packet_max = 0;
512 }
513 #if USB_DATA_DEBUG
514 rt_kprintf("%s %d dma usb num=%d tx\n", __func__, __LINE__, num);
515 #endif /*USB_DATA_DEBUG */
516
517 #else /*no dma*/
518
519 #if defined(SF32LB52X) || defined(SF32LB56X)
520 __IO uint8_t *fifox = (__IO uint8_t *) & (hhcd->Instance->fifox[ch_num]);
521 #else
522 __IO uint8_t *fifox = (__IO uint8_t *) & (hhcd->Instance->fifox[ep_num]);
523 #endif /*defined(SF32LB52X) || defined(SF32LB56X) */
524 for (i = 0; i < length; i++) // REVISIT: Use 16bits/32bits FIFO to speed up
525 *fifox = *(pbuff + i);
526 #if USB_DATA_DEBUG
527 rt_kprintf("%s %d TX ep=%d usb chnum=%d tx num=%d ep_type=%d\n", __func__, __LINE__, ep_num, ch_num, num, hhcd->hc[num].ep_type);
528 for (i = 0; i < length; i++)
529 rt_kprintf("%x ", pbuff[i]);
530 rt_kprintf("\n");
531 #endif /*USB_DATA_DEBUG */
532 csr |= USB_TXCSR_AUTOSET;
533 csr |= USB_TXCSR_TXPKTRDY;
534
535 epn->txmaxp = hhcd->hc[ch_num].max_packet;
536 epn->txtype = (hhcd->hc[ch_num].ep_type << 4) + ep_num;
537
538 epn->txinterval = (HAL_HCD_GetCurrentSpeed(hhcd) == HCD_SPEED_HIGH ? 16 : 2);
539 epn->txcsr = csr;
540
541 #endif
542 #ifdef SF32LB58X
543 hhcd->Instance->swcntl1 = 0x0;
544 #endif
545 }
546 }
547 else//RX
548 {
549 uint16_t csr;
550 uint16_t csr_tx;
551
552 ep_num &= 0x7f;
553 hhcd->hc[ch_num].xfer_buff = pbuff;
554 if (ep_num == 0)
555 {
556 __IO struct musb_ep0_regs *ep0 = &(hhcd->Instance->ep[ep_num].ep0);
557 csr = ep0->csr0;
558 csr |= USB_CSR0_H_REQPKT;
559 ep0->csr0 = csr;
560 hhcd->ep0_state = HAL_HCD_EP0_RX;
561 }
562 else
563 {
564 #if defined(SF32LB52X) || defined(SF32LB56X)
565 __IO struct musb_epN_regs *epn = &(hhcd->Instance->ep[ch_num].epN);
566 #ifndef SF32LB55X
567 struct musb_rqpktcount *count_t = (struct musb_rqpktcount *) & (hhcd->Instance->rqpktcount[ch_num - 1]);
568 #endif
569 #else
570 __IO struct musb_epN_regs *epn = &(hhcd->Instance->ep[ep_num].epN);
571 #ifndef SF32LB55X
572 struct musb_rqpktcount *count_t = (struct musb_rqpktcount *) & (hhcd->Instance->rqpktcount[ep_num - 1]);
573 #endif /*SF32LB55X*/
574 #endif /*defined(SF32LB52X) || defined(SF32LB56X)*/
575
576 epn->rxcsr &= ~(USB_RXCSR_H_REQPKT
577 | USB_RXCSR_DMAENAB
578 | USB_RXCSR_AUTOCLEAR
579 | USB_RXCSR_H_AUTOREQ);
580 #if USB_DATA_DEBUG
581 rt_kprintf("%s %d RX ep=%d usb chnum=%d rx pbuff=0x%p\n", __func__, __LINE__, ep_num, ch_num, pbuff);
582 #endif /*USB_DATA_DEBUG */
583 #ifdef USB_RX_CONT_DMA_ENABLED
584 uint32_t packet_num = length / hhcd->hc[ch_num].max_packet;
585 if (packet_num > 1)
586 {
587 #ifndef SF32LB55X
588 count_t->count = packet_num;
589 #endif/*SF32LB55X*/
590 //RT_ASSERT(0);
591 }
592 struct musb_dma_regs *dma = (struct musb_dma_regs *) & (hhcd->Instance->dma[ch_num]);
593 dma->addr = (REG32)pbuff;
594 dma->count = length;
595 hhcd->hc[ch_num].xfer_count = length;
596 dma->cntl = (0 << USB_DMACTRL_TRANSMIT_SHIFT) | //RX
597 #if defined(SF32LB52X) || defined(SF32LB56X)
598 ((ch_num) << USB_DMACTRL_ENDPOINT_SHIFT) |
599 #else
600 ((ep_num) << USB_DMACTRL_ENDPOINT_SHIFT) |
601 #endif/*defined(SF32LB52X) || defined(SF32LB56X) */
602 (1 << USB_DMACTRL_IRQENABLE_SHIFT) | USB_DMACTRL_BURSTMODE;
603 if (packet_num > 1)
604 {
605 dma->cntl |= (1 << USB_DMACTRL_ENABLE_SHIFT) | (1 << USB_DMACTRL_MODE1_SHIFT);
606 rx_packet_max = 1;
607 }
608 else
609 {
610 rx_packet_max = 0;
611 }
612
613 #endif/*USB_RX_CONT_DMA_ENABLED */
614
615 csr = epn->rxcsr;
616 csr_tx = epn->txcsr;
617 csr_tx &= ~USB_TXCSR_MODE;
618
619 /* scrub any stale state, leaving toggle alone */
620 // csr &= USB_RXCSR_DISNYET;
621 csr |= USB_RXCSR_H_REQPKT;
622 #ifdef USB_RX_CONT_DMA_ENABLED
623 if (rx_packet_max)
624 csr |= USB_RXCSR_DMAENAB | USB_RXCSR_AUTOCLEAR | USB_RXCSR_H_AUTOREQ;
625 #endif/*USB_RX_CONT_DMA_ENABLED */
626 epn->rxmaxp = hhcd->hc[ch_num].max_packet;
627 epn->rxtype = (hhcd->hc[ch_num].ep_type << 4) + ep_num;
628 epn->rxinterval = (HAL_HCD_GetCurrentSpeed(hhcd) == HCD_SPEED_HIGH ? 16 : 2);
629 epn->txcsr = csr_tx;
630 epn->rxcsr = csr;
631 //if(test_pcd) RT_ASSERT(0);
632 }
633 #ifdef SF32LB58X
634 hhcd->Instance->swcntl1 = 0x0;
635 #endif
636
637 }
638 return HAL_OK;
639 }
640
641
642
musbh_stage0_irq(HCD_HandleTypeDef * hhcd,uint8_t int_usb)643 static int musbh_stage0_irq(HCD_HandleTypeDef *hhcd, uint8_t int_usb)
644 {
645 HCD_TypeDef *mbase = hhcd->Instance;
646 int r = 0;
647
648 if (int_usb & USB_INTR_RESUME)
649 {
650 }
651
652 if (int_usb & USB_INTR_SUSPEND)
653 {
654 if ((mbase->devctl & USB_DEVCTL_HM) == 0) // A0 only, disconnect HCD will change to device mode.
655 {
656 __HAL_HCD_DISABLE(hhcd);
657 HAL_HCD_Disconnect_Callback(hhcd);
658 }
659
660 }
661
662 /* see manual for the order of the tests */
663 if (int_usb & USB_INTR_SESSREQ)
664 {
665 mbase->devctl = USB_INTR_SESSREQ;
666 HAL_DBG_printf("%s %d,mbase->devctl=%d\n", __func__, __LINE__, mbase->devctl);
667 }
668
669 if (int_usb & USB_INTR_VBUSERROR)
670 {
671 mbase->devctl |= USB_INTR_SESSREQ;
672 HAL_DBG_printf("%s %d,mbase->devctl=%d\n", __func__, __LINE__, mbase->devctl);
673 }
674
675
676 if (int_usb & USB_INTR_CONNECT)
677 {
678 //HAL_Delay_us(5);
679 //mbase->power &= (~USB_POWER_RESET);
680 __HAL_HCD_ENABLE(hhcd);
681 HAL_HCD_Connect_Callback(hhcd);
682 }
683
684 if (int_usb & USB_INTR_DISCONNECT)
685 {
686 __HAL_HCD_DISABLE(hhcd);
687 HAL_HCD_Disconnect_Callback(hhcd);
688 }
689
690 if (int_usb & USB_INTR_RESET)
691 {
692 __HAL_HCD_DISABLE(hhcd);
693 HAL_HCD_Disconnect_Callback(hhcd);
694 }
695
696
697 return r;
698 }
HAL_HCD_Timerout_Callback(HCD_HandleTypeDef * hhcd)699 void HAL_HCD_Timerout_Callback(HCD_HandleTypeDef *hhcd)
700 {
701 #if !defined(SF32LB55X) && !defined(SF32LB52X)
702 HCD_TypeDef *mbase = hhcd->Instance;
703 mbase->rsvd0 = 0x5f;
704 mbase->utmicfg13 = 0x4;
705 mbase->swcntl1 = 0x40;
706 mbase->swcntl2 = 0x03;
707 if (mbase->swcntl2 & 0x80)
708 {
709 mbase->rsvd0 = 0;
710 mbase->utmicfg13 = 0;
711 mbase->swcntl1 = 0;
712 mbase->swcntl2 = 0;
713 mbase->devctl = 0x00;
714 mbase->usbcfg &= 0xf7;
715 HAL_HCD_Disconnect_Callback(hhcd);
716 }
717 else
718 {
719 mbase->rsvd0 = 0;
720 mbase->utmicfg13 = 0;
721 mbase->swcntl1 = 0;
722 mbase->swcntl2 = 0;
723 }
724 #endif/*SF32LB55X && SF32LB52X*/
725 }
726 /**
727 * @brief Handle HCD interrupt request.
728 * @param hhcd HCD handle
729 * @retval None
730 */
HAL_HCD_IRQHandler(HCD_HandleTypeDef * hhcd)731 void HAL_HCD_IRQHandler(HCD_HandleTypeDef *hhcd)
732 {
733 uint32_t reg;
734 int ep_num;
735 uint8_t int_usb = hhcd->Instance->intrusb;
736 uint16_t int_tx = hhcd->Instance->intrtx;
737 uint16_t int_rx = hhcd->Instance->intrrx;
738 uint32_t dmaintr = hhcd->Instance->dmaintr;
739 #if USB_DATA_DEBUG
740 rt_kprintf("USB interrupt usb=%x, tx=%d, rx=%d, usbe=%x, dma_intr=%x\r\n", int_usb, int_tx, int_rx, hhcd->Instance->intrusbe, dmaintr);
741 #endif /*USB_DATA_DEBUG */
742 if (int_usb != USB_INTR_SOF)
743 {
744 musbh_stage0_irq(hhcd, int_usb);
745 }
746
747 if (int_tx & 1)
748 HCD_HC_EP0_IRQHandler(hhcd);
749
750 #if defined(USB_TX_DMA_ENABLED)||defined(USB_RX_DMA_ENABLED) || defined(USB_RX_CONT_DMA_ENABLED)
751 reg = (dmaintr >> 1);
752 ep_num = 1;
753 while (reg)
754 {
755 if (reg & 1)
756 {
757 uint8_t ch_num = hhcd->hc[ep_num].ep_num;
758 #if defined(SF32LB52X) || defined(SF32LB56X)
759 __IO struct musb_epN_regs *epn = &(hhcd->Instance->ep[ep_num].epN);
760
761 #else
762 __IO struct musb_epN_regs *epn = &(hhcd->Instance->ep[ch_num].epN);
763
764 #endif/*defined(SF32LB52X) || defined(SF32LB56X)*/
765 uint16_t csr = epn->txcsr;
766 if (hhcd->hc[ep_num].ep_is_in)
767 {
768 //rx
769 #if USB_DATA_DEBUG
770 rt_kprintf("%d usb chnum=%d RX,epn->rxcount=%d xfer_buff=%p\n", __LINE__, ep_num, hhcd->hc[ep_num].xfer_count, hhcd->hc[ep_num].xfer_buff);
771 for (int i = 0; i < 0x20; i++)
772 rt_kprintf("%x ", hhcd->hc[ep_num].xfer_buff[i]);
773 rt_kprintf("\n");
774 #endif/*USB_DATA_DEBUG */
775 hhcd->hc[ep_num].state = HC_XFRC;
776 hhcd->hc[ep_num].urb_state = URB_DONE;
777 if (0 == rx_packet_max)
778 epn->rxcsr &= (~USB_RXCSR_RXPKTRDY);//
779 epn->rxcsr &= ~(USB_RXCSR_H_REQPKT
780 | USB_RXCSR_DMAENAB
781 | USB_RXCSR_AUTOCLEAR
782 | USB_RXCSR_H_AUTOREQ);
783 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ep_num, hhcd->hc[ep_num].urb_state);
784 }
785 else
786 {
787 //tx
788
789 epn->txmaxp = hhcd->hc[ep_num].max_packet;
790 if (0 == tx_packet_max)
791 {
792 csr |= USB_TXCSR_AUTOSET;
793 csr |= USB_TXCSR_TXPKTRDY;
794
795 #if USB_DATA_DEBUG
796 rt_kprintf("11111111111111 ch_num=%d,ep_num=%d dmaintr=%x,ep_is_in=%d csr=0x%x\n", ep_num, ch_num, dmaintr, hhcd->hc[ep_num].ep_is_in, csr);
797 #endif/*USB_DATA_DEBUG */
798 epn->txcsr = csr;
799 }
800 else if (!(epn->txcsr & USB_TXCSR_TXPKTRDY))
801 {
802 HCD_HC_OUT_IRQHandler(hhcd, ep_num);
803 }
804 else
805 hhcd->hc[ep_2_ch(hhcd, ep_num)].max_packet_unfinish = 1;
806
807 }
808
809
810 }
811 ep_num++;
812 reg >>= 1;
813 }
814 #endif/*defined(USB_TX_DMA_ENABLED)||defined(USB_RX_DMA_ENABLED) || defined(USB_RX_CONT_DMA_ENABLED) */
815
816 if (int_tx & 0xFFFE)
817 {
818 int_tx >>= 1;
819 ep_num = 1;
820 while (int_tx)
821 {
822 if (int_tx & 1)
823 {
824 if ((0 == tx_packet_max) ||
825 (hhcd->hc[ep_2_ch(hhcd, ep_num)].max_packet_unfinish == 1))
826 {
827 if (hhcd->hc[ep_2_ch(hhcd, ep_num)].max_packet_unfinish == 1)
828 hhcd->hc[ep_2_ch(hhcd, ep_num)].max_packet_unfinish = 0;
829 HCD_HC_OUT_IRQHandler(hhcd, ep_num);
830 }
831 }
832 int_tx >>= 1;
833 ep_num++;
834 }
835 }
836
837 if (int_rx)
838 {
839 int_rx >>= 1;
840 ep_num = 1;
841 while (int_rx)
842 {
843 if (int_rx & 1)
844 {
845 HCD_HC_IN_IRQHandler(hhcd, ep_num);
846 }
847 int_rx >>= 1;
848 ep_num++;
849 }
850 }
851
852 __DSB();
853 }
854
855
856 /**
857 * @brief Handles HCD Wakeup interrupt request.
858 * @param hhcd HCD handle
859 * @retval HAL status
860 */
HAL_HCD_WKUP_IRQHandler(HCD_HandleTypeDef * hhcd)861 void HAL_HCD_WKUP_IRQHandler(HCD_HandleTypeDef *hhcd)
862 {
863 UNUSED(hhcd);
864 }
865
866
867 /**
868 * @brief SOF callback.
869 * @param hhcd HCD handle
870 * @retval None
871 */
HAL_HCD_SOF_Callback(HCD_HandleTypeDef * hhcd)872 __weak void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd)
873 {
874 /* Prevent unused argument(s) compilation warning */
875 UNUSED(hhcd);
876
877 /* NOTE : This function should not be modified, when the callback is needed,
878 the HAL_HCD_SOF_Callback could be implemented in the user file
879 */
880 }
881
882 /**
883 * @brief Connection Event callback.
884 * @param hhcd HCD handle
885 * @retval None
886 */
HAL_HCD_Connect_Callback(HCD_HandleTypeDef * hhcd)887 __weak void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
888 {
889 /* Prevent unused argument(s) compilation warning */
890 UNUSED(hhcd);
891
892 /* NOTE : This function should not be modified, when the callback is needed,
893 the HAL_HCD_Connect_Callback could be implemented in the user file
894 */
895 }
896
897 /**
898 * @brief Disconnection Event callback.
899 * @param hhcd HCD handle
900 * @retval None
901 */
HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef * hhcd)902 __weak void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
903 {
904 /* Prevent unused argument(s) compilation warning */
905 UNUSED(hhcd);
906
907 /* NOTE : This function should not be modified, when the callback is needed,
908 the HAL_HCD_Disconnect_Callback could be implemented in the user file
909 */
910 }
911
912 /**
913 * @brief Port Enabled Event callback.
914 * @param hhcd HCD handle
915 * @retval None
916 */
HAL_HCD_PortEnabled_Callback(HCD_HandleTypeDef * hhcd)917 __weak void HAL_HCD_PortEnabled_Callback(HCD_HandleTypeDef *hhcd)
918 {
919 /* Prevent unused argument(s) compilation warning */
920 UNUSED(hhcd);
921
922 /* NOTE : This function should not be modified, when the callback is needed,
923 the HAL_HCD_Disconnect_Callback could be implemented in the user file
924 */
925 }
926
927 /**
928 * @brief Port Disabled Event callback.
929 * @param hhcd HCD handle
930 * @retval None
931 */
HAL_HCD_PortDisabled_Callback(HCD_HandleTypeDef * hhcd)932 __weak void HAL_HCD_PortDisabled_Callback(HCD_HandleTypeDef *hhcd)
933 {
934 /* Prevent unused argument(s) compilation warning */
935 UNUSED(hhcd);
936
937 /* NOTE : This function should not be modified, when the callback is needed,
938 the HAL_HCD_Disconnect_Callback could be implemented in the user file
939 */
940 }
941
942 /**
943 * @brief Notify URB state change callback.
944 * @param hhcd HCD handle
945 * @param chnum Channel number.
946 * This parameter can be a value from 1 to 15
947 * @param urb_state:
948 * This parameter can be one of these values:
949 * URB_IDLE/
950 * URB_DONE/
951 * URB_NOTREADY/
952 * URB_NYET/
953 * URB_ERROR/
954 * URB_STALL/
955 * @retval None
956 */
HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef * hhcd,uint8_t chnum,HCD_URBStateTypeDef urb_state)957 __weak void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, HCD_URBStateTypeDef urb_state)
958 {
959 /* Prevent unused argument(s) compilation warning */
960 UNUSED(hhcd);
961 UNUSED(chnum);
962 UNUSED(urb_state);
963
964 /* NOTE : This function should not be modified, when the callback is needed,
965 the HAL_HCD_HC_NotifyURBChange_Callback could be implemented in the user file
966 */
967 }
968
969 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
970 /**
971 * @brief Register a User USB HCD Callback
972 * To be used instead of the weak predefined callback
973 * @param hhcd USB HCD handle
974 * @param CallbackID ID of the callback to be registered
975 * This parameter can be one of the following values:
976 * @arg @ref HAL_HCD_SOF_CB_ID USB HCD SOF callback ID
977 * @arg @ref HAL_HCD_CONNECT_CB_ID USB HCD Connect callback ID
978 * @arg @ref HAL_HCD_DISCONNECT_CB_ID OTG HCD Disconnect callback ID
979 * @arg @ref HAL_HCD_PORT_ENABLED_CB_ID USB HCD Port Enable callback ID
980 * @arg @ref HAL_HCD_PORT_DISABLED_CB_ID USB HCD Port Disable callback ID
981 * @arg @ref HAL_HCD_MSPINIT_CB_ID MspDeInit callback ID
982 * @arg @ref HAL_HCD_MSPDEINIT_CB_ID MspDeInit callback ID
983 * @param pCallback pointer to the Callback function
984 * @retval HAL status
985 */
HAL_HCD_RegisterCallback(HCD_HandleTypeDef * hhcd,HAL_HCD_CallbackIDTypeDef CallbackID,pHCD_CallbackTypeDef pCallback)986 HAL_StatusTypeDef HAL_HCD_RegisterCallback(HCD_HandleTypeDef *hhcd,
987 HAL_HCD_CallbackIDTypeDef CallbackID,
988 pHCD_CallbackTypeDef pCallback)
989 {
990 HAL_StatusTypeDef status = HAL_OK;
991
992 if (pCallback == NULL)
993 {
994 /* Update the error code */
995 hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
996 return HAL_ERROR;
997 }
998 /* Process locked */
999 __HAL_LOCK(hhcd);
1000
1001 if (hhcd->State == HAL_HCD_STATE_READY)
1002 {
1003 switch (CallbackID)
1004 {
1005 case HAL_HCD_SOF_CB_ID :
1006 hhcd->SOFCallback = pCallback;
1007 break;
1008
1009 case HAL_HCD_CONNECT_CB_ID :
1010 hhcd->ConnectCallback = pCallback;
1011 break;
1012
1013 case HAL_HCD_DISCONNECT_CB_ID :
1014 hhcd->DisconnectCallback = pCallback;
1015 break;
1016
1017 case HAL_HCD_PORT_ENABLED_CB_ID :
1018 hhcd->PortEnabledCallback = pCallback;
1019 break;
1020
1021 case HAL_HCD_PORT_DISABLED_CB_ID :
1022 hhcd->PortDisabledCallback = pCallback;
1023 break;
1024
1025 case HAL_HCD_MSPINIT_CB_ID :
1026 hhcd->MspInitCallback = pCallback;
1027 break;
1028
1029 case HAL_HCD_MSPDEINIT_CB_ID :
1030 hhcd->MspDeInitCallback = pCallback;
1031 break;
1032
1033 default :
1034 /* Update the error code */
1035 hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
1036 /* Return error status */
1037 status = HAL_ERROR;
1038 break;
1039 }
1040 }
1041 else if (hhcd->State == HAL_HCD_STATE_RESET)
1042 {
1043 switch (CallbackID)
1044 {
1045 case HAL_HCD_MSPINIT_CB_ID :
1046 hhcd->MspInitCallback = pCallback;
1047 break;
1048
1049 case HAL_HCD_MSPDEINIT_CB_ID :
1050 hhcd->MspDeInitCallback = pCallback;
1051 break;
1052
1053 default :
1054 /* Update the error code */
1055 hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
1056 /* Return error status */
1057 status = HAL_ERROR;
1058 break;
1059 }
1060 }
1061 else
1062 {
1063 /* Update the error code */
1064 hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
1065 /* Return error status */
1066 status = HAL_ERROR;
1067 }
1068
1069 /* Release Lock */
1070 __HAL_UNLOCK(hhcd);
1071 return status;
1072 }
1073
1074 /**
1075 * @brief Unregister an USB HCD Callback
1076 * USB HCD callback is redirected to the weak predefined callback
1077 * @param hhcd USB HCD handle
1078 * @param CallbackID ID of the callback to be unregistered
1079 * This parameter can be one of the following values:
1080 * @arg @ref HAL_HCD_SOF_CB_ID USB HCD SOF callback ID
1081 * @arg @ref HAL_HCD_CONNECT_CB_ID USB HCD Connect callback ID
1082 * @arg @ref HAL_HCD_DISCONNECT_CB_ID OTG HCD Disconnect callback ID
1083 * @arg @ref HAL_HCD_PORT_ENABLED_CB_ID USB HCD Port Enabled callback ID
1084 * @arg @ref HAL_HCD_PORT_DISABLED_CB_ID USB HCD Port Disabled callback ID
1085 * @arg @ref HAL_HCD_MSPINIT_CB_ID MspDeInit callback ID
1086 * @arg @ref HAL_HCD_MSPDEINIT_CB_ID MspDeInit callback ID
1087 * @retval HAL status
1088 */
HAL_HCD_UnRegisterCallback(HCD_HandleTypeDef * hhcd,HAL_HCD_CallbackIDTypeDef CallbackID)1089 HAL_StatusTypeDef HAL_HCD_UnRegisterCallback(HCD_HandleTypeDef *hhcd, HAL_HCD_CallbackIDTypeDef CallbackID)
1090 {
1091 HAL_StatusTypeDef status = HAL_OK;
1092
1093 /* Process locked */
1094 __HAL_LOCK(hhcd);
1095
1096 /* Setup Legacy weak Callbacks */
1097 if (hhcd->State == HAL_HCD_STATE_READY)
1098 {
1099 switch (CallbackID)
1100 {
1101 case HAL_HCD_SOF_CB_ID :
1102 hhcd->SOFCallback = HAL_HCD_SOF_Callback;
1103 break;
1104
1105 case HAL_HCD_CONNECT_CB_ID :
1106 hhcd->ConnectCallback = HAL_HCD_Connect_Callback;
1107 break;
1108
1109 case HAL_HCD_DISCONNECT_CB_ID :
1110 hhcd->DisconnectCallback = HAL_HCD_Disconnect_Callback;
1111 break;
1112
1113 case HAL_HCD_PORT_ENABLED_CB_ID :
1114 hhcd->PortEnabledCallback = HAL_HCD_PortEnabled_Callback;
1115 break;
1116
1117 case HAL_HCD_PORT_DISABLED_CB_ID :
1118 hhcd->PortDisabledCallback = HAL_HCD_PortDisabled_Callback;
1119 break;
1120
1121 case HAL_HCD_MSPINIT_CB_ID :
1122 hhcd->MspInitCallback = HAL_HCD_MspInit;
1123 break;
1124
1125 case HAL_HCD_MSPDEINIT_CB_ID :
1126 hhcd->MspDeInitCallback = HAL_HCD_MspDeInit;
1127 break;
1128
1129 default :
1130 /* Update the error code */
1131 hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
1132
1133 /* Return error status */
1134 status = HAL_ERROR;
1135 break;
1136 }
1137 }
1138 else if (hhcd->State == HAL_HCD_STATE_RESET)
1139 {
1140 switch (CallbackID)
1141 {
1142 case HAL_HCD_MSPINIT_CB_ID :
1143 hhcd->MspInitCallback = HAL_HCD_MspInit;
1144 break;
1145
1146 case HAL_HCD_MSPDEINIT_CB_ID :
1147 hhcd->MspDeInitCallback = HAL_HCD_MspDeInit;
1148 break;
1149
1150 default :
1151 /* Update the error code */
1152 hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
1153
1154 /* Return error status */
1155 status = HAL_ERROR;
1156 break;
1157 }
1158 }
1159 else
1160 {
1161 /* Update the error code */
1162 hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
1163
1164 /* Return error status */
1165 status = HAL_ERROR;
1166 }
1167
1168 /* Release Lock */
1169 __HAL_UNLOCK(hhcd);
1170 return status;
1171 }
1172
1173 /**
1174 * @brief Register USB HCD Host Channel Notify URB Change Callback
1175 * To be used instead of the weak HAL_HCD_HC_NotifyURBChange_Callback() predefined callback
1176 * @param hhcd HCD handle
1177 * @param pCallback pointer to the USB HCD Host Channel Notify URB Change Callback function
1178 * @retval HAL status
1179 */
HAL_HCD_RegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef * hhcd,pHCD_HC_NotifyURBChangeCallbackTypeDef pCallback)1180 HAL_StatusTypeDef HAL_HCD_RegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd,
1181 pHCD_HC_NotifyURBChangeCallbackTypeDef pCallback)
1182 {
1183 HAL_StatusTypeDef status = HAL_OK;
1184
1185 if (pCallback == NULL)
1186 {
1187 /* Update the error code */
1188 hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
1189
1190 return HAL_ERROR;
1191 }
1192
1193 /* Process locked */
1194 __HAL_LOCK(hhcd);
1195
1196 if (hhcd->State == HAL_HCD_STATE_READY)
1197 {
1198 hhcd->HC_NotifyURBChangeCallback = pCallback;
1199 }
1200 else
1201 {
1202 /* Update the error code */
1203 hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
1204
1205 /* Return error status */
1206 status = HAL_ERROR;
1207 }
1208
1209 /* Release Lock */
1210 __HAL_UNLOCK(hhcd);
1211
1212 return status;
1213 }
1214
1215 /**
1216 * @brief Unregister the USB HCD Host Channel Notify URB Change Callback
1217 * USB HCD Host Channel Notify URB Change Callback is redirected to the weak HAL_HCD_HC_NotifyURBChange_Callback() predefined callback
1218 * @param hhcd HCD handle
1219 * @retval HAL status
1220 */
HAL_HCD_UnRegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef * hhcd)1221 HAL_StatusTypeDef HAL_HCD_UnRegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd)
1222 {
1223 HAL_StatusTypeDef status = HAL_OK;
1224
1225 /* Process locked */
1226 __HAL_LOCK(hhcd);
1227
1228 if (hhcd->State == HAL_HCD_STATE_READY)
1229 {
1230 hhcd->HC_NotifyURBChangeCallback = HAL_HCD_HC_NotifyURBChange_Callback; /* Legacy weak DataOutStageCallback */
1231 }
1232 else
1233 {
1234 /* Update the error code */
1235 hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
1236
1237 /* Return error status */
1238 status = HAL_ERROR;
1239 }
1240
1241 /* Release Lock */
1242 __HAL_UNLOCK(hhcd);
1243
1244 return status;
1245 }
1246 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
1247
1248 /**
1249 * @}
1250 */
1251
1252 /** @defgroup HCD_Exported_Functions_Group3 Peripheral Control functions
1253 * @brief Management functions
1254 *
1255 @verbatim
1256 ===============================================================================
1257 ##### Peripheral Control functions #####
1258 ===============================================================================
1259 [..]
1260 This subsection provides a set of functions allowing to control the HCD data
1261 transfers.
1262
1263 @endverbatim
1264 * @{
1265 */
1266
1267 /**
1268 * @brief Start the host driver.
1269 * @param hhcd HCD handle
1270 * @retval HAL status
1271 */
HAL_HCD_Start(HCD_HandleTypeDef * hhcd)1272 HAL_StatusTypeDef HAL_HCD_Start(HCD_HandleTypeDef *hhcd)
1273 {
1274 HCD_TypeDef *mbase = hhcd->Instance;
1275 uint8_t power = 0;
1276 __HAL_LOCK(hhcd);
1277
1278 NVIC_EnableIRQ(USBC_IRQn);
1279 __HAL_SYSCFG_Enable_USB();
1280 __HAL_SYSCFG_USB_DM_PD();
1281
1282 __HAL_HCD_ENABLE(hhcd);
1283 mbase->testmode = 0;
1284 #ifdef SF32LB58X
1285 power |= USB_POWER_HSENAB;//hs
1286 //power &= (~USB_POWER_HSENAB);//fs
1287 #endif/*SF32LB58X */
1288 power |= USB_POWER_SOFTCONN;
1289
1290 mbase->power = power;
1291
1292 // Start Host
1293 #ifdef SF32LB55X
1294 mbase->devctl |= USB_DEVCTL_HR;
1295 #else
1296 mbase->usbcfg &= 0xEF;
1297 mbase->devctl |= 0x01;
1298 #endif/*SF32LB55X */
1299
1300 HAL_DBG_printf("%s %d,mbase->devctl=%d\n", __func__, __LINE__, mbase->devctl);
1301 __HAL_UNLOCK(hhcd);
1302
1303 return HAL_OK;
1304 }
1305
1306 /**
1307 * @brief Stop the host driver.
1308 * @param hhcd HCD handle
1309 * @retval HAL status
1310 */
1311
HAL_HCD_Stop(HCD_HandleTypeDef * hhcd)1312 HAL_StatusTypeDef HAL_HCD_Stop(HCD_HandleTypeDef *hhcd)
1313 {
1314 HCD_TypeDef *mbase = hhcd->Instance;
1315
1316 __HAL_LOCK(hhcd);
1317
1318 __HAL_HCD_DISABLE(hhcd);
1319 mbase->devctl = 0;
1320
1321 __HAL_UNLOCK(hhcd);
1322
1323 return HAL_OK;
1324 }
1325
1326 /**
1327 * @brief Reset the host port.
1328 * @param hhcd HCD handle
1329 * @retval HAL status
1330 */
HAL_HCD_ResetPort(HCD_HandleTypeDef * hhcd)1331 HAL_StatusTypeDef HAL_HCD_ResetPort(HCD_HandleTypeDef *hhcd)
1332 {
1333 HCD_TypeDef *mbase = hhcd->Instance;
1334 uint8_t power;
1335
1336 power = mbase->power;
1337
1338 if (power & USB_POWER_RESUME)
1339 {
1340 HAL_Delay(20);
1341 mbase->power = power & (~USB_POWER_RESUME);
1342 }
1343 else
1344 {
1345 #if defined(SF32LB58X)
1346 mbase->utmicfg25 |= 0xc0;
1347 mbase->utmicfg21 = 0x23;
1348 mbase->swcntl2 = 0x7c;
1349 mbase->utmicfg0 = 0x30;
1350
1351 #endif/*SF32LB58X */
1352 power &= 0xf0;
1353 mbase->power = power | USB_POWER_RESET;
1354 #if defined(SF32LB58X)
1355 hwp_usbc->rsvd0 = 0xc;//58
1356 #endif/*SF32LB58X */
1357 HAL_Delay(500);
1358 //__asm("B .");
1359 mbase->power &= (~USB_POWER_RESET);
1360 HAL_Delay(5);
1361 //mbase->power &= (~USB_POWER_HSENAB);
1362 #if defined(SF32LB58X)
1363 hwp_usbc->rsvd0 = 0x0;//58
1364 #endif /* SF32LB58X*/
1365 #if defined(SF32LB58X)
1366 mbase->utmicfg25 &= ~0xc0;
1367 mbase->utmicfg21 = 0x2f;
1368 mbase->swcntl2 = 0x40;
1369 mbase->utmicfg0 = 0x00;
1370 #endif/*SF32LB58X */
1371
1372 }
1373
1374 return HAL_OK;
1375 }
1376
1377 /**
1378 * @}
1379 */
1380
1381 /** @defgroup HCD_Exported_Functions_Group4 Peripheral State functions
1382 * @brief Peripheral State functions
1383 *
1384 @verbatim
1385 ===============================================================================
1386 ##### Peripheral State functions #####
1387 ===============================================================================
1388 [..]
1389 This subsection permits to get in run-time the status of the peripheral
1390 and the data flow.
1391
1392 @endverbatim
1393 * @{
1394 */
1395
1396 /**
1397 * @brief Return the HCD handle state.
1398 * @param hhcd HCD handle
1399 * @retval HAL state
1400 */
HAL_HCD_GetState(HCD_HandleTypeDef * hhcd)1401 HCD_StateTypeDef HAL_HCD_GetState(HCD_HandleTypeDef *hhcd)
1402 {
1403 return hhcd->State;
1404 }
1405
1406 /**
1407 * @brief Return URB state for a channel.
1408 * @param hhcd HCD handle
1409 * @param chnum Channel number.
1410 * This parameter can be a value from 1 to 15
1411 * @retval URB state.
1412 * This parameter can be one of these values:
1413 * URB_IDLE/
1414 * URB_DONE/
1415 * URB_NOTREADY/
1416 * URB_NYET/
1417 * URB_ERROR/
1418 * URB_STALL
1419 */
HAL_HCD_HC_GetURBState(HCD_HandleTypeDef * hhcd,uint8_t chnum)1420 HCD_URBStateTypeDef HAL_HCD_HC_GetURBState(HCD_HandleTypeDef *hhcd, uint8_t chnum)
1421 {
1422 return hhcd->hc[chnum].urb_state;
1423 }
1424
1425
1426 /**
1427 * @brief Return the last host transfer size.
1428 * @param hhcd HCD handle
1429 * @param chnum Channel number.
1430 * This parameter can be a value from 1 to 15
1431 * @retval last transfer size in byte
1432 */
HAL_HCD_HC_GetXferCount(HCD_HandleTypeDef * hhcd,uint8_t chnum)1433 uint32_t HAL_HCD_HC_GetXferCount(HCD_HandleTypeDef *hhcd, uint8_t chnum)
1434 {
1435 return hhcd->hc[chnum].xfer_count;
1436 }
1437
1438 /**
1439 * @brief Return the Host Channel state.
1440 * @param hhcd HCD handle
1441 * @param chnum Channel number.
1442 * This parameter can be a value from 1 to 15
1443 * @retval Host channel state
1444 * This parameter can be one of these values:
1445 * HC_IDLE/
1446 * HC_XFRC/
1447 * HC_HALTED/
1448 * HC_NYET/
1449 * HC_NAK/
1450 * HC_STALL/
1451 * HC_XACTERR/
1452 * HC_BBLERR/
1453 * HC_DATATGLERR
1454 */
HAL_HCD_HC_GetState(HCD_HandleTypeDef * hhcd,uint8_t chnum)1455 HCD_HCStateTypeDef HAL_HCD_HC_GetState(HCD_HandleTypeDef *hhcd, uint8_t chnum)
1456 {
1457 return hhcd->hc[chnum].state;
1458 }
1459
1460 /**
1461 * @brief Return the current Host frame number.
1462 * @param hhcd HCD handle
1463 * @retval Current Host frame number
1464 */
HAL_HCD_GetCurrentFrame(HCD_HandleTypeDef * hhcd)1465 uint32_t HAL_HCD_GetCurrentFrame(HCD_HandleTypeDef *hhcd)
1466 {
1467
1468 HCD_TypeDef *mbase = hhcd->Instance;
1469
1470 return mbase->frame;
1471 }
1472
1473 /**
1474 * @brief Return the Host enumeration speed.
1475 * @param hhcd HCD handle
1476 * @retval Enumeration speed
1477 */
HAL_HCD_GetCurrentSpeed(HCD_HandleTypeDef * hhcd)1478 uint32_t HAL_HCD_GetCurrentSpeed(HCD_HandleTypeDef *hhcd)
1479 {
1480 HCD_TypeDef *mbase = hhcd->Instance;
1481
1482 HAL_ASSERT((mbase->devctl & USB_DEVCTL_HM));
1483 if (mbase->power & USB_POWER_HSMODE)
1484 return HCD_SPEED_HIGH;
1485 if (mbase->devctl & USB_DEVCTL_FSDEV)
1486 return HCD_SPEED_FULL;
1487 if (mbase->devctl & USB_DEVCTL_LSDEV)
1488 return HCD_SPEED_LOW;
1489 return HCD_SPEED_FULL;
1490 }
1491
1492 /**
1493 * @}
1494 */
1495
1496 /**
1497 * @}
1498 */
1499
1500 /** @addtogroup HCD_Private_Functions
1501 * @{
1502 */
1503 /**
1504 * @brief Handle Host Channel IN interrupt requests.
1505 * @param hhcd HCD handle
1506 * @param ep_num endpoint number.
1507 * This parameter can be a value from 1 to 15
1508 * @retval none
1509 */
HCD_HC_IN_IRQHandler(HCD_HandleTypeDef * hhcd,uint8_t ep_num)1510 static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t ep_num)
1511 {
1512 USBC_X_Typedef *musb = hhcd->Instance;
1513 uint8_t *pBuf;
1514 __IO uint8_t *fifox = (__IO uint8_t *) & (hhcd->Instance->fifox[ep_num]);
1515 #ifdef SF32LB58X
1516 int chnum = ep_2_ch(hhcd, ep_num | USB_DIR_IN);
1517 #else
1518 int chnum = ep_num;//ep_2_ch(hhcd, ep_num | USB_DIR_IN);
1519 #endif/*SF32LB58X */
1520 if (chnum < 0)
1521 return;
1522
1523 volatile struct musb_epN_regs *epn = &musb->ep[ep_num].epN;
1524 uint16_t rxcsr = epn->rxcsr;
1525 uint16_t rx_count = epn->rxcount;
1526 //HAL_DBG_printf("rx complete: ep_num=%d,chnum=%d,csr=0x%x, count=%d\n", ep_num, chnum, rxcsr,rx_count);
1527 #if USB_DATA_DEBUG
1528 rt_kprintf("%s %d ep_num=%d,chnum=%d,rxcsr=0x%x,rx_count=%d\n", __func__, __LINE__, ep_num, chnum, rxcsr, rx_count);
1529 #endif/*USB_DATA_DEBUG */
1530 if (rxcsr & USB_RXCSR_H_RXSTALL)
1531 {
1532 hhcd->hc[chnum].state = HC_STALL;
1533 hhcd->hc[chnum].urb_state = URB_STALL;
1534 epn->rxcsr &= (~USB_RXCSR_H_RXSTALL);
1535 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1536 }
1537 else if (rxcsr & USB_RXCSR_H_ERROR)
1538 {
1539 epn->rxinterval = 0;
1540 epn->rxcsr &= ~USB_RXCSR_H_ERROR;
1541
1542 hhcd->hc[chnum].state = HC_XACTERR;
1543 hhcd->hc[chnum].urb_state = URB_ERROR;
1544 epn->rxcsr &= (~USB_RXCSR_H_ERROR);
1545 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1546 }
1547 else if (rxcsr & USB_RXCSR_DATAERROR)
1548 {
1549 rxcsr |= USB_RXCSR_H_WZC_BITS;
1550 rxcsr &= ~USB_RXCSR_DATAERROR;
1551 hhcd->hc[chnum].state = HC_DATATGLERR;
1552 hhcd->hc[chnum].urb_state = URB_ERROR;
1553 epn->rxcsr &= (~USB_RXCSR_DATAERROR);
1554 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1555 }
1556 else
1557 {
1558 int i;
1559 if ((rxcsr | USB_RXCSR_H_DATATOGGLE))
1560 {
1561 hhcd->hc[chnum].xfer_count = rx_count;
1562 pBuf = hhcd->hc[chnum].xfer_buff;
1563 HAL_DBG_printf("%s %d chnum=%d,ep_num=%d,rxcsr=0x%x\n", __func__, __LINE__, chnum, ep_num, rxcsr);
1564 HCD_HCTypeDef *ep = &hhcd->hc[chnum];
1565 struct musb_dma_regs *dma = (struct musb_dma_regs *) & (hhcd->Instance->dma[chnum]);
1566 #if USB_RX_DMA_ENABLED
1567 //__IO struct musb_epN_regs *epn = &(hhcd->Instance->ep[chnum].epN);
1568
1569 HAL_DBG_printf("DMA RX pipe=%d, len=%d,xfer_buff=%p\n", chnum, epn->rxcount, ep->xfer_buff);
1570 if (epn->rxcount)
1571 {
1572 if (IS_DCACHED_RAM(ep->xfer_buff))
1573 SCB_InvalidateDCache_by_Addr(ep->xfer_buff, epn->rxcount);
1574
1575 dma->addr = (REG32)ep->xfer_buff;
1576 dma->count = epn->rxcount;
1577 ep->xfer_count = dma->count;
1578 //epn->rxcount = 0;
1579 dma->cntl = (1 << USB_DMACTRL_ENABLE_SHIFT) |
1580 (0 << USB_DMACTRL_TRANSMIT_SHIFT) |
1581 (0 << USB_DMACTRL_MODE1_SHIFT) |
1582 (ep_num << USB_DMACTRL_ENDPOINT_SHIFT) |
1583 (1 << USB_DMACTRL_IRQENABLE_SHIFT);
1584 }
1585 #elif USB_RX_CONT_DMA_ENABLED
1586 if (0 == rx_packet_max)
1587 {
1588 if (IS_DCACHED_RAM(ep->xfer_buff))
1589 SCB_InvalidateDCache_by_Addr(ep->xfer_buff, epn->rxcount);
1590 ep->xfer_count = rx_count;
1591 epn->rxcsr &= ~(USB_RXCSR_H_REQPKT
1592 | USB_RXCSR_DMAENAB
1593 | USB_RXCSR_AUTOCLEAR
1594 | USB_RXCSR_H_AUTOREQ);
1595 dma->cntl |= (1 << USB_DMACTRL_ENABLE_SHIFT) | (0 << USB_DMACTRL_MODE1_SHIFT);
1596 }
1597 #else
1598 for (i = 0; i < rx_count; i++)
1599 *(pBuf + i) = *fifox;
1600 #if USB_DATA_DEBUG
1601 rt_kprintf("%d usb chnum=%d rx\n", __LINE__, chnum);
1602 for (i = 0; i < rx_count; i++)
1603 rt_kprintf("%x ", pBuf[i]);
1604 rt_kprintf("\n");
1605 #endif/*USB_DATA_DEBUG */
1606 hhcd->hc[chnum].state = HC_XFRC;
1607 hhcd->hc[chnum].urb_state = URB_DONE;
1608 epn->rxcsr &= (~USB_RXCSR_RXPKTRDY);
1609 if ((rxcsr | USB_RXCSR_H_DATATOGGLE))
1610 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1611 #endif/*USB_RX_DMA_ENABLED */
1612 }
1613
1614 }
1615
1616 #if 0
1617 if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_AHBERR) == USB_OTG_HCINT_AHBERR)
1618 {
1619 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_AHBERR);
1620 __HAL_HCD_UNMASK_HALT_HC_INT(ch_num);
1621 }
1622 else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_BBERR) == USB_OTG_HCINT_BBERR)
1623 {
1624 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_BBERR);
1625 hhcd->hc[ch_num].state = HC_BBLERR;
1626 __HAL_HCD_UNMASK_HALT_HC_INT(ch_num);
1627 (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1628 }
1629 else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_ACK) == USB_OTG_HCINT_ACK)
1630 {
1631 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_ACK);
1632 }
1633 else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_STALL) == USB_OTG_HCINT_STALL)
1634 {
1635 __HAL_HCD_UNMASK_HALT_HC_INT(ch_num);
1636 hhcd->hc[ch_num].state = HC_STALL;
1637 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NAK);
1638 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_STALL);
1639 (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1640 }
1641 else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_DTERR) == USB_OTG_HCINT_DTERR)
1642 {
1643 __HAL_HCD_UNMASK_HALT_HC_INT(ch_num);
1644 hhcd->hc[ch_num].state = HC_DATATGLERR;
1645 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NAK);
1646 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_DTERR);
1647 (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1648 }
1649 else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_TXERR) == USB_OTG_HCINT_TXERR)
1650 {
1651 __HAL_HCD_UNMASK_HALT_HC_INT(ch_num);
1652 hhcd->hc[ch_num].state = HC_XACTERR;
1653 (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1654 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_TXERR);
1655 }
1656 else
1657 {
1658 /* ... */
1659 }
1660
1661 if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_FRMOR) == USB_OTG_HCINT_FRMOR)
1662 {
1663 __HAL_HCD_UNMASK_HALT_HC_INT(ch_num);
1664 (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1665 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_FRMOR);
1666 }
1667 else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_XFRC) == USB_OTG_HCINT_XFRC)
1668 {
1669 if (hhcd->Init.dma_enable != 0U)
1670 {
1671 hhcd->hc[ch_num].xfer_count = hhcd->hc[ch_num].XferSize - \
1672 (USBx_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ);
1673 }
1674
1675 hhcd->hc[ch_num].state = HC_XFRC;
1676 hhcd->hc[ch_num].ErrCnt = 0U;
1677 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_XFRC);
1678
1679 if ((hhcd->hc[ch_num].ep_type == EP_TYPE_CTRL) ||
1680 (hhcd->hc[ch_num].ep_type == EP_TYPE_BULK))
1681 {
1682 __HAL_HCD_UNMASK_HALT_HC_INT(ch_num);
1683 (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1684 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NAK);
1685 }
1686 else if (hhcd->hc[ch_num].ep_type == EP_TYPE_INTR)
1687 {
1688 USBx_HC(ch_num)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
1689 hhcd->hc[ch_num].urb_state = URB_DONE;
1690
1691 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
1692 hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
1693 #else
1694 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
1695 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
1696 }
1697 else if (hhcd->hc[ch_num].ep_type == EP_TYPE_ISOC)
1698 {
1699 hhcd->hc[ch_num].urb_state = URB_DONE;
1700 hhcd->hc[ch_num].toggle_in ^= 1U;
1701
1702 #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
1703 hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
1704 #else
1705 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
1706 #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
1707 }
1708 else
1709 {
1710 /* ... */
1711 }
1712
1713 if (hhcd->Init.dma_enable == 1U)
1714 {
1715 if (((hhcd->hc[ch_num].XferSize / hhcd->hc[ch_num].max_packet) & 1U) != 0U)
1716 {
1717 hhcd->hc[ch_num].toggle_in ^= 1U;
1718 }
1719 }
1720 else
1721 {
1722 hhcd->hc[ch_num].toggle_in ^= 1U;
1723 }
1724 }
1725 else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_CHH) == USB_OTG_HCINT_CHH)
1726 {
1727 __HAL_HCD_MASK_HALT_HC_INT(ch_num);
1728
1729 if (hhcd->hc[ch_num].state == HC_XFRC)
1730 {
1731 hhcd->hc[ch_num].urb_state = URB_DONE;
1732 }
1733 else if (hhcd->hc[ch_num].state == HC_STALL)
1734 {
1735 hhcd->hc[ch_num].urb_state = URB_STALL;
1736 }
1737 else if ((hhcd->hc[ch_num].state == HC_XACTERR) ||
1738 (hhcd->hc[ch_num].state == HC_DATATGLERR))
1739 {
1740 hhcd->hc[ch_num].ErrCnt++;
1741 if (hhcd->hc[ch_num].ErrCnt > 2U)
1742 {
1743 hhcd->hc[ch_num].ErrCnt = 0U;
1744 hhcd->hc[ch_num].urb_state = URB_ERROR;
1745 }
1746 else
1747 {
1748 hhcd->hc[ch_num].urb_state = URB_NOTREADY;
1749
1750 /* re-activate the channel */
1751 tmpreg = USBx_HC(ch_num)->HCCHAR;
1752 tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
1753 tmpreg |= USB_OTG_HCCHAR_CHENA;
1754 USBx_HC(ch_num)->HCCHAR = tmpreg;
1755 }
1756 }
1757 else if (hhcd->hc[ch_num].state == HC_NAK)
1758 {
1759 hhcd->hc[ch_num].urb_state = URB_NOTREADY;
1760
1761 /* re-activate the channel */
1762 tmpreg = USBx_HC(ch_num)->HCCHAR;
1763 tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
1764 tmpreg |= USB_OTG_HCCHAR_CHENA;
1765 USBx_HC(ch_num)->HCCHAR = tmpreg;
1766 }
1767 else if (hhcd->hc[ch_num].state == HC_BBLERR)
1768 {
1769 hhcd->hc[ch_num].ErrCnt++;
1770 hhcd->hc[ch_num].urb_state = URB_ERROR;
1771 }
1772 else
1773 {
1774 /* ... */
1775 }
1776 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_CHH);
1777 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
1778 }
1779 else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_NAK) == USB_OTG_HCINT_NAK)
1780 {
1781 if (hhcd->hc[ch_num].ep_type == EP_TYPE_INTR)
1782 {
1783 hhcd->hc[ch_num].ErrCnt = 0U;
1784 __HAL_HCD_UNMASK_HALT_HC_INT(ch_num);
1785 (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1786 }
1787 else if ((hhcd->hc[ch_num].ep_type == EP_TYPE_CTRL) ||
1788 (hhcd->hc[ch_num].ep_type == EP_TYPE_BULK))
1789 {
1790 hhcd->hc[ch_num].ErrCnt = 0U;
1791
1792 if (hhcd->Init.dma_enable == 0U)
1793 {
1794 hhcd->hc[ch_num].state = HC_NAK;
1795 __HAL_HCD_UNMASK_HALT_HC_INT(ch_num);
1796 (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
1797 }
1798 }
1799 else
1800 {
1801 /* ... */
1802 }
1803 __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NAK);
1804 }
1805 else
1806 {
1807 /* ... */
1808 }
1809 #endif/* USB_RX_DMA_ENABLED */
1810
1811 }
1812
1813 /**
1814 * @brief Handle Host Channel OUT interrupt requests.
1815 * @param hhcd HCD handle
1816 * @param endpoint number.
1817 * This parameter can be a value from 1 to 15
1818 * @retval none
1819 */
HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef * hhcd,uint8_t ep_num)1820 static void HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t ep_num)
1821 {
1822 USBC_X_Typedef *musb = hhcd->Instance;
1823 #ifdef SF32LB58X
1824 uint8_t chnum = ep_2_ch(hhcd, ep_num | USB_DIR_OUT);
1825 #else
1826 uint8_t chnum = ep_num;
1827 #endif/*SF32LB58X */
1828 uint16_t txcsr = musb->ep[ep_num].epN.txcsr;
1829
1830 if (chnum < 0)
1831 return;
1832 if (txcsr & USB_TXCSR_H_RXSTALL)
1833 {
1834 hhcd->hc[chnum].state = HC_STALL;
1835 hhcd->hc[chnum].urb_state = URB_STALL;
1836 txcsr &= (~USB_TXCSR_H_RXSTALL);
1837 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1838 }
1839 else if (txcsr & USB_TXCSR_H_ERROR)
1840 {
1841 hhcd->hc[chnum].state = HC_XACTERR;
1842 hhcd->hc[chnum].urb_state = URB_ERROR;
1843 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1844 }
1845 else if (txcsr & USB_TXCSR_H_NAKTIMEOUT)
1846 {
1847 hhcd->hc[chnum].state = HC_NAK;
1848 hhcd->hc[chnum].urb_state = URB_NOTREADY;
1849 musb->ep[ep_num].epN.txcsr = USB_TXCSR_H_WZC_BITS | USB_TXCSR_TXPKTRDY;
1850 musb->ep[ep_num].epN.txcsr &= ~USB_TXCSR_H_NAKTIMEOUT;
1851 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1852 }
1853 else if (txcsr & 0x0002)
1854 {
1855 //HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1856 }
1857 else
1858 {
1859 hhcd->hc[chnum].state = HC_XFRC;
1860 hhcd->hc[chnum].urb_state = URB_DONE;
1861 txcsr &= (~0xa000);
1862 if (txcsr == 0x000) HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1863 if ((txcsr | USB_TXCSR_H_DATATOGGLE)) HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1864 else musb->ep[chnum].epN.txcsr &= ~USB_TXCSR_H_DATATOGGLE;
1865 }
1866
1867 }
1868
1869
HCD_HC_EP0_IRQHandler(HCD_HandleTypeDef * hhcd)1870 static void HCD_HC_EP0_IRQHandler(HCD_HandleTypeDef *hhcd)
1871 {
1872 uint16_t csr;
1873 uint16_t len;
1874 int chnum;
1875 __IO struct musb_ep0_regs *ep0 = &(hhcd->Instance->ep[0].ep0);
1876 __IO uint8_t *fifox = (__IO uint8_t *) & (hhcd->Instance->fifox[0]);
1877
1878 csr = ep0->csr0;
1879 len = ep0->count0;
1880
1881 if (hhcd->ep0_state == HAL_HCD_EP0_SETUP || hhcd->ep0_state == HAL_HCD_EP0_TX)
1882 {
1883 chnum = ep_2_ch(hhcd, 0);
1884 }
1885 else if (hhcd->ep0_state == HAL_HCD_EP0_RX)
1886 {
1887 chnum = ep_2_ch(hhcd, 0 | USB_DIR_IN);
1888 }
1889 else
1890 {
1891 HAL_ASSERT(0);
1892 }
1893 HAL_DBG_printf("ep0 ISR: csr=0x%x, len=%d, chnum=%d\r\n", csr, len, chnum);
1894 if ((csr & 0xa0) == 0xa0)
1895 {
1896 //csr = USB_CSR0_RXPKTRDY;
1897 ep0->csr0 &= (~0xa0);
1898 }
1899
1900 //HAL_DBG_printf("ep0 ISR: csr=0x%x, len=%d, chnum=%d\r\n", csr, len, chnum);
1901
1902 if (csr & USB_CSR0_H_ERROR)
1903 {
1904 hhcd->hc[chnum].state = HC_XACTERR;
1905 hhcd->hc[chnum].urb_state = URB_ERROR;
1906 ep0->csr0 &= (~USB_CSR0_H_ERROR);
1907 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1908 }
1909 else if (csr & USB_CSR0_H_RXSTALL)
1910 {
1911 hhcd->hc[chnum].state = HC_STALL;
1912 hhcd->hc[chnum].urb_state = URB_STALL;
1913 ep0->csr0 &= (~USB_CSR0_H_RXSTALL);
1914 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1915 }
1916 else if (csr & USB_CSR0_H_NAKTIMEOUT)
1917 {
1918 hhcd->hc[chnum].state = HC_NAK;
1919 hhcd->hc[chnum].urb_state = URB_NYET;
1920 ep0->csr0 &= (~USB_CSR0_H_NAKTIMEOUT);
1921 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1922 }
1923 else
1924 {
1925 if (csr & USB_CSR0_RXPKTRDY)
1926 {
1927 int i;
1928 hhcd->hc[chnum].xfer_count = len;
1929 uint8_t *pBuf = hhcd->hc[chnum].xfer_buff;
1930 for (i = 0; i < len; i++)
1931 *(pBuf + i) = *fifox;
1932 #if USB_DATA_DEBUG
1933 rt_kprintf("%d usb chnum=%d rx\n", __LINE__, chnum);
1934 for (i = 0; i < len; i++)
1935 rt_kprintf("%x ", pBuf[i]);
1936 rt_kprintf("\n");
1937 #endif/*USB_DATA_DEBUG */
1938 ep0->csr0 &= (~USB_CSR0_RXPKTRDY);
1939 }
1940 hhcd->hc[chnum].state = HC_XFRC;
1941 hhcd->hc[chnum].urb_state = URB_DONE;
1942 if ((csr | USB_CSR0_H_DATATOGGLE))
1943 HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum].urb_state);
1944 }
1945 hhcd->ep0_state = HAL_HCD_EP0_IDLE;
1946
1947 }
1948
1949 #if 0
1950 /**
1951 * @brief Handle Rx Queue Level interrupt requests.
1952 * @param hhcd HCD handle
1953 * @retval none
1954 */
1955 static void HCD_RXQLVL_IRQHandler(HCD_HandleTypeDef *hhcd)
1956 {
1957
1958 uint32_t GrxstspReg = hhcd->Instance->GRXSTSP;
1959 uint32_t ch_num = GrxstspReg & USB_OTG_GRXSTSP_EPNUM;
1960 uint32_t pktsts = (GrxstspReg & USB_OTG_GRXSTSP_PKTSTS) >> 17;
1961 uint32_t pktcnt = (GrxstspReg & USB_OTG_GRXSTSP_BCNT) >> 4;
1962
1963 switch (pktsts)
1964 {
1965 case GRXSTS_PKTSTS_IN:
1966 /* Read the data into the host buffer. */
1967 if ((pktcnt > 0U) && (hhcd->hc[ch_num].xfer_buff != (void *)0))
1968 {
1969 if ((hhcd->hc[ch_num].xfer_count + pktcnt) <= hhcd->hc[ch_num].xfer_len)
1970 {
1971 (void)USB_ReadPacket(hhcd->Instance,
1972 hhcd->hc[ch_num].xfer_buff, (uint16_t)pktcnt);
1973
1974 /* manage multiple Xfer */
1975 hhcd->hc[ch_num].xfer_buff += pktcnt;
1976 hhcd->hc[ch_num].xfer_count += pktcnt;
1977
1978 /* get transfer size packet count */
1979 uint32_t xferSizePktCnt = (USBx_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) >> 19;
1980
1981 if ((hhcd->hc[ch_num].max_packet == pktcnt) && (xferSizePktCnt > 0U))
1982 {
1983 /* re-activate the channel when more packets are expected */
1984 uint32_t tmpreg;
1985 tmpreg = USBx_HC(ch_num)->HCCHAR;
1986 tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
1987 tmpreg |= USB_OTG_HCCHAR_CHENA;
1988 USBx_HC(ch_num)->HCCHAR = tmpreg;
1989 hhcd->hc[ch_num].toggle_in ^= 1U;
1990 }
1991 }
1992 else
1993 {
1994 hhcd->hc[ch_num].urb_state = URB_ERROR;
1995 }
1996 }
1997 break;
1998
1999 case GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
2000 break;
2001
2002 case GRXSTS_PKTSTS_IN_XFER_COMP:
2003 case GRXSTS_PKTSTS_CH_HALTED:
2004 default:
2005 break;
2006 }
2007 }
2008 #endif
2009
2010 /**
2011 * @}
2012 */
2013
2014 /**
2015 * @}
2016 */
2017
2018 #endif /* HAL_HCD_MODULE_ENABLED */
2019
2020 /**
2021 * @}
2022 */
2023
2024 /**
2025 * @}
2026 */
2027