
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>


enum {
//    _xinfreq = 20_000_000,
    _xtlfreq = 20_000_000,
    _clkfreq = 8_000_000,

    DOWNLOAD_BAUD = 230_400,
    DEBUG_BAUD = DOWNLOAD_BAUD,

    MISO = 32,
    MOSI = MISO + 1,  // this pairing is required for internal simultaneous management of both smartpins
    CLK = 34,
    CS = 37,
    CMODE = 3,  // CPOL is bit1, CPHA is bit0

    BUFFSIZE = 1024,    // longwords

    SPINS = CS | CLK<<6 | MISO<<12 | MOSI<<18 | CMODE<<30,

};


//struct __using("spi_slave.spin2") dut;
struct __using("spi_slave_truncated.spin2") rpi;


static uint32_t  sbuff[BUFFSIZE];



void  main( void )
{
    uint32_t  func, func1;

    printf( "   clkfreq = %d   clkmode = 0x%x\n", _clockfreq(), _clockmode() );

    rpi.start_SPI_slave( SPINS, sbuff, BUFFSIZE );
    _waitatn();    // wait for cog to start up


    // Slave cog is ready and waiting.  At this stage the tester program has not setup any of its pins,
    // so we are going to be glitching them from the start.


    func1 = sbuff[0];
    while( 1 )
    {
        func = sbuff[0];
        if( func != func1 )
        {
            func1 = func;    // edge detect
            printf( " Function %d\n", func );
        }
    }
}
