STM32 V5 source code
guowenxue
2018-02-04 785deec23b4cb1e7c4c4d81eb808f195adb1d98a
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
/* ----------------------------------------------------------------------------
 *         SAM Software Package License
 * ----------------------------------------------------------------------------
 * Copyright (c) 2011, Atmel Corporation
 *
 * 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 disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL 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.
 * ----------------------------------------------------------------------------
 */
 
/**
 * \file
 *
 * Implementation of ILI9325 driver.
 *
 */
 
/*----------------------------------------------------------------------------
 *        Headers
 *----------------------------------------------------------------------------*/
 
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "lcd_r61509v.h"
#include "stm32f10x.h"
#include "stm32f10x_fsmc.h"
#include "stm32v5_systick.h"
#include "lcd_r61509v.h"
 
/*----------------------------------------------------------------------------
 *        Local variables
 *----------------------------------------------------------------------------*/
 
/* Pixel cache used to speed up communication */
#define LCD_DATA_CACHE_SIZE BOARD_LCD_WIDTH
static LcdColor_t gLcdPixelCache[LCD_DATA_CACHE_SIZE];
 
 
/* ³õʼ»¯LCDËùʹÓõĹܽÅΪGPIOģʽ»òFSMCģʽ */
static void lcd_gpio_init(void)
{
    GPIO_InitTypeDef                GPIO_InitStructure;
    
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);    /* Ê¹ÄÜFSMCÍâÉèʱÖÓ */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE, ENABLE); /* Ê¹ÄÜLCD±³¹âºÍ¸´Î»¹Ü½ÅʱÖÓ */
 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;                   /* LCD±³¹â¿ØÖ¹¹Ü½Å PD13 */    
    GPIO_Init(GPIOD, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;            /* LCD¸´Î»¿ØÖƹܽŠPE1 */              
    GPIO_Init(GPIOE, &GPIO_InitStructure);
    
    /* ¸´ÓÃGPIODµÄGPIO¶Ë¿ÚΪFSMCģʽ,²Î¿¼datasheet <Table 5. High-density STM32F103xx pin definitions> */
    GPIO_InitStructure.GPIO_Pin =   GPIO_Pin_14  /* PD14 - D0  */
                                  | GPIO_Pin_15  /* PD15 - D1  */    
                                  | GPIO_Pin_0   /* PD0  - D2  */        
                                  | GPIO_Pin_1   /* PD1  - D3  */
                                                                | GPIO_Pin_8   /* PD8  - D13 */
                                  | GPIO_Pin_9   /* PD9  - D14 */
                                  | GPIO_Pin_10  /* PD10 - D15 */                                                                
                                  | GPIO_Pin_4   /* PD4  - nOE Êä³öʹÄÜ */
                                  | GPIO_Pin_5   /* PD5  - nEW Ð´Ê¹ÄÜ */        
                                                                | GPIO_Pin_7   /* PD7  - FSMC_NE1  LCDƬѡ */
                                                                | GPIO_Pin_11; /* PD11 - A16(LCD RS) LCDÖ¸Áî/Êý¾ÝÇл» */                                                                
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOD, &GPIO_InitStructure);   
 
    /* ¸´ÓÃGPIOEµÄGPIO¶Ë¿ÚΪFSMCģʽ,²Î¿¼datasheet <Table 5. High-density STM32F103xx pin definitions> */
    GPIO_InitStructure.GPIO_Pin =   GPIO_Pin_7   /* PE7  - D4  */
                                  | GPIO_Pin_8   /* PE8  - D5  */
                                  | GPIO_Pin_9   /* PE9  - D6  */
                                  | GPIO_Pin_10  /* PE10 - D7  */                                                            
                                  | GPIO_Pin_11  /* PE11 - D8  */
                                  | GPIO_Pin_12  /* PE12 - D9  */
                                  | GPIO_Pin_13  /* PE13 - D10 */
                                  | GPIO_Pin_14  /* PE14 - D11 */
                                  | GPIO_Pin_15; /* PE15 - D12 */                                                    
  GPIO_Init(GPIOE, &GPIO_InitStructure);    
    
    /* ´ò¿ªLCD±³¹â */
    GPIO_SetBits(GPIOD, GPIO_Pin_13);
}
 
 
/* ³õʼ»¯FSMC¹¤×÷ģʽ¼°ÆäÅäÖà*/
static void lcd_fsmc_init(void)
{
    FSMC_NORSRAMInitTypeDef         FSMC_NORSRAMInitStructure;    
    FSMC_NORSRAMTimingInitTypeDef   p;
    
  /* FSMC½Ó¿ÚÌØÐÔÅäÖòÎÊý,¶ÔÏÂÃæ¸÷³ÉÔ±¸³µÄÖµX±íʾX¸öʱÖÓÖÜÆÚ£¬ËüµÄʱÖÓÊÇÓÉHCLK¾­¹ý
       FSMC_CLKDivisionÉèÖÃµÄ·ÖÆµ²ÎÊý·ÖƵºóµÃµ½¡£*/
  p.FSMC_AddressSetupTime = 0x02;     /* µØÖ·½¨Á¢Ê±¼ä */
  p.FSMC_AddressHoldTime = 0x00;      /* µØÖ·±£³Öʱ¼ä */
  p.FSMC_DataSetupTime = 0x05;        /* Êý¾Ý½¨Á¢Ê±¼ä */
  p.FSMC_DataLatency = 0x00;          /* Êý¾Ý±£³Öʱ¼ä */    
  p.FSMC_BusTurnAroundDuration = 0x00;/* ×ÜÏ߻ָ´Ê±¼ä */
  p.FSMC_CLKDivision = 0x00;          /* Ê±ÖÓ·ÖÆµ */
  /*FSMC_AccessMode: ÔÚµØÖ·Ïß²»¸´ÓõÄÇé¿öÏ£¬ABCDģʽµÄÇø±ð²»´ó¡£¸ÃÅäÖÃÖ»ÔÚÀ©Õ¹Ä£Ê½ÓÐЧ */    
  p.FSMC_AccessMode = FSMC_AccessMode_B; 
    
    /* LCDµÄCSÁ¬µÄPD7(NE1),ËùÒÔÎÒÃÇʹÓõÄÊÇNorflashµÄBank1  */
  FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
    /* PD11Á¬µØÖ·ÏßA16, Êµ¼ÊÉÏֻʹÓÃÁËÒ»ÌõµØÖ·Ïߣ¬ÒòΪI/O×ÊÔ´²»½ôÕÅ£¬ËùÒÔÅäÖõØÖ·ÏߺÍÊý¾ÝÏß²»¸´Óà*/
  FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
    /* ´æ´¢Æ÷ÀàÐÍΪNorflashÀàÐÍ,ËüµÄʱÐòÓë8080×ÜÏ߸ü½Ó½ü,¸ÃÅäÖû¹¿ÉÒÔΪPSRAMºÎSRAMģʽ.*/
  FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_NOR;
    /* Êý¾Ýλ¿íΪ16λ */
  FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
    /* Ê¹ÓÃÒ첽дģʽ£¬½ûֹͻ·¢Ä£Ê½£»8080×ÜÏ߸üÊʺÏÒ첽ģʽ¡£ */
  FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
    /* ±¾³ÉÔ±Ö»ÔÚÍ»·¢Ä£Ê½ÏÂÓÐЧ£¬µÈ´ýÐźż«ÐÔΪµÍ */
  FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
    /* ±¾³ÉÔ±Ö»ÔÚÍ»·¢Ä£Ê½ÏÂÓÐЧ£¬½ûÖ¹·Ç¶ÔÆëÍ»·¢Ä£Ê½ */
  FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
    /* ±¾³ÉÔ±Ö»ÔÚÍ»·¢Ä£Ê½ÏÂÓÐЧ£¬NWAITÐźÅÔÚʲôʱÆÚ²úÉú */
  FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
    /* ±¾³ÉÔ±Ö»ÔÚÍ»·¢Ä£Ê½ÏÂÓÐЧ£¬½ûÓÃNWAITÐźŠ*/
  FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;    
    /* ±¾³ÉÔ±Ö»ÔÚÍ»·¢Ä£Ê½ÏÂÓÐЧ£¬½ûֹͻ·¢Ð´²Ù×÷ */
  FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;    
    
    /* Ð´Ê¹ÄÜ£¬Èç¹û½ûÖ¹ÁËд²Ù×÷£¬FSMC²»»á²úÉúдʱÐò£¬µ«¿ÉÒÔ¶Á³öÊý¾Ý */
  FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
    /* ½ûÖ¹À©Õ¹Ä£Ê½£¬À©Õ¹Ä£Ê½¿ÉÒÔʹÓöÀÁ¢µÄ¶Á¡¢Ð´Ä£Ê½ */
  FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
 
    /* ÅäÖöÁдʱÐò£¡Èç¹ûʹÓÃÁËÀ©Õ¹Ä£Ê½£¬ÔòǰÕßÅäÖõÄÊǶÁʱÐò£¬ºóÕßÅäÖõÄÊÇдʱÐò¡£
     * Èç¹û½ûÖ¹ÁËÀ©Õ¹Ä£Ê½£¬Ôò¶ÁдʱÐò¶¼Ê¹ÓÃFSMC_ReadWriteTimingStruct½á¹¹ÌåÖеIJÎÊý¡£
      */
  FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
  FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
 
  FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);         
  FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);  /* Ê¹ÄÜFSMC Bank1_SRAM Bank */
}
 
void lcd_reset(void)
{
  GPIO_ResetBits(GPIOE, GPIO_Pin_1);     /* PE1 ÎªLCD ¸´Î»ÐźŠ*/
  msleep(500);                       
  GPIO_SetBits(GPIOE, GPIO_Pin_1);              
  msleep(500);
}
 
 
/*----------------------------------------------------------------------------
 *        Export functions
 *----------------------------------------------------------------------------*/
 
/**
 * \brief Write data to LCD Register.
 *
 * \param reg   Register address.
 * \param data  Data to be written.
 */
void LCD_WriteReg( uint16_t reg, uint16_t data )
{
        LCD_IR() = 0;
    LCD_IR() = reg;
    LCD_D()  = data;
}
 
/**
 * \brief Read data from LCD Register.
 *
 * \param reg   Register address.
 *
 * \return      Readed data.
 */
uint16_t LCD_ReadReg( uint16_t reg )
{
    uint16_t value;
    
        LCD_IR() = 0;
    LCD_IR() = reg;
    value = LCD_D();
 
    return value;
}
 
/**
 * \brief Write two byte to LCD GRAM.
 *
 * \param color  16-bits RGB color.
 */
extern void LCD_WriteRAMWord( uint16_t wColor )
{
    LCD_D() = wColor ;
}
 
/**
 * \brief Prepare to write GRAM data.
 */
extern void LCD_WriteRAM_Prepare( void )
{
    LCD_IR() = 0 ;
    LCD_IR() = R61509V_R202H ; /* Write Data to GRAM (R202h)  */
}
 
/**
 * \brief Write data to LCD GRAM.
 *
 * \param color  16-bits RGB color.
 */
extern  void LCD_WriteRAM( LcdColor_t wColor )
{
    LCD_WriteRAMWord(wColor);
}
 
/**
 * \brief Write mutiple data in buffer to LCD controller.
 *
 * \param pBuf  data buffer.
 * \param size  size in pixels.
 */
static void LCD_WriteRAMBuffer(const LcdColor_t *pBuf, uint32_t size)
{
    uint32_t addr ;
 
    for ( addr = 0 ; addr < size ; addr++ )
    {
        LCD_WriteRAM(pBuf[addr]);
    }
}
 
/**
 * \brief Prepare to read GRAM data.
 */
extern void LCD_ReadRAM_Prepare( void )
{
    LCD_IR() = 0 ;
    LCD_IR() = R61509V_R202H ; /* Read Data from GRAM (R202h)  */
}
 
/**
 * \brief Read data to LCD GRAM.
 *
 * \note Because pixel data LCD GRAM is 18-bits, so convertion to RGB 24-bits
 * will cause low color bit lose.
 *
 * \return color  24-bits RGB color.
 */
extern uint32_t LCD_ReadRAM( void )
{
    uint16_t color;
 
    color = LCD_D();       /* dummy read */
    color = LCD_D();       /* data */
 
    return color;
}
 
/*----------------------------------------------------------------------------
 *        Basic R61509V primitives
 *----------------------------------------------------------------------------*/
 
 
/**
 * \brief Check Box coordinates. Return upper left and bottom right coordinates.
 *
 * \param pX1      X-coordinate of upper-left corner on LCD.
 * \param pY1      Y-coordinate of upper-left corner on LCD.
 * \param pX2      X-coordinate of lower-right corner on LCD.
 * \param pY2      Y-coordinate of lower-right corner on LCD.
 */
static void CheckBoxCoordinates( uint32_t *pX1, uint32_t *pY1, uint32_t *pX2, uint32_t *pY2 )
{
    uint32_t dw;
 
    if ( *pX1 >= BOARD_LCD_WIDTH )
    {
        *pX1 = BOARD_LCD_WIDTH-1 ;
    }
    if ( *pX2 >= BOARD_LCD_WIDTH )
    {
        *pX2 = BOARD_LCD_WIDTH-1 ;
    }
    if ( *pY1 >= BOARD_LCD_HEIGHT )
    {
        *pY1 = BOARD_LCD_HEIGHT-1 ;
    }
    if ( *pY2 >= BOARD_LCD_HEIGHT )
    {
        *pY2 = BOARD_LCD_HEIGHT-1 ;
    }
    if (*pX1 > *pX2)
    {
        dw = *pX1;
        *pX1 = *pX2;
        *pX2 = dw;
    }
    if (*pY1 > *pY2)
    {
        dw = *pY1;
        *pY1 = *pY2;
        *pY2 = dw;
    }
}
 
/**
 * \brief Initialize the LCD controller.
 */
uint32_t LCD_Initialize( void )
{
    uint16_t chipid ;
    
    lcd_gpio_init();     /* ³õʼ»¯LCDʹÓõ½µÄ¹Ü½Å */
    lcd_fsmc_init();     /* ³õʼ»¯FSMCÅäÖà*/
  lcd_reset();
 
  /* Check R61509V chipid */
  chipid = LCD_ReadReg( R61509V_R000H ) ; /* Driver Code Read (R000h) */
  if ( chipid != R61509V_DEVICE_CODE )
  {
     //printf( "Read R61509V chip ID (0x%04x) error, skip initialization.\r\n", chipid ) ;
     return 1 ;
    }        
 
    /* Register settins reference to R61509 LCD drvier datasheet <CPT 3.0¡±(C030JB) Initial Code> */
    
    /* Device Code Read */
    LCD_WriteReg(R61509V_R000H, 0x0000);
  LCD_WriteReg(R61509V_R000H, 0x0000);
    LCD_WriteReg(R61509V_R000H, 0x0000);
    LCD_WriteReg(R61509V_R000H, 0x0000);
    
  msleep(10);
    
    /* Base Image Number of Line */
    LCD_WriteReg(R61509V_R400H, 0x6200); 
    /* Display Control 2 */
    LCD_WriteReg(R61509V_R008H, 0x0808); 
    
    /* Gamma Control settings */
    LCD_WriteReg(R61509V_R300H, 0x0C00);
    LCD_WriteReg(R61509V_R301H, 0x5A0B);
    LCD_WriteReg(R61509V_R302H, 0x0906);
    LCD_WriteReg(R61509V_R303H, 0x1017);
    LCD_WriteReg(R61509V_R304H, 0x2300);
    LCD_WriteReg(R61509V_R305H, 0x1700);
    LCD_WriteReg(R61509V_R306H, 0x6309);
    LCD_WriteReg(R61509V_R307H, 0x0C09);
    LCD_WriteReg(R61509V_R308H, 0x100C);
    LCD_WriteReg(R61509V_R309H, 0x2232);
    
    /* Panel Interface Control settings */
    LCD_WriteReg(R61509V_R010H, 0x0016);//69.5Hz
    LCD_WriteReg(R61509V_R011H, 0x0101);//
    LCD_WriteReg(R61509V_R012H, 0x0000);//
    LCD_WriteReg(R61509V_R013H, 0x0001);//
    
    /* Power Control settings */
    LCD_WriteReg(R61509V_R100H, 0x0330);//BT,AP
    LCD_WriteReg(R61509V_R101H, 0x0237);//DC0,DC1,VC
    LCD_WriteReg(R61509V_R103H, 0x0F00);//VDV
    
    LCD_WriteReg(R61509V_R280H, 0x6100);//VCM
    LCD_WriteReg(R61509V_R102H, 0xC1B0);//VRH[11000],VCMR[1],PSON,PON[11]
    msleep(10);
 
    LCD_WriteReg(R61509V_R001H, 0x00100);
    LCD_WriteReg(R61509V_R002H, 0x00100);
    LCD_WriteReg(R61509V_R003H, 0x01030);
    LCD_WriteReg(R61509V_R009H, 0x00001);
    LCD_WriteReg(R61509V_R00CH, 0x00000);
    LCD_WriteReg(R61509V_R090H, 0x08000);
    LCD_WriteReg(R61509V_R00FH, 0x00000);
    LCD_WriteReg(R61509V_R210H, 0x00000);
    LCD_WriteReg(R61509V_R211H, 0x000EF);
    LCD_WriteReg(R61509V_R212H, 0x00000);
    LCD_WriteReg(R61509V_R213H, 0x0018F);//432=1AF, 400=18F
    LCD_WriteReg(R61509V_R500H, 0x00000);
    LCD_WriteReg(R61509V_R501H, 0x00000);
    LCD_WriteReg(R61509V_R502H, 0x0005F);
    
    LCD_WriteReg(R61509V_R401H, 0x00001);
    LCD_WriteReg(R61509V_R404H, 0x00000);
    msleep(100);
 
    LCD_WriteReg(R61509V_R007H, 0x00100);//BASEE
    msleep(100);
 
    LCD_WriteReg(R61509V_R200H, 0x00000); /* Horizontal GRAM Address Set  */
    LCD_WriteReg(R61509V_R201H, 0x00000); /* Vertical GRAM Address Set   */
    
  LCD_SetWindow( 0, 0, BOARD_LCD_WIDTH, BOARD_LCD_HEIGHT ) ;
  LCD_SetCursor( 0, 0 ) ;
    
    return 0;
}
 
 
/**
 * \brief Turn on the LCD.
 */
extern void LCD_On( void )
{
      uint16_t Reg;
 
      /* Display Control 1 (R007h)  When BASEE = 1 the base image is displayed. */
      Reg = LCD_ReadReg( R61509V_R007H );
    LCD_WriteReg( R61509V_R007H, Reg|R61509V_R007H_BASEE ) ;
 
    Reg = LCD_ReadReg( R61509V_R102H );
      LCD_WriteReg(R61509V_R102H, Reg|R61509V_R102H_PSON|R61509V_R102H_PON);//PSON[1],PON[1]
}
 
 
/**
 * \brief Turn off the LCD.
 */
extern void LCD_Off( void )
{
      uint16_t Reg;
      Reg = LCD_ReadReg( R61509V_R007H );
    
      /* Display Control 1 (R007h)  When BASEE = 0 No base image is displayed. */
    LCD_WriteReg( R61509V_R007H,  (Reg & ~R61509V_R007H_BASEE) ) ;
}
 
/**
 * \brief Power down the LCD.
 */
extern void LCD_PowerDown( void )
{
      uint16_t Reg;
    
        LCD_Off();
 
    Reg = LCD_ReadReg( R61509V_R102H );
      LCD_WriteReg(R61509V_R102H, Reg&(~(R61509V_R102H_PSON|R61509V_R102H_PON)) );//PSON[0],PON[0]    
}
 
/**
 * \brief Convert 24 bit RGB color into 5-6-5 rgb color space.
 *
 * Initialize the LcdColor_t cache with the color pattern.
 * \param x  24-bits RGB color.
 * \return 0 for successfull operation.
 */
extern uint32_t LCD_SetColor( uint32_t dwRgb24Bits )
{
    uint32_t i ;
 
    /* Fill the cache with selected color */
    for ( i = 0 ; i < LCD_DATA_CACHE_SIZE ; ++i )
    {
        gLcdPixelCache[i] = dwRgb24Bits ;
    }
 
    return 0;
}
 
/**
 * \brief Set cursor of LCD srceen.
 *
 * \param x  X-coordinate of upper-left corner on LCD.
 * \param y  Y-coordinate of upper-left corner on LCD.
 */
extern void LCD_SetCursor( uint16_t x, uint16_t y )
{
    /* GRAM Horizontal/Vertical Address Set (R20h, R21h) */
    LCD_WriteReg( R61509V_R200H, x ) ; /* column */
    LCD_WriteReg( R61509V_R201H, y ) ; /* row */
}
 
extern void LCD_SetWindow( uint32_t dwX, uint32_t dwY, uint32_t dwWidth, uint32_t dwHeight )
{
    /* Horizontal and Vertical RAM Address Position (R50h, R51h, R52h, R53h) */
 
    /* Set Horizontal Address Start Position */
   LCD_WriteReg( R61509V_R210H, (uint16_t)dwX ) ;
 
   /* Set Horizontal Address End Position */
   LCD_WriteReg( R61509V_R211H, (uint16_t)dwX+dwWidth-1 ) ;
 
   /* Set Vertical Address Start Position */
   LCD_WriteReg( R61509V_R212H, (uint16_t)dwY ) ;
 
   /* Set Vertical Address End Position */
   LCD_WriteReg( R61509V_R213H, (uint16_t)dwY+dwHeight-1 ) ;
}
 
extern void LCD_SetDisplayLandscape( uint32_t dwRGB )
{
    uint16_t dwValue ;
 
    /* When AM = ?? the address is updated in vertical writing direction. */
    /* DFM Set the mode of transferring data to the internal RAM when TRI = ?? */
    /* When TRI = ?? data are transferred to the internal RAM in 8-bit x 3 transfers mode via the 8-bit interface. */
    /* Use the high speed write mode (HWM=1) */
    /* ORG = ?? The original address ?0000h?moves according to the I/D[1:0] setting.  */
    /* I/D[1:0] = 00 Horizontal : decrement Vertical :  decrement, AM=0:Horizontal */
    dwValue = R61509V_R003H_AM | R61509V_R003H_DFM | R61509V_R003H_TRI | R61509V_R003H_ORG ;
 
    if ( dwRGB == 0 )
    {
        /* BGR=?? Swap the RGB data to BGR in writing into GRAM. */
        dwValue |= R61509V_R003H_BGR ;
    }
    LCD_WriteReg( R61509V_R003H, dwValue ) ;
 
    //    LCD_WriteReg( ILI9325_R60H, (0x1d<<8)|0x00 ) ; /*Gate Scan Control */
 
    LCD_SetWindow( 0, 0, BOARD_LCD_HEIGHT, BOARD_LCD_WIDTH ) ;
}
 
extern void LCD_SetDisplayPortrait( uint16_t wRGB )
{
    uint16_t dwValue ;
 
    /* Use the high speed write mode (HWM=1) */
    /* When TRI = 1 data are transferred to the internal RAM in 8-bit x 3 transfers mode via the 8-bit interface. */
    /* DFM=0: 18bpp (R:G:B = 6:6:6), DFM=1: 16bpp (R:G:B = 5:6:5) */
    /* I/D[1:0] = 11 Horizontal : increment Vertical :  increment, AM=0:Horizontal */
    dwValue =  R61509V_R003H_DFM | R61509V_R003H_ID1 | R61509V_R003H_ID0 ;
 
    if ( wRGB == MODE_BGR )
    {
        /* BGR=?? Swap the RGB data to BGR in writing into GRAM. */
        dwValue |= R61509V_R003H_BGR ;
    }
    LCD_WriteReg( R61509V_R003H, dwValue ) ;
        
    /* Gate Scan Control (R400h, R401h, R404h) */
    /* SCN[5:0]->bit[6:1] = 00 */
    /* NL[5:0]->bit[14:9] = 0x27: Sets the number of lines to drive the LCD at an interval of 8 lines. */
    //LCD_WriteReg( R61509V_R400H, R61509V_R400H_GS|(0x27<<9)|0x00 ) ;
}
 
 
extern void LCD_VerticalScroll( uint16_t wY )
{
    /* Gate Scan Control (R400h, R401h, R404h) */
    /*  Enables the grayscale inversion of the image by setting REV=1. */
    /* VLE[1]: Vertical scroll display enable bit */
    LCD_WriteReg( R61509V_R401H, 3 ) ;
    LCD_WriteReg( R61509V_R404H, wY ) ;
}
 
 
extern void LCD_SetPartialImage1( uint32_t dwDisplayPos, uint32_t dwStart, uint32_t dwEnd )
{
    if( dwStart <= dwEnd ) 
            return;
 
    /* Partial Image 1 Display Position (R500h) */
    LCD_WriteReg( R61509V_R500H, dwDisplayPos&0x1ff ) ;
    /* Partial Image 1 RAM Start/End Address (R501h, R502h) */
    LCD_WriteReg( R61509V_R501H, dwStart&0x1ff ) ;
    LCD_WriteReg( R61509V_R502H, dwEnd&0x1ff ) ;
 
}
 
extern void LCD_EnablePartialImage1( uint32_t OnOff )
{
    uint16_t Reg;
 
    Reg = LCD_ReadReg( R61509V_R007H );
    LCD_WriteReg( R61509V_R007H,  (Reg & ~R61509V_R007H_BASEE) |  R61509V_R007H_PTDE  ) ;
}
 
 
/**
 * \brief Draw a LcdColor_t on LCD of given color.
 *
 * \param x  X-coordinate of pixel.
 * \param y  Y-coordinate of pixel.
 */
extern uint32_t LCD_DrawPixel( uint32_t x, uint32_t y )
{
    if( (x >= BOARD_LCD_WIDTH) || (y >= BOARD_LCD_HEIGHT) )
    {
        return 1;
    }
 
    /* Set cursor */
    LCD_SetCursor( x, y );
 
    /* Prepare to write in GRAM */
    LCD_WriteRAM_Prepare();
    LCD_WriteRAM( *gLcdPixelCache );
 
    return 0;
}
 
 
 
extern void LCD_TestPattern( uint32_t dwRGB )
{
    uint32_t dwLine ;
    uint32_t dw ;
 
    LCD_SetWindow( 10, 10, 100, 20 ) ;
    LCD_SetCursor( 10, 10 ) ;
    LCD_WriteRAM_Prepare() ;
 
    for ( dwLine=0 ; dwLine < 20 ; dwLine++ )
    {
        /* Draw White bar */
        for ( dw=0 ; dw < 20 ; dw++ )
        {
                LCD_D() = 0xff ;
                LCD_D() = 0xff ;
                LCD_D() = 0xff ;
        }
        /* Draw Red bar */
        for ( dw=0 ; dw < 20 ; dw++ )
        {
            if ( dwRGB == 0 )
            {
                LCD_D() = 0xff ;
                LCD_D() = 0x00 ;
                LCD_D() = 0x00 ;
            }
            else
            {
                LCD_D() = 0x00 ;
                LCD_D() = 0x00 ;
                LCD_D() = 0xff ;
            }
        }
        /* Draw Green bar */
        for ( dw=0 ; dw < 20 ; dw++ )
        {
                LCD_D() = 0x00 ;
                LCD_D() = 0xff ;
                LCD_D() = 0x00 ;
        }
        /* Draw Blue bar */
        for ( dw=0 ; dw < 20 ; dw++ )
        {
            if ( dwRGB == 0 )
            {
                LCD_D() = 0x00 ;
                LCD_D() = 0x00 ;
                LCD_D() = 0xff ;
            }
            else
            {
                LCD_D() = 0xff ;
                LCD_D() = 0x00 ;
                LCD_D() = 0x00 ;
            }
        }
        /* Draw Black bar */
        for ( dw=0 ; dw < 20 ; dw++ )
        {
                LCD_D() = 0x00 ;
                LCD_D() = 0x00 ;
                LCD_D() = 0x00 ;
        }
    }
 
    LCD_SetWindow( 0, 0, BOARD_LCD_WIDTH, BOARD_LCD_HEIGHT ) ;
}
 
 
/**
 * \brief Write several pixels with the same color to LCD GRAM.
 *
 * LcdColor_t color is set by the LCD_SetColor() function.
 * This function is optimized using an sram buffer to transfer block instead of
 * individual pixels in order to limit the number of SPI interrupts.
 * \param dwX1      X-coordinate of upper-left corner on LCD.
 * \param dwY1      Y-coordinate of upper-left corner on LCD.
 * \param dwX2      X-coordinate of lower-right corner on LCD.
 * \param dwY2      Y-coordinate of lower-right corner on LCD.
 */
extern uint32_t LCD_DrawFilledRectangle( uint32_t dwX1, uint32_t dwY1, uint32_t dwX2, uint32_t dwY2 )
{
    uint32_t size, blocks;
 
    /* Swap coordinates if necessary */
    CheckBoxCoordinates(&dwX1, &dwY1, &dwX2, &dwY2);
 
    /* Determine the refresh window area */
    /* Horizontal and Vertical RAM Address Position (R210h, R211h, R212h, R213h) */
    LCD_WriteReg(R61509V_R210H, (uint16_t)dwX1);
    LCD_WriteReg(R61509V_R211H, (uint16_t)dwX2);
    LCD_WriteReg(R61509V_R212H, (uint16_t)dwY1);
    LCD_WriteReg(R61509V_R213H, (uint16_t)dwY2);
 
    /* Set cursor */
    LCD_SetCursor( dwX1, dwY1 );
 
    /* Prepare to write in GRAM */
    LCD_WriteRAM_Prepare();
 
    size = (dwX2 - dwX1 + 1) * (dwY2 - dwY1 + 1);
    /* Send pixels blocks => one SPI IT / block */
    blocks = size / LCD_DATA_CACHE_SIZE;
    while (blocks--)
    {
        LCD_WriteRAMBuffer(gLcdPixelCache, LCD_DATA_CACHE_SIZE);
    }
    /* Send remaining pixels */
    LCD_WriteRAMBuffer(gLcdPixelCache, size % LCD_DATA_CACHE_SIZE);
 
    /* Reset the refresh window area */
    /* Horizontal and Vertical RAM Address Position (R210h, R211h, R212h, R213h) */
    LCD_WriteReg(R61509V_R210H, (uint16_t)0 ) ;
    LCD_WriteReg(R61509V_R211H, (uint16_t)BOARD_LCD_WIDTH - 1 ) ;
    LCD_WriteReg(R61509V_R212H, (uint16_t)0) ;
    LCD_WriteReg(R61509V_R213H, (uint16_t)BOARD_LCD_HEIGHT - 1  ) ;
 
    return 0 ;
}
 
/**
 * \brief Write several pixels pre-formatted in a bufer to LCD GRAM.
 *
 * \param dwX1      X-coordinate of upper-left corner on LCD.
 * \param dwY1      Y-coordinate of upper-left corner on LCD.
 * \param dwX2      X-coordinate of lower-right corner on LCD.
 * \param dwY2      Y-coordinate of lower-right corner on LCD.
 * \param pBuffer   LcdColor_t buffer area.
 */
extern uint32_t LCD_DrawPicture( uint32_t dwX1, uint32_t dwY1, uint32_t dwX2, uint32_t dwY2, const LcdColor_t *pBuffer )
{
    uint32_t size;
 
    /* Swap coordinates if necessary */
    CheckBoxCoordinates(&dwX1, &dwY1, &dwX2, &dwY2);
 
    /* Determine the refresh window area */
    /* Horizontal and Vertical RAM Address Position (R210h, R211h, R212h, R213h) */
    LCD_WriteReg(R61509V_R210H, (uint16_t)dwX1 ) ;
    LCD_WriteReg(R61509V_R211H, (uint16_t)dwX2 ) ;
    LCD_WriteReg(R61509V_R212H, (uint16_t)dwY1 ) ;
    LCD_WriteReg(R61509V_R213H, (uint16_t)dwY2 ) ;
 
    /* Set cursor */
    LCD_SetCursor( dwX1, dwY1 );
 
    /* Prepare to write in GRAM */
    LCD_WriteRAM_Prepare();
 
    size = (dwX2 - dwX1 + 1) * (dwY2 - dwY1 + 1);
 
    LCD_WriteRAMBuffer(pBuffer, size);
 
    /* Reset the refresh window area */
    /* Horizontal and Vertical RAM Address Position (R210h, R211h, R212h, R213h) */
    LCD_WriteReg(R61509V_R210H, (uint16_t)0 ) ;
    LCD_WriteReg(R61509V_R211H, (uint16_t)BOARD_LCD_WIDTH - 1 ) ;
    LCD_WriteReg(R61509V_R212H, (uint16_t)0 ) ;
    LCD_WriteReg(R61509V_R213H, (uint16_t)BOARD_LCD_HEIGHT - 1 ) ;
 
    return 0 ;
}
 
/*
 * \brief Draw a line on LCD, which is not horizontal or vertical.
 *
 * \param x         X-coordinate of line start.
 * \param y         Y-coordinate of line start.
 * \param length    line length.
 * \param direction line direction: 0 - horizontal, 1 - vertical.
 * \param color     LcdColor_t color.
 */
static uint32_t DrawLineBresenham( uint32_t dwX1, uint32_t dwY1, uint32_t dwX2, uint32_t dwY2 )
{
    int dx, dy ;
    int i ;
    int xinc, yinc, cumul ;
    int x, y ;
 
    x = dwX1 ;
    y = dwY1 ;
    dx = dwX2 - dwX1 ;
    dy = dwY2 - dwY1 ;
 
    xinc = ( dx > 0 ) ? 1 : -1 ;
    yinc = ( dy > 0 ) ? 1 : -1 ;
    dx = ( dx > 0 ) ? dx : -dx ;
    dy = ( dy > 0 ) ? dy : -dy ;
 
    LCD_DrawPixel( x, y ) ;
 
    if ( dx > dy )
    {
      cumul = dx / 2 ;
      for ( i = 1 ; i <= dx ; i++ )
      {
        x += xinc ;
        cumul += dy ;
 
        if ( cumul >= dx )
        {
          cumul -= dx ;
          y += yinc ;
        }
        LCD_DrawPixel( x, y ) ;
      }
    }
    else
    {
        cumul = dy / 2 ;
        for ( i = 1 ; i <= dy ; i++ )
        {
            y += yinc ;
            cumul += dx ;
 
            if ( cumul >= dy )
            {
                cumul -= dy ;
                x += xinc ;
            }
 
            LCD_DrawPixel( x, y ) ;
        }
    }
 
    return 0 ;
}
 
/*
 * \brief Draw a line on LCD, horizontal and vertical line are supported.
 *
 * \param dwX1      X-coordinate of line start.
 * \param dwY1      Y-coordinate of line start.
 * \param dwX2      X-coordinate of line end.
 * \param dwY2      Y-coordinate of line end.
  */
extern uint32_t LCD_DrawLine ( uint32_t dwX1, uint32_t dwY1, uint32_t dwX2, uint32_t dwY2 )
{
    /* Optimize horizontal or vertical line drawing */
    if (( dwY1 == dwY2 ) || (dwX1 == dwX2))
    {
        LCD_DrawFilledRectangle( dwX1, dwY1, dwX2, dwY2 );
    }
    else
    {
        DrawLineBresenham( dwX1, dwY1, dwX2, dwY2 ) ;
    }
 
    return 0 ;
}
 
/**
 * \brief Draws a circle on LCD, at the given coordinates.
 *
 * \param dwX      X-coordinate of circle center.
 * \param dwY      Y-coordinate of circle center.
 * \param dwR      circle radius.
*/
extern uint32_t LCD_DrawCircle( uint32_t dwX, uint32_t dwY, uint32_t dwR )
{
    int32_t   d;    /* Decision Variable */
    uint32_t  curX; /* Current X Value */
    uint32_t  curY; /* Current Y Value */
 
    if (dwR == 0)
    {
        return 0;
    }
    d = 3 - (dwR << 1);
    curX = 0;
    curY = dwR;
 
    while (curX <= curY)
    {
        LCD_DrawPixel(dwX + curX, dwY + curY);
        LCD_DrawPixel(dwX + curX, dwY - curY);
        LCD_DrawPixel(dwX - curX, dwY + curY);
        LCD_DrawPixel(dwX - curX, dwY - curY);
        LCD_DrawPixel(dwX + curY, dwY + curX);
        LCD_DrawPixel(dwX + curY, dwY - curX);
        LCD_DrawPixel(dwX - curY, dwY + curX);
        LCD_DrawPixel(dwX - curY, dwY - curX);
 
        if (d < 0)
        {
            d += (curX << 2) + 6;
        }
        else
        {
            d += ((curX - curY) << 2) + 10;
            curY--;
        }
        curX++;
    }
    return 0;
}
 
extern uint32_t LCD_DrawFilledCircle( uint32_t dwX, uint32_t dwY, uint32_t dwRadius)
{
    signed int d ; /* Decision Variable */
    uint32_t dwCurX ; /* Current X Value */
    uint32_t dwCurY ; /* Current Y Value */
    uint32_t dwXmin, dwYmin;
 
    if (dwRadius == 0)
    {
        return 0;
    }
    d = 3 - (dwRadius << 1) ;
    dwCurX = 0 ;
    dwCurY = dwRadius ;
 
    while ( dwCurX <= dwCurY )
    {
        dwXmin = (dwCurX > dwX) ? 0 : dwX-dwCurX;
        dwYmin = (dwCurY > dwY) ? 0 : dwY-dwCurY;
        LCD_DrawFilledRectangle( dwXmin, dwYmin, dwX+dwCurX, dwYmin ) ;
        LCD_DrawFilledRectangle( dwXmin, dwY+dwCurY, dwX+dwCurX, dwY+dwCurY ) ;
        dwXmin = (dwCurY > dwX) ? 0 : dwX-dwCurY;
        dwYmin = (dwCurX > dwY) ? 0 : dwY-dwCurX;
        LCD_DrawFilledRectangle( dwXmin, dwYmin, dwX+dwCurY, dwYmin ) ;
        LCD_DrawFilledRectangle( dwXmin, dwY+dwCurX, dwX+dwCurY, dwY+dwCurX ) ;
 
        if ( d < 0 )
        {
            d += (dwCurX << 2) + 6 ;
        }
        else
        {
            d += ((dwCurX - dwCurY) << 2) + 10;
            dwCurY-- ;
        }
 
        dwCurX++ ;
    }
 
    return 0 ;
}
 
extern uint32_t LCD_DrawRectangle( uint32_t dwX1, uint32_t dwY1, uint32_t dwX2, uint32_t dwY2 )
{
    CheckBoxCoordinates(&dwX1, &dwY1, &dwX2, &dwY2);
 
    LCD_DrawFilledRectangle( dwX1, dwY1, dwX2, dwY1 ) ;
    LCD_DrawFilledRectangle( dwX1, dwY2, dwX2, dwY2 ) ;
 
    LCD_DrawFilledRectangle( dwX1, dwY1, dwX1, dwY2 ) ;
    LCD_DrawFilledRectangle( dwX2, dwY1, dwX2, dwY2 ) ;
 
    return 0 ;
}