Skip to content

Commit

Permalink
Pulse train queue and bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Jan 7, 2025
1 parent 874a370 commit 6f70bc4
Show file tree
Hide file tree
Showing 7 changed files with 1,628 additions and 1,577 deletions.
3,108 changes: 1,562 additions & 1,546 deletions firmware/build/neodk_g071.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion firmware/inc/pulse_train.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ uint16_t PulseTrain_amplitude(PulseTrain const *);
uint8_t PulseTrain_pulseWidth(PulseTrain const *);
Burst const *PulseTrain_getBurst(PulseTrain const *, Burst *);
void PulseTrain_getDeltas(PulseTrain const *, Deltas *);
void PulseTrain_print(PulseTrain const *);
void PulseTrain_print(PulseTrain const *, uint16_t sz);

#endif
6 changes: 3 additions & 3 deletions firmware/src/attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static SubscriptionId setSubForId(AttributeId ai, AttrNotifier notify, void *tar

SubscriptionId Attribute_awaitRead(AttributeId ai, AttrNotifier notify, void *target)
{
BSP_logf("%s for id=%hu\n", __func__, ai);
// BSP_logf("%s for id=%hu\n", __func__, ai);
return setSubForId(ai, notify, target, 1);
}

Expand All @@ -75,11 +75,11 @@ SubscriptionId Attribute_subscribe(AttributeId ai, AttrNotifier notify, void *ta
void Attribute_changed(AttributeId ai, ElementEncoding enc, uint8_t const *data, uint16_t size)
{
Subscription *sub = findSubForId(ai);
if (sub == NULL) return; // No subscription for this attribute.
if (sub == NULL) return;

sub->notify(sub->target, ai, enc, data, size);
if (sub->times == 1) { // Subscription expired?
BSP_logf("Cancelling subscription for id=%hu\n", sub->ai);
// BSP_logf("Cancelling subscription for id=%hu\n", sub->ai);
*sub = subscriptions[--nr_of_subs]; // Cancel it.
} else if (sub->times != 0) {
sub->times -= 1;
Expand Down
11 changes: 6 additions & 5 deletions firmware/src/bsp_stm32g071.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@


// #define ADC_TIMER_FREQ_Hz 100000UL
#define PULSE_TIMER_FREQ_Hz 4000000UL
#define PULSE_TIMER_FREQ_Hz 1000000UL
#define APP_TIMER_FREQ_Hz 4000000UL
#define TICKS_PER_MICROSECOND (APP_TIMER_FREQ_Hz / 1000000UL)

Expand Down Expand Up @@ -638,7 +638,7 @@ void BSP_init()

char const *BSP_firmwareVersion()
{
return "v0.44-beta";
return "v0.45-beta";
}


Expand Down Expand Up @@ -827,7 +827,8 @@ uint16_t BSP_setPrimaryVoltage_mV(uint16_t V_prim_mV)

bool BSP_startBurst(Burst const *burst)
{
M_ASSERT(burst->pace_ms >= 5); // Repetition rate <= 200 Hz.
M_ASSERT(burst->pace_ms >= 5); // Repetition rate <= 200 Hz.
M_ASSERT(burst->pace_ms <= 65); // Repetition rate > 15 Hz.
M_ASSERT(burst->pulse_width_¼_µs != 0);
M_ASSERT(burst->nr_of_pulses != 0);

Expand All @@ -836,10 +837,10 @@ bool BSP_startBurst(Burst const *burst)
pulse_timer->CNT = 0;
pulse_timer->SR &= ~(TIM_SR_CC1IF | TIM_SR_CC2IF);
if (burst->phase == 0) {
pulse_timer->CCR1 = burst->pulse_width_¼_µs - 1;
pulse_timer->CCR1 = (burst->pulse_width_¼_µs + 2) / 4 - 1;
pulse_timer->DIER |= TIM_DIER_CC1IE;
} else if (burst->phase == 1) {
pulse_timer->CCR2 = burst->pulse_width_¼_µs - 1;
pulse_timer->CCR2 = (burst->pulse_width_¼_µs + 2) / 4 - 1;
pulse_timer->DIER |= TIM_DIER_CC2IE;
} else return false; // We only have one output stage.

Expand Down
5 changes: 3 additions & 2 deletions firmware/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ static void sendStatusResponse(Controller *me, AttributeAction const *aa, Status
{
if (sc != SC_SUCCESS) BSP_logf("%s %hu for attr id=%hu\n", __func__, sc, aa->attribute_id);
uint16_t nbtw = sizeof(PacketHeader) + sizeof(AttributeAction);
uint8_t packet[nbtw];
uint8_t packet[nbtw + Matter_encodedDataLength(EE_UNSIGNED_INT, 1)];
initResponsePacket((PacketHeader *)packet);
initAttributeAction((AttributeAction *)(packet + sizeof(PacketHeader)), aa->transaction_id, sc, aa->attribute_id);
initAttributeAction((AttributeAction *)(packet + sizeof(PacketHeader)), aa->transaction_id, OC_STATUS_RESPONSE, aa->attribute_id);
nbtw += Matter_encode(packet + nbtw, EE_UNSIGNED_INT, &sc, 1);
DataLink_sendDatagram(me->datalink, packet, nbtw);
}

Expand Down
5 changes: 3 additions & 2 deletions firmware/src/pulse_train.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ void PulseTrain_setDeltas(PulseTrain *me, int16_t delta_width_¼_µs, int8_t del
}


void PulseTrain_print(PulseTrain const *me)
void PulseTrain_print(PulseTrain const *me, uint16_t sz)
{
BSP_logf("Pt %3hhu: t=%u µs, ec=0x%x<>0x%x, phase=%hhu, np=%hu, pace=%hhu ms, amp=%hhu, pw=%hhu µs, Δ=%hhd ¼µs\n",
me->sequence_number, me->start_time_µs, me->electrode_set[0], me->electrode_set[1],
me->phase, me->nr_of_pulses, me->pace_ms, me->amplitude, me->pulse_width_µs, me->delta_pulse_width_¼_µs);
me->phase, me->nr_of_pulses, me->pace_ms, me->amplitude, me->pulse_width_µs,
sz >= 16 ? me->delta_pulse_width_¼_µs : 0);
}
68 changes: 50 additions & 18 deletions firmware/src/sequencer.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static PatternDescr const pattern_descriptors[] =
.name = "Jackhammer",
.pattern = pattern_jackhammer,
.nr_of_elcons = M_DIM(pattern_jackhammer),
.pace_ms = 125,
.pace_ms = 65,
.nr_of_steps = 3,
.nr_of_reps = 200,
},
Expand Down Expand Up @@ -234,19 +234,6 @@ static void handleAdcValues(uint16_t const *v)
}


static bool queuePulseTrain(Sequencer *me, PulseTrain const *pt, uint16_t sz)
{
PulseTrain_print(pt);
// Scale amplitude 0..255 to 0..8160 mV (for now).
BSP_setPrimaryVoltage_mV(PulseTrain_amplitude(pt) * 32);
setPulseWidth(me, PulseTrain_pulseWidth(pt));
Sequencer_notifyPtQueue(me); // Just testing!
// Execute immediately, for now.
Burst burst;
return BSP_startBurst(PulseTrain_getBurst(pt, &burst));
}


static void *stateNop(Sequencer *me, AOEvent const *evt)
{
BSP_logf("Sequencer_%s unexpected event: %u\n", __func__, AOEvent_type(evt));
Expand Down Expand Up @@ -288,22 +275,67 @@ static void *stateCanopy(Sequencer *me, AOEvent const *evt)
}


static void handleDescriptor(Sequencer *me, AOEvent const *evt)
{
uint16_t sz = AOEvent_dataSize(evt);
// TODO Check size.
PulseTrain const *pt = (PulseTrain const *)AOEvent_data(evt);
PulseTrain_print(pt, sz);
// Scale amplitude 0..255 to 0..8160 mV (for now).
BSP_setPrimaryVoltage_mV(PulseTrain_amplitude(pt) * 32);
setPulseWidth(me, PulseTrain_pulseWidth(pt));
Burst burst;
BSP_startBurst(PulseTrain_getBurst(pt, &burst));
}


static bool enQueueDescriptor(Sequencer *me, AOEvent const *evt)
{
if (EventQueue_repostEvent(&me->ptd_queue, evt)) {
Sequencer_notifyPtQueue(me);
return true;
}

return false;
}


static bool handleQueuedDescriptor(Sequencer *me)
{
if (EventQueue_handleNextEvent(&me->ptd_queue, (EvtFunc)&handleDescriptor, me)) {
Sequencer_notifyPtQueue(me);
return true;
}

return false;
}


static void *stateStreaming(Sequencer *me, AOEvent const *evt)
{
switch (AOEvent_type(evt))
{
case ET_AO_ENTRY:
BSP_logf("Sequencer_%s ENTRY\n", __func__);
handleQueuedDescriptor(me);
break;
case ET_AO_EXIT:
BSP_logf("Sequencer_%s EXIT\n", __func__);
break;
case ET_QUEUE_PULSE_TRAIN:
enQueueDescriptor(me, evt);
break;
case ET_SELECT_NEXT_PATTERN:
case ET_SELECT_PATTERN_BY_NAME:
// Ignore for now.
break;
case ET_BURST_STARTED:
break;
case ET_BURST_COMPLETED:
break;
case ET_BURST_EXPIRED:
return &stateIdle; // Transition.
if (handleQueuedDescriptor(me)) break;
return &stateIdle; // Otherwise transition.
default:
return stateCanopy(me, evt); // Forward the event.
}
Expand All @@ -328,8 +360,8 @@ static void *stateIdle(Sequencer *me, AOEvent const *evt)
CLI_logf("Starting '%s'\n", me->pi.pattern_descr->name);
return &statePulsing; // Transition.
case ET_QUEUE_PULSE_TRAIN:
if (queuePulseTrain(me, (PulseTrain const *)AOEvent_data(evt), AOEvent_dataSize(evt))) {
return &stateStreaming; // Transition.
if (enQueueDescriptor(me, evt)) {
return &stateStreaming; // Transition;
}
break;
case ET_BURST_EXPIRED:
Expand Down Expand Up @@ -532,7 +564,7 @@ void Sequencer_notifyPlayState(Sequencer const *me)

void Sequencer_notifyPtQueue(Sequencer const *me)
{
uint16_t nr_of_bytes_free = EventQueue_availableSpace(&me->ptd_queue);
uint16_t nr_of_bytes_free = EventQueue_availableSpace(&me->ptd_queue) - AOEvent_minimumSize();
Attribute_changed(AI_PT_DESCRIPTOR_QUEUE, EE_UNSIGNED_INT_2, (uint8_t const *)&nr_of_bytes_free, sizeof nr_of_bytes_free);
}

Expand Down

0 comments on commit 6f70bc4

Please sign in to comment.