]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/driverlib/timer32.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / driverlib / timer32.c
1 /*
2  * -------------------------------------------
3  *    MSP432 DriverLib - v3_10_00_09 
4  * -------------------------------------------
5  *
6  * --COPYRIGHT--,BSD,BSD
7  * Copyright (c) 2014, Texas Instruments Incorporated
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * *  Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
17  * *  Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * *  Neither the name of Texas Instruments Incorporated nor the names of
22  *    its contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  * --/COPYRIGHT--*/
37 #include <timer32.h>
38 #include <interrupt.h>
39 #include <debug.h>
40
41 void Timer32_initModule(uint32_t timer, uint32_t preScaler, uint32_t resolution,
42         uint32_t mode)
43 {
44     /* Setting up one shot or continuous mode */
45     if (mode == TIMER32_PERIODIC_MODE)
46         BITBAND_PERI(TIMER32_CMSIS(timer)->CONTROL, TIMER32_CONTROL_MODE_OFS)
47                     = 1;
48     else if (mode == TIMER32_FREE_RUN_MODE)
49         BITBAND_PERI(TIMER32_CMSIS(timer)->CONTROL, TIMER32_CONTROL_MODE_OFS)
50                     = 0;
51     else
52         ASSERT(false);
53
54     /* Setting the resolution of the timer */
55     if (resolution == TIMER32_1_MODULE6BIT)
56         BITBAND_PERI(TIMER32_CMSIS(timer)->CONTROL, TIMER32_CONTROL_SIZE_OFS)
57                             = 0;
58     else if (resolution == TIMER32_32BIT)
59         BITBAND_PERI(TIMER32_CMSIS(timer)->CONTROL, TIMER32_CONTROL_SIZE_OFS)
60                                         = 1;
61     else
62         ASSERT(false);
63
64     /* Setting the PreScaler */
65     ASSERT(
66             resolution == TIMER32_PRESCALER_1
67             || resolution == TIMER32_PRESCALER_16
68             || resolution == TIMER32_PRESCALER_256);
69
70     TIMER32_CMSIS(timer)->CONTROL = TIMER32_CMSIS(timer)->CONTROL
71                 & (~TIMER32_CONTROL_PRESCALE_MASK) | preScaler;
72
73 }
74
75 void Timer32_setCount(uint32_t timer, uint32_t count)
76 {
77     if (!BITBAND_PERI(TIMER32_CMSIS(timer)->CONTROL, TIMER32_CONTROL_SIZE_OFS)
78                 && (count > UINT16_MAX))
79         TIMER32_CMSIS(timer)->LOAD = UINT16_MAX;
80     else
81         TIMER32_CMSIS(timer)->LOAD = count;
82 }
83
84 void Timer32_setCountInBackground(uint32_t timer, uint32_t count)
85 {
86     if (!BITBAND_PERI(TIMER32_CMSIS(timer)->CONTROL, TIMER32_CONTROL_SIZE_OFS)
87                 && (count > UINT16_MAX))
88         TIMER32_CMSIS(timer)->BGLOAD = UINT16_MAX;
89     else
90         TIMER32_CMSIS(timer)->BGLOAD = count;
91 }
92
93 uint32_t Timer32_getValue(uint32_t timer)
94 {
95     return TIMER32_CMSIS(timer)->VALUE;
96 }
97
98 void Timer32_startTimer(uint32_t timer, bool oneShot)
99 {
100     ASSERT(timer == TIMER32_0_MODULE || timer == TIMER32_1_MODULE);
101
102     if (oneShot)
103         BITBAND_PERI(TIMER32_CMSIS(timer)->CONTROL, TIMER32_CONTROL_ONESHOT_OFS)
104                     = 1;
105     else
106         BITBAND_PERI(TIMER32_CMSIS(timer)->CONTROL, TIMER32_CONTROL_ONESHOT_OFS)
107                     = 0;
108
109     TIMER32_CMSIS(timer)->CONTROL |= TIMER32_CONTROL_ENABLE;
110 }
111
112 void Timer32_haltTimer(uint32_t timer)
113 {
114     ASSERT(timer == TIMER32_0_MODULE || timer == TIMER32_1_MODULE);
115
116     TIMER32_CMSIS(timer)->CONTROL &= ~TIMER32_CONTROL_ENABLE;
117 }
118
119 void Timer32_enableInterrupt(uint32_t timer)
120 {
121     TIMER32_CMSIS(timer)->CONTROL |= TIMER32_CONTROL_IE;
122 }
123
124 void Timer32_disableInterrupt(uint32_t timer)
125 {
126     TIMER32_CMSIS(timer)->CONTROL &= ~TIMER32_CONTROL_IE;
127 }
128
129 void Timer32_clearInterruptFlag(uint32_t timer)
130 {
131     TIMER32_CMSIS(timer)->INTCLR |= 0x01;
132 }
133
134 uint32_t Timer32_getInterruptStatus(uint32_t timer)
135 {
136     return TIMER32_CMSIS(timer)->MIS;
137 }
138
139 void Timer32_registerInterrupt(uint32_t timerInterrupt,
140         void (*intHandler)(void))
141 {
142     Interrupt_registerInterrupt(timerInterrupt, intHandler);
143     Interrupt_enableInterrupt(timerInterrupt);
144 }
145
146 void Timer32_unregisterInterrupt(uint32_t timerInterrupt)
147 {
148     Interrupt_disableInterrupt(timerInterrupt);
149     Interrupt_unregisterInterrupt(timerInterrupt);
150 }
151