/**************************************
* Minimal binary transfer using 2 serial channels
* control channel receives requests to send or receive
* data channel sends or receives binary data
* This program is written for T4X and saves buffered
* data in either DMAMEM or EXTMEM
*/
const int ledpin = 13;
#define LEDON digitalWriteFast(ledpin, HIGH);
#define LEDOFF digitalWriteFast(ledpin, LOW);
#define BUFFSIZE 300*1024
uint8_t buffer[BUFFSIZE] DMAMEM;
elapsedMillis trtimer = 0;
void setup() {
Serial.begin(9600); // the control channel
SerialUSB1.begin(9600); // the data channel
delay(400);
pinMode(ledpin, OUTPUT);
Serial.println("Sending q");
memset(buffer, 0x44,BUFFSIZE);
}
// This global gets set by RcvBuffer call
uint32_t rcvbytes = 100;
void loop() {
char ch;
if (Serial.available()) {
ch = Serial.read();
switch (ch) {
case 's' :
SendBuffer(rcvbytes);
break;
case 'r' :
rcvbytes = RcvBuffer();
break;
} // end of switch(ch)
} // end of if(Serial.available())
}
void SendBuffer(uint32_t numbytes){
uint32_t datlen, endmillis;
float frate;
LEDON
trtimer = 0;
datlen = numbytes;
// Tell the PC how many bytes to expect
SerialUSB1.write((uint8_t *)&datlen,4);
// Now send the bytes from the buffer
// I write the whole thing in one chunk---just because I can!
SerialUSB1.write((unsigned char *)&buffer[0], datlen);
LEDOFF
endmillis = trtimer;
Serial.printf("Teensy Sent %lu bytes in %lu mSec ", datlen,endmillis);
frate = (float)datlen/1024.0/(endmillis /1024.0);
Serial.printf(" or %6.1f KBytes/Second\n", frate);
Serial.println("q");
}
// RcvBuffer expects a 4-byte unsigned long indicating the
// number of bytes to receive, then that many following
// bytes. The data is stored in buffer.
#define TIMEOUT 2000
uint32_t RcvBuffer(void){
uint32_t datlen, dcount, endmillis;
float frate;
dcount = 0;
do {
delay(1);
dcount ++;
} while((dcount <TIMEOUT) && (SerialUSB1.available()<4));
if(dcount >= TIMEOUT) {
Serial.println("Timeout waiting for data\n");
Serial.println("q"); // tells PC that we're done
return 0;
}
LEDON
trtimer = 0;
SerialUSB1.readBytes((char *)&datlen,4);
if(datlen > BUFFSIZE) datlen = BUFFSIZE;
// now read the data into buffer
SerialUSB1.readBytes((char *)&buffer, datlen);
endmillis = trtimer;
LEDOFF
Serial.printf("Teensy Received %lu bytes in %lu mSec. ", datlen,endmillis);
frate = (float)datlen/1024.0/(endmillis /1024.0);
Serial.printf(" or %6.1f KBytes/Second\n", frate);
// Uncomment the next line to have the teensy echo back 200 bytes
// PlayBuffer();
Serial.print("\nq");// Tell PC we're done
return datlen;
}
// Send back some buffer contentsin hex format over the control USB Serial link
void PlayBuffer(void){
uint16_t i;
for(i= 0; i< 200; i++){
Serial.printf("%02X ",buffer[i]);
}
Serial.println("Playback done. q");
}