summaryrefslogtreecommitdiff
path: root/bsp/nuvoton/libraries/m2354/StdDriver/src/nu_lcd.c
blob: cccb4df5a4d46707b126fd00dce51efb6ca3dc57 (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
/**************************************************************************//**
 * @file     lcd.c
 * @version  V3.00
 * @brief    Liquid-Crystal Display(LCD) driver source file
 *
 * @copyright SPDX-License-Identifier: Apache-2.0
 * @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
 *****************************************************************************/
#include "NuMicro.h"


/** @addtogroup Standard_Driver Standard Driver
  @{
*/

/** @addtogroup LCD_Driver LCD Driver
  @{
*/


/// @cond HIDDEN_SYMBOLS

/** @addtogroup LCD_EXPORTED_VARIABLES LCD Exported Variables
  @{
*/
/*---------------------------------------------------------------------------------------------------------*/
/* Global file scope (static) variables                                                                    */
/*---------------------------------------------------------------------------------------------------------*/
static uint32_t g_LCDFrameRate;

/**@}*/ /* end of group LCD_EXPORTED_VARIABLES */

/// @endcond /* HIDDEN_SYMBOLS */


/** @addtogroup LCD_EXPORTED_FUNCTIONS LCD Exported Functions
  @{
*/

/**
  * @brief      LCD Initialization routine
  *
  * @param[in]  pLCDCfg Specify the LCD property. It includes:
  *                     u32SrcFreq: Clock source frequency of LCD controller.
  *                     u32ComDuty: LCD COM duty ratio selection. Valid values are:
  *                                 - \ref LCD_COM_DUTY_1_1
  *                                 - \ref LCD_COM_DUTY_1_2
  *                                 - \ref LCD_COM_DUTY_1_3
  *                                 - \ref LCD_COM_DUTY_1_4
  *                                 - \ref LCD_COM_DUTY_1_5
  *                                 - \ref LCD_COM_DUTY_1_6
  *                                 - \ref LCD_COM_DUTY_1_7
  *                                 - \ref LCD_COM_DUTY_1_8
  *                     u32BiasLevel: LCD Bias level selection. Valid values are:
  *                                     - \ref LCD_BIAS_LV_1_2
  *                                     - \ref LCD_BIAS_LV_1_3
  *                                     - \ref LCD_BIAS_LV_1_4
  *                     u32Framerate: Specify the target LCD operating frame rate (Hz).
  *                     u32WaveformType: Specify the LCD waveform type. Valid values are:
  *                                         - \ref LCD_WAVEFORM_TYPE_A_NORMAL
  *                                         - \ref LCD_WAVEFORM_TYPE_B_NORMAL
  *                                         - \ref LCD_WAVEFORM_TYPE_A_INVERSE
  *                                         - \ref LCD_WAVEFORM_TYPE_B_INVERSE
  *                     u32IntSrc: Interrupt source selection. Valid values are:
  *                                 - \ref LCD_DISABLE_ALL_INT
  *                                 - \ref LCD_FRAME_COUNTING_END_INT
  *                                 - \ref LCD_FRAME_END_INT
  *                                 - \ref LCD_CPTOUT_INT
  *                                 - \ref LCD_ENABLE_ALL_INT
  *                     u32DrivingMode: LCD operation driving mode selection. Valid values are:
  *                                 - \ref LCD_LOW_DRIVING_AND_BUF_OFF
  *                                 - \ref LCD_HIGH_DRIVING_AND_BUF_OFF
  *                                 - \ref LCD_HIGH_DRIVING_AND_BUF_OFF_AND_PWR_SAVING
  *                                 - \ref LCD_HIGH_DRIVING_AND_BUF_OFF_AND_PWR_SAVING
  *                                 - \ref LCD_LOW_DRIVING_AND_BUF_ON_AND_PWR_SAVING
  *                     u32VSrc: Voltage source selection. Valid values are:
  *                                 - \ref LCD_VOLTAGE_SOURCE_VLCD
  *                                 - \ref LCD_VOLTAGE_SOURCE_AVDD
  *                                 - \ref LCD_VOLTAGE_SOURCE_CP
  *
  * @return     The real LCD operating frame rate. Or 0 means LCD_Open failed.
  *
  * @details    This function will configure the LCD properties for driving the LCD display well.
  *             After that, user can perform \ref LCD_ENABLE_DISPLAY() to enable LCD controller for LCD display.
  */
uint32_t LCD_Open(S_LCD_CFG_T *pLCDCfg)
{
    uint32_t u32ComNum, u32FreqLCD, u32FreqDiv;

    /* Display LCD display first */
    LCD_DISABLE_DISPLAY();

    /* Turn all segments off */
    LCD_SetAllPixels(0);

    /* Set com and bias */
    LCD->PCTL = (pLCDCfg->u32ComDuty | pLCDCfg->u32BiasLevel);

    /* Set waveform type */
    LCD_WAVEFORM_TYPE(pLCDCfg->u32WaveformType);

    /* Configure interrupt source */
    LCD->INTEN = pLCDCfg->u32IntSrc;

    /* Set driving mode */
    LCD_DRIVING_MODE(pLCDCfg->u32DrivingMode);

    /* Select voltage source */
    LCD_VOLTAGE_SOURCE(pLCDCfg->u32VSrc);

    /*
        An example for specify frame rate.
            If LCD source clock is 32768Hz, COM duty 4.
            In type-A:
                One frame rate 32Hz, frame end event rate 32Hz.
                    32 = (1/4) * F_LCD * (1/2)
                    F_LCD = 32 * 4 * 2 = 256 = (32768 / F_Div)
                    F_Div = (32768 / F_LCD) = 128
            In type-B:
                Each even/odd frame rate 32Hz, frame end event rate 16Hz.
                    32 = (1/4) * F_LCD
                    F_LCD = 32 * 4 = (32768 / F_Div)
                    F_Div = (32768 / F_LCD) = 256
    */
    u32ComNum = ((pLCDCfg->u32ComDuty & LCD_PCTL_DUTY_Msk) >> LCD_PCTL_DUTY_Pos) + 1;
    if((pLCDCfg->u32WaveformType & LCD_PCTL_TYPE_Msk) == LCD_PCTL_TYPE_Msk)
    {
        /* In type-B */

        /* Calculate LCD operation frequency */
        u32FreqLCD = (pLCDCfg->u32Framerate * u32ComNum);

        /* Calculate possible freq. divider */
        u32FreqDiv = (pLCDCfg->u32SrcFreq  / u32FreqLCD);

        if(u32FreqDiv > 1024)
        {
            /* Invalid frame rate */
            g_LCDFrameRate = 0ul;
        }
        else
        {
            /* Set freq. divider */
            LCD_SET_FREQDIV(u32FreqDiv);

            /* Calculate target frame rate */
            g_LCDFrameRate = pLCDCfg->u32SrcFreq / (u32ComNum * u32FreqDiv);
        }
    }
    else
    {
        /* In type-A */

        /* Calculate LCD operation frequency */
        u32FreqLCD = (pLCDCfg->u32Framerate * u32ComNum) * 2;

        /* Calculate possible freq. divider */
        u32FreqDiv = (pLCDCfg->u32SrcFreq  / u32FreqLCD);

        if(u32FreqDiv > 1024)
        {
            /* Invalid frame rate */
            g_LCDFrameRate = 0ul;
        }
        else
        {
            /* Set freq. divider */
            LCD_SET_FREQDIV(u32FreqDiv);

            /* Calculate target frame rate */
            g_LCDFrameRate = (pLCDCfg->u32SrcFreq  / (u32ComNum * u32FreqDiv)) / 2;
        }
    }

    return g_LCDFrameRate;
}

/**
  * @brief      Disable LCD Display
  *
  * @param[in]  None
  *
  * @return     None
  *
  * @details    This function is used to disable LCD display.
  */
void LCD_Close(void)
{
    LCD_DISABLE_DISPLAY();
}

/**
  * @brief      Enables a Segment Display
  *
  * @param[in]  u32Com      Specify COM number. Valid values are from 0~7.
  * @param[in]  u32Seg      Specify Segment number. Valid values are from 0~43.
  * @param[in]  u32OnFlag   0 : Segment not display
  *                         1 : Segment display
  *
  * @return     None
  *
  * @details    This function is used to enable specified segment display on the LCD.
  */
void LCD_SetPixel(uint32_t u32Com, uint32_t u32Seg, uint32_t u32OnFlag)
{
    uint32_t seg_num = (u32Seg / 4);
    uint32_t seg_shift = (8 * (u32Seg - (4 * seg_num)));

    if(seg_num < 11)
    {
        if(u32OnFlag)
        {
            LCD->DATA[seg_num] |= ((uint32_t)(1 << u32Com) << seg_shift);
        }
        else
        {
            LCD->DATA[seg_num] &= (~((uint32_t)(1 << u32Com) << seg_shift));
        }
    }
}

/**
  * @brief      Enable/Disable all LCD segments
  *
  * @param[in]  u32OnOff    0 : Disable all segments display
  *                         1 : Enable all segments display
  *
  * @return     None
  *
  * @details    This function is used to enable/disable all LCD segments display.
  */
void LCD_SetAllPixels(uint32_t u32OnOff)
{
    uint32_t i, u32Value;

    if(u32OnOff == 1ul)
    {
        u32Value = 0xFFFFFFFFul;
    }
    else
    {
        u32Value = 0x00000000ul;
    }

    for(i = 0; i < 11; i++)
        LCD->DATA[i] = u32Value;
}

/**
  * @brief      Enable LCD Blinking
  *
  * @param[in]  u32ms       Blinking period time(unit: ms)
  *
  * @return     Real blinking period time(ms)
  *
  * @details    This function is used to enable blink display with specified period.
  */
uint32_t LCD_EnableBlink(uint32_t u32ms)
{
    uint32_t u32OneCountPeriod, u32TargetCounts;

    if((LCD->PCTL & LCD_PCTL_TYPE_Msk) == LCD_PCTL_TYPE_Msk)
    {
        /* In type-B */
        u32OneCountPeriod = (1000 * 2) / g_LCDFrameRate; // ms
    }
    else
    {
        /* In type-A */
        u32OneCountPeriod = 1000 / g_LCDFrameRate; // ms
    }

    u32TargetCounts = (u32ms / u32OneCountPeriod);
    if(u32TargetCounts == 0)
        u32TargetCounts = 1;
    if(u32TargetCounts > 1024)
        u32TargetCounts = 1024;

    LCD_SET_FRAME_COUNTING_VALUE(u32TargetCounts);

    /* Enable blink display */
    LCD_BLINKING_ON();

    return (u32OneCountPeriod * u32TargetCounts);
}

/**
  * @brief      Disable LCD Blinking
  *
  * @param[in]  None
  *
  * @return     None
  *
  * @details    This function is used to disable LCD blink display.
  */
void LCD_DisableBlink(void)
{
    /* Disable blink display */
    LCD_BLINKING_OFF();
}

/**
  * @brief      Enable LCD Interrupt
  *
  * @param[in]  IntSrc      Interrupt Source. It could be a combination of
  *                             \ref LCD_FRAME_COUNTING_END_INT, \ref LCD_FRAME_END_INT and \ref LCD_CPTOUT_INT.
  *
  * @return     None
  *
  * @details    This function is used to enable the specific LCD interrupt.
  */
void LCD_EnableInt(uint32_t u32IntSrc)
{
    LCD->INTEN |= (u32IntSrc);
}

/**
  * @brief      Disable LCD Interrupt
  *
  * @param[in]  IntSrc      Interrupt Source. It could be a combination of
  *                             \ref LCD_FRAME_COUNTING_END_INT, \ref LCD_FRAME_END_INT and \ref LCD_CPTOUT_INT.
  *
  * @return     None
  *
  * @details    This function is used to disable the specific LCD interrupt.
  */
void LCD_DisableInt(uint32_t u32IntSrc)
{
    LCD->INTEN &= ~(u32IntSrc);
}


/**@}*/ /* end of group LCD_EXPORTED_FUNCTIONS */

/**@}*/ /* end of group LCD_Driver */

/**@}*/ /* end of group Standard_Driver */