summaryrefslogtreecommitdiff
path: root/msp340/Timers/driverlib/MSP430FR5xx_6xx/lcd_c.c
blob: e43aabc4f2fa253c4970fac1785eb026bd997a4f (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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* --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--*/
//*****************************************************************************
//
// lcd_c.c - Driver for the lcd_c Module.
//
//*****************************************************************************

//*****************************************************************************
//
//! \addtogroup lcd_c_api lcd_c
//! @{
//
//*****************************************************************************

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

#ifdef __MSP430_HAS_LCD_C__
#include "lcd_c.h"

#include <assert.h>

//*****************************************************************************
//
// Initialization parameter instance
//
//*****************************************************************************
const LCD_C_initParam LCD_C_INIT_PARAM = {
    LCD_C_CLOCKSOURCE_ACLK,
    LCD_C_CLOCKDIVIDER_1,
    LCD_C_CLOCKPRESCALAR_1,
    LCD_C_STATIC,
    LCD_C_STANDARD_WAVEFORMS,
    LCD_C_SEGMENTS_DISABLED
};

static void setLCDFunction(uint16_t baseAddress,
                           uint8_t index,
                           uint16_t value)
{
    switch(index)
    {
    case 0:
        HWREG16(baseAddress + OFS_LCDCPCTL0) |= value;
        break;
    case 1:
        HWREG16(baseAddress + OFS_LCDCPCTL1) |= value;
        break;
    case 2:
        HWREG16(baseAddress + OFS_LCDCPCTL2) |= value;
        break;
    case 3:
        HWREG16(baseAddress + OFS_LCDCPCTL3) |= value;
        break;
    default: break;
    }
}

void LCD_C_init(uint16_t baseAddress,
                LCD_C_initParam *initParams)
{
    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~LCDON;
    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~(LCDMX0 | LCDMX1 | LCDMX2 | LCDSSEL
                                             | LCDLP | LCDSON | LCDDIV_31);

    HWREG16(baseAddress + OFS_LCDCCTL0) |= initParams->muxRate;
    HWREG16(baseAddress + OFS_LCDCCTL0) |= initParams->clockSource;
    HWREG16(baseAddress + OFS_LCDCCTL0) |= initParams->waveforms;
    HWREG16(baseAddress + OFS_LCDCCTL0) |= initParams->segments;
    HWREG16(baseAddress + OFS_LCDCCTL0) |= initParams->clockDivider;
    HWREG16(baseAddress + OFS_LCDCCTL0) |= initParams->clockPrescalar;
}

void LCD_C_on(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_LCDCCTL0) |= LCDON;
}

void LCD_C_off(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~LCDON;
}

void LCD_C_clearInterrupt(uint16_t baseAddress,
                          uint16_t mask)
{
    HWREG8(baseAddress + OFS_LCDCCTL1_L) &= ~(mask >> 8);
}

uint16_t LCD_C_getInterruptStatus(uint16_t baseAddress,
                                  uint16_t mask)
{
    return (HWREG8(baseAddress + OFS_LCDCCTL1_L) & (mask >> 8));
}

void LCD_C_enableInterrupt(uint16_t baseAddress,
                           uint16_t mask)
{
    HWREG16(baseAddress + OFS_LCDCCTL1) |= mask;
}

void LCD_C_disableInterrupt(uint16_t baseAddress,
                            uint16_t mask)
{
    HWREG16(baseAddress + OFS_LCDCCTL1) &= ~mask;
}

void LCD_C_clearMemory(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_LCDCMEMCTL) |= LCDCLRM;
}

void LCD_C_clearBlinkingMemory(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_LCDCMEMCTL) |= LCDCLRBM;
}

void LCD_C_selectDisplayMemory(uint16_t baseAddress,
                               uint16_t displayMemory)
{
    HWREG16(baseAddress + OFS_LCDCMEMCTL) &= ~LCDDISP;
    HWREG16(baseAddress + OFS_LCDCMEMCTL) |= displayMemory;
}

void LCD_C_setBlinkingControl(uint16_t baseAddress,
                              uint8_t clockDivider,
                              uint8_t clockPrescalar,
                              uint8_t mode)
{
    HWREG16(baseAddress +
            OFS_LCDCBLKCTL) &= ~(LCDBLKDIV0 | LCDBLKDIV1 | LCDBLKDIV2 |
                                 LCDBLKPRE0 | LCDBLKPRE1 |
                                 LCDBLKPRE2 |
                                 LCDBLKMOD0 | LCDBLKMOD1
                                 );
    HWREG16(baseAddress +
            OFS_LCDCBLKCTL) |= clockDivider | clockPrescalar | mode;
}

void LCD_C_enableChargePump(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~LCDON;
    HWREG16(baseAddress + OFS_LCDCVCTL) |= LCDCPEN;
}

void LCD_C_disableChargePump(uint16_t baseAddress)
{
    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~LCDON;
    HWREG16(baseAddress + OFS_LCDCVCTL) &= ~LCDCPEN;
}

void LCD_C_selectBias(uint16_t baseAddress,
                      uint16_t bias)
{
    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~LCDON;
    HWREG16(baseAddress + OFS_LCDCVCTL) &= ~LCD2B;

    HWREG16(baseAddress + OFS_LCDCVCTL) |= bias;
}

void LCD_C_selectChargePumpReference(uint16_t baseAddress,
                                     uint16_t reference)
{
    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~LCDON;
    HWREG16(baseAddress + OFS_LCDCVCTL) &= ~VLCDREF_3;

    HWREG16(baseAddress + OFS_LCDCVCTL) |= reference;
}

void LCD_C_setVLCDSource(uint16_t baseAddress,
                         uint16_t vlcdSource,
                         uint16_t v2v3v4Source,
                         uint16_t v5Source)
{
    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~LCDON;
    HWREG16(baseAddress + OFS_LCDCVCTL) &= ~VLCDEXT;
    HWREG16(baseAddress + OFS_LCDCVCTL) &= ~LCDREXT;
    HWREG16(baseAddress + OFS_LCDCVCTL) &= ~LCDEXTBIAS;
    HWREG16(baseAddress + OFS_LCDCVCTL) &= ~R03EXT;

    HWREG16(baseAddress + OFS_LCDCVCTL) |= vlcdSource;
    HWREG16(baseAddress + OFS_LCDCVCTL) |= v2v3v4Source;
    HWREG16(baseAddress + OFS_LCDCVCTL) |= v5Source;
}

void LCD_C_setVLCDVoltage(uint16_t baseAddress,
                          uint16_t voltage)
{
    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~LCDON;
    HWREG16(baseAddress + OFS_LCDCVCTL) &= ~VLCD_15;

    HWREG16(baseAddress + OFS_LCDCVCTL) |= voltage;
}

void LCD_C_setPinAsLCDFunction(uint16_t baseAddress,
                               uint8_t pin)
{
    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~LCDON;

    uint8_t idx = pin >> 4;
    uint16_t val = 1 << (pin & 0xF);

    setLCDFunction(baseAddress, idx, val);
}

void LCD_C_setPinAsPortFunction(uint16_t baseAddress,
                                uint8_t pin)
{
    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~LCDON;

    uint8_t idx = pin >> 4;
    uint16_t val = 1 << (pin & 0xF);

    switch(idx)
    {
    case 0:
        HWREG16(baseAddress + OFS_LCDCPCTL0) &= ~val;
        break;
    case 1:
        HWREG16(baseAddress + OFS_LCDCPCTL1) &= ~val;
        break;
    case 2:
        HWREG16(baseAddress + OFS_LCDCPCTL2) &= ~val;
        break;
    case 3:
        HWREG16(baseAddress + OFS_LCDCPCTL3) &= ~val;
        break;
    default: break;
    }
}

void LCD_C_setPinAsLCDFunctionEx(uint16_t baseAddress,
                                 uint8_t startPin,
                                 uint8_t endPin)
{
    uint8_t startIdx = startPin >> 4;
    uint8_t endIdx = endPin >> 4;
    uint8_t startPos = startPin & 0xF;
    uint8_t endPos = endPin & 0xF;
    uint16_t val = 0;
    uint8_t i = 0;

    HWREG16(baseAddress + OFS_LCDCCTL0) &= ~LCDON;

    if(startIdx == endIdx)
    {
        val = (0xFFFF >> (15 - endPos)) & (0xFFFF << startPos);

        setLCDFunction(baseAddress, startIdx, val);
    }
    else
    {
        val = 0xFFFF >> (15 - endPos);
        setLCDFunction(baseAddress, endIdx, val);

        for(i = endIdx - 1; i > startIdx; i--)
        {
            setLCDFunction(baseAddress, i, 0xFFFF);
        }

        val = 0xFFFF << startPos;
        setLCDFunction(baseAddress, startIdx, val);
    }
}

void LCD_C_setMemory(uint16_t baseAddress,
                     uint8_t pin,
                     uint8_t value)
{
    uint8_t muxRate = HWREG16(baseAddress + OFS_LCDCCTL0)
                      & (LCDMX2 | LCDMX1 | LCDMX0);

    // static, 2-mux, 3-mux, 4-mux
    if(muxRate <= (LCDMX1 | LCDMX0))
    {
        if(pin & 1)
        {
            HWREG8(baseAddress + OFS_LCDM1 + pin / 2) &= 0x0F;
            HWREG8(baseAddress + OFS_LCDM1 + pin / 2) |= (value & 0xF) << 4;
        }
        else
        {
            HWREG8(baseAddress + OFS_LCDM1 + pin / 2) &= 0xF0;
            HWREG8(baseAddress + OFS_LCDM1 + pin / 2) |= (value & 0xF);
        }
    }
    else
    {
        //5-mux, 6-mux, 7-mux, 8-mux
        HWREG8(baseAddress + OFS_LCDM1 + pin) = value;
    }
}

void LCD_C_setBlinkingMemory(uint16_t baseAddress,
                             uint8_t pin,
                             uint8_t value)
{
    uint8_t muxRate = HWREG16(baseAddress + OFS_LCDCCTL0)
                      & (LCDMX2 | LCDMX1 | LCDMX0);

    // static, 2-mux, 3-mux, 4-mux
    if(muxRate <= (LCDMX1 | LCDMX0))
    {
        if(pin & 1)
        {
            HWREG8(baseAddress + OFS_LCDBM1 + pin / 2) &= 0x0F;
            HWREG8(baseAddress + OFS_LCDBM1 + pin / 2) |= (value & 0xF) << 4;
        }
        else
        {
            HWREG8(baseAddress + OFS_LCDBM1 + pin / 2) &= 0xF0;
            HWREG8(baseAddress + OFS_LCDBM1 + pin / 2) |= (value & 0xF);
        }
    }
    else
    {
        //5-mux, 6-mux, 7-mux, 8-mux
        HWREG8(baseAddress + OFS_LCDBM1 + pin) = value;
    }
}

void LCD_C_configChargePump(uint16_t baseAddress,
                            uint16_t syncToClock,
                            uint16_t functionControl)
{
    HWREG16(baseAddress + OFS_LCDCCPCTL) &= ~(LCDCPCLKSYNC);
    HWREG16(baseAddress + OFS_LCDCCPCTL) &= ~(LCDCPDIS7 | LCDCPDIS6 | LCDCPDIS5
                                              | LCDCPDIS4 | LCDCPDIS3 |
                                              LCDCPDIS2 | LCDCPDIS1 |
                                              LCDCPDIS0);

    HWREG16(baseAddress + OFS_LCDCCPCTL) |= syncToClock | functionControl;
}

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