74
74
#define CRITICAL_SECTION_START () unsigned char _sreg = SREG; cli()
75
75
#define CRITICAL_SECTION_END () SREG = _sreg
76
76
#endif
77
- #define ISRS_ENABLED () TEST(SREG, SREG_I)
78
- #define ENABLE_ISRS () sei()
79
- #define DISABLE_ISRS () cli ()
77
+
78
+ #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
79
+ #define PWM_FREQUENCY 1000 // Default PWM frequency when set_pwm_duty () is called without set_pwm_frequency ()
80
80
81
81
// ------------------------
82
82
// Types
83
83
// ------------------------
84
84
85
85
typedef int8_t pin_t ;
86
86
87
- #define SHARED_SERVOS HAS_SERVOS
88
- #define HAL_SERVO_LIB Servo
87
+ #define SHARED_SERVOS HAS_SERVOS // Use shared/servos.cpp
88
+
89
+ class Servo ;
90
+ typedef Servo hal_servo_t ;
89
91
90
92
// ------------------------
91
- // Public Variables
93
+ // Serial ports
92
94
// ------------------------
93
95
94
- extern uint8_t reset_reason ;
95
-
96
- // Serial ports
97
96
#ifdef USBCON
98
97
#include " ../../core/serial_hook.h"
99
98
typedef ForwardSerial1Class< decltype(Serial) > DefaultSerial1;
@@ -142,20 +141,31 @@ extern uint8_t reset_reason;
142
141
#endif
143
142
#endif
144
143
145
- // ------------------------
146
- // Public functions
147
- // ------------------------
144
+ //
145
+ // ADC
146
+ //
147
+ #define HAL_ADC_VREF 5.0
148
+ #define HAL_ADC_RESOLUTION 10
148
149
149
- void HAL_init ();
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)
150
156
151
- //void cli();
157
+ # define HAL_SENSITIVE_PINS 0 , 1 ,
152
158
153
- //void _delay_ms(const int delay);
159
+ #ifdef __AVR_AT90USB1286__
160
+ #define JTAG_DISABLE () do { MCUCR = 0x80 ; MCUCR = 0x80 ; }while (0 )
161
+ #endif
154
162
155
- inline void HAL_clear_reset_source () { }
156
- inline uint8_t HAL_get_reset_source () { return reset_reason ; }
163
+ // AVR compatibility
164
+ # define strtof strtod
157
165
158
- void HAL_reboot ();
166
+ // ------------------------
167
+ // Class Utilities
168
+ // ------------------------
159
169
160
170
#pragma GCC diagnostic push
161
171
#if GCC_VERSION <= 50000
@@ -166,70 +176,96 @@ extern "C" int freeMemory();
166
176
167
177
#pragma GCC diagnostic pop
168
178
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
179
+ // ------------------------
180
+ // MarlinHAL Class
181
+ // ------------------------
175
182
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
- }
183
+ class MarlinHAL {
184
+ public:
183
185
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
186
+ // Earliest possible init, before setup()
187
+ MarlinHAL () {}
190
188
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)
189
+ static void init (); // Called early in setup()
190
+ static void init_board () {} // Called less early in setup()
191
+ static void reboot (); // Restart the firmware from 0x0
195
192
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)
193
+ // Interrupts
194
+ static bool isr_state () { return TEST (SREG, SREG_I); }
195
+ static void isr_on () { sei (); }
196
+ static void isr_off () { cli (); }
199
197
200
- #define HAL_SENSITIVE_PINS 0, 1,
198
+ static void delay_ms ( const int ms) { _delay_ms (ms); }
201
199
202
- #ifdef __AVR_AT90USB1286__
203
- #define JTAG_DISABLE () do{ MCUCR = 0x80; MCUCR = 0x80; }while(0)
204
- #endif
200
+ // Tasks, called from idle()
201
+ static void idletask () {}
205
202
206
- // AVR compatibility
207
- #define strtof strtod
203
+ // Reset
204
+ static uint8_t reset_reason;
205
+ static uint8_t get_reset_source () { return reset_reason; }
206
+ static void clear_reset_source () { MCUSR = 0 ; }
208
207
209
- #define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
210
- #define PWM_FREQUENCY 1000 // Default PWM frequency when set_pwm_duty () is called without set_pwm_frequency()
208
+ // Free SRAM
209
+ static int freeMemory () { return :: freeMemory (); }
211
210
212
- /**
213
- * set_pwm_frequency
214
- * Sets the frequency of the timer corresponding to the provided pin
215
- * as close as possible to the provided desired frequency. Internally
216
- * calculates the required waveform generation mode, prescaler and
217
- * resolution values required and sets the timer registers accordingly.
218
- * NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
219
- * NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST FAN PWM Settings)
220
- */
221
- void set_pwm_frequency (const pin_t pin , const uint16_t f_desired );
211
+ //
212
+ // ADC Methods
213
+ //
222
214
223
- /**
224
- * set_pwm_duty
225
- * Set the PWM duty cycle of the provided pin to the provided value
226
- * Optionally allows inverting the duty cycle [default = false]
227
- * Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
228
- */
229
- void set_pwm_duty (const pin_t pin , const uint16_t v , const uint16_t v_size = 255 , const bool invert = false);
215
+ // Called by Temperature::init once at startup
216
+ static void adc_init () {
217
+ ADCSRA = _BV (ADEN) | _BV (ADSC) | _BV (ADIF) | 0x07 ;
218
+ DIDR0 = 0 ;
219
+ #ifdef DIDR2
220
+ DIDR2 = 0 ;
221
+ #endif
222
+ }
230
223
231
- /*
232
- * init_pwm_timers
233
- * sets the default frequency for timers 2-5 to 1000HZ
234
- */
235
- void init_pwm_timers ();
224
+ // Called by Temperature::init for each sensor at startup
225
+ static void adc_enable (const uint8_t ch) {
226
+ #ifdef DIDR2
227
+ if (ch > 7 ) { SBI (DIDR2, ch & 0x07 ); return ; }
228
+ #endif
229
+ SBI (DIDR0, ch);
230
+ }
231
+
232
+ // Begin ADC sampling on the given channel
233
+ static void adc_start (const uint8_t ch) {
234
+ #ifdef MUX5
235
+ ADCSRB = ch > 7 ? _BV (MUX5) : 0 ;
236
+ #else
237
+ ADCSRB = 0 ;
238
+ #endif
239
+ ADMUX = _BV (REFS0) | (ch & 0x07 );
240
+ SBI (ADCSRA, ADSC);
241
+ }
242
+
243
+ // Is the ADC ready for reading?
244
+ static bool adc_ready () { return !TEST (ADCSRA, ADSC); }
245
+
246
+ // The current value of the ADC register
247
+ static __typeof__ (ADC) adc_value() { return ADC; }
248
+
249
+ /* *
250
+ * init_pwm_timers
251
+ * Set the default frequency for timers 2-5 to 1000HZ
252
+ */
253
+ static void init_pwm_timers ();
254
+
255
+ /* *
256
+ * Set the PWM duty cycle for the pin to the given value.
257
+ * Optionally invert the duty cycle [default = false]
258
+ * Optionally change the scale of the provided value to enable finer PWM duty control [default = 255]
259
+ */
260
+ static void set_pwm_duty (const pin_t pin, const uint16_t v, const uint16_t v_size=255 , const bool invert=false );
261
+
262
+ /* *
263
+ * Set the frequency of the timer for the given pin as close as
264
+ * possible to the provided desired frequency. Internally calculate
265
+ * the required waveform generation mode, prescaler, and resolution
266
+ * values and set timer registers accordingly.
267
+ * NOTE that the frequency is applied to all pins on the timer (Ex OC3A, OC3B and OC3B)
268
+ * NOTE that there are limitations, particularly if using TIMER2. (see Configuration_adv.h -> FAST_PWM_FAN Settings)
269
+ */
270
+ static void set_pwm_frequency (const pin_t pin, const uint16_t f_desired);
271
+ };
0 commit comments