Skip to content

Commit

Permalink
fix esc drivers and pgm imports
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlopez committed Jan 13, 2025
1 parent 05b675e commit 1391039
Show file tree
Hide file tree
Showing 14 changed files with 156 additions and 129 deletions.
35 changes: 18 additions & 17 deletions lib/EscDriver/src/EscDriverEsp32c3.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if defined(ESP32C3)

#include "EscDriverEsp32c3.h"
#include <Arduino.h>
#include <driver/timer.h>
#include <algorithm>

Expand All @@ -18,12 +19,12 @@ void EscDriverEsp32c3::_isr_init(EscDriverTimer timer, void * driver)
{
timer_group_t group = (timer_group_t)timer;
timer_config_t config = {
.alarm_en = TIMER_ALARM_EN,
.counter_en = TIMER_PAUSE,
.intr_type = TIMER_INTR_LEVEL,
.counter_dir = TIMER_COUNT_UP,
.auto_reload = TIMER_AUTORELOAD_DIS,
.divider = ESC_TIMER_DIVIDER,
.alarm_en = TIMER_ALARM_EN,
.counter_en = TIMER_PAUSE,
.intr_type = TIMER_INTR_LEVEL,
.counter_dir = TIMER_COUNT_UP,
.auto_reload = TIMER_AUTORELOAD_DIS,
.divider = ESC_TIMER_DIVIDER,
};
timer_init(group, ESC_TIMER_IDX, &config);
timer_set_counter_value(group, ESC_TIMER_IDX, 0);
Expand All @@ -32,7 +33,7 @@ void EscDriverEsp32c3::_isr_init(EscDriverTimer timer, void * driver)
timer_start(group, ESC_TIMER_IDX);
}

bool EscDriverEsp32c3::_isr_wait(EscDriverTimer timer, const uint32_t ticks)
bool IRAM_ATTR EscDriverEsp32c3::_isr_wait(EscDriverTimer timer, const uint32_t ticks)
{
if(ticks >= TIMER_WAIT_EDGE) { // yield
uint64_t value = timer_group_get_counter_value_in_isr((timer_group_t)timer, ESC_TIMER_IDX);
Expand All @@ -53,7 +54,7 @@ bool EscDriverEsp32c3::_isr_wait(EscDriverTimer timer, const uint32_t ticks)
}

// run as soon as possible
void EscDriverEsp32c3::_isr_start(EscDriverTimer timer)
void IRAM_ATTR EscDriverEsp32c3::_isr_start(EscDriverTimer timer)
{
_isr_wait(timer, TIMER_WAIT_EDGE + 10);
//timer_start((timer_group_t)timer, ESC_TIMER_IDX);
Expand All @@ -65,7 +66,7 @@ void EscDriverEsp32c3::_isr_end(EscDriverTimer timer, void* p)
timer_disable_intr((timer_group_t)timer, ESC_TIMER_IDX);
}

int EscDriverEsp32c3::attach(size_t channel, int pin, int pulse)
int IRAM_ATTR EscDriverEsp32c3::attach(size_t channel, int pin, int pulse)
{
if(channel < 0 || channel >= ESC_CHANNEL_COUNT) return 0;
_slots[channel].pin = pin;
Expand All @@ -75,7 +76,7 @@ int EscDriverEsp32c3::attach(size_t channel, int pin, int pulse)
return 1;
}

int EscDriverEsp32c3::write(size_t channel, int pulse)
int IRAM_ATTR EscDriverEsp32c3::write(size_t channel, int pulse)
{
if(channel < 0 || channel >= ESC_CHANNEL_COUNT) return 0;
_slots[channel].pulse = usToTicks(pulse);
Expand All @@ -93,7 +94,7 @@ uint32_t EscDriverEsp32c3::telemetry(size_t channel) const
return 0;
}

void EscDriverEsp32c3::apply()
void IRAM_ATTR EscDriverEsp32c3::apply()
{
if(_protocol == ESC_PROTOCOL_DISABLED) return;
if(_protocol >= ESC_PROTOCOL_DSHOT150)
Expand All @@ -105,7 +106,7 @@ void EscDriverEsp32c3::apply()
_isr_start(_timer);
}

bool EscDriverEsp32c3::handle(void * p)
bool IRAM_ATTR EscDriverEsp32c3::handle(void * p)
{
// Time critical section
EscDriver * instance = (EscDriver *)p;
Expand Down Expand Up @@ -145,7 +146,7 @@ bool EscDriverEsp32c3::handle(void * p)
return false;
}

void EscDriverEsp32c3::commit()
void IRAM_ATTR EscDriverEsp32c3::commit()
{
Slot sorted[ESC_CHANNEL_COUNT];
std::copy(_slots, _slots + ESC_CHANNEL_COUNT, sorted);
Expand Down Expand Up @@ -208,17 +209,17 @@ void EscDriverEsp32c3::commit()
}
}

uint32_t EscDriverEsp32c3::usToTicksReal(EscDriverTimer timer, uint32_t us)
uint32_t IRAM_ATTR EscDriverEsp32c3::usToTicksReal(EscDriverTimer timer, uint32_t us)
{
return (APB_CLK_FREQ / ESC_TIMER_DIVIDER / 1000000L) * us;
}

int32_t EscDriverEsp32c3::minTicks(EscDriverTimer timer)
int32_t IRAM_ATTR EscDriverEsp32c3::minTicks(EscDriverTimer timer)
{
return 150L;
}

uint32_t EscDriverEsp32c3::usToTicks(uint32_t us)
uint32_t IRAM_ATTR EscDriverEsp32c3::usToTicks(uint32_t us)
{
uint32_t ticks = 0;
switch(_protocol)
Expand Down Expand Up @@ -312,7 +313,7 @@ static inline void dshotDelay(int delay)
while(delay--) __asm__ __volatile__ ("nop");
}

void EscDriverEsp32c3::dshotWrite()
void IRAM_ATTR EscDriverEsp32c3::dshotWrite()
{
// zero mask arrays
mask_t * sm = dshotSetMask;
Expand Down
22 changes: 11 additions & 11 deletions lib/EscDriver/src/EscDriverEsp32c3.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ class EscDriverEsp32c3: public EscDriverBase

int begin(const EscConfig& conf);
void end();
int attach(size_t channel, int pin, int pulse) IRAM_ATTR;
int write(size_t channel, int pulse) IRAM_ATTR;
void apply() IRAM_ATTR;
int attach(size_t channel, int pin, int pulse);
int write(size_t channel, int pulse);
void apply();
int pin(size_t channel) const;
uint32_t telemetry(size_t channel) const;
static bool handle(void * p) IRAM_ATTR;
static bool handle(void * p);

private:
void commit() IRAM_ATTR;
uint32_t usToTicks(uint32_t us) IRAM_ATTR;
uint32_t usToTicksReal(EscDriverTimer timer, uint32_t us) IRAM_ATTR;
int32_t minTicks(EscDriverTimer timer) IRAM_ATTR;
void dshotWrite() IRAM_ATTR;
void commit();
uint32_t usToTicks(uint32_t us);
uint32_t usToTicksReal(EscDriverTimer timer, uint32_t us);
int32_t minTicks(EscDriverTimer timer);
void dshotWrite();

static void _isr_init(EscDriverTimer timer, void * driver);
static bool _isr_wait(EscDriverTimer timer, const uint32_t ticks) IRAM_ATTR;
static void _isr_start(EscDriverTimer timer) IRAM_ATTR;
static bool _isr_wait(EscDriverTimer timer, const uint32_t ticks);
static void _isr_start(EscDriverTimer timer);
static void _isr_end(EscDriverTimer timer, void* p);

volatile bool _busy;
Expand Down
27 changes: 14 additions & 13 deletions lib/EscDriver/src/EscDriverEsp8266.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if defined(ESP8266)

#include "EscDriverEsp8266.h"
#include <Arduino.h>
#include <algorithm>
#include <user_interface.h>

Expand Down Expand Up @@ -40,7 +41,7 @@ void EscDriverEsp8266::_isr_init(EscDriverTimer timer, void * driver)
}
}

void EscDriverEsp8266::_isr_begin(EscDriverTimer timer)
void IRAM_ATTR EscDriverEsp8266::_isr_begin(EscDriverTimer timer)
{
switch(timer)
{
Expand All @@ -62,7 +63,7 @@ void EscDriverEsp8266::_isr_begin(EscDriverTimer timer)
#define TIMER1_WAIT_EDGE 140UL
#define TIMER1_WAIT_COMP 115UL

bool EscDriverEsp8266::_isr_wait(EscDriverTimer timer, const uint32_t ticks)
bool IRAM_ATTR EscDriverEsp8266::_isr_wait(EscDriverTimer timer, const uint32_t ticks)
{
switch(timer)
{
Expand Down Expand Up @@ -103,7 +104,7 @@ bool EscDriverEsp8266::_isr_wait(EscDriverTimer timer, const uint32_t ticks)
}

// run as soon as possible
void EscDriverEsp8266::_isr_start(EscDriverTimer timer)
void IRAM_ATTR EscDriverEsp8266::_isr_start(EscDriverTimer timer)
{
switch(timer)
{
Expand All @@ -122,7 +123,7 @@ void EscDriverEsp8266::_isr_start(EscDriverTimer timer)
}
}

void EscDriverEsp8266::_isr_reboot(void* p)
void IRAM_ATTR EscDriverEsp8266::_isr_reboot(void* p)
{
EscDriver* d = (EscDriver*)p;
_isr_begin(d->_timer);
Expand Down Expand Up @@ -152,7 +153,7 @@ void EscDriverEsp8266::_isr_end(EscDriverTimer timer, void* p)
}
}

int EscDriverEsp8266::attach(size_t channel, int pin, int pulse)
int IRAM_ATTR EscDriverEsp8266::attach(size_t channel, int pin, int pulse)
{
if(channel < 0 || channel >= ESC_CHANNEL_COUNT) return 0;
_slots[channel].pin = pin;
Expand All @@ -173,14 +174,14 @@ uint32_t EscDriverEsp8266::telemetry(size_t channel) const
return 0;
}

int EscDriverEsp8266::write(size_t channel, int pulse)
int IRAM_ATTR EscDriverEsp8266::write(size_t channel, int pulse)
{
if(channel < 0 || channel >= ESC_CHANNEL_COUNT) return 0;
_slots[channel].pulse = usToTicks(pulse);
return 1;
}

void EscDriverEsp8266::apply()
void IRAM_ATTR EscDriverEsp8266::apply()
{
if(_protocol == ESC_PROTOCOL_DISABLED) return;
if(_protocol >= ESC_PROTOCOL_DSHOT150)
Expand All @@ -192,7 +193,7 @@ void EscDriverEsp8266::apply()
_isr_start(_timer);
}

void EscDriverEsp8266::handle(void * p, void * x)
void IRAM_ATTR EscDriverEsp8266::handle(void * p, void * x)
{
// Time critical section
EscDriver * instance = (EscDriver *)p;
Expand Down Expand Up @@ -234,7 +235,7 @@ void EscDriverEsp8266::handle(void * p, void * x)
}
}

void EscDriverEsp8266::commit()
void IRAM_ATTR EscDriverEsp8266::commit()
{
Slot sorted[ESC_CHANNEL_COUNT];
std::copy(_slots, _slots + ESC_CHANNEL_COUNT, sorted);
Expand Down Expand Up @@ -296,7 +297,7 @@ void EscDriverEsp8266::commit()
}
}

uint32_t EscDriverEsp8266::usToTicksReal(EscDriverTimer timer, uint32_t us)
uint32_t IRAM_ATTR EscDriverEsp8266::usToTicksReal(EscDriverTimer timer, uint32_t us)
{
switch(timer)
{
Expand All @@ -307,7 +308,7 @@ uint32_t EscDriverEsp8266::usToTicksReal(EscDriverTimer timer, uint32_t us)
}
}

int32_t EscDriverEsp8266::minTicks(EscDriverTimer timer)
int32_t IRAM_ATTR EscDriverEsp8266::minTicks(EscDriverTimer timer)
{
switch(timer)
{
Expand All @@ -318,7 +319,7 @@ int32_t EscDriverEsp8266::minTicks(EscDriverTimer timer)
}
}

uint32_t EscDriverEsp8266::usToTicks(uint32_t us)
uint32_t IRAM_ATTR EscDriverEsp8266::usToTicks(uint32_t us)
{
uint32_t ticks = 0;
switch(_protocol)
Expand Down Expand Up @@ -415,7 +416,7 @@ static IRAM_ATTR void dshotDelay(int delay)
while(delay--) __asm__ __volatile__ ("nop");
}

void EscDriverEsp8266::dshotWrite()
void IRAM_ATTR EscDriverEsp8266::dshotWrite()
{
// zero mask arrays
mask_t smask[DSHOT_BIT_COUNT];
Expand Down
31 changes: 14 additions & 17 deletions lib/EscDriver/src/EscDriverEsp8266.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef _ESC_DRIVER_ESP8266_H_
#define _ESC_DRIVER_ESP8266_H_
#pragma once

#if defined(ESP8266)

Expand Down Expand Up @@ -52,25 +51,25 @@ class EscDriverEsp8266: public EscDriverBase

int begin(const EscConfig& conf);
void end();
int attach(size_t channel, int pin, int pulse) IRAM_ATTR;
int write(size_t channel, int pulse) IRAM_ATTR;
void apply() IRAM_ATTR;
int attach(size_t channel, int pin, int pulse);
int write(size_t channel, int pulse);
void apply();
int pin(size_t channel) const;
uint32_t telemetry(size_t channel) const;
static void handle(void * p, void * x) IRAM_ATTR;
static void handle(void * p, void * x);

private:
void commit() IRAM_ATTR;
uint32_t usToTicks(uint32_t us) IRAM_ATTR;
uint32_t usToTicksReal(EscDriverTimer timer, uint32_t us) IRAM_ATTR;
int32_t minTicks(EscDriverTimer timer) IRAM_ATTR;
void dshotWrite() IRAM_ATTR;
void commit();
uint32_t usToTicks(uint32_t us);
uint32_t usToTicksReal(EscDriverTimer timer, uint32_t us);
int32_t minTicks(EscDriverTimer timer);
void dshotWrite();

static void _isr_init(EscDriverTimer timer, void * driver);
static void _isr_begin(EscDriverTimer timer) IRAM_ATTR;
static bool _isr_wait(EscDriverTimer timer, const uint32_t ticks) IRAM_ATTR;
static void _isr_start(EscDriverTimer timer) IRAM_ATTR;
static void _isr_reboot(void* p) IRAM_ATTR;
static void _isr_begin(EscDriverTimer timer);
static bool _isr_wait(EscDriverTimer timer, const uint32_t ticks);
static void _isr_start(EscDriverTimer timer);
static void _isr_reboot(void* p);
static void _isr_end(EscDriverTimer timer, void* p);

volatile bool _busy;
Expand All @@ -94,5 +93,3 @@ class EscDriverEsp8266: public EscDriverBase
};

#endif // ESP8266

#endif
11 changes: 6 additions & 5 deletions lib/EscDriver/src/EscDriverRP2040.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if defined(ARCH_RP2040)

#include "EscDriverRP2040.h"
#include <Arduino.h>
#include <hardware/gpio.h>
#include <hardware/pwm.h>
#include <hardware/dma.h>
Expand All @@ -15,7 +16,7 @@
#define DSHOT150_T1H 4666u
#define DSHOT150_T 6666u

int EscDriverRP2040::attach(size_t channel, int pin, int pulse)
int IRAM_ATTR EscDriverRP2040::attach(size_t channel, int pin, int pulse)
{
if(channel >= ESC_CHANNEL_COUNT) return 0;
if(pin > 28) return 0;
Expand Down Expand Up @@ -96,14 +97,14 @@ bool EscDriverRP2040::isSliceDriven(int slice)
return false;
}

int EscDriverRP2040::write(size_t channel, int pulse)
int IRAM_ATTR EscDriverRP2040::write(size_t channel, int pulse)
{
if(channel >= ESC_CHANNEL_COUNT) return 0;
_slots[channel].pulse = usToTicks(pulse);
return 1;
}

void EscDriverRP2040::apply()
void IRAM_ATTR EscDriverRP2040::apply()
{
if(_protocol >= ESC_PROTOCOL_DSHOT150 && _protocol <= ESC_PROTOCOL_DSHOT600)
{
Expand All @@ -117,7 +118,7 @@ void EscDriverRP2040::apply()
}
}

uint32_t EscDriverRP2040::usToTicks(uint32_t us)
uint32_t IRAM_ATTR EscDriverRP2040::usToTicks(uint32_t us)
{
uint32_t ticks = 0;
switch(_protocol)
Expand Down Expand Up @@ -146,7 +147,7 @@ uint32_t EscDriverRP2040::usToTicks(uint32_t us)
return ticks;
}

uint32_t EscDriverRP2040::usToTicksReal(uint32_t us)
uint32_t IRAM_ATTR EscDriverRP2040::usToTicksReal(uint32_t us)
{
uint64_t t = (uint64_t)us * (F_CPU / _divider);
return t / 1000000ul;
Expand Down
Loading

0 comments on commit 1391039

Please sign in to comment.