|
| 1 | +/* |
| 2 | + * This file is part of the libopencm3 project. |
| 3 | + * |
| 4 | + * Copyright (C) 2009 Uwe Hermann <[email protected]> |
| 5 | + * Copyright (C) 2011 Stephen Caudle <[email protected]> |
| 6 | + * Copyright (C) 2012 Daniel Serpell <[email protected]> |
| 7 | + * Copyright (C) 2015 Piotr Esden-Tempski <[email protected]> |
| 8 | + * Copyright (C) 2015 Chuck McManis <[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 | +#include <stdio.h> |
| 25 | +#include <ctype.h> |
| 26 | +#include <libopencm3/stm32/rcc.h> |
| 27 | +#include <libopencm3/stm32/gpio.h> |
| 28 | +#include <libopencm3/stm32/usart.h> |
| 29 | +#include <libopencm3/cm3/systick.h> |
| 30 | +#include <libopencm3/cm3/nvic.h> |
| 31 | +#include "clock.h" |
| 32 | + |
| 33 | +void clock_setup(void) |
| 34 | +{ |
| 35 | + rcc_clock_setup_hse_3v3(&hse_8mhz_3v3[CLOCK_3V3_168MHZ]); |
| 36 | + |
| 37 | + /* set up the SysTick function (1mS interrupts) */ |
| 38 | + systick_set_clocksource(STK_CSR_CLKSOURCE_AHB); |
| 39 | + STK_CVR = 0; |
| 40 | + systick_set_reload(rcc_ahb_frequency / 1000); |
| 41 | + systick_counter_enable(); |
| 42 | + systick_interrupt_enable(); |
| 43 | +} |
| 44 | + |
| 45 | +/* simple millisecond counter */ |
| 46 | +static volatile uint32_t system_millis; |
| 47 | +static volatile uint32_t delay_timer; |
| 48 | + |
| 49 | +/* |
| 50 | + * Simple systick handler |
| 51 | + * |
| 52 | + * Increments a 32 bit value once per millesecond |
| 53 | + * which rolls over every 49 days. |
| 54 | + */ |
| 55 | +void sys_tick_handler(void) |
| 56 | +{ |
| 57 | + system_millis++; |
| 58 | + if (delay_timer > 0) { |
| 59 | + delay_timer--; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + * Simple spin loop waiting for time to pass |
| 65 | + * |
| 66 | + * A couple of things to note: |
| 67 | + * First, you can't just compare to |
| 68 | + * system_millis because doing so will mean |
| 69 | + * you delay forever if you happen to hit a |
| 70 | + * time where it is rolling over. |
| 71 | + * Second, accuracy is "at best" 1mS as you |
| 72 | + * may call this "just before" the systick hits |
| 73 | + * with a value of '1' and it would return |
| 74 | + * nearly immediately. So if you need really |
| 75 | + * precise delays, use one of the timers. |
| 76 | + */ |
| 77 | +void |
| 78 | +msleep(uint32_t delay) |
| 79 | +{ |
| 80 | + delay_timer = delay; |
| 81 | + while (delay_timer); |
| 82 | +} |
| 83 | + |
| 84 | +uint32_t |
| 85 | +mtime(void) |
| 86 | +{ |
| 87 | + return system_millis; |
| 88 | +} |
0 commit comments