@@ -116,6 +116,7 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
116
116
117
117
#if defined (USB )
118
118
static HAL_StatusTypeDef PCD_EP_ISR_Handler (PCD_HandleTypeDef * hpcd );
119
+ static HAL_StatusTypeDef HAL_PCD_EP_ReceiveData (PCD_HandleTypeDef * hpcd , PCD_EPTypeDef * ep )
119
120
#endif /* USB */
120
121
/**
121
122
* @}
@@ -190,6 +191,7 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
190
191
hpcd -> IN_ep [index ].maxpacket = 0U ;
191
192
hpcd -> IN_ep [index ].xfer_buff = 0U ;
192
193
hpcd -> IN_ep [index ].xfer_len = 0U ;
194
+ hpcd -> IN_ep [index ].pending = 0U ;
193
195
}
194
196
195
197
for (index = 0U ; index < 15U ; index ++ )
@@ -202,6 +204,7 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
202
204
hpcd -> OUT_ep [index ].maxpacket = 0U ;
203
205
hpcd -> OUT_ep [index ].xfer_buff = 0U ;
204
206
hpcd -> OUT_ep [index ].xfer_len = 0U ;
207
+ hpcd -> OUT_ep [index ].pending = 0U ;
205
208
}
206
209
207
210
/* Init Device */
@@ -907,7 +910,6 @@ HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
907
910
return HAL_OK ;
908
911
}
909
912
910
-
911
913
/**
912
914
* @brief Receive an amount of data
913
915
* @param hpcd: PCD handle
@@ -919,7 +921,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
919
921
HAL_StatusTypeDef HAL_PCD_EP_Receive (PCD_HandleTypeDef * hpcd , uint8_t ep_addr , uint8_t * pBuf , uint32_t len )
920
922
{
921
923
PCD_EPTypeDef * ep = NULL ;
922
-
924
+
923
925
ep = & hpcd -> OUT_ep [ep_addr & 0x7FU ];
924
926
925
927
/*setup and start the Xfer */
@@ -933,7 +935,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u
933
935
}
934
936
else
935
937
{
936
- USB_EPStartXfer (hpcd -> Instance , ep );
938
+ HAL_PCD_EP_ReceiveData (hpcd , ep );
937
939
}
938
940
939
941
return HAL_OK ;
@@ -1192,7 +1194,7 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1192
1194
uint16_t count = 0 ;
1193
1195
uint16_t pmaBuffer = 0 ;
1194
1196
uint8_t epindex = 0 ;
1195
- __IO uint16_t wIstr = 0 ;
1197
+ __IO uint16_t wIstr = 0 ;
1196
1198
__IO uint16_t wEPVal = 0 ;
1197
1199
1198
1200
/* stay in loop while pending interrupts */
@@ -1220,14 +1222,14 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1220
1222
1221
1223
/* TX COMPLETE */
1222
1224
HAL_PCD_DataInStageCallback (hpcd , 0U );
1223
-
1224
-
1225
+
1226
+
1225
1227
if ((hpcd -> USB_Address > 0U )&& ( ep -> xfer_len == 0U ))
1226
1228
{
1227
1229
hpcd -> Instance -> DADDR = (hpcd -> USB_Address | USB_DADDR_EF );
1228
1230
hpcd -> USB_Address = 0U ;
1229
1231
}
1230
-
1232
+
1231
1233
}
1232
1234
else
1233
1235
{
@@ -1242,14 +1244,14 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1242
1244
{
1243
1245
/* Get SETUP Packet*/
1244
1246
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 );
1247
+ USB_ReadPMA (hpcd -> Instance , (uint8_t * )hpcd -> Setup ,ep -> pmaadress , ep -> xfer_count );
1246
1248
/* SETUP bit kept frozen while CTR_RX = 1*/
1247
1249
PCD_CLEAR_RX_EP_CTR (hpcd -> Instance , PCD_ENDP0 );
1248
1250
1249
1251
/* Process SETUP Packet*/
1250
1252
HAL_PCD_SetupStageCallback (hpcd );
1251
1253
}
1252
-
1254
+
1253
1255
else if ((wEPVal & USB_EP_CTR_RX ) != 0U )
1254
1256
{
1255
1257
PCD_CLEAR_RX_EP_CTR (hpcd -> Instance , PCD_ENDP0 );
@@ -1281,7 +1283,7 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1281
1283
/* clear int flag */
1282
1284
PCD_CLEAR_RX_EP_CTR (hpcd -> Instance , epindex );
1283
1285
ep = & hpcd -> OUT_ep [epindex ];
1284
-
1286
+
1285
1287
/* OUT double Buffering*/
1286
1288
if (ep -> doublebuffer == 0U )
1287
1289
{
@@ -1302,24 +1304,38 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1302
1304
count = (uint16_t ) PCD_GET_EP_DBUF1_CNT (hpcd -> Instance , ep -> num );
1303
1305
pmaBuffer = ep -> pmaaddr1 ;
1304
1306
}
1305
- PCD_FreeUserBuffer (hpcd -> Instance , ep -> num , PCD_EP_DBUF_OUT );
1307
+ if (ep -> xfer_len == 0 || ep -> xfer_buff == 0 )
1308
+ {
1309
+ /* have no buffer at all, leave data untouched and exit */
1310
+ ep -> pending = 1U ;
1311
+ return HAL_OK ;
1312
+ }
1313
+ PCD_FreeUserBuffer (hpcd -> Instance , ep -> num , PCD_EP_DBUF_OUT );
1306
1314
}
1315
+
1307
1316
if (count != 0U )
1308
1317
{
1318
+ /* buffer doesn't contains enough space, stall and error */
1319
+ if (count > ep -> xfer_len )
1320
+ {
1321
+ USB_EPSetStall (hpcd -> Instance , ep );
1322
+ return HAL_ERROR ;
1323
+ }
1309
1324
USB_ReadPMA (hpcd -> Instance , ep -> xfer_buff , pmaBuffer , count );
1310
1325
}
1311
1326
/*multi-packet on the NON control OUT endpoint*/
1312
1327
ep -> xfer_count += count ;
1313
1328
ep -> xfer_buff += count ;
1314
-
1329
+ ep -> xfer_len -= count ;
1330
+
1315
1331
if ((ep -> xfer_len == 0U ) || (count < ep -> maxpacket ))
1316
1332
{
1317
1333
/* RX COMPLETE */
1318
1334
HAL_PCD_DataOutStageCallback (hpcd , ep -> num );
1319
1335
}
1320
- else
1336
+ else if ( ep -> doublebuffer == 0U )
1321
1337
{
1322
- HAL_PCD_EP_Receive (hpcd , ep -> num , ep -> xfer_buff , ep -> xfer_len );
1338
+ PCD_SET_EP_RX_STATUS (hpcd -> Instance , ep -> num , USB_EP_RX_VALID );
1323
1339
}
1324
1340
1325
1341
} /* if((wEPVal & EP_CTR_RX) */
@@ -1369,6 +1385,57 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
1369
1385
}
1370
1386
return HAL_OK ;
1371
1387
}
1388
+
1389
+ HAL_StatusTypeDef HAL_PCD_EP_ReceiveData (PCD_HandleTypeDef * hpcd , PCD_EPTypeDef * ep )
1390
+ {
1391
+ uint16_t count ;
1392
+ uint16_t pmaBuffer ;
1393
+
1394
+ if (ep -> pending )
1395
+ {
1396
+ ep -> pending = 0U ;
1397
+ PCD_FreeUserBuffer (hpcd -> Instance , ep -> num , ep -> is_in );
1398
+ if (PCD_GET_ENDPOINT (hpcd -> Instance , ep -> num ) & USB_EP_DTOG_RX )
1399
+ {
1400
+ /*read from endpoint BUF0Addr buffer*/
1401
+ count = (uint16_t ) PCD_GET_EP_DBUF0_CNT (hpcd -> Instance , ep -> num );
1402
+ pmaBuffer = ep -> pmaaddr0 ;
1403
+ }
1404
+ else
1405
+ {
1406
+ /*read from endpoint BUF1Addr buffer*/
1407
+ count = (uint16_t ) PCD_GET_EP_DBUF1_CNT (hpcd -> Instance , ep -> num );
1408
+ pmaBuffer = ep -> pmaaddr1 ;
1409
+ }
1410
+
1411
+ /* buffer doesn't contains enough space, stall and error */
1412
+ if (count > ep -> xfer_len )
1413
+ {
1414
+ USB_EPSetStall (hpcd -> Instance , ep );
1415
+ return HAL_ERROR ;
1416
+ }
1417
+
1418
+ USB_ReadPMA (hpcd -> Instance , ep -> xfer_buff , pmaBuffer , count );
1419
+
1420
+ /*multi-packet on the NON control OUT endpoint*/
1421
+ ep -> xfer_count += count ;
1422
+ ep -> xfer_buff += count ;
1423
+ ep -> xfer_len -= count ;
1424
+
1425
+ if ((ep -> xfer_len == 0U ) || (count < ep -> maxpacket ))
1426
+ {
1427
+ /* RX COMPLETE */
1428
+ HAL_PCD_DataOutStageCallback (hpcd , ep -> num );
1429
+ return HAL_OK ;
1430
+ }
1431
+ }
1432
+ if (ep -> doublebuffer == 0U )
1433
+ {
1434
+ PCD_SET_EP_RX_STATUS (hpcd -> Instance , ep -> num , USB_EP_RX_VALID );
1435
+ }
1436
+ return HAL_OK ;
1437
+ }
1438
+
1372
1439
#endif /* USB */
1373
1440
1374
1441
/**
0 commit comments