diff --git a/Makefile-ARM b/Makefile-ARM index f16f0b4..19bbc0c 100644 --- a/Makefile-ARM +++ b/Makefile-ARM @@ -139,6 +139,7 @@ else ifeq ($(MCU), stm32f411) CFLAGS += -mfloat-abi=hard CFLAGS += -mlittle-endian CFLAGS += -D__FPU_PRESENT=1 + CFLAGS += -fomit-frame-pointer endif # Other options ... CFLAGS += -Wall diff --git a/arduino_stm32f411.h b/arduino_stm32f411.h index 672eb0f..994ddf8 100644 --- a/arduino_stm32f411.h +++ b/arduino_stm32f411.h @@ -20,18 +20,36 @@ #include "cmsis-stm32f4xx.h" -#define F_CPU __SYSTEM_CLOCK - /** Pins for UART, the serial port. */ #define RXD PA_3 #define TXD PA_2 +/** Clock setup for APB1 and APB2 clock. +*/ +#define F_CPU __SYSTEM_CLOCK + +#define PPRE1_DIV (RCC_CFGR_PPRE1_DIV2) // 0x1000 +#define PPRE2_DIV (RCC_CFGR_PPRE2_DIV1) // 0x0000 + +#if PPRE1_DIV > 0 + #define APB1_DIV (1 << ((PPRE1_DIV >> 10) - 3)) +#else + #define APB1_DIV (1) +#endif +#if PPRE2_DIV > 0 + #define APB2_DIV (1 << ((PPRE2_DIV >> 13) - 3)) +#else + #define APB2_DIV (1) +#endif + +#define _APB1_CLOCK (__SYSTEM_CLOCK/APB1_DIV) +#define _APB2_CLOCK (__SYSTEM_CLOCK/APB2_DIV) + /** We define only pins available on the Nucleo F411RE here. Use alphas for PORT and numerics for PIN, close to the design. */ - #define NO_TIMER ((TIM_TypeDef *) 0) #define PA_0_PIN 0 diff --git a/cmsis-stm32f4xx.h b/cmsis-stm32f4xx.h index 69e8511..69cb392 100644 --- a/cmsis-stm32f4xx.h +++ b/cmsis-stm32f4xx.h @@ -67,10 +67,6 @@ #ifndef __STM32F4xx_H #define __STM32F4xx_H -#ifdef __cplusplus - extern "C" { -#endif /* __cplusplus */ - /** @addtogroup Library_configuration_section * @{ */ @@ -727,17 +723,17 @@ USB_OTG_HostChannelTypeDef; /** * @brief Peripheral_memory_map */ -#define FLASH_BASE ((uint32_t)0x08000000) /*!< FLASH(up to 1 MB) base address in the alias region */ -#define CCMDATARAM_BASE ((uint32_t)0x10000000) /*!< CCM(core coupled memory) data RAM(64 KB) base address in the alias region */ -#define SRAM1_BASE ((uint32_t)0x20000000) /*!< SRAM1(112 KB) base address in the alias region */ -#define SRAM2_BASE ((uint32_t)0x2001C000) /*!< SRAM2(16 KB) base address in the alias region */ -#define PERIPH_BASE ((uint32_t)0x40000000) /*!< Peripheral base address in the alias region */ -#define BKPSRAM_BASE ((uint32_t)0x40024000) /*!< Backup SRAM(4 KB) base address in the alias region */ -#define SRAM1_BB_BASE ((uint32_t)0x22000000) /*!< SRAM1(112 KB) base address in the bit-band region */ -#define SRAM2_BB_BASE ((uint32_t)0x22380000) /*!< SRAM2(16 KB) base address in the bit-band region */ -#define PERIPH_BB_BASE ((uint32_t)0x42000000) /*!< Peripheral base address in the bit-band region */ -#define BKPSRAM_BB_BASE ((uint32_t)0x42480000) /*!< Backup SRAM(4 KB) base address in the bit-band region */ -#define FLASH_END ((uint32_t)0x0807FFFF) /*!< FLASH end address */ +#define FLASH_BASE (0x08000000UL) /*!< FLASH(up to 1 MB) base address in the alias region */ +#define CCMDATARAM_BASE (0x10000000UL) /*!< CCM(core coupled memory) data RAM(64 KB) base address in the alias region */ +#define SRAM1_BASE (0x20000000UL) /*!< SRAM1(112 KB) base address in the alias region */ +#define SRAM2_BASE (0x2001C000UL) /*!< SRAM2(16 KB) base address in the alias region */ +#define PERIPH_BASE (0x40000000UL) /*!< Peripheral base address in the alias region */ +#define BKPSRAM_BASE (0x40024000UL) /*!< Backup SRAM(4 KB) base address in the alias region */ +#define SRAM1_BB_BASE (0x22000000UL) /*!< SRAM1(112 KB) base address in the bit-band region */ +#define SRAM2_BB_BASE (0x22380000UL) /*!< SRAM2(16 KB) base address in the bit-band region */ +#define PERIPH_BB_BASE (0x42000000UL) /*!< Peripheral base address in the bit-band region */ +#define BKPSRAM_BB_BASE (0x42480000UL) /*!< Backup SRAM(4 KB) base address in the bit-band region */ +#define FLASH_END (0x0807FFFFUL) /*!< FLASH end address */ /* Legacy defines */ #define SRAM_BASE SRAM1_BASE @@ -814,23 +810,23 @@ USB_OTG_HostChannelTypeDef; #define DMA2_Stream7_BASE (DMA2_BASE + 0x0B8) /* Debug MCU registers base address */ -#define DBGMCU_BASE ((uint32_t )0xE0042000) +#define DBGMCU_BASE (0xE0042000UL) /*!< USB registers base address */ -#define USB_OTG_FS_PERIPH_BASE ((uint32_t )0x50000000) +#define USB_OTG_FS_PERIPH_BASE (0x50000000UL) -#define USB_OTG_GLOBAL_BASE ((uint32_t )0x000) -#define USB_OTG_DEVICE_BASE ((uint32_t )0x800) -#define USB_OTG_IN_ENDPOINT_BASE ((uint32_t )0x900) -#define USB_OTG_OUT_ENDPOINT_BASE ((uint32_t )0xB00) -#define USB_OTG_EP_REG_SIZE ((uint32_t )0x20) -#define USB_OTG_HOST_BASE ((uint32_t )0x400) -#define USB_OTG_HOST_PORT_BASE ((uint32_t )0x440) -#define USB_OTG_HOST_CHANNEL_BASE ((uint32_t )0x500) -#define USB_OTG_HOST_CHANNEL_SIZE ((uint32_t )0x20) -#define USB_OTG_PCGCCTL_BASE ((uint32_t )0xE00) -#define USB_OTG_FIFO_BASE ((uint32_t )0x1000) -#define USB_OTG_FIFO_SIZE ((uint32_t )0x1000) +#define USB_OTG_GLOBAL_BASE (0x000UL) +#define USB_OTG_DEVICE_BASE (0x800UL) +#define USB_OTG_IN_ENDPOINT_BASE (0x900UL) +#define USB_OTG_OUT_ENDPOINT_BASE (0xB00UL) +#define USB_OTG_EP_REG_SIZE (0x20UL) +#define USB_OTG_HOST_BASE (0x400UL) +#define USB_OTG_HOST_PORT_BASE (0x440UL) +#define USB_OTG_HOST_CHANNEL_BASE (0x500UL) +#define USB_OTG_HOST_CHANNEL_SIZE (0x20UL) +#define USB_OTG_PCGCCTL_BASE (0xE00UL) +#define USB_OTG_FIFO_BASE (0x1000UL) +#define USB_OTG_FIFO_SIZE (0x1000UL) /** * @} @@ -923,360 +919,360 @@ USB_OTG_HostChannelTypeDef; /* */ /******************************************************************************/ /******************** Bit definition for ADC_SR register ********************/ -#define ADC_SR_AWD ((uint32_t)0x00000001) /*!CR |= (uint32_t)0x00000001; + RCC->CR |= RCC_CR_HSION; /* Reset CFGR register */ RCC->CFGR = 0x00000000; /* Reset HSEON, CSSON and PLLON bits */ - RCC->CR &= (uint32_t)0xFEF6FFFF; + RCC->CR &= ~(RCC_CR_HSEON | + RCC_CR_CSSON | + RCC_CR_PLLON); /* Reset PLLCFGR register */ RCC->PLLCFGR = 0x24003010; /* Reset HSEBYP bit */ - RCC->CR &= (uint32_t)0xFFFBFFFF; + RCC->CR &= ~(RCC_CR_HSEBYP); /* Disable all interrupts */ RCC->CIR = 0x00000000; @@ -209,7 +156,6 @@ void SystemInit(void) /* Configure the System clock source, PLL Multiplier and Divider factors, AHB/APBx prescalers and Flash settings */ SetSysClock(); - } /** @@ -257,13 +203,13 @@ void SystemCoreClockUpdate(void) switch (tmp) { - case 0x00: /* HSI used as system clock source */ + case RCC_CFGR_SWS_HSI: /* HSI used as system clock source */ SystemCoreClock = HSI_VALUE; break; - case 0x04: /* HSE used as system clock source */ + case RCC_CFGR_SWS_HSE: /* HSE used as system clock source */ SystemCoreClock = HSE_VALUE; break; - case 0x08: /* PLL used as system clock source */ + case RCC_CFGR_SWS_PLL: /* PLL used as system clock source */ /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N SYSCLK = PLL_VCO / PLL_P @@ -312,57 +258,61 @@ void SetSysClock(void) regarding system frequency refer to product datasheet. */ RCC->APB1ENR |= RCC_APB1ENR_PWREN; - MODIFY_REG(PWR->CR, PWR_CR_VOS, PWR_CR_VOS_1); + PWR->CR |= PWR_CR_VOS; /* Enable HSE oscillator and activate PLL with HSE as source */ /*------------------------------- HSE Configuration ------------------------*/ /* Reset HSEON and HSEBYP bits before configuring the HSE --------------*/ RCC->CR &= ~(RCC_CR_HSEON); - //__HAL_RCC_HSE_CONFIG(RCC_HSE_OFF); - - /* Wait till HSE is disabled */ while(RCC->CR & RCC_CR_HSERDY); /* Set the new HSE configuration ---------------------------------------*/ RCC->CR |= RCC_CR_HSEON; - - /* Wait till HSE is ready */ while(!(RCC->CR & RCC_CR_HSERDY)); /*-------------------------------- PLL Configuration -----------------------*/ /* Disable the main PLL. */ RCC->CR &= ~(RCC_CR_PLLON); - - /* Wait till PLL is ready */ while(RCC->CR & RCC_CR_PLLRDY); /* Configure the main PLL clock source, multiplication and division factors. */ - // PLLM_2: VCO input clock = 2 MHz (8 MHz / 4) - // PLLN_6/7: VCO output clock = 384 MHz (2 MHz * 192) - // PLLP_0: PLLCLK = 96 MHz (384 MHz / 4) - // PLLQ_3: USB clock = 48 MHz (384 MHz / 8) --> 48MHz is best choice for USB + // PLLM: VCO input clock = 2 MHz (8 MHz / 4) + // PLLN: VCO output clock = 384 MHz (2 MHz * 192) + // PLLP: PLLCLK = 96 MHz (384 MHz / 4) + // PLLQ: USB clock = 48 MHz (384 MHz / 8) --> 48MHz is best choice for USB #if __SYSTEM_CLOCK == 96000000 - RCC->PLLCFGR = RCC_PLLCFGR_PLLSRC_HSE | RCC_PLLCFGR_PLLM_2 | \ - RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | \ - RCC_PLLCFGR_PLLP_0 | \ - RCC_PLLCFGR_PLLQ_3; + #define PLLM 4 + #define PLLN 192 + #define PLLP 4 + #define PLLQ 8 #elif __SYSTEM_CLOCK == 100000000 - RCC->PLLCFGR = RCC_PLLCFGR_PLLSRC_HSE | RCC_PLLCFGR_PLLM_2 | \ - RCC_PLLCFGR_PLLN_3 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | \ - RCC_PLLCFGR_PLLP_0 | \ - RCC_PLLCFGR_PLLQ_3; + #define PLLM 4 + #define PLLN 200 + #define PLLP 4 + #define PLLQ 8 #elif __SYSTEM_CLOCK == 108000000 - RCC->PLLCFGR = RCC_PLLCFGR_PLLSRC_HSE | RCC_PLLCFGR_PLLM_2 | \ - RCC_PLLCFGR_PLLN_3 | RCC_PLLCFGR_PLLN_4 | RCC_PLLCFGR_PLLN_6 | RCC_PLLCFGR_PLLN_7 | \ - RCC_PLLCFGR_PLLP_0 | \ - RCC_PLLCFGR_PLLQ_0 | RCC_PLLCFGR_PLLQ_3; + #warning You are running the controller out of specification! + #define PLLM 4 + #define PLLN 216 + #define PLLP 4 + #define PLLQ 9 + #elif __SYSTEM_CLOCK == 125000000 + #warning You are running the controller out of specification! + #define PLLM 4 + #define PLLN 250 + #define PLLP 4 + #define PLLQ 10 #endif + RCC->PLLCFGR = RCC_PLLCFGR_PLLSRC_HSE | + ((PLLM << 0) & RCC_PLLCFGR_PLLM) | + ((PLLN << 6) & RCC_PLLCFGR_PLLN) | + (((PLLP/2 - 1) << 16) & RCC_PLLCFGR_PLLP) | + ((PLLQ << 24) & RCC_PLLCFGR_PLLQ); + /* Enable the main PLL. */ RCC->CR |= RCC_CR_PLLON; - - /* Wait till PLL is ready */ while(!(RCC->CR & RCC_CR_PLLRDY)); /* To correctly read data from FLASH memory, the number of wait states (LATENCY) @@ -373,39 +323,26 @@ void SetSysClock(void) if(FLASH_ACR_LATENCY_3WS > (FLASH->ACR & FLASH_ACR_LATENCY)) { /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */ - MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, FLASH_ACR_LATENCY_3WS); - /*-------------------------- HCLK Configuration --------------------------*/ - MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_CFGR_HPRE_DIV1); - /*------------------------- SYSCLK Configuration ---------------------------*/ - MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL); + FLASH->ACR &= ~(FLASH_ACR_LATENCY); + FLASH->ACR |= FLASH_ACR_LATENCY_3WS; + + RCC->CFGR &= ~(RCC_CFGR_HPRE | RCC_CFGR_SW); + RCC->CFGR |= RCC_CFGR_HPRE_DIV1 | RCC_CFGR_SW_PLL; } /* Decreasing the CPU frequency */ else { - /*-------------------------- HCLK Configuration --------------------------*/ - MODIFY_REG(RCC->CFGR, RCC_CFGR_HPRE, RCC_CFGR_HPRE_DIV1); - /*------------------------- SYSCLK Configuration -------------------------*/ - MODIFY_REG(RCC->CFGR, RCC_CFGR_SW, RCC_CFGR_SW_PLL); + RCC->CFGR &= ~(RCC_CFGR_HPRE | RCC_CFGR_SW); + RCC->CFGR |= RCC_CFGR_HPRE_DIV1 | RCC_CFGR_SW_PLL; + /* Program the new number of wait states to the LATENCY bits in the FLASH_ACR register */ - MODIFY_REG(FLASH->ACR, FLASH_ACR_LATENCY, FLASH_ACR_LATENCY_3WS); + FLASH->ACR &= ~(FLASH_ACR_LATENCY); + FLASH->ACR |= FLASH_ACR_LATENCY_3WS; } - /*-------------------------- PCLK1 Configuration ---------------------------*/ - MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE1, RCC_CFGR_PPRE1_DIV2); - /*-------------------------- PCLK2 Configuration ---------------------------*/ - MODIFY_REG(RCC->CFGR, RCC_CFGR_PPRE2, RCC_CFGR_PPRE2_DIV1); + RCC->CFGR &= ~(RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2); + RCC->CFGR |= PPRE1_DIV | PPRE2_DIV; } -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ #endif diff --git a/cmsis-system_stm32f4xx.h b/cmsis-system_stm32f4xx.h index 6b7ad9e..c57ef09 100644 --- a/cmsis-system_stm32f4xx.h +++ b/cmsis-system_stm32f4xx.h @@ -41,16 +41,9 @@ - Prefixed names of #include files with cmsis- to match the names of the copies in the Teacup repo. - + - Cleanup file + - Add different clock frequencies */ - -/** @addtogroup CMSIS - * @{ - */ - -/** @addtogroup stm32f4xx_system - * @{ - */ /** * @brief Define to prevent recursive inclusion @@ -65,59 +58,15 @@ // #define __SYSTEM_CLOCK 96000000 #define __SYSTEM_CLOCK 100000000 // #define __SYSTEM_CLOCK 108000000 // Overclocking is not recommended! -/** @addtogroup STM32F4xx_System_Includes - * @{ - */ -/** - * @} - */ - - -/** @addtogroup STM32F4xx_System_Exported_types - * @{ - */ - /* This variable is updated in three ways: - 1) by calling CMSIS function SystemCoreClockUpdate() - 2) by calling HAL API function HAL_RCC_GetSysClockFreq() - 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency - Note: If you use this function to configure the system clock; then there - is no need to call the 2 first functions listed above, since SystemCoreClock - variable is updated automatically. - */ +/* + This variable is updated in by calling CMSIS function SystemCoreClockUpdate() +*/ extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ - -/** - * @} - */ - -/** @addtogroup STM32F4xx_System_Exported_Constants - * @{ - */ - -/** - * @} - */ - -/** @addtogroup STM32F4xx_System_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @addtogroup STM32F4xx_System_Exported_Functions - * @{ - */ - extern void SystemInit(void); extern void SystemCoreClockUpdate(void); extern void SetSysClock(void); -/** - * @} - */ #ifdef __cplusplus } @@ -125,11 +74,4 @@ extern void SetSysClock(void); #endif /*__SYSTEM_STM32F4XX_H */ -/** - * @} - */ - -/** - * @} - */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/