Statistics
| Branch: | Tag: | Revision:

amiro-blt / Target / Source / ARMCM3_STM32 / timer.c @ f7d2c786

History | View | Annotate | Download (6.223 KB)

1 69661903 Thomas Schöpping
/************************************************************************************//**
2
* \file         Source\ARMCM3_STM32\timer.c
3
* \brief        Bootloader timer driver source file.
4
* \ingroup      Target_ARMCM3_STM32
5
* \internal
6
*----------------------------------------------------------------------------------------
7
*                          C O P Y R I G H T
8
*----------------------------------------------------------------------------------------
9
*   Copyright (c) 2011  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
39
40
/****************************************************************************************
41
* Type definitions
42
****************************************************************************************/
43
/** \brief Systick registers. */
44
typedef struct
45
{
46
  volatile blt_int32u CTRL;                   /**< SysTick Control and Status Register */
47
  volatile blt_int32u LOAD;                   /**< SysTick Reload Value Register       */
48
  volatile blt_int32u VAL;                    /**< SysTick Current Value Register      */
49
} tSysTickRegs;
50
51
52
/****************************************************************************************
53
* Macro definitions
54
****************************************************************************************/
55
/** \brief CLKSOURCE bit of the system tick. */
56
#define SYSTICK_BIT_CLKSOURCE    ((blt_int32u)0x00000004)
57
/** \brief ENABLE bit of the system tick. */
58
#define SYSTICK_BIT_ENABLE       ((blt_int32u)0x00000001)
59
/** \brief COUNTERFLAG bit of the system tick. */
60
#define SYSTICK_BIT_COUNTERFLAG  ((blt_int32u)0x00010000)
61
62
63
/****************************************************************************************
64
* Local data declarations
65
****************************************************************************************/
66
/** \brief Local variable for storing the number of milliseconds that have elapsed since
67
 *         startup.
68
 */
69
static blt_int32u millisecond_counter;
70
71
72
/****************************************************************************************
73
* Register definitions
74
****************************************************************************************/
75
/** \brief Macro to access the system tick registers. */
76
#define SYSTICK          ((tSysTickRegs *) (blt_int32u)0xE000E010)
77
78
79
/************************************************************************************//**
80
** \brief     Initializes the polling based millisecond timer driver.
81
** \return    none.
82
**
83
****************************************************************************************/
84
void TimerInit(void)
85
{
86
  /* reset the timer configuration */
87
  TimerReset();
88
  /* configure the systick frequency as a 1 ms event generator */
89
  SYSTICK->LOAD = BOOT_CPU_SYSTEM_SPEED_KHZ - 1;
90
  /* reset the current counter value */
91
  SYSTICK->VAL = 0;
92
  /* select core clock as source and enable the timer */
93
  SYSTICK->CTRL = SYSTICK_BIT_CLKSOURCE | SYSTICK_BIT_ENABLE;
94
  /* reset the millisecond counter value */
95
  millisecond_counter = 0;
96
} /*** end of TimerInit ***/
97
98
99
/************************************************************************************//**
100
** \brief     Reset the timer by placing the timer back into it's default reset
101
**            configuration.
102
** \return    none.
103
**
104
****************************************************************************************/
105
void TimerReset(void)
106
{
107
  /* set the systick's status and control register back into the default reset value */
108
  SYSTICK->CTRL = 0;
109
} /* end of TimerReset */
110
111
112
/************************************************************************************//**
113
** \brief     Updates the millisecond timer.
114
** \return    none.
115
**
116
****************************************************************************************/
117
void TimerUpdate(void)
118
{
119
  /* check if the millisecond event occurred */
120
  if ((SYSTICK->CTRL & SYSTICK_BIT_COUNTERFLAG) != 0)
121
  {
122
    /* increment the millisecond counter */
123
    millisecond_counter++;
124
  }
125
} /*** end of TimerUpdate ***/
126
127
128
/************************************************************************************//**
129
** \brief     Obtains the counter value of the millisecond timer.
130
** \return    Current value of the millisecond timer.
131
**
132
****************************************************************************************/
133
blt_int32u TimerGet(void)
134
{
135
  /* updating timer here allows this function to be called in a loop with timeout
136
   * detection.
137
   */
138
  TimerUpdate();
139
  /* read and return the amount of milliseconds that passed since initialization */
140
  return millisecond_counter;
141
} /*** end of TimerGet ***/
142
143
144
/*********************************** end of timer.c ************************************/