2020-2021 Sunseeker Telemetry and Lighting System
comp_b.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // comp_b.c - Driver for the comp_b Module.
4 //
5 //*****************************************************************************
6 
7 //*****************************************************************************
8 //
11 //
12 //*****************************************************************************
13 
14 #include "inc/hw_memmap.h"
15 
16 #ifdef __MSP430_HAS_COMPB__
17 #include "comp_b.h"
18 
19 #include <assert.h>
20 
21 bool Comp_B_init(uint16_t baseAddress, Comp_B_initParam *param)
22 {
23  bool retVal = STATUS_SUCCESS;
24 
25  //Reset COMPB Control 1 & Interrupt Registers for initialization (OFS_CBCTL3
26  //is not reset because it controls the input buffers of the analog signals
27  //and may cause parasitic effects if an analog signal is still attached and
28  //the buffer is re-enabled
29  HWREG16(baseAddress + OFS_CBCTL0) &= 0x0000;
30  HWREG16(baseAddress + OFS_CBINT) &= 0x0000;
31 
32  //Clear reference voltage and reference source
33  HWREG16(baseAddress + OFS_CBCTL2) &= ~(CBRS_3 | CBREFL_3);
34 
35  //Set the Positive Terminal
36  if(COMP_B_VREF != param->positiveTerminalInput) {
37  //Enable Positive Terminal Input Mux and Set it to the appropriate input
38  HWREG16(baseAddress + OFS_CBCTL0) |= CBIPEN + param->positiveTerminalInput;
39 
40  //Disable the input buffer
41  HWREG16(baseAddress + OFS_CBCTL3) |= (1 << param->positiveTerminalInput);
42  }
43  else {
44  //Reset and Set COMPB Control 2 Register
45  //Set Vref to go to (+)terminal
46  HWREG16(baseAddress + OFS_CBCTL2) &= ~(CBRSEL);
47  }
48 
49  //Set the Negative Terminal
50  if (COMP_B_VREF != param->negativeTerminalInput) {
51  //Enable Negative Terminal Input Mux and Set it to the appropriate input
52  HWREG16(baseAddress + OFS_CBCTL0) |= CBIMEN + (param->negativeTerminalInput << 8);
53 
54  //Disable the input buffer
55  HWREG16(baseAddress + OFS_CBCTL3) |= (1 << param->negativeTerminalInput);
56  }
57  else {
58  //Reset and Set COMPB Control 2 Register
59  //Set Vref to go to (-) terminal
60  HWREG16(baseAddress + OFS_CBCTL2) |= CBRSEL;
61  }
62 
63  //Reset and Set COMPB Control 1 Register
64  HWREG16(baseAddress + OFS_CBCTL1) =
65  param->powerModeSelect //Set the power mode
66  + param->outputFilterEnableAndDelayLevel //Set the filter enable bit and delay
67  + param->invertedOutputPolarity; //Set the polarity of the output
68 
69  return (retVal);
70 }
71 
72 void Comp_B_configureReferenceVoltage(uint16_t baseAddress,
73  Comp_B_configureReferenceVoltageParam *param)
74 {
75  //Set to VREF0
76  HWREG16(baseAddress + OFS_CBCTL1) &= ~(CBMRVS);
77 
78  //Reset COMPB Control 2 Bits (Except for CBRSEL which is set in Comp_Init())
79  HWREG16(baseAddress + OFS_CBCTL2) &= CBRSEL;
80 
81  //Set Voltage Source (Vcc | Vref, resistor ladder or not)
82  if (COMP_B_VREFBASE_VCC == param->supplyVoltageReferenceBase) {
83  HWREG16(baseAddress + OFS_CBCTL2) |= CBRS_1; //Vcc with resistor ladder
84  }
85  else if (param->lowerLimitSupplyVoltageFractionOf32 == 32) {
86  //If the lower limit is 32, then the upper limit has to be 32 due to the
87  //assertion that upper must be >= to the lower limit. If the numerator is
88  //equal to 32, then the equation would be 32/32 == 1, therefore no resistor
89  //ladder is needed
90  HWREG16(baseAddress + OFS_CBCTL2) |= CBRS_3; //Vref, no resistor ladder
91  }
92  else {
93  HWREG16(baseAddress + OFS_CBCTL2) |= CBRS_2; //Vref with resistor ladder
94  }
95 
96  //Set COMPD Control 2 Register
97  HWREG16(baseAddress + OFS_CBCTL2) |=
98  param->supplyVoltageReferenceBase //Set Supply Voltage Base
99  + ((param->upperLimitSupplyVoltageFractionOf32 - 1) << 8) //Set Supply Voltage Num.
100  + (param->lowerLimitSupplyVoltageFractionOf32 - 1);
101 
102  HWREG16(baseAddress + OFS_CBCTL2) &= ~(CBREFACC);
103  HWREG16(baseAddress + OFS_CBCTL2) |= param->referenceAccuracy;
104 }
105 
106 void Comp_B_enableInterrupt(uint16_t baseAddress,
107  uint16_t interruptMask)
108 {
109  //Set the Interrupt enable bit
110  HWREG16(baseAddress + OFS_CBINT) |= interruptMask;
111 }
112 
113 void Comp_B_disableInterrupt(uint16_t baseAddress,
114  uint16_t interruptMask)
115 {
116  HWREG16(baseAddress + OFS_CBINT) &= ~(interruptMask);
117 }
118 
119 void Comp_B_clearInterrupt(uint16_t baseAddress,
120  uint16_t interruptFlagMask)
121 {
122  HWREG16(baseAddress + OFS_CBINT) &= ~(interruptFlagMask);
123 }
124 
125 uint8_t Comp_B_getInterruptStatus(uint16_t baseAddress,
126  uint16_t interruptFlagMask)
127 {
128  return(HWREG16(baseAddress + OFS_CBINT) & interruptFlagMask);
129 }
130 
131 void Comp_B_setInterruptEdgeDirection(uint16_t baseAddress,
132  uint16_t edgeDirection)
133 {
134  //Set the edge direction that will trigger an interrupt
135  if(COMP_B_RISINGEDGE == edgeDirection) {
136  HWREG16(baseAddress + OFS_CBCTL1) &= ~(CBIES);
137  }
138  else if(COMP_B_FALLINGEDGE == edgeDirection) {
139  HWREG16(baseAddress + OFS_CBCTL1) |= CBIES;
140  }
141 }
142 
143 void Comp_B_toggleInterruptEdgeDirection(uint16_t baseAddress)
144 {
145  HWREG16(baseAddress + OFS_CBCTL1) ^= CBIES;
146 }
147 
148 void Comp_B_enable(uint16_t baseAddress)
149 {
150  HWREG16(baseAddress + OFS_CBCTL1) |= CBON;
151 }
152 
153 void Comp_B_disable(uint16_t baseAddress)
154 {
155  HWREG16(baseAddress + OFS_CBCTL1) &= ~(CBON);
156 }
157 
158 void Comp_B_shortInputs(uint16_t baseAddress)
159 {
160  HWREG16(baseAddress + OFS_CBCTL1) |= CBSHORT;
161 }
162 
163 void Comp_B_unshortInputs(uint16_t baseAddress)
164 {
165  HWREG16(baseAddress + OFS_CBCTL1) &= ~(CBSHORT);
166 }
167 
168 void Comp_B_disableInputBuffer(uint16_t baseAddress,
169  uint8_t inputPort)
170 {
171  HWREG16(baseAddress + OFS_CBCTL3) |= (1 << inputPort);
172 }
173 
174 void Comp_B_enableInputBuffer(uint16_t baseAddress,
175  uint8_t inputPort)
176 {
177  HWREG16(baseAddress + OFS_CBCTL3) &= ~(1 << inputPort);
178 }
179 
180 void Comp_B_swapIO(uint16_t baseAddress)
181 {
182  HWREG16(baseAddress + OFS_CBCTL1) ^= CBEX;
183 }
184 
185 uint16_t Comp_B_outputValue(uint16_t baseAddress)
186 {
187  return (HWREG16(baseAddress + OFS_CBCTL1) & CBOUT);
188 
189 }
190 
191 void Comp_B_selectReferenceVoltage(uint16_t baseAddress, uint16_t selectType,
192  uint16_t selectVRef)
193 {
194  HWREG16(baseAddress + OFS_CBCTL1) &= ~(CBMRVS | CBMRVL);
195  if(selectType == COMP_B_VREF_MANUAL_SELECT) {
196  HWREG16(baseAddress + OFS_CBCTL1) |= CBMRVS;
197  if(selectVRef == COMP_B_SELECT_VREF1) {
198  HWREG16(baseAddress + OFS_CBCTL1) |= CBMRVL;
199  }
200  }
201 }
202 
203 
204 #endif
205 //*****************************************************************************
206 //
209 //
210 //*****************************************************************************
#define HWREG16(x)
Definition: hw_memmap.h:39
#define STATUS_SUCCESS
Definition: hw_memmap.h:22