//#DOC Main, program entry and main loop
#include "main.h"
#include <otSerial.h>
#include <otTimer.h>
#include <otPrintfLite.h>
#include <otUSB.h>

otUSB   usb; // Declare USB library

// Write a double word on USB channel, on the other side (PC) the sequence is checked for a complete test
FAST_CODE_1 void write(otUSB * usbHandler)
{
    // Get the number of bytes to transfer
    U32     n = usbHandler->getLong();
    // Convert in a number of double words
    n >>= 2;
    for(U32 i = 0; i < n; i++)
        usbHandler->putLong(i);
}

void setup()
{
    // Setup the UART0 used by printf
    UartEnableSignals(UART0);
    UartSetBaud(UART0, 115200);
    // Init printf
    setStdout(UART0);   // printf output is now directed on UART0
    DelayMs(1000);      // Wait something for TERMINAL start
    usb.init(32);       // Init USB channel, the command list can accept 32 different commands
    // Add the command used for this test
    U16 id = usb.addCallback(write);
    // Some diagnostic print
    printf("write id = %u\n", id);
    printf("Please, execute SharkDuino_USB_Test on connected PC!\n");
}

void loop()
{
    // Wait a command and execute it
    usb.process();
}

/*
 ********* OUTPUT ON PC ***********************************************

Loops = 10
[0000] : ET =   515 ms, BW =  7953 Kbyte/s, transferred     4096 Kbytes
[0001] : ET =   515 ms, BW =  7953 Kbyte/s, transferred     8192 Kbytes
[0002] : ET =   515 ms, BW =  7953 Kbyte/s, transferred    12288 Kbytes
[0003] : ET =   515 ms, BW =  7953 Kbyte/s, transferred    16384 Kbytes
[0004] : ET =   515 ms, BW =  7953 Kbyte/s, transferred    20480 Kbytes
[0005] : ET =   515 ms, BW =  7953 Kbyte/s, transferred    24576 Kbytes
[0006] : ET =   515 ms, BW =  7953 Kbyte/s, transferred    28672 Kbytes
[0007] : ET =   514 ms, BW =  7968 Kbyte/s, transferred    32768 Kbytes
[0008] : ET =   515 ms, BW =  7953 Kbyte/s, transferred    36864 Kbytes
[0009] : ET =   515 ms, BW =  7953 Kbyte/s, transferred    40960 Kbytes

*/