#include #include "Board_LED.h" // ::Board Support:LED #include "Board_Buttons.h" // ::Board Support:Buttons #include "stm32f4xx.h" // Device header #include "ecl_timer.h" #define N_TIMERS 4 struct ecl_timer_config timers[N_TIMERS]; ecl_timer_id_t short_toggle; ecl_timer_id_t long_toggle; ecl_timer_id_t start_timers; ecl_timer_id_t swap_timers; /*---------------------------------------------------------------------------- * SystemCoreClockConfigure: configure SystemCoreClock using HSI (HSE is not populated on Nucleo board) *----------------------------------------------------------------------------*/ void SystemCoreClockConfigure(void) { RCC->CR |= ((uint32_t)RCC_CR_HSION); // Enable HSI while ((RCC->CR & RCC_CR_HSIRDY) == 0); // Wait for HSI Ready RCC->CFGR = RCC_CFGR_SW_HSI; // HSI is system clock while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI); // Wait for HSI used as system clock FLASH->ACR = FLASH_ACR_PRFTEN; // Enable Prefetch Buffer FLASH->ACR |= FLASH_ACR_ICEN; // Instruction cache enable FLASH->ACR |= FLASH_ACR_DCEN; // Data cache enable FLASH->ACR |= FLASH_ACR_LATENCY_5WS; // Flash 5 wait state RCC->CFGR |= RCC_CFGR_HPRE_DIV1; // HCLK = SYSCLK RCC->CFGR |= RCC_CFGR_PPRE1_DIV4; // APB1 = HCLK/4 RCC->CFGR |= RCC_CFGR_PPRE2_DIV2; // APB2 = HCLK/2 RCC->CR &= ~RCC_CR_PLLON; // Disable PLL // PLL configuration: VCO = HSI/M * N, Sysclk = VCO/P RCC->PLLCFGR = ( 16ul | // PLL_M = 16 (384ul << 6) | // PLL_N = 384 ( 3ul << 16) | // PLL_P = 8 (RCC_PLLCFGR_PLLSRC_HSI) | // PLL_SRC = HSI ( 8ul << 24) ); // PLL_Q = 8 RCC->CR |= RCC_CR_PLLON; // Enable PLL while((RCC->CR & RCC_CR_PLLRDY) == 0) __NOP(); // Wait till PLL is ready RCC->CFGR &= ~RCC_CFGR_SW; // Select PLL as system clock source RCC->CFGR |= RCC_CFGR_SW_PLL; while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL); // Wait till PLL is system clock src } void StartTimers() { ecl_timer_start(short_toggle, ECL_TIMER_START_DELAYED); ecl_timer_start(swap_timers, ECL_TIMER_START_DELAYED); } void SwapTimers() { static int state = 0; if (!state) { ecl_timer_stop(short_toggle); ecl_timer_start(long_toggle, ECL_TIMER_START_DELAYED); } else { ecl_timer_start(short_toggle, ECL_TIMER_START_DELAYED); ecl_timer_stop(long_toggle); } state = !state; ecl_timer_start(swap_timers, ECL_TIMER_START_DELAYED); } void LedToggle() { static int state = 0; if (state) { LED_On(0); } else { LED_Off(0); } state = !state; } int main() { SystemCoreClockConfigure(); // configure HSI as System Clock SystemCoreClockUpdate(); LED_Initialize(); Buttons_Initialize(); ecl_timer_init(timers, N_TIMERS); // The LEDs will start flashing fast after 2 seconds. // After another 5 seconds they will start flashing slower. short_toggle = ecl_timer_register(LedToggle, 50, ECL_TIMER_REPEATING); long_toggle = ecl_timer_register(LedToggle, 100, ECL_TIMER_REPEATING); start_timers = ecl_timer_register(StartTimers, 2000, ECL_TIMER_SINGLE_SHOT); swap_timers = ecl_timer_register(SwapTimers, 1500, ECL_TIMER_SINGLE_SHOT); SysTick_Config(SystemCoreClock / 1000); ecl_timer_enable(ECL_TIMER_ENABLED); ecl_timer_start(start_timers, ECL_TIMER_START_DELAYED); while (1) { __NOP(); } } void SysTick_Handler() { const uint32_t TICK = 1; static uint32_t nticks = TICK; if (ecl_timer_tick(nticks)) { nticks = TICK; } else { nticks += TICK; } }