2020-2021 Sunseeker Telemetry and Lighting System
rtc_a.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // rtc_a.c - Driver for the rtc_a Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_RTC__
17 #include "rtc_a.h"
18 
19 #include <assert.h>
20 
21 void RTC_A_startClock (uint16_t baseAddress)
22 {
23  HWREG8(baseAddress + OFS_RTCCTL01_H) &= ~(RTCHOLD_H);
24 }
25 
26 void RTC_A_holdClock (uint16_t baseAddress)
27 {
28  HWREG8(baseAddress + OFS_RTCCTL01_H) |= RTCHOLD_H;
29 }
30 
31 void RTC_A_setCalibrationFrequency (uint16_t baseAddress,
32  uint16_t frequencySelect)
33 {
34  HWREG16(baseAddress + OFS_RTCCTL23) &= ~(RTCCALF_3);
35  HWREG16(baseAddress + OFS_RTCCTL23) |= frequencySelect;
36 }
37 
38 void RTC_A_setCalibrationData (uint16_t baseAddress,
39  uint8_t offsetDirection,
40  uint8_t offsetValue)
41 {
42  HWREG8(baseAddress + OFS_RTCCTL23_L) = offsetValue + offsetDirection;
43 }
44 
45 void RTC_A_initCounter (uint16_t baseAddress,
46  uint16_t clockSelect,
47  uint16_t counterSizeSelect)
48 {
49  HWREG8(baseAddress + OFS_RTCCTL01_H) |= RTCHOLD_H;
50  HWREG8(baseAddress + OFS_RTCCTL01_H) &= ~(RTCMODE_H);
51 
52  HWREG16(baseAddress + OFS_RTCCTL01) &= 0xF0FF; //~(RTCSSEL_3 + RTCTEV_3);
53  HWREG16(baseAddress + OFS_RTCCTL01) |= clockSelect + counterSizeSelect;
54 }
55 
56 void RTC_A_initCalendar (uint16_t baseAddress,
57  Calendar *CalendarTime,
58  uint16_t formatSelect)
59 {
60  HWREG8(baseAddress + OFS_RTCCTL01_H) |= RTCMODE_H + RTCHOLD_H;
61 
62  HWREG16(baseAddress + OFS_RTCCTL01) &= ~(RTCBCD);
63  HWREG16(baseAddress + OFS_RTCCTL01) |= formatSelect;
64 
65  HWREG8(baseAddress + OFS_RTCTIM0_L) = CalendarTime->Seconds;
66  HWREG8(baseAddress + OFS_RTCTIM0_H) = CalendarTime->Minutes;
67  HWREG8(baseAddress + OFS_RTCTIM1_L) = CalendarTime->Hours;
68  HWREG8(baseAddress + OFS_RTCTIM1_H) = CalendarTime->DayOfWeek;
69  HWREG8(baseAddress + OFS_RTCDATE_L) = CalendarTime->DayOfMonth;
70  HWREG8(baseAddress + OFS_RTCDATE_H) = CalendarTime->Month;
71  HWREG16(baseAddress + OFS_RTCYEAR) = CalendarTime->Year;
72 }
73 
74 Calendar RTC_A_getCalendarTime (uint16_t baseAddress)
75 {
76  Calendar tempCal;
77 
78  while ( !(HWREG16(baseAddress + OFS_RTCCTL01) & RTCRDY) ) ;
79 
80  tempCal.Seconds = HWREG8(baseAddress + OFS_RTCTIM0_L);
81  tempCal.Minutes = HWREG8(baseAddress + OFS_RTCTIM0_H);
82  tempCal.Hours = HWREG8(baseAddress + OFS_RTCTIM1_L);
83  tempCal.DayOfWeek = HWREG8(baseAddress + OFS_RTCTIM1_H);
84  tempCal.DayOfMonth = HWREG8(baseAddress + OFS_RTCDATE_L);
85  tempCal.Month = HWREG8(baseAddress + OFS_RTCDATE_H);
86  tempCal.Year = HWREG16(baseAddress + OFS_RTCYEAR);
87 
88  return ( tempCal) ;
89 }
90 
91 void RTC_A_configureCalendarAlarm(uint16_t baseAddress,
92  RTC_A_configureCalendarAlarmParam *param)
93 {
94  //Each of these is XORed with 0x80 to turn on if an integer is passed,
95  //or turn OFF if RTC_A_ALARM_OFF (0x80) is passed.
96  HWREG8(baseAddress + OFS_RTCAMINHR_L) = (param->minutesAlarm ^ 0x80);
97  HWREG8(baseAddress + OFS_RTCAMINHR_H) = (param->hoursAlarm ^ 0x80);
98  HWREG8(baseAddress + OFS_RTCADOWDAY_L) = (param->dayOfWeekAlarm ^ 0x80);
99  HWREG8(baseAddress + OFS_RTCADOWDAY_H) = (param->dayOfMonthAlarm ^ 0x80);
100 }
101 void RTC_A_setCalendarEvent (uint16_t baseAddress,
102  uint16_t eventSelect)
103 {
104  HWREG16(baseAddress + OFS_RTCCTL01) &= ~(RTCTEV_3); //Reset bits
105  HWREG16(baseAddress + OFS_RTCCTL01) |= eventSelect;
106 }
107 
108 uint32_t RTC_A_getCounterValue (uint16_t baseAddress)
109 {
110  if ( (HWREG8(baseAddress + OFS_RTCCTL01_H) & RTCHOLD_H)
111  || (HWREG8(baseAddress + OFS_RTCPS1CTL) & RT1PSHOLD) ){
112  return ( 0) ;
113  }
114 
115  uint32_t counterValue_L = HWREG16(baseAddress + OFS_RTCTIM0);
116  uint32_t counterValue_H = HWREG16(baseAddress + OFS_RTCTIM1);
117  return ( (counterValue_H << 16) + counterValue_L );
118 }
119 
120 void RTC_A_setCounterValue (uint16_t baseAddress,
121  uint32_t counterValue)
122 {
123  HWREG16(baseAddress + OFS_RTCTIM0) = counterValue;
124  HWREG16(baseAddress + OFS_RTCTIM1) = ( counterValue >> 16 );
125 }
126 
127 void RTC_A_initCounterPrescale (uint16_t baseAddress,
128  uint8_t prescaleSelect,
129  uint16_t prescaleClockSelect,
130  uint16_t prescaleDivider)
131 {
132  //Reset bits and set clock select
133  HWREG16(baseAddress + OFS_RTCPS0CTL + prescaleSelect) =
134  prescaleClockSelect + prescaleDivider;
135 }
136 
137 void RTC_A_holdCounterPrescale (uint16_t baseAddress,
138  uint8_t prescaleSelect)
139 {
140  HWREG8(baseAddress + OFS_RTCPS0CTL_H + prescaleSelect) |= RT0PSHOLD_H;
141 }
142 
143 void RTC_A_startCounterPrescale (uint16_t baseAddress,
144  uint8_t prescaleSelect)
145 {
146  HWREG8(baseAddress + OFS_RTCPS0CTL_H + prescaleSelect) &= ~(RT0PSHOLD_H);
147 }
148 
149 void RTC_A_definePrescaleEvent (uint16_t baseAddress,
150  uint8_t prescaleSelect,
151  uint8_t prescaleEventDivider)
152 {
153  HWREG8(baseAddress + OFS_RTCPS0CTL_L + prescaleSelect) &= ~(RT0IP_7);
154  HWREG8(baseAddress + OFS_RTCPS0CTL_L +
155  prescaleSelect) |= prescaleEventDivider;
156 }
157 
158 uint8_t RTC_A_getPrescaleValue (uint16_t baseAddress,
159  uint8_t prescaleSelect)
160 {
161  if (HWREG8(baseAddress + OFS_RTCPS0CTL_H + prescaleSelect) & RT0PSHOLD_H){
162  return ( 0) ;
163  }
164 
165  if (RTC_A_PRESCALE_0 == prescaleSelect){
166  return ( HWREG8(baseAddress + OFS_RTCPS_L) );
167  } else if (RTC_A_PRESCALE_1 == prescaleSelect){
168  return ( HWREG8(baseAddress + OFS_RTCPS_H) );
169  } else {
170  return ( 0) ;
171  }
172 }
173 
174 void RTC_A_setPrescaleValue (uint16_t baseAddress,
175  uint8_t prescaleSelect,
176  uint8_t prescaleCounterValue)
177 {
178  if (RTC_A_PRESCALE_0 == prescaleSelect){
179  HWREG8(baseAddress + OFS_RTCPS_L) = prescaleCounterValue;
180  } else if (RTC_A_PRESCALE_1 == prescaleSelect){
181  HWREG8(baseAddress + OFS_RTCPS_H) = prescaleCounterValue;
182  }
183 }
184 
185 void RTC_A_enableInterrupt (uint16_t baseAddress,
186  uint8_t interruptMask)
187 {
188  if ( interruptMask & (RTCTEVIE + RTCAIE + RTCRDYIE) ){
189  HWREG8(baseAddress + OFS_RTCCTL01_L) |=
190  (interruptMask & (RTCTEVIE + RTCAIE + RTCRDYIE));
191  }
192 
193  if (interruptMask & RTC_A_PRESCALE_TIMER0_INTERRUPT){
194  HWREG8(baseAddress + OFS_RTCPS0CTL) |= RT0PSIE;
195  }
196 
197  if (interruptMask & RTC_A_PRESCALE_TIMER1_INTERRUPT){
198  HWREG8(baseAddress + OFS_RTCPS1CTL) |= RT1PSIE;
199  }
200 }
201 
202 void RTC_A_disableInterrupt (uint16_t baseAddress,
203  uint8_t interruptMask)
204 {
205  if ( interruptMask & ( RTCTEVIE + RTCAIE + RTCRDYIE) ){
206  HWREG8(baseAddress + OFS_RTCCTL01_L) &=
207  ~(interruptMask & (RTCTEVIE + RTCAIE + RTCRDYIE));
208  }
209 
210  if (interruptMask & RTC_A_PRESCALE_TIMER0_INTERRUPT){
211  HWREG8(baseAddress + OFS_RTCPS0CTL) &= ~(RT0PSIE);
212  }
213 
214  if (interruptMask & RTC_A_PRESCALE_TIMER1_INTERRUPT){
215  HWREG8(baseAddress + OFS_RTCPS1CTL) &= ~(RT1PSIE);
216  }
217 }
218 
219 uint8_t RTC_A_getInterruptStatus (uint16_t baseAddress,
220  uint8_t interruptFlagMask)
221 {
222  uint8_t tempInterruptFlagMask = 0x0000;
223 
224  tempInterruptFlagMask |= (HWREG8(baseAddress + OFS_RTCCTL01_L)
225  & ((interruptFlagMask >> 4)
226  & (
227  RTCTEVIFG +
228  RTCAIFG +
229  RTCRDYIFG)));
230 
231  tempInterruptFlagMask = tempInterruptFlagMask << 4;
232 
233  if (interruptFlagMask & RTC_A_PRESCALE_TIMER0_INTERRUPT){
234  if ( HWREG8(baseAddress + OFS_RTCPS0CTL) & RT0PSIFG){
235  tempInterruptFlagMask |= RTC_A_PRESCALE_TIMER0_INTERRUPT;
236  }
237  }
238 
239  if (interruptFlagMask & RTC_A_PRESCALE_TIMER1_INTERRUPT){
240  if ( HWREG8(baseAddress + OFS_RTCPS1CTL) & RT1PSIFG){
241  tempInterruptFlagMask |= RTC_A_PRESCALE_TIMER1_INTERRUPT;
242  }
243  }
244 
245  return ( tempInterruptFlagMask) ;
246 }
247 
248 void RTC_A_clearInterrupt (uint16_t baseAddress,
249  uint8_t interruptFlagMask)
250 {
251  if ( interruptFlagMask & (RTC_A_TIME_EVENT_INTERRUPT +
252  RTC_A_CLOCK_ALARM_INTERRUPT +
253  RTC_A_CLOCK_READ_READY_INTERRUPT
254  ) ){
255 
256  HWREG8(baseAddress + OFS_RTCCTL01_L) &=
257  ~((interruptFlagMask>>4) & (RTCTEVIFG +
258  RTCAIFG +
259  RTCRDYIFG));
260  }
261 
262  if (interruptFlagMask & RTC_A_PRESCALE_TIMER0_INTERRUPT){
263  HWREG8(baseAddress + OFS_RTCPS0CTL) &= ~(RT0PSIFG);
264  }
265 
266  if (interruptFlagMask & RTC_A_PRESCALE_TIMER1_INTERRUPT){
267  HWREG8(baseAddress + OFS_RTCPS1CTL) &= ~(RT1PSIFG);
268  }
269 }
270 
271 
272 #endif
273 //*****************************************************************************
274 //
277 //
278 //*****************************************************************************
MPU_initThreeSegmentsParam param
#define HWREG8(x)
Definition: hw_memmap.h:41
#define HWREG16(x)
Definition: hw_memmap.h:39