Skip to content

Commit

Permalink
Firmware revision 10 (patch 3)
Browse files Browse the repository at this point in the history
Make millisecond delay atomic to avoid missed ticks
  • Loading branch information
neoxic committed Mar 27, 2024
1 parent 1d0ccb9 commit 1b18550
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ extern const Cfg cfgdata;
extern Cfg cfg;
extern int throt, ertm, erpm, temp, volt, curr, csum, dshotval, beepval;
extern char analog, telreq, telmode, flipdir, beacon, dshotext;
extern volatile uint32_t tickms;

void init(void);
void initio(void);
Expand All @@ -124,6 +123,7 @@ void io_serial(void);
void io_analog(void);
void adctrig(void);
void adcdata(int t, int v, int c, int x);
void delay(int ms);
void kisstelem(void);
void autotelem(void);
int execcmd(char *buf);
Expand Down
13 changes: 11 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ Cfg cfg = cfgdata;

int throt, ertm, erpm, temp, volt, curr, csum, dshotval, beepval = -1;
char analog, telreq, telmode, flipdir, beacon, dshotext;
volatile uint32_t tickms;

static int step, sine, sync, ival, tval;
static char prep, accl, tick, reverse, ready, brushed;
static uint32_t tickms, tickmsv;
static volatile char tickmsf;

static void reset(void) {
__disable_irq();
Expand Down Expand Up @@ -465,7 +466,15 @@ void sys_tick_handler(void) {
SCB_ICSR = SCB_ICSR_PENDSVSET; // Continue with low priority
SCB_SCR = 0; // Resume main loop
if (++tick & 15) return; // 16kHz -> 1kHz
++tickms;
if (++tickms == tickmsv) tickmsf = 0;
}

void delay(int ms) {
__disable_irq();
tickmsv = tickms + ms;
tickmsf = 1;
__enable_irq();
while (tickmsf) TIM_EGR(GPTIM) = TIM_EGR_UG; // Reset arming timeout
}

void pend_sv_handler(void) {
Expand Down
3 changes: 1 addition & 2 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,7 @@ int playmusic(const char *str, int vol) {
if (a < 1 || a > 99) break; // Sanity check
str = end;
}
uint32_t t = tickms + tmp * a;
while (t != tickms) TIM_EGR(GPTIM) = TIM_EGR_UG; // Reset arming timeout
delay(tmp * a);
TIM1_CCR2 = 0; // Preload silence
}
#ifdef PWM_ENABLE
Expand Down

0 comments on commit 1b18550

Please sign in to comment.