]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/driverlib/rtc_c.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / driverlib / rtc_c.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 <rtc_c.h>
38 #include <interrupt.h>
39 #include <debug.h>
40
41 void RTC_C_startClock(void)
42 {
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;
46 }
47
48 void RTC_C_holdClock(void)
49 {
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;
53 }
54
55 void RTC_C_setCalibrationFrequency(uint_fast16_t frequencySelect)
56 {
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;
60 }
61
62 void RTC_C_setCalibrationData(uint_fast8_t offsetDirection,
63         uint_fast8_t offsetValue)
64 {
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;
68 }
69
70 bool RTC_C_setTemperatureCompensation(uint_fast16_t offsetDirection,
71         uint_fast8_t offsetValue)
72 {
73     while (!BITBAND_PERI(RTC_C->rTCMP.r, RTCTCRDY_OFS))
74         ;
75
76     RTC_C->rTCMP.r = offsetValue + offsetDirection;
77
78     if (BITBAND_PERI(RTC_C->rTCMP.r, RTCTCOK_OFS))
79         return true;
80     else
81         return false;
82 }
83
84 void RTC_C_initCalendar(const RTC_C_Calendar *calendarTime,
85         uint_fast16_t formatSelect)
86 {
87     RTC_C->rCTL0.b.bKEY = RTCKEY_H;
88
89     BITBAND_PERI(RTC_C->rCTL13.r, RTCHOLD_OFS) = 1;
90
91     if (formatSelect)
92         BITBAND_PERI(RTC_C->rCTL13.r, RTCBCD_OFS) = 1;
93     else
94         BITBAND_PERI(RTC_C->rCTL13.r, RTCBCD_OFS) = 0;
95
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;
103
104     BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
105 }
106
107 RTC_C_Calendar RTC_C_getCalendarTime(void)
108 {
109     RTC_C_Calendar tempCal;
110
111     while (!(BITBAND_PERI(RTC_C->rCTL13.r, RTCRDY_OFS)))
112         ;
113
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;
121
122     return (tempCal);
123 }
124
125 void RTC_C_setCalendarAlarm(uint_fast8_t minutesAlarm, uint_fast8_t hoursAlarm,
126         uint_fast8_t dayOfWeekAlarm, uint_fast8_t dayOfmonthAlarm)
127 {
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);
134 }
135
136 void RTC_C_setCalendarEvent(uint_fast16_t eventSelect)
137 {
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;
141 }
142
143 void RTC_C_definePrescaleEvent(uint_fast8_t prescaleSelect,
144         uint_fast8_t prescaleEventDivider)
145 {
146     HWREG8(RTC_C_BASE + OFS_RTCPS0CTL + prescaleSelect) &= ~(RT0IP_7);
147     HWREG8(RTC_C_BASE + OFS_RTCPS0CTL + prescaleSelect) |=
148             prescaleEventDivider;
149 }
150
151 uint_fast8_t RTC_C_getPrescaleValue(uint_fast8_t prescaleSelect)
152 {
153     if (RTC_C_PRESCALE_0 == prescaleSelect)
154     {
155         return (RTC_C->rPS.b.bRT0PS);
156     } else if (RTC_C_PRESCALE_1 == prescaleSelect)
157     {
158         return (RTC_C->rPS.b.bRT1PS);
159     } else
160     {
161         return (0);
162     }
163 }
164
165 void RTC_C_setPrescaleValue(uint_fast8_t prescaleSelect,
166         uint_fast8_t prescaleCounterValue)
167 {
168     RTC_C->rCTL0.b.bKEY = RTCKEY_H;
169
170     if (RTC_C_PRESCALE_0 == prescaleSelect)
171     {
172         RTC_C->rPS.b.bRT0PS = prescaleCounterValue;
173     } else if (RTC_C_PRESCALE_1 == prescaleSelect)
174     {
175         RTC_C->rPS.b.bRT1PS = prescaleCounterValue;
176     }
177
178     BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
179 }
180
181 uint16_t RTC_C_convertBCDToBinary(uint16_t valueToConvert)
182 {
183     RTC_C->rBCD2BIN = valueToConvert;
184     return (RTC_C->rBCD2BIN);
185 }
186
187 uint16_t RTC_C_convertBinaryToBCD(uint16_t valueToConvert)
188 {
189     RTC_C->rBIN2BCD = valueToConvert;
190     return (RTC_C->rBIN2BCD);
191 }
192
193 void RTC_C_enableInterrupt(uint8_t interruptMask)
194 {
195     if (interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE))
196     {
197         RTC_C->rCTL0.r = RTCKEY | (interruptMask
198                 & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE));
199         BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
200     }
201
202     if (interruptMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
203     {
204         BITBAND_PERI(RTC_C->rPS0CTL.r,RT0PSIE_OFS) = 1;
205     }
206
207     if (interruptMask & RTC_C_PRESCALE_TIMER1_INTERRUPT)
208     {
209         BITBAND_PERI(RTC_C->rPS1CTL.r,RT1PSIE_OFS) = 1;
210     }
211 }
212
213 void RTC_C_disableInterrupt(uint8_t interruptMask)
214 {
215     if (interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE))
216     {
217         RTC_C->rCTL0.r = RTCKEY
218                 | (RTC_C->rCTL0.r
219                         & ~((interruptMask | RTCKEY_M)
220                                 & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE)));
221         BITBAND_PERI(RTC_C->rCTL0.r, RTCKEY_OFS) = 0;
222     }
223
224     if (interruptMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
225     {
226         BITBAND_PERI(RTC_C->rPS0CTL.r,RT0PSIE_OFS) = 0;
227     }
228
229     if (interruptMask & RTC_C_PRESCALE_TIMER1_INTERRUPT)
230     {
231         BITBAND_PERI(RTC_C->rPS1CTL.r,RT1PSIE_OFS) = 0;
232     }
233 }
234
235 uint_fast8_t RTC_C_getInterruptStatus(void)
236 {
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;
242
243     tempInterruptFlagMask |= (RTC_C->rCTL0.r & (interruptFlagMask >> 4));
244
245     tempInterruptFlagMask = tempInterruptFlagMask << 4;
246
247     if (interruptFlagMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
248     {
249         if (BITBAND_PERI(RTC_C->rPS0CTL.r, RT0PSIFG_OFS))
250         {
251             tempInterruptFlagMask |= RTC_C_PRESCALE_TIMER0_INTERRUPT;
252         }
253     }
254
255     if (interruptFlagMask & RTC_C_PRESCALE_TIMER1_INTERRUPT)
256     {
257         if (BITBAND_PERI(RTC_C->rPS1CTL.r, RT1PSIFG_OFS))
258         {
259             tempInterruptFlagMask |= RTC_C_PRESCALE_TIMER1_INTERRUPT;
260         }
261     }
262
263     return (tempInterruptFlagMask);
264 }
265
266 uint_fast8_t RTC_C_getEnabledInterruptStatus(void)
267 {
268
269     uint32_t intStatus = RTC_C_getInterruptStatus();
270
271     if (!BITBAND_PERI(RTC_C->rCTL0.r, RTCOFIE_OFS))
272     {
273         intStatus &= ~RTC_C_OSCILLATOR_FAULT_INTERRUPT;
274     }
275
276     if (!BITBAND_PERI(RTC_C->rCTL0.r, RTCTEVIE_OFS))
277     {
278         intStatus &= ~RTC_C_TIME_EVENT_INTERRUPT;
279     }
280
281     if (!BITBAND_PERI(RTC_C->rCTL0.r, RTCAIE_OFS))
282     {
283         intStatus &= ~RTC_C_CLOCK_ALARM_INTERRUPT;
284     }
285
286     if (!BITBAND_PERI(RTC_C->rCTL0.r, RTCRDYIE_OFS))
287     {
288         intStatus &= ~RTC_C_CLOCK_READ_READY_INTERRUPT;
289     }
290
291     if (!BITBAND_PERI(RTC_C->rPS0CTL, RT0PSIE_OFS))
292     {
293         intStatus &= ~RTC_C_PRESCALE_TIMER0_INTERRUPT;
294     }
295
296     if (!BITBAND_PERI(RTC_C->rPS1CTL.r, RT1PSIE_OFS))
297     {
298         intStatus &= ~RTC_C_PRESCALE_TIMER1_INTERRUPT;
299     }
300
301     return intStatus;
302 }
303
304 void RTC_C_clearInterruptFlag(uint_fast8_t interruptFlagMask)
305 {
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))
310     {
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;
314     }
315
316     if (interruptFlagMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
317     {
318         BITBAND_PERI(RTC_C->rPS0CTL.r,RT0PSIFG_OFS) = 0;
319     }
320
321     if (interruptFlagMask & RTC_C_PRESCALE_TIMER1_INTERRUPT)
322     {
323         BITBAND_PERI(RTC_C->rPS1CTL.r, RT1PSIFG_OFS) = 0;
324     }
325 }
326
327 void RTC_C_registerInterrupt(void (*intHandler)(void))
328 {
329     Interrupt_registerInterrupt(INT_RTC_C, intHandler);
330     Interrupt_enableInterrupt(INT_RTC_C);
331 }
332
333 void RTC_C_unregisterInterrupt(void)
334 {
335     Interrupt_disableInterrupt(INT_RTC_C);
336     Interrupt_unregisterInterrupt(INT_RTC_C);
337 }
338