Skip to content

Commit

Permalink
Some API prep.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Oct 9, 2024
1 parent c19b6a4 commit 16f3886
Show file tree
Hide file tree
Showing 12 changed files with 1,455 additions and 1,373 deletions.
2,658 changes: 1,335 additions & 1,323 deletions firmware/build/neodk_g071.hex

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions firmware/inc/app_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
enum {
ET_THIRD_PARTY = ET_AO_FIRST_APP_EVENT, // To keep other libs working.
ET_APP_HEARTBEAT, ET_ADC_DATA_AVAILABLE,
ET_INCOMING_PACKET,
ET_CONTROLLER_CONNECTED, ET_CONTROLLER_DISCONNECTED,
ET_CONTROLLER_SENT_REQUEST, ET_CONTROLLER_SENT_CHOICE,
ET_SEQUENCER_READY, ET_PLAY_PAUSE, ET_NEXT_ROUTINE,
Expand Down
3 changes: 2 additions & 1 deletion firmware/inc/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef INC_CONTROLLER_H_
#define INC_CONTROLLER_H_

#include "sequencer.h"
#include "datalink.h"

typedef struct _Controller Controller; // Opaque type.
Expand All @@ -21,7 +22,7 @@ typedef struct _Controller Controller; // Opaque type.
Controller *Controller_new();

// Instance methods.
void Controller_init(Controller *, DataLink *);
void Controller_init(Controller *, Sequencer *, DataLink *);
void Controller_start(Controller *);
bool Controller_handleEvent(Controller *);
void Controller_stop(Controller *);
Expand Down
5 changes: 3 additions & 2 deletions firmware/inc/datalink.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
#include <stdbool.h>
#include <stdint.h>

typedef void (*PacketCallback)(void *, uint8_t const *packet, uint16_t nb);
#include "eventqueue.h"

typedef struct _DataLink DataLink; // Opaque type.

// Class method.
DataLink *DataLink_new();

// Instance methods.
bool DataLink_open(DataLink *, void *packet_handler, PacketCallback);
bool DataLink_open(DataLink *, EventQueue *);
void DataLink_waitForSync(DataLink *);
bool DataLink_sendDebugPacket(DataLink *, uint8_t const *, uint16_t);
bool DataLink_sendDatagram(DataLink *, uint8_t const *, uint16_t);
Expand Down
3 changes: 3 additions & 0 deletions firmware/inc/sequencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ typedef struct _Sequencer Sequencer; // Opaque type.
Sequencer *Sequencer_new(void);

// Instance methods.
Sequencer *Sequencer_init(Sequencer *);
void Sequencer_start(Sequencer *);
bool Sequencer_handleEvent(Sequencer *);
uint16_t Sequencer_nrOfPatterns(Sequencer const *me);
void Sequencer_getPatternNames(Sequencer const *, char const *[], uint16_t);
void Sequencer_stop(Sequencer *);
void Sequencer_delete(Sequencer *);

Expand Down
2 changes: 1 addition & 1 deletion firmware/src/bsp_stm32g071.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ void BSP_doChannelAction(DeviceId device_id, ChannelAction action)
switch (device_id)
{
case 1:
if (device_id >= 0) doUartAction(USART2, action);
doUartAction(USART2, action);
break;
default:
BSP_logf("%s(%u): unknown device id\n", __func__, device_id);
Expand Down
45 changes: 37 additions & 8 deletions firmware/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "bsp_dbg.h"
#include "bsp_app.h"
#include "app_event.h"
#include "sequencer.h"
#include "debug_cli.h"

// This module implements:
Expand All @@ -29,23 +28,48 @@ typedef struct {
uint8_t message[0];
} PacketHeader;

typedef enum { OC_NONE, OC_STATUS_RESPONSE, OC_READ_REQUEST } Opcode;
typedef enum {
OC_NONE, OC_STATUS_RESPONSE, OC_READ_REQUEST, OC_SUBSCRIBE_REQUEST, OC_SUBSCRIBE_RESPONSE, OC_REPORT_DATA
} Opcode;

typedef void *(*StateFunc)(Controller *, AOEvent const *);

struct _Controller {
EventQueue event_queue; // This MUST be the first member.
uint8_t event_storage[400];
StateFunc state;
DataLink *datalink;
Sequencer *sequencer;
DataLink *datalink;
};


static void handleHostPacket(Controller *me, uint8_t const *packet, uint16_t nb)
static void handleReadRequest(Controller *me, uint8_t const *request)
{
uint8_t const *request = packet + sizeof(PacketHeader);
BSP_logf("%s, transaction=%hu, opcode=0x%02hhx, attribute_id=%hu\n", __func__, *(uint16_t *)request, request[2], *(uint16_t *)(request + 4));
uint16_t attribute_id = *(uint16_t *)(request + 4);
BSP_logf("Transaction %hu: read attribute %hu\n", *(uint16_t *)request, attribute_id);
// Assume a request for the routine names for now.
uint16_t nr_of_patterns = Sequencer_nrOfPatterns(me->sequencer);
BSP_logf("We have %u patterns:\n", nr_of_patterns);
char const *pattern_names[nr_of_patterns];
Sequencer_getPatternNames(me->sequencer, pattern_names, nr_of_patterns);
for (uint16_t i = 0; i < nr_of_patterns; i++) {
BSP_logf(" %s\n", pattern_names[i]);
}
}


static void handleRequest(Controller *me, uint8_t const *request)
{
uint8_t opcode = request[2];
switch (opcode)
{
case OC_READ_REQUEST:
handleReadRequest(me, request);
break;
default:
BSP_logf("%s, unknown opcode 0x%02hhx\n", __func__, opcode);
break;
}
}


Expand All @@ -66,6 +90,10 @@ static void *stateIdle(Controller *me, AOEvent const *evt)
case ET_AO_EXIT:
BSP_logf("Controller_%s EXIT\n", __func__);
break;
case ET_INCOMING_PACKET:
// Ignore the packet header for now.
handleRequest(me, AOEvent_data(evt) + sizeof(PacketHeader));
break;
default:
BSP_logf("Controller_%s unexpected event: %u\n", __func__, AOEvent_type(evt));
}
Expand Down Expand Up @@ -100,13 +128,14 @@ Controller *Controller_new()
}


void Controller_init(Controller *me, DataLink *datalink)
void Controller_init(Controller *me, Sequencer *sequencer, DataLink *datalink)
{
me->sequencer = sequencer;
me->datalink = datalink;
BSP_logf("%s\n", __func__);
me->state = &stateIdle;
me->state(me, AOEvent_newEntryEvent());
DataLink_open(me->datalink, me, (PacketCallback)&handleHostPacket);
DataLink_open(me->datalink, &me->event_queue);
DataLink_waitForSync(me->datalink);
}

Expand Down
63 changes: 36 additions & 27 deletions firmware/src/datalink.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "bsp_dbg.h"
#include "bsp_mao.h"
#include "bsp_app.h"
#include "circbuffer.h"
#include "app_event.h"
#include "net_frame.h"
#include "debug_cli.h" // Temporary.

Expand All @@ -25,9 +25,8 @@
#define MAX_PAYLOAD_SIZE 1024

struct _DataLink {
EventQueue *delegate_queue;
uint8_t tx_buf_store[200];
void *packet_handler;
PacketCallback packet_callback;
CircBuffer output_buffer;
uint8_t *rx_frame_buffer;
uint8_t rx_nb;
Expand All @@ -39,6 +38,16 @@ 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)
{
if (prefix != NULL) BSP_logf("%s", prefix);
Expand Down Expand Up @@ -107,18 +116,16 @@ static void respondWithAckFrame(DataLink *me, uint8_t ack_nr, NetworkServiceType

static void handleIncomingDataFrame(DataLink *me, PhysFrame const *frame)
{
uint8_t rx_seq_nr = PhysFrame_seqNr(frame);
NetworkServiceType nst = PhysFrame_serviceType(frame);
respondWithAckFrame(me, rx_seq_nr, nst);

uint8_t rx_seq_nr = PhysFrame_seqNr(frame);
uint8_t const *payload = PhysFrame_payload(frame);
uint16_t payload_size = PhysFrame_payloadSize(frame);
if (nst == NST_DEBUG) {
BSP_logf("Got debug frame, seq_nr=%hhu, command length=%hu\n", rx_seq_nr, payload_size);
CLI_handleRemoteInput(payload, payload_size);
} else {
BSP_logf("Got network frame, seq_nr=%hhu, packet size=%hu\n", rx_seq_nr, payload_size);
me->packet_callback(me->packet_handler, payload, payload_size);
BSP_logf("Got packet frame, seq_nr=%hhu, packet size=%hu\n", rx_seq_nr, payload_size);
EventQueue_postEvent(me->delegate_queue, ET_INCOMING_PACKET, payload, payload_size);
}
}

Expand Down Expand Up @@ -148,6 +155,7 @@ static void handleIncomingFrame(DataLink *me, PhysFrame const *frame)
uint16_t payload_size = PhysFrame_payloadSize(frame);
if (frame_type == FT_DATA) {
handleIncomingDataFrame(me, frame);
respondWithAckFrame(me, rx_seq_nr, PhysFrame_serviceType(frame));
} else {
BSP_logf("Got %s frame, seq_nr=%hhu, payload_size=%hu\n",
PhysFrame_frameTypeName(frame_type), rx_seq_nr, payload_size);
Expand Down Expand Up @@ -199,10 +207,24 @@ static void txErrorCallback(DataLink *me, uint32_t tx_error)
}


static void setupChannel(DataLink *me)
{
Selector rx_sel, rx_err_sel, tx_err_sel;
Selector_init(&rx_sel, (Action)&rxCallback, me);
Selector_init(&rx_err_sel, (Action)&rxErrorCallback, me);
Selector_init(&tx_err_sel, (Action)&txErrorCallback, me);
BSP_registerRxCallback(me->channel_fd, &rx_sel, &rx_err_sel);
BSP_registerTxCallback(me->channel_fd, (void (*)(void *, uint8_t *))&txCallback, me, &tx_err_sel);
BSP_doChannelAction(me->channel_fd, CA_OVERRUN_CB_ENABLE);
BSP_doChannelAction(me->channel_fd, CA_FRAMING_CB_ENABLE);
BSP_doChannelAction(me->channel_fd, CA_RX_CB_ENABLE);
}


static bool sendPacket(DataLink *me, NetworkServiceType nst, uint8_t const *packet, uint16_t nb)
{
uint8_t frame_store[me->header_size + nb];
PhysFrame_init((PhysFrame *)frame_store, FT_DATA, me->tx_seq_nr, NST_DEBUG, packet, nb);
PhysFrame_init((PhysFrame *)frame_store, FT_DATA, me->tx_seq_nr, nst, packet, nb);
// BSP_logf("Writing frame with size %hu to buffer\n", sizeof frame_store);
return writeFrame(me, frame_store, sizeof frame_store) == sizeof frame_store;
}
Expand All @@ -220,29 +242,16 @@ DataLink *DataLink_new()
}


bool DataLink_open(DataLink *me, void *packet_handler, PacketCallback packet_callback)
bool DataLink_open(DataLink *me, EventQueue *dq)
{
M_ASSERT(me->channel_fd < 0);
me->header_size = PhysFrame_headerSize();
me->packet_handler = packet_handler;
me->packet_callback = packet_callback;
me->rx_frame_buffer = malloc(me->header_size + MAX_PAYLOAD_SIZE);
me->rx_nb = 0;
me->synced = false;
me->tx_seq_nr = 0;
M_ASSERT(me->channel_fd < 0); // Make sure this link is inactive.
me->delegate_queue = dq;
init(me);

BSP_initComms(); // Initialise the communication peripheral.
if ((me->channel_fd = BSP_openSerialPort("serial_1")) < 0) return false;

Selector rx_sel, rx_err_sel, tx_err_sel;
Selector_init(&rx_sel, (Action)&rxCallback, me);
Selector_init(&rx_err_sel, (Action)&rxErrorCallback, me);
Selector_init(&tx_err_sel, (Action)&txErrorCallback, me);
BSP_registerRxCallback(me->channel_fd, &rx_sel, &rx_err_sel);
BSP_registerTxCallback(me->channel_fd, (void (*)(void *, uint8_t *))&txCallback, me, &tx_err_sel);
BSP_doChannelAction(me->channel_fd, CA_OVERRUN_CB_ENABLE);
BSP_doChannelAction(me->channel_fd, CA_FRAMING_CB_ENABLE);
BSP_doChannelAction(me->channel_fd, CA_RX_CB_ENABLE);
setupChannel(me);
return true;
}

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 @@ -85,7 +85,7 @@ static void interpretCommand(CmndInterp *me, char ch)
BSP_changePrimaryVoltage_mV(+200);
break;
case 'v':
CLI_logf("Firmware v0.27-beta\n");
CLI_logf("Firmware v0.28-beta\n");
break;
default:
CLI_logf("Unknown command '/%c'\n", ch);
Expand Down
3 changes: 2 additions & 1 deletion firmware/src/neodk_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,9 @@ static bool noEventsPending(Boss const *me)

static void setupAndRunApplication(Boss *me)
{
Sequencer_init(me->sequencer);
DataLink *datalink = DataLink_new();
Controller_init(me->controller, datalink);
Controller_init(me->controller, me->sequencer, datalink);
CLI_init(&me->event_queue, datalink);
Sequencer_start(me->sequencer);

Expand Down
Loading

0 comments on commit 16f3886

Please sign in to comment.