main changed for arduino nano debug

This commit is contained in:
Lukas Loidold 2020-04-21 18:38:38 +02:00
parent 2c1925d18f
commit 866944fa26
1 changed files with 18 additions and 2 deletions

View File

@ -16,6 +16,7 @@
#define RING_BUFFER_SIZE 100 #define RING_BUFFER_SIZE 100
#define MAX_PACKET_LENGTH 100 #define MAX_PACKET_LENGTH 100
#define SERIAL_RX_BUFFER_SIZE 256
static const uint8_t COMMAND_TRANSFER_SPI = 1; static const uint8_t COMMAND_TRANSFER_SPI = 1;
@ -60,8 +61,11 @@ void handlePacket(uint8_t *packet, size_t packetLen) {
// 8 bit command | 8 bit address | 16bit length | <length> byte data | 16 bit crc // 8 bit command | 8 bit address | 16bit length | <length> byte data | 16 bit crc
uint16_t crc = Calculate_CRC(packet, packetLen); uint16_t crc = Calculate_CRC(packet, packetLen);
if (crc != 0) { if (crc != 0) {
Serial.println("invalid Checksum"); Serial.println("invalid Checksum");
return; return;
@ -77,11 +81,18 @@ void handlePacket(uint8_t *packet, size_t packetLen) {
uint8_t command = packet[0]; uint8_t command = packet[0];
uint8_t address = packet[1]; uint8_t address = packet[1];
switch (command) { switch (command) {
case COMMAND_TRANSFER_SPI: case COMMAND_TRANSFER_SPI:
transferSPI(address, packet + 4, payloadLen); transferSPI(address, packet + 4, payloadLen);
//echo the data back, no need to change the header fields, they are the same //echo the data back, no need to change the header fields, they are the same
//checksum will be written by sendData() //checksum will be written by sendData()
//check reply:
Serial.println("Data response check: ");
for(size_t i =0; i< packetLen; i++){
Serial.print("packet nr ");Serial.print(i);Serial.print(" ");Serial.println(packet[i]);
}
sendData(packet, packetLen); sendData(packet, packetLen);
break; break;
default: default:
@ -102,11 +113,13 @@ void handleNewData() {
size_t firstSTXinRawData = 0; size_t firstSTXinRawData = 0;
while ((firstSTXinRawData < rawDataSize) while ((firstSTXinRawData < rawDataSize)
&& (rawData[firstSTXinRawData] != DleEncoder::STX)) { && (rawData[firstSTXinRawData] != DleEncoder::STX)) {
Serial.println(rawData[firstSTXinRawData]);
firstSTXinRawData++; firstSTXinRawData++;
} }
if (rawData[firstSTXinRawData] != DleEncoder::STX) { if (rawData[firstSTXinRawData] != DleEncoder::STX) {
//there is no STX in our data, throw it away... //there is no STX in our data, throw it away...
Serial.println("NO STX");
ringBuffer.deleteData(rawDataSize); ringBuffer.deleteData(rawDataSize);
return; return;
} }
@ -136,7 +149,9 @@ void handleNewData() {
//TODO check if this is thread safe by arduino //TODO check if this is thread safe by arduino
void serialEvent() { void serialEvent() {
//Serial.println(ringBuffer.availableWriteSpace()); //Serial.println(ringBuffer.availableWriteSpace());
while (Serial.available()) { uint8_t i = 0;
while (Serial.available()>0) {
uint8_t byte = Serial.read(); uint8_t byte = Serial.read();
ringBuffer.writeData(&byte, 1); ringBuffer.writeData(&byte, 1);
} }
@ -151,6 +166,7 @@ void setup() {
} }
void loop() { void loop() {
;
handleNewData(); handleNewData();
delay(100); delay(1000);
} }