Skip to content

Commit

Permalink
More stream handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Jan 14, 2025
1 parent 9eab844 commit 6bba7fc
Show file tree
Hide file tree
Showing 7 changed files with 1,556 additions and 1,537 deletions.
3,016 changes: 1,513 additions & 1,503 deletions firmware/build/neodk_g071.hex

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions firmware/inc/burst.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
#define MAX_PULSE_PACE_µs 63000

typedef struct {
uint32_t start_time_µs;
uint8_t elcon[2];
uint16_t pace_µs;
uint16_t nr_of_pulses;
uint16_t pulse_width_¼_µs;
uint8_t phase;
uint8_t amplitude;
} Burst;

typedef struct {
Expand All @@ -38,6 +40,8 @@ typedef struct {

bool Burst_isValid(Burst const *);
uint32_t Burst_duration_µs(Burst const *);
uint8_t Burst_pulseWidth_µs(Burst const *);
void Burst_applyDeltas(Burst *, Deltas const *);
void Burst_print(Burst const *);

#endif
2 changes: 1 addition & 1 deletion firmware/maolib/inc/ptd_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void PtdQueue_clear(PtdQueue *);
bool PtdQueue_isEmpty(PtdQueue const *);
void PtdQueue_nrOfBytesFree(PtdQueue *, uint16_t[2]);
bool PtdQueue_addDescriptor(PtdQueue *, PulseTrain const *, uint16_t sz);
bool PtdQueue_getNextPtd(PtdQueue *, PulseTrain *);
bool PtdQueue_getNextBurst(PtdQueue *, Burst *);
void PtdQueue_delete(PtdQueue *);

#ifdef __cplusplus
Expand Down
Binary file modified firmware/maolib/libmao.a
Binary file not shown.
20 changes: 18 additions & 2 deletions firmware/src/burst.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Copyright 2025 Neostim™
*/

#include "bsp_dbg.h"

// This module implements:
#include "burst.h"

Expand All @@ -25,9 +27,15 @@ bool Burst_isValid(Burst const *me)
}


uint32_t Burst_duration_µs(Burst const *burst)
uint32_t Burst_duration_µs(Burst const *me)
{
return me->nr_of_pulses * me->pace_µs;
}


uint8_t Burst_pulseWidth_µs(Burst const *me)
{
return burst->nr_of_pulses * burst->pace_µs;
return (me->pulse_width_¼_µs + 2) / 4;
}


Expand All @@ -43,3 +51,11 @@ void Burst_applyDeltas(Burst *me, Deltas const *deltas)
else if (new_pace > MAX_PULSE_PACE_µs) new_pace = MAX_PULSE_PACE_µs;
me->pace_µs = new_pace;
}


void Burst_print(Burst const *me)
{
BSP_logf("Pt: t=%u µs, ec=0x%x<>0x%x, phase=%hhu, np=%hu, pace=%hu µs, amp=%hhu, pw=%hhu µs\n",
me->start_time_µs, me->elcon[0], me->elcon[1], me->phase,
me->nr_of_pulses, me->pace_µs, me->amplitude, Burst_pulseWidth_µs(me));
}
10 changes: 6 additions & 4 deletions firmware/src/pulse_train.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ uint32_t PulseTrain_timestamp(PulseTrain const *me)
}


uint8_t PulseTrain_phase(PulseTrain const *me)
uint8_t PulseTrain_phase(PulseTrain const *me)
{
return me->phase;
return me->phase & 0x7;
}


Expand All @@ -106,12 +106,14 @@ uint16_t PulseTrain_amplitude(PulseTrain const *me)

Burst const *PulseTrain_getBurst(PulseTrain const *me, Burst *burst)
{
burst->start_time_µs = me->start_time_µs;
burst->elcon[0] = me->electrode_set[0];
burst->elcon[1] = me->electrode_set[1];
burst->phase = me->phase;
burst->pace_µs = me->pace_ms * 1000;
burst->phase = PulseTrain_phase(me);
burst->pace_µs = (me->pace_ms & 0x3f) * 1000;
burst->pulse_width_¼_µs = me->pulse_width_µs * 4;
burst->nr_of_pulses = me->nr_of_pulses;
burst->amplitude = me->amplitude;
return burst;
}

Expand Down
41 changes: 14 additions & 27 deletions firmware/src/sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,39 +123,26 @@ static void *stateCanopy(Sequencer *me, AOEvent const *evt)
}


static bool startBurst(Burst const *burst, Deltas const *deltas)
{
if (Burst_isValid(burst)) {
return BSP_startBurst(burst, deltas);
}

BSP_logf("Invalid burst\n");
return false;
}


static void execPulseTrain(PatternIterator *pi, PulseTrain const *pt, uint16_t sz)
static bool execBurst(PatternIterator *pi, Burst *burst)
{
// Scale amplitude 0..255 to 0..8160 mV (for now).
BSP_setPrimaryVoltage_mV(PulseTrain_amplitude(pt) * 32);
PatternIterator_setPulseWidth(pi, PulseTrain_pulseWidth(pt));
Burst burst;
Deltas deltas;
startBurst(PulseTrain_getBurst(pt, &burst), PulseTrain_getDeltas(pt, sz, &deltas));
BSP_setPrimaryVoltage_mV(burst->amplitude * 32);
Deltas deltas = {0};
return BSP_startBurst(burst, &deltas);
}


static bool processNextPulseTrain(Sequencer *me)
static bool processNextBurst(Sequencer *me)
{
uint8_t pt_buf[PulseTrain_size()];
PulseTrain *pt = (PulseTrain *)pt_buf; // Alias.
bool ok = PtdQueue_getNextPtd(me->ptd_queue, pt);
if (ok) {
Burst burst;
if (PtdQueue_getNextBurst(me->ptd_queue, &burst)) {
Sequencer_notifyPtQueue(me);
PulseTrain_print(pt, sizeof pt_buf);
execPulseTrain(&me->pi, pt, sizeof pt_buf);
if (Burst_isValid(&burst)) {
Burst_print(&burst);
return execBurst(&me->pi, &burst);
}
}
return ok;
return false;
}


Expand All @@ -165,7 +152,7 @@ static void *stateStreaming(Sequencer *me, AOEvent const *evt)
{
case ET_AO_ENTRY:
BSP_logf("Sequencer_%s ENTRY\n", __func__);
processNextPulseTrain(me);
processNextBurst(me);
break;
case ET_AO_EXIT:
PtdQueue_clear(me->ptd_queue);
Expand All @@ -191,7 +178,7 @@ static void *stateStreaming(Sequencer *me, AOEvent const *evt)
break;
case ET_BURST_EXPIRED:
// BSP_logf("Burst expired\n");
if (processNextPulseTrain(me)) break;
if (processNextBurst(me)) break;
return &stateIdle; // No more descriptors, transition.
default:
return stateCanopy(me, evt); // Forward the event.
Expand Down

0 comments on commit 6bba7fc

Please sign in to comment.