@@ -117,6 +117,9 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
117
117
#if defined (USB )
118
118
static HAL_StatusTypeDef PCD_EP_ISR_Handler (PCD_HandleTypeDef * hpcd );
119
119
#endif /* USB */
120
+
121
+ static HAL_StatusTypeDef HAL_PCD_EP_ReceiveData (PCD_HandleTypeDef * hpcd , PCD_EPTypeDef * ep );
122
+
120
123
/**
121
124
* @}
122
125
*/
@@ -190,6 +193,7 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
190
193
hpcd -> IN_ep [index ].maxpacket = 0U ;
191
194
hpcd -> IN_ep [index ].xfer_buff = 0U ;
192
195
hpcd -> IN_ep [index ].xfer_len = 0U ;
196
+ hpcd -> IN_ep [index ].pending = 0U ;
193
197
}
194
198
195
199
for (index = 0U ; index < 15U ; index ++ )
@@ -202,6 +206,7 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
202
206
hpcd -> OUT_ep [index ].maxpacket = 0U ;
203
207
hpcd -> OUT_ep [index ].xfer_buff = 0U ;
204
208
hpcd -> OUT_ep [index ].xfer_len = 0U ;
209
+ hpcd -> OUT_ep [index ].pending = 0U ;
205
210
}
206
211
207
212
/* Init Device */
@@ -907,7 +912,6 @@ HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
907
912
return HAL_OK ;
908
913
}
909
914
910
-
911
915
/**
912
916
* @brief Receive an amount of data
913
917
* @param hpcd: PCD handle
@@ -919,7 +923,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
919
923
HAL_StatusTypeDef HAL_PCD_EP_Receive (PCD_HandleTypeDef * hpcd , uint8_t ep_addr , uint8_t * pBuf , uint32_t len )
920
924
{
921
925
PCD_EPTypeDef * ep = NULL ;
922
-
926
+
923
927
ep = & hpcd -> OUT_ep [ep_addr & 0x7FU ];
924
928
925
929
/*setup and start the Xfer */
@@ -933,7 +937,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u
933
937
}
934
938
else
935
939
{
936
- USB_EPStartXfer (hpcd -> Instance , ep );
940
+ HAL_PCD_EP_ReceiveData (hpcd , ep );
937
941
}
938
942
939
943
return HAL_OK ;
@@ -1178,6 +1182,12 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
1178
1182
1179
1183
return HAL_OK ;
1180
1184
}
1185
+
1186
+ HAL_StatusTypeDef HAL_PCD_EP_ReceiveData (PCD_HandleTypeDef * hpcd , PCD_EPTypeDef * ep )
1187
+ {
1188
+ USB_EPStartXfer (hpcd -> Instance , ep );
1189
+ }
1190
+
1181
1191
#endif /* USB_OTG_FS */
1182
1192
1183
1193
#if defined (USB )
@@ -1192,7 +1202,7 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1192
1202
uint16_t count = 0 ;
1193
1203
uint16_t pmaBuffer = 0 ;
1194
1204
uint8_t epindex = 0 ;
1195
- __IO uint16_t wIstr = 0 ;
1205
+ __IO uint16_t wIstr = 0 ;
1196
1206
__IO uint16_t wEPVal = 0 ;
1197
1207
1198
1208
/* stay in loop while pending interrupts */
@@ -1220,14 +1230,14 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1220
1230
1221
1231
/* TX COMPLETE */
1222
1232
HAL_PCD_DataInStageCallback (hpcd , 0U );
1223
-
1224
-
1233
+
1234
+
1225
1235
if ((hpcd -> USB_Address > 0U )&& ( ep -> xfer_len == 0U ))
1226
1236
{
1227
1237
hpcd -> Instance -> DADDR = (hpcd -> USB_Address | USB_DADDR_EF );
1228
1238
hpcd -> USB_Address = 0U ;
1229
1239
}
1230
-
1240
+
1231
1241
}
1232
1242
else
1233
1243
{
@@ -1242,14 +1252,14 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1242
1252
{
1243
1253
/* Get SETUP Packet*/
1244
1254
ep -> xfer_count = PCD_GET_EP_RX_CNT (hpcd -> Instance , ep -> num );
1245
- USB_ReadPMA (hpcd -> Instance , (uint8_t * )hpcd -> Setup ,ep -> pmaadress , ep -> xfer_count );
1255
+ USB_ReadPMA (hpcd -> Instance , (uint8_t * )hpcd -> Setup ,ep -> pmaadress , ep -> xfer_count );
1246
1256
/* SETUP bit kept frozen while CTR_RX = 1*/
1247
1257
PCD_CLEAR_RX_EP_CTR (hpcd -> Instance , PCD_ENDP0 );
1248
1258
1249
1259
/* Process SETUP Packet*/
1250
1260
HAL_PCD_SetupStageCallback (hpcd );
1251
1261
}
1252
-
1262
+
1253
1263
else if ((wEPVal & USB_EP_CTR_RX ) != 0U )
1254
1264
{
1255
1265
PCD_CLEAR_RX_EP_CTR (hpcd -> Instance , PCD_ENDP0 );
@@ -1281,7 +1291,7 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1281
1291
/* clear int flag */
1282
1292
PCD_CLEAR_RX_EP_CTR (hpcd -> Instance , epindex );
1283
1293
ep = & hpcd -> OUT_ep [epindex ];
1284
-
1294
+
1285
1295
/* OUT double Buffering*/
1286
1296
if (ep -> doublebuffer == 0U )
1287
1297
{
@@ -1302,24 +1312,38 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1302
1312
count = (uint16_t ) PCD_GET_EP_DBUF1_CNT (hpcd -> Instance , ep -> num );
1303
1313
pmaBuffer = ep -> pmaaddr1 ;
1304
1314
}
1305
- PCD_FreeUserBuffer (hpcd -> Instance , ep -> num , PCD_EP_DBUF_OUT );
1315
+ if (ep -> xfer_len == 0 || ep -> xfer_buff == 0 )
1316
+ {
1317
+ /* have no buffer at all, leave data untouched and exit */
1318
+ ep -> pending = 1U ;
1319
+ return HAL_OK ;
1320
+ }
1321
+ PCD_FreeUserBuffer (hpcd -> Instance , ep -> num , PCD_EP_DBUF_OUT );
1306
1322
}
1323
+
1307
1324
if (count != 0U )
1308
1325
{
1326
+ /* buffer doesn't contains enough space, stall and error */
1327
+ if (count > ep -> xfer_len )
1328
+ {
1329
+ USB_EPSetStall (hpcd -> Instance , ep );
1330
+ return HAL_ERROR ;
1331
+ }
1309
1332
USB_ReadPMA (hpcd -> Instance , ep -> xfer_buff , pmaBuffer , count );
1310
1333
}
1311
1334
/*multi-packet on the NON control OUT endpoint*/
1312
1335
ep -> xfer_count += count ;
1313
1336
ep -> xfer_buff += count ;
1314
-
1337
+ ep -> xfer_len -= count ;
1338
+
1315
1339
if ((ep -> xfer_len == 0U ) || (count < ep -> maxpacket ))
1316
1340
{
1317
1341
/* RX COMPLETE */
1318
1342
HAL_PCD_DataOutStageCallback (hpcd , ep -> num );
1319
1343
}
1320
- else
1344
+ else if ( ep -> doublebuffer == 0U )
1321
1345
{
1322
- HAL_PCD_EP_Receive (hpcd , ep -> num , ep -> xfer_buff , ep -> xfer_len );
1346
+ PCD_SET_EP_RX_STATUS (hpcd -> Instance , ep -> num , USB_EP_RX_VALID );
1323
1347
}
1324
1348
1325
1349
} /* if((wEPVal & EP_CTR_RX) */
@@ -1369,6 +1393,57 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1369
1393
}
1370
1394
return HAL_OK ;
1371
1395
}
1396
+
1397
+ HAL_StatusTypeDef HAL_PCD_EP_ReceiveData (PCD_HandleTypeDef * hpcd , PCD_EPTypeDef * ep )
1398
+ {
1399
+ uint16_t count ;
1400
+ uint16_t pmaBuffer ;
1401
+
1402
+ if (ep -> pending )
1403
+ {
1404
+ ep -> pending = 0U ;
1405
+ PCD_FreeUserBuffer (hpcd -> Instance , ep -> num , ep -> is_in );
1406
+ if (PCD_GET_ENDPOINT (hpcd -> Instance , ep -> num ) & USB_EP_DTOG_RX )
1407
+ {
1408
+ /*read from endpoint BUF0Addr buffer*/
1409
+ count = (uint16_t ) PCD_GET_EP_DBUF0_CNT (hpcd -> Instance , ep -> num );
1410
+ pmaBuffer = ep -> pmaaddr0 ;
1411
+ }
1412
+ else
1413
+ {
1414
+ /*read from endpoint BUF1Addr buffer*/
1415
+ count = (uint16_t ) PCD_GET_EP_DBUF1_CNT (hpcd -> Instance , ep -> num );
1416
+ pmaBuffer = ep -> pmaaddr1 ;
1417
+ }
1418
+
1419
+ /* buffer doesn't contains enough space, stall and error */
1420
+ if (count > ep -> xfer_len )
1421
+ {
1422
+ USB_EPSetStall (hpcd -> Instance , ep );
1423
+ return HAL_ERROR ;
1424
+ }
1425
+
1426
+ USB_ReadPMA (hpcd -> Instance , ep -> xfer_buff , pmaBuffer , count );
1427
+
1428
+ /*multi-packet on the NON control OUT endpoint*/
1429
+ ep -> xfer_count += count ;
1430
+ ep -> xfer_buff += count ;
1431
+ ep -> xfer_len -= count ;
1432
+
1433
+ if ((ep -> xfer_len == 0U ) || (count < ep -> maxpacket ))
1434
+ {
1435
+ /* RX COMPLETE */
1436
+ HAL_PCD_DataOutStageCallback (hpcd , ep -> num );
1437
+ return HAL_OK ;
1438
+ }
1439
+ }
1440
+ if (ep -> doublebuffer == 0U )
1441
+ {
1442
+ PCD_SET_EP_RX_STATUS (hpcd -> Instance , ep -> num , USB_EP_RX_VALID );
1443
+ }
1444
+ return HAL_OK ;
1445
+ }
1446
+
1372
1447
#endif /* USB */
1373
1448
1374
1449
/**
0 commit comments