Skip to content

Commit

Permalink
More API prep.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Oct 10, 2024
1 parent 16f3886 commit f2a2660
Show file tree
Hide file tree
Showing 9 changed files with 1,480 additions and 1,361 deletions.
Empty file added .vscode/settings.json
Empty file.
2,694 changes: 1,364 additions & 1,330 deletions firmware/build/neodk_g071.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion firmware/inc/app_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +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_DEBUG_SYNC, ET_DATAGRAM_SYNC, 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
2 changes: 1 addition & 1 deletion firmware/inc/sequencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ 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_getPatternNames(Sequencer const *, char const *[], uint8_t);
void Sequencer_stop(Sequencer *);
void Sequencer_delete(Sequencer *);

Expand Down
4 changes: 2 additions & 2 deletions firmware/src/bsp_stm32g071.c
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,12 @@ void TIM1_CC_IRQHandler(void)
if ((pulse_timer->DIER & TIM_DIER_CC1IE) && (pulse_timer->SR & TIM_SR_CC1IF)) {
pulse_timer->SR &= ~TIM_SR_CC1IF;
bsp.pulse_seqnr += 1;
BSP_logf("CC1 %hu\n", bsp.pulse_seqnr);
// BSP_logf("CC1 %hu\n", bsp.pulse_seqnr);
}
if ((pulse_timer->DIER & TIM_DIER_CC2IE) && (pulse_timer->SR & TIM_SR_CC2IF)) {
pulse_timer->SR &= ~TIM_SR_CC2IF;
bsp.pulse_seqnr += 1;
BSP_logf("CC2 %hu\n", bsp.pulse_seqnr);
// BSP_logf("CC2 %hu\n", bsp.pulse_seqnr);
}
if (bsp.pulse_seqnr == pulse_timer->RCR + 1) {
EventQueue_postEvent(bsp.pulse_delegate_queue, ET_BURST_COMPLETED, NULL, 0);
Expand Down
117 changes: 100 additions & 17 deletions firmware/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
*/

#include <stdlib.h>
#include <string.h>
#include <stddef.h>

#include "bsp_dbg.h"
#include "bsp_app.h"
#include "app_event.h"
#include "debug_cli.h"

// This module implements:
#include "controller.h"
Expand All @@ -29,9 +30,32 @@ typedef struct {
} PacketHeader;

typedef enum {
OC_NONE, OC_STATUS_RESPONSE, OC_READ_REQUEST, OC_SUBSCRIBE_REQUEST, OC_SUBSCRIBE_RESPONSE, OC_REPORT_DATA
OC_NONE, OC_STATUS_RESPONSE, OC_READ_REQUEST, OC_SUBSCRIBE_REQUEST, OC_SUBSCRIBE_RESPONSE, OC_REPORT_DATA,
OC_WRITE_REQUEST, OC_WRITE_RESPONSE, OC_INVOKE_REQUEST, OC_INVOKE_RESPONSE, OC_TIMED_REQUEST
} Opcode;

typedef enum {
EE_SIGNED_INT_1, EE_SIGNED_INT_2, EE_SIGNED_INT_4, EE_SIGNED_INT_8,
EE_UNSIGNED_INT_1, EE_UNSIGNED_INT_2, EE_UNSIGNED_INT_4, EE_UNSIGNED_INT_8,
EE_BOOLEAN_FALSE, EE_BOOLEAN_TRUE, EE_FLOAT_4, EE_FLOAT_8,
EE_UTF8_1LEN, EE_UTF8_2LEN, EE_UTF8_4LEN, EE_UTF8_8LEN,
EE_BYTE_1LEN, EE_BYTE_2LEN, EE_BYTE_4LEN, EE_BYTE_8LEN,
EE_NULL, EE_STRUCT, EE_ARRAY, EE_LIST, EE_END_OF_CONTAINER
} ElementEncoding;

typedef enum {
AI_PATTERN_NAMES = 5,
} AttributeId;

typedef struct {
uint16_t transaction_id;
uint8_t opcode;
uint8_t reserved;
uint16_t attribute_id;
uint8_t data[0];
} AttributeAction;


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

struct _Controller {
Expand All @@ -43,31 +67,87 @@ struct _Controller {
};


static void handleReadRequest(Controller *me, uint8_t const *request)
static uint8_t const welcome_msg[] = "Push the button to play or pause :-)\n";


static uint16_t encodeString(uint8_t dst[], char const *str)
{
uint16_t nbe = 0;
dst[nbe++] = EE_UTF8_1LEN;
uint8_t str_len = strlen(str);
dst[nbe++] = str_len;
memcpy(dst + nbe, str, str_len);
return nbe + str_len;
}


static uint16_t encodeStringArray(uint8_t dst[], char const *strings[], uint8_t nr_of_strings)
{
uint16_t nbe = 0;
dst[nbe++] = EE_ARRAY;
for (uint8_t i = 0; i < nr_of_strings; i++) {
nbe += encodeString(dst + nbe, strings[i]);
}
dst[nbe++] = EE_END_OF_CONTAINER;
return nbe;
}

// Yeah, this function will be refactored :-)
static void readPatternNames(Controller *me, AttributeAction const *aa)
{
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);
uint8_t nr_of_patterns = Sequencer_nrOfPatterns(me->sequencer);
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]);
uint16_t total_bytes = 0;
for (uint8_t i = 0; i < nr_of_patterns; i++) {
total_bytes += strlen(pattern_names[i]);
}
uint16_t nbw = sizeof(PacketHeader) + sizeof(AttributeAction);
uint16_t packet_size = nbw + 2 + nr_of_patterns * 2 + total_bytes;
// TODO Check for max payload size.
uint8_t packet[packet_size];
PacketHeader *ph = (PacketHeader *)packet;
// TODO Fill in the packet header.
AttributeAction *rsp_aa = (AttributeAction *)ph->message;
*rsp_aa = *aa;
rsp_aa->opcode = OC_REPORT_DATA;
nbw += encodeStringArray(packet + nbw, pattern_names, nr_of_patterns);
DataLink_sendDatagram(me->datalink, packet, nbw);
}


static void handleReadRequest(Controller *me, AttributeAction const *aa)
{
if (aa->attribute_id == AI_PATTERN_NAMES) {
readPatternNames(me, aa);
} else {
BSP_logf("%s: unknown attribute id=%hu\n", __func__, aa->attribute_id);
// TODO Respond with NOT_FOUND code.
}
}


static void handleWriteRequest(Controller *me, AttributeAction const *aa)
{
// TODO Implement.
}


static void handleRequest(Controller *me, uint8_t const *request)
{
uint8_t opcode = request[2];
switch (opcode)
AttributeAction const *aa = (AttributeAction const *)request;
switch (aa->opcode)
{
case OC_READ_REQUEST:
handleReadRequest(me, request);
BSP_logf("Transaction %hu: read attribute %hu\n", aa->transaction_id, aa->attribute_id);
handleReadRequest(me, aa);
break;
case OC_WRITE_REQUEST:
BSP_logf("Transaction %hu: write attribute %hu\n", aa->transaction_id, aa->attribute_id);
handleWriteRequest(me, aa);
break;
default:
BSP_logf("%s, unknown opcode 0x%02hhx\n", __func__, opcode);
BSP_logf("%s, unknown opcode 0x%02hhx\n", __func__, aa->opcode);
break;
}
}
Expand All @@ -90,6 +170,9 @@ static void *stateIdle(Controller *me, AOEvent const *evt)
case ET_AO_EXIT:
BSP_logf("Controller_%s EXIT\n", __func__);
break;
case ET_DEBUG_SYNC:
DataLink_sendDebugPacket(me->datalink, welcome_msg, sizeof welcome_msg);
break;
case ET_INCOMING_PACKET:
// Ignore the packet header for now.
handleRequest(me, AOEvent_data(evt) + sizeof(PacketHeader));
Expand Down Expand Up @@ -131,7 +214,7 @@ Controller *Controller_new()
void Controller_init(Controller *me, Sequencer *sequencer, DataLink *datalink)
{
me->sequencer = sequencer;
me->datalink = datalink;
me->datalink = datalink;
BSP_logf("%s\n", __func__);
me->state = &stateIdle;
me->state(me, AOEvent_newEntryEvent());
Expand All @@ -143,7 +226,7 @@ void Controller_init(Controller *me, Sequencer *sequencer, DataLink *datalink)
void Controller_start(Controller *me)
{
BSP_logf("Starting NeoDK!\n");
BSP_logf("Push the button to play or pause :-)\n");
BSP_logf("%s", welcome_msg);
BSP_setPrimaryVoltage_mV(DEFAULT_PRIMARY_VOLTAGE_mV);
BSP_primaryVoltageEnable(true);
}
Expand All @@ -159,7 +242,7 @@ void Controller_stop(Controller *me)
{
me->state(me, AOEvent_newExitEvent());
me->state = stateNop;
CLI_logf("End of session\n");
BSP_logf("End of session\n");
DataLink_close(me->datalink);
}

Expand Down
10 changes: 6 additions & 4 deletions firmware/src/datalink.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// This module implements:
#include "datalink.h"

#define MAX_PAYLOAD_SIZE 1024
#define MAX_PAYLOAD_SIZE 2048

struct _DataLink {
EventQueue *delegate_queue;
Expand Down Expand Up @@ -148,7 +148,9 @@ static void handleIncomingFrame(DataLink *me, PhysFrame const *frame)
uint8_t rx_seq_nr = PhysFrame_seqNr(frame);
if (frame_type == FT_SYNC) {
BSP_logf("Got SYNC frame, seq_nr=%hhu\n", rx_seq_nr);
respondWithAckFrame(me, rx_seq_nr, PhysFrame_serviceType(frame));
NetworkServiceType nst = PhysFrame_serviceType(frame);
respondWithAckFrame(me, rx_seq_nr, nst);
EventQueue_postEvent(me->delegate_queue, nst == NST_DEBUG ? ET_DEBUG_SYNC : ET_DATAGRAM_SYNC, NULL, 0);
return;
}

Expand Down Expand Up @@ -269,9 +271,9 @@ bool DataLink_sendDebugPacket(DataLink *me, uint8_t const *packet, uint16_t nb)
}


bool DataLink_sendDatagram(DataLink *me, uint8_t const *datagram, uint16_t nb)
bool DataLink_sendDatagram(DataLink *me, uint8_t const *packet, uint16_t nb)
{
return sendPacket(me, NST_DATAGRAM, datagram, nb);
return sendPacket(me, NST_DATAGRAM, packet, nb);
}


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.28-beta\n");
CLI_logf("Firmware v0.29-beta\n");
break;
default:
CLI_logf("Unknown command '/%c'\n", ch);
Expand Down
10 changes: 5 additions & 5 deletions firmware/src/sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ struct _Sequencer {
uint8_t event_storage[400];
StateFunc state;
PatternDescr const *pattern_descr;
uint16_t nr_of_patterns;
PatternIterator pi;
uint8_t nr_of_patterns;
uint8_t pattern_index;
};

Expand Down Expand Up @@ -229,7 +229,7 @@ static bool scheduleNextPulseTrain(Sequencer *me)
if (pt.pulse_width_micros > MAX_PULSE_WIDTH_MICROS) {
pt.pulse_width_micros = MAX_PULSE_WIDTH_MICROS;
}
BSP_logf("Pulse width is %hu µs\n", pt.pulse_width_micros);
// BSP_logf("Pulse width is %hu µs\n", pt.pulse_width_micros);
return BSP_startPulseTrain(&pt);
}

Expand Down Expand Up @@ -314,7 +314,7 @@ Sequencer *Sequencer_init(Sequencer *me)

void Sequencer_start(Sequencer *me)
{
for (uint16_t i = 0; i < me->nr_of_patterns; i++) {
for (uint8_t i = 0; i < me->nr_of_patterns; i++) {
PatternDescr const *pd = &me->pattern_descr[i];
BSP_logf("Checking '%s'\n", pd->name);
PatternIterator_checkPattern(pd->pattern, pd->nr_of_elcons);
Expand Down Expand Up @@ -347,10 +347,10 @@ uint16_t Sequencer_nrOfPatterns(Sequencer const *me)
}


void Sequencer_getPatternNames(Sequencer const *me, char const *names[], uint16_t cnt)
void Sequencer_getPatternNames(Sequencer const *me, char const *names[], uint8_t cnt)
{
if (cnt > me->nr_of_patterns) cnt = me->nr_of_patterns;
for (uint16_t i = 0; i < cnt; i++) {
for (uint8_t i = 0; i < cnt; i++) {
names[i] = me->pattern_descr[i].name;
}
}
Expand Down

0 comments on commit f2a2660

Please sign in to comment.