Skip to content

Commit

Permalink
Pulse train streaming prep and some refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwrikbaar committed Dec 28, 2024
1 parent f77d75a commit 510672e
Show file tree
Hide file tree
Showing 16 changed files with 1,657 additions and 1,427 deletions.
3 changes: 2 additions & 1 deletion firmware/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# Author mark
# Copyright 2023, 2024 Neostim
# Copyright 2023..2025 Neostim
#

PROJECT_NAME := neodk
Expand Down Expand Up @@ -39,6 +39,7 @@ PROJ_COMMON_SRC += \
$(PROJ_DIR_SRC)/controller.c \
$(PROJ_DIR_SRC)/sequencer.c \
$(PROJ_DIR_SRC)/pattern_iter.c \
$(PROJ_DIR_SRC)/pulse_train.c \
$(PROJ_DIR_SRC)/datalink.c \
$(PROJ_DIR_SRC)/debug_cli.c \
$(PROJ_DIR_SRC)/attributes.c \
Expand Down
2,678 changes: 1,355 additions & 1,323 deletions firmware/build/neodk_g071.hex

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions firmware/inc/app_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* Created on: 22 May 2021
* Author: Mark
* Copyright 2021..2024 Neostim™
* Copyright 2021..2025 Neostim™
*/

#ifndef INC_APP_EVENT_H_
Expand All @@ -24,7 +24,8 @@ enum {
ET_CONTROLLER_CONNECTED, ET_CONTROLLER_DISCONNECTED,
ET_PLAY, ET_PAUSE, ET_STOP, ET_UNKNOWN_COMMAND, ET_TOGGLE_PLAY_PAUSE,
ET_SELECT_NEXT_PATTERN, ET_SELECT_PATTERN_BY_NAME, ET_SET_INTENSITY,
ET_BURST_REQUESTED, ET_BURST_REJECTED, ET_BURST_STARTED, ET_BURST_COMPLETED, ET_BURST_EXPIRED,
ET_QUEUE_PULSE_TRAIN,
ET_BURST_STARTED, ET_BURST_COMPLETED, ET_BURST_EXPIRED,
};

#endif
4 changes: 2 additions & 2 deletions firmware/inc/attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Created on: 15 Oct 2024
* Author: mark
* Copyright 2024 Neostim™
* Copyright 2024, 2025 Neostim™
*/

#ifndef INC_ATTRIBUTES_H_
Expand All @@ -19,7 +19,7 @@
typedef enum {
AI_FIRMWARE_VERSION = 2, AI_VOLTAGES, AI_CLOCK_MICROS,
AI_ALL_PATTERN_NAMES, AI_CURRENT_PATTERN_NAME, AI_INTENSITY_PERCENT, AI_PLAY_PAUSE_STOP,
AI_BOX_NAME
AI_BOX_NAME, AI_PT_DESCRIPTOR_QUEUE
} AttributeId;

typedef void (*AttrNotifier)(void *target, AttributeId, ElementEncoding, uint8_t const *data, uint16_t size);
Expand Down
6 changes: 3 additions & 3 deletions firmware/inc/bsp_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Created on: 6 Oct 2020
* Author: mark
* Copyright 2020..2024 Neostim™
* Copyright 2020..2025 Neostim™
*/

#ifndef INC_BSP_APP_H_
Expand All @@ -21,7 +21,7 @@ typedef struct {
uint8_t pulse_width_micros;
uint8_t pace_ms;
uint8_t nr_of_pulses;
} PulseTrain;
} Burst;


void BSP_init(void); // Get the hardware ready for action.
Expand All @@ -43,7 +43,7 @@ int BSP_closeSerialPort(int fd);
// Pulse generation related functions.
uint16_t BSP_setPrimaryVoltage_mV(uint16_t V_prim_mV);
void BSP_primaryVoltageEnable(bool must_be_on);
bool BSP_startPulseTrain(PulseTrain const *);
bool BSP_startBurst(Burst const *);

// Debugging stuff.
void BSP_triggerADC(void);
Expand Down
34 changes: 0 additions & 34 deletions firmware/inc/burst.h

This file was deleted.

4 changes: 2 additions & 2 deletions firmware/inc/pattern_iter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Created on: 6 Mar 2024
* Author: mark
* Copyright 2024 Neostim™
* Copyright 2024, 2025 Neostim™
*/

#ifndef INC_PATTERN_ITER_H_
Expand Down Expand Up @@ -40,6 +40,6 @@ bool PatternIterator_checkPattern(uint8_t const pattern[][2], uint16_t nr_of_elc
// Instance methods.
void PatternIterator_init(PatternIterator *, PatternDescr const *);
bool PatternIterator_done(PatternIterator *);
bool PatternIterator_getNextPulseTrain(PatternIterator *, PulseTrain *);
bool PatternIterator_getNextBurst(PatternIterator *, Burst *);

#endif
35 changes: 35 additions & 0 deletions firmware/inc/pulse_train.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* pulse_train.h -- the interface to the PulseTrain object.
*
* NOTICE (do not remove):
* This file is part of project NeoDK (https://github.com/Onwrikbaar/NeoDK).
* See https://github.com/Onwrikbaar/NeoDK/blob/main/LICENSE.txt for full license details.
*
* Created on: 29 Dec 2019
* Author: mark
* Copyright 2019..2025 Neostim™
*/

#ifndef INC_BURST_H_
#define INC_BURST_H_

#include <stdint.h>

typedef struct _PulseTrain PulseTrain; // Opaque type.
typedef uint8_t burst_size_t;
typedef uint8_t burst_phase_t;

// Class methods.
burst_size_t PulseTrain_size();
PulseTrain *PulseTrain_new(void *addr, burst_size_t);
PulseTrain *PulseTrain_copy(void *addr, burst_size_t, PulseTrain const *original);

// Instance methods.
PulseTrain *PulseTrain_init(PulseTrain *, uint8_t electrode_set[2], uint16_t nr_of_pulses);
void PulseTrain_setPhase(PulseTrain *, burst_phase_t phase);
burst_phase_t PulseTrain_phase(PulseTrain const *);
PulseTrain *PulseTrain_setStartTimeMicros(PulseTrain *, uint32_t start_time_micros);
uint16_t PulseTrain_nrOfPulses(PulseTrain const *);
void PulseTrain_print(PulseTrain const *);

#endif
8 changes: 4 additions & 4 deletions firmware/maolib/inc/matter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Created on: 11 October 2024
* Author: Mark
* Copyright 2024 Neostim™
* Copyright 2024, 2025 Neostim™
*/

#ifndef INC_MATTER_H_
Expand All @@ -29,9 +29,9 @@ typedef enum {
} StatusCode;

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_SIGNED_INT_1, EE_SIGNED_INT = EE_SIGNED_INT_1, EE_SIGNED_INT_2, EE_SIGNED_INT_4, EE_SIGNED_INT_8,
EE_UNSIGNED_INT_1, EE_UNSIGNED_INT = 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 = EE_FLOAT_4, EE_FLOAT_8,
EE_UTF8_1LEN, EE_UTF8_2LEN, EE_UTF8_4LEN, EE_UTF8_8LEN,
EE_BYTES_1LEN, EE_BYTES_2LEN, EE_BYTES_4LEN, EE_BYTES_8LEN,
EE_NULL, EE_STRUCT, EE_ARRAY, EE_LIST, EE_END_OF_CONTAINER
Expand Down
Binary file modified firmware/maolib/libmao.a
Binary file not shown.
24 changes: 12 additions & 12 deletions firmware/src/bsp_stm32g071.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Created on: 20 Oct 2023
* Author: mark
* Copyright 2023, 2024 Neostim™
* Copyright 2023..2025 Neostim™
*/

#include <limits.h>
Expand Down Expand Up @@ -645,7 +645,7 @@ void BSP_init()

char const *BSP_firmwareVersion()
{
return "v0.41-beta";
return "v0.42-beta";
}


Expand Down Expand Up @@ -832,27 +832,27 @@ uint16_t BSP_setPrimaryVoltage_mV(uint16_t V_prim_mV)
}


bool BSP_startPulseTrain(PulseTrain const *pt)
bool BSP_startBurst(Burst const *burst)
{
M_ASSERT(pt->pace_ms >= 5); // Repetition rate <= 200 Hz.
M_ASSERT(pt->pulse_width_micros != 0);
M_ASSERT(pt->nr_of_pulses != 0);
M_ASSERT(burst->pace_ms >= 5); // Repetition rate <= 200 Hz.
M_ASSERT(burst->pulse_width_micros != 0);
M_ASSERT(burst->nr_of_pulses != 0);

pulse_timer->ARR = pulsePaceMillisecondsToTicks(pt->pace_ms) - 1;
pulse_timer->RCR = pt->nr_of_pulses - 1;
pulse_timer->ARR = pulsePaceMillisecondsToTicks(burst->pace_ms) - 1;
pulse_timer->RCR = burst->nr_of_pulses - 1;
pulse_timer->CNT = 0;
pulse_timer->SR &= ~(TIM_SR_CC1IF | TIM_SR_CC2IF);
uint8_t phase = getPhase(pt->elcon);
uint8_t phase = getPhase(burst->elcon);
if (phase == 0) {
pulse_timer->CCR1 = pt->pulse_width_micros - 1;
pulse_timer->CCR1 = burst->pulse_width_micros - 1;
pulse_timer->DIER |= TIM_DIER_CC1IE;
} else if (phase == 1) {
pulse_timer->CCR2 = pt->pulse_width_micros - 1;
pulse_timer->CCR2 = burst->pulse_width_micros - 1;
pulse_timer->DIER |= TIM_DIER_CC2IE;
} else return false; // We only have one output stage.

pulse_timer->EGR |= TIM_EGR_UG; // Force update of the shadow registers.
setSwitches(pt->elcon[0] | pt->elcon[1]);
setSwitches(burst->elcon[0] | burst->elcon[1]);
bsp.pulse_seqnr = 0;
pulse_timer->CCR1 = 0;
pulse_timer->CCR2 = 0;
Expand Down
36 changes: 30 additions & 6 deletions firmware/src/controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Created on: 21 Aug 2024
* Author: mark
* Copyright 2024 Neostim™
* Copyright 2024, 2025 Neostim™
*/

#include <stdlib.h>
Expand Down Expand Up @@ -108,19 +108,31 @@ static void logTransaction(AttributeAction const *aa, char const *action_str)
}


static void sendStatusResponse(Controller *me, AttributeAction const *aa, StatusCode sc)
{
BSP_logf("%s %hu for attr id=%hu\n", __func__, sc, aa->attribute_id);
// TODO Implement.
}


static void handleReadRequest(Controller *me, AttributeAction const *aa)
{
switch (aa->attribute_id)
{
case AI_FIRMWARE_VERSION: {
char const *fw_version = BSP_firmwareVersion();
attributeChanged(me, AI_FIRMWARE_VERSION, EE_UTF8_1LEN, (uint8_t const *)fw_version, strlen(fw_version));
attributeChanged(me, aa->attribute_id, EE_UTF8_1LEN, (uint8_t const *)fw_version, strlen(fw_version));
break;
}
case AI_VOLTAGES:
Attribute_awaitRead(aa->attribute_id, (AttrNotifier)&attributeChanged, me);
BSP_triggerADC();
break;
case AI_CLOCK_MICROS: {
uint64_t clock_micros = BSP_microsecondsSinceBoot();
attributeChanged(me, aa->attribute_id, EE_UNSIGNED_INT, (uint8_t const *)&clock_micros, sizeof clock_micros);
break;
}
case AI_ALL_PATTERN_NAMES:
readPatternNames(me, aa);
break;
Expand All @@ -138,8 +150,10 @@ static void handleReadRequest(Controller *me, AttributeAction const *aa)
break;
default:
BSP_logf("%s: unknown attribute id=%hu\n", __func__, aa->attribute_id);
// TODO Respond with NOT_FOUND code to the UI.
sendStatusResponse(me, aa, SC_UNSUPPORTED_ATTRIBUTE);
return;
}
sendStatusResponse(me, aa, SC_SUCCESS);
}


Expand Down Expand Up @@ -186,16 +200,24 @@ static void handleWriteRequest(Controller *me, AttributeAction const *aa)
updateBoxName(me, aa->data + 2, aa->data[1]);
}
break;
case AI_PT_DESCRIPTOR_QUEUE:
if (aa->data[0] == EE_BYTES_1LEN) {
EventQueue_postEvent((EventQueue *)me->sequencer, ET_QUEUE_PULSE_TRAIN, aa->data + 2, aa->data[1]);
}
break;
default:
BSP_logf("%s: unknown attribute id=%hu\n", __func__, aa->attribute_id);
// TODO Respond with NOT_FOUND code.
sendStatusResponse(me, aa, SC_UNSUPPORTED_ATTRIBUTE);
return;
}
sendStatusResponse(me, aa, SC_SUCCESS);
}


static void handleSubscribeRequest(Controller *me, AttributeAction const *aa)
{
Attribute_subscribe(aa->attribute_id, (AttrNotifier)&attributeChanged, me);
// Do not send a status response here. It will be sent later.
}


Expand All @@ -210,8 +232,10 @@ static void handleInvokeRequest(Controller *me, AttributeAction const *aa)
break;
default:
BSP_logf("%s: unknown attribute id=%hu\n", __func__, aa->attribute_id);
// TODO Respond with NOT_FOUND code.
sendStatusResponse(me, aa, SC_UNSUPPORTED_ATTRIBUTE);
return;
}
sendStatusResponse(me, aa, SC_SUCCESS);
}


Expand All @@ -238,7 +262,7 @@ static void handleRequest(Controller *me, AttributeAction const *aa)
break;
default:
BSP_logf("%s, unknown opcode 0x%02hhx\n", __func__, aa->opcode);
break;
sendStatusResponse(me, aa, SC_UNSUPPORTED_COMMAND);
}
}

Expand Down
4 changes: 2 additions & 2 deletions firmware/src/neodk_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Created on: Oct 20, 2023
* Author: mark
* Copyright 2023, 2024 Neostim™
* Copyright 2023..2025 Neostim™
*/

#include <stdlib.h>
Expand Down Expand Up @@ -145,7 +145,7 @@ static void setupAndRunApplication(Boss *me)
int main()
{
BSP_initDebug();
BSP_logf("Initialising...\n");
BSP_logf("Initialising %s...\n", BSP_firmwareVersion());
BSP_init(); // Get the hardware ready.

Boss boss; // The supervisor object.
Expand Down
14 changes: 7 additions & 7 deletions firmware/src/pattern_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* Created on: 6 Mar 2024
* Author: mark
* Copyright 2024 Neostim™
* Copyright 2024, 2025 Neostim™
*/

#include <stdbool.h>
Expand Down Expand Up @@ -80,14 +80,14 @@ bool PatternIterator_done(PatternIterator *me)
}


bool PatternIterator_getNextPulseTrain(PatternIterator *me, PulseTrain *pt)
bool PatternIterator_getNextBurst(PatternIterator *me, Burst *burst)
{
if (PatternIterator_done(me)) return false;

uint8_t const *elcon = getNextPattern(me, &pt->nr_of_pulses);
pt->elcon[0] = elcon[0];
pt->elcon[1] = elcon[1];
pt->pulse_width_micros = me->pulse_width_micros;
pt->pace_ms = me->pattern_descr->pace_ms; // Yields 1000/pace_ms pulses per second.
uint8_t const *elcon = getNextPattern(me, &burst->nr_of_pulses);
burst->elcon[0] = elcon[0];
burst->elcon[1] = elcon[1];
burst->pulse_width_micros = me->pulse_width_micros;
burst->pace_ms = me->pattern_descr->pace_ms;// Yields 1000/pace_ms pulses per second.
return true;
}
Loading

0 comments on commit 510672e

Please sign in to comment.