6
6
* Portions:
7
7
* Copyright (c) 2016 STMicroelectronics
8
8
* Copyright (c) 2019 Ha Thach (tinyusb.org)
9
+ * Copyright (c) 2022 Simon Küppers (skuep)
10
+ * Copyright (c) 2022 HiFiPhile
9
11
*
10
12
* Permission is hereby granted, free of charge, to any person obtaining a copy
11
13
* of this software and associated documentation files (the "Software"), to deal
@@ -987,7 +989,7 @@ bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16
987
989
uint8_t const dir = tu_edpt_dir (ep_addr );
988
990
989
991
xfer -> buffer = NULL ;
990
- xfer -> ff = ff ; // TODO support dcd_edpt_xfer_fifo API
992
+ xfer -> ff = ff ;
991
993
xfer -> total_len = total_bytes ;
992
994
xfer -> queued_len = 0 ;
993
995
@@ -1088,7 +1090,7 @@ static bool dcd_write_packet_memory(uint16_t dst, const void *__restrict src, si
1088
1090
if (wNBytes & 0x01 )
1089
1091
{
1090
1092
temp1 = * srcVal ;
1091
- * pdwVal = temp2 ;
1093
+ * pdwVal = temp1 ;
1092
1094
}
1093
1095
1094
1096
return true;
@@ -1100,41 +1102,47 @@ static bool dcd_write_packet_memory(uint16_t dst, const void *__restrict src, si
1100
1102
* @param wNBytes no. of bytes to be copied.
1101
1103
* @retval None
1102
1104
*/
1103
-
1104
- // THIS FUNCTION IS UNTESTED
1105
-
1106
1105
static bool dcd_write_packet_memory_ff (tu_fifo_t * ff , uint16_t dst , uint16_t wNBytes )
1107
1106
{
1108
1107
// Since we copy from a ring buffer FIFO, a wrap might occur making it necessary to conduct two copies
1109
- // Check for first linear part
1110
1108
tu_fifo_buffer_info_t info ;
1111
- tu_fifo_get_read_info (ff , & info ); // We want to read from the FIFO
1112
- TU_VERIFY (info .len_lin && dcd_write_packet_memory (dst , info .ptr_lin , info .len_lin )); // and write it into the PMA
1113
- tu_fifo_advance_read_pointer (ff , info .len_lin );
1109
+ tu_fifo_get_read_info (ff , & info );
1110
+
1111
+ uint16_t cnt_lin = TU_MIN (wNBytes , info .len_lin );
1112
+ uint16_t cnt_wrap = TU_MIN (wNBytes - cnt_lin , info .len_wrap );
1113
+
1114
+ // We want to read from the FIFO and write it into the PMA, if LIN part is ODD and has WRAPPED part,
1115
+ // last lin byte will be combined with wrapped part
1116
+ // To ensure PMA is always access 16bit aligned (dst aligned to 16 bit)
1117
+ if ((cnt_lin & 0x01 ) && cnt_wrap )
1118
+ {
1119
+ // Copy first linear part
1120
+ dcd_write_packet_memory (dst , info .ptr_lin , cnt_lin & ~0x01 );
1121
+ dst += cnt_lin & ~0x01 ;
1122
+
1123
+ // Copy last linear byte & first wrapped byte
1124
+ uint16_t tmp = ((uint8_t * )info .ptr_lin )[cnt_lin - 1 ] | ((uint16_t )(((uint8_t * )info .ptr_wrap )[0 ]) << 8U );
1125
+ dcd_write_packet_memory (dst , & tmp , 2 );
1126
+ dst += 2 ;
1114
1127
1115
- // Check for wrapped part
1116
- if (info .len_wrap )
1128
+ // Copy rest of wrapped byte
1129
+ dcd_write_packet_memory (dst , ((uint8_t * )info .ptr_wrap ) + 1 , cnt_wrap - 1 );
1130
+ }
1131
+ else
1117
1132
{
1118
- // Update destination pointer
1133
+ // Copy linear part
1134
+ dcd_write_packet_memory (dst , info .ptr_lin , cnt_lin );
1119
1135
dst += info .len_lin ;
1120
- uint8_t * src = (uint8_t * )info .ptr_wrap ;
1121
- uint16_t len2 = info .len_wrap ;
1122
1136
1123
- // Since PMA is accessed 16-bit wise we need to handle the case when a 16 bit value was split
1124
- if (info .len_lin % 2 ) // If len is uneven there is a byte left to copy
1137
+ if (info .len_wrap )
1125
1138
{
1126
- TU_ASSERT (false); // TODO: Step through and check -> untested
1127
-
1128
- uint16_t temp = ((uint8_t * )info .ptr_lin )[info .len_lin - 1 ] | src [0 ] << 16 ; // CHECK endianess
1129
- pma [PMA_STRIDE * (dst >>1 )] = temp ;
1130
- src ++ ;
1131
- len2 -- ;
1139
+ // Copy wrapped byte
1140
+ dcd_write_packet_memory (dst , info .ptr_wrap , cnt_wrap );
1132
1141
}
1133
-
1134
- TU_VERIFY (dcd_write_packet_memory (dst , src , len2 ));
1135
- tu_fifo_advance_write_pointer (ff , info .len_wrap );
1136
1142
}
1137
1143
1144
+ tu_fifo_advance_read_pointer (ff , cnt_lin + cnt_wrap );
1145
+
1138
1146
return true;
1139
1147
}
1140
1148
@@ -1178,40 +1186,51 @@ static bool dcd_read_packet_memory(void *__restrict dst, uint16_t src, size_t wN
1178
1186
* @param wNBytes no. of bytes to be copied.
1179
1187
* @retval None
1180
1188
*/
1181
-
1182
- // THIS FUNCTION IS UNTESTED
1183
-
1184
1189
static bool dcd_read_packet_memory_ff (tu_fifo_t * ff , uint16_t src , uint16_t wNBytes )
1185
1190
{
1186
1191
// Since we copy into a ring buffer FIFO, a wrap might occur making it necessary to conduct two copies
1187
1192
// Check for first linear part
1188
1193
tu_fifo_buffer_info_t info ;
1189
1194
tu_fifo_get_write_info (ff , & info ); // We want to read from the FIFO
1190
1195
1191
- TU_VERIFY ( info . len_lin && dcd_read_packet_memory ( info . ptr_lin , src , info .len_lin ) );
1192
- tu_fifo_advance_write_pointer ( ff , info .len_lin );
1196
+ uint16_t cnt_lin = TU_MIN ( wNBytes , info .len_lin );
1197
+ uint16_t cnt_wrap = TU_MIN ( wNBytes - cnt_lin , info .len_wrap );
1193
1198
1194
- // Check for wrapped part
1195
- if (info .len_wrap )
1199
+ // We want to read from PMA and write it into the FIFO, if LIN part is ODD and has WRAPPED part,
1200
+ // last lin byte will be combined with wrapped part
1201
+ // To ensure PMA is always access 16bit aligned (src aligned to 16 bit)
1202
+ if ((cnt_lin & 0x01 ) && cnt_wrap )
1196
1203
{
1197
- // Update source pointer
1198
- src += info .len_lin ;
1204
+ // Copy first linear part
1205
+ dcd_read_packet_memory (info .ptr_lin , src , cnt_lin & ~0x01 );
1206
+ src += cnt_lin & ~0x01 ;
1207
+
1208
+ // Copy last linear byte & first wrapped byte
1209
+ uint16_t tmp ;
1210
+ dcd_read_packet_memory (& tmp , src , 2 );
1211
+
1212
+ ((uint8_t * )info .ptr_lin )[cnt_lin - 1 ] = (uint8_t )tmp ;
1213
+ ((uint8_t * )info .ptr_wrap )[0 ] = (uint8_t )(tmp >> 8U );
1214
+ src += 2 ;
1215
+
1216
+ // Copy rest of wrapped byte
1217
+ dcd_read_packet_memory (((uint8_t * )info .ptr_wrap ) + 1 , src , cnt_wrap - 1 );
1218
+ }
1219
+ else
1220
+ {
1221
+ // Copy linear part
1222
+ dcd_read_packet_memory (info .ptr_lin , src , cnt_lin );
1223
+ src += cnt_lin ;
1199
1224
1200
- // Since PMA is accessed 16-bit wise we need to handle the case when a 16 bit value was split
1201
- if (info .len_lin % 2 ) // If len is uneven there is a byte left to copy
1225
+ if (info .len_wrap )
1202
1226
{
1203
- TU_ASSERT (false); //TODO: step through -> untested
1204
- uint32_t temp = pma [PMA_STRIDE * (src >>1 )];
1205
- * ((uint8_t * )info .ptr_wrap ++ ) = ((temp >> 8 ) & 0xFF );
1206
- src ++ ;
1207
- tu_fifo_advance_write_pointer (ff , 1 );
1208
- info .len_wrap -- ;
1227
+ // Copy wrapped byte
1228
+ dcd_read_packet_memory (info .ptr_wrap , src , cnt_wrap );
1209
1229
}
1210
-
1211
- TU_VERIFY (dcd_read_packet_memory (info .ptr_wrap , src , info .len_wrap ));
1212
- tu_fifo_advance_write_pointer (ff , info .len_wrap );
1213
1230
}
1214
1231
1232
+ tu_fifo_advance_write_pointer (ff , cnt_lin + cnt_wrap );
1233
+
1215
1234
return true;
1216
1235
}
1217
1236
0 commit comments