2 * -------------------------------------------
3 * MSP432 DriverLib - v01_04_00_18
4 * -------------------------------------------
6 * --COPYRIGHT--,BSD,BSD
7 * Copyright (c) 2015, Texas Instruments Incorporated
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
38 #include <interrupt.h>
41 void RTC_C_startClock(void)
43 RTC_C->rCTL0.b.bKEY = RTCKEY_H;
44 BITBAND_PERI(RTC_C->rCTL13.r, RTCHOLD_OFS) = 0;
45 BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
48 void RTC_C_holdClock(void)
50 RTC_C->rCTL0.b.bKEY = RTCKEY_H;
51 BITBAND_PERI(RTC_C->rCTL13.r, RTCHOLD_OFS) = 1;
52 BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
55 void RTC_C_setCalibrationFrequency(uint_fast16_t frequencySelect)
57 RTC_C->rCTL0.b.bKEY = RTCKEY_H;
58 RTC_C->rCTL13.r = (RTC_C->rCTL13.r & ~(RTCCALF_3)) | frequencySelect;
59 BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
62 void RTC_C_setCalibrationData(uint_fast8_t offsetDirection,
63 uint_fast8_t offsetValue)
65 RTC_C->rCTL0.b.bKEY = RTCKEY_H;
66 RTC_C->rOCAL.r = offsetValue + offsetDirection;
67 BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
70 bool RTC_C_setTemperatureCompensation(uint_fast16_t offsetDirection,
71 uint_fast8_t offsetValue)
73 while (!BITBAND_PERI(RTC_C->rTCMP.r, RTCTCRDY_OFS))
76 RTC_C->rTCMP.r = offsetValue + offsetDirection;
78 if (BITBAND_PERI(RTC_C->rTCMP.r, RTCTCOK_OFS))
84 void RTC_C_initCalendar(const RTC_C_Calendar *calendarTime,
85 uint_fast16_t formatSelect)
87 RTC_C->rCTL0.b.bKEY = RTCKEY_H;
89 BITBAND_PERI(RTC_C->rCTL13.r, RTCHOLD_OFS) = 1;
92 BITBAND_PERI(RTC_C->rCTL13.r, RTCBCD_OFS) = 1;
94 BITBAND_PERI(RTC_C->rCTL13.r, RTCBCD_OFS) = 0;
96 RTC_C->rTIM0.b.bSEC = calendarTime->seconds;
97 RTC_C->rTIM0.b.bMIN = calendarTime->minutes;
98 RTC_C->rTIM1.b.bHOUR = calendarTime->hours;
99 RTC_C->rTIM1.b.bDOW = calendarTime->dayOfWeek;
100 RTC_C->rDATE.b.bDAY = calendarTime->dayOfmonth;
101 RTC_C->rDATE.b.bMON = calendarTime->month;
102 RTC_C->rYEAR.r = calendarTime->year;
104 BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
107 RTC_C_Calendar RTC_C_getCalendarTime(void)
109 RTC_C_Calendar tempCal;
111 while (!(BITBAND_PERI(RTC_C->rCTL13.r, RTCRDY_OFS)))
114 tempCal.seconds = RTC_C->rTIM0.b.bSEC;
115 tempCal.minutes = RTC_C->rTIM0.b.bMIN;
116 tempCal.hours = RTC_C->rTIM1.b.bHOUR;
117 tempCal.dayOfWeek = RTC_C->rTIM1.b.bDOW;
118 tempCal.dayOfmonth = RTC_C->rDATE.b.bDAY;
119 tempCal.month = RTC_C->rDATE.b.bMON;
120 tempCal.year = RTC_C->rYEAR.r;
125 void RTC_C_setCalendarAlarm(uint_fast8_t minutesAlarm, uint_fast8_t hoursAlarm,
126 uint_fast8_t dayOfWeekAlarm, uint_fast8_t dayOfmonthAlarm)
128 //Each of these is XORed with 0x80 to turn on if an integer is passed,
129 //or turn OFF if RTC_ALARM_OFF (0x80) is passed.
130 HWREG8(RTC_C_BASE + OFS_RTCAMINHR) = (minutesAlarm ^ 0x80);
131 HWREG8(RTC_C_BASE + OFS_RTCAMINHR + 1) = (hoursAlarm ^ 0x80);
132 HWREG8(RTC_C_BASE + OFS_RTCADOWDAY) = (dayOfWeekAlarm ^ 0x80);
133 HWREG8(RTC_C_BASE + OFS_RTCADOWDAY + 1) = (dayOfmonthAlarm ^ 0x80);
136 void RTC_C_setCalendarEvent(uint_fast16_t eventSelect)
138 RTC_C->rCTL0.b.bKEY = RTCKEY_H;
139 RTC_C->rCTL13.r = (RTC_C->rCTL13.r & ~(RTCTEV_3)) | eventSelect;
140 BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
143 void RTC_C_definePrescaleEvent(uint_fast8_t prescaleSelect,
144 uint_fast8_t prescaleEventDivider)
146 HWREG8(RTC_C_BASE + OFS_RTCPS0CTL + prescaleSelect) &= ~(RT0IP_7);
147 HWREG8(RTC_C_BASE + OFS_RTCPS0CTL + prescaleSelect) |=
148 prescaleEventDivider;
151 uint_fast8_t RTC_C_getPrescaleValue(uint_fast8_t prescaleSelect)
153 if (RTC_C_PRESCALE_0 == prescaleSelect)
155 return (RTC_C->rPS.b.bRT0PS);
156 } else if (RTC_C_PRESCALE_1 == prescaleSelect)
158 return (RTC_C->rPS.b.bRT1PS);
165 void RTC_C_setPrescaleValue(uint_fast8_t prescaleSelect,
166 uint_fast8_t prescaleCounterValue)
168 RTC_C->rCTL0.b.bKEY = RTCKEY_H;
170 if (RTC_C_PRESCALE_0 == prescaleSelect)
172 RTC_C->rPS.b.bRT0PS = prescaleCounterValue;
173 } else if (RTC_C_PRESCALE_1 == prescaleSelect)
175 RTC_C->rPS.b.bRT1PS = prescaleCounterValue;
178 BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
181 uint16_t RTC_C_convertBCDToBinary(uint16_t valueToConvert)
183 RTC_C->rBCD2BIN = valueToConvert;
184 return (RTC_C->rBCD2BIN);
187 uint16_t RTC_C_convertBinaryToBCD(uint16_t valueToConvert)
189 RTC_C->rBIN2BCD = valueToConvert;
190 return (RTC_C->rBIN2BCD);
193 void RTC_C_enableInterrupt(uint8_t interruptMask)
195 if (interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE))
197 RTC_C->rCTL0.r = RTCKEY | (interruptMask
198 & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE));
199 BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
202 if (interruptMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
204 BITBAND_PERI(RTC_C->rPS0CTL.r,RT0PSIE_OFS) = 1;
207 if (interruptMask & RTC_C_PRESCALE_TIMER1_INTERRUPT)
209 BITBAND_PERI(RTC_C->rPS1CTL.r,RT1PSIE_OFS) = 1;
213 void RTC_C_disableInterrupt(uint8_t interruptMask)
215 if (interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE))
217 RTC_C->rCTL0.r = RTCKEY
219 & ~((interruptMask | RTCKEY_M)
220 & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE)));
221 BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
224 if (interruptMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
226 BITBAND_PERI(RTC_C->rPS0CTL.r,RT0PSIE_OFS) = 0;
229 if (interruptMask & RTC_C_PRESCALE_TIMER1_INTERRUPT)
231 BITBAND_PERI(RTC_C->rPS1CTL.r,RT1PSIE_OFS) = 0;
235 uint_fast8_t RTC_C_getInterruptStatus(void)
237 uint_fast8_t tempInterruptFlagMask = 0x00;
238 uint_fast8_t interruptFlagMask = RTC_C_TIME_EVENT_INTERRUPT
239 | RTC_C_CLOCK_ALARM_INTERRUPT | RTC_C_CLOCK_READ_READY_INTERRUPT
240 | RTC_C_PRESCALE_TIMER0_INTERRUPT | RTC_C_PRESCALE_TIMER1_INTERRUPT
241 | RTC_C_OSCILLATOR_FAULT_INTERRUPT;
243 tempInterruptFlagMask |= (RTC_C->rCTL0.r & (interruptFlagMask >> 4));
245 tempInterruptFlagMask = tempInterruptFlagMask << 4;
247 if (interruptFlagMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
249 if (BITBAND_PERI(RTC_C->rPS0CTL.r, RT0PSIFG_OFS))
251 tempInterruptFlagMask |= RTC_C_PRESCALE_TIMER0_INTERRUPT;
255 if (interruptFlagMask & RTC_C_PRESCALE_TIMER1_INTERRUPT)
257 if (BITBAND_PERI(RTC_C->rPS1CTL.r, RT1PSIFG_OFS))
259 tempInterruptFlagMask |= RTC_C_PRESCALE_TIMER1_INTERRUPT;
263 return (tempInterruptFlagMask);
266 uint_fast8_t RTC_C_getEnabledInterruptStatus(void)
269 uint32_t intStatus = RTC_C_getInterruptStatus();
271 if (!BITBAND_PERI(RTC_C->rCTL0.r, RTCOFIE_OFS))
273 intStatus &= ~RTC_C_OSCILLATOR_FAULT_INTERRUPT;
276 if (!BITBAND_PERI(RTC_C->rCTL0.r, RTCTEVIE_OFS))
278 intStatus &= ~RTC_C_TIME_EVENT_INTERRUPT;
281 if (!BITBAND_PERI(RTC_C->rCTL0.r, RTCAIE_OFS))
283 intStatus &= ~RTC_C_CLOCK_ALARM_INTERRUPT;
286 if (!BITBAND_PERI(RTC_C->rCTL0.r, RTCRDYIE_OFS))
288 intStatus &= ~RTC_C_CLOCK_READ_READY_INTERRUPT;
291 if (!BITBAND_PERI(RTC_C->rPS0CTL, RT0PSIE_OFS))
293 intStatus &= ~RTC_C_PRESCALE_TIMER0_INTERRUPT;
296 if (!BITBAND_PERI(RTC_C->rPS1CTL.r, RT1PSIE_OFS))
298 intStatus &= ~RTC_C_PRESCALE_TIMER1_INTERRUPT;
304 void RTC_C_clearInterruptFlag(uint_fast8_t interruptFlagMask)
306 if (interruptFlagMask
307 & (RTC_C_TIME_EVENT_INTERRUPT + RTC_C_CLOCK_ALARM_INTERRUPT
308 + RTC_C_CLOCK_READ_READY_INTERRUPT
309 + RTC_C_OSCILLATOR_FAULT_INTERRUPT))
311 RTC_C->rCTL0.r = RTCKEY
312 | (RTC_C->rCTL0.r & ~((interruptFlagMask >> 4) | RTCKEY_M));
313 BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
316 if (interruptFlagMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
318 BITBAND_PERI(RTC_C->rPS0CTL.r,RT0PSIFG_OFS) = 0;
321 if (interruptFlagMask & RTC_C_PRESCALE_TIMER1_INTERRUPT)
323 BITBAND_PERI(RTC_C->rPS1CTL.r, RT1PSIFG_OFS) = 0;
327 void RTC_C_registerInterrupt(void (*intHandler)(void))
329 Interrupt_registerInterrupt(INT_RTC_C, intHandler);
330 Interrupt_enableInterrupt(INT_RTC_C);
333 void RTC_C_unregisterInterrupt(void)
335 Interrupt_disableInterrupt(INT_RTC_C);
336 Interrupt_unregisterInterrupt(INT_RTC_C);