Skip to content

Commit

Permalink
Lower initial voltage and pulse width. Also minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Oct 8, 2024
1 parent 8307f0d commit c19b6a4
Show file tree
Hide file tree
Showing 12 changed files with 1,410 additions and 1,392 deletions.
2,691 changes: 1,349 additions & 1,342 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 @@ -22,7 +22,7 @@ enum {
ET_APP_HEARTBEAT, ET_ADC_DATA_AVAILABLE,
ET_CONTROLLER_CONNECTED, ET_CONTROLLER_DISCONNECTED,
ET_CONTROLLER_SENT_REQUEST, ET_CONTROLLER_SENT_CHOICE,
ET_SEQUENCER_READY, ET_SEQUENCER_PLAY_PAUSE, ET_NEXT_ROUTINE,
ET_SEQUENCER_READY, ET_PLAY_PAUSE, ET_NEXT_ROUTINE,
ET_BURST_REQUESTED, ET_BURST_REJECTED, ET_BURST_STARTED, ET_BURST_COMPLETED, ET_BURST_EXPIRED,
};

Expand Down
2 changes: 1 addition & 1 deletion firmware/inc/debug_cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@

void CLI_init(EventQueue *, DataLink *);
int CLI_logf(char const *fmt, ...);
void CLI_handleConsoleInput(char const *, uint16_t nb);
void CLI_handleRemoteInput(uint8_t const *, uint16_t nb);

#endif
9 changes: 4 additions & 5 deletions firmware/inc/pattern_iter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ typedef struct {
} PatternDescr;

typedef struct {
uint8_t const (*pattern)[2];
uint16_t nr_of_elcons, elcon_nr;
uint8_t nr_of_steps, step_nr;
uint8_t segment_nr;
PatternDescr const *pattern_descr;
uint16_t elcon_nr;
uint8_t pulse_width_micros;
uint8_t pace_ms;
uint8_t step_nr;
uint16_t nr_of_reps;
uint8_t segment_nr;
} PatternIterator;


Expand Down
2 changes: 1 addition & 1 deletion firmware/inc/sequencer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include <stdint.h>

#define DEFAULT_PRIMARY_VOLTAGE_mV 1500
#define DEFAULT_PRIMARY_VOLTAGE_mV 1200

typedef struct _Sequencer Sequencer; // Opaque type.

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 @@ -401,8 +401,8 @@ static uint16_t pulsePaceMillisecondsToTicks(uint8_t pace_ms)
* @brief Calculate the value (0..4095) for the 12-bit DAC to set the desired primary voltage [mV].
* @note Assuming R15 = 115 kΩ, R18 = 13 kΩ and R19 = 42.2 kΩ (Refer to the schematic).
*/
#define VPRIM_MIN_mV 1064 // 1202 for Tokmas instead of SGM 61410 buck chip.
#define VPRIM_MAX_mV 10057 //10195
#define VPRIM_MIN_mV 1202 // 1202 for Tokmas buck chip, 1064 for SGM 61410.
#define VPRIM_MAX_mV 10195 // 10195 for Tokmas, 10057 for SGM.

static uint16_t Vcap_mV_ToDacVal(uint16_t Vcap_mV)
{
Expand Down
17 changes: 14 additions & 3 deletions firmware/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
// This module implements:
#include "controller.h"

typedef struct {
uint8_t flags; // Version, etc.
uint8_t reserved; // Hop count, etc.
uint16_t src_address;
uint16_t dst_address;
uint8_t message[0];
} PacketHeader;

typedef enum { OC_NONE, OC_STATUS_RESPONSE, OC_READ_REQUEST } Opcode;

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

struct _Controller {
Expand All @@ -32,9 +42,10 @@ struct _Controller {
};


static void handleHostMessage(Controller *me, uint8_t const *msg, uint16_t nb)
static void handleHostPacket(Controller *me, uint8_t const *packet, uint16_t nb)
{
BSP_logf("%s, len=%hu\n", __func__, nb);
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));
}


Expand Down Expand Up @@ -95,7 +106,7 @@ void Controller_init(Controller *me, DataLink *datalink)
BSP_logf("%s\n", __func__);
me->state = &stateIdle;
me->state(me, AOEvent_newEntryEvent());
DataLink_open(me->datalink, me, (PacketCallback)&handleHostMessage);
DataLink_open(me->datalink, me, (PacketCallback)&handleHostPacket);
DataLink_waitForSync(me->datalink);
}

Expand Down
11 changes: 4 additions & 7 deletions firmware/src/datalink.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/

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

#include "bsp_dbg.h"
#include "bsp_mao.h"
Expand Down Expand Up @@ -109,18 +108,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);
uint16_t payload_size = PhysFrame_payloadSize(frame);
BSP_logf("Got packet, seq_nr=%hhu, payload_size=%hu\n", rx_seq_nr, payload_size);
NetworkServiceType nst = PhysFrame_serviceType(frame);
respondWithAckFrame(me, rx_seq_nr, nst);

uint8_t const *payload = PhysFrame_payload(frame);
uint16_t payload_size = PhysFrame_payloadSize(frame);
if (nst == NST_DEBUG) {
char zt_msg[payload_size + 1];
strncpy(zt_msg, (const char *)payload, payload_size);
zt_msg[payload_size] = '\0';
CLI_handleConsoleInput(zt_msg, payload_size);
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);
}
}
Expand Down
15 changes: 11 additions & 4 deletions 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.26-beta\n");
CLI_logf("Firmware v0.27-beta\n");
break;
default:
CLI_logf("Unknown command '/%c'\n", ch);
Expand Down Expand Up @@ -140,10 +140,17 @@ int CLI_logf(char const *fmt, ...)
}


void CLI_handleConsoleInput(char const *cmnd, uint16_t nb)
void CLI_handleRemoteInput(uint8_t const *cmnd, uint16_t len)
{
if (nb == 2 && cmnd[0] == '/') interpretCommand(&my, cmnd[1]);
else BSP_logf("%s('%s')\n", __func__, cmnd);
if (len == 2 && cmnd[0] == '/') {
interpretCommand(&my, cmnd[1]);
} else {
char response[len + 5];
int nc = snprintf(response, sizeof response, "'%.*s'?\n", len, cmnd);
if (nc > 0 && nc < sizeof response) {
DataLink_sendDebugPacket(my.datalink, (uint8_t const *)response, nc);
}
}
}


Expand Down
2 changes: 1 addition & 1 deletion firmware/src/neodk_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static void dispatchEvent(Boss *me, AOEvent const *evt)
handlePosixSignal(me, *(int const *)AOEvent_data(evt));
break;
case ET_BUTTON_PUSHED:
EventQueue_postEvent((EventQueue *)me->sequencer, ET_SEQUENCER_PLAY_PAUSE, NULL, 0);
EventQueue_postEvent((EventQueue *)me->sequencer, ET_PLAY_PAUSE, NULL, 0);
break;
case ET_BUTTON_RELEASED:
// Ignore for now.
Expand Down
24 changes: 11 additions & 13 deletions firmware/src/pattern_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

static void nextElconIfLastStep(PatternIterator *me)
{
if (me->step_nr == me->nr_of_steps - 1) { // Transition done.
PatternDescr const *pd = me->pattern_descr;
if (me->step_nr == pd->nr_of_steps - 1) { // Transition done.
me->step_nr = 0;
if (++me->elcon_nr == me->nr_of_elcons) {
if (++me->elcon_nr == pd->nr_of_elcons) {
me->elcon_nr = 0; // Wrap around.
me->nr_of_reps -= 1;
}
Expand All @@ -31,17 +32,18 @@ static void nextElconIfLastStep(PatternIterator *me)

static uint8_t const *getNextPattern(PatternIterator *me, uint8_t *nr_of_pulses)
{
PatternDescr const *pd = me->pattern_descr;
uint8_t elcon_nr = me->elcon_nr;
if (me->segment_nr == 0) {
*nr_of_pulses = me->nr_of_steps - me->step_nr - 1;
*nr_of_pulses = pd->nr_of_steps - me->step_nr - 1;
me->segment_nr = 1;
} else {
if (++elcon_nr == me->nr_of_elcons) elcon_nr = 0;
if (++elcon_nr == pd->nr_of_elcons) elcon_nr = 0;
*nr_of_pulses = ++me->step_nr;
nextElconIfLastStep(me);
me->segment_nr = 0;
}
return me->pattern[elcon_nr];
return pd->pattern[elcon_nr];
}

/*
Expand All @@ -63,15 +65,11 @@ bool PatternIterator_checkPattern(uint8_t const pattern[][2], uint16_t nr_of_elc

void PatternIterator_init(PatternIterator *me, PatternDescr const *pd)
{
me->pattern = pd->pattern;
me->nr_of_elcons = pd->nr_of_elcons;
me->pace_ms = pd->pace_ms;
me->pattern_descr = pd;
me->nr_of_reps = pd->nr_of_reps;
me->nr_of_steps = pd->nr_of_steps; // Length of a transition.

me->pulse_width_micros = 120;
me->pulse_width_micros = 100;
me->elcon_nr = 0;
M_ASSERT(me->nr_of_steps != 0);
M_ASSERT(pd->nr_of_steps != 0);
me->step_nr = 0;
me->segment_nr = 0; // 0 or 1.
}
Expand All @@ -91,6 +89,6 @@ bool PatternIterator_getNextPulseTrain(PatternIterator *me, PulseTrain *pt)
pt->elcon[0] = elcon[0];
pt->elcon[1] = elcon[1];
pt->pulse_width_micros = me->pulse_width_micros;
pt->pace_ms = me->pace_ms; // Yields 1000/pace_ms pulses per second.
pt->pace_ms = me->pattern_descr->pace_ms; // Yields 1000/pace_ms pulses per second.
return true;
}
23 changes: 11 additions & 12 deletions firmware/src/sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ static void selectNextRoutine(Sequencer *me)
BSP_setPrimaryVoltage_mV(DEFAULT_PRIMARY_VOLTAGE_mV);
if (++me->pattern_index == M_DIM(pattern_descr)) me->pattern_index = 0;
PatternDescr const *pd = &pattern_descr[me->pattern_index];
CLI_logf("Switching to pattern '%s'\n", pd->name);
CLI_logf("Switching to '%s'\n", pd->name);
PatternIterator_init(&me->pi, pd);
}


static void printAdcValues(uint16_t const *v)
{
uint32_t Iprim_mA = ((uint32_t)v[0] * 2063UL) / 1024;
uint32_t Iprim_mA = ((uint32_t)v[0] * 2063UL) / 1024;
uint32_t Vcap_mV = ((uint32_t)v[1] * 52813UL) / 16384;
uint32_t Vbat_mV = ((uint32_t)v[2] * 52813UL) / 16384;
CLI_logf("Iprim=%u mA, Vcap=%u mV, Vbat=%u mV\n", Iprim_mA, Vcap_mV, Vbat_mV);
Expand Down Expand Up @@ -164,9 +164,9 @@ static void *stateIdle(Sequencer *me, AOEvent const *evt)
case ET_ADC_DATA_AVAILABLE:
printAdcValues((uint16_t const *)AOEvent_data(evt));
break;
case ET_SEQUENCER_PLAY_PAUSE:
case ET_PLAY_PAUSE:
PatternIterator_init(&me->pi, &pattern_descr[me->pattern_index]);
CLI_logf("Starting\n");
CLI_logf("Starting '%s'\n", me->pi.pattern_descr->name);
return &statePulsing; // Transition.
case ET_NEXT_ROUTINE:
selectNextRoutine(me);
Expand All @@ -175,7 +175,7 @@ static void *stateIdle(Sequencer *me, AOEvent const *evt)
BSP_logf("Pulse train last pulse done\n");
break;
case ET_BURST_EXPIRED:
CLI_logf("Finished\n");
CLI_logf("Finished '%s'\n", me->pi.pattern_descr->name);
break;
default:
BSP_logf("Sequencer_%s unexpected event: %u\n", __func__, AOEvent_type(evt));
Expand All @@ -194,8 +194,8 @@ static void *statePaused(Sequencer *me, AOEvent const *evt)
case ET_AO_EXIT:
BSP_logf("%s EXIT\n", __func__);
break;
case ET_SEQUENCER_PLAY_PAUSE:
CLI_logf("Resuming\n");
case ET_PLAY_PAUSE:
CLI_logf("Resuming '%s'\n", me->pi.pattern_descr->name);
return &statePulsing;
case ET_NEXT_ROUTINE:
selectNextRoutine(me);
Expand All @@ -205,10 +205,10 @@ static void *statePaused(Sequencer *me, AOEvent const *evt)
break;
case ET_BURST_EXPIRED:
if (PatternIterator_done(&me->pi)) {
CLI_logf("Finished\n");
CLI_logf("Finished '%s'\n", me->pi.pattern_descr->name);
return &stateIdle; // Transition.
}
CLI_logf("Pausing\n");
CLI_logf("Pausing '%s'\n", me->pi.pattern_descr->name);
break;
case ET_ADC_DATA_AVAILABLE:
printAdcValues((uint16_t const *)AOEvent_data(evt));
Expand Down Expand Up @@ -246,7 +246,7 @@ static void *statePulsing(Sequencer *me, AOEvent const *evt)
case ET_AO_EXIT:
BSP_logf("%s EXIT\n", __func__);
break;
case ET_SEQUENCER_PLAY_PAUSE:
case ET_PLAY_PAUSE:
return &statePaused; // Transition.
case ET_NEXT_ROUTINE:
selectNextRoutine(me);
Expand All @@ -259,7 +259,7 @@ static void *statePulsing(Sequencer *me, AOEvent const *evt)
break;
case ET_BURST_EXPIRED:
if (! scheduleNextPulseTrain(me)) {
CLI_logf("Finished\n");
CLI_logf("Finished '%s'\n", me->pi.pattern_descr->name);
return &stateIdle; // Transition.
}
break;
Expand Down Expand Up @@ -311,7 +311,6 @@ void Sequencer_start(Sequencer *me)
}

PatternIterator_init(&me->pi, &pattern_descr[me->pattern_index]);

uint32_t total_nr_of_pulses = 0;
PulseTrain pt;
while (PatternIterator_getNextPulseTrain(&me->pi, &pt)) {
Expand Down

0 comments on commit c19b6a4

Please sign in to comment.