Skip to content

Commit

Permalink
MARLIN-E3S1PROFORK-BYTT initial commit
Browse files Browse the repository at this point in the history
This is MARLIN-E3S1PRO-FORK-BYTT Release Candidate 1.

This fork was ported from the Original Creality Ender 3 S1 Pro Code from here:
https://github.com/CrealityOfficial/Ender-3S1/tree/s1_pro

This port was created by Thomas Toka.

This code is based on Marlin bugfix-2.1.x taken from:
Marlin Github
on the 21st may 2023 after this commit:
MarlinFirmware@06b1f85

Other Code was implemented from:
Creality Github
Synman Github
Mriscoc Github
johncarlson21 Github

Initial commit code taken from the Creality Ender 3 S1 Github Repo:
CrealityOfficial/Ender-3S1@f8be17f

Crosschecked with the initial and following commits of Synman:
synman@25d641e

Laser implementation partly taken from Creality´s commit from:
CrealityOfficial/Ender-3S1@cb5f3c3

Extended temperature table from mriscoc from:
https://github.com/mriscoc/Ender3V2S1/blob/Ender3V2S1-Released/Marlin/src/module/thermistor/thermistor_1.h#L27

Cardreader Update inspiration taken from:
https://github.com/CrealityOfficial/CR-10-Smart-Pro/blob/main/Marlin/src/lcd/dwin/lcd_rts.cpp

Autopid implementation taken from here:
https://github.com/CrealityOfficial/Ender-5S1

Meshviewer taken from:
https://github.com/johncarlson21/SV04-Marlin-2.1.x
  • Loading branch information
ThomasToka committed Jun 10, 2023
1 parent 683916e commit d5edd7b
Show file tree
Hide file tree
Showing 103 changed files with 11,319 additions and 315 deletions.
590 changes: 475 additions & 115 deletions Marlin/Configuration.h

Large diffs are not rendered by default.

208 changes: 126 additions & 82 deletions Marlin/Configuration_adv.h

Large diffs are not rendered by default.

111 changes: 111 additions & 0 deletions Marlin/src/HAL/STM32/timers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,115 @@ static constexpr bool verify_no_timer_conflicts() {
// when hovering over it, making it easy to identify the conflicting timers.
static_assert(verify_no_timer_conflicts(), "One or more timer conflict detected. Examine \"timers_in_use\" to help identify conflict.");

#if ALL(E3S1PRO_RTS, HAS_CUTTER)

#define LASER_TIMER_NUM 3
#define LASER_TIMER_DEV _TIMER_DEV(LASER_TIMER_NUM)
#define LASER_TIMER_PRESCALE(timer_clk, freq) ((timer_clk) / ((freq) * (LASER_TIMER_PWM_MAX + 1)))
#define LASER_TIMER_IRQ_PRIO 1

typedef enum
{
LASER_PWM_STATE_L = 0,
LASER_PWM_STATE_H
}laser_pwm_state_t;

static HardwareTimer *timer_laser = nullptr;
static uint8_t laser_h = 0, laser_l = 0;
static laser_pwm_state_t laser_pwm_state;

FORCE_INLINE static void laser_timer_set_compare(const hal_timer_t overflow)
{
if (timer_laser)
{
timer_laser->setOverflow(overflow + 1, TICK_FORMAT);
if (overflow < timer_laser->getCount())
timer_laser->refresh();
}
}

static void laser_timer_handler(void)
{
// SERIAL_ECHO_MSG("laser_timer_handler");

switch(laser_pwm_state)
{
case LASER_PWM_STATE_L:
laser_timer_set_compare(laser_h);
WRITE(LASER_SOFT_PWM_PIN, 1);
laser_pwm_state = LASER_PWM_STATE_H;
break;
case LASER_PWM_STATE_H:
laser_timer_set_compare(laser_l);
WRITE(LASER_SOFT_PWM_PIN, 0);
laser_pwm_state = LASER_PWM_STATE_L;
break;
}
}

void laser_timer_soft_pwm_start(uint8_t pwm)
{
// SERIAL_ECHOLNPAIR("laser_timer_soft_pwm_start():", pwm);

if(timer_laser == nullptr) return;

if(pwm > LASER_TIMER_PWM_MAX) pwm = LASER_TIMER_PWM_MAX;

if(pwm == 0x00)
{
laser_timer_soft_pwm_stop();
}
else if(pwm == 0xFF)
{
timer_laser->pause();
OUT_WRITE(LASER_SOFT_PWM_PIN, 1);
}
else
{
timer_laser->pause();
laser_pwm_state = LASER_PWM_STATE_H;
WRITE(LASER_SOFT_PWM_PIN, 1);
laser_l = LASER_TIMER_PWM_MAX - pwm;
laser_h = pwm;
laser_timer_set_compare(laser_h);
timer_laser->resume();
// 立即进入中断 -> 设置为0无法触发中断
// laser_timer_set_compare(0);
}
}


void laser_timer_soft_pwm_stop(void)
{
// SERIAL_ECHO_MSG("laser_timer_soft_pwm_stop()");

laser_timer_soft_pwm_start(1);
}

void laser_timer_soft_pwm_close()
{
// SERIAL_ECHO_MSG("laser_timer_soft_pwm_close()");

if(timer_laser == nullptr) return;

timer_laser->pause();
WRITE(LASER_SOFT_PWM_PIN, 0);
}

void laser_timer_soft_pwm_init(const uint32_t frequency)
{
if(timer_laser == nullptr)
{
timer_laser = new HardwareTimer(LASER_TIMER_DEV);
uint32_t prescale = LASER_TIMER_PRESCALE(timer_laser->getTimerClkFreq(), frequency);
timer_laser->setPrescaleFactor(prescale);
timer_laser->setOverflow(_MIN(hal_timer_t(HAL_TIMER_TYPE_MAX), (timer_laser->getTimerClkFreq()) / (prescale) /* /frequency */), TICK_FORMAT);
timer_laser->setPreloadEnable(false);
if(!timer_laser->hasInterrupt()) timer_laser->attachInterrupt(laser_timer_handler);
timer_laser->pause();
timer_laser->setInterruptPriority(LASER_TIMER_IRQ_PRIO, 0);
}
}
#endif //#if HAS_CUTTER

#endif // HAL_STM32
10 changes: 10 additions & 0 deletions Marlin/src/HAL/STM32/timers.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,13 @@ FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const ha

#define HAL_timer_isr_prologue(T) NOOP
#define HAL_timer_isr_epilogue(T) NOOP

#if ALL(E3S1PRO_RTS, HAS_CUTTER)
#define LASER_TIMER_FREQUENCY 1000 // PWM freq:1000Hz
#define LASER_TIMER_PWM_MAX 255 // PWM value range: 0~255

void laser_timer_soft_pwm_init(const uint32_t frequency);
void laser_timer_soft_pwm_start(uint8_t pwm);
void laser_timer_soft_pwm_stop(void);
void laser_timer_soft_pwm_close();
#endif
74 changes: 74 additions & 0 deletions Marlin/src/HAL/STM32F1/timers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,78 @@ timer_dev* HAL_get_timer_dev(int number) {
}
}

#if ALL(E3S1PRO_RTS, HAS_CUTTER)
static unsigned int sw=0, laser_h = 0, laser_l = 0;

void laser_timer_soft_pwm_start(uint8_t pwm)
{
//SERIAL_ECHOLNPAIR("laser_timer_soft_pwm_start():", pwm);

if(pwm>255) pwm =255;
if(pwm<=0) pwm = 1;

if(pwm <= 0X00){//
timer_pause(LASER_TIMER_DEV);
WRITE(LASER_SOFT_PWM_PIN, 0); //WRITE(PC0, 0);
}else if(pwm>=0xFE){
timer_pause(LASER_TIMER_DEV);

WRITE(LASER_SOFT_PWM_PIN, 1); //WRITE(PC0, 0);
}else{
timer_pause(LASER_TIMER_DEV);
sw = 0;
laser_l = 256 - pwm;
laser_h = pwm;
timer_resume(LASER_TIMER_DEV);
}
}

void laser_timer_soft_pwm_stop(void)
{
// SERIAL_ECHO_MSG("laser_timer_soft_pwm_stop()");
// SERIAL_ECHOLNPAIR(str, val)

laser_timer_soft_pwm_start(1);
}

void laser_timer_soft_pwm_close()
{
timer_pause(LASER_TIMER_DEV);
WRITE(LASER_SOFT_PWM_PIN, 0);//WRITE(PC0, 0);
}

void laser_timer_handler(void)
{
// SERIAL_ECHO_MSG("laser_timer_handler");

switch(sw)
{
case 0:
timer_set_reload(LASER_TIMER_DEV, laser_l);//0xFFFF);
WRITE(LASER_SOFT_PWM_PIN, 1);//WRITE(PC0, 1);
sw = 1;
break;
case 1:
timer_set_reload(LASER_TIMER_DEV, laser_h);//0xFFFF);
WRITE(LASER_SOFT_PWM_PIN, 0);//WRITE(PC0, 0);
sw=0;
break;
}
}

void laser_timer_soft_pwm_init(const uint32_t frequency)
{
timer_pause(LASER_TIMER_DEV);
//timer_set_mode(LASER_TIMER_DEV, TEMP_TIMER_CHAN, TIMER_OUTPUT_COMPARE);
timer_set_count(LASER_TIMER_DEV, 0);
timer_set_prescaler(LASER_TIMER_DEV, (uint16_t)(LASER_TIMER_PRESCALE(frequency) - 1));
timer_set_reload(LASER_TIMER_DEV, LASER_TIMER_PWM_MAX);//0xFFFF);
//timer_set_compare(LASER_TIMER_DEV, TEMP_TIMER_CHAN, _MIN(hal_timer_t(HAL_TIMER_TYPE_MAX), (F_CPU) / (LASER_TIMER_PRESCALE) / frequency));
timer_attach_interrupt(LASER_TIMER_DEV, TEMP_TIMER_CHAN, laser_timer_handler);
timer_set_interrupt_priority(LASER_TIMER_NUM, LASER_TIMER_IRQ_PRIO);
timer_generate_update(LASER_TIMER_DEV);
// timer_resume(LASER_TIMER_DEV);
}
#endif //#if HAS_CUTTER

#endif // __STM32F1__
15 changes: 15 additions & 0 deletions Marlin/src/HAL/STM32F1/timers.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,18 @@ FORCE_INLINE static void timer_no_ARR_preload_ARPE(timer_dev *dev) {
void HAL_timer_set_interrupt_priority(uint_fast8_t timer_num, uint_fast8_t priority);

#define TIMER_OC_NO_PRELOAD 0 // Need to disable preload also on compare registers.

#if ALL(E3S1PRO_RTS, HAS_CUTTER)
#define LASER_TIMER_NUM 3
#define LASER_TIMER_DEV TIMER_DEV(LASER_TIMER_NUM)
#define LASER_TIMER_FREQUENCY 1000 // PWM freq:1000Hz
#define LASER_TIMER_PWM_MAX 255 // PWM value range: 0~255
#define LASER_TIMER_PRESCALE(freq) (HAL_TIMER_RATE / (freq * (LASER_TIMER_PWM_MAX + 1))) // (72M/1000*256)=281
#define LASER_TIMER_CHAN 1
#define LASER_TIMER_IRQ_PRIO 1

void laser_timer_soft_pwm_init(const uint32_t frequency);
void laser_timer_soft_pwm_start(uint8_t pwm);
void laser_timer_soft_pwm_stop(void);
void laser_timer_soft_pwm_close();
#endif
Loading

0 comments on commit d5edd7b

Please sign in to comment.