Skip to content

Commit 8af69dc

Browse files
committed
STM32 HAL HCD : USBHOST changes for f4,f2,l4,f7
- reset toggle_out , toggle_in at init - in/out toggle in on ctrl endpoint - remove call back when transmission restarted
1 parent 54db0a4 commit 8af69dc

File tree

8 files changed

+176
-30
lines changed

8 files changed

+176
-30
lines changed

targets/TARGET_STM/TARGET_STM32F2/device/stm32f2xx_hal_hcd.c

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ HAL_StatusTypeDef HAL_HCD_HC_Init(HCD_HandleTypeDef *hhcd,
200200
hhcd->hc[ch_num].ep_num = epnum & 0x7FU;
201201
hhcd->hc[ch_num].ep_is_in = ((epnum & 0x80U) == 0x80U);
202202
hhcd->hc[ch_num].speed = speed;
203-
203+
/* reset to 0 */
204+
hhcd->hc[ch_num].toggle_out = 0;
205+
hhcd->hc[ch_num].toggle_in = 0;
206+
204207
status = USB_HC_Init(hhcd->Instance,
205208
ch_num,
206209
epnum,
@@ -335,7 +338,26 @@ HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
335338
uint16_t length,
336339
uint8_t do_ping)
337340
{
338-
hhcd->hc[ch_num].ep_is_in = direction;
341+
if ((hhcd->hc[ch_num].ep_is_in != direction)) {
342+
if ((hhcd->hc[ch_num].ep_type == EP_TYPE_CTRL)){
343+
/* reconfigure the endpoint !!! from tx -> rx, and rx ->tx */
344+
USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
345+
if (direction)
346+
{
347+
USBx_HC(ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
348+
USBx_HC(ch_num)->HCCHAR |= 1 << 15;
349+
}
350+
else
351+
{
352+
USBx_HC(ch_num)->HCINTMSK &= ~USB_OTG_HCINTMSK_BBERRM;
353+
USBx_HC(ch_num)->HCCHAR &= ~(1 << 15);
354+
}
355+
hhcd->hc[ch_num].ep_is_in = direction;
356+
/* if reception put toggle_in to 1 */
357+
if (direction == 1) hhcd->hc[ch_num].toggle_in=1;
358+
}
359+
}
360+
339361
hhcd->hc[ch_num].ep_type = ep_type;
340362

341363
if(token == 0)
@@ -372,6 +394,18 @@ HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
372394
hhcd->hc[ch_num].do_ping = do_ping;
373395
}
374396
}
397+
else if ((token == 1) && (direction == 1))
398+
{
399+
if( hhcd->hc[ch_num].toggle_in == 0)
400+
{
401+
hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
402+
}
403+
else
404+
{
405+
hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
406+
}
407+
408+
}
375409
break;
376410

377411
case EP_TYPE_BULK:
@@ -402,7 +436,7 @@ HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
402436
hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
403437
}
404438
}
405-
439+
406440
break;
407441
case EP_TYPE_INTR:
408442
if(direction == 0U)
@@ -429,7 +463,7 @@ HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
429463
}
430464
}
431465
break;
432-
466+
433467
case EP_TYPE_ISOC:
434468
hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
435469
break;
@@ -866,6 +900,8 @@ static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum)
866900
}
867901
else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH)
868902
{
903+
904+
int reactivate = 0;
869905
__HAL_HCD_MASK_HALT_HC_INT(chnum);
870906

871907
if(hhcd->hc[chnum].state == HC_XFRC)
@@ -896,9 +932,10 @@ static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum)
896932
tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
897933
tmpreg |= USB_OTG_HCCHAR_CHENA;
898934
USBx_HC(chnum)->HCCHAR = tmpreg;
935+
reactivate = 1;
899936
}
900937
__HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH);
901-
HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
938+
if (reactivate == 0) HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
902939
}
903940

904941
else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR)

targets/TARGET_STM/TARGET_STM32F2/device/stm32f2xx_ll_usb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,7 @@ HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_HCTypeDe
15181518

15191519
/* Write packet into the Tx FIFO. */
15201520
USB_WritePacket(USBx, hc->xfer_buff, hc->ch_num, hc->xfer_len, 0);
1521+
hc->xfer_count = hc->xfer_len;
15211522
}
15221523
}
15231524

targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_hal_hcd.c

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,10 @@ HAL_StatusTypeDef HAL_HCD_HC_Init(HCD_HandleTypeDef *hhcd,
204204
hhcd->hc[ch_num].ep_num = epnum & 0x7FU;
205205
hhcd->hc[ch_num].ep_is_in = ((epnum & 0x80U) == 0x80U);
206206
hhcd->hc[ch_num].speed = speed;
207-
207+
/* reset to 0 */
208+
hhcd->hc[ch_num].toggle_out = 0;
209+
hhcd->hc[ch_num].toggle_in = 0;
210+
208211
status = USB_HC_Init(hhcd->Instance,
209212
ch_num,
210213
epnum,
@@ -339,9 +342,27 @@ HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
339342
uint16_t length,
340343
uint8_t do_ping)
341344
{
342-
hhcd->hc[ch_num].ep_is_in = direction;
343-
hhcd->hc[ch_num].ep_type = ep_type;
344-
345+
if ((hhcd->hc[ch_num].ep_is_in != direction)) {
346+
if ((hhcd->hc[ch_num].ep_type == EP_TYPE_CTRL)){
347+
/* reconfigure the endpoint !!! from tx -> rx, and rx ->tx */
348+
USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
349+
if (direction)
350+
{
351+
USBx_HC(ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
352+
USBx_HC(ch_num)->HCCHAR |= 1 << 15;
353+
}
354+
else
355+
{
356+
USBx_HC(ch_num)->HCINTMSK &= ~USB_OTG_HCINTMSK_BBERRM;
357+
USBx_HC(ch_num)->HCCHAR &= ~(1 << 15);
358+
}
359+
hhcd->hc[ch_num].ep_is_in = direction;
360+
/* if reception put toggle_in to 1 */
361+
if (direction == 1) hhcd->hc[ch_num].toggle_in=1;
362+
}
363+
}
364+
hhcd->hc[ch_num].ep_type = ep_type;
365+
345366
if(token == 0U)
346367
{
347368
hhcd->hc[ch_num].data_pid = HC_PID_SETUP;
@@ -376,6 +397,17 @@ HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
376397
hhcd->hc[ch_num].do_ping = do_ping;
377398
}
378399
}
400+
else if ((token == 1) && (direction == 1))
401+
{
402+
if( hhcd->hc[ch_num].toggle_in == 0)
403+
{
404+
hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
405+
}
406+
else
407+
{
408+
hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
409+
}
410+
}
379411
break;
380412

381413
case EP_TYPE_BULK:
@@ -870,20 +902,21 @@ static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum)
870902
}
871903
else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH)
872904
{
905+
int reactivate=0;
873906
__HAL_HCD_MASK_HALT_HC_INT(chnum);
874-
907+
875908
if(hhcd->hc[chnum].state == HC_XFRC)
876909
{
877910
hhcd->hc[chnum].urb_state = URB_DONE;
878911
}
879-
912+
880913
else if (hhcd->hc[chnum].state == HC_STALL)
881914
{
882915
hhcd->hc[chnum].urb_state = URB_STALL;
883916
}
884-
917+
885918
else if((hhcd->hc[chnum].state == HC_XACTERR) ||
886-
(hhcd->hc[chnum].state == HC_DATATGLERR))
919+
(hhcd->hc[chnum].state == HC_DATATGLERR))
887920
{
888921
if(hhcd->hc[chnum].ErrCnt++ > 3U)
889922
{
@@ -894,15 +927,16 @@ static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum)
894927
{
895928
hhcd->hc[chnum].urb_state = URB_NOTREADY;
896929
}
897-
930+
898931
/* re-activate the channel */
899932
tmpreg = USBx_HC(chnum)->HCCHAR;
900933
tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
901934
tmpreg |= USB_OTG_HCCHAR_CHENA;
902935
USBx_HC(chnum)->HCCHAR = tmpreg;
936+
reactivate = 1;
903937
}
904938
__HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH);
905-
HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
939+
if (reactivate == 0) HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
906940
}
907941

908942
else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR)

targets/TARGET_STM/TARGET_STM32F4/device/stm32f4xx_ll_usb.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,7 @@ HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_HCTypeDe
15401540

15411541
/* Write packet into the Tx FIFO. */
15421542
USB_WritePacket(USBx, hc->xfer_buff, hc->ch_num, hc->xfer_len, 0);
1543+
hc->xfer_count = hc->xfer_len;
15431544
}
15441545
}
15451546

targets/TARGET_STM/TARGET_STM32F7/device/stm32f7xx_hal_hcd.c

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ HAL_StatusTypeDef HAL_HCD_HC_Init(HCD_HandleTypeDef *hhcd,
200200
hhcd->hc[ch_num].ep_num = epnum & 0x7F;
201201
hhcd->hc[ch_num].ep_is_in = ((epnum & 0x80) == 0x80);
202202
hhcd->hc[ch_num].speed = speed;
203+
/* reset to 0 */
204+
hhcd->hc[ch_num].toggle_out = 0;
205+
hhcd->hc[ch_num].toggle_in = 0;
203206

204207
status = USB_HC_Init(hhcd->Instance,
205208
ch_num,
@@ -337,9 +340,27 @@ HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
337340
uint16_t length,
338341
uint8_t do_ping)
339342
{
340-
hhcd->hc[ch_num].ep_is_in = direction;
341-
hhcd->hc[ch_num].ep_type = ep_type;
342-
343+
if ((hhcd->hc[ch_num].ep_is_in != direction)) {
344+
if ((hhcd->hc[ch_num].ep_type == EP_TYPE_CTRL)){
345+
/* reconfigure the endpoint !!! from tx -> rx, and rx ->tx */
346+
USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
347+
if (direction)
348+
{
349+
USBx_HC(ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
350+
USBx_HC(ch_num)->HCCHAR |= 1 << 15;
351+
}
352+
else
353+
{
354+
USBx_HC(ch_num)->HCINTMSK &= ~USB_OTG_HCINTMSK_BBERRM;
355+
USBx_HC(ch_num)->HCCHAR &= ~(1 << 15);
356+
}
357+
hhcd->hc[ch_num].ep_is_in = direction;
358+
/* if reception put toggle_in to 1 */
359+
if (direction == 1) hhcd->hc[ch_num].toggle_in=1;
360+
}
361+
}
362+
hhcd->hc[ch_num].ep_type = ep_type;
363+
343364
if(token == 0)
344365
{
345366
hhcd->hc[ch_num].data_pid = HC_PID_SETUP;
@@ -348,7 +369,7 @@ HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
348369
{
349370
hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
350371
}
351-
372+
352373
/* Manage Data Toggle */
353374
switch(ep_type)
354375
{
@@ -359,7 +380,7 @@ HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
359380
{ /* For Status OUT stage, Length==0, Status Out PID = 1 */
360381
hhcd->hc[ch_num].toggle_out = 1;
361382
}
362-
383+
363384
/* Set the Data Toggle bit as per the Flag */
364385
if ( hhcd->hc[ch_num].toggle_out == 0)
365386
{ /* Put the PID 0 */
@@ -374,8 +395,20 @@ HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
374395
hhcd->hc[ch_num].do_ping = do_ping;
375396
}
376397
}
398+
else if ((token == 1) && (direction == 1))
399+
{
400+
if( hhcd->hc[ch_num].toggle_in == 0)
401+
{
402+
hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
403+
}
404+
else
405+
{
406+
hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
407+
}
408+
409+
}
377410
break;
378-
411+
379412
case EP_TYPE_BULK:
380413
if(direction == 0)
381414
{
@@ -404,7 +437,7 @@ HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
404437
hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
405438
}
406439
}
407-
440+
408441
break;
409442
case EP_TYPE_INTR:
410443
if(direction == 0)
@@ -870,7 +903,8 @@ static void HCD_HC_IN_IRQHandler (HCD_HandleTypeDef *hhcd, uint8_t chnum)
870903
}
871904
else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_CHH)
872905
{
873-
__HAL_HCD_MASK_HALT_HC_INT(chnum);
906+
int reactivate = 0;
907+
__HAL_HCD_MASK_HALT_HC_INT(chnum);
874908

875909
if(hhcd->hc[chnum].state == HC_XFRC)
876910
{
@@ -894,15 +928,16 @@ static void HCD_HC_IN_IRQHandler (HCD_HandleTypeDef *hhcd, uint8_t chnum)
894928
{
895929
hhcd->hc[chnum].urb_state = URB_NOTREADY;
896930
}
897-
931+
898932
/* re-activate the channel */
899933
tmpreg = USBx_HC(chnum)->HCCHAR;
900934
tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
901935
tmpreg |= USB_OTG_HCCHAR_CHENA;
902-
USBx_HC(chnum)->HCCHAR = tmpreg;
936+
USBx_HC(chnum)->HCCHAR = tmpreg;
937+
reactivate = 1;
903938
}
904939
__HAL_HCD_CLEAR_HC_INT(chnum, USB_OTG_HCINT_CHH);
905-
HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
940+
if (reactivate == 0 )HAL_HCD_HC_NotifyURBChange_Callback(hhcd, chnum, hhcd->hc[chnum].urb_state);
906941
}
907942

908943
else if ((USBx_HC(chnum)->HCINT) & USB_OTG_HCINT_TXERR)

targets/TARGET_STM/TARGET_STM32F7/device/stm32f7xx_ll_usb.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,8 @@ HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_HCTypeDe
15201520

15211521
/* Write packet into the Tx FIFO. */
15221522
USB_WritePacket(USBx, hc->xfer_buff, hc->ch_num, hc->xfer_len, 0);
1523+
hc->xfer_count = hc->xfer_len;
1524+
15231525
}
15241526
}
15251527

0 commit comments

Comments
 (0)