From 48de9591f766d9b300c6489d965e787b81c127e9 Mon Sep 17 00:00:00 2001 From: "Robin.Mueller" Date: Tue, 13 Oct 2020 02:00:04 +0200 Subject: [PATCH] timer lib added --- ArduinoConfig.h | 3 ++- IOBoard.cpp | 13 ++++++++++--- Makefile | 1 + arduino_core | 2 +- main.cpp | 15 +++++++++++++-- 5 files changed, 27 insertions(+), 7 deletions(-) diff --git a/ArduinoConfig.h b/ArduinoConfig.h index 8b75d7c..f634de0 100644 --- a/ArduinoConfig.h +++ b/ArduinoConfig.h @@ -7,12 +7,13 @@ static const uint8_t COMMAND_TRANSFER_SPI = 1; // serial port which is usually also used to flash the Arduino // Can be disabled if this output interferes with the usual // serial communication logic -#define PROGRAMMING_OUTPUT 1 +#define PROGRAMMING_OUTPUT 1 #define BAUD_RATE 115200 #define SERIAL_RX_BUFFER_SIZE 256 #define RING_BUFFER_SIZE 100 #define MAX_PACKET_LENGTH 100 +#define RING_BUFFER_CHECK_INTVL 1000 // Define which port to use for the SPI Chip Select by using the register // definitions. The data direction register is assigned as well. diff --git a/IOBoard.cpp b/IOBoard.cpp index 6f03539..927fe7e 100644 --- a/IOBoard.cpp +++ b/IOBoard.cpp @@ -10,6 +10,12 @@ SimpleRingBuffer ringBuffer(RING_BUFFER_SIZE, true); uint8_t rawData[2 * RING_BUFFER_SIZE]; size_t rawDataSize = 0; +#define PACKET_COMMAND_LENGTH 1 +#define PACKET_ADDRESS_LENGTH 1 +#define PACKET_SIZE_FIELD_LENGTH 2 + +#define PACKET_HEADER_LENGTH 4 + namespace IOBoard { void handleNewData() { @@ -79,14 +85,14 @@ void handlePacket(uint8_t *packet, size_t packetLen) { uint16_t crc = Calculate_CRC(packet, packetLen); if (crc != 0) { - Serial.println("invalid Checksum"); + Serial.println("-AI- Invalid packet checksum detected!"); return; } uint16_t payloadLen = (packet[2] << 8) | packet[3]; if (payloadLen != packetLen - 6) { - Serial.println("invalid len"); + Serial.println("-AI- Invalid packet length detected!"); return; } @@ -95,7 +101,7 @@ void handlePacket(uint8_t *packet, size_t packetLen) { switch (command) { case COMMAND_TRANSFER_SPI: - transferSPI(address, packet + 4, payloadLen); + transferSPI(address, packet + PACKET_HEADER_LENGTH, payloadLen); //echo the data back, no need to change the header fields, they are the same //checksum will be written by sendReply() //check reply: @@ -149,6 +155,7 @@ void sendReply(uint8_t *data, size_t len) { //TODO check if this is thread safe by arduino +// I think it is, see HardwareSerial::available() void serialEvent() { //Serial.println(ringBuffer.availableWriteSpace()); //uint8_t i = 0; diff --git a/Makefile b/Makefile index 72b33da..819440a 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,7 @@ PSRC += arduino_core/ArduinoCore-avr/libraries/SPI/src/SPI.cpp ARDLIBS = EXTRAINCDIRS = arduino_core/ArduinoCore-avr/libraries/SPI/src +EXTRAINCDIRS = arduino_core/src # this line includes the Makefile.base include arduino_core/arduino-base.mk diff --git a/arduino_core b/arduino_core index 527ec73..b9aa89f 160000 --- a/arduino_core +++ b/arduino_core @@ -1 +1 @@ -Subproject commit 527ec7353dc51c35168c2b77e7981e6329323f89 +Subproject commit b9aa89f6d2393a59b1103870b7fc2f95f1d8ba76 diff --git a/main.cpp b/main.cpp index e5010d3..63ad9a1 100644 --- a/main.cpp +++ b/main.cpp @@ -3,12 +3,16 @@ #include "IOBoard.h" #include "ArduinoConfig.h" +// TODO: Copy this header into arduino_core, so it can be used without +// Eclipse Sloeber as well. +#include #include //SPI is formally a library, so it is not part of the objects compiled //from the core and we need to include it explicitly #include +auto timer = timer_create_default(); void setup() { // Set data direction of selected port to output. @@ -28,9 +32,16 @@ void setup() { SPI.begin(); } -void loop() { +bool periodicHandler1(void* args) { + if(args) {}; IOBoard::handleNewData(); - delay(1000); + return false; +} + +void loop() { + timer.tick(); + timer.every(RING_BUFFER_CHECK_INTVL, periodicHandler1); + //delay(1000); }