#include <SPI.h>
#include <SPI_MSTransfer.h>
#include "A_ConfigDefines.h"
//#define SPI_SPEED 30500
#define SPI_SPEED 600000
#define OT_CALC 100*(30000000/SPI_SPEED)
SPI_MSTransfer teensy_gpio = SPI_MSTransfer("Serial", 43, &SPI2, 4000000 ); // bad with default timeouts
SPI_MSTransfer teensy_gpio2 = SPI_MSTransfer("Serial", 43, &SPI2, 300000 ); // bad with default timeouts
SPI_MSTransfer _spitest = SPI_MSTransfer("Serial2", 31, &SPI1, 1000000 ); // bad with default timeouts
void setup() {
Serial.begin(115200);
while (!Serial && millis() < 2000 ) {}
Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
//teensy_gpio.onTransfer(myCallback);
#ifdef SPI_MST_SCK
// SPI_MST_BUS.setSCK( SPI_MST_SCK );
#endif
// digitalWrite(42, OUTPUT);
// digitalWrite(43, OUTPUT);
// SPI_MST_BUS.begin();
delay(1000);
//
SPI.begin();
pinMode(2, INPUT_PULLUP);
teensy_gpio.debug(Serial);
return;
// //
// Circular_Buffer<float, 16> myFloats;
// // Circular_Buffer<float, 16, 10> myFloats2;
// // Circular_Buffer<int, 16, 10> int2;
// myFloats.push_back(-3.14159);
// myFloats.push_back(-12.3456);
// myFloats.push_back(-78.91234);
// myFloats.push_back(-7.91234);
// myFloats.push_back(-11.91234);
// myFloats.push_back(-58.91234);
// myFloats.push_back(-18.91234);
// //// int moo[2] = { 1, 2};
// //// int2.push_front(moo, 2);
// //// int2.push_front(moo, 2);
// //// int2.push_front(moo, 2);
// // // int2.read();
// myFloats.list();
// // // Serial.println();
// // int2.list();
////
//
//
//
// while (1);
Circular_Buffer<uint8_t, 4, 6> ca;
uint8_t buf[6] = { 24, 25, 26, 3, 4, 5 };
uint8_t buf1[6] = { 6, 7, 8, 9, 10, 11 };
uint8_t buf2[6] = { 12, 13, 14, 15, 16, 17 };
uint8_t buf3[6] = { 18, 19, 20, 21, 22, 23 };
uint8_t buf4[6] = { 24, 25, 26, 27, 28, 29 };
uint8_t buf5[6] = { 30, 31, 32, 33, 34, 35 };
uint8_t buf6[6] = { 36, 37, 38, 39, 40, 41 };
ca.push_back(buf, 6);
ca.push_back(buf1, 6);
ca.push_back(buf2, 6);
ca.push_back(buf3, 6);
ca.push_back(buf4, 6);
ca.push_back(buf5, 6);
ca.push_front(buf6, 6);
Serial.print("LF: "); Serial.println(ca.length_front());
Serial.print("LB: "); Serial.println(ca.length_back());
Serial.print("T0: "); Serial.println(ca.front()[0]);
Serial.print("T1: "); Serial.println(ca.front()[1]);
Serial.print("T2: "); Serial.println(ca.front()[2]);
ca.replace(buf, 6, 0, 1, 2);
ca.list();
ca.remove(0);
ca.list();
// while (1);
Circular_Buffer<float, 4, 6> flt;
float _f[] = { 123.456789, 789.012345 };
float _f1[] = { 333.78643, 69.11 };
float _f2[] = { 88.02, 743.69 };
flt.push_back(_f, 2);
flt.push_back(_f1, 2);
flt.push_back(_f2, 2);
flt.list();
//
Serial.println(flt.length_front());
Serial.println(flt.length_back());
Serial.println(flt.peek_front()[1], 7);
float dump[2];
flt.pop_front(dump, 2);
Serial.print(dump[0], 7); Serial.print(" : "); Serial.println(dump[1], 7);
//
Circular_Buffer<uint16_t, 8> k;
Circular_Buffer<uint16_t, 8> l;
k.push_back(4000);
k.push_back(2);
k.push_back(99);
k.push_back(4000);
k.push_back(399);
k.push_back(4000);
k.push_back(4000);
k.push_back(4000);
k.push_back(4000);
k.list();
// while (1);
// k.pop_back();
// k.push_back(10);
// k.push_back(10);
// k.pop_back();
// k.pop_back();
//// k.pop_back();
// k.pop_back();
// k.push_back(49);
// k.push_front(55);
uint8_t myArray[10] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
bool fail_check = 0;
for (uint16_t i = 0; i < sizeof(myArray) / sizeof(myArray[0]); i++) {
if (myArray[i] < 9) {
fail_check = 1;
break;
}
}
( fail_check ) ? Serial.println("FAIL") : Serial.println("PASS");
//Serial.println(SPI2.setCS(43));
//Serial.println(SPI2.setCS(54));
// while (1);
SPI1.begin();
SPI1.setCS(31);
SPI1.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
// teensy_gpio.debug(Serial);
return;
while (1) {
static bool flip = 0;
flip = !flip;
uint32_t value = 0;
( flip ) ? value = 0x4000FFFF : value = 0x40000000;
// transfer32(value, 31);
delay(1000);
Serial.print("VAL: "); Serial.println(digitalRead(2));
uint8_t buf[4] = { 0x40, 0x00, (uint8_t)(value >> 8), (uint8_t)value }, bufr[4] = { 0 };
uint16_t b[2] = { 0x4000, (uint16_t)(value) };
// transfer16(b, nullptr, 2, 31);
// _spitest._transfer16(b, nullptr, 2);
// _spitest._transfer(buf, nullptr, 4);
// Serial.println(b[0]);
// Serial.println(b[1]);
// transfer(buf, bufr, 4, 31);
// Serial.println(bufr[0]);
// Serial.println(bufr[1]);
// Serial.println(bufr[2]);
// Serial.println(bufr[3]);
}
}
void transfer16(uint16_t *buffer, uint16_t *retbuf, uint16_t length, uint8_t pcs) {
for ( uint16_t i = 0; i < length; i++ ) {
if ( i == length - 1 ) {
SPI1_SR = SPI_SR_TCF;
SPI1_PUSHR = buffer[i] | SPI_PUSHR_CTAS(1) | SPI_PUSHR_PCS(pcs);
while (!(SPI1_SR & SPI_SR_TCF));
if ( retbuf ) retbuf[i] = SPI1_POPR;
break;
}
SPI1_SR = SPI_SR_TCF;
SPI1_PUSHR = buffer[i] | SPI_PUSHR_CTAS(1) | SPI_PUSHR_PCS(pcs) | SPI_PUSHR_CONT;
while (!(SPI1_SR & SPI_SR_TCF));
if ( retbuf ) retbuf[i] = SPI1_POPR;
}
}
void transfer(uint8_t *buffer, uint8_t *retbuf, uint16_t length, uint8_t pcs) {
for ( uint16_t i = 0; i < length; i++ ) {
if ( i == length - 1 ) {
SPI1_SR = SPI_SR_TCF;
SPI1_PUSHR = buffer[i] | SPI_PUSHR_CTAS(0) | SPI_PUSHR_PCS(pcs);
while (!(SPI1_SR & SPI_SR_TCF));
if ( retbuf ) retbuf[i] = SPI1_POPR;
break;
}
SPI1_SR = SPI_SR_TCF;
SPI1_PUSHR = buffer[i] | SPI_PUSHR_CTAS(0) | SPI_PUSHR_PCS(pcs) | SPI_PUSHR_CONT;
while (!(SPI1_SR & SPI_SR_TCF));
if ( retbuf ) retbuf[i] = SPI1_POPR;
}
}
void transfer32(uint32_t val, uint8_t pcs) {
SPI1_SR = SPI_SR_TCF;
SPI1_PUSHR = (uint16_t)(val >> 16) | SPI_PUSHR_CTAS(1) | SPI_PUSHR_PCS(pcs);// | SPI_PUSHR_CONT;
while (!(SPI1_SR & SPI_SR_TCF));
SPI1_SR = SPI_SR_TCF;
SPI1_PUSHR = (uint16_t)val | SPI_PUSHR_CTAS(1) | SPI_PUSHR_PCS(pcs);
while (!(SPI1_SR & SPI_SR_TCF));
}
void myCallback(uint16_t *buffer, uint16_t length, AsyncMST info) {
Serial.print("PacketID: "); Serial.println(info.packetID);
Serial.print("Length: "); Serial.println(length);
for ( uint16_t i = 0; i < length; i++ ) {
Serial.print(buffer[i], HEX); Serial.print(" ");
}
// Serial.println();
}
uint32_t OverTime = 0;
void loop() {
static bool setOnce = 1;
if ( setOnce ) {
setOnce = 0;
}
static uint32_t _timer = millis();
// teensy_gpio2.events();
static uint32_t _t = millis();
if ( millis() - _t >= 50 ) {
_t = millis();
uint16_t buf[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
uint32_t t = micros();
teensy_gpio2.transfer16(buf, 10, 60, 1);
Serial.println(micros() - t);
//Serial.println(teensy_gpio.digitalRead(13));
// teensy_gpio.digitalWrite(13, !teensy_gpio.digitalRead(13));
//return;
// delay(10);
// teensy_gpio.digitalWrite(13,1);
//delay(50);
// teensy_gpio.digitalWrite(13,0);
// teensy_gpio.println("Hello World!");
// teensy_gpio2.pinToggle(LED_BUILTIN);
// teensy_gpio.println("Hello world!");
}
return;
// if ( micros() - _timer >= 9168250 ) {
if ( micros() - _timer >= 30 ) {
_timer = micros();
uint32_t _time = micros();
uint16_t *buf;
double MST_PrintVals[12];
buf = (uint16_t *)MST_PrintVals;
int ii = 0;
static uint16_t __count = 0;
static uint16_t __countB = 0;
for ( uint32_t i = 0; i < sizeof(MST_PrintVals) / sizeof( MST_PrintVals[0] ); i++ ) MST_PrintVals[i] = __count++;
Serial.print("F&F (OT=");
Serial.print( OverTime );
Serial.print(")");
_time = micros();
__countB++;
if ( __countB % 25 ) {
teensy_gpio.transfer16((uint16_t *)MST_PrintVals, sizeof(MST_PrintVals) / 2, 60, 1); // DEBUGHACK output
}
else {
teensy_gpio.transfer16((uint16_t *)MST_PrintVals, sizeof(MST_PrintVals) / 2, 55, 1);
}
_time = micros() - _time;
Serial.print(" OT_CALC==");
Serial.print(OT_CALC);
Serial.print(" micros() _time==");
Serial.println(_time);
if ( _time > OT_CALC ) OverTime++;
}
}