️ Refactor still needs work

Reverting #23295
This commit is contained in:
Scott Lahteine
2021-12-25 23:15:17 -06:00
parent 00e6e90648
commit 6a8b9274a3
69 changed files with 1301 additions and 1771 deletions

View File

@@ -52,7 +52,7 @@
// Externs
// ------------------------
portMUX_TYPE MarlinHAL::spinlock = portMUX_INITIALIZER_UNLOCKED;
portMUX_TYPE spinlock = portMUX_INITIALIZER_UNLOCKED;
// ------------------------
// Local defines
@@ -64,7 +64,7 @@ portMUX_TYPE MarlinHAL::spinlock = portMUX_INITIALIZER_UNLOCKED;
// Public Variables
// ------------------------
uint16_t MarlinHAL::adc_result;
uint16_t HAL_adc_result;
// ------------------------
// Private Variables
@@ -95,22 +95,20 @@ volatile int numPWMUsed = 0,
#endif
#if ENABLED(USE_ESP32_EXIO)
HardwareSerial YSerial2(2);
void Write_EXIO(uint8_t IO, uint8_t v) {
if (hal.isr_state()) {
hal.isr_off();
if (ISRS_ENABLED()) {
DISABLE_ISRS();
YSerial2.write(0x80 | (((char)v) << 5) | (IO - 100));
hal.isr_on();
ENABLE_ISRS();
}
else
YSerial2.write(0x80 | (((char)v) << 5) | (IO - 100));
}
#endif
void MarlinHAL::init_board() {
void HAL_init_board() {
#if ENABLED(USE_ESP32_TASK_WDT)
esp_task_wdt_init(10, true);
#endif
@@ -156,24 +154,27 @@ void MarlinHAL::init_board() {
#endif
}
void MarlinHAL::idletask() {
void HAL_idletask() {
#if BOTH(WIFISUPPORT, OTASUPPORT)
OTA_handle();
#endif
TERN_(ESP3D_WIFISUPPORT, esp3dlib.idletask());
}
uint8_t MarlinHAL::get_reset_source() { return rtc_get_reset_reason(1); }
void HAL_clear_reset_source() { }
void MarlinHAL::reboot() { ESP.restart(); }
uint8_t HAL_get_reset_source() { return rtc_get_reset_reason(1); }
void HAL_reboot() { ESP.restart(); }
void _delay_ms(int delay_ms) { delay(delay_ms); }
// return free memory between end of heap (or end bss) and whatever is current
int MarlinHAL::freeMemory() { return ESP.getFreeHeap(); }
int freeMemory() { return ESP.getFreeHeap(); }
// ------------------------
// ADC
// ------------------------
#define ADC1_CHANNEL(pin) ADC1_GPIO ## pin ## _CHANNEL
adc1_channel_t get_channel(int pin) {
@@ -195,7 +196,7 @@ void adc1_set_attenuation(adc1_channel_t chan, adc_atten_t atten) {
}
}
void MarlinHAL::adc_init() {
void HAL_adc_init() {
// Configure ADC
adc1_config_width(ADC_WIDTH_12Bit);
@@ -225,11 +226,11 @@ void MarlinHAL::adc_init() {
}
}
void MarlinHAL::adc_start(const pin_t pin) {
const adc1_channel_t chan = get_channel(pin);
void HAL_adc_start_conversion(const uint8_t adc_pin) {
const adc1_channel_t chan = get_channel(adc_pin);
uint32_t mv;
esp_adc_cal_get_voltage((adc_channel_t)chan, &characteristics[attenuations[chan]], &mv);
adc_result = mv * 1023.0 / 3300.0;
HAL_adc_result = mv * 1023.0 / 3300.0;
// Change the attenuation level based on the new reading
adc_atten_t atten;

View File

@@ -49,6 +49,8 @@
// Defines
// ------------------------
extern portMUX_TYPE spinlock;
#define MYSERIAL1 flushableSerial
#if EITHER(WIFISUPPORT, ESP3D_WIFISUPPORT)
@@ -63,6 +65,9 @@
#define CRITICAL_SECTION_START() portENTER_CRITICAL(&spinlock)
#define CRITICAL_SECTION_END() portEXIT_CRITICAL(&spinlock)
#define ISRS_ENABLED() (spinlock.owner == portMUX_FREE_VAL)
#define ENABLE_ISRS() if (spinlock.owner != portMUX_FREE_VAL) portEXIT_CRITICAL(&spinlock)
#define DISABLE_ISRS() portENTER_CRITICAL(&spinlock)
// ------------------------
// Types
@@ -70,8 +75,14 @@
typedef int16_t pin_t;
class Servo;
typedef Servo hal_servo_t;
#define HAL_SERVO_LIB Servo
// ------------------------
// Public Variables
// ------------------------
/** result of last ADC conversion */
extern uint16_t HAL_adc_result;
// ------------------------
// Public functions
@@ -80,18 +91,59 @@ typedef Servo hal_servo_t;
//
// Tone
//
void toneInit();
void tone(const pin_t _pin, const unsigned int frequency, const unsigned long duration=0);
void noTone(const pin_t _pin);
// clear reset reason
void HAL_clear_reset_source();
// reset reason
uint8_t HAL_get_reset_source();
void HAL_reboot();
void _delay_ms(int delay);
#pragma GCC diagnostic push
#if GCC_VERSION <= 50000
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
int freeMemory();
#pragma GCC diagnostic pop
void analogWrite(pin_t pin, int value);
//
// Pin Mapping for M42, M43, M226
//
// ADC
#define HAL_ANALOG_SELECT(pin)
void HAL_adc_init();
#define HAL_ADC_VREF 3.3
#define HAL_ADC_RESOLUTION 10
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_READ_ADC() HAL_adc_result
#define HAL_ADC_READY() true
void HAL_adc_start_conversion(const uint8_t adc_pin);
// PWM
inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) { analogWrite(pin, v); }
// Pin Map
#define GET_PIN_MAP_PIN(index) index
#define GET_PIN_MAP_INDEX(pin) pin
#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
// Enable hooks into idle and setup for HAL
#define HAL_IDLETASK 1
#define BOARD_INIT() HAL_init_board();
void HAL_idletask();
inline void HAL_init() {}
void HAL_init_board();
#if ENABLED(USE_ESP32_EXIO)
void Write_EXIO(uint8_t IO, uint8_t v);
#endif
@@ -136,86 +188,3 @@ FORCE_INLINE static void DELAY_CYCLES(uint32_t x) {
}
}
// ------------------------
// Class Utilities
// ------------------------
#pragma GCC diagnostic push
#if GCC_VERSION <= 50000
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
int freeMemory();
#pragma GCC diagnostic pop
void _delay_ms(const int ms);
// ------------------------
// MarlinHAL Class
// ------------------------
#define HAL_ADC_VREF 3.3
#define HAL_ADC_RESOLUTION 10
class MarlinHAL {
public:
// Earliest possible init, before setup()
MarlinHAL() {}
static inline void init() {} // Called early in setup()
static void init_board(); // Called less early in setup()
static void reboot(); // Restart the firmware
static portMUX_TYPE spinlock;
static inline bool isr_state() { return spinlock.owner == portMUX_FREE_VAL; }
static inline void isr_on() { if (spinlock.owner != portMUX_FREE_VAL) portEXIT_CRITICAL(&spinlock); }
static inline void isr_off() { portENTER_CRITICAL(&spinlock); }
static inline void delay_ms(const int ms) { _delay_ms(ms); }
// Tasks, called from idle()
static void idletask();
// Reset
static uint8_t get_reset_source();
static inline void clear_reset_source() {}
// Free SRAM
static int freeMemory();
//
// ADC Methods
//
static uint16_t adc_result;
// Called by Temperature::init once at startup
static void adc_init();
// Called by Temperature::init for each sensor at startup
static inline void adc_enable(const pin_t pin) {}
// Begin ADC sampling on the given channel
static void adc_start(const pin_t pin);
// Is the ADC ready for reading?
static inline bool adc_ready() { return true; }
// The current value of the ADC register
static inline uint16_t adc_value() { return adc_result; }
/**
* Set the PWM duty cycle for the pin to the given value.
* No inverting the duty cycle in this HAL.
* No changing the maximum size of the provided value to enable finer PWM duty control in this HAL.
*/
static inline void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t=255, const bool=false) {
analogWrite(pin, v);
}
};
extern MarlinHAL hal;

View File

@@ -136,5 +136,5 @@ void HAL_timer_enable_interrupt(const uint8_t timer_num);
void HAL_timer_disable_interrupt(const uint8_t timer_num);
bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
#define HAL_timer_isr_prologue(T) NOOP
#define HAL_timer_isr_epilogue(T) NOOP
#define HAL_timer_isr_prologue(T)
#define HAL_timer_isr_epilogue(T)