]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MSP430FR5969_LaunchPad/driverlib/MSP430FR5xx_6xx/rtc_b.c
e99af60d8e41e3d60b9fe6b62f85d4d382a2c780
[freertos] / FreeRTOS / Demo / MSP430FR5969_LaunchPad / driverlib / MSP430FR5xx_6xx / rtc_b.c
1 /* --COPYRIGHT--,BSD\r
2  * Copyright (c) 2014, Texas Instruments Incorporated\r
3  * All rights reserved.\r
4  *\r
5  * Redistribution and use in source and binary forms, with or without\r
6  * modification, are permitted provided that the following conditions\r
7  * are met:\r
8  *\r
9  * *  Redistributions of source code must retain the above copyright\r
10  *    notice, this list of conditions and the following disclaimer.\r
11  *\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
15  *\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
19  *\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
31  * --/COPYRIGHT--*/\r
32 //*****************************************************************************\r
33 //\r
34 // rtc_b.c - Driver for the rtc_b Module.\r
35 //\r
36 //*****************************************************************************\r
37 \r
38 //*****************************************************************************\r
39 //\r
40 //! \addtogroup rtc_b_api rtc_b\r
41 //! @{\r
42 //\r
43 //*****************************************************************************\r
44 \r
45 #include "inc/hw_regaccess.h"\r
46 #include "inc/hw_memmap.h"\r
47 \r
48 #ifdef __MSP430_HAS_RTC_B__\r
49 #include "rtc_b.h"\r
50 \r
51 #include <assert.h>\r
52 \r
53 void RTC_B_startClock(uint16_t baseAddress)\r
54 {\r
55     HWREG8(baseAddress + OFS_RTCCTL01_H) &= ~(RTCHOLD_H);\r
56 }\r
57 \r
58 void RTC_B_holdClock(uint16_t baseAddress)\r
59 {\r
60     HWREG8(baseAddress + OFS_RTCCTL01_H) |= RTCHOLD_H;\r
61 }\r
62 \r
63 void RTC_B_setCalibrationFrequency(uint16_t baseAddress,\r
64                                    uint16_t frequencySelect)\r
65 {\r
66     HWREG16(baseAddress + OFS_RTCCTL23) &= ~(RTCCALF_3);\r
67     HWREG16(baseAddress + OFS_RTCCTL23) |= frequencySelect;\r
68 }\r
69 \r
70 void RTC_B_setCalibrationData(uint16_t baseAddress,\r
71                               uint8_t offsetDirection,\r
72                               uint8_t offsetValue)\r
73 {\r
74     HWREG8(baseAddress + OFS_RTCCTL23_L) = offsetValue + offsetDirection;\r
75 }\r
76 \r
77 void RTC_B_initCalendar(uint16_t baseAddress,\r
78                         Calendar *CalendarTime,\r
79                         uint16_t formatSelect)\r
80 {\r
81     HWREG8(baseAddress + OFS_RTCCTL01_H) |= RTCHOLD_H;\r
82 \r
83     HWREG16(baseAddress + OFS_RTCCTL01) &= ~(RTCBCD);\r
84     HWREG16(baseAddress + OFS_RTCCTL01) |= formatSelect;\r
85 \r
86     HWREG8(baseAddress + OFS_RTCTIM0_L) = CalendarTime->Seconds;\r
87     HWREG8(baseAddress + OFS_RTCTIM0_H) = CalendarTime->Minutes;\r
88     HWREG8(baseAddress + OFS_RTCTIM1_L) = CalendarTime->Hours;\r
89     HWREG8(baseAddress + OFS_RTCTIM1_H) = CalendarTime->DayOfWeek;\r
90     HWREG8(baseAddress + OFS_RTCDATE_L) = CalendarTime->DayOfMonth;\r
91     HWREG8(baseAddress + OFS_RTCDATE_H) = CalendarTime->Month;\r
92     HWREG16(baseAddress + OFS_RTCYEAR) = CalendarTime->Year;\r
93 }\r
94 \r
95 Calendar RTC_B_getCalendarTime(uint16_t baseAddress)\r
96 {\r
97     Calendar tempCal;\r
98 \r
99     while(!(HWREG16(baseAddress + OFS_RTCCTL01) & RTCRDY))\r
100     {\r
101         ;\r
102     }\r
103 \r
104     tempCal.Seconds = HWREG8(baseAddress + OFS_RTCTIM0_L);\r
105     tempCal.Minutes = HWREG8(baseAddress + OFS_RTCTIM0_H);\r
106     tempCal.Hours = HWREG8(baseAddress + OFS_RTCTIM1_L);\r
107     tempCal.DayOfWeek = HWREG8(baseAddress + OFS_RTCTIM1_H);\r
108     tempCal.DayOfMonth = HWREG8(baseAddress + OFS_RTCDATE_L);\r
109     tempCal.Month = HWREG8(baseAddress + OFS_RTCDATE_H);\r
110     tempCal.Year = HWREG16(baseAddress + OFS_RTCYEAR);\r
111 \r
112     return (tempCal);\r
113 }\r
114 \r
115 void RTC_B_configureCalendarAlarm(uint16_t baseAddress,\r
116                                   RTC_B_configureCalendarAlarmParam *param)\r
117 {\r
118     //Each of these is XORed with 0x80 to turn on if an integer is passed,\r
119     //or turn OFF if RTC_B_ALARM_OFF (0x80) is passed.\r
120     HWREG8(baseAddress + OFS_RTCAMINHR_L) = (param->minutesAlarm ^ 0x80);\r
121     HWREG8(baseAddress + OFS_RTCAMINHR_H) = (param->hoursAlarm ^ 0x80);\r
122     HWREG8(baseAddress + OFS_RTCADOWDAY_L) = (param->dayOfWeekAlarm ^ 0x80);\r
123     HWREG8(baseAddress + OFS_RTCADOWDAY_H) = (param->dayOfMonthAlarm ^ 0x80);\r
124 }\r
125 \r
126 void RTC_B_setCalendarEvent(uint16_t baseAddress,\r
127                             uint16_t eventSelect)\r
128 {\r
129     HWREG16(baseAddress + OFS_RTCCTL01) &= ~(RTCTEV_3); //Reset bits\r
130     HWREG16(baseAddress + OFS_RTCCTL01) |= eventSelect;\r
131 }\r
132 \r
133 void RTC_B_definePrescaleEvent(uint16_t baseAddress,\r
134                                uint8_t prescaleSelect,\r
135                                uint8_t prescaleEventDivider)\r
136 {\r
137     HWREG8(baseAddress + OFS_RTCPS0CTL_L + prescaleSelect) &= ~(RT0IP_7);\r
138     HWREG8(baseAddress + OFS_RTCPS0CTL_L +\r
139            prescaleSelect) |= prescaleEventDivider;\r
140 }\r
141 \r
142 uint8_t RTC_B_getPrescaleValue(uint16_t baseAddress,\r
143                                uint8_t prescaleSelect)\r
144 {\r
145     if(RTC_B_PRESCALE_0 == prescaleSelect)\r
146     {\r
147         return (HWREG8(baseAddress + OFS_RTCPS_L));\r
148     }\r
149     else if(RTC_B_PRESCALE_1 == prescaleSelect)\r
150     {\r
151         return (HWREG8(baseAddress + OFS_RTCPS_H));\r
152     }\r
153     else\r
154     {\r
155         return (0);\r
156     }\r
157 }\r
158 \r
159 void RTC_B_setPrescaleValue(uint16_t baseAddress,\r
160                             uint8_t prescaleSelect,\r
161                             uint8_t prescaleCounterValue)\r
162 {\r
163     if(RTC_B_PRESCALE_0 == prescaleSelect)\r
164     {\r
165         HWREG8(baseAddress + OFS_RTCPS_L) = prescaleCounterValue;\r
166     }\r
167     else if(RTC_B_PRESCALE_1 == prescaleSelect)\r
168     {\r
169         HWREG8(baseAddress + OFS_RTCPS_H) = prescaleCounterValue;\r
170     }\r
171 }\r
172 \r
173 void RTC_B_enableInterrupt(uint16_t baseAddress,\r
174                            uint8_t interruptMask)\r
175 {\r
176     if(interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE))\r
177     {\r
178         HWREG8(baseAddress + OFS_RTCCTL01_L) |=\r
179             (interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE));\r
180     }\r
181 \r
182     if(interruptMask & RTC_B_PRESCALE_TIMER0_INTERRUPT)\r
183     {\r
184         HWREG8(baseAddress + OFS_RTCPS0CTL) |= RT0PSIE;\r
185     }\r
186 \r
187     if(interruptMask & RTC_B_PRESCALE_TIMER1_INTERRUPT)\r
188     {\r
189         HWREG8(baseAddress + OFS_RTCPS1CTL) |= RT1PSIE;\r
190     }\r
191 }\r
192 \r
193 void RTC_B_disableInterrupt(uint16_t baseAddress,\r
194                             uint8_t interruptMask)\r
195 {\r
196     if(interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE))\r
197     {\r
198         HWREG8(baseAddress + OFS_RTCCTL01_L) &=\r
199             ~(interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE));\r
200     }\r
201 \r
202     if(interruptMask & RTC_B_PRESCALE_TIMER0_INTERRUPT)\r
203     {\r
204         HWREG8(baseAddress + OFS_RTCPS0CTL) &= ~(RT0PSIE);\r
205     }\r
206 \r
207     if(interruptMask & RTC_B_PRESCALE_TIMER1_INTERRUPT)\r
208     {\r
209         HWREG8(baseAddress + OFS_RTCPS1CTL) &= ~(RT1PSIE);\r
210     }\r
211 }\r
212 \r
213 uint8_t RTC_B_getInterruptStatus(uint16_t baseAddress,\r
214                                  uint8_t interruptFlagMask)\r
215 {\r
216     uint8_t tempInterruptFlagMask = 0x0000;\r
217 \r
218     tempInterruptFlagMask |= (HWREG8(baseAddress + OFS_RTCCTL01_L)\r
219                               & ((interruptFlagMask >> 4)\r
220                                  & (RTCOFIFG +\r
221                                     RTCTEVIFG +\r
222                                     RTCAIFG +\r
223                                     RTCRDYIFG)));\r
224 \r
225     tempInterruptFlagMask = tempInterruptFlagMask << 4;\r
226 \r
227     if(interruptFlagMask & RTC_B_PRESCALE_TIMER0_INTERRUPT)\r
228     {\r
229         if(HWREG8(baseAddress + OFS_RTCPS0CTL) & RT0PSIFG)\r
230         {\r
231             tempInterruptFlagMask |= RTC_B_PRESCALE_TIMER0_INTERRUPT;\r
232         }\r
233     }\r
234 \r
235     if(interruptFlagMask & RTC_B_PRESCALE_TIMER1_INTERRUPT)\r
236     {\r
237         if(HWREG8(baseAddress + OFS_RTCPS1CTL) & RT1PSIFG)\r
238         {\r
239             tempInterruptFlagMask |= RTC_B_PRESCALE_TIMER1_INTERRUPT;\r
240         }\r
241     }\r
242 \r
243     return (tempInterruptFlagMask);\r
244 }\r
245 \r
246 void RTC_B_clearInterrupt(uint16_t baseAddress,\r
247                           uint8_t interruptFlagMask)\r
248 {\r
249     if(interruptFlagMask & (RTC_B_TIME_EVENT_INTERRUPT +\r
250                             RTC_B_CLOCK_ALARM_INTERRUPT +\r
251                             RTC_B_CLOCK_READ_READY_INTERRUPT +\r
252                             RTC_B_OSCILLATOR_FAULT_INTERRUPT))\r
253     {\r
254         HWREG8(baseAddress + OFS_RTCCTL01_L) &=\r
255             ~((interruptFlagMask >> 4) & (RTCOFIFG +\r
256                                           RTCTEVIFG +\r
257                                           RTCAIFG +\r
258                                           RTCRDYIFG));\r
259     }\r
260 \r
261     if(interruptFlagMask & RTC_B_PRESCALE_TIMER0_INTERRUPT)\r
262     {\r
263         HWREG8(baseAddress + OFS_RTCPS0CTL) &= ~(RT0PSIFG);\r
264     }\r
265 \r
266     if(interruptFlagMask & RTC_B_PRESCALE_TIMER1_INTERRUPT)\r
267     {\r
268         HWREG8(baseAddress + OFS_RTCPS1CTL) &= ~(RT1PSIFG);\r
269     }\r
270 }\r
271 \r
272 uint16_t RTC_B_convertBCDToBinary(uint16_t baseAddress,\r
273                                   uint16_t valueToConvert)\r
274 {\r
275     HWREG16(baseAddress + OFS_BCD2BIN) = valueToConvert;\r
276     return (HWREG16(baseAddress + OFS_BCD2BIN));\r
277 }\r
278 \r
279 uint16_t RTC_B_convertBinaryToBCD(uint16_t baseAddress,\r
280                                   uint16_t valueToConvert)\r
281 {\r
282     HWREG16(baseAddress + OFS_BIN2BCD) = valueToConvert;\r
283     return (HWREG16(baseAddress + OFS_BIN2BCD));\r
284 }\r
285 \r
286 #endif\r
287 //*****************************************************************************\r
288 //\r
289 //! Close the doxygen group for rtc_b_api\r
290 //! @}\r
291 //\r
292 //*****************************************************************************\r