summaryrefslogtreecommitdiff
path: root/msp340/Timers/driverlib/MSP430FR5xx_6xx/comp_e.c
blob: ce3e0551e1797c3b8a2ef3811015a7bad9425729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* --COPYRIGHT--,BSD
 * Copyright (c) 2014, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * --/COPYRIGHT--*/
//*****************************************************************************
//
// comp_e.c - Driver for the comp_e Module.
//
//*****************************************************************************

//*****************************************************************************
//
//! \addtogroup comp_e_api comp_e
//! @{
//
//*****************************************************************************

#include "inc/hw_regaccess.h"
#include "inc/hw_memmap.h"

#ifdef __MSP430_HAS_COMP_E__
#include "comp_e.h"

#include <assert.h>

static uint16_t __getRegisterSettingForInput(uint32_t input)
{
    switch(input)
    {
    case COMP_E_INPUT0:
        return(CEIPSEL_0);
    case COMP_E_INPUT1:
        return(CEIPSEL_1);
    case COMP_E_INPUT2:
        return(CEIPSEL_2);
    case COMP_E_INPUT3:
        return(CEIPSEL_3);
    case COMP_E_INPUT4:
        return(CEIPSEL_4);
    case COMP_E_INPUT5:
        return(CEIPSEL_5);
    case COMP_E_INPUT6:
        return(CEIPSEL_6);
    case COMP_E_INPUT7:
        return(CEIPSEL_7);
    case COMP_E_INPUT8:
        return(CEIPSEL_8);
    case COMP_E_INPUT9:
        return(CEIPSEL_9);
    case COMP_E_INPUT10:
        return(CEIPSEL_10);
    case COMP_E_INPUT11:
        return(CEIPSEL_11);
    case COMP_E_INPUT12:
        return(CEIPSEL_12);
    case COMP_E_INPUT13:
        return(CEIPSEL_13);
    case COMP_E_INPUT14:
        return(CEIPSEL_14);
    case COMP_E_INPUT15:
        return(CEIPSEL_15);
    case COMP_E_VREF:
        return(COMP_E_VREF);
    default:
        return(0x11);
    }
}

bool Comp_E_init(uint16_t baseAddress,
                 Comp_E_initParam *param)
{
    uint8_t positiveTerminalInput = __getRegisterSettingForInput(
        param->posTerminalInput);
    uint8_t negativeTerminalInput = __getRegisterSettingForInput(
        param->negTerminalInput);
    bool retVal = STATUS_SUCCESS;

    //Reset COMPE Control 1 & Interrupt Registers for initialization (OFS_CECTL3
    //is not reset because it controls the input buffers of the analog signals
    //and may cause parasitic effects if an analog signal is still attached and
    //the buffer is re-enabled
    HWREG16(baseAddress + OFS_CECTL0) &= 0x0000;
    HWREG16(baseAddress + OFS_CEINT) &= 0x0000;

    //Set the Positive Terminal
    if(COMP_E_VREF != positiveTerminalInput)
    {
        //Enable Positive Terminal Input Mux and Set it to the appropriate input
        HWREG16(baseAddress + OFS_CECTL0) |= CEIPEN + positiveTerminalInput;

        //Disable the input buffer
        HWREG16(baseAddress + OFS_CECTL3) |= (1 << positiveTerminalInput);
    }
    else
    {
        //Reset and Set COMPE Control 2 Register
        HWREG16(baseAddress + OFS_CECTL2) &= ~(CERSEL); //Set Vref to go to (+)terminal
    }

    //Set the Negative Terminal
    if(COMP_E_VREF != negativeTerminalInput)
    {
        //Enable Negative Terminal Input Mux and Set it to the appropriate input
        HWREG16(baseAddress +
                OFS_CECTL0) |= CEIMEN + (negativeTerminalInput << 8);

        //Disable the input buffer
        HWREG16(baseAddress + OFS_CECTL3) |= (1 << negativeTerminalInput);
    }
    else
    {
        //Reset and Set COMPE Control 2 Register
        HWREG16(baseAddress + OFS_CECTL2) |= CERSEL; //Set Vref to go to (-) terminal
    }

    //Reset and Set COMPE Control 1 Register
    HWREG16(baseAddress + OFS_CECTL1) =
        +param->outputFilterEnableAndDelayLevel  //Set the filter enable bit and delay
        + param->invertedOutputPolarity; //Set the polarity of the output

    return (retVal);
}

void Comp_E_setReferenceVoltage(uint16_t baseAddress,
                                uint16_t supplyVoltageReferenceBase,
                                uint16_t lowerLimitSupplyVoltageFractionOf32,
                                uint16_t upperLimitSupplyVoltageFractionOf32)
{
    HWREG16(baseAddress + OFS_CECTL1) &= ~(CEMRVS); //Set to VREF0

    //Reset COMPE Control 2 Bits (Except for CERSEL which is set in Comp_Init() )
    HWREG16(baseAddress + OFS_CECTL2) &= CERSEL;

    //Set Voltage Source (Vcc | Vref, resistor ladder or not)
    if(COMP_E_REFERENCE_AMPLIFIER_DISABLED == supplyVoltageReferenceBase)
    {
        HWREG16(baseAddress + OFS_CECTL2) |= CERS_1; //Vcc with resistor ladder
    }
    else if(lowerLimitSupplyVoltageFractionOf32 == 32)
    {
        //If the lower limit is 32, then the upper limit has to be 32 due to the
        //assertion that upper must be >= to the lower limit. If the numerator is
        //equal to 32, then the equation would be 32/32 == 1, therefore no resistor
        //ladder is needed
        HWREG16(baseAddress + OFS_CECTL2) |= CERS_3; //Vref, no resistor ladder
    }
    else
    {
        HWREG16(baseAddress + OFS_CECTL2) |= CERS_2; //Vref with resistor ladder
    }

    //Set COMPE Control 2 Register
    HWREG16(baseAddress + OFS_CECTL2) |=
        supplyVoltageReferenceBase //Set Supply Voltage Base
        + ((upperLimitSupplyVoltageFractionOf32 - 1) << 8) //Set Supply Voltage Num.
        + (lowerLimitSupplyVoltageFractionOf32 - 1);
}

void Comp_E_setReferenceAccuracy(uint16_t baseAddress,
                                 uint16_t referenceAccuracy)
{
    HWREG16(baseAddress + OFS_CECTL2) &= ~(CEREFACC);
    HWREG16(baseAddress + OFS_CECTL2) |= referenceAccuracy;
}

void Comp_E_setPowerMode(uint16_t baseAddress,
                         uint16_t powerMode)
{
    HWREG16(baseAddress +
            OFS_CECTL1) &= ~(COMP_E_NORMAL_MODE | COMP_E_ULTRA_LOW_POWER_MODE);
    HWREG16(baseAddress + OFS_CECTL1) |= powerMode;
}

void Comp_E_enableInterrupt(uint16_t baseAddress,
                            uint16_t interruptMask)
{
    //Set the Interrupt enable bit
    HWREG16(baseAddress + OFS_CEINT) |= interruptMask;
}

void Comp_E_disableInterrupt(uint16_t baseAddress,
                             uint16_t interruptMask)
{
    HWREG16(baseAddress + OFS_CEINT) &= ~(interruptMask);
}

void Comp_E_clearInterrupt(uint16_t baseAddress,
                           uint16_t interruptFlagMask)
{
    HWREG16(baseAddress + OFS_CEINT) &= ~(interruptFlagMask);
}

uint8_t Comp_E_getInterruptStatus(uint16_t baseAddress,
                                  uint16_t interruptFlagMask)
{
    return (HWREG16(baseAddress + OFS_CEINT) & interruptFlagMask);
}

void Comp_E_setInterruptEdgeDirection(uint16_t baseAddress,
                                      uint16_t edgeDirection)
{
    //Set the edge direction that will trigger an interrupt
    if(COMP_E_RISINGEDGE == edgeDirection)
    {
        HWREG16(baseAddress + OFS_CECTL1) |= CEIES;
    }
    else if(COMP_E_FALLINGEDGE == edgeDirection)
    {
        HWREG16(baseAddress + OFS_CECTL1) &= ~(CEIES);
    }
}

void Comp_E_toggleInterruptEdgeDirection(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_CECTL1) ^= CEIES;
}

void Comp_E_enable(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_CECTL1) |= CEON;
}

void Comp_E_disable(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_CECTL1) &= ~(CEON);
}

void Comp_E_shortInputs(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_CECTL1) |= CESHORT;
}

void Comp_E_unshortInputs(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_CECTL1) &= ~(CESHORT);
}

void Comp_E_disableInputBuffer(uint16_t baseAddress,
                               uint16_t inputPort)
{
    HWREG16(baseAddress + OFS_CECTL3) |= (inputPort);
}

void Comp_E_enableInputBuffer(uint16_t baseAddress,
                              uint16_t inputPort)
{
    HWREG16(baseAddress + OFS_CECTL3) &= ~(inputPort);
}

void Comp_E_swapIO(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_CECTL1) ^= CEEX; //Toggle CEEX bit
}

uint16_t Comp_E_outputValue(uint16_t baseAddress)
{
    return (HWREG16(baseAddress + OFS_CECTL1) & CEOUT);
}

#endif
//*****************************************************************************
//
//! Close the doxygen group for comp_e_api
//! @}
//
//*****************************************************************************