2020-2021 Sunseeker Telemetry and Lighting System
dac12_a.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // dac12_a.c - Driver for the dac12_a Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_DAC12_2__
17 #include "dac12_a.h"
18 
19 #include <assert.h>
20 
21 bool DAC12_A_init(uint16_t baseAddress, DAC12_A_initParam *param)
22 {
23  baseAddress += param->submoduleSelect; //Add 0x10 to base address IF
24  //DAC12_A_1 is selected.
25  HWREG16(baseAddress + OFS_DAC12_0CTL1) &= ~(DAC12OG + DAC12DFJ);
26 
27  //Reset and Set DAC12_A Control 0 Bits
28  HWREG16(baseAddress + OFS_DAC12_0CTL0) = param->outputSelect
29  + param->positiveReferenceVoltage
30  + param->amplifierSetting
31  + param->conversionTriggerSelect;
32 
33  if (DAC12_A_VREFx1 == param->outputVoltageMultiplier){
34  HWREG16(baseAddress + OFS_DAC12_0CTL0) |= DAC12IR;
35  } else if (DAC12_A_VREFx2 == param->outputVoltageMultiplier){
36  HWREG16(baseAddress + OFS_DAC12_0CTL1) |= DAC12OG;
37  }
38  //else if(DAC12_A_VREFx3 == outputVoltageMultiplier)
39  //Both DAC12IR and DAC12OG values == 0
40 
41  return ( STATUS_SUCCESS) ;
42 }void DAC12_A_setAmplifierSetting (uint16_t baseAddress,
43  uint8_t submoduleSelect,
44  uint8_t amplifierSetting)
45 {
46  //Reset amplifier setting to set it
47  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CTL0) &= ~(DAC12AMP_7);
48  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CTL0) |= amplifierSetting;
49 }
50 
51 void DAC12_A_disable (uint16_t baseAddress,
52  uint8_t submoduleSelect)
53 {
54  //Reset amplifier setting to turn DAC12_A off completely
55  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CTL0) &= ~(DAC12AMP_7);
56 }
57 
58 void DAC12_A_enableGrouping (uint16_t baseAddress)
59 {
60  HWREG16(baseAddress + OFS_DAC12_0CTL0) |= DAC12GRP;
61 }
62 
63 void DAC12_A_disableGrouping (uint16_t baseAddress)
64 {
65  HWREG16(baseAddress + OFS_DAC12_0CTL0) &= ~(DAC12GRP);
66 }
67 
68 void DAC12_A_enableInterrupt (uint16_t baseAddress,
69  uint8_t submoduleSelect)
70 {
71  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CTL0) |= DAC12IE;
72 }
73 
74 void DAC12_A_disableInterrupt (uint16_t baseAddress,
75  uint8_t submoduleSelect)
76 {
77  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CTL0) &= ~(DAC12IE);
78 }
79 
80 uint16_t DAC12_A_getInterruptStatus (uint16_t baseAddress,
81  uint8_t submoduleSelect)
82 {
83  return (HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CTL0) & DAC12IFG);
84 }
85 
86 void DAC12_A_clearInterrupt (uint16_t baseAddress,
87  uint8_t submoduleSelect)
88 {
89  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CTL0) &= ~(DAC12IFG);
90 }
91 
92 void DAC12_A_calibrateOutput (uint16_t baseAddress,
93  uint8_t submoduleSelect)
94 {
95  //Unlock Calibration
96  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CALCTL) = DAC12PW;
97 
98  //Start Calibration
99  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CTL0) |= DAC12CALON;
100 
101  //Wait for Calibration to Finish
102  while (HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CTL0) & DAC12CALON);
103 
104  //Lock Calibration
105  HWREG16(baseAddress + submoduleSelect +
106  OFS_DAC12_0CALCTL) = DAC12PW + DAC12LOCK;
107 }
108 
109 uint16_t DAC12_A_getCalibrationData (uint16_t baseAddress,
110  uint8_t submoduleSelect)
111 {
112  return ((uint16_t)(HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CALDAT))) ;
113 }
114 
115 void DAC12_A_setCalibrationOffset (uint16_t baseAddress,
116  uint8_t submoduleSelect,
117  uint16_t calibrationOffsetValue)
118 {
119  //Unlock Calibration
120  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CALCTL) = DAC12PW;
121 
122  //Set Calibration Offset
123  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CALDAT) =
124  calibrationOffsetValue;
125 
126  //Lock Calibration
127  HWREG16(baseAddress + submoduleSelect +
128  OFS_DAC12_0CALCTL) = DAC12PW + DAC12LOCK;
129 }
130 
131 void DAC12_A_enableConversions (uint16_t baseAddress,
132  uint8_t submoduleSelect)
133 {
134  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CTL0) |= DAC12ENC;
135 }
136 
137 void DAC12_A_setData (uint16_t baseAddress,
138  uint8_t submoduleSelect,
139  uint16_t data)
140 {
141  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0DAT) = data;
142 }
143 
144 void DAC12_A_disableConversions (uint16_t baseAddress,
145  uint8_t submoduleSelect)
146 {
147  HWREG16(baseAddress + submoduleSelect + OFS_DAC12_0CTL0) &= ~(DAC12ENC);
148 }
149 
150 void DAC12_A_setResolution (uint16_t baseAddress,
151  uint8_t submoduleSelect,
152  uint16_t resolutionSelect)
153 {
154  //Store the ENC bit status
155  uint16_t conversionsEnabledStatus =
156  ( HWREG16(baseAddress + OFS_DAC12_0CTL0) & (DAC12ENC) );
157 
158  baseAddress += submoduleSelect; //Add 0x10 to base address IF
159  //DAC12_A_1 is selected.
160 
161  if (DAC12_A_RESOLUTION_8BIT == resolutionSelect){
162  HWREG16(baseAddress + OFS_DAC12_0CTL0) |= DAC12RES;
163  } else if (DAC12_A_RESOLUTION_12BIT == resolutionSelect){
164  HWREG16(baseAddress + OFS_DAC12_0CTL0) &= ~(DAC12RES);
165  }
166 
167  //Restore the ENC bit status
168  HWREG16(baseAddress + OFS_DAC12_0CTL0) |= conversionsEnabledStatus;
169 }
170 
171 void DAC12_A_setInputDataFormat (uint16_t baseAddress,
172  uint8_t submoduleSelect,
173  uint8_t inputJustification,
174  uint8_t inputSign)
175 {
176  //Store the ENC bit status
177  uint16_t conversionsEnabledStatus =
178  ( HWREG16(baseAddress + OFS_DAC12_0CTL0) & (DAC12ENC) );
179 
180  baseAddress += submoduleSelect; //Add 0x10 to base address IF
181  //DAC12_A_1 is selected.
182 
183  if (DAC12_A_JUSTIFICATION_LEFT == inputJustification){
184  HWREG16(baseAddress + OFS_DAC12_0CTL1) |= DAC12DFJ;
185  } else if (DAC12_A_JUSTIFICATION_RIGHT == inputJustification){
186  HWREG16(baseAddress + OFS_DAC12_0CTL1) &= ~(DAC12DFJ);
187  }
188 
189  if (DAC12_A_SIGNED_2SCOMPLEMENT == inputSign){
190  HWREG16(baseAddress + OFS_DAC12_0CTL0) |= DAC12DF;
191  } else if (DAC12_A_UNSIGNED_BINARY == inputSign){
192  HWREG16(baseAddress + OFS_DAC12_0CTL0) &= ~(DAC12DF);
193  }
194 
195  //Restore the ENC bit status
196  HWREG16(baseAddress + OFS_DAC12_0CTL0) |= conversionsEnabledStatus;
197 }
198 
199 uint32_t DAC12_A_getDataBufferMemoryAddressForDMA (uint16_t baseAddress,
200  uint8_t submoduleSelect)
201 {
202  return ( baseAddress + submoduleSelect + OFS_DAC12_0DAT );
203 }
204 
205 
206 #endif
207 //*****************************************************************************
208 //
211 //
212 //*****************************************************************************
MPU_initThreeSegmentsParam param
#define HWREG16(x)
Definition: hw_memmap.h:39
#define STATUS_SUCCESS
Definition: hw_memmap.h:22