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
8485typedef 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);
0 commit comments