]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/driverlib/timer32.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / driverlib / timer32.c
1 /*
2  * -------------------------------------------
3  *    MSP432 DriverLib - v01_04_00_18 
4  * -------------------------------------------
5  *
6  * --COPYRIGHT--,BSD,BSD
7  * Copyright (c) 2015, 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         HWREGBIT8(timer + OFS_TIMER32_CONTROL1, TIMER32_CONTROL1_MODE_OFS) = 1;
47     else if (mode == TIMER32_FREE_RUN_MODE)
48         HWREGBIT8(timer + OFS_TIMER32_CONTROL1, TIMER32_CONTROL1_MODE_OFS) = 0;
49     else
50         ASSERT(false);
51
52     /* Setting the resolution of the timer */
53     if (resolution == TIMER32_1_MODULE6BIT)
54         HWREGBIT8(timer + OFS_TIMER32_CONTROL1, TIMER32_CONTROL1_SIZE_OFS) = 0;
55     else if (resolution == TIMER32_32BIT)
56         HWREGBIT8(timer + OFS_TIMER32_CONTROL1, TIMER32_CONTROL1_SIZE_OFS) = 1;
57     else
58         ASSERT(false);
59
60     /* Setting the PreScaler */
61     ASSERT(
62             resolution == TIMER32_PRESCALER_1
63             || resolution == TIMER32_PRESCALER_16
64             || resolution == TIMER32_PRESCALER_256);
65
66     HWREG32(timer + OFS_TIMER32_CONTROL1) =
67             (HWREG32(timer + OFS_TIMER32_CONTROL1)
68                     & ~TIMER32_CONTROL1_PRESCALE_M) | preScaler;
69
70 }
71
72 void Timer32_setCount(uint32_t timer, uint32_t count)
73 {
74     if (!HWREGBIT32(timer + OFS_TIMER32_CONTROL1,
75             TIMER32_CONTROL1_SIZE_OFS) && (count > UINT16_MAX))
76         HWREG32(timer + OFS_TIMER32_LOAD1) = UINT16_MAX;
77     else
78         HWREG32(timer + OFS_TIMER32_LOAD1) = count;
79 }
80
81 void Timer32_setCountInBackground(uint32_t timer, uint32_t count)
82 {
83     if (!HWREGBIT32(timer + OFS_TIMER32_CONTROL1,
84             TIMER32_CONTROL1_SIZE_OFS) && (count > UINT16_MAX))
85         HWREG32(timer + OFS_TIMER32_BGLOAD1) = UINT16_MAX;
86     else
87         HWREG32(timer + OFS_TIMER32_BGLOAD1) = count;
88 }
89
90 uint32_t Timer32_getValue(uint32_t timer)
91 {
92     return HWREG32(timer + OFS_TIMER32_VALUE1);
93 }
94
95 void Timer32_startTimer(uint32_t timer, bool oneShot)
96 {
97     ASSERT(timer == TIMER32_0_MODULE || timer == TIMER32_1_MODULE);
98
99     if (oneShot)
100         HWREGBIT8(timer + OFS_TIMER32_CONTROL1, TIMER32_CONTROL1_ONESHOT_OFS) =
101                 1;
102     else
103         HWREGBIT8(timer + OFS_TIMER32_CONTROL1, TIMER32_CONTROL1_ONESHOT_OFS) =
104                 0;
105
106     HWREG32(timer + OFS_TIMER32_CONTROL1) |= TIMER32_CONTROL1_ENABLE;
107 }
108
109 void Timer32_haltTimer(uint32_t timer)
110 {
111     ASSERT(timer == TIMER32_0_MODULE || timer == TIMER32_1_MODULE);
112
113     HWREG32(timer + OFS_TIMER32_CONTROL1) &= ~TIMER32_CONTROL1_ENABLE;
114 }
115
116 void Timer32_enableInterrupt(uint32_t timer)
117 {
118     HWREG32(timer + OFS_TIMER32_CONTROL1) |= TIMER32_CONTROL1_IE;
119 }
120
121 void Timer32_disableInterrupt(uint32_t timer)
122 {
123     HWREG32(timer + OFS_TIMER32_CONTROL1) &= ~TIMER32_CONTROL1_IE;
124 }
125
126 void Timer32_clearInterruptFlag(uint32_t timer)
127 {
128     HWREG32(timer + OFS_TIMER32_INTCLR1) |= 0x01;
129 }
130
131 uint32_t Timer32_getInterruptStatus(uint32_t timer)
132 {
133     return HWREG32(timer + OFS_TIMER32_MIS1);
134 }
135
136 void Timer32_registerInterrupt(uint32_t timerInterrupt,
137         void (*intHandler)(void))
138 {
139     Interrupt_registerInterrupt(timerInterrupt, intHandler);
140     Interrupt_enableInterrupt(timerInterrupt);
141 }
142
143 void Timer32_unregisterInterrupt(uint32_t timerInterrupt)
144 {
145     Interrupt_disableInterrupt(timerInterrupt);
146     Interrupt_unregisterInterrupt(timerInterrupt);
147 }
148