Skip to content

Commit 02735ff

Browse files
thinkyheadmh-dm
authored andcommitted
⏪️ Refactor still needs work
Reverting MarlinFirmware#23295
1 parent 8529e10 commit 02735ff

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1279
-1749
lines changed

Marlin/src/HAL/AVR/HAL.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
// ------------------------
3737

3838
// Don't initialize/override variable (which would happen in .init4)
39-
uint8_t MarlinHAL::reset_reason __attribute__((section(".noinit")));
39+
uint8_t reset_reason __attribute__((section(".noinit")));
4040

4141
// ------------------------
4242
// Public functions
@@ -45,22 +45,22 @@ uint8_t MarlinHAL::reset_reason __attribute__((section(".noinit")));
4545
__attribute__((naked)) // Don't output function pro- and epilogue
4646
__attribute__((used)) // Output the function, even if "not used"
4747
__attribute__((section(".init3"))) // Put in an early user definable section
48-
void save_reset_reason() {
48+
void HAL_save_reset_reason() {
4949
#if ENABLED(OPTIBOOT_RESET_REASON)
5050
__asm__ __volatile__(
5151
A("STS %0, r2")
52-
: "=m"(hal.reset_reason)
52+
: "=m"(reset_reason)
5353
);
5454
#else
55-
hal.reset_reason = MCUSR;
55+
reset_reason = MCUSR;
5656
#endif
5757

5858
// Clear within 16ms since WDRF bit enables a 16ms watchdog timer -> Boot loop
59-
hal.clear_reset_source();
59+
MCUSR = 0;
6060
wdt_disable();
6161
}
6262

63-
void MarlinHAL::init() {
63+
void HAL_init() {
6464
// Init Servo Pins
6565
#define INIT_SERVO(N) OUT_WRITE(SERVO##N##_PIN, LOW)
6666
#if HAS_SERVO_0
@@ -77,7 +77,7 @@ void MarlinHAL::init() {
7777
#endif
7878
}
7979

80-
void MarlinHAL::reboot() {
80+
void HAL_reboot() {
8181
#if ENABLED(USE_WATCHDOG)
8282
while (1) { /* run out the watchdog */ }
8383
#else

Marlin/src/HAL/AVR/HAL.h

Lines changed: 67 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,26 @@
7474
#define CRITICAL_SECTION_START() unsigned char _sreg = SREG; cli()
7575
#define CRITICAL_SECTION_END() SREG = _sreg
7676
#endif
77-
78-
#define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
77+
#define ISRS_ENABLED() TEST(SREG, SREG_I)
78+
#define ENABLE_ISRS() sei()
79+
#define DISABLE_ISRS() cli()
7980

8081
// ------------------------
8182
// Types
8283
// ------------------------
8384

8485
typedef int8_t pin_t;
8586

86-
// Use shared/servos.cpp
8787
#define SHARED_SERVOS HAS_SERVOS
88-
89-
class Servo;
90-
typedef Servo hal_servo_t;
88+
#define HAL_SERVO_LIB Servo
9189

9290
// ------------------------
93-
// Serial ports
91+
// Public Variables
9492
// ------------------------
9593

94+
extern uint8_t reset_reason;
95+
96+
// Serial ports
9697
#ifdef USBCON
9798
#include "../../core/serial_hook.h"
9899
typedef ForwardSerial1Class< decltype(Serial) > DefaultSerial1;
@@ -141,31 +142,20 @@ typedef Servo hal_servo_t;
141142
#endif
142143
#endif
143144

144-
//
145-
// ADC
146-
//
147-
#define HAL_ADC_VREF 5.0
148-
#define HAL_ADC_RESOLUTION 10
145+
// ------------------------
146+
// Public functions
147+
// ------------------------
149148

150-
//
151-
// Pin Mapping for M42, M43, M226
152-
//
153-
#define GET_PIN_MAP_PIN(index) index
154-
#define GET_PIN_MAP_INDEX(pin) pin
155-
#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
149+
void HAL_init();
156150

157-
#define HAL_SENSITIVE_PINS 0, 1,
151+
//void cli();
158152

159-
#ifdef __AVR_AT90USB1286__
160-
#define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
161-
#endif
153+
//void _delay_ms(const int delay);
162154

163-
// AVR compatibility
164-
#define strtof strtod
155+
inline void HAL_clear_reset_source() { }
156+
inline uint8_t HAL_get_reset_source() { return reset_reason; }
165157

166-
// ------------------------
167-
// Class Utilities
168-
// ------------------------
158+
void HAL_reboot();
169159

170160
#pragma GCC diagnostic push
171161
#if GCC_VERSION <= 50000
@@ -176,91 +166,63 @@ extern "C" int freeMemory();
176166

177167
#pragma GCC diagnostic pop
178168

179-
// ------------------------
180-
// MarlinHAL Class
181-
// ------------------------
182-
183-
class MarlinHAL {
184-
public:
169+
// ADC
170+
#ifdef DIDR2
171+
#define HAL_ANALOG_SELECT(ind) do{ if (ind < 8) SBI(DIDR0, ind); else SBI(DIDR2, ind & 0x07); }while(0)
172+
#else
173+
#define HAL_ANALOG_SELECT(ind) SBI(DIDR0, ind);
174+
#endif
185175

186-
// Earliest possible init, before setup()
187-
MarlinHAL() {}
176+
inline void HAL_adc_init() {
177+
ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
178+
DIDR0 = 0;
179+
#ifdef DIDR2
180+
DIDR2 = 0;
181+
#endif
182+
}
188183

189-
static void init(); // Called early in setup()
190-
static inline void init_board() {} // Called less early in setup()
191-
static void reboot(); // Restart the firmware from 0x0
184+
#define SET_ADMUX_ADCSRA(ch) ADMUX = _BV(REFS0) | (ch & 0x07); SBI(ADCSRA, ADSC)
185+
#ifdef MUX5
186+
#define HAL_START_ADC(ch) if (ch > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
187+
#else
188+
#define HAL_START_ADC(ch) ADCSRB = 0; SET_ADMUX_ADCSRA(ch)
189+
#endif
192190

193-
static inline bool isr_state() { return TEST(SREG, SREG_I); }
194-
static inline void isr_on() { sei(); }
195-
static inline void isr_off() { cli(); }
191+
#define HAL_ADC_VREF 5.0
192+
#define HAL_ADC_RESOLUTION 10
193+
#define HAL_READ_ADC() ADC
194+
#define HAL_ADC_READY() !TEST(ADCSRA, ADSC)
196195

197-
static inline void delay_ms(const int ms) { _delay_ms(ms); }
196+
#define GET_PIN_MAP_PIN(index) index
197+
#define GET_PIN_MAP_INDEX(pin) pin
198+
#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
198199

199-
// Tasks, called from idle()
200-
static inline void idletask() {}
200+
#define HAL_SENSITIVE_PINS 0, 1,
201201

202-
// Reset
203-
static uint8_t reset_reason;
204-
static inline uint8_t get_reset_source() { return reset_reason; }
205-
static inline void clear_reset_source() { MCUSR = 0; }
202+
#ifdef __AVR_AT90USB1286__
203+
#define JTAG_DISABLE() do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
204+
#endif
206205

207-
// Free SRAM
208-
static inline int freeMemory() { return ::freeMemory(); }
206+
// AVR compatibility
207+
#define strtof strtod
209208

210-
//
211-
// ADC Methods
212-
//
209+
#define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
213210

214-
// Called by Temperature::init once at startup
215-
static inline void adc_init() {
216-
ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
217-
DIDR0 = 0;
218-
#ifdef DIDR2
219-
DIDR2 = 0;
220-
#endif
221-
}
211+
/**
212+
* set_pwm_frequency
213+
* Sets the frequency of the timer corresponding to the provided pin
214+
* as close as possible to the provided desired frequency. Internally
215+
* calculates the required waveform generation mode, prescaler and
216+
* resolution values required and sets the timer registers accordingly.
217+
* NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
218+
* NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
219+
*/
220+
void set_pwm_frequency(const pin_t pin, int f_desired);
222221

223-
// Called by Temperature::init for each sensor at startup
224-
static inline void adc_enable(const uint8_t ch) {
225-
#ifdef DIDR2
226-
if (ch > 7) { SBI(DIDR2, ch & 0x07); return; }
227-
#endif
228-
SBI(DIDR0, ch);
229-
}
230-
231-
// Begin ADC sampling on the given channel
232-
static inline void adc_start(const uint8_t ch) {
233-
#ifdef MUX5
234-
ADCSRB = ch > 7 ? _BV(MUX5) : 0;
235-
#else
236-
ADCSRB = 0;
237-
#endif
238-
ADMUX = _BV(REFS0) | (ch & 0x07);
239-
SBI(ADCSRA, ADSC);
240-
}
241-
242-
// Is the ADC ready for reading?
243-
static inline bool adc_ready() { return !TEST(ADCSRA, ADSC); }
244-
245-
// The current value of the ADC register
246-
static inline __typeof__(ADC) adc_value() { return ADC; }
247-
248-
/**
249-
* Set the PWM duty cycle for the pin to the given value.
250-
* Optionally invert the duty cycle [default = false]
251-
* Optionally change the scale of the provided value to enable finer PWM duty control [default = 255]
252-
*/
253-
static void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);
254-
255-
/**
256-
* Set the frequency of the timer for the given pin as close as
257-
* possible to the provided desired frequency. Internally calculate
258-
* the required waveform generation mode, prescaler, and resolution
259-
* values and set timer registers accordingly.
260-
* NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
261-
* NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
262-
*/
263-
static void set_pwm_frequency(const pin_t pin, int f_desired);
264-
};
265-
266-
extern MarlinHAL hal;
222+
/**
223+
* set_pwm_duty
224+
* Set the PWM duty cycle of the provided pin to the provided value
225+
* Optionally allows inverting the duty cycle [default = false]
226+
* Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
227+
*/
228+
void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);

Marlin/src/HAL/AVR/MarlinSerial.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ void MarlinSerial<Cfg>::write(const uint8_t c) {
486486
const uint8_t i = (tx_buffer.head + 1) & (Cfg::TX_SIZE - 1);
487487

488488
// If global interrupts are disabled (as the result of being called from an ISR)...
489-
if (!hal.isr_state()) {
489+
if (!ISRS_ENABLED()) {
490490

491491
// Make room by polling if it is possible to transmit, and do so!
492492
while (i == tx_buffer.tail) {
@@ -534,7 +534,7 @@ void MarlinSerial<Cfg>::flushTX() {
534534
if (!_written) return;
535535

536536
// If global interrupts are disabled (as the result of being called from an ISR)...
537-
if (!hal.isr_state()) {
537+
if (!ISRS_ENABLED()) {
538538

539539
// Wait until everything was transmitted - We must do polling, as interrupts are disabled
540540
while (tx_buffer.head != tx_buffer.tail || !B_TXC) {

Marlin/src/HAL/AVR/fast_pwm.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ struct Timer {
3535
};
3636

3737
/**
38-
* Get the timer information and register for a pin.
39-
* Return a Timer struct containing this information.
40-
* Used by set_pwm_frequency, set_pwm_duty
38+
* get_pwm_timer
39+
* Get the timer information and register of the provided pin.
40+
* Return a Timer struct containing this information.
41+
* Used by set_pwm_frequency, set_pwm_duty
4142
*/
4243
Timer get_pwm_timer(const pin_t pin) {
4344
uint8_t q = 0;
@@ -149,7 +150,7 @@ Timer get_pwm_timer(const pin_t pin) {
149150
return timer;
150151
}
151152

152-
void MarlinHAL::set_pwm_frequency(const pin_t pin, int f_desired) {
153+
void set_pwm_frequency(const pin_t pin, int f_desired) {
153154
Timer timer = get_pwm_timer(pin);
154155
if (timer.n == 0) return; // Don't proceed if protected timer or not recognized
155156
uint16_t size;
@@ -229,7 +230,7 @@ void MarlinHAL::set_pwm_frequency(const pin_t pin, int f_desired) {
229230

230231
#endif // NEEDS_HARDWARE_PWM
231232

232-
void MarlinHAL::set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
233+
void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size/*=255*/, const bool invert/*=false*/) {
233234
#if NEEDS_HARDWARE_PWM
234235

235236
// If v is 0 or v_size (max), digitalWrite to LOW or HIGH.

Marlin/src/HAL/AVR/timers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ FORCE_INLINE void HAL_timer_start(const uint8_t timer_num, const uint32_t) {
109109
* (otherwise, characters will be lost due to UART overflow).
110110
* Then: Stepper, Endstops, Temperature, and -finally- all others.
111111
*/
112-
#define HAL_timer_isr_prologue(T) NOOP
113-
#define HAL_timer_isr_epilogue(T) NOOP
112+
#define HAL_timer_isr_prologue(T)
113+
#define HAL_timer_isr_epilogue(T)
114114

115115
/* 18 cycles maximum latency */
116116
#ifndef HAL_STEP_TIMER_ISR

0 commit comments

Comments
 (0)