2020-2021 Sunseeker Telemetry and Lighting System
rtc_c.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // rtc_c.c - Driver for the rtc_c Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #if defined(__MSP430_HAS_RTC_C__) || defined(__MSP430_HAS_RTC_CE__)
17 #include "rtc_c.h"
18 
19 #include <assert.h>
20 
21 void RTC_C_startClock (uint16_t baseAddress)
22 {
23  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
24  HWREG8(baseAddress + OFS_RTCCTL13_L) &= ~(RTCHOLD);
25  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
26 }
27 
28 void RTC_C_holdClock (uint16_t baseAddress)
29 {
30  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
31  HWREG8(baseAddress + OFS_RTCCTL13_L) |= RTCHOLD;
32  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
33 }
34 
35 void RTC_C_setCalibrationFrequency (uint16_t baseAddress,
36  uint16_t frequencySelect)
37 {
38  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
39  HWREG16(baseAddress + OFS_RTCCTL13) &= ~(RTCCALF_3);
40  HWREG16(baseAddress + OFS_RTCCTL13) |= frequencySelect;
41  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
42 }
43 
44 void RTC_C_setCalibrationData (uint16_t baseAddress,
45  uint8_t offsetDirection,
46  uint8_t offsetValue)
47 {
48  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
49  HWREG16(baseAddress + OFS_RTCOCAL) = offsetValue + offsetDirection;
50  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
51 }
52 
53 void RTC_C_initCounter (uint16_t baseAddress,
54  uint16_t clockSelect,
55  uint16_t counterSizeSelect)
56 {
57  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
58 
59  HWREG8(baseAddress + OFS_RTCCTL13) |= RTCHOLD;
60  HWREG8(baseAddress + OFS_RTCCTL13) &= ~(RTCMODE);
61 
62  HWREG16(baseAddress + OFS_RTCCTL13) &= ~(RTCSSEL_3 | RTCTEV_3 );
63  HWREG16(baseAddress + OFS_RTCCTL13) |= clockSelect + counterSizeSelect;
64 
65  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
66 }
67 
68 bool RTC_C_setTemperatureCompensation(uint16_t baseAddress,
69  uint16_t offsetDirection,
70  uint8_t offsetValue)
71 {
72 
73  while(!(HWREG8(baseAddress + OFS_RTCTCMP_H) & RTCTCRDY_H));
74 
75  HWREG16(baseAddress + OFS_RTCTCMP) = offsetValue + offsetDirection;
76 
77  if(HWREG8(baseAddress + OFS_RTCTCMP_H) & RTCTCOK_H) {
78  return STATUS_SUCCESS;
79  } else {
80  return STATUS_FAIL;
81  }
82 }
83 
84 void RTC_C_initCalendar (uint16_t baseAddress,
85  Calendar *CalendarTime,
86  uint16_t formatSelect)
87 {
88 
89  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
90 
91  HWREG8(baseAddress + OFS_RTCCTL13_L) |= RTCHOLD;
92 
93  HWREG16(baseAddress + OFS_RTCCTL13_L) &= ~(RTCBCD);
94  HWREG16(baseAddress + OFS_RTCCTL13_L) |= formatSelect;
95 
96  HWREG8(baseAddress + OFS_RTCTIM0_L) = CalendarTime->Seconds;
97  HWREG8(baseAddress + OFS_RTCTIM0_H) = CalendarTime->Minutes;
98  HWREG8(baseAddress + OFS_RTCTIM1_L) = CalendarTime->Hours;
99  HWREG8(baseAddress + OFS_RTCTIM1_H) = CalendarTime->DayOfWeek;
100  HWREG8(baseAddress + OFS_RTCDATE_L) = CalendarTime->DayOfMonth;
101  HWREG8(baseAddress + OFS_RTCDATE_H) = CalendarTime->Month;
102  HWREG16(baseAddress + OFS_RTCYEAR) = CalendarTime->Year;
103 
104  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
105 }
106 
107 Calendar RTC_C_getCalendarTime (uint16_t baseAddress)
108 {
109  Calendar tempCal;
110 
111  while ( !(HWREG8(baseAddress + OFS_RTCCTL13_L) & RTCRDY) ) ;
112 
113  tempCal.Seconds = HWREG8(baseAddress + OFS_RTCTIM0_L);
114  tempCal.Minutes = HWREG8(baseAddress + OFS_RTCTIM0_H);
115  tempCal.Hours = HWREG8(baseAddress + OFS_RTCTIM1_L);
116  tempCal.DayOfWeek = HWREG8(baseAddress + OFS_RTCTIM1_H);
117  tempCal.DayOfMonth = HWREG8(baseAddress + OFS_RTCDATE_L);
118  tempCal.Month = HWREG8(baseAddress + OFS_RTCDATE_H);
119  tempCal.Year = HWREG16(baseAddress + OFS_RTCYEAR);
120 
121  return ( tempCal) ;
122 }
123 
124 void RTC_C_configureCalendarAlarm(uint16_t baseAddress,
125  RTC_C_configureCalendarAlarmParam *param)
126 {
127  //Each of these is XORed with 0x80 to turn on if an integer is passed,
128  //or turn OFF if RTC_C_ALARM_OFF (0x80) is passed.
129  HWREG8(baseAddress + OFS_RTCAMINHR_L) = (param->minutesAlarm ^ 0x80);
130  HWREG8(baseAddress + OFS_RTCAMINHR_H) = (param->hoursAlarm ^ 0x80);
131  HWREG8(baseAddress + OFS_RTCADOWDAY_L) = (param->dayOfWeekAlarm ^ 0x80);
132  HWREG8(baseAddress + OFS_RTCADOWDAY_H) = (param->dayOfMonthAlarm ^ 0x80);
133 }
134 void RTC_C_setCalendarEvent (uint16_t baseAddress,
135  uint16_t eventSelect)
136 {
137  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
138  HWREG8(baseAddress + OFS_RTCCTL13_L) &= ~(RTCTEV_3); //Reset bits
139  HWREG8(baseAddress + OFS_RTCCTL13_L) |= eventSelect;
140  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
141 }
142 
143 uint32_t RTC_C_getCounterValue (uint16_t baseAddress)
144 {
145  if ( (HWREG8(baseAddress + OFS_RTCCTL13) & RTCHOLD)
146  || (HWREG8(baseAddress + OFS_RTCPS1CTL) & RT1PSHOLD) ){
147  return ( 0) ;
148  }
149 
150  uint32_t counterValue_L = HWREG16(baseAddress + OFS_RTCTIM0);
151  uint32_t counterValue_H = HWREG16(baseAddress + OFS_RTCTIM1);
152  return ( (counterValue_H << 16) + counterValue_L );
153 }
154 
155 void RTC_C_setCounterValue (uint16_t baseAddress,
156  uint32_t counterValue)
157 {
158  uint16_t mode = HWREG16(baseAddress + OFS_RTCCTL13) & RTCTEV_3;
159 
160  if(mode == RTC_C_COUNTERSIZE_8BIT && counterValue > UINT8_MAX)
161  counterValue = UINT8_MAX;
162  else if(mode == RTC_C_COUNTERSIZE_16BIT && counterValue > UINT16_MAX)
163  counterValue = UINT16_MAX;
164  else if(mode == RTC_C_COUNTERSIZE_24BIT && counterValue > 0xFFFFFF)
165  counterValue = 0xFFFFFF;
166 
167  HWREG16(baseAddress + OFS_RTCTIM0) = counterValue;
168  HWREG16(baseAddress + OFS_RTCTIM1) = ( counterValue >> 16 );
169 }
170 
171 void RTC_C_initCounterPrescale (uint16_t baseAddress,
172  uint8_t prescaleSelect,
173  uint16_t prescaleClockSelect,
174  uint16_t prescaleDivider)
175 {
176  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
177 
178  //Reset bits and set clock select
179  HWREG16(baseAddress + OFS_RTCPS0CTL + prescaleSelect) =
180  prescaleClockSelect + prescaleDivider;
181 
182  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
183 }
184 
185 void RTC_C_holdCounterPrescale (uint16_t baseAddress,
186  uint8_t prescaleSelect)
187 {
188  HWREG8(baseAddress + OFS_RTCPS0CTL_H + prescaleSelect) |= RT0PSHOLD_H;
189 }
190 
191 void RTC_C_startCounterPrescale (uint16_t baseAddress,
192  uint8_t prescaleSelect)
193 {
194  HWREG8(baseAddress + OFS_RTCPS0CTL_H + prescaleSelect) &= ~(RT0PSHOLD_H);
195 }
196 
197 void RTC_C_definePrescaleEvent (uint16_t baseAddress,
198  uint8_t prescaleSelect,
199  uint8_t prescaleEventDivider)
200 {
201  HWREG8(baseAddress + OFS_RTCPS0CTL_L + prescaleSelect) &= ~(RT0IP_7);
202  HWREG8(baseAddress + OFS_RTCPS0CTL_L +
203  prescaleSelect) |= prescaleEventDivider;
204 }
205 
206 uint8_t RTC_C_getPrescaleValue (uint16_t baseAddress,
207  uint8_t prescaleSelect)
208 {
209  if (RTC_C_PRESCALE_0 == prescaleSelect){
210  return ( HWREG8(baseAddress + OFS_RTCPS_L) );
211  } else if (RTC_C_PRESCALE_1 == prescaleSelect){
212  return ( HWREG8(baseAddress + OFS_RTCPS_H) );
213  } else {
214  return ( 0) ;
215  }
216 }
217 
218 void RTC_C_setPrescaleValue(uint16_t baseAddress,
219  uint8_t prescaleSelect,
220  uint8_t prescaleCounterValue)
221 {
222  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
223  if (RTC_C_PRESCALE_0 == prescaleSelect){
224  HWREG8(baseAddress + OFS_RTCPS_L) = prescaleCounterValue;
225  } else if (RTC_C_PRESCALE_1 == prescaleSelect){
226  HWREG8(baseAddress + OFS_RTCPS_H) = prescaleCounterValue;
227  }
228  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
229 }
230 
231 void RTC_C_enableInterrupt (uint16_t baseAddress,
232  uint8_t interruptMask)
233 {
234  if ( interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE) ){
235  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
236  HWREG8(baseAddress + OFS_RTCCTL0_L) |=
237  (interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE));
238  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
239  }
240 
241  if (interruptMask & RTC_C_PRESCALE_TIMER0_INTERRUPT){
242  HWREG8(baseAddress + OFS_RTCPS0CTL_L) |= RT0PSIE;
243  }
244 
245  if (interruptMask & RTC_C_PRESCALE_TIMER1_INTERRUPT){
246  HWREG8(baseAddress + OFS_RTCPS1CTL_L) |= RT1PSIE;
247  }
248 }
249 
250 void RTC_C_disableInterrupt (uint16_t baseAddress,
251  uint8_t interruptMask)
252 {
253 
254  if ( interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE) ){
255  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
256  HWREG8(baseAddress + OFS_RTCCTL0_L) &=
257  ~(interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE));
258  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
259  }
260 
261  if (interruptMask & RTC_C_PRESCALE_TIMER0_INTERRUPT){
262  HWREG8(baseAddress + OFS_RTCPS0CTL_L) &= ~(RT0PSIE);
263  }
264 
265  if (interruptMask & RTC_C_PRESCALE_TIMER1_INTERRUPT){
266  HWREG8(baseAddress + OFS_RTCPS1CTL_L) &= ~(RT1PSIE);
267  }
268 }
269 
270 uint8_t RTC_C_getInterruptStatus (uint16_t baseAddress,
271  uint8_t interruptFlagMask)
272 {
273  uint8_t tempInterruptFlagMask = 0x0000;
274 
275  tempInterruptFlagMask |= (HWREG8(baseAddress + OFS_RTCCTL0_L)
276  & ((interruptFlagMask >> 4)
277  & (RTCOFIFG +
278  RTCTEVIFG +
279  RTCAIFG +
280  RTCRDYIFG)));
281 
282  tempInterruptFlagMask = tempInterruptFlagMask << 4;
283 
284  if (interruptFlagMask & RTC_C_PRESCALE_TIMER0_INTERRUPT){
285  if ( HWREG8(baseAddress + OFS_RTCPS0CTL_L) & RT0PSIFG){
286  tempInterruptFlagMask |= RTC_C_PRESCALE_TIMER0_INTERRUPT;
287  }
288  }
289 
290  if (interruptFlagMask & RTC_C_PRESCALE_TIMER1_INTERRUPT){
291  if ( HWREG8(baseAddress + OFS_RTCPS1CTL_L) & RT1PSIFG){
292  tempInterruptFlagMask |= RTC_C_PRESCALE_TIMER1_INTERRUPT;
293  }
294  }
295 
296  return ( tempInterruptFlagMask) ;
297 }
298 
299 void RTC_C_clearInterrupt (uint16_t baseAddress,
300  uint8_t interruptFlagMask)
301 {
302 
303  if ( interruptFlagMask & (RTC_C_TIME_EVENT_INTERRUPT +
304  RTC_C_CLOCK_ALARM_INTERRUPT +
305  RTC_C_CLOCK_READ_READY_INTERRUPT +
306  RTC_C_OSCILLATOR_FAULT_INTERRUPT) ){
307  HWREG8(baseAddress + OFS_RTCCTL0_H) = RTCKEY_H;
308  HWREG8(baseAddress + OFS_RTCCTL0_L) &=
309  ~((interruptFlagMask>>4) & (RTCOFIFG +
310  RTCTEVIFG +
311  RTCAIFG +
312  RTCRDYIFG));
313  HWREG8(baseAddress + OFS_RTCCTL0_H) = 0x00;
314  }
315 
316  if (interruptFlagMask & RTC_C_PRESCALE_TIMER0_INTERRUPT){
317  HWREG8(baseAddress + OFS_RTCPS0CTL_L) &= ~(RT0PSIFG);
318  }
319 
320  if (interruptFlagMask & RTC_C_PRESCALE_TIMER1_INTERRUPT){
321  HWREG8(baseAddress + OFS_RTCPS1CTL_L) &= ~(RT1PSIFG);
322  }
323 }
324 
325 uint16_t RTC_C_convertBCDToBinary (uint16_t baseAddress,
326  uint16_t valueToConvert)
327 {
328  HWREG16(baseAddress + OFS_BCD2BIN) = valueToConvert;
329  return ( HWREG16(baseAddress + OFS_BCD2BIN) );
330 }
331 
332 uint16_t RTC_C_convertBinaryToBCD (uint16_t baseAddress,
333  uint16_t valueToConvert)
334 {
335  HWREG16(baseAddress + OFS_BIN2BCD) = valueToConvert;
336  return ( HWREG16(baseAddress + OFS_BIN2BCD) );
337 }
338 
339 #endif
340 //*****************************************************************************
341 //
344 //
345 //*****************************************************************************
MPU_initThreeSegmentsParam param
#define HWREG8(x)
Definition: hw_memmap.h:41
#define HWREG16(x)
Definition: hw_memmap.h:39
#define STATUS_FAIL
Definition: hw_memmap.h:23
#define STATUS_SUCCESS
Definition: hw_memmap.h:22