Skip to content

Commit b7d1a7a

Browse files
committed
Merge pull request ARMmbed#1381 from TomoYamanaka/master
In USB Host, fix bugs of device insertion and removal and isochronous transfer.
2 parents 3f2632b + 0046b63 commit b7d1a7a

File tree

3 files changed

+78
-83
lines changed

3 files changed

+78
-83
lines changed

libraries/USBHost/USBHost/TARGET_RENESAS/TARGET_RZ_A1H/ohci_wrapp_RZ_A1.c

Lines changed: 67 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ static int32_t iso_trans_doing(hced_t *p_ed, uint32_t index);
199199
static void chk_iso_td_done(genelal_ed_t *p_g_ed);
200200
static int32_t chk_iso_ed(genelal_ed_t *p_g_ed);
201201
static void iso_trans_setting(genelal_ed_t *p_g_ed, uint32_t index);
202-
static uint32_t iso_chk_starting_frame(genelal_ed_t *p_g_ed);
203202
static void iso_trans(genelal_ed_t *p_g_ed);
204203
#endif
205204
static void connect_check(void);
@@ -215,6 +214,7 @@ static usb_ohci_reg_t *p_usb_reg = &usb_reg;
215214
static usbisr_fnc_t *p_usbisr_cb = NULL;
216215
static osSemaphoreId semid_cb = NULL;
217216
static uint32_t connect_change = 0xFFFFFFFF;
217+
static uint32_t connect_status = 0;
218218
static uint32_t init_end = 0;
219219
static genelal_ed_t ctl_ed;
220220
static genelal_ed_t blk_ed;
@@ -283,13 +283,13 @@ osThreadDef(int_ed_task_4, osPriorityNormal, 512);
283283
static void iso_ed_task_1(void const * argument) {
284284
iso_ed_task(argument);
285285
}
286-
osThreadDef(iso_ed_task_1, osPriorityNormal, 512);
286+
osThreadDef(iso_ed_task_1, osPriorityAboveNormal, 512);
287287
#endif
288288
#if (ISO_TRANS_MAX_NUM >= 2)
289289
static void iso_ed_task_2(void const * argument) {
290290
iso_ed_task(argument);
291291
}
292-
osThreadDef(iso_ed_task_2, osPriorityNormal, 512);
292+
osThreadDef(iso_ed_task_2, osPriorityAboveNormal, 512);
293293
#endif
294294

295295
void ohciwrapp_init(usbisr_fnc_t *p_usbisr_fnc) {
@@ -878,25 +878,28 @@ static void control_trans(genelal_ed_t *p_g_ed) {
878878
p_g_ed->pipe_no = USB_HOST_PIPE0;
879879

880880
p_g_ed->trans_wait = 1;
881-
882-
if (td_info.direction == 0) {
883-
uint16_t Req = (p_td->currBufPtr[1] << 8) + p_td->currBufPtr[0];
884-
uint16_t Val = (p_td->currBufPtr[3] << 8) + p_td->currBufPtr[2];
885-
uint16_t Indx = (p_td->currBufPtr[5] << 8) + p_td->currBufPtr[4];
886-
uint16_t Len = (p_td->currBufPtr[7] << 8) + p_td->currBufPtr[6];
887-
888-
g_usbx_host_data_pointer[USB_HOST_PIPE0] = p_td->bufEnd;
889-
usbx_host_SetupStage(Req, Val, Indx, Len);
890-
} else if (td_info.direction == 1) {
891-
usbx_host_CtrlWriteStart(td_info.count, p_td->currBufPtr);
881+
if (connect_status == 0) {
882+
ohciwrapp_loc_TransEnd(p_g_ed->pipe_no, TD_CC_DEVICENOTRESPONDING);
892883
} else {
893-
usbx_host_CtrlReadStart(td_info.count, p_td->currBufPtr);
894-
}
884+
if (td_info.direction == 0) {
885+
uint16_t Req = (p_td->currBufPtr[1] << 8) + p_td->currBufPtr[0];
886+
uint16_t Val = (p_td->currBufPtr[3] << 8) + p_td->currBufPtr[2];
887+
uint16_t Indx = (p_td->currBufPtr[5] << 8) + p_td->currBufPtr[4];
888+
uint16_t Len = (p_td->currBufPtr[7] << 8) + p_td->currBufPtr[6];
889+
890+
g_usbx_host_data_pointer[USB_HOST_PIPE0] = p_td->bufEnd;
891+
usbx_host_SetupStage(Req, Val, Indx, Len);
892+
} else if (td_info.direction == 1) {
893+
usbx_host_CtrlWriteStart(td_info.count, p_td->currBufPtr);
894+
} else {
895+
usbx_host_CtrlReadStart(td_info.count, p_td->currBufPtr);
896+
}
895897

896-
(void)osSemaphoreWait(p_g_ed->semid_wait, CTL_TRANS_TIMEOUT);
897-
if (p_g_ed->trans_wait == 1) {
898-
p_g_ed->trans_wait = 0;
899-
RZA_IO_RegWrite_32(&p_td->control, TD_CC_DEVICENOTRESPONDING, TD_CTL_SHFT_CC, TD_CTL_MSK_CC);
898+
(void)osSemaphoreWait(p_g_ed->semid_wait, CTL_TRANS_TIMEOUT);
899+
if (p_g_ed->trans_wait == 1) {
900+
p_g_ed->trans_wait = 0;
901+
RZA_IO_RegWrite_32(&p_td->control, TD_CC_DEVICENOTRESPONDING, TD_CTL_SHFT_CC, TD_CTL_MSK_CC);
902+
}
900903
}
901904

902905
g_usbx_host_CmdStage &= (~USB_HOST_CMD_FIELD);
@@ -928,14 +931,18 @@ static void bulk_trans(genelal_ed_t *p_g_ed) {
928931
set_togle(p_g_ed->pipe_no, p_td, p_ed);
929932

930933
p_g_ed->trans_wait = 1;
931-
if (td_info.direction == 1) {
932-
usbx_host_start_send_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
934+
if (connect_status == 0) {
935+
ohciwrapp_loc_TransEnd(p_g_ed->pipe_no, TD_CC_DEVICENOTRESPONDING);
933936
} else {
934-
usbx_host_start_receive_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
935-
}
937+
if (td_info.direction == 1) {
938+
usbx_host_start_send_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
939+
} else {
940+
usbx_host_start_receive_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
941+
}
936942

937-
(void)osSemaphoreWait(p_g_ed->semid_wait, BLK_TRANS_TIMEOUT);
938-
usbx_host_stop_transfer(p_g_ed->pipe_no);
943+
(void)osSemaphoreWait(p_g_ed->semid_wait, BLK_TRANS_TIMEOUT);
944+
usbx_host_stop_transfer(p_g_ed->pipe_no);
945+
}
939946
}
940947

941948
static void int_trans_setting(genelal_ed_t *p_g_ed, uint32_t index) {
@@ -1023,10 +1030,14 @@ static void int_trans(genelal_ed_t *p_g_ed) {
10231030

10241031
get_td_info(p_g_ed, &td_info);
10251032
p_g_ed->trans_wait = 1;
1026-
if (td_info.direction == 1) {
1027-
usbx_host_start_send_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
1033+
if (connect_status == 0) {
1034+
ohciwrapp_loc_TransEnd(p_g_ed->pipe_no, TD_CC_DEVICENOTRESPONDING);
10281035
} else {
1029-
usbx_host_start_receive_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
1036+
if (td_info.direction == 1) {
1037+
usbx_host_start_send_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
1038+
} else {
1039+
usbx_host_start_receive_transfer(p_g_ed->pipe_no, td_info.count, p_td->currBufPtr);
1040+
}
10301041
}
10311042
}
10321043

@@ -1089,7 +1100,6 @@ static void set_togle(uint32_t pipe, hctd_t *p_td, hced_t *p_ed) {
10891100
static void iso_ed_task(void const * argument) {
10901101
genelal_ed_t *p_iso_ed = &iso_ed[(uint32_t)argument];
10911102
uint32_t wait_cnt = 0;
1092-
uint32_t wait_time = 0;
10931103
hcca_t *p_hcca;
10941104
hced_t *p_ed;
10951105

@@ -1115,17 +1125,26 @@ static void iso_ed_task(void const * argument) {
11151125
if (p_iso_ed->p_curr_ed != NULL) {
11161126
while ((p_usb_reg->HcControl & OR_CONTROL_IE) != 0) {
11171127
if (chk_iso_ed(p_iso_ed) != 0) {
1118-
wait_time = iso_chk_starting_frame(p_iso_ed);
1119-
if (wait_time != 0) {
1120-
osDelay(wait_time);
1121-
p_usb_reg->HcFmNumber += wait_time;
1122-
p_usb_reg->HcFmNumber &= 0x0000FFFF;
1128+
hcisotd_t *p_isotd = (hcisotd_t *)p_iso_ed->p_curr_td;
1129+
uint32_t starting_frame = p_isotd->control & 0x0000FFFF;
1130+
uint32_t wait_time = 0;
1131+
uint32_t wk_HcFmNumber = p_usb_reg->HcFmNumber;
1132+
1133+
if (starting_frame > wk_HcFmNumber) {
1134+
wait_time = starting_frame - wk_HcFmNumber;
1135+
} else {
1136+
wait_time = (0xFFFF - wk_HcFmNumber) + starting_frame;
1137+
}
1138+
if ((wait_time >= 2) && (wait_time <= 1000)) {
1139+
for (int cnt = 0; cnt < (wait_time - 1); cnt++) {
1140+
osDelay(1);
1141+
p_usb_reg->HcFmNumber = (wk_HcFmNumber + cnt) & 0x0000FFFF;
1142+
}
11231143
}
11241144
p_iso_ed->psw_idx = 0;
11251145
iso_trans(p_iso_ed);
11261146
(void)osSemaphoreWait(p_iso_ed->semid_wait, osWaitForever);
1127-
usbx_host_stop_transfer(p_iso_ed->pipe_no);
1128-
wait_cnt = 1;
1147+
wait_cnt = 8;
11291148
} else {
11301149
if (wait_cnt > 0) {
11311150
wait_cnt--;
@@ -1234,18 +1253,6 @@ static void iso_trans_setting(genelal_ed_t *p_g_ed, uint32_t index) {
12341253
usbx_api_host_SetEndpointTable(td_info.devadr, user_table, wk_table);
12351254
}
12361255

1237-
static uint32_t iso_chk_starting_frame(genelal_ed_t *p_g_ed) {
1238-
hcisotd_t *p_isotd = (hcisotd_t *)p_g_ed->p_curr_td;
1239-
uint32_t starting_frame = p_isotd->control & 0x0000FFFF;
1240-
uint32_t wait_time = 0;
1241-
1242-
if ((p_g_ed->psw_idx == 0) && (starting_frame > p_usb_reg->HcFmNumber)) {
1243-
wait_time = starting_frame - p_usb_reg->HcFmNumber;
1244-
}
1245-
1246-
return wait_time;
1247-
}
1248-
12491256
static void iso_trans(genelal_ed_t *p_g_ed) {
12501257
hcisotd_t *p_isotd = (hcisotd_t *)p_g_ed->p_curr_td;
12511258
tdinfo_t td_info;
@@ -1268,10 +1275,14 @@ static void iso_trans(genelal_ed_t *p_g_ed) {
12681275

12691276
get_td_info(p_g_ed, &td_info);
12701277
p_g_ed->trans_wait = 1;
1271-
if (td_info.direction == 1) {
1272-
usbx_host_start_send_transfer(p_g_ed->pipe_no, data_size, (uint8_t *)buff_addr);
1278+
if (connect_status == 0) {
1279+
ohciwrapp_loc_TransEnd(p_g_ed->pipe_no, TD_CC_DEVICENOTRESPONDING);
12731280
} else {
1274-
usbx_host_start_receive_transfer(p_g_ed->pipe_no, data_size, (uint8_t *)buff_addr);
1281+
if (td_info.direction == 1) {
1282+
usbx_host_start_send_transfer(p_g_ed->pipe_no, data_size, (uint8_t *)buff_addr);
1283+
} else {
1284+
usbx_host_start_receive_transfer(p_g_ed->pipe_no, data_size, (uint8_t *)buff_addr);
1285+
}
12751286
}
12761287
}
12771288
#endif
@@ -1326,6 +1337,7 @@ static void connect_check(void) {
13261337
void ohciwrapp_loc_Connect(uint32_t type) {
13271338
uint32_t cnt;
13281339

1340+
connect_status = type;
13291341
connect_change = type;
13301342
if (type == 0) {
13311343
if (ctl_ed.trans_wait == 1) {
@@ -1445,11 +1457,7 @@ void ohciwrapp_loc_TransEnd(uint32_t pipe, uint32_t ConditionCode) {
14451457

14461458
if (p_isotd != NULL) {
14471459
usbx_host_stop_transfer(pipe);
1448-
if (p_usb_reg->HcFmNumber < 0x0000FFFF) {
1449-
p_usb_reg->HcFmNumber++;
1450-
} else {
1451-
p_usb_reg->HcFmNumber = 0;
1452-
}
1460+
p_usb_reg->HcFmNumber = ((p_isotd->control & 0x0000FFFF) + p_wait_ed->psw_idx) & 0x0000FFFF;
14531461

14541462
/* Size of packet */
14551463
p_isotd->offsetPSW[p_wait_ed->psw_idx] -= g_usbx_host_data_count[pipe];
@@ -1469,10 +1477,8 @@ void ohciwrapp_loc_TransEnd(uint32_t pipe, uint32_t ConditionCode) {
14691477
p_wait_ed->psw_idx++;
14701478
}
14711479
if (chk_iso_ed(p_wait_ed) != 0) {
1472-
if (iso_chk_starting_frame(p_wait_ed) == 0) {
1473-
iso_trans(p_wait_ed);
1474-
next_trans = 1;
1475-
}
1480+
iso_trans(p_wait_ed);
1481+
next_trans = 1;
14761482
}
14771483
if (next_trans == 0) {
14781484
p_wait_ed->trans_wait = 0;

libraries/USBHost/USBHost/TARGET_RENESAS/TARGET_RZ_A1H/ohci_wrapp_pipe.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ Exported global variables and functions (to be accessed by other files)
5151
/* Endpoint Configuration Data Format */
5252
/********************************************************************************************************/
5353
/* LINE1: Pipe Window Select Register */
54-
/* CPU Access PIPE : PIPE1 to PIPE9 [ ### SET ### ] */
54+
/* CPU Access PIPE : USB_HOST_PIPE1 to USB_HOST_PIPE9 [ ### SET ### ] */
5555
/* LINE2: Pipe Configuration Register */
5656
/* Transfer Type : USB_HOST_NONE [ USB_HOST_NONE ] */
5757
/* Buffer Ready interrupt : USB_HOST_NONE [ USB_HOST_NONE ] */
58-
/* Double Buffer Mode : USB_HOST_CNT_ON / USB_HOST_CNT_OFF [ ### SET ### ] */
59-
/* Continuous Transmit: : USB_HOST_CNT_ON / USB_HOST_CNT_OFF [ ### SET ### ] */
58+
/* Double Buffer Mode : USB_HOST_DBLBON / USB_HOST_DBLBOFF [ ### SET ### ] */
59+
/* Continuous Transmit: : USB_HOST_CNTMDON / USB_HOST_CNTMDOFF [ ### SET ### ] */
6060
/* Short NAK : USB_HOST_NONE [ USB_HOST_NONE ] */
6161
/* Transfer Direction : USB_HOST_NONE [ USB_HOST_NONE ] */
6262
/* Endpoint Number : USB_HOST_NONE [ USB_HOST_NONE ] */
@@ -72,8 +72,7 @@ Exported global variables and functions (to be accessed by other files)
7272
/* LINE6: use FIFO port */
7373
/* : USB_HOST_CUSE [ ### SET ### ] */
7474
/* : USB_HOST_D0USE / USB_HOST_D1USE */
75-
/* : USB_HOST_D0DMA / USB_HOST_D0DMA */
76-
/* LINE7: use FIFO port Endian : USB_HOST_FIFO_BIG / USB_HOST_FIFO_LITTLE [ #SET# ] */
75+
/* : USB_HOST_D0DMA / USB_HOST_D0DMA */
7776
/********************************************************************************************************/
7877

7978
/* Device Address 1 */
@@ -105,7 +104,7 @@ USB_HOST_CFG_PIPETBL_t usb_host_int_ep_tbl1[ ] =
105104
{
106105
USB_HOST_PIPE6,
107106
/* TYPE / BFRE / DBLB / CNTMD / SHTNAK / DIR / EPNUM */
108-
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBON | USB_HOST_CNTMDON | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
107+
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBOFF | USB_HOST_CNTMDOFF | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
109108
(uint16_t)((uint16_t)(((64) / 64) - 1) << 10) | (uint16_t)(40),
110109
USB_HOST_NONE,
111110
USB_HOST_NONE,
@@ -115,7 +114,7 @@ USB_HOST_CFG_PIPETBL_t usb_host_int_ep_tbl1[ ] =
115114
{
116115
USB_HOST_PIPE7,
117116
/* TYPE / BFRE / DBLB / CNTMD / SHTNAK / DIR / EPNUM */
118-
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBON | USB_HOST_CNTMDON | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
117+
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBOFF | USB_HOST_CNTMDOFF | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
119118
(uint16_t)((uint16_t)(((64) / 64) - 1) << 10) | (uint16_t)(41),
120119
USB_HOST_NONE,
121120
USB_HOST_NONE,
@@ -125,7 +124,7 @@ USB_HOST_CFG_PIPETBL_t usb_host_int_ep_tbl1[ ] =
125124
{
126125
USB_HOST_PIPE8,
127126
/* TYPE / BFRE / DBLB / CNTMD / SHTNAK / DIR / EPNUM */
128-
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBON | USB_HOST_CNTMDON | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
127+
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBOFF | USB_HOST_CNTMDOFF | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
129128
(uint16_t)((uint16_t)(((64) / 64) - 1) << 10) | (uint16_t)(42),
130129
USB_HOST_NONE,
131130
USB_HOST_NONE,
@@ -135,7 +134,7 @@ USB_HOST_CFG_PIPETBL_t usb_host_int_ep_tbl1[ ] =
135134
{
136135
USB_HOST_PIPE9,
137136
/* TYPE / BFRE / DBLB / CNTMD / SHTNAK / DIR / EPNUM */
138-
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBON | USB_HOST_CNTMDON | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
137+
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBOFF | USB_HOST_CNTMDOFF | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
139138
(uint16_t)((uint16_t)(((64) / 64) - 1) << 10) | (uint16_t)(43),
140139
USB_HOST_NONE,
141140
USB_HOST_NONE,
@@ -158,7 +157,7 @@ USB_HOST_CFG_PIPETBL_t usb_host_iso_ep_tbl1[ ] =
158157
{
159158
USB_HOST_PIPE1,
160159
/* TYPE / BFRE / DBLB / CNTMD / SHTNAK / DIR / EPNUM */
161-
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBON | USB_HOST_CNTMDON | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
160+
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBON | USB_HOST_CNTMDOFF | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
162161
(uint16_t)((uint16_t)(((1024) / 64) - 1) << 10) | (uint16_t)(44),
163162
USB_HOST_NONE,
164163
USB_HOST_NONE,
@@ -168,8 +167,8 @@ USB_HOST_CFG_PIPETBL_t usb_host_iso_ep_tbl1[ ] =
168167
{
169168
USB_HOST_PIPE2,
170169
/* TYPE / BFRE / DBLB / CNTMD / SHTNAK / DIR / EPNUM */
171-
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBON | USB_HOST_CNTMDON | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
172-
(uint16_t)((uint16_t)(((1024) / 64) - 1) << 10) | (uint16_t)(60),
170+
USB_HOST_NONE | USB_HOST_NONE | USB_HOST_DBLBON | USB_HOST_CNTMDOFF | USB_HOST_NONE | USB_HOST_NONE | USB_HOST_NONE,
171+
(uint16_t)((uint16_t)(((1024) / 64) - 1) << 10) | (uint16_t)(76),
173172
USB_HOST_NONE,
174173
USB_HOST_NONE,
175174
USB_HOST_D1USE

libraries/USBHost/USBHost/USBHALHost_RZ_A1.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,7 @@ void USBHALHost::UsbIrqhandler() {
261261

262262
//Root device disconnected
263263
else {
264-
265-
if (!(int_status & OR_INTR_STATUS_WDH)) {
266-
usb_hcca->DoneHead = 0;
267-
}
268-
269264
deviceDisconnected(0, 1, NULL, usb_hcca->DoneHead & 0xFFFFFFFE);
270-
271-
if (int_status & OR_INTR_STATUS_WDH) {
272-
usb_hcca->DoneHead = 0;
273-
ohciwrapp_reg_w(OHCI_REG_INTERRUPTSTATUS, OR_INTR_STATUS_WDH);
274-
}
275265
}
276266
}
277267
ohciwrapp_reg_w(OHCI_REG_RHPORTSTATUS1, OR_RH_PORT_CSC);

0 commit comments

Comments
 (0)