2020-2021 Sunseeker Telemetry and Lighting System
rtc_b.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // rtc_b.c - Driver for the rtc_b Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_RTC_B__
17 #include "rtc_b.h"
18 
19 #include <assert.h>
20 
21 void RTC_B_startClock (uint16_t baseAddress)
22 {
23  HWREG8(baseAddress + OFS_RTCCTL01_H) &= ~(RTCHOLD_H);
24 }
25 
26 void RTC_B_holdClock (uint16_t baseAddress)
27 {
28  HWREG8(baseAddress + OFS_RTCCTL01_H) |= RTCHOLD_H;
29 }
30 
31 void RTC_B_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_B_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_B_initCalendar (uint16_t baseAddress,
46  Calendar *CalendarTime,
47  uint16_t formatSelect)
48 {
49  HWREG8(baseAddress + OFS_RTCCTL01_H) |= RTCHOLD_H;
50 
51  HWREG16(baseAddress + OFS_RTCCTL01) &= ~(RTCBCD);
52  HWREG16(baseAddress + OFS_RTCCTL01) |= formatSelect;
53 
54  HWREG8(baseAddress + OFS_RTCTIM0_L) = CalendarTime->Seconds;
55  HWREG8(baseAddress + OFS_RTCTIM0_H) = CalendarTime->Minutes;
56  HWREG8(baseAddress + OFS_RTCTIM1_L) = CalendarTime->Hours;
57  HWREG8(baseAddress + OFS_RTCTIM1_H) = CalendarTime->DayOfWeek;
58  HWREG8(baseAddress + OFS_RTCDATE_L) = CalendarTime->DayOfMonth;
59  HWREG8(baseAddress + OFS_RTCDATE_H) = CalendarTime->Month;
60  HWREG16(baseAddress + OFS_RTCYEAR) = CalendarTime->Year;
61 }
62 
63 Calendar RTC_B_getCalendarTime (uint16_t baseAddress)
64 {
65  Calendar tempCal;
66 
67  while ( !(HWREG16(baseAddress + OFS_RTCCTL01) & RTCRDY) ) ;
68 
69  tempCal.Seconds = HWREG8(baseAddress + OFS_RTCTIM0_L);
70  tempCal.Minutes = HWREG8(baseAddress + OFS_RTCTIM0_H);
71  tempCal.Hours = HWREG8(baseAddress + OFS_RTCTIM1_L);
72  tempCal.DayOfWeek = HWREG8(baseAddress + OFS_RTCTIM1_H);
73  tempCal.DayOfMonth = HWREG8(baseAddress + OFS_RTCDATE_L);
74  tempCal.Month = HWREG8(baseAddress + OFS_RTCDATE_H);
75  tempCal.Year = HWREG16(baseAddress + OFS_RTCYEAR);
76 
77  return ( tempCal) ;
78 }
79 
80 void RTC_B_configureCalendarAlarm(uint16_t baseAddress,
81  RTC_B_configureCalendarAlarmParam *param)
82 {
83  //Each of these is XORed with 0x80 to turn on if an integer is passed,
84  //or turn OFF if RTC_B_ALARM_OFF (0x80) is passed.
85  HWREG8(baseAddress + OFS_RTCAMINHR_L) = (param->minutesAlarm ^ 0x80);
86  HWREG8(baseAddress + OFS_RTCAMINHR_H) = (param->hoursAlarm ^ 0x80);
87  HWREG8(baseAddress + OFS_RTCADOWDAY_L) = (param->dayOfWeekAlarm ^ 0x80);
88  HWREG8(baseAddress + OFS_RTCADOWDAY_H) = (param->dayOfMonthAlarm ^ 0x80);
89 }
90 void RTC_B_setCalendarEvent (uint16_t baseAddress,
91  uint16_t eventSelect)
92 {
93  HWREG16(baseAddress + OFS_RTCCTL01) &= ~(RTCTEV_3); //Reset bits
94  HWREG16(baseAddress + OFS_RTCCTL01) |= eventSelect;
95 }
96 
97 void RTC_B_definePrescaleEvent (uint16_t baseAddress,
98  uint8_t prescaleSelect,
99  uint8_t prescaleEventDivider)
100 {
101  HWREG8(baseAddress + OFS_RTCPS0CTL_L + prescaleSelect) &= ~(RT0IP_7);
102  HWREG8(baseAddress + OFS_RTCPS0CTL_L +
103  prescaleSelect) |= prescaleEventDivider;
104 }
105 
106 uint8_t RTC_B_getPrescaleValue (uint16_t baseAddress,
107  uint8_t prescaleSelect)
108 {
109  if (RTC_B_PRESCALE_0 == prescaleSelect){
110  return ( HWREG8(baseAddress + OFS_RTCPS_L) );
111  } else if (RTC_B_PRESCALE_1 == prescaleSelect){
112  return ( HWREG8(baseAddress + OFS_RTCPS_H) );
113  } else {
114  return ( 0) ;
115  }
116 }
117 
118 void RTC_B_setPrescaleValue (uint16_t baseAddress,
119  uint8_t prescaleSelect,
120  uint8_t prescaleCounterValue)
121 {
122  if (RTC_B_PRESCALE_0 == prescaleSelect){
123  HWREG8(baseAddress + OFS_RTCPS_L) = prescaleCounterValue;
124  } else if (RTC_B_PRESCALE_1 == prescaleSelect){
125  HWREG8(baseAddress + OFS_RTCPS_H) = prescaleCounterValue;
126  }
127 }
128 
129 void RTC_B_enableInterrupt (uint16_t baseAddress,
130  uint8_t interruptMask)
131 {
132  if ( interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE) ){
133  HWREG8(baseAddress + OFS_RTCCTL01_L) |=
134  (interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE));
135  }
136 
137  if (interruptMask & RTC_B_PRESCALE_TIMER0_INTERRUPT){
138  HWREG8(baseAddress + OFS_RTCPS0CTL) |= RT0PSIE;
139  }
140 
141  if (interruptMask & RTC_B_PRESCALE_TIMER1_INTERRUPT){
142  HWREG8(baseAddress + OFS_RTCPS1CTL) |= RT1PSIE;
143  }
144 }
145 
146 void RTC_B_disableInterrupt (uint16_t baseAddress,
147  uint8_t interruptMask)
148 {
149  if ( interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE) ){
150  HWREG8(baseAddress + OFS_RTCCTL01_L) &=
151  ~(interruptMask & (RTCOFIE + RTCTEVIE + RTCAIE + RTCRDYIE));
152  }
153 
154  if (interruptMask & RTC_B_PRESCALE_TIMER0_INTERRUPT){
155  HWREG8(baseAddress + OFS_RTCPS0CTL) &= ~(RT0PSIE);
156  }
157 
158  if (interruptMask & RTC_B_PRESCALE_TIMER1_INTERRUPT){
159  HWREG8(baseAddress + OFS_RTCPS1CTL) &= ~(RT1PSIE);
160  }
161 }
162 
163 uint8_t RTC_B_getInterruptStatus (uint16_t baseAddress,
164  uint8_t interruptFlagMask)
165 {
166  uint8_t tempInterruptFlagMask = 0x0000;
167 
168  tempInterruptFlagMask |= (HWREG8(baseAddress + OFS_RTCCTL01_L)
169  & ((interruptFlagMask >> 4)
170  & (RTCOFIFG +
171  RTCTEVIFG +
172  RTCAIFG +
173  RTCRDYIFG)));
174 
175  tempInterruptFlagMask = tempInterruptFlagMask << 4;
176 
177  if (interruptFlagMask & RTC_B_PRESCALE_TIMER0_INTERRUPT){
178  if ( HWREG8(baseAddress + OFS_RTCPS0CTL) & RT0PSIFG){
179  tempInterruptFlagMask |= RTC_B_PRESCALE_TIMER0_INTERRUPT;
180  }
181  }
182 
183  if (interruptFlagMask & RTC_B_PRESCALE_TIMER1_INTERRUPT){
184  if ( HWREG8(baseAddress + OFS_RTCPS1CTL) & RT1PSIFG){
185  tempInterruptFlagMask |= RTC_B_PRESCALE_TIMER1_INTERRUPT;
186  }
187  }
188 
189  return ( tempInterruptFlagMask) ;
190 }
191 
192 void RTC_B_clearInterrupt (uint16_t baseAddress,
193  uint8_t interruptFlagMask)
194 {
195  if ( interruptFlagMask & (RTC_B_TIME_EVENT_INTERRUPT +
196  RTC_B_CLOCK_ALARM_INTERRUPT +
197  RTC_B_CLOCK_READ_READY_INTERRUPT +
198  RTC_B_OSCILLATOR_FAULT_INTERRUPT) ){
199 
200  HWREG8(baseAddress + OFS_RTCCTL01_L) &=
201  ~((interruptFlagMask>>4) & (RTCOFIFG +
202  RTCTEVIFG +
203  RTCAIFG +
204  RTCRDYIFG));
205  }
206 
207  if (interruptFlagMask & RTC_B_PRESCALE_TIMER0_INTERRUPT){
208  HWREG8(baseAddress + OFS_RTCPS0CTL) &= ~(RT0PSIFG);
209  }
210 
211  if (interruptFlagMask & RTC_B_PRESCALE_TIMER1_INTERRUPT){
212  HWREG8(baseAddress + OFS_RTCPS1CTL) &= ~(RT1PSIFG);
213  }
214 }
215 
216 uint16_t RTC_B_convertBCDToBinary (uint16_t baseAddress,
217  uint16_t valueToConvert)
218 {
219  HWREG16(baseAddress + OFS_BCD2BIN) = valueToConvert;
220  return ( HWREG16(baseAddress + OFS_BCD2BIN) );
221 }
222 
223 uint16_t RTC_B_convertBinaryToBCD (uint16_t baseAddress,
224  uint16_t valueToConvert)
225 {
226  HWREG16(baseAddress + OFS_BIN2BCD) = valueToConvert;
227  return ( HWREG16(baseAddress + OFS_BIN2BCD) );
228 }
229 
230 
231 #endif
232 //*****************************************************************************
233 //
236 //
237 //*****************************************************************************
MPU_initThreeSegmentsParam param
#define HWREG8(x)
Definition: hw_memmap.h:41
#define HWREG16(x)
Definition: hw_memmap.h:39