2 * Copyright (c) 2014, Texas Instruments Incorporated
\r
3 * All rights reserved.
\r
5 * Redistribution and use in source and binary forms, with or without
\r
6 * modification, are permitted provided that the following conditions
\r
9 * * Redistributions of source code must retain the above copyright
\r
10 * notice, this list of conditions and the following disclaimer.
\r
12 * * Redistributions in binary form must reproduce the above copyright
\r
13 * notice, this list of conditions and the following disclaimer in the
\r
14 * documentation and/or other materials provided with the distribution.
\r
16 * * Neither the name of Texas Instruments Incorporated nor the names of
\r
17 * its contributors may be used to endorse or promote products derived
\r
18 * from this software without specific prior written permission.
\r
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
\r
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
\r
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
\r
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
\r
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
\r
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
\r
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
\r
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
32 //*****************************************************************************
\r
34 // timer_b.c - Driver for the timer_b Module.
\r
36 //*****************************************************************************
\r
38 //*****************************************************************************
\r
40 //! \addtogroup timer_b_api timer_b
\r
43 //*****************************************************************************
\r
45 #include "inc/hw_regaccess.h"
\r
46 #include "inc/hw_memmap.h"
\r
48 #ifdef __MSP430_HAS_TxB7__
\r
49 #include "timer_b.h"
\r
53 void Timer_B_startCounter(uint16_t baseAddress,
\r
56 HWREG16(baseAddress + OFS_TBxCTL) |= timerMode;
\r
59 void Timer_B_initContinuousMode(uint16_t baseAddress,
\r
60 Timer_B_initContinuousModeParam *param)
\r
62 HWREG16(baseAddress +
\r
63 OFS_TBxCTL) &= ~(TIMER_B_CLOCKSOURCE_INVERTED_EXTERNAL_TXCLK +
\r
64 TIMER_B_UPDOWN_MODE +
\r
66 TIMER_B_TBIE_INTERRUPT_ENABLE +
\r
70 HWREG16(baseAddress + OFS_TBxEX0) &= ~TBIDEX_7;
\r
72 HWREG16(baseAddress + OFS_TBxEX0) |= param->clockSourceDivider & 0x7;
\r
74 HWREG16(baseAddress + OFS_TBxCTL) |= (param->clockSource +
\r
76 param->timerInterruptEnable_TBIE +
\r
77 ((param->clockSourceDivider >>
\r
80 if(param->startTimer)
\r
82 HWREG16(baseAddress + OFS_TBxCTL) |= TIMER_B_CONTINUOUS_MODE;
\r
86 void Timer_B_initUpMode(uint16_t baseAddress,
\r
87 Timer_B_initUpModeParam *param)
\r
89 HWREG16(baseAddress + OFS_TBxCTL) &=
\r
90 ~(TIMER_B_CLOCKSOURCE_INVERTED_EXTERNAL_TXCLK +
\r
91 TIMER_B_UPDOWN_MODE +
\r
93 TIMER_B_TBIE_INTERRUPT_ENABLE +
\r
96 HWREG16(baseAddress + OFS_TBxEX0) &= ~TBIDEX_7;
\r
98 HWREG16(baseAddress + OFS_TBxEX0) |= param->clockSourceDivider & 0x7;
\r
100 HWREG16(baseAddress + OFS_TBxCTL) |= (param->clockSource +
\r
101 param->timerClear +
\r
102 param->timerInterruptEnable_TBIE +
\r
103 ((param->clockSourceDivider >>
\r
106 if(param->startTimer)
\r
108 HWREG16(baseAddress + OFS_TBxCTL) |= TIMER_B_UP_MODE;
\r
111 if(TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE ==
\r
112 param->captureCompareInterruptEnable_CCR0_CCIE)
\r
114 HWREG16(baseAddress +
\r
115 OFS_TBxCCTL0) |= TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE;
\r
119 HWREG16(baseAddress +
\r
120 OFS_TBxCCTL0) &= ~TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE;
\r
123 HWREG16(baseAddress + OFS_TBxCCR0) = param->timerPeriod;
\r
126 void Timer_B_initUpDownMode(uint16_t baseAddress,
\r
127 Timer_B_initUpDownModeParam *param)
\r
129 HWREG16(baseAddress + OFS_TBxCTL) &=
\r
130 ~(TIMER_B_CLOCKSOURCE_INVERTED_EXTERNAL_TXCLK +
\r
131 TIMER_B_UPDOWN_MODE +
\r
133 TIMER_B_TBIE_INTERRUPT_ENABLE +
\r
136 HWREG16(baseAddress + OFS_TBxEX0) &= ~TBIDEX_7;
\r
138 HWREG16(baseAddress + OFS_TBxEX0) |= param->clockSourceDivider & 0x7;
\r
140 HWREG16(baseAddress + OFS_TBxCTL) |= (param->clockSource +
\r
141 TIMER_B_STOP_MODE +
\r
142 param->timerClear +
\r
143 param->timerInterruptEnable_TBIE +
\r
144 ((param->clockSourceDivider >>
\r
147 if(param->startTimer)
\r
149 HWREG16(baseAddress + OFS_TBxCTL) |= TIMER_B_UPDOWN_MODE;
\r
152 if(TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE ==
\r
153 param->captureCompareInterruptEnable_CCR0_CCIE)
\r
155 HWREG16(baseAddress +
\r
156 OFS_TBxCCTL0) |= TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE;
\r
160 HWREG16(baseAddress +
\r
161 OFS_TBxCCTL0) &= ~TIMER_B_CCIE_CCR0_INTERRUPT_ENABLE;
\r
164 HWREG16(baseAddress + OFS_TBxCCR0) = param->timerPeriod;
\r
167 void Timer_B_initCaptureMode(uint16_t baseAddress,
\r
168 Timer_B_initCaptureModeParam *param)
\r
170 HWREG16(baseAddress + param->captureRegister) |= CAP;
\r
172 HWREG16(baseAddress + param->captureRegister) &=
\r
173 ~(TIMER_B_CAPTUREMODE_RISING_AND_FALLING_EDGE +
\r
174 TIMER_B_CAPTURE_INPUTSELECT_Vcc +
\r
175 TIMER_B_CAPTURE_SYNCHRONOUS +
\r
177 TIMER_B_TBIE_INTERRUPT_ENABLE +
\r
181 HWREG16(baseAddress + param->captureRegister) |= (param->captureMode +
\r
182 param->captureInputSelect
\r
185 synchronizeCaptureSource +
\r
187 captureInterruptEnable +
\r
188 param->captureOutputMode
\r
192 void Timer_B_initCompareMode(uint16_t baseAddress,
\r
193 Timer_B_initCompareModeParam *param)
\r
195 HWREG16(baseAddress + param->compareRegister) &= ~CAP;
\r
197 HWREG16(baseAddress + param->compareRegister) &=
\r
198 ~(TIMER_B_CAPTURECOMPARE_INTERRUPT_ENABLE +
\r
199 TIMER_B_OUTPUTMODE_RESET_SET
\r
202 HWREG16(baseAddress +
\r
203 param->compareRegister) |= (param->compareInterruptEnable +
\r
204 param->compareOutputMode
\r
207 HWREG16(baseAddress + param->compareRegister +
\r
208 OFS_TBxR) = param->compareValue;
\r
211 void Timer_B_enableInterrupt(uint16_t baseAddress)
\r
213 HWREG16(baseAddress + OFS_TBxCTL) |= TBIE;
\r
216 void Timer_B_disableInterrupt(uint16_t baseAddress)
\r
218 HWREG16(baseAddress + OFS_TBxCTL) &= ~TBIE;
\r
221 uint32_t Timer_B_getInterruptStatus(uint16_t baseAddress)
\r
223 return (HWREG16(baseAddress + OFS_TBxCTL) & TBIFG);
\r
226 void Timer_B_enableCaptureCompareInterrupt(uint16_t baseAddress,
\r
227 uint16_t captureCompareRegister)
\r
229 HWREG16(baseAddress + captureCompareRegister) |= CCIE;
\r
232 void Timer_B_disableCaptureCompareInterrupt(uint16_t baseAddress,
\r
233 uint16_t captureCompareRegister)
\r
235 HWREG16(baseAddress + captureCompareRegister) &= ~CCIE;
\r
238 uint32_t Timer_B_getCaptureCompareInterruptStatus(uint16_t baseAddress,
\r
239 uint16_t captureCompareRegister,
\r
242 return (HWREG16(baseAddress + captureCompareRegister) & mask);
\r
245 void Timer_B_clear(uint16_t baseAddress)
\r
247 HWREG16(baseAddress + OFS_TBxCTL) |= TBCLR;
\r
250 uint8_t Timer_B_getSynchronizedCaptureCompareInput(uint16_t baseAddress,
\r
251 uint16_t captureCompareRegister,
\r
252 uint16_t synchronized)
\r
254 if(HWREG16(baseAddress + captureCompareRegister) & synchronized)
\r
256 return (TIMER_B_CAPTURECOMPARE_INPUT_HIGH);
\r
260 return (TIMER_B_CAPTURECOMPARE_INPUT_LOW);
\r
264 uint8_t Timer_B_getOutputForOutputModeOutBitValue(uint16_t baseAddress,
\r
265 uint16_t captureCompareRegister)
\r
267 if(HWREG16(baseAddress + captureCompareRegister) & OUT)
\r
269 return (TIMER_B_OUTPUTMODE_OUTBITVALUE_HIGH);
\r
273 return (TIMER_B_OUTPUTMODE_OUTBITVALUE_LOW);
\r
277 uint16_t Timer_B_getCaptureCompareCount(uint16_t baseAddress,
\r
278 uint16_t captureCompareRegister)
\r
280 return (HWREG16(baseAddress + OFS_TBxR + captureCompareRegister));
\r
283 void Timer_B_setOutputForOutputModeOutBitValue(uint16_t baseAddress,
\r
284 uint16_t captureCompareRegister,
\r
285 uint8_t outputModeOutBitValue)
\r
287 HWREG16(baseAddress + captureCompareRegister) &= ~OUT;
\r
288 HWREG16(baseAddress + captureCompareRegister) |= outputModeOutBitValue;
\r
291 void Timer_B_outputPWM(uint16_t baseAddress,
\r
292 Timer_B_outputPWMParam *param)
\r
294 HWREG16(baseAddress + OFS_TBxCTL) &=
\r
295 ~(TIMER_B_CLOCKSOURCE_INVERTED_EXTERNAL_TXCLK +
\r
296 TIMER_B_UPDOWN_MODE + TIMER_B_DO_CLEAR +
\r
297 TIMER_B_TBIE_INTERRUPT_ENABLE
\r
299 HWREG16(baseAddress + OFS_TBxEX0) &= ~TBIDEX_7;
\r
301 HWREG16(baseAddress + OFS_TBxEX0) |= param->clockSourceDivider & 0x7;
\r
303 HWREG16(baseAddress + OFS_TBxCTL) |= (param->clockSource +
\r
306 ((param->clockSourceDivider >>
\r
309 HWREG16(baseAddress + OFS_TBxCCR0) = param->timerPeriod;
\r
311 HWREG16(baseAddress + OFS_TBxCCTL0) &=
\r
312 ~(TIMER_B_CAPTURECOMPARE_INTERRUPT_ENABLE +
\r
313 TIMER_B_OUTPUTMODE_RESET_SET
\r
316 HWREG16(baseAddress + param->compareRegister) |= param->compareOutputMode;
\r
318 HWREG16(baseAddress + param->compareRegister + OFS_TBxR) = param->dutyCycle;
\r
321 void Timer_B_stop(uint16_t baseAddress)
\r
323 HWREG16(baseAddress + OFS_TBxCTL) &= ~MC_3;
\r
326 void Timer_B_setCompareValue(uint16_t baseAddress,
\r
327 uint16_t compareRegister,
\r
328 uint16_t compareValue)
\r
330 HWREG16(baseAddress + compareRegister + OFS_TBxR) = compareValue;
\r
333 void Timer_B_clearTimerInterrupt(uint16_t baseAddress)
\r
335 HWREG16(baseAddress + OFS_TBxCTL) &= ~TBIFG;
\r
338 void Timer_B_clearCaptureCompareInterrupt(uint16_t baseAddress,
\r
339 uint16_t captureCompareRegister)
\r
341 HWREG16(baseAddress + captureCompareRegister) &= ~CCIFG;
\r
344 void Timer_B_selectCounterLength(uint16_t baseAddress,
\r
345 uint16_t counterLength)
\r
347 HWREG16(baseAddress + OFS_TBxCTL) &= ~CNTL_3;
\r
348 HWREG16(baseAddress + OFS_TBxCTL) |= counterLength;
\r
351 void Timer_B_selectLatchingGroup(uint16_t baseAddress,
\r
352 uint16_t groupLatch)
\r
354 HWREG16(baseAddress + OFS_TBxCTL) &= ~TBCLGRP_3;
\r
355 HWREG16(baseAddress + OFS_TBxCTL) |= groupLatch;
\r
358 void Timer_B_initCompareLatchLoadEvent(uint16_t baseAddress,
\r
359 uint16_t compareRegister,
\r
360 uint16_t compareLatchLoadEvent)
\r
362 HWREG16(baseAddress + compareRegister) &= ~CLLD_3;
\r
363 HWREG16(baseAddress + compareRegister) |= compareLatchLoadEvent;
\r
366 uint16_t Timer_B_getCounterValue(uint16_t baseAddress)
\r
368 uint16_t voteOne, voteTwo, res;
\r
370 voteTwo = HWREG16(baseAddress + OFS_TBxR);
\r
375 voteTwo = HWREG16(baseAddress + OFS_TBxR);
\r
377 if(voteTwo > voteOne)
\r
379 res = voteTwo - voteOne;
\r
381 else if(voteOne > voteTwo)
\r
383 res = voteOne - voteTwo;
\r
390 while(res > TIMER_B_THRESHOLD);
\r
396 //*****************************************************************************
\r
398 //! Close the doxygen group for timer_b_api
\r
401 //*****************************************************************************
\r