Official STMicroelectronics Arduino Core STM32F4 HAL compatibility (#11006)

This commit is contained in:
Karl Andersson 2018-06-13 01:38:00 +02:00 committed by Scott Lahteine
parent 4dbec774b5
commit e0276d2f32
18 changed files with 176 additions and 138 deletions

View file

@ -54,6 +54,7 @@ static SPISettings spiConfig;
// --------------------------------------------------------------------------
#if ENABLED(SOFTWARE_SPI)
// --------------------------------------------------------------------------
// Software SPI
// --------------------------------------------------------------------------
@ -101,8 +102,7 @@ void spiInit(uint8_t spiRate) {
case SPI_EIGHTH_SPEED: clock = SPI_CLOCK_DIV16; break;
case SPI_SPEED_5: clock = SPI_CLOCK_DIV32; break;
case SPI_SPEED_6: clock = SPI_CLOCK_DIV64; break;
default:
clock = SPI_CLOCK_DIV2; // Default from the SPI library
default: clock = SPI_CLOCK_DIV2; // Default from the SPI library
}
spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
SPI.begin();
@ -168,7 +168,6 @@ void spiSendBlock(uint8_t token, const uint8_t* buf) {
/** Begin SPI transaction, set clock, bit order, data mode */
void spiBeginTransaction(uint32_t spiClock, uint8_t bitOrder, uint8_t dataMode) {
spiConfig = SPISettings(spiClock, (BitOrder)bitOrder, dataMode);
SPI.beginTransaction(spiConfig);
}

View file

@ -47,7 +47,7 @@
/** @addtogroup EEPROM_Emulation
* @{
*/
#ifdef STM32F4
#if defined(STM32F4) || defined(STM32F4xx)
/* Includes ------------------------------------------------------------------*/
#include "eeprom_emul.h"
@ -562,7 +562,7 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) {
return FlashStatus;
}
#endif // STM32F4
#endif // STM32F4 || STM32F4xx
/**
* @}

View file

@ -17,7 +17,7 @@
*
*/
#ifdef STM32F4
#if defined(STM32F4) || defined(STM32F4xx)
/**
* Description: functions for I2C connected external EEPROM.
@ -139,5 +139,5 @@ void eeprom_update_block(const void *__src, void *__dst, size_t __n) {
}
#endif // ENABLED(EEPROM_SETTINGS) && DISABLED(I2C_EEPROM) && DISABLED(SPI_EEPROM)
#endif // STM32F4
#endif // STM32F4 || STM32F4xx

View file

@ -21,8 +21,7 @@
*
*/
#ifdef STM32F4
#if defined(STM32F4) || defined(STM32F4xx)
// --------------------------------------------------------------------------
// Includes
@ -81,17 +80,11 @@ void sei(void) { interrupts(); }
void HAL_clear_reset_source(void) { __HAL_RCC_CLEAR_RESET_FLAGS(); }
uint8_t HAL_get_reset_source (void) {
if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET)
return RST_WATCHDOG;
if (__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST) != RESET) return RST_WATCHDOG;
if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) != RESET)
return RST_SOFTWARE;
if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
return RST_EXTERNAL;
if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
return RST_POWER_ON;
if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) != RESET) return RST_SOFTWARE;
if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET) return RST_EXTERNAL;
if (__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET) return RST_POWER_ON;
return 0;
}
@ -137,4 +130,4 @@ uint16_t HAL_adc_get_result(void) {
return HAL_adc_result;
}
#endif // STM32F4
#endif // STM32F4 || STM32F4xx

View file

@ -21,8 +21,6 @@
*
*/
#ifndef _HAL_STM32F4_H
#define _HAL_STM32F4_H
@ -41,6 +39,10 @@
#include "Arduino.h"
#ifdef USBCON
#include <USBSerial.h>
#endif
#include "../math_32bit.h"
#include "../HAL_SPI.h"
#include "fastio_STM32F4.h"
@ -48,7 +50,6 @@
#include "HAL_timers_STM32F4.h"
// --------------------------------------------------------------------------
// Defines
// --------------------------------------------------------------------------
@ -186,6 +187,7 @@ extern "C" {
*/
extern "C" char* _sbrk(int incr);
/*
static int freeMemory() {
volatile int top;
@ -193,6 +195,7 @@ static int freeMemory() {
return top;
}
*/
static int freeMemory() {
volatile char top;
return &top - reinterpret_cast<char*>(_sbrk(0));

View file

@ -21,7 +21,7 @@
*
*/
#ifdef STM32F4
#if defined(STM32F4) || defined(STM32F4xx)
#include "../../inc/MarlinConfig.h"
@ -50,4 +50,4 @@ void libServo::move(const int value) {
}
#endif // HAS_SERVOS
#endif // STM32F4
#endif // STM32F4 || STM32F4xx

View file

@ -30,7 +30,7 @@
* Adapted to the STM32F4 HAL
*/
#ifdef STM32F4
#if defined(STM32F4) || defined(STM32F4xx)
// --------------------------------------------------------------------------
// Includes
@ -54,6 +54,7 @@ static SPISettings spiConfig;
// --------------------------------------------------------------------------
#if ENABLED(SOFTWARE_SPI)
// --------------------------------------------------------------------------
// Software SPI
// --------------------------------------------------------------------------
@ -81,8 +82,7 @@ void spiBegin(void) {
#error SS_PIN not defined!
#endif
SET_OUTPUT(SS_PIN);
WRITE(SS_PIN, HIGH);
OUT_WRITE(SS_PIN, HIGH);
}
/** Configure SPI for specified SPI speed */
@ -96,8 +96,7 @@ void spiInit(uint8_t spiRate) {
case SPI_EIGHTH_SPEED: clock = 1250000; break;
case SPI_SPEED_5: clock = 625000; break;
case SPI_SPEED_6: clock = 300000; break;
default:
clock = 4000000; // Default from the SPI libarary
default: clock = 4000000; // Default from the SPI libarary
}
spiConfig = SPISettings(clock, MSBFIRST, SPI_MODE0);
SPI.begin();
@ -128,7 +127,13 @@ uint8_t spiRec(void) {
*/
void spiRead(uint8_t* buf, uint16_t nbyte) {
SPI.beginTransaction(spiConfig);
#ifdef STM32GENERIC
SPI.dmaTransfer(0, const_cast<uint8_t*>(buf), nbyte);
#else
SPI.transfer((uint8_t*)buf, nbyte);
#endif
SPI.endTransaction();
}
@ -156,10 +161,16 @@ void spiSend(uint8_t b) {
void spiSendBlock(uint8_t token, const uint8_t* buf) {
SPI.beginTransaction(spiConfig);
SPI.transfer(token);
#ifdef STM32GENERIC
SPI.dmaSend(const_cast<uint8_t*>(buf), 512);
#else
SPI.transfer((uint8_t*)buf, (uint8_t*)0, 512);
#endif
SPI.endTransaction();
}
#endif // SOFTWARE_SPI
#endif // STM32F4
#endif // STM32F4 || STM32F4xx

View file

@ -20,7 +20,7 @@
*
*/
#ifdef STM32F4
#if defined(STM32F4) || defined(STM32F4xx)
// --------------------------------------------------------------------------
// Includes
@ -39,6 +39,8 @@
// --------------------------------------------------------------------------
#define NUM_HARDWARE_TIMERS 2
#define STEP_TIMER_IRQ_ID TIM5_IRQn
#define TEMP_TIMER_IRQ_ID TIM7_IRQn
//#define PRESCALER 1
// --------------------------------------------------------------------------
@ -53,7 +55,7 @@
// Private Variables
// --------------------------------------------------------------------------
tTimerConfig timerConfig[NUM_HARDWARE_TIMERS];
stm32f4_timer_t TimerHandle[NUM_HARDWARE_TIMERS];
// --------------------------------------------------------------------------
// Function prototypes
@ -72,90 +74,86 @@ bool timers_initialised[NUM_HARDWARE_TIMERS] = {false};
void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
if (!timers_initialised[timer_num]) {
constexpr uint32_t step_prescaler = STEPPER_TIMER_PRESCALE - 1,
temp_prescaler = TEMP_TIMER_PRESCALE - 1;
switch (timer_num) {
case STEP_TIMER_NUM:
//STEPPER TIMER TIM5 //use a 32bit timer
// STEPPER TIMER TIM5 - use a 32bit timer
#ifdef STM32GENERIC
__HAL_RCC_TIM5_CLK_ENABLE();
timerConfig[0].timerdef.Instance = TIM5;
timerConfig[0].timerdef.Init.Prescaler = (STEPPER_TIMER_PRESCALE);
timerConfig[0].timerdef.Init.CounterMode = TIM_COUNTERMODE_UP;
timerConfig[0].timerdef.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
timerConfig[0].IRQ_Id = TIM5_IRQn;
timerConfig[0].callback = (uint32_t)TC5_Handler;
HAL_NVIC_SetPriority(timerConfig[0].IRQ_Id, 1, 0);
TimerHandle[timer_num].handle.Instance = TIM5;
TimerHandle[timer_num].handle.Init.Prescaler = step_prescaler;
TimerHandle[timer_num].handle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimerHandle[timer_num].handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TimerHandle[timer_num].callback = (uint32_t)TC5_Handler;
#else
TimerHandle[timer_num].timer = TIM5;
TimerHandle[timer_num].irqHandle = TC5_Handler;
TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / step_prescaler) / frequency) - 1, step_prescaler);
#endif
HAL_NVIC_SetPriority(STEP_TIMER_IRQ_ID, 6, 0);
break;
case TEMP_TIMER_NUM:
//TEMP TIMER TIM7 // any available 16bit Timer (1 already used for PWM)
// TEMP TIMER TIM7 - any available 16bit Timer (1 already used for PWM)
#ifdef STM32GENERIC
__HAL_RCC_TIM7_CLK_ENABLE();
timerConfig[1].timerdef.Instance = TIM7;
timerConfig[1].timerdef.Init.Prescaler = (TEMP_TIMER_PRESCALE);
timerConfig[1].timerdef.Init.CounterMode = TIM_COUNTERMODE_UP;
timerConfig[1].timerdef.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
timerConfig[1].IRQ_Id = TIM7_IRQn;
timerConfig[1].callback = (uint32_t)TC7_Handler;
HAL_NVIC_SetPriority(timerConfig[1].IRQ_Id, 2, 0);
TimerHandle[timer_num].handle.Instance = TIM7;
TimerHandle[timer_num].handle.Init.Prescaler = temp_prescaler;
TimerHandle[timer_num].handle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimerHandle[timer_num].handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TimerHandle[timer_num].callback = (uint32_t)TC7_Handler;
#else
TimerHandle[timer_num].timer = TIM7;
TimerHandle[timer_num].irqHandle = TC7_Handler;
TimerHandleInit(&TimerHandle[timer_num], (((HAL_TIMER_RATE) / temp_prescaler) / frequency) - 1, temp_prescaler);
#endif
HAL_NVIC_SetPriority(TEMP_TIMER_IRQ_ID, 2, 0);
break;
}
timers_initialised[timer_num] = true;
}
timerConfig[timer_num].timerdef.Init.Period = (((HAL_TIMER_RATE) / timerConfig[timer_num].timerdef.Init.Prescaler) / frequency) - 1;
if (HAL_TIM_Base_Init(&timerConfig[timer_num].timerdef) == HAL_OK)
HAL_TIM_Base_Start_IT(&timerConfig[timer_num].timerdef);
#ifdef STM32GENERIC
TimerHandle[timer_num].handle.Init.Period = (((HAL_TIMER_RATE) / TimerHandle[timer_num].handle.Init.Prescaler) / frequency) - 1;
if (HAL_TIM_Base_Init(&TimerHandle[timer_num].handle) == HAL_OK)
HAL_TIM_Base_Start_IT(&TimerHandle[timer_num].handle);
#endif
}
//forward the interrupt
#ifdef STM32GENERIC
extern "C" void TIM5_IRQHandler() {
((void(*)(void))timerConfig[0].callback)();
((void(*)(void))TimerHandle[0].callback)();
}
extern "C" void TIM7_IRQHandler() {
((void(*)(void))timerConfig[1].callback)();
}
void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare) {
__HAL_TIM_SetAutoreload(&timerConfig[timer_num].timerdef, compare);
((void(*)(void))TimerHandle[1].callback)();
}
#endif
void HAL_timer_enable_interrupt(const uint8_t timer_num) {
HAL_NVIC_EnableIRQ(timerConfig[timer_num].IRQ_Id);
switch (timer_num) {
case STEP_TIMER_NUM: HAL_NVIC_EnableIRQ(STEP_TIMER_IRQ_ID); break;
case TEMP_TIMER_NUM: HAL_NVIC_EnableIRQ(TEMP_TIMER_IRQ_ID); break;
}
}
void HAL_timer_disable_interrupt(const uint8_t timer_num) {
HAL_NVIC_DisableIRQ(timerConfig[timer_num].IRQ_Id);
switch (timer_num) {
case STEP_TIMER_NUM: HAL_NVIC_DisableIRQ(STEP_TIMER_IRQ_ID); break;
case TEMP_TIMER_NUM: HAL_NVIC_DisableIRQ(TEMP_TIMER_IRQ_ID); break;
}
// We NEED memory barriers to ensure Interrupts are actually disabled!
// ( https://dzone.com/articles/nvic-disabling-interrupts-on-arm-cortex-m-and-the )
__DSB();
__ISB();
}
hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
return __HAL_TIM_GetAutoreload(&timerConfig[timer_num].timerdef);
}
uint32_t HAL_timer_get_count(const uint8_t timer_num) {
return __HAL_TIM_GetCounter(&timerConfig[timer_num].timerdef);
}
void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
}
void HAL_timer_isr_prologue(const uint8_t timer_num) {
if (__HAL_TIM_GET_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE) == SET) {
__HAL_TIM_CLEAR_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE);
}
}
bool HAL_timer_interrupt_enabled(const uint8_t timer_num) {
if (NVIC->ISER[(uint32_t)((int32_t)timerConfig[timer_num].IRQ_Id) >> 5] & (uint32_t)(1 << ((uint32_t)((int32_t)timerConfig[timer_num].IRQ_Id) & (uint32_t)0x1F))) {
return true;
switch (timer_num) {
case STEP_TIMER_NUM: return NVIC->ISER[(uint32_t)((int32_t)STEP_TIMER_IRQ_ID) >> 5] & (uint32_t)(1 << ((uint32_t)((int32_t)STEP_TIMER_IRQ_ID) & (uint32_t)0x1F));
case TEMP_TIMER_NUM: return NVIC->ISER[(uint32_t)((int32_t)TEMP_TIMER_IRQ_ID) >> 5] & (uint32_t)(1 << ((uint32_t)((int32_t)TEMP_TIMER_IRQ_ID) & (uint32_t)0x1F));
}
else {
return false;
}
}
#endif // STM32F4
#endif // STM32F4 || STM32F4xx

View file

@ -63,27 +63,38 @@
// TODO change this
#ifdef STM32GENERIC
extern void TC5_Handler();
extern void TC7_Handler();
#define HAL_STEP_TIMER_ISR void TC5_Handler()
#define HAL_TEMP_TIMER_ISR void TC7_Handler()
#else
extern void TC5_Handler(stimer_t *htim);
extern void TC7_Handler(stimer_t *htim);
#define HAL_STEP_TIMER_ISR void TC5_Handler(stimer_t *htim)
#define HAL_TEMP_TIMER_ISR void TC7_Handler(stimer_t *htim)
#endif
// --------------------------------------------------------------------------
// Types
// --------------------------------------------------------------------------
#ifdef STM32GENERIC
typedef struct {
TIM_HandleTypeDef timerdef;
IRQn_Type IRQ_Id;
TIM_HandleTypeDef handle;
uint32_t callback;
} tTimerConfig;
typedef tTimerConfig stm32f4_timer_t;
#else
typedef stimer_t stm32f4_timer_t;
#endif
// --------------------------------------------------------------------------
// Public Variables
// --------------------------------------------------------------------------
//extern const tTimerConfig timerConfig[];
extern stm32f4_timer_t TimerHandle[];
// --------------------------------------------------------------------------
// Public functions
@ -94,12 +105,35 @@ 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);
void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare);
hal_timer_t HAL_timer_get_compare(const uint8_t timer_num);
uint32_t HAL_timer_get_count(const uint8_t timer_num);
void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks);
FORCE_INLINE static uint32_t HAL_timer_get_count(const uint8_t timer_num) {
return __HAL_TIM_GET_COUNTER(&TimerHandle[timer_num].handle);
}
FORCE_INLINE static void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare) {
__HAL_TIM_SET_AUTORELOAD(&TimerHandle[timer_num].handle, compare);
if (HAL_timer_get_count(timer_num) >= compare)
TimerHandle[timer_num].handle.Instance->EGR |= TIM_EGR_UG; // Generate an immediate update interrupt
}
FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
return __HAL_TIM_GET_AUTORELOAD(&TimerHandle[timer_num].handle);
}
FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
if (HAL_timer_get_compare(timer_num) < mincmp)
HAL_timer_set_compare(timer_num, mincmp);
}
#ifdef STM32GENERIC
FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
if (__HAL_TIM_GET_FLAG(&TimerHandle[timer_num].handle, TIM_FLAG_UPDATE) == SET)
__HAL_TIM_CLEAR_FLAG(&TimerHandle[timer_num].handle, TIM_FLAG_UPDATE);
}
#else
#define HAL_timer_isr_prologue(TIMER_NUM)
#endif
void HAL_timer_isr_prologue(const uint8_t timer_num);
#define HAL_timer_isr_epilogue(TIMER_NUM)
#endif // _HAL_TIMERS_STM32F4_H

View file

@ -21,7 +21,7 @@
*
*/
#ifdef STM32F4
#if defined(STM32F4) || defined(STM32F4xx)
#include "../persistent_store_api.h"
@ -72,4 +72,4 @@ bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc, const boo
} // HAL
#endif // EEPROM_SETTINGS
#endif // STM32F4
#endif // STM32F4 || STM32F4xx

View file

@ -20,7 +20,7 @@
*
*/
#ifdef STM32F4
#if defined(STM32F4) || defined(STM32F4xx)
#include "../../inc/MarlinConfig.h"
@ -54,4 +54,4 @@
#endif // USE_WATCHDOG
#endif // STM32F4
#endif // STM32F4 || STM32F4xx

View file

@ -62,7 +62,7 @@
#define END_FLASH_ADDR 0x00080000
#endif
#ifdef STM32F4
#if defined(STM32F4) || defined(STM32F4xx)
// For STM32F407VET
// SRAM (0x20000000 - 0x20030000) (192kb)
// FLASH (0x08000000 - 0x08080000) (512kb)

View file

@ -13,7 +13,7 @@
#define HAL_PLATFORM HAL_LPC1768
#elif defined(__STM32F1__) || defined(TARGET_STM32F1)
#define HAL_PLATFORM HAL_STM32F1
#elif defined(STM32F4)
#elif defined(STM32F4) || defined(STM32F4xx)
#define HAL_PLATFORM HAL_STM32F4
#elif defined(STM32F7)
#define HAL_PLATFORM HAL_STM32F7

View file

@ -53,7 +53,7 @@
#include "../inc/MarlinConfig.h"
#if HAS_SERVOS && !(IS_32BIT_TEENSY || defined(TARGET_LPC1768) || defined(STM32F4))
#if HAS_SERVOS && !(IS_32BIT_TEENSY || defined(TARGET_LPC1768) || defined(STM32F4) || defined(STM32F4xx))
//#include <Arduino.h>
#include "servo.h"

View file

@ -74,7 +74,7 @@
#elif defined(TARGET_LPC1768)
#include "HAL_LPC1768/LPC1768_Servo.h"
#elif defined(STM32F4)
#elif defined(STM32F4) || defined(STM32F4xx)
#include "HAL_STM32F4/HAL_Servo_STM32F4.h"
#else
#include <stdint.h>

View file

@ -20,7 +20,7 @@
*
*/
#if !defined(STM32F4)
#if !defined(STM32F4) && !defined(STM32F4xx)
#error "Oops! Make sure you have an STM32F4 board selected from the 'Tools -> Boards' menu."
#endif

View file

@ -265,7 +265,7 @@ monitor_speed = 250000
platform = ststm32
framework = arduino
board = disco_f407vg
build_flags = ${common.build_flags} -DUSE_STM32GENERIC -DMENU_USB_SERIAL -DMENU_SERIAL=SerialUSB
build_flags = ${common.build_flags} -DUSE_STM32GENERIC -DSTM32GENERIC -DMENU_USB_SERIAL -DMENU_SERIAL=SerialUSB
lib_deps = ${common.lib_deps}
lib_ignore = Adafruit NeoPixel, c1921b4, TMC2130Stepper
src_filter = ${common.default_src_filter}