|
| 1 | +/* |
| 2 | + * This file is part of the libopencm3 project. |
| 3 | + * |
| 4 | + * Copyright (C) 2009 Uwe Hermann <[email protected]> |
| 5 | + * Copyright (C) 2011 Damjan Marion <[email protected]> |
| 6 | + * Copyright (C) 2011 Mark Panajotovic <[email protected]> |
| 7 | + * Copyright (C) 2013 Chuck McManis <[email protected]> |
| 8 | + * Copyright (C) 2015 Piotr Esden-Tempski <[email protected]> |
| 9 | + * |
| 10 | + * This library is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Lesser General Public License as published by |
| 12 | + * the Free Software Foundation, either version 3 of the License, or |
| 13 | + * (at your option) any later version. |
| 14 | + * |
| 15 | + * This library is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU Lesser General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Lesser General Public License |
| 21 | + * along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + */ |
| 23 | + |
| 24 | +/* This version derived from fancy blink */ |
| 25 | + |
| 26 | +#include <libopencm3/stm32/rcc.h> |
| 27 | +#include <libopencm3/stm32/gpio.h> |
| 28 | +#include <libopencm3/cm3/nvic.h> |
| 29 | +#include <libopencm3/cm3/systick.h> |
| 30 | + |
| 31 | +/* monotonically increasing number of milliseconds from reset |
| 32 | + * overflows every 49 days if you're wondering |
| 33 | + */ |
| 34 | +volatile uint32_t system_millis; |
| 35 | + |
| 36 | +/* Called when systick fires */ |
| 37 | +void sys_tick_handler(void) |
| 38 | +{ |
| 39 | + system_millis++; |
| 40 | +} |
| 41 | + |
| 42 | +/* sleep for delay milliseconds */ |
| 43 | +static void msleep(uint32_t delay) |
| 44 | +{ |
| 45 | + uint32_t wake = system_millis + delay; |
| 46 | + while (wake > system_millis); |
| 47 | +} |
| 48 | + |
| 49 | +/* |
| 50 | + * systick_setup(void) |
| 51 | + * |
| 52 | + * This function sets up the 1khz "system tick" count. The SYSTICK counter is a |
| 53 | + * standard feature of the Cortex-M series. |
| 54 | + */ |
| 55 | +static void systick_setup(void) |
| 56 | +{ |
| 57 | + /* clock rate / 1000 to get 1mS interrupt rate */ |
| 58 | + systick_set_reload(168000); |
| 59 | + systick_set_clocksource(STK_CSR_CLKSOURCE_AHB); |
| 60 | + systick_counter_enable(); |
| 61 | + /* this done last */ |
| 62 | + systick_interrupt_enable(); |
| 63 | +} |
| 64 | + |
| 65 | +/* Set STM32 system clock to 168 MHz. */ |
| 66 | +static void clock_setup(void) |
| 67 | +{ |
| 68 | + rcc_clock_setup_hse_3v3(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]); |
| 69 | + |
| 70 | + /* Enable GPIOD clock. */ |
| 71 | + rcc_periph_clock_enable(RCC_GPIOD); |
| 72 | +} |
| 73 | + |
| 74 | +static void gpio_setup(void) |
| 75 | +{ |
| 76 | + /* Set GPIO4-5 (in GPIO port D) to 'output push-pull'. */ |
| 77 | + gpio_mode_setup(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, |
| 78 | + GPIO4 | GPIO5); |
| 79 | +} |
| 80 | + |
| 81 | +int main(void) |
| 82 | +{ |
| 83 | + clock_setup(); |
| 84 | + gpio_setup(); |
| 85 | + systick_setup(); |
| 86 | + |
| 87 | + /* Set two LEDs for wigwag effect when toggling. */ |
| 88 | + gpio_set(GPIOD, GPIO4); |
| 89 | + |
| 90 | + /* Blink the LEDs (PG13 and PG14) on the board. */ |
| 91 | + while (1) { |
| 92 | + gpio_toggle(GPIOD, GPIO4 | GPIO5); |
| 93 | + msleep(100); |
| 94 | + } |
| 95 | + |
| 96 | + return 0; |
| 97 | +} |
0 commit comments