@@ -151,28 +151,28 @@ void Wifi::countMs()
151
151
{
152
152
// Process any queued packets
153
153
if (!packets.empty ())
154
- processPackets ();
154
+ receivePackets ();
155
155
156
156
if (wUsCountcnt) // Counter enable
157
157
{
158
- // Trigger a pre- beacon interrupt at the pre-beacon timestamp
159
- if (wBeaconCount == wPreBeacon)
158
+ // Decrement the beacon counter and trigger an interrupt if the pre-beacon value matches
159
+ if (-- wBeaconCount == wPreBeacon && wUsComparecnt )
160
160
sendInterrupt (15 );
161
161
162
- // Decrement the beacon millisecond counter and handle underflows
163
- if (-- wBeaconCount == 0 )
162
+ // Increment the main counter by a millisecond and handle compare events
163
+ if ((wUsCount += 0x400 ) == wUsCompare || wBeaconCount == 0 )
164
164
{
165
- // Trigger a beacon transfer and reload the millisecond counter
166
- if ((wTxbufLoc[4 ] & BIT (15 )) && (wTxreqRead & BIT (4 )))
167
- transfer (4 );
165
+ // Reload the beacon counter and trigger an interrupt with transmission if enabled
168
166
wBeaconCount = wBeaconInt;
169
-
170
- // Trigger an immediate beacon interrupt if enabled
171
167
if (wUsComparecnt)
168
+ {
172
169
sendInterrupt (14 );
170
+ if ((wTxbufLoc[4 ] & BIT (15 )) && (wTxreqRead & BIT (4 )))
171
+ transmitPacket (4 );
172
+ }
173
173
}
174
174
175
- // Trigger a post-beacon interrupt when the counter reaches zero
175
+ // Decrement the post-beacon counter until it reaches zero; then trigger an interrupt
176
176
if (wPostBeacon && --wPostBeacon == 0 )
177
177
sendInterrupt (13 );
178
178
}
@@ -201,69 +201,87 @@ void Wifi::sendInterrupt(int bit)
201
201
}
202
202
}
203
203
204
- void Wifi::processPackets ()
204
+ void Wifi::receivePackets ()
205
205
{
206
+ // Start receiving packets
207
+ sendInterrupt (6 );
206
208
mutex.lock ();
207
209
208
210
// Write all queued packets to the circular buffer
209
- for (size_t i = 0 ; i < packets.size (); i++)
211
+ for (uint32_t i = 0 ; i < packets.size (); i++)
210
212
{
211
213
uint16_t size = (packets[i][4 ] + 12 ) / 2 ;
212
-
213
- for (size_t j = 0 ; j < size; j++)
214
+ for (uint32_t j = 0 ; j < size; j++)
214
215
{
215
216
// Write a half-word of the packet to memory
216
217
core->memory .write <uint16_t >(1 , 0x4804000 + wRxbufWrcsr, packets[i][j]);
217
218
218
219
// Increment the circular buffer address
219
220
wRxbufWrcsr += 2 ;
220
- if ((wRxbufBegin & 0x1FFE ) != (wRxbufEnd & 0x1FFE ))
221
- wRxbufWrcsr = (wRxbufBegin & 0x1FFE ) + (wRxbufWrcsr - (wRxbufBegin & 0x1FFE )) % ((wRxbufEnd & 0x1FFE ) - (wRxbufBegin & 0x1FFE ));
222
- wRxbufWrcsr &= 0x1FFE ;
221
+ if (int bufSize = (wRxbufEnd & 0x1FFE ) - (wRxbufBegin & 0x1FFE ))
222
+ wRxbufWrcsr = ((wRxbufBegin & 0x1FFE ) + (wRxbufWrcsr - (wRxbufBegin & 0x1FFE )) % bufSize) & 0x1FFE ;
223
223
}
224
224
225
225
delete[] packets[i];
226
-
227
- // Trigger a receive complete interrupt
228
- sendInterrupt (0 );
229
226
}
230
227
228
+ // Finish receiving packets
231
229
packets.clear ();
232
230
mutex.unlock ();
231
+ sendInterrupt (0 );
233
232
}
234
233
235
- void Wifi::transfer (int index)
234
+ void Wifi::transmitPacket (int index)
236
235
{
237
236
uint16_t address = (wTxbufLoc[index ] & 0xFFF ) << 1 ;
238
237
uint16_t size = core->memory .read <uint16_t >(1 , 0x4804000 + address + 0x0A ) + 8 ;
239
- LOG ( " Sending packet on channel %d with size 0x%X\n " , index , size);
238
+ printf ( " Instance %d sending packet on channel %d with size 0x%X\n " , core-> id , index , size);
240
239
240
+ // Start transmitting a packet
241
+ sendInterrupt (7 );
241
242
mutex.lock ();
242
243
243
- for (size_t i = 0 ; i < connections.size (); i++)
244
+ for (uint32_t i = 0 ; i < connections.size (); i++)
244
245
{
245
246
// Read the packet from memory
246
247
uint16_t *data = new uint16_t [size / 2 ];
247
- for (size_t j = 0 ; j < size; j += 2 )
248
+ for (uint32_t j = 0 ; j < size; j += 2 )
248
249
data[j / 2 ] = core->memory .read <uint16_t >(1 , 0x4804000 + address + j);
249
250
250
- // Set the packet size in the outgoing header
251
- data[4 ] = size - 12 ;
251
+ // Fill out the hardware RX header
252
+ data[0 ] = 0x8010 | (index == 4 ); // Frame type
253
+ data[1 ] = 0x0040 ; // Something?
254
+ data[3 ] = 0x0010 ; // Transfer rate
255
+ data[4 ] = size - 12 ; // Data length
256
+ data[5 ] = 0x00FF ; // Signal strength
257
+
258
+ // Set and update the IEEE sequence number if enabled
259
+ if (index == 4 || !(wTxbufLoc[index ] & BIT (13 )))
260
+ data[17 ] = (wTxSeqno++) << 4 ;
252
261
253
262
// Add the packet to the queue
254
263
connections[i]->mutex .lock ();
255
264
connections[i]->packets .push_back (data);
256
265
connections[i]->mutex .unlock ();
257
266
}
258
267
268
+ // Finish transmitting a packet
259
269
mutex.unlock ();
270
+ sendInterrupt (1 );
260
271
261
272
// Clear the enable flag for non-beacons
262
273
if (index != 4 )
263
274
wTxbufLoc[index ] &= ~BIT (15 );
264
275
265
- // Trigger a transmit complete interrupt
266
- sendInterrupt (1 );
276
+ // Update transmission status based on type and certain bits
277
+ switch (index )
278
+ {
279
+ case 0 : wTxstat = (wTxstatCnt & BIT (14 )) ? 0x0801 : 0x0001 ; break ; // CMD
280
+ case 1 : wTxstat = (wTxbufLoc[1 ] & BIT (12 )) ? 0x0701 : 0x0001 ; break ; // LOC1
281
+ case 2 : wTxstat = (wTxbufLoc[2 ] & BIT (12 )) ? 0x1701 : 0x1001 ; break ; // LOC2
282
+ case 3 : wTxstat = (wTxbufLoc[3 ] & BIT (12 )) ? 0x2701 : 0x2001 ; break ; // LOC3
283
+ case 4 : wTxstat = (wTxstatCnt & BIT (15 )) ? 0x0301 : 0x0001 ; break ; // BEACON
284
+ }
267
285
}
268
286
269
287
void Wifi::writeWModeWep (uint16_t mask, uint16_t value)
@@ -272,6 +290,13 @@ void Wifi::writeWModeWep(uint16_t mask, uint16_t value)
272
290
wModeWep = (wModeWep & ~mask) | (value & mask);
273
291
}
274
292
293
+ void Wifi::writeWTxstatCnt (uint16_t mask, uint16_t value)
294
+ {
295
+ // Write to the W_TXSTAT_CNT register
296
+ mask &= 0xF000 ;
297
+ wTxstatCnt = (wTxstatCnt & ~mask) | (value & mask);
298
+ }
299
+
275
300
void Wifi::writeWIrf (uint16_t mask, uint16_t value)
276
301
{
277
302
// Write to the W_IF register
@@ -447,7 +472,7 @@ void Wifi::writeWTxbufLoc(int index, uint16_t mask, uint16_t value)
447
472
448
473
// Send a packet to connected cores if triggered for non-beacons
449
474
if (index != 4 && (wTxbufLoc[index ] & BIT (15 )) && (wTxreqRead & BIT (index )))
450
- transfer (index );
475
+ transmitPacket (index );
451
476
}
452
477
453
478
void Wifi::writeWBeaconInt (uint16_t mask, uint16_t value)
@@ -475,10 +500,8 @@ void Wifi::writeWTxreqSet(uint16_t mask, uint16_t value)
475
500
476
501
// Send a packet to connected cores if triggered for non-beacons
477
502
for (int i = 0 ; i < 4 ; i++)
478
- {
479
503
if ((wTxbufLoc[i] & BIT (15 )) && (wTxreqRead & BIT (i)))
480
- transfer (i);
481
- }
504
+ transmitPacket (i);
482
505
}
483
506
484
507
void Wifi::writeWUsCountcnt (uint16_t mask, uint16_t value)
@@ -499,6 +522,21 @@ void Wifi::writeWUsComparecnt(uint16_t mask, uint16_t value)
499
522
sendInterrupt (14 );
500
523
}
501
524
525
+ void Wifi::writeWUsCompare (int index, uint16_t mask, uint16_t value)
526
+ {
527
+ // Write to part of the W_US_COMPARE register
528
+ int shift = index * 16 ;
529
+ mask &= (index ? 0xFFFF : 0xFC00 );
530
+ wUsCompare = (wUsCompare & ~(uint64_t (mask) << shift)) | (uint64_t (value & mask) << shift);
531
+ }
532
+
533
+ void Wifi::writeWUsCount (int index, uint16_t mask, uint16_t value)
534
+ {
535
+ // Write to part of the W_US_COUNT register
536
+ int shift = index * 16 ;
537
+ wUsCount = (wUsCount & ~(uint64_t (mask) << shift)) | (uint64_t (value & mask) << shift);
538
+ }
539
+
502
540
void Wifi::writeWPreBeacon (uint16_t mask, uint16_t value)
503
541
{
504
542
// Write to the W_PRE_BEACON register
@@ -580,13 +618,11 @@ uint16_t Wifi::readWRxbufRdData()
580
618
// Increment the read address
581
619
if ((wRxbufRdAddr += 2 ) == wRxbufGap)
582
620
wRxbufRdAddr += wRxbufGapdisp << 1 ;
583
- if ((wRxbufBegin & 0x1FFE ) != (wRxbufEnd & 0x1FFE ))
584
- wRxbufRdAddr = (wRxbufBegin & 0x1FFE ) + (wRxbufRdAddr - (wRxbufBegin & 0x1FFE )) % ((wRxbufEnd & 0x1FFE ) - (wRxbufBegin & 0x1FFE ));
585
- wRxbufRdAddr &= 0x1FFF ;
621
+ if (int bufSize = (wRxbufEnd & 0x1FFE ) - (wRxbufBegin & 0x1FFE ))
622
+ wRxbufRdAddr = ((wRxbufBegin & 0x1FFE ) + (wRxbufRdAddr - (wRxbufBegin & 0x1FFE )) % bufSize) & 0x1FFE ;
586
623
587
624
// Decrement the read counter and trigger an interrupt at the end
588
625
if (wRxbufCount > 0 && --wRxbufCount == 0 )
589
626
sendInterrupt (9 );
590
-
591
627
return value;
592
628
}
0 commit comments