Marlin_Firmware/Marlin/src/HAL/STM32/HAL.h

208 lines
5.6 KiB
C
Raw Normal View History

/**
* Marlin 3D Printer Firmware
*
2020-02-03 08:00:57 -06:00
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
* Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
* Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
* Copyright (c) 2017 Victor Perez
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2020-07-22 22:20:14 -05:00
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once
#define CPU_32_BIT
2019-06-21 00:46:23 -05:00
#include "../../core/macros.h"
2019-05-02 00:45:50 -05:00
#include "../shared/Marduino.h"
#include "../shared/math_32bit.h"
#include "../shared/HAL_SPI.h"
2019-09-02 19:49:58 -05:00
#include "fastio.h"
#include "Servo.h"
2019-09-02 19:49:58 -05:00
#include "watchdog.h"
2020-05-26 00:43:29 -05:00
#include "MarlinSerial.h"
2019-05-02 00:45:50 -05:00
#include "../../inc/MarlinConfigPre.h"
2019-05-02 00:45:50 -05:00
#include <stdint.h>
#ifdef USBCON
#include <USBSerial.h>
#endif
2019-07-09 22:30:06 -05:00
// ------------------------
// Defines
2019-07-09 22:30:06 -05:00
// ------------------------
#define _MSERIAL(X) MSerial##X
#define MSERIAL(X) _MSERIAL(X)
#if SERIAL_PORT == -1
#define MYSERIAL0 SerialUSB
#elif WITHIN(SERIAL_PORT, 1, 6)
#define MYSERIAL0 MSERIAL(SERIAL_PORT)
2020-01-02 17:59:06 -06:00
#else
#error "SERIAL_PORT must be -1 or from 1 to 6. Please update your configuration."
#endif
#ifdef SERIAL_PORT_2
#if SERIAL_PORT_2 == -1
#define MYSERIAL1 SerialUSB
#elif WITHIN(SERIAL_PORT_2, 1, 6)
#define MYSERIAL1 MSERIAL(SERIAL_PORT_2)
2020-01-02 17:59:06 -06:00
#else
#error "SERIAL_PORT_2 must be -1 or from 1 to 6. Please update your configuration."
#endif
#endif
#ifdef LCD_SERIAL_PORT
#if LCD_SERIAL_PORT == -1
#define LCD_SERIAL SerialUSB
#elif WITHIN(LCD_SERIAL_PORT, 1, 6)
#define LCD_SERIAL MSERIAL(LCD_SERIAL_PORT)
#else
#error "LCD_SERIAL_PORT must be -1 or from 1 to 6. Please update your configuration."
#endif
#if HAS_DGUS_LCD
#define SERIAL_GET_TX_BUFFER_FREE() LCD_SERIAL.availableForWrite()
#endif
#endif
/**
* TODO: review this to return 1 for pins that are not analog input
*/
#ifndef analogInputToDigitalPin
#define analogInputToDigitalPin(p) (p)
#endif
2020-02-11 01:13:02 -06:00
#define CRITICAL_SECTION_START() uint32_t primask = __get_PRIMASK(); __disable_irq()
#define CRITICAL_SECTION_END() if (!primask) __enable_irq()
#define ISRS_ENABLED() (!__get_PRIMASK())
#define ENABLE_ISRS() __enable_irq()
#define DISABLE_ISRS() __disable_irq()
#define cli() __disable_irq()
#define sei() __enable_irq()
// On AVR this is in math.h?
#define square(x) ((x)*(x))
#ifndef strncpy_P
#define strncpy_P(dest, src, num) strncpy((dest), (src), (num))
#endif
// Fix bug in pgm_read_ptr
#undef pgm_read_ptr
#define pgm_read_ptr(addr) (*(addr))
2019-07-09 22:30:06 -05:00
// ------------------------
// Types
2019-07-09 22:30:06 -05:00
// ------------------------
typedef int16_t pin_t;
#define HAL_SERVO_LIB libServo
#define PAUSE_SERVO_OUTPUT() libServo::pause_all_servos()
#define RESUME_SERVO_OUTPUT() libServo::resume_all_servos()
2019-07-09 22:30:06 -05:00
// ------------------------
// Public Variables
2019-07-09 22:30:06 -05:00
// ------------------------
2019-08-06 04:46:30 -05:00
// result of last ADC conversion
extern uint16_t HAL_adc_result;
2019-07-09 22:30:06 -05:00
// ------------------------
// Public functions
2019-07-09 22:30:06 -05:00
// ------------------------
// Memory related
#define __bss_end __bss_end__
// Enable hooks into setup for HAL
2019-09-16 20:31:08 -05:00
void HAL_init();
2019-08-06 04:46:30 -05:00
// Clear reset reason
2019-09-16 20:31:08 -05:00
void HAL_clear_reset_source();
2019-08-06 04:46:30 -05:00
// Reset reason
2019-09-16 20:31:08 -05:00
uint8_t HAL_get_reset_source();
inline void HAL_reboot() {} // reboot the board or restart the bootloader
void _delay_ms(const int delay);
extern "C" char* _sbrk(int incr);
2020-12-15 00:53:53 -06:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
2019-08-06 04:46:30 -05:00
static inline int freeMemory() {
volatile char top;
return &top - reinterpret_cast<char*>(_sbrk(0));
}
2020-12-15 00:53:53 -06:00
#pragma GCC diagnostic pop
2019-08-06 04:46:30 -05:00
2019-02-20 06:13:07 -06:00
//
// ADC
2019-02-20 06:13:07 -06:00
//
#define HAL_ANALOG_SELECT(pin) pinMode(pin, INPUT)
2020-06-18 15:23:03 -05:00
#define HAL_ADC_VREF 3.3
#define HAL_ADC_RESOLUTION ADC_RESOLUTION // 12
2020-06-18 15:23:03 -05:00
#define HAL_START_ADC(pin) HAL_adc_start_conversion(pin)
#define HAL_READ_ADC() HAL_adc_result
#define HAL_ADC_READY() true
inline void HAL_adc_init() { analogReadResolution(HAL_ADC_RESOLUTION); }
void HAL_adc_start_conversion(const uint8_t adc_pin);
2019-09-16 20:31:08 -05:00
uint16_t HAL_adc_get_result();
#define GET_PIN_MAP_PIN(index) index
#define GET_PIN_MAP_INDEX(pin) pin
#define PARSED_PIN_INDEX(code, dval) parser.intval(code, dval)
#ifdef STM32F1xx
#define JTAG_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_JTAGDISABLE)
#define JTAGSWD_DISABLE() AFIO_DBGAFR_CONFIG(AFIO_MAPR_SWJ_CFG_DISABLE)
#endif
#define PLATFORM_M997_SUPPORT
2020-03-23 17:11:00 -05:00
void flashFirmware(const int16_t);
// Maple Compatibility
typedef void (*systickCallback_t)(void);
void systick_attach_callback(systickCallback_t cb);
void HAL_SYSTICK_Callback();
extern volatile uint32_t systick_uptime_millis;
2020-11-04 15:15:19 -06:00
#define HAL_CAN_SET_PWM_FREQ // This HAL supports PWM Frequency adjustment
/**
* set_pwm_frequency
* Set the frequency of the timer corresponding to the provided pin
* All Timer PWM pins run at the same frequency
*/
void set_pwm_frequency(const pin_t pin, int f_desired);
/**
* set_pwm_duty
* Set the PWM duty cycle of the provided pin to the provided value
* Optionally allows inverting the duty cycle [default = false]
* Optionally allows changing the maximum size of the provided value to enable finer PWM duty control [default = 255]
*/
void set_pwm_duty(const pin_t pin, const uint16_t v, const uint16_t v_size=255, const bool invert=false);