]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/driverlib/rtc_c.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / driverlib / rtc_c.c
1 /*
2  * -------------------------------------------
3  *    MSP432 DriverLib - v3_10_00_09 
4  * -------------------------------------------
5  *
6  * --COPYRIGHT--,BSD,BSD
7  * Copyright (c) 2014, 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 #include <hw_memmap.h>
41
42 void RTC_C_startClock(void)
43 {
44         RTC_C->CTL0 = (RTC_C->CTL0 & ~RTC_C_CTL0_KEY_MASK) | RTC_C_KEY;
45     BITBAND_PERI(RTC_C->CTL13, RTC_C_CTL13_HOLD_OFS) = 0;
46     BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_KEY_OFS) = 0;
47 }
48
49 void RTC_C_holdClock(void)
50 {
51         RTC_C->CTL0 = (RTC_C->CTL0 & ~RTC_C_CTL0_KEY_MASK) | RTC_C_KEY;
52     BITBAND_PERI(RTC_C->CTL13, RTC_C_CTL13_HOLD_OFS) = 1;
53     BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_KEY_OFS) = 0;
54 }
55
56 void RTC_C_setCalibrationFrequency(uint_fast16_t frequencySelect)
57 {
58         RTC_C->CTL0 = (RTC_C->CTL0 & ~RTC_C_CTL0_KEY_MASK) | RTC_C_KEY;
59     RTC_C->CTL13 = (RTC_C->CTL13 & ~(RTC_C_CTL13_CALF_3)) | frequencySelect;
60     BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_KEY_OFS) = 0;
61 }
62
63 void RTC_C_setCalibrationData(uint_fast8_t offsetDirection,
64         uint_fast8_t offsetValue)
65 {
66         RTC_C->CTL0 = (RTC_C->CTL0 & ~RTC_C_CTL0_KEY_MASK) | RTC_C_KEY;
67     RTC_C->OCAL = offsetValue + offsetDirection;
68     BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_KEY_OFS) = 0;
69 }
70
71 bool RTC_C_setTemperatureCompensation(uint_fast16_t offsetDirection,
72         uint_fast8_t offsetValue)
73 {
74     while (!BITBAND_PERI(RTC_C->TCMP, RTC_C_TCMP_TCRDY_OFS))
75         ;
76
77     RTC_C->TCMP = offsetValue + offsetDirection;
78
79     if (BITBAND_PERI(RTC_C->TCMP, RTC_C_TCMP_TCOK_OFS))
80         return true;
81     else
82         return false;
83 }
84
85 void RTC_C_initCalendar(const RTC_C_Calendar *calendarTime,
86         uint_fast16_t formatSelect)
87 {
88         RTC_C->CTL0 = (RTC_C->CTL0 & ~RTC_C_CTL0_KEY_MASK) | RTC_C_KEY;
89
90     BITBAND_PERI(RTC_C->CTL13, RTC_C_CTL13_HOLD_OFS) = 1;
91
92     if (formatSelect)
93         BITBAND_PERI(RTC_C->CTL13, RTC_C_CTL13_BCD_OFS) = 1;
94     else
95         BITBAND_PERI(RTC_C->CTL13, RTC_C_CTL13_BCD_OFS) = 0;
96
97     RTC_C->TIM0 = (calendarTime->minutes<<RTC_C_TIM0_MIN_OFS) | calendarTime->seconds;
98     RTC_C->TIM1 = (calendarTime->dayOfWeek<<RTC_C_TIM1_DOW_OFS) | calendarTime->hours;
99     RTC_C->DATE = (calendarTime->month<<RTC_C_DATE_MON_OFS) | calendarTime->dayOfmonth;
100     RTC_C->YEAR = calendarTime->year;
101
102     BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_KEY_OFS) = 0;
103 }
104
105 RTC_C_Calendar RTC_C_getCalendarTime(void)
106 {
107     RTC_C_Calendar tempCal;
108
109     while (!(BITBAND_PERI(RTC_C->CTL13, RTC_C_CTL13_RDY_OFS)))
110         ;
111
112     tempCal.seconds = RTC_C->TIM0 & RTC_C_TIM0_SEC_MASK;
113     tempCal.minutes = (RTC_C->TIM0 & RTC_C_TIM0_MIN_MASK)>>RTC_C_TIM0_MIN_OFS;
114     tempCal.hours   = RTC_C->TIM1 & RTC_C_TIM1_HOUR_MASK;
115     tempCal.dayOfWeek   = (RTC_C->TIM1 & RTC_C_TIM1_DOW_MASK)>>RTC_C_TIM1_DOW_OFS;
116     tempCal.dayOfmonth = RTC_C->DATE & RTC_C_DATE_DAY_MASK;
117     tempCal.month = (RTC_C->DATE & RTC_C_DATE_MON_MASK)>>RTC_C_DATE_MON_OFS;
118     tempCal.year = RTC_C->YEAR;
119
120     return (tempCal);
121 }
122
123 void RTC_C_setCalendarAlarm(uint_fast8_t minutesAlarm, uint_fast8_t hoursAlarm,
124         uint_fast8_t dayOfWeekAlarm, uint_fast8_t dayOfmonthAlarm)
125 {
126     //Each of these is XORed with 0x80 to turn on if an integer is passed,
127     //or turn OFF if RTC_ALARM_OFF (0x80) is passed.
128     RTC_C->AMINHR = ((hoursAlarm ^ 0x80) << 8 )| (minutesAlarm ^ 0x80);
129     RTC_C->ADOWDAY = ((dayOfmonthAlarm ^ 0x80) << 8 )| (dayOfWeekAlarm ^ 0x80);
130 }
131
132 void RTC_C_setCalendarEvent(uint_fast16_t eventSelect)
133 {
134         RTC_C->CTL0 = (RTC_C->CTL0 & ~RTC_C_CTL0_KEY_MASK) | RTC_C_KEY;
135     RTC_C->CTL13 = (RTC_C->CTL13 & ~(RTC_C_CTL13_TEV_3)) | eventSelect;
136     BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_KEY_OFS) = 0;
137 }
138
139 void RTC_C_definePrescaleEvent(uint_fast8_t prescaleSelect,
140         uint_fast8_t prescaleEventDivider)
141 {
142     HWREG8(&RTC_C->PS0CTL + prescaleSelect) &= ~(RTC_C_PS0CTL_RT0IP_7);
143     HWREG8(&RTC_C->PS0CTL + prescaleSelect) |=
144             prescaleEventDivider;
145 }
146
147 uint_fast8_t RTC_C_getPrescaleValue(uint_fast8_t prescaleSelect)
148 {
149     if (RTC_C_PRESCALE_0 == prescaleSelect)
150     {
151         return (RTC_C->PS & RTC_C_PS_RT0PS_MASK);
152     } else if (RTC_C_PRESCALE_1 == prescaleSelect)
153     {
154         return (RTC_C->PS & RTC_C_PS_RT1PS_MASK)>>RTC_C_PS_RT1PS_OFS;
155     } else
156     {
157         return (0);
158     }
159 }
160
161 void RTC_C_setPrescaleValue(uint_fast8_t prescaleSelect,
162         uint_fast8_t prescaleCounterValue)
163 {
164         RTC_C->CTL0 = (RTC_C->CTL0 & ~RTC_C_CTL0_KEY_MASK) | RTC_C_KEY;
165
166     if (RTC_C_PRESCALE_0 == prescaleSelect)
167     {
168         RTC_C->PS = (RTC_C->PS & ~RTC_C_PS_RT0PS_MASK) | prescaleCounterValue;
169     } else if (RTC_C_PRESCALE_1 == prescaleSelect)
170     {
171         RTC_C->PS = (RTC_C->PS & ~RTC_C_PS_RT1PS_MASK)
172                         | (prescaleCounterValue << RTC_C_PS_RT1PS_OFS);
173     }
174
175     BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_KEY_OFS) = 0;
176 }
177
178 uint16_t RTC_C_convertBCDToBinary(uint16_t valueToConvert)
179 {
180     RTC_C->BCD2BIN = valueToConvert;
181     return (RTC_C->BCD2BIN);
182 }
183
184 uint16_t RTC_C_convertBinaryToBCD(uint16_t valueToConvert)
185 {
186     RTC_C->BIN2BCD = valueToConvert;
187     return (RTC_C->BIN2BCD);
188 }
189
190 void RTC_C_enableInterrupt(uint8_t interruptMask)
191 {
192     if (interruptMask & (RTC_C_CTL0_OFIE + RTC_C_CTL0_TEVIE + RTC_C_CTL0_AIE
193                 + RTC_C_CTL0_RDYIE))
194     {
195         RTC_C->CTL0 = RTC_C_KEY | (interruptMask
196                 & (RTC_C_CTL0_OFIE + RTC_C_CTL0_TEVIE + RTC_C_CTL0_AIE
197                 + RTC_C_CTL0_RDYIE));
198         BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_KEY_OFS) = 0;
199     }
200
201     if (interruptMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
202     {
203         BITBAND_PERI(RTC_C->PS0CTL, RTC_C_PS0CTL_RT0PSIE_OFS) = 1;
204     }
205
206     if (interruptMask & RTC_C_PRESCALE_TIMER1_INTERRUPT)
207     {
208         BITBAND_PERI(RTC_C->PS1CTL,RTC_C_PS1CTL_RT1PSIE_OFS) = 1;
209     }
210 }
211
212 void RTC_C_disableInterrupt(uint8_t interruptMask)
213 {
214     if (interruptMask & (RTC_C_CTL0_OFIE + RTC_C_CTL0_TEVIE + RTC_C_CTL0_AIE
215                 + RTC_C_CTL0_RDYIE))
216     {
217         RTC_C->CTL0 = RTC_C_KEY
218                 | (RTC_C->CTL0 & ~((interruptMask | RTC_C_CTL0_KEY_MASK)
219                 & (RTC_C_CTL0_OFIE + RTC_C_CTL0_TEVIE + RTC_C_CTL0_AIE
220                 + RTC_C_CTL0_RDYIE)));
221         BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_KEY_OFS) = 0;
222     }
223
224     if (interruptMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
225     {
226         BITBAND_PERI(RTC_C->PS0CTL, RTC_C_PS0CTL_RT0PSIE_OFS) = 0;
227     }
228
229     if (interruptMask & RTC_C_PRESCALE_TIMER1_INTERRUPT)
230     {
231         BITBAND_PERI(RTC_C->PS1CTL, RTC_C_PS1CTL_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->CTL0 & (interruptFlagMask >> 4));
244
245     tempInterruptFlagMask = tempInterruptFlagMask << 4;
246
247     if (interruptFlagMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
248     {
249         if (BITBAND_PERI(RTC_C->PS0CTL, RTC_C_PS0CTL_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->PS1CTL, RTC_C_PS1CTL_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->CTL0, RTC_C_CTL0_OFIE_OFS))
272     {
273         intStatus &= ~RTC_C_OSCILLATOR_FAULT_INTERRUPT;
274     }
275
276     if (!BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_TEVIE_OFS))
277     {
278         intStatus &= ~RTC_C_TIME_EVENT_INTERRUPT;
279     }
280
281     if (!BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_AIE_OFS))
282     {
283         intStatus &= ~RTC_C_CLOCK_ALARM_INTERRUPT;
284     }
285
286     if (!BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_RDYIE_OFS))
287     {
288         intStatus &= ~RTC_C_CLOCK_READ_READY_INTERRUPT;
289     }
290
291     if (!BITBAND_PERI(RTC_C->PS0CTL, RTC_C_PS0CTL_RT0PSIE_OFS))
292     {
293         intStatus &= ~RTC_C_PRESCALE_TIMER0_INTERRUPT;
294     }
295
296     if (!BITBAND_PERI(RTC_C->PS1CTL, RTC_C_PS1CTL_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->CTL0 = RTC_C_KEY
312                 | (RTC_C->CTL0 & ~((interruptFlagMask >> 4) | RTC_C_CTL0_KEY_MASK));
313         BITBAND_PERI(RTC_C->CTL0, RTC_C_CTL0_KEY_OFS) = 0;
314     }
315
316     if (interruptFlagMask & RTC_C_PRESCALE_TIMER0_INTERRUPT)
317     {
318         BITBAND_PERI(RTC_C->PS0CTL, RTC_C_PS0CTL_RT0PSIFG_OFS) = 0;
319     }
320
321     if (interruptFlagMask & RTC_C_PRESCALE_TIMER1_INTERRUPT)
322     {
323         BITBAND_PERI(RTC_C->PS1CTL, RTC_C_PS1CTL_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