Skip to content

Commit

Permalink
Comms PAR algorithm prep.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Oct 20, 2024
1 parent 3612903 commit 2f0af7e
Show file tree
Hide file tree
Showing 8 changed files with 1,421 additions and 1,383 deletions.
2,716 changes: 1,362 additions & 1,354 deletions firmware/build/neodk_g071.hex

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion firmware/inc/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@


typedef enum {
AI_ALL_PATTERN_NAMES = 5, AI_CURRENT_PATTERN_NAME, AI_INTENSITY_PERCENT, AI_PLAY_PAUSE_STOP
AI_FIRMWARE_VERSION = 2, AI_BATTERY_LEVEL, AI_CLOCK_MICROS,
AI_ALL_PATTERN_NAMES, AI_CURRENT_PATTERN_NAME, AI_INTENSITY_PERCENT, AI_PLAY_PAUSE_STOP
} AttributeId;

typedef void (*AttrNotifier)(void *target, AttributeId, ElementEncoding, uint8_t const *data, uint16_t size);

#ifdef __cplusplus
extern "C" {
#endif

SubscriptionId Attribute_subscribe(AttributeId, AttrNotifier, void *target);
void Attribute_changed(AttributeId, ElementEncoding, uint8_t const *data, uint16_t size);

#ifdef __cplusplus
}
#endif

#endif
2 changes: 1 addition & 1 deletion firmware/maolib/inc/bsp_mao.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Created on: 23 May 2021
* Author: mark
* Copyright 2021..2024 Neostim
* Copyright 2021..2024 Neostim
*/

#ifndef INC_BSP_MAO_H_
Expand Down
6 changes: 4 additions & 2 deletions firmware/maolib/inc/net_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
*
* Created on: 29 Feb 2020
* Author: mark
* Copyright 2020..2024 Neostim
* Copyright 2020..2024 Neostim
*/

#include <stdbool.h>
#include <stdint.h>

#define FRAME_HEADER_SIZE 8
#define NR_OF_FRAME_SEQ_NRS 8

// ProtocolVersion (2 bits) determines size and interpretation of the frame header.
typedef enum { PROTO_FIXED, PROTO_VAR } ProtocolVersion;
typedef enum { FT_NONE, FT_ACK, FT_NAK, FT_SYNC, FT_DATA, FT_OPTIONS, FT_RESERVED_1, FT_RESERVED_2 } FrameType;
Expand All @@ -21,7 +24,6 @@ extern "C" {
#endif

// Class methods.
uint16_t PhysFrame_headerSize(void);
char const *PhysFrame_frameTypeName(FrameType);
char const *PhysFrame_serviceTypeName(NetworkServiceType);

Expand Down
Binary file modified firmware/maolib/libmao.a
Binary file not shown.
64 changes: 42 additions & 22 deletions firmware/src/datalink.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@
// This module implements:
#include "datalink.h"

#define MAX_PAYLOAD_SIZE 256
#define MAX_PAYLOAD_SIZE 512

typedef struct {
uint8_t frame[FRAME_HEADER_SIZE + MAX_PAYLOAD_SIZE];
uint32_t frame_timestamp_µs;
} TxFrameInfo;

struct _DataLink {
EventQueue *delegate_queue;
uint8_t tx_buf_store[200];
TxFrameInfo tx_frame_info[NR_OF_FRAME_SEQ_NRS];
CircBuffer output_buffer;
uint32_t frame_timestamp_µs[8];
uint8_t *rx_frame_buffer;
uint8_t tx_buf_store[200];
uint8_t rx_frame_buffer[FRAME_HEADER_SIZE + MAX_PAYLOAD_SIZE];
uint8_t rx_nb;
DeviceId channel_fd;
uint16_t header_size;
uint16_t rx_payload_size;
uint8_t tx_seq_nr;
uint8_t synced;
Expand All @@ -41,15 +45,13 @@ struct _DataLink {

static void init(DataLink *me)
{
me->header_size = PhysFrame_headerSize();
me->rx_frame_buffer = malloc(me->header_size + MAX_PAYLOAD_SIZE);
me->rx_nb = 0;
me->synced = false;
me->tx_seq_nr = 0;
}


/*static*/ void dumpBuffer(const char *prefix, const uint8_t *bbuf, uint8_t nb)
__attribute__((unused))
static void dumpBuffer(const char *prefix, const uint8_t *bbuf, uint8_t nb)
{
if (prefix != NULL) BSP_logf("%s", prefix);
for (uint8_t i = 0; i < nb; i++) {
Expand All @@ -71,11 +73,11 @@ static FrameType assembleIncomingFrame(DataLink *me, uint8_t ch)
{
me->rx_frame_buffer[me->rx_nb++] = ch;
// Just collect bytes until we have a full header.
if (me->rx_nb < me->header_size) return FT_NONE;
if (me->rx_nb < FRAME_HEADER_SIZE) return FT_NONE;

PhysFrame const *frame = (PhysFrame const *)me->rx_frame_buffer;
// Shift/append the input buffer until it contains a valid header.
if (me->rx_nb == me->header_size) {
if (me->rx_nb == FRAME_HEADER_SIZE) {
if (! PhysFrame_hasValidHeader(frame)) {
makeRoomForNextByte(me);
return FT_NONE;
Expand All @@ -88,7 +90,7 @@ static FrameType assembleIncomingFrame(DataLink *me, uint8_t ch)
return FT_NONE;
}
}
if (me->rx_nb == me->header_size + me->rx_payload_size) {
if (me->rx_nb == FRAME_HEADER_SIZE + me->rx_payload_size) {
me->rx_nb = 0; // Prepare for the next frame.
return PhysFrame_type(frame);
}
Expand All @@ -108,7 +110,9 @@ static uint32_t writeFrame(DataLink *me, uint8_t const *frame, uint32_t nb)

static void respondWithAckFrame(DataLink *me, uint8_t ack_nr, NetworkServiceType nst)
{
uint8_t ack_frame[me->header_size];
uint8_t ack_frame[FRAME_HEADER_SIZE];
// TODO Check whether we have an outbound frame queued that
// we can piggyback on instead of sending a separate ACK frame.
PhysFrame_initHeaderWithAck((PhysFrame *)ack_frame, FT_ACK, 0, ack_nr, nst);
// BSP_logf("%s(%hhu) to controller\n", __func__, ack_nr);
writeFrame(me, ack_frame, sizeof ack_frame);
Expand All @@ -119,7 +123,7 @@ static void handleIncomingDataFrame(DataLink *me, PhysFrame const *frame)
{
NetworkServiceType nst = PhysFrame_serviceType(frame);
uint8_t const *payload = PhysFrame_payload(frame);
uint16_t payload_size = PhysFrame_payloadSize(frame);
uint16_t payload_size = PhysFrame_payloadSize(frame);
if (nst == NST_DEBUG) {
// BSP_logf("Got debug frame, seq_nr=%hhu, command length=%hu\n", PhysFrame_seqNr(frame), payload_size);
CLI_handleRemoteInput(payload, payload_size);
Expand All @@ -130,17 +134,32 @@ static void handleIncomingDataFrame(DataLink *me, PhysFrame const *frame)
}


static void setFrameQueuedTimestamp(DataLink *me)
{
me->tx_frame_info[me->tx_seq_nr].frame_timestamp_µs = BSP_microsecondsSinceBoot();
}


static uint32_t getFrameAckDelay(DataLink *me, uint8_t ack_nr)
{
return BSP_microsecondsSinceBoot() - me->tx_frame_info[ack_nr].frame_timestamp_µs;
}


static void handleIncomingFrame(DataLink *me, PhysFrame const *frame)
{
if (! PhysFrame_isIntact(frame)) {
BSP_logf("%s: bad frame\n", __func__);
// Do not ACK, wait for retransmission.
// Can also NAK if the header is correct but payload corrupt.
return;
}

FrameType frame_type = PhysFrame_type(frame);
if (frame_type == FT_ACK) {
// uint8_t ack_nr = PhysFrame_ackNr(frame);
// uint32_t ack_delay = BSP_microsecondsSinceBoot() - me->frame_timestamp_µs[ack_nr];
uint8_t ack_nr = PhysFrame_ackNr(frame);
__attribute__((unused)) // Temporary.
uint32_t ack_delay = getFrameAckDelay(me, ack_nr);
// BSP_logf("Got ACK for frame %hhu after %u µs\n", ack_nr, ack_delay);
// TODO Slide the window.
return;
Expand Down Expand Up @@ -226,12 +245,14 @@ static void setupChannel(DataLink *me)

static bool sendPacket(DataLink *me, NetworkServiceType nst, uint8_t const *packet, uint16_t nb)
{
uint8_t frame_store[me->header_size + nb];
uint8_t *frame_store = me->tx_frame_info[me->tx_seq_nr].frame;
PhysFrame_init((PhysFrame *)frame_store, FT_DATA, me->tx_seq_nr, nst, packet, nb);
me->frame_timestamp_µs[me->tx_seq_nr] = BSP_microsecondsSinceBoot();
me->tx_seq_nr = (me->tx_seq_nr + 1) & 0x7;
// BSP_logf("Writing frame with size %hu to buffer\n", sizeof frame_store);
return writeFrame(me, frame_store, sizeof frame_store) == sizeof frame_store;
if (writeFrame(me, frame_store, FRAME_HEADER_SIZE + nb) == FRAME_HEADER_SIZE + nb) {
setFrameQueuedTimestamp(me);
me->tx_seq_nr = (me->tx_seq_nr + 1) & 0x7;
return true;
}
return false;
}

/*
Expand Down Expand Up @@ -283,7 +304,6 @@ bool DataLink_sendDatagram(DataLink *me, uint8_t const *packet, uint16_t nb)
void DataLink_close(DataLink *me)
{
BSP_doChannelAction(me->channel_fd, CA_CLOSE);
free(me->rx_frame_buffer);
me->channel_fd = -1;
}

Expand Down
2 changes: 1 addition & 1 deletion firmware/src/debug_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static void interpretCommand(CmndInterp *me, char ch)
changeIntensity(me, +2);
break;
case 'v':
CLI_logf("Firmware v0.35-beta\n");
CLI_logf("Firmware v0.36-beta\n");
break;
case 'w': // Allow rediscovery by Dweeb.
DataLink_waitForSync(me->datalink);
Expand Down
4 changes: 2 additions & 2 deletions firmware/src/neodk_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ static bool noEventsPending(Boss const *me)

static void setupAndRunApplication(Boss *me)
{
Sequencer_init(me->sequencer);
DataLink *datalink = DataLink_new();
Sequencer_init(me->sequencer);
Controller_init(me->controller, me->sequencer, datalink);
CLI_init(&me->event_queue, me->sequencer, datalink);
Sequencer_start(me->sequencer);
Expand All @@ -153,7 +153,7 @@ int main()
BSP_logf("Initialising...\n");
BSP_init(); // Get the hardware ready.

Boss boss;
Boss boss; // The supervisor object.
Boss_init(&boss);

BSP_registerAppTimerHandler((void (*)(void *, uint64_t))&onAppTimerTick, &boss, MICROSECONDS_PER_APP_TIMER_TICK);
Expand Down

0 comments on commit 2f0af7e

Please sign in to comment.