amiro-blt / Target / Source / ARMCM4_STM32 / timer.c @ 768babc5
History | View | Annotate | Download (4.944 KB)
1 |
/************************************************************************************//** |
---|---|
2 |
* \file Source\ARMCM4_STM32\timer.c
|
3 |
* \brief Bootloader timer driver source file.
|
4 |
* \ingroup Target_ARMCM4_STM32
|
5 |
* \internal
|
6 |
*----------------------------------------------------------------------------------------
|
7 |
* C O P Y R I G H T
|
8 |
*----------------------------------------------------------------------------------------
|
9 |
* Copyright (c) 2013 by Feaser http://www.feaser.com All rights reserved
|
10 |
*
|
11 |
*----------------------------------------------------------------------------------------
|
12 |
* L I C E N S E
|
13 |
*----------------------------------------------------------------------------------------
|
14 |
* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
|
15 |
* modify it under the terms of the GNU General Public License as published by the Free
|
16 |
* Software Foundation, either version 3 of the License, or (at your option) any later
|
17 |
* version.
|
18 |
*
|
19 |
* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
20 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
21 |
* PURPOSE. See the GNU General Public License for more details.
|
22 |
*
|
23 |
* You should have received a copy of the GNU General Public License along with OpenBLT.
|
24 |
* If not, see <http://www.gnu.org/licenses/>.
|
25 |
*
|
26 |
* A special exception to the GPL is included to allow you to distribute a combined work
|
27 |
* that includes OpenBLT without being obliged to provide the source code for any
|
28 |
* proprietary components. The exception text is included at the bottom of the license
|
29 |
* file <license.html>.
|
30 |
*
|
31 |
* \endinternal
|
32 |
****************************************************************************************/
|
33 |
|
34 |
/****************************************************************************************
|
35 |
* Include files
|
36 |
****************************************************************************************/
|
37 |
#include "boot.h" /* bootloader generic header */ |
38 |
#include "stm32f4xx.h" /* STM32 registers */ |
39 |
|
40 |
|
41 |
/****************************************************************************************
|
42 |
* Local data declarations
|
43 |
****************************************************************************************/
|
44 |
/** \brief Local variable for storing the number of milliseconds that have elapsed since
|
45 |
* startup.
|
46 |
*/
|
47 |
static blt_int32u millisecond_counter;
|
48 |
|
49 |
|
50 |
/************************************************************************************//** |
51 |
** \brief Initializes the polling based millisecond timer driver.
|
52 |
** \return none.
|
53 |
**
|
54 |
****************************************************************************************/
|
55 |
void TimerInit(void) |
56 |
{ |
57 |
/* reset the timer configuration */
|
58 |
TimerReset(); |
59 |
|
60 |
/* configure the systick frequency as a 1 ms event generator */
|
61 |
SysTick->LOAD = BOOT_CPU_SYSTEM_SPEED_KHZ - 1;
|
62 |
/* reset the current counter value */
|
63 |
SysTick->VAL = 0;
|
64 |
/* select core clock as source and enable the timer */
|
65 |
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk; |
66 |
/* reset the millisecond counter value */
|
67 |
millisecond_counter = 0;
|
68 |
} /*** end of TimerInit ***/
|
69 |
|
70 |
|
71 |
/************************************************************************************//** |
72 |
** \brief Reset the timer by placing the timer back into it's default reset
|
73 |
** configuration.
|
74 |
** \return none.
|
75 |
**
|
76 |
****************************************************************************************/
|
77 |
void TimerReset(void) |
78 |
{ |
79 |
/* set the systick's status and control register back into the default reset value */
|
80 |
SysTick->CTRL = 0;
|
81 |
} /* end of TimerReset */
|
82 |
|
83 |
|
84 |
/************************************************************************************//** |
85 |
** \brief Updates the millisecond timer.
|
86 |
** \return none.
|
87 |
**
|
88 |
****************************************************************************************/
|
89 |
void TimerUpdate(void) |
90 |
{ |
91 |
/* check if the millisecond event occurred */
|
92 |
if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0) |
93 |
{ |
94 |
/* increment the millisecond counter */
|
95 |
millisecond_counter++; |
96 |
} |
97 |
} /*** end of TimerUpdate ***/
|
98 |
|
99 |
|
100 |
/************************************************************************************//** |
101 |
** \brief Obtains the counter value of the millisecond timer.
|
102 |
** \return Current value of the millisecond timer.
|
103 |
**
|
104 |
****************************************************************************************/
|
105 |
blt_int32u TimerGet(void)
|
106 |
{ |
107 |
/* updating timer here allows this function to be called in a loop with timeout
|
108 |
* detection.
|
109 |
*/
|
110 |
TimerUpdate(); |
111 |
/* read and return the amount of milliseconds that passed since initialization */
|
112 |
return millisecond_counter;
|
113 |
} /*** end of TimerGet ***/
|
114 |
|
115 |
|
116 |
/*********************************** end of timer.c ************************************/
|