timer lib added

This commit is contained in:
Robin Müller 2020-10-13 02:00:04 +02:00
parent 325f7c0bca
commit 48de9591f7
5 changed files with 27 additions and 7 deletions

View File

@ -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.

View File

@ -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;

View File

@ -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

@ -1 +1 @@
Subproject commit 527ec7353dc51c35168c2b77e7981e6329323f89
Subproject commit b9aa89f6d2393a59b1103870b7fc2f95f1d8ba76

View File

@ -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 <arduino-timer.h>
#include <avr/io.h>
//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 <SPI.h>
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);
}