2020-2021 Sunseeker Telemetry and Lighting System
ctsd16.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // ctsd16.c - Driver for the ctsd16 Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_CTSD16__
17 #include "ctsd16.h"
18 
19 #include <assert.h>
20 
21 void CTSD16_init(uint16_t baseAddress, uint16_t chargePumpBurstRequest,
22  uint8_t referenceSelect)
23 {
24  // Reset all interrupts and flags
25  HWREG16(baseAddress + OFS_CTSD16IE) &= 0x0000; //Reset ALL interrupt enables
26  HWREG16(baseAddress + OFS_CTSD16IFG) &= 0x0000; //Reset ALL interrupt flags
27 
28  // Configure CTSD16
29  HWREG16(baseAddress + OFS_CTSD16CTL) &= ~(CTSD16RRIBURST | CTSD16OFFG |
30  CTSD16REFS);
31 
32  HWREG16(baseAddress + OFS_CTSD16CTL) |= chargePumpBurstRequest | referenceSelect;
33 
34  return;
35 }
36 
37 void CTSD16_initConverter(uint16_t baseAddress, uint8_t converter,
38  uint16_t conversionMode, uint8_t inputChannel)
39 {
40  uint16_t address;
41  address = baseAddress + (OFS_CTSD16CCTL0 + (converter * 0x06));
42 
43  // Clearing previous settings for configuration
44  HWREG16(address) &= ~(CTSD16SC | CTSD16SNGL);
45 
46  HWREG16(address) |= conversionMode;
47 
48  address += 0x2;
49 
50  HWREG16(address) &= ~(CTSD16INCH4 | CTSD16INCH3 | CTSD16INCH2 |
51  CTSD16INCH1 | CTSD16INCH0);
52 
53  HWREG16(address) |= inputChannel;
54 }
55 
56 void CTSD16_initConverterAdvanced(uint16_t baseAddress,
57  CTSD16_initConverterAdvancedParam *param)
58 {
59  uint16_t address;
60 
61  // Getting correct CTSD16CCTLx register
62  address = baseAddress + (OFS_CTSD16CCTL0 + (param->converter * 0x06));
63 
64  // Clearing previous settings for configuration
65  HWREG16(address) &= ~(CTSD16SC | CTSD16SNGL | CTSD16GRP | CTSD16OSR__32 |
66  CTSD16DF);
67 
68  HWREG16(address) |= (param->groupEnable | param->conversionMode | param->oversampleRatio | param->dataFormat);
69 
70  // Getting correct CTSD16INCTLx register
71  address += 0x2;
72 
73  HWREG16(address) &= ~(CTSD16RRI | CTSD16INTDLY | CTSD16GAIN2 |
74  CTSD16GAIN1 | CTSD16GAIN0 | CTSD16INCH4 | CTSD16INCH3 | CTSD16INCH2 |
75  CTSD16INCH1 | CTSD16INCH0);
76 
77  HWREG16(address) |= (param->railToRailInput | param->interruptDelay |
78  param->gain | param->inputChannel);
79 
80 }
81 
82 void CTSD16_startConverterConversion(uint16_t baseAddress,
83  uint8_t converter)
84 {
85  uint16_t address;
86 
87  address = baseAddress + (OFS_CTSD16CCTL0 + (converter * 0x06));
88 
89  // Setting CTSD16SC bit to start conversion
90  HWREG16(address) |= CTSD16SC;
91 }
92 
93 void CTSD16_stopConverterConversion(uint16_t baseAddress,
94  uint8_t converter)
95 {
96  uint16_t address;
97 
98  address = baseAddress + (OFS_CTSD16CCTL0 + (converter * 0x06));
99 
100  // Setting CTSD16SC bit to start conversion
101  HWREG16(address) &= ~(CTSD16SC);
102 }
103 
104 void CTSD16_setConverterDataFormat(uint16_t baseAddress,
105  uint8_t converter,
106  uint8_t dataFormat)
107 {
108  uint16_t address;
109 
110  address = baseAddress + (OFS_CTSD16CCTL0 + (converter * 0x06));
111 
112  // Clearing previous settings for configuration
113  HWREG8(address) &= ~CTSD16DF;
114 
115  HWREG8(address) |= dataFormat;
116 }
117 
118 void CTSD16_setInputChannel(uint16_t baseAddress,
119  uint8_t converter,
120  uint8_t inputChannel)
121 {
122  uint16_t address;
123 
124  address = baseAddress + (OFS_CTSD16INCTL0 + (converter * 0x06));
125 
126  // Clear previous settings
127  HWREG16(address) &= ~(CTSD16INCH4 | CTSD16INCH3 | CTSD16INCH2 |
128  CTSD16INCH1 | CTSD16INCH0);
129 
130  HWREG16(address) |= inputChannel;
131 }
132 
133 void CTSD16_setRailToRailInput(uint16_t baseAddress,
134  uint8_t converter,
135  uint16_t railToRailInput)
136 {
137  uint16_t address;
138 
139  address = baseAddress + (OFS_CTSD16INCTL0 + (converter * 0x06));
140 
141  // Clear previous settings
142  HWREG16(address) &= ~CTSD16RRI;
143 
144  HWREG16(address) |= railToRailInput;
145 }
146 
147 void CTSD16_setInterruptDelay(uint16_t baseAddress,
148  uint8_t converter,
149  uint16_t interruptDelay)
150 {
151  uint16_t address;
152 
153  address = baseAddress + (OFS_CTSD16INCTL0 + (converter * 0x06));
154 
155  // Clear previous settings
156  HWREG16(address) &= ~CTSD16INTDLY;
157 
158  HWREG16(address) |= interruptDelay;
159 
160 }
161 
162 void CTSD16_setOversampling(uint16_t baseAddress,
163  uint8_t converter,
164  uint16_t oversampleRatio)
165 {
166  uint16_t address;
167 
168  address = baseAddress + (OFS_CTSD16CCTL0 + (converter * 0x06));
169 
170  // Setting CTSD16SC bit to start conversion
171  HWREG16(address) |= oversampleRatio;
172 }
173 
174 void CTSD16_setGain(uint16_t baseAddress,
175  uint8_t converter,
176  uint8_t gain)
177 {
178  uint16_t address;
179 
180  address = baseAddress + (OFS_CTSD16INCTL0 + (converter * 0x06));
181 
182  // Clear previous settings
183  HWREG16(address) &= ~(CTSD16GAIN2 | CTSD16GAIN1 | CTSD16GAIN0);
184 
185  HWREG16(address) |= gain;
186 }
187 
188 uint32_t CTSD16_getResults(uint16_t baseAddress,
189  uint8_t converter)
190 {
191  volatile uint16_t OSR;
192 
193  uint16_t address = baseAddress + (OFS_CTSD16MEM0 + (converter * 0x02));
194 
195  // Get high word result
196  HWREG16(baseAddress + (OFS_CTSD16CCTL0 + (converter * 0x02))) &= ~(CTSD16LSBACC);
197  uint32_t highResult = (uint32_t)HWREG16(address);
198 
199  // Get low word result
200  HWREG16(baseAddress + (OFS_CTSD16CCTL0 + (converter * 0x02))) |= CTSD16LSBACC;
201  uint16_t lowResult = HWREG16(address);
202  HWREG16(baseAddress + (OFS_CTSD16CCTL0 + (converter * 0x02))) &= ~(CTSD16LSBACC);
203 
204  // Determine the OSR and combine the high and low result words as appropriate
205  OSR = HWREG16(baseAddress + (OFS_CTSD16CCTL0 + (converter * 0x02))) & (CTSD16OSR0 | CTSD16OSR1);
206 
207  if(OSR == CTSD16_OVERSAMPLE_256)
208  {
209  return (highResult << 8) | lowResult;
210  }
211  else if(OSR == CTSD16_OVERSAMPLE_128)
212  {
213  return (highResult << 5) | lowResult;
214  }
215  else if(OSR == CTSD16_OVERSAMPLE_64)
216  {
217  return (highResult << 2) | lowResult;
218  }
219  else // OSR = CTSD16_OVERSAMPLE_32
220  {
221  return (highResult);
222  }
223 }
224 
225 uint16_t CTSD16_getHighWordResults(uint16_t baseAddress,
226  uint8_t converter)
227 {
228  // Calculate address of MEM results
229  uint16_t address = baseAddress + (OFS_CTSD16MEM0 + (converter * 0x02));
230 
231  // Get high word result
232  HWREG16(baseAddress + (OFS_CTSD16CCTL0 + (converter * 0x02))) &= ~(CTSD16LSBACC);
233  uint16_t highResult = HWREG16(address);
234 
235  return(highResult);
236 }
237 
238 uint16_t CTSD16_isRailToRailInputReady(uint16_t baseAddress)
239 {
240  return HWREG16(baseAddress + OFS_CTSD16CTL) & CTSD16RRIRDY;
241 }
242 
243 void CTSD16_enableInterrupt (uint16_t baseAddress,
244  uint8_t converter,
245  uint16_t mask)
246 {
247  //Enable Interrupt
248  HWREG16(baseAddress + OFS_CTSD16IE) |= (mask << converter);
249 
250 }
251 
252 void CTSD16_disableInterrupt (uint16_t baseAddress,
253  uint8_t converter,
254  uint16_t mask)
255 {
256  //Enable Interrupt
257  HWREG16(baseAddress + OFS_CTSD16IE) &= ~(mask << converter);
258 
259 }
260 
261 void CTSD16_clearInterrupt (uint16_t baseAddress,
262  uint8_t converter,
263  uint16_t mask)
264 {
265  HWREG16(baseAddress + OFS_CTSD16IFG) &= ~(mask << converter);
266 }
267 
268 uint16_t CTSD16_getInterruptStatus (uint16_t baseAddress,
269  uint8_t converter,
270  uint16_t mask)
271 {
272  return ( HWREG16(baseAddress + OFS_CTSD16IFG) & (mask << converter) );
273 }
274 
275 #endif
276 //*****************************************************************************
277 //
280 //
281 //*****************************************************************************
MPU_initThreeSegmentsParam param
#define HWREG8(x)
Definition: hw_memmap.h:41
#define HWREG16(x)
Definition: hw_memmap.h:39