|
| 1 | +/* |
| 2 | + * This file is part of the libopencm3 project. |
| 3 | + * |
| 4 | + * Copyright (C) 2013 Chuck McManis <[email protected]> |
| 5 | + * |
| 6 | + * This library is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Lesser General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This library is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Now this is just the clock setup code from systick-blink as it is the |
| 22 | + * transferrable part. |
| 23 | + */ |
| 24 | + |
| 25 | +#include <libopencm3/stm32/rcc.h> |
| 26 | +#include <libopencm3/stm32/gpio.h> |
| 27 | +#include <libopencm3/cm3/nvic.h> |
| 28 | +#include <libopencm3/cm3/systick.h> |
| 29 | + |
| 30 | +/* Common function descriptions */ |
| 31 | +#include "../util/clock.h" |
| 32 | + |
| 33 | +/* milliseconds since boot */ |
| 34 | +static volatile uint32_t system_millis; |
| 35 | +/* millisecond count down when sleeping */ |
| 36 | +static volatile uint32_t system_delay; |
| 37 | + |
| 38 | +/* Called when systick fires */ |
| 39 | +void sys_tick_handler(void) |
| 40 | +{ |
| 41 | + system_millis++; |
| 42 | + if (system_delay != 0) { |
| 43 | + system_delay--; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/* simple sleep for delay milliseconds */ |
| 48 | +void msleep(uint32_t delay) |
| 49 | +{ |
| 50 | + system_delay = delay; |
| 51 | + while (system_delay > 0); |
| 52 | +} |
| 53 | + |
| 54 | +/* Getter function for the current time */ |
| 55 | +uint32_t mtime(void) |
| 56 | +{ |
| 57 | + return system_millis; |
| 58 | +} |
| 59 | + |
| 60 | +/* |
| 61 | + * clock_setup(void) |
| 62 | + * |
| 63 | + * This function sets up both the base board clock rate |
| 64 | + * and a 1khz "system tick" count. The SYSTICK counter is |
| 65 | + * a standard feature of the Cortex-M series. |
| 66 | + */ |
| 67 | +void clock_setup(void) |
| 68 | +{ |
| 69 | + /* Base board frequency, set to 168Mhz */ |
| 70 | + rcc_clock_setup_hse_3v3(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ]); |
| 71 | + |
| 72 | + /* clock rate / 168000 to get 1mS interrupt rate */ |
| 73 | + systick_set_reload(168000); |
| 74 | + systick_set_clocksource(STK_CSR_CLKSOURCE_AHB); |
| 75 | + systick_counter_enable(); |
| 76 | + |
| 77 | + /* this done last */ |
| 78 | + systick_interrupt_enable(); |
| 79 | +} |
0 commit comments