summaryrefslogtreecommitdiff
path: root/bsp/hc32l136/Libraries/HC32L136_StdPeriph_Driver/src/lcd.c
blob: 65206068d270463ba76f6b9c5d851302ac8e0986 (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*************************************************************************************
* Copyright (C) 2018, Huada Semiconductor Co.,Ltd All rights reserved.    
*
* This software is owned and published by: 
* Huada Semiconductor Co.,Ltd ("HDSC").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND 
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with HDSC 
* components. This software is licensed by HDSC to be adapted only 
* for use in systems utilizing HDSC components. HDSC shall not be 
* responsible for misuse or illegal use of this software for devices not 
* supported herein. HDSC is providing this software "AS IS" and will 
* not be responsible for issues arising from incorrect user implementation 
* of the software.  
*
* Disclaimer:
* HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS), 
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING, 
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED 
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED 
* WARRANTY OF NONINFRINGEMENT.  
* HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT, 
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT 
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, 
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR 
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT, 
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA, 
* SAVINGS OR PROFITS, 
* EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED 
* FROM, THE SOFTWARE.  
*
* This software may be replicated in part or whole for the licensed use, 
* with the restriction that this Disclaimer and Copyright notice must be 
* included with each copy of this software, whether used in part or whole, 
* at all times.                        
*/
/******************************************************************************/
/** \file lcd.c
 **
 ** WDT function driver API.
 ** @link SampleGroup Some description @endlink
 **
 **   - 2018-5-3  1.0  CJ First version for Device Driver Library of Module.
 **
 ******************************************************************************/

/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "lcd.h"

/**
 *******************************************************************************
 ** \addtogroup I2cGroup
 ******************************************************************************/
//@{

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
 en_result_t LCD_SetClkSrc(en_lcd_clk_t enLcdClk)
 {
     en_result_t enRet = Error;
     M0P_LCD->CR1_f.CLKSRC = enLcdClk;
     enRet = Ok;
     return enRet;
 }
/**
 ******************************************************************************
 ** \brief  LCD Bias源选择函数
 **
 ** \param [in] enBiasSrc偏置源选择
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
 en_result_t LCD_SelBiasSrc(en_lcd_biassrc_t enBiasSrc)
 {
     en_result_t enRet = Error;
     switch(enBiasSrc)
     {
         case LcdInRes_High:
         case LcdInRes_Low:
         case LcdInRes_Mid:
         case LcdExtCap:
         case LcdExtRes:
            M0P_LCD->CR0_f.BSEL = enBiasSrc;
            break;
        default:
            return ErrorInvalidParameter;
     }
     enRet = Ok;
     return enRet;
 }
/**
 ******************************************************************************
 ** \brief  LCD 占空比选择函数
 **
 ** \param [in] enDuty占空比
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
en_result_t LCD_SetDuty(en_lcd_duty_t enDuty)
{
    en_result_t enRet = Error;
    switch(enDuty)
    {
        case LcdStatic:
        case LcdDuty2:
        case LcdDuty3:
        case LcdDuty4:
        case LcdDuty6:
        case LcdDuty8:
            M0P_LCD->CR0_f.DUTY = enDuty;
            break;
        default:
            return ErrorInvalidParameter;
    }
    enRet = Ok;
    return enRet; 
}
/**
 ******************************************************************************
 ** \brief  LCD bias设置函数
 **
 ** \param [in] enBias 偏置
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
 en_result_t LCD_SetBias(en_lcd_bias_t enBias)
 {
    en_result_t enRet = Error;
    switch(enBias)
    {
        case LcdBias3:
        case LcdBias2:
            M0P_LCD->CR0_f.BIAS = enBias;
            break;
        default:
            return ErrorInvalidParameter;
    }
    enRet = Ok;
    return enRet;     
 }
 /**
 ******************************************************************************
 ** \brief  LCD 电压泵时钟频率选择函数
 **
 ** \param [in] enCpClk 电压泵频率
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
en_result_t LCD_SelCpClk(en_lcd_cpclk_t enCpClk)
{
    en_result_t enRet = Error;
    switch(enCpClk)
    {
        case LcdClk2k:
        case LcdClk4k:
        case LcdClk8k:
        case LcdClk16k:
            M0P_LCD->CR0_f.CPCLK = enCpClk;
            break;
        default:
            return ErrorInvalidParameter;
    }
    enRet = Ok;
    return enRet;  
}
 /**
 ******************************************************************************
 ** \brief  LCD 扫描时钟频率选择函数
 **
 ** \param [in] enScanClk 扫描时钟频率
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
en_result_t LCD_SelScanClk(en_lcd_scanclk_t enScanClk)
{
    en_result_t enRet = Error;
    switch(enScanClk)
    {
        case LcdClk64hz:
        case LcdClk128hz:
        case LcdClk256hz:
        case LcdClk512hz:
            M0P_LCD->CR0_f.LCDCLK = enScanClk;
            break;
        default:
            return ErrorInvalidParameter;
    }
    enRet = Ok;
    return enRet;    
}
 /**
 ******************************************************************************
 ** \brief  LCD 模块使能或闪屏使能禁止函数
 **
 ** \param [in] enFunc功能,bFlag使能或禁止
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
 en_result_t LCD_EnFunc(en_lcd_func_t enFunc,boolean_t bFlag)
 {
    en_result_t enRet = Error;
    switch(enFunc)
    {
        case LcdEn:
            M0P_LCD->CR0_f.EN = bFlag;
            break;
        case LcdBlinkEn:
            M0P_LCD->CR1_f.BLINKEN = bFlag;
            break;
        default:
            return ErrorInvalidParameter;
    }
    enRet = Ok;
    return enRet;
 }
 /**
 ******************************************************************************
 ** \brief  LCD 显示模式0/1设置
 **
 ** \param [in] enDispMode模式
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
 en_result_t LCD_SetDispMode(en_lcd_dispmode_t enDispMode)
 {
    en_result_t enRet = Error;
    switch(enDispMode)
    {
        case LcdMode0:
        case LcdMode1:
            M0P_LCD->CR1_f.MODE = enDispMode;
            break;
        default:
            return ErrorInvalidParameter;
    }
    enRet = Ok;
    return enRet; 
 }
 /**
 ******************************************************************************
 ** \brief  LCD 对比度设置
 **
 ** \param [in] u8Contrast对比度
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
 en_result_t LCD_SetContrast(uint8_t u8Contrast)
 {
    en_result_t enRet = Error;
    M0P_LCD->CR0_f.CONTRAST = u8Contrast;
    enRet = Ok;
    return enRet;
 }
 /**
 ******************************************************************************
 ** \brief  LCD 闪屏计数器设置
 **
 ** \param [in] u8BlinkCnt计数器
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
 en_result_t LCD_SetBlinkCnt(uint8_t u8BlinkCnt)
 {
    en_result_t enRet = Error;
    M0P_LCD->CR1_f.BLINKCNT = u8BlinkCnt;
    enRet = Ok;
    return enRet;   
 }
  /**
 ******************************************************************************
 ** \brief  LCD 中断标记清除
 **
 ** \param [in] 无
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
en_result_t LCD_ClrIntState(void)
{
	en_result_t enRet = Error;
	M0P_LCD->INTCLR_f.INTF = 0;
    enRet = Ok;
    return enRet;  
}
  /**
 ******************************************************************************
 ** \brief  根据LCD显示模式获取端口配置
 **
 ** \param [in]enLcdRunMode:显示方式, stcSegCom获取端口参数
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
 en_result_t LCD_GetSegCom(stc_lcd_segcompara_t *pstcSegComPara,stc_lcd_segcom_t *pstcSegCom)
{
    en_result_t enRet = Error;
    if(pstcSegComPara->u8MaxSeg>40)
    {
        return ErrorInvalidParameter;
    }
    switch(pstcSegComPara->enBiasSrc)//seg32_35
    {
        case LcdInRes_High:
        case LcdInRes_Low:
        case LcdInRes_Mid:
            pstcSegCom->bMux = 1;
            pstcSegCom->Seg32_39VLcdCom7_4_t.SegVLcdCom = 0xff;
            break;
        case LcdExtCap:
        case LcdExtRes:
            //VLCD模拟端口配置
            if(pstcSegComPara->u8MaxSeg>36)
            {
                return ErrorInvalidParameter;
            }
            pstcSegCom->bMux = 0;
            pstcSegCom->Seg32_39VLcdCom7_4_t.SegVLcdCom = 0x0f;//seg32_35置0
            break;
        default:
          return ErrorInvalidParameter;
    }
    switch(pstcSegComPara->enDuty)//COM0_7
    {
        case LcdStatic:
            pstcSegCom->u8Com0_3 = 0xfe;
            pstcSegCom->Seg32_39VLcdCom7_4_t.SegVLcdCom &= 0xff;
            break;
        case LcdDuty2:
            pstcSegCom->u8Com0_3 = 0xfc;//COM口配置,默认按顺序进行配置com0/com1
            pstcSegCom->Seg32_39VLcdCom7_4_t.SegVLcdCom &= 0xff;
            break;
        case LcdDuty3:
            pstcSegCom->u8Com0_3 = 0xf8;//只取低4bit
            pstcSegCom->Seg32_39VLcdCom7_4_t.SegVLcdCom &= 0xff;
            break;
        case LcdDuty4:
            pstcSegCom->u8Com0_3 = 0xf0;//只取低4bit
            pstcSegCom->Seg32_39VLcdCom7_4_t.SegVLcdCom &= 0xff;
            break;
        case LcdDuty6:
            if(pstcSegComPara->u8MaxSeg>38)
            {
                return ErrorInvalidParameter;
            }
            pstcSegCom->u8Com0_3 = 0xf0;//只取低4bit
            pstcSegCom->Seg32_39VLcdCom7_4_t.SegVLcdCom &= 0xfc;
            break;
        case LcdDuty8:
            if(pstcSegComPara->u8MaxSeg>36)
            {
                return ErrorInvalidParameter;
            }
            pstcSegCom->u8Com0_3 = 0xf0;//只取低4bit
            pstcSegCom->Seg32_39VLcdCom7_4_t.SegVLcdCom &= 0xf0;
            break;            
        default:
        return ErrorInvalidParameter;
    }
    return enRet;
}
 /**
 ******************************************************************************
 ** \brief  LCD COMSEG端口配置
 **
 ** \param [in] pstcSegCom端口配置结构体
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
 en_result_t LCD_SetSegCom(stc_lcd_segcom_t *pstcSegCom)
 {
    en_result_t enRet = Error;
    M0P_LCD->POEN0 = pstcSegCom->u32Seg0_31;
    M0P_LCD->POEN1 = (uint32_t)(pstcSegCom->Seg32_39VLcdCom7_4_t.SegVLcdCom);
	M0P_LCD->POEN1_f.MUX = pstcSegCom->bMux;
    M0P_LCD->POEN1_f.C0 = pstcSegCom->u8Com0_3&0x01;
    M0P_LCD->POEN1_f.C1 = pstcSegCom->u8Com0_3&0x02;
    M0P_LCD->POEN1_f.C2 = pstcSegCom->u8Com0_3&0x04;
    M0P_LCD->POEN1_f.C3 = pstcSegCom->u8Com0_3&0x08;
    enRet = Ok;
    return enRet;
 }
  /**
 ******************************************************************************
 ** \brief  液晶全显
 **
 ** \param [in] 无
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
  en_result_t LCD_FullDisp(void)
  {
    en_result_t enRet=Error;
    uint8_t i;
	uint32_t volatile *p = NULL;
	p = &M0P_LCD->RAM0;
    for(i=0;i<8;i++)
    {
        *p = 0xffffffffu;
        p++;
    }
    for(i=0;i<8;i++)
    {
        *p = 0xffu;
        p++;
    } 
    enRet = Ok;
    return enRet;
  }
    /**
 ******************************************************************************
 ** \brief  液晶全清
 **
 ** \param [in] 无
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
 en_result_t LCD_ClearDisp(void)
{
    en_result_t enRet=Error;
    uint8_t i;
    uint32_t volatile *p = NULL;
    p = &M0P_LCD->RAM0;
    for(i=0;i<16;i++)
    {
        *p = 0x00;
        p++;
    }
    enRet = Ok;
    return enRet;
}
  /**
 ******************************************************************************
 ** \brief  LCD RAM bit设置函数
 **
 ** \param [in] u16Row RAM地址索引,u32List bit位索引,bData写入0或1
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
  en_result_t LCD_WriteRam(uint16_t u16Row, uint32_t u32List, boolean_t bData)
  {
    en_result_t  enRet = Error;
    uint8_t RamListSize = 0;
    volatile uint32_t *ptemp = NULL;
    ptemp = (volatile uint32_t*)&M0P_LCD->RAM0;
    if(u16Row>=8)
    {
        RamListSize = LCDRAM8_FSIZE;
    }
    else
    {
        RamListSize = LCDRAM0_7SIZE;
    }
    if ((u16Row > LCDRAMSIZE) || (u32List > RamListSize))
    {
        enRet = ErrorInvalidParameter;
        return enRet;
    }
    
    ptemp += u16Row;

    if (bData == TRUE)
    {
        *ptemp |= (uint32_t)(1 << u32List);
    } 
    else
    {
        *ptemp &= (uint32_t)(0 << u32List);
    }
    enRet = Ok;
    return  enRet; 
  }
/**
 ******************************************************************************
 ** \brief  LCD RAM 0-7寄存器设置函数
 **
 ** \param [in] u8Row RAM地址索引,u32Data写入寄存器数值
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
en_result_t LCD_WriteRam0_7Int32(uint8_t u8Row,uint32_t u32Data)
{
    en_result_t  enRet = Error;
    volatile uint32_t *ptemp = NULL;
    ptemp = (volatile uint32_t*)&M0P_LCD->RAM0;

    if (u8Row > LCDRAMSIZE)
    {
        enRet = ErrorInvalidParameter;
        return enRet;
    }
    
    ptemp += u8Row;
    *ptemp = u32Data;
    enRet = Ok;
    return  enRet; 
}
/**
 ******************************************************************************
 ** \brief  LCD RAM 8-f寄存器设置函数
 **
 ** \param [in] u8Row RAM地址索引,u8Data写入寄存器数值
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
en_result_t LCD_WriteRam8_FInt8(uint8_t u8Row,uint8_t u8Data)
{
    en_result_t  enRet = Error;
    volatile uint32_t *ptemp = NULL;
    ptemp = (volatile uint32_t*)&M0P_LCD->RAM0;

    if (u8Row > LCDRAMSIZE)
    {
        enRet = ErrorInvalidParameter;
        return enRet;
    }
    
    ptemp += u8Row;
    *ptemp = u8Data;
    enRet = Ok;
    return  enRet;  
}
/**
 ******************************************************************************
 ** \brief  LCD模块初始化函数
 **
 ** \param [in] stcLcdCfg配置结构体
 **
 ** \retval enRet 成功或失败
 **
 ******************************************************************************/
 en_result_t LCD_Init(stc_lcd_config_t *pstcLcdCfg)
 {
     en_result_t enRet = Error;
     enRet = LCD_SelBiasSrc(pstcLcdCfg->enBiasSrc);
     enRet = LCD_SetDuty(pstcLcdCfg->enDuty);
     enRet = LCD_SetBias(pstcLcdCfg->enBias);
     enRet = LCD_SelCpClk(pstcLcdCfg->enCpClk);
     enRet = LCD_SelScanClk(pstcLcdCfg->enScanClk);
     enRet = LCD_SetDispMode(pstcLcdCfg->enDispMode);
     enRet = LCD_SetClkSrc(pstcLcdCfg->enClk);
     if(Ok!=enRet)
     {
        return ErrorInvalidParameter;
     }
     if(pstcLcdCfg->bTouchNvic)
     {
         M0P_LCD->CR1_f.IE = 1;
         EnableNvic(LCD_IRQn,IrqLevel3,TRUE);
     }
     else
     {
         EnableNvic(LCD_IRQn,IrqLevel3,FALSE);
     }
     return Ok;
 }
//@} // LCDGroup