@@ -567,28 +567,61 @@ void NetSIO::updateFIFO()
567567 return ;
568568}
569569
570+ void NetSIO::handle_write_sync (uint8_t c)
571+ {
572+ // handle pending sync request
573+ if (_sync_write_size < 0 )
574+ {
575+ // SYNC RESPONSE
576+ // send byte (should be ACK/NAK) bundled in sync response
577+ sendSyncResponse (NETSIO_ACK_SYNC, c, 0 );
578+ return ;
579+ }
580+ else
581+ {
582+ // sio_late_ack() was not from whatever reason followed by bus_to_peripheral()
583+ Debug_println (" Warn: NetSIO late ACK without bus_to_peripheral" );
584+ // send late ACK byte
585+ sendSyncResponse (NETSIO_ACK_SYNC, _sync_ack_byte, _sync_write_size);
586+ }
587+
588+ return ;
589+ }
590+
570591size_t NetSIO::dataOut (const void *buffer, size_t size)
571592{
572593 int result;
573594 int to_send;
574595 int txbytes = 0 ;
575596 uint8_t txbuf[513 ];
597+ uint8_t *ptr = (uint8_t *) buffer;
576598
577599 if (!_initialized)
578600 return 0 ;
579601
580- while (txbytes < size)
602+ if (size && _sync_request_num >= 0 )
603+ {
604+ handle_write_sync (ptr[0 ]);
605+ ptr++;
606+ size--;
607+ }
608+
609+ while (size)
581610 {
582611 // send block
583- to_send = (( size-txbytes) > sizeof (txbuf)- 1 ) ? sizeof (txbuf)- 1 : (size-txbytes );
612+ to_send = std::min ( size, sizeof (txbuf) - 1 );
584613 txbuf[0 ] = NETSIO_DATA_BLOCK;
585- memcpy (txbuf+1 , (( uint8_t *)buffer) +txbytes, to_send);
614+ memcpy (txbuf+1 , ptr +txbytes, to_send);
586615 // ? calculate credit based on amount of data ?
587616 if (!wait_for_credit (1 ))
588617 break ;
589618 result = write_sock (txbuf, to_send+1 );
590619 if (result > 0 )
591- txbytes += result-1 ;
620+ {
621+ result--;
622+ txbytes += result;
623+ size -= result;
624+ }
592625 else if (result < 0 )
593626 break ;
594627 }
0 commit comments