Skip to content

Commit

Permalink
fixed pure ISR IO update and update algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Paciente8159 committed Jan 11, 2024
1 parent f88487b commit d923d6a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
71 changes: 35 additions & 36 deletions uCNC/src/hal/mcus/esp32/mcu_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
#include <stdint.h>
#include <math.h>

#ifndef ESP_IO_BASE_SAMPLERATE
#define ESP_IO_BASE_SAMPLERATE 125000UL
#endif

static volatile bool esp32_global_isr_enabled;
static volatile bool mcu_itp_timer_running;
#ifdef IC74HC595_CUSTOM_SHIFT_IO
Expand Down Expand Up @@ -68,25 +72,31 @@ static flash_eeprom_t mcu_eeprom;
#endif

MCU_CALLBACK void mcu_itp_isr(void *arg);
MCU_CALLBACK void ic74hc595_shift_io_pins(void);
FORCEINLINE static void esp32_io_updater(void);
MCU_CALLBACK void mcu_gpio_isr(void *type);

/**
* IO 74HC595 expander via I2S
* **/
#ifdef IC74HC595_CUSTOM_SHIFT_IO
MCU_CALLBACK void ic74hc595_shift_io_pins(void);
#if IC74HC595_COUNT != 4
#error "IC74HC595_COUNT must be 4(bytes) to use ESP32 I2S mode for IO shifting"
#endif
#include "driver/i2s.h"
#include "soc/i2s_struct.h"

#define I2S_SAMPLE_RATE 250000UL
#ifndef I2S_SAMPLE_RATE
#define I2S_SAMPLE_RATE ESP_IO_BASE_SAMPLERATE
#endif
#define I2S_SAMPLES_PER_BUFFER (I2S_SAMPLE_RATE / 500) // number of samples per 2ms (0.002/1 = 1/500)
#define I2S_BUFFER_COUNT 5 // DMA buffer size 5 * 2ms = 10ms stored motions (can be adjusted but may cause to much or too little latency)
#define I2S_SAMPLE_US (1000000UL / I2S_SAMPLE_RATE) // (1s/250KHz = 0.000004s = 4us)

#ifndef ITP_SAMPLE_RATE
#define ITP_SAMPLE_RATE I2S_SAMPLE_RATE
#endif

static volatile uint32_t i2s_mode;
#define I2S_MODE __atomic_load_n((uint32_t *)&i2s_mode, __ATOMIC_RELAXED)

Expand Down Expand Up @@ -247,7 +257,10 @@ static FORCEINLINE void esp32_i2s_extender_init(void)
}
#endif

#ifdef IC74HC595_HAS_PWMS
#ifndef ITP_SAMPLE_RATE
#define ITP_SAMPLE_RATE ESP_IO_BASE_SAMPLERATE
#endif

#if SERVOS_MASK > 0
// also run servo pin signals
static uint32_t servo_tick_counter = 0;
Expand All @@ -261,17 +274,18 @@ void servo_reset(void);
/**
*
* This function updates IO pins
* Should run @128KHz rate. In here the following IO is calculated and updated
* In here the following IO is calculated and updated
* - step and dir pins (using a step aliasing similar to the bresenham)
* - any software PWM pins
* - all servo pins
* - any software PWM pins (@125KHz)
* - all servo pins (@125KHz)
*
* @128KHz the function should do it's calculations in under 8µs.
* @125KHz the function should do it's calculations in under 8µs.
* With 3 axis running and 4 software PWM it takes less then 900ns so it should hold.
*
* **/
static int32_t mcu_itp_timer_counter;
static int32_t mcu_itp_timer_reload;
static int16_t mcu_soft_io_counter;
static void esp32_io_updater(void)
{
static bool step_reset = true;
Expand All @@ -282,7 +296,7 @@ static void esp32_io_updater(void)
// stream mode tick
int32_t t = mcu_itp_timer_counter;
bool reset = step_reset;
t -= I2S_SAMPLE_US;
t -= (1000000UL / ITP_SAMPLE_RATE);
if (t <= 0)
{
if (!reset)
Expand All @@ -302,11 +316,14 @@ static void esp32_io_updater(void)
}
}

static bool run_half = false;
if (!run_half)
int8_t t2 = mcu_soft_io_counter;
t2--;
if (t2 <= 0)
{
// updated software PWM pins
// updated software PWM pins
#if defined(IC74HC595_HAS_PWMS) || defined(MCU_HAS_SOFT_PWM_TIMER)
io_soft_pwm_update();
#endif

// update servo pins
#if SERVOS_MASK > 0
Expand All @@ -328,11 +345,13 @@ static void esp32_io_updater(void)
// resets every 3ms
servo_tick_counter = ++counter;
// servo_tick_counter = (++counter != 384) ? counter : 0;
mcu_soft_io_counter = (ITP_SAMPLE_RATE / 125000UL);
#endif
}
run_half = !run_half;
else{
mcu_soft_io_counter = t2;
}
}
#endif

#if SERVOS_MASK > 0
static uint8_t mcu_servos[6];
Expand Down Expand Up @@ -362,63 +381,43 @@ void start_servo_timeout(uint8_t timeout);
MCU_CALLBACK void servo_update(void)
{
static uint8_t servo_counter = 0;
#ifdef IC74HC595_HAS_SERVOS
uint8_t servomask = 0;
#endif

switch (servo_counter)
{
#if ASSERT_PIN(SERVO0)
case SERVO0_FRAME:
io_set_output(SERVO0);
start_servo_timeout(mcu_servos[0]);
#ifdef IC74HC595_HAS_SERVOS
servomask = SERVO0_MASK;
#endif
break;
#endif
#if ASSERT_PIN(SERVO1)
case SERVO1_FRAME:
io_set_output(SERVO1);
start_servo_timeout(mcu_servos[1]);
#ifdef IC74HC595_HAS_SERVOS
servomask = SERVO1_MASK;
#endif
break;
#endif
#if ASSERT_PIN(SERVO2)
case SERVO2_FRAME:
io_set_output(SERVO2);
start_servo_timeout(mcu_servos[2]);
#ifdef IC74HC595_HAS_SERVOS
servomask = SERVO2_MASK;
#endif
break;
#endif
#if ASSERT_PIN(SERVO3)
case SERVO3_FRAME:
io_set_output(SERVO3);
start_servo_timeout(mcu_servos[3]);
#ifdef IC74HC595_HAS_SERVOS
servomask = SERVO3_MASK;
#endif
break;
#endif
#if ASSERT_PIN(SERVO4)
case SERVO4_FRAME:
io_set_output(SERVO4);
start_servo_timeout(mcu_servos[4]);
#ifdef IC74HC595_HAS_SERVOS
servomask = SERVO4_MASK;
#endif
break;
#endif
#if ASSERT_PIN(SERVO5)
case SERVO5_FRAME:
io_set_output(SERVO5);
start_servo_timeout(mcu_servos[5]);
#ifdef IC74HC595_HAS_SERVOS
servomask = SERVO5_MASK;
#endif
break;
#endif
}
Expand All @@ -429,7 +428,7 @@ MCU_CALLBACK void servo_update(void)

MCU_CALLBACK void start_servo_timeout(uint8_t timeout)
{
servo_tick_alarm = servo_tick_counter + timeout + 64 /*0.5ms*/;
servo_tick_alarm = servo_tick_counter + timeout + 64; /*aprox. 0.5ms*/;
}
#endif

Expand Down Expand Up @@ -594,7 +593,7 @@ void mcu_init(void)
Also, if auto_reload is set, this value will be automatically reload on alarm */
timer_set_counter_value(ITP_TIMER_TG, ITP_TIMER_IDX, 0x00000000ULL);
/* Configure the alarm value and the interrupt on alarm. */
timer_set_alarm_value(ITP_TIMER_TG, ITP_TIMER_IDX, (uint64_t)4);
timer_set_alarm_value(ITP_TIMER_TG, ITP_TIMER_IDX, (uint64_t)(1000000UL / ITP_SAMPLE_RATE));
// register PWM isr
timer_isr_register(ITP_TIMER_TG, ITP_TIMER_IDX, mcu_itp_isr, NULL, 0, NULL);
timer_enable_intr(ITP_TIMER_TG, ITP_TIMER_IDX);
Expand Down
2 changes: 1 addition & 1 deletion uCNC/src/hal/mcus/esp32/mcumap_esp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -2831,7 +2831,7 @@ extern uint32_t ic74hc595_i2s_pins;
}
#define mcu_toggle_output(X) \
{ \
__atomic_fetch_xor(&__indirect__(X, OUTREG)->OUT, 1UL << (0x1F & __indirect__(X, BIT)), __ATOMIC_RELAXED); \
__indirect__(X, OUTREG)->OUT ^= (1UL << (0x1F & __indirect__(X, BIT))); \
}

#define mcu_config_pwm(X, Y) \
Expand Down

0 comments on commit d923d6a

Please sign in to comment.