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