2020-2021 Sunseeker Telemetry and Lighting System
adc10_a.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // adc10_a.c - Driver for the adc10_a Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_ADC10_A__
17 #include "adc10_a.h"
18 
19 #include <assert.h>
20 
21 bool ADC10_A_init (uint16_t baseAddress,
22  uint16_t sampleHoldSignalSourceSelect,
23  uint8_t clockSourceSelect,
24  uint16_t clockSourceDivider)
25 {
26  bool retVal = STATUS_SUCCESS;
27 
28  //Turn OFF ADC10_A Module & Clear Interrupt Registers
29  HWREG16(baseAddress + OFS_ADC10IFG) &= 0x0000; //Reset ALL interrupt flags
30 
31  //Set ADC10_A Control 1
32  HWREG16(baseAddress + OFS_ADC10CTL1) =
33  sampleHoldSignalSourceSelect //Setup the Sample-and-Hold Source
34  + (clockSourceDivider & ADC10DIV_7) //Set Clock Divider
35  + clockSourceSelect; //Setup Clock Source
36 
37  //Set ADC10_A Control 2
38  HWREG16(baseAddress + OFS_ADC10CTL2) =
39  (clockSourceDivider & (ADC10PDIV_1 | ADC10PDIV_2)) //Set Clock Pre-Divider
40  + ADC10RES; //Default resolution to 10-bits
41 
42  return ( retVal) ;
43 }
44 
45 void ADC10_A_enable (uint16_t baseAddress)
46 {
47  //Reset the ADC10ON bit to enable the ADC10_A Module
48  HWREG16(baseAddress + OFS_ADC10CTL0) |= ADC10ON;
49 }
50 
51 void ADC10_A_disable (uint16_t baseAddress)
52 {
53  //Set the ADC10ON bit to disable the ADC10_A Module
54  HWREG16(baseAddress + OFS_ADC10CTL0) &= ~ADC10ON;
55 }
56 
57 void ADC10_A_setupSamplingTimer (uint16_t baseAddress,
58  uint16_t clockCycleHoldCount,
59  uint16_t multipleSamplesEnabled)
60 {
61  HWREG16(baseAddress + OFS_ADC10CTL1) |= ADC10SHP;
62 
63  //Reset and Set CB Control 0 Bits
64  HWREG16(baseAddress + OFS_ADC10CTL0) &= ~(ADC10SHT_15 + ADC10MSC);
65  HWREG16(baseAddress + OFS_ADC10CTL0) |= clockCycleHoldCount
66  + multipleSamplesEnabled;
67 }
68 
69 void ADC10_A_disableSamplingTimer (uint16_t baseAddress)
70 {
71  HWREG16(baseAddress + OFS_ADC10CTL1) &= ~(ADC10SHP);
72 }
73 
74 void ADC10_A_configureMemory (uint16_t baseAddress,
75  uint8_t inputSourceSelect,
76  uint8_t positiveRefVoltageSourceSelect,
77  uint8_t negativeRefVoltageSourceSelect)
78 {
79  //Make sure the ENC bit is cleared before configuring a Memory Buffer Control
80  assert( !(HWREG16(baseAddress + OFS_ADC10CTL0) & ADC10ENC) );
81 
82  if(!(HWREG16(baseAddress + OFS_ADC10CTL0) & ADC10ENC))
83  {
84  assert(inputSourceSelect <= ADC10_A_INPUT_A15);
85  assert(positiveRefVoltageSourceSelect <= ADC10_A_VREFPOS_INT);
86  assert(negativeRefVoltageSourceSelect <= ADC10_A_VREFNEG_EXT);
87 
88  //Reset and Set the Memory Buffer Control Bits
89  HWREG8(baseAddress + OFS_ADC10MCTL0) = inputSourceSelect
90  + positiveRefVoltageSourceSelect +
91  negativeRefVoltageSourceSelect;
92  }
93 }
94 
95 void ADC10_A_enableInterrupt (uint16_t baseAddress,
96  uint8_t interruptMask)
97 {
98  HWREG16(baseAddress + OFS_ADC10IE) |= interruptMask;
99 }
100 
101 void ADC10_A_disableInterrupt (uint16_t baseAddress,
102  uint8_t interruptMask)
103 {
104  HWREG16(baseAddress + OFS_ADC10IE) &= ~(interruptMask);
105 }
106 
107 void ADC10_A_clearInterrupt (uint16_t baseAddress,
108  uint8_t interruptFlagMask)
109 {
110  HWREG16(baseAddress + OFS_ADC10IFG) &= ~(interruptFlagMask);
111 }
112 
113 uint16_t ADC10_A_getInterruptStatus (uint16_t baseAddress,
114  uint8_t interruptFlagMask)
115 {
116  return ( HWREG16(baseAddress + OFS_ADC10IFG) & interruptFlagMask );
117 }
118 
119 void ADC10_A_startConversion (uint16_t baseAddress,
120  uint8_t conversionSequenceModeSelect)
121 {
122  //Reset the ENC bit to set the conversion mode sequence
123  HWREG16(baseAddress + OFS_ADC10CTL0) &= ~(ADC10ENC);
124 
125  HWREG16(baseAddress + OFS_ADC10CTL1) |= conversionSequenceModeSelect;
126  HWREG16(baseAddress + OFS_ADC10CTL0) |= ADC10ENC + ADC10SC;
127 }
128 
129 void ADC10_A_disableConversions (uint16_t baseAddress, bool preempt)
130 {
131  if (ADC10_A_PREEMPTCONVERSION == preempt){
132  HWREG16(baseAddress + OFS_ADC10CTL1) &= ~(ADC10CONSEQ_3);
133  //Reset conversion sequence mode to single-channel, single-conversion
134  } else if ( ~(HWREG16(baseAddress + OFS_ADC10CTL1) & ADC10CONSEQ_3) ){
135  //To prevent preemoption of a single-channel, single-conversion we must
136  //wait for the ADC core to finish the conversion.
137  while (HWREG16(baseAddress + OFS_ADC10CTL1) & ADC10BUSY) ;
138  }
139 
140  HWREG16(baseAddress + OFS_ADC10CTL0) &= ~(ADC10ENC);
141 }
142 
143 int16_t ADC10_A_getResults (uint16_t baseAddress)
144 {
145  return ( HWREG16(baseAddress + OFS_ADC10MEM0) );
146 }
147 
148 void ADC10_A_setResolution (uint16_t baseAddress,
149  uint8_t resolutionSelect)
150 {
151  HWREG16(baseAddress + OFS_ADC10CTL2) &= ~(ADC10RES);
152  HWREG16(baseAddress + OFS_ADC10CTL2) |= resolutionSelect;
153 }
154 
155 void ADC10_A_setSampleHoldSignalInversion (uint16_t baseAddress,
156  uint16_t invertedSignal)
157 {
158  HWREG16(baseAddress + OFS_ADC10CTL1) &= ~(ADC10ISSH);
159  HWREG16(baseAddress + OFS_ADC10CTL1) |= invertedSignal;
160 }
161 
162 void ADC10_A_setDataReadBackFormat (uint16_t baseAddress,
163  uint16_t readBackFormat)
164 {
165  HWREG16(baseAddress + OFS_ADC10CTL2) &= ~(ADC10DF);
166  HWREG16(baseAddress + OFS_ADC10CTL2) |= readBackFormat;
167 }
168 
169 void ADC10_A_enableReferenceBurst (uint16_t baseAddress)
170 {
171  HWREG16(baseAddress + OFS_ADC10CTL2) |= ADC10REFBURST;
172 }
173 
174 void ADC10_A_disableReferenceBurst (uint16_t baseAddress)
175 {
176  HWREG16(baseAddress + OFS_ADC10CTL2) &= ~(ADC10REFBURST);
177 }
178 
179 void ADC10_A_setReferenceBufferSamplingRate (uint16_t baseAddress,
180  uint16_t samplingRateSelect)
181 {
182  HWREG16(baseAddress + OFS_ADC10CTL2) &= ~(ADC10SR);
183  HWREG16(baseAddress + OFS_ADC10CTL2) |= samplingRateSelect;
184 }
185 
186 void ADC10_A_setWindowComp (uint16_t baseAddress,
187  uint16_t highThreshold,
188  uint16_t lowThreshold)
189 {
190  HWREG16(baseAddress + OFS_ADC10HI) = highThreshold;
191  HWREG16(baseAddress + OFS_ADC10LO) = lowThreshold;
192 }
193 
194 uint32_t ADC10_A_getMemoryAddressForDMA (uint16_t baseAddress)
195 {
196  return ( baseAddress + OFS_ADC10MEM0 );
197 }
198 
199 uint16_t ADC10_A_isBusy (uint16_t baseAddress)
200 {
201  return (HWREG16(baseAddress + OFS_ADC10CTL1) & ADC10BUSY);
202 }
203 
204 #endif
205 //*****************************************************************************
206 //
209 //
210 //*****************************************************************************
#define HWREG8(x)
Definition: hw_memmap.h:41
#define HWREG16(x)
Definition: hw_memmap.h:39
#define STATUS_SUCCESS
Definition: hw_memmap.h:22