|
|
@ -79,7 +79,7 @@ |
|
|
|
#define SCB_AIRCR_PRIGROUP_Msk (7UL << SCB_AIRCR_PRIGROUP_Pos) /*!< SCB AIRCR: PRIGROUP Mask */ |
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
// Public Variables
|
|
|
|
// Serial ports
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
#if defined(SERIAL_USB) && !HAS_SD_HOST_DRIVE |
|
|
@ -112,74 +112,37 @@ |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|
|
|
|
uint16_t HAL_adc_result; |
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
// Private Variables
|
|
|
|
// ADC
|
|
|
|
// ------------------------
|
|
|
|
STM32ADC adc(ADC1); |
|
|
|
|
|
|
|
const uint8_t adc_pins[] = { |
|
|
|
OPTITEM(HAS_TEMP_ADC_0, TEMP_0_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_1, TEMP_1_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_2, TEMP_2_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_3, TEMP_3_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_4, TEMP_4_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_5, TEMP_5_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_6, TEMP_6_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_7, TEMP_7_PIN) |
|
|
|
OPTITEM(HAS_HEATED_BED, TEMP_BED_PIN) |
|
|
|
OPTITEM(HAS_TEMP_CHAMBER, TEMP_CHAMBER_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE_PIN) |
|
|
|
OPTITEM(HAS_TEMP_COOLER, TEMP_COOLER_PIN) |
|
|
|
OPTITEM(HAS_TEMP_BOARD, TEMP_BOARD_PIN) |
|
|
|
OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH_PIN) |
|
|
|
OPTITEM(HAS_ADC_BUTTONS, ADC_KEYPAD_PIN) |
|
|
|
OPTITEM(HAS_JOY_ADC_X, JOY_X_PIN) |
|
|
|
OPTITEM(HAS_JOY_ADC_Y, JOY_Y_PIN) |
|
|
|
OPTITEM(HAS_JOY_ADC_Z, JOY_Z_PIN) |
|
|
|
OPTITEM(POWER_MONITOR_CURRENT, POWER_MONITOR_CURRENT_PIN) |
|
|
|
OPTITEM(POWER_MONITOR_VOLTAGE, POWER_MONITOR_VOLTAGE_PIN) |
|
|
|
}; |
|
|
|
|
|
|
|
enum TempPinIndex : char { |
|
|
|
OPTITEM(HAS_TEMP_ADC_0, TEMP_0) |
|
|
|
OPTITEM(HAS_TEMP_ADC_1, TEMP_1) |
|
|
|
OPTITEM(HAS_TEMP_ADC_2, TEMP_2) |
|
|
|
OPTITEM(HAS_TEMP_ADC_3, TEMP_3) |
|
|
|
OPTITEM(HAS_TEMP_ADC_4, TEMP_4) |
|
|
|
OPTITEM(HAS_TEMP_ADC_5, TEMP_5) |
|
|
|
OPTITEM(HAS_TEMP_ADC_6, TEMP_6) |
|
|
|
OPTITEM(HAS_TEMP_ADC_7, TEMP_7) |
|
|
|
OPTITEM(HAS_HEATED_BED, TEMP_BED) |
|
|
|
OPTITEM(HAS_TEMP_CHAMBER, TEMP_CHAMBER) |
|
|
|
OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE) |
|
|
|
OPTITEM(HAS_TEMP_COOLER, TEMP_COOLER) |
|
|
|
OPTITEM(HAS_TEMP_BOARD, TEMP_BOARD) |
|
|
|
OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH) |
|
|
|
OPTITEM(HAS_ADC_BUTTONS, ADC_KEY) |
|
|
|
OPTITEM(HAS_JOY_ADC_X, JOY_X) |
|
|
|
OPTITEM(HAS_JOY_ADC_Y, JOY_Y) |
|
|
|
OPTITEM(HAS_JOY_ADC_Z, JOY_Z) |
|
|
|
OPTITEM(POWER_MONITOR_CURRENT, POWERMON_CURRENT) |
|
|
|
OPTITEM(POWER_MONITOR_VOLTAGE, POWERMON_VOLTS) |
|
|
|
ADC_PIN_COUNT |
|
|
|
}; |
|
|
|
// Watch out for recursion here! Our pin_t is signed, so pass through to Arduino -> analogRead(uint8_t)
|
|
|
|
|
|
|
|
uint16_t analogRead(const pin_t pin) { |
|
|
|
const bool is_analog = _GET_MODE(pin) == GPIO_INPUT_ANALOG; |
|
|
|
return is_analog ? analogRead(uint8_t(pin)) : 0; |
|
|
|
} |
|
|
|
|
|
|
|
// Wrapper to maple unprotected analogWrite
|
|
|
|
void analogWrite(const pin_t pin, int pwm_val8) { |
|
|
|
if (PWM_PIN(pin)) analogWrite(uint8_t(pin), pwm_val8); |
|
|
|
} |
|
|
|
|
|
|
|
uint16_t HAL_adc_results[ADC_PIN_COUNT]; |
|
|
|
uint16_t MarlinHAL::adc_result; |
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
// Private functions
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) { |
|
|
|
uint32_t reg_value; |
|
|
|
uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07); /* only values 0..7 are used */ |
|
|
|
uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07); // only values 0..7 are used
|
|
|
|
|
|
|
|
reg_value = SCB->AIRCR; /* read old register configuration */ |
|
|
|
reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk); /* clear bits to change */ |
|
|
|
reg_value = SCB->AIRCR; // read old register configuration
|
|
|
|
reg_value &= ~(SCB_AIRCR_VECTKEY_Msk | SCB_AIRCR_PRIGROUP_Msk); // clear bits to change
|
|
|
|
reg_value = (reg_value | |
|
|
|
((uint32_t)0x5FA << SCB_AIRCR_VECTKEY_Pos) | |
|
|
|
(PriorityGroupTmp << 8)); /* Insert write key & priority group */ |
|
|
|
(PriorityGroupTmp << 8)); // Insert write key & priority group
|
|
|
|
SCB->AIRCR = reg_value; |
|
|
|
} |
|
|
|
|
|
|
@ -187,6 +150,8 @@ static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) { |
|
|
|
// Public functions
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
void flashFirmware(const int16_t) { hal.reboot(); } |
|
|
|
|
|
|
|
//
|
|
|
|
// Leave PA11/PA12 intact if USBSerial is not used
|
|
|
|
//
|
|
|
@ -206,7 +171,11 @@ static void NVIC_SetPriorityGrouping(uint32_t PriorityGroup) { |
|
|
|
|
|
|
|
TERN_(POSTMORTEM_DEBUGGING, extern void install_min_serial()); |
|
|
|
|
|
|
|
void HAL_init() { |
|
|
|
// ------------------------
|
|
|
|
// MarlinHAL class
|
|
|
|
// ------------------------
|
|
|
|
|
|
|
|
void MarlinHAL::init() { |
|
|
|
NVIC_SetPriorityGrouping(0x3); |
|
|
|
#if PIN_EXISTS(LED) |
|
|
|
OUT_WRITE(LED_PIN, LOW); |
|
|
@ -225,7 +194,7 @@ void HAL_init() { |
|
|
|
} |
|
|
|
|
|
|
|
// HAL idle task
|
|
|
|
void HAL_idletask() { |
|
|
|
void MarlinHAL::idletask() { |
|
|
|
#if HAS_SHARED_MEDIA |
|
|
|
// If Marlin is using the SD card we need to lock it to prevent access from
|
|
|
|
// a PC via USB.
|
|
|
@ -240,14 +209,7 @@ void HAL_idletask() { |
|
|
|
#endif |
|
|
|
} |
|
|
|
|
|
|
|
void HAL_clear_reset_source() { } |
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Check this and change or remove. |
|
|
|
*/ |
|
|
|
uint8_t HAL_get_reset_source() { return RST_POWER_ON; } |
|
|
|
|
|
|
|
void _delay_ms(const int delay_ms) { delay(delay_ms); } |
|
|
|
void MarlinHAL::reboot() { nvic_sys_reset(); } |
|
|
|
|
|
|
|
extern "C" { |
|
|
|
extern unsigned int _ebss; // end of bss section
|
|
|
@ -281,31 +243,76 @@ extern "C" { |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
// ------------------------
|
|
|
|
//
|
|
|
|
// ADC
|
|
|
|
// ------------------------
|
|
|
|
//
|
|
|
|
|
|
|
|
enum ADCIndex : uint8_t { |
|
|
|
OPTITEM(HAS_TEMP_ADC_0, TEMP_0) |
|
|
|
OPTITEM(HAS_TEMP_ADC_1, TEMP_1) |
|
|
|
OPTITEM(HAS_TEMP_ADC_2, TEMP_2) |
|
|
|
OPTITEM(HAS_TEMP_ADC_3, TEMP_3) |
|
|
|
OPTITEM(HAS_TEMP_ADC_4, TEMP_4) |
|
|
|
OPTITEM(HAS_TEMP_ADC_5, TEMP_5) |
|
|
|
OPTITEM(HAS_TEMP_ADC_6, TEMP_6) |
|
|
|
OPTITEM(HAS_TEMP_ADC_7, TEMP_7) |
|
|
|
OPTITEM(HAS_HEATED_BED, TEMP_BED) |
|
|
|
OPTITEM(HAS_TEMP_CHAMBER, TEMP_CHAMBER) |
|
|
|
OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE) |
|
|
|
OPTITEM(HAS_TEMP_COOLER, TEMP_COOLER) |
|
|
|
OPTITEM(HAS_TEMP_BOARD, TEMP_BOARD) |
|
|
|
OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH) |
|
|
|
OPTITEM(HAS_ADC_BUTTONS, ADC_KEY) |
|
|
|
OPTITEM(HAS_JOY_ADC_X, JOY_X) |
|
|
|
OPTITEM(HAS_JOY_ADC_Y, JOY_Y) |
|
|
|
OPTITEM(HAS_JOY_ADC_Z, JOY_Z) |
|
|
|
OPTITEM(POWER_MONITOR_CURRENT, POWERMON_CURRENT) |
|
|
|
OPTITEM(POWER_MONITOR_VOLTAGE, POWERMON_VOLTS) |
|
|
|
ADC_COUNT |
|
|
|
}; |
|
|
|
|
|
|
|
static uint16_t adc_results[ADC_COUNT]; |
|
|
|
|
|
|
|
// Init the AD in continuous capture mode
|
|
|
|
void HAL_adc_init() { |
|
|
|
void MarlinHAL::adc_init() { |
|
|
|
static const uint8_t adc_pins[] = { |
|
|
|
OPTITEM(HAS_TEMP_ADC_0, TEMP_0_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_1, TEMP_1_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_2, TEMP_2_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_3, TEMP_3_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_4, TEMP_4_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_5, TEMP_5_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_6, TEMP_6_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_7, TEMP_7_PIN) |
|
|
|
OPTITEM(HAS_HEATED_BED, TEMP_BED_PIN) |
|
|
|
OPTITEM(HAS_TEMP_CHAMBER, TEMP_CHAMBER_PIN) |
|
|
|
OPTITEM(HAS_TEMP_ADC_PROBE, TEMP_PROBE_PIN) |
|
|
|
OPTITEM(HAS_TEMP_COOLER, TEMP_COOLER_PIN) |
|
|
|
OPTITEM(HAS_TEMP_BOARD, TEMP_BOARD_PIN) |
|
|
|
OPTITEM(FILAMENT_WIDTH_SENSOR, FILWIDTH_PIN) |
|
|
|
OPTITEM(HAS_ADC_BUTTONS, ADC_KEYPAD_PIN) |
|
|
|
OPTITEM(HAS_JOY_ADC_X, JOY_X_PIN) |
|
|
|
OPTITEM(HAS_JOY_ADC_Y, JOY_Y_PIN) |
|
|
|
OPTITEM(HAS_JOY_ADC_Z, JOY_Z_PIN) |
|
|
|
OPTITEM(POWER_MONITOR_CURRENT, POWER_MONITOR_CURRENT_PIN) |
|
|
|
OPTITEM(POWER_MONITOR_VOLTAGE, POWER_MONITOR_VOLTAGE_PIN) |
|
|
|
}; |
|
|
|
static STM32ADC adc(ADC1); |
|
|
|
// configure the ADC
|
|
|
|
adc.calibrate(); |
|
|
|
#if F_CPU > 72000000 |
|
|
|
adc.setSampleRate(ADC_SMPR_71_5); // 71.5 ADC cycles
|
|
|
|
#else |
|
|
|
adc.setSampleRate(ADC_SMPR_41_5); // 41.5 ADC cycles
|
|
|
|
#endif |
|
|
|
adc.setPins((uint8_t *)adc_pins, ADC_PIN_COUNT); |
|
|
|
adc.setDMA(HAL_adc_results, (uint16_t)ADC_PIN_COUNT, (uint32_t)(DMA_MINC_MODE | DMA_CIRC_MODE), nullptr); |
|
|
|
adc.setSampleRate((F_CPU > 72000000) ? ADC_SMPR_71_5 : ADC_SMPR_41_5); // 71.5 or 41.5 ADC cycles
|
|
|
|
adc.setPins((uint8_t *)adc_pins, ADC_COUNT); |
|
|
|
adc.setDMA(adc_results, uint16_t(ADC_COUNT), uint32_t(DMA_MINC_MODE | DMA_CIRC_MODE), nullptr); |
|
|
|
adc.setScanMode(); |
|
|
|
adc.setContinuous(); |
|
|
|
adc.startConversion(); |
|
|
|
} |
|
|
|
|
|
|
|
void HAL_adc_start_conversion(const uint8_t adc_pin) { |
|
|
|
void MarlinHAL::adc_start(const pin_t pin) { |
|
|
|
#define __TCASE(N,I) case N: pin_index = I; break; |
|
|
|
#define _TCASE(C,N,I) TERN_(C, __TCASE(N, I)) |
|
|
|
//TEMP_PINS pin_index;
|
|
|
|
TempPinIndex pin_index; |
|
|
|
switch (adc_pin) { |
|
|
|
ADCIndex pin_index; |
|
|
|
switch (pin) { |
|
|
|
default: return; |
|
|
|
_TCASE(HAS_TEMP_ADC_0, TEMP_0_PIN, TEMP_0) |
|
|
|
_TCASE(HAS_TEMP_ADC_1, TEMP_1_PIN, TEMP_1) |
|
|
@ -328,23 +335,7 @@ void HAL_adc_start_conversion(const uint8_t adc_pin) { |
|
|
|
_TCASE(POWER_MONITOR_CURRENT, POWER_MONITOR_CURRENT_PIN, POWERMON_CURRENT) |
|
|
|
_TCASE(POWER_MONITOR_VOLTAGE, POWER_MONITOR_VOLTAGE_PIN, POWERMON_VOLTS) |
|
|
|
} |
|
|
|
HAL_adc_result = HAL_adc_results[(int)pin_index] >> (12 - HAL_ADC_RESOLUTION); // shift out unused bits
|
|
|
|
} |
|
|
|
|
|
|
|
uint16_t HAL_adc_get_result() { return HAL_adc_result; } |
|
|
|
|
|
|
|
uint16_t analogRead(pin_t pin) { |
|
|
|
const bool is_analog = _GET_MODE(pin) == GPIO_INPUT_ANALOG; |
|
|
|
return is_analog ? analogRead(uint8_t(pin)) : 0; |
|
|
|
} |
|
|
|
|
|
|
|
// Wrapper to maple unprotected analogWrite
|
|
|
|
void analogWrite(pin_t pin, int pwm_val8) { |
|
|
|
if (PWM_PIN(pin)) analogWrite(uint8_t(pin), pwm_val8); |
|
|
|
adc_result = (adc_results[(int)pin_index] & 0xFFF) >> (12 - HAL_ADC_RESOLUTION); // shift out unused bits
|
|
|
|
} |
|
|
|
|
|
|
|
void HAL_reboot() { nvic_sys_reset(); } |
|
|
|
|
|
|
|
void flashFirmware(const int16_t) { HAL_reboot(); } |
|
|
|
|
|
|
|
#endif // __STM32F1__
|
|
|
|