summaryrefslogtreecommitdiff
path: root/peripherals/lcd/lcd.c
blob: 8c2f08e1922f0bd01508a23f96cc706357f8c700 (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
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support 
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, 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.
 * ----------------------------------------------------------------------------
 */

#include <board.h>

#if defined (AT91C_ID_LCDC)

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include "lcd.h"
#include <utility/assert.h>
#include <utility/trace.h>


//------------------------------------------------------------------------------
//         Exported functions
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
/// Enables the LCD controller, after waiting for the specified number of
/// frames.
/// \param frames  Number of frames before the LCD is enabled.
//------------------------------------------------------------------------------
void LCD_Enable(unsigned int frames)
{
    TRACE_DEBUG("LCD enable\n\r");
    ASSERT((frames & 0xFFFFFF80) == 0,
           "LCD_Enable: Wrong frames value.\n\r");
    if( (AT91C_BASE_LCDC->LCDC_PWRCON & AT91C_LCDC_BUSY) == AT91C_LCDC_BUSY ) {
        TRACE_DEBUG("LCD BUSY E\n\r");
    }
    AT91C_BASE_LCDC->LCDC_PWRCON = AT91C_LCDC_PWR | (frames << 1);
}

//------------------------------------------------------------------------------
/// Disables the LCD controller, after waiting for the specified number of
/// frames.
/// \param frames  Number of frames before the LCD is shut down.
//------------------------------------------------------------------------------
void LCD_Disable(unsigned int frames)
{
    TRACE_DEBUG("LCD disable\n\r");
    ASSERT((frames & 0xFFFFFF80) == 0,
           "LCD_Disable: Wrong frames value.\n\r");
    // Remove AT91C_LCDC_PWR
    AT91C_BASE_LCDC->LCDC_PWRCON = frames << 1;
    // wait LCDC Core is in idle state
    while( (AT91C_BASE_LCDC->LCDC_PWRCON & AT91C_LCDC_BUSY) == AT91C_LCDC_BUSY ) {
    }
    TRACE_DEBUG("LCD is in IDLE state\n\r");
}

//------------------------------------------------------------------------------
/// Enables the DMA of the LCD controller.
//------------------------------------------------------------------------------
void LCD_EnableDma(void)
{
    if( (AT91C_BASE_LCDC->LCDC_DMACON & AT91C_LCDC_DMABUSY) == AT91C_LCDC_DMABUSY ) {
        TRACE_DEBUG("LCD DMA BUSY E\n\r");
    }
    AT91C_BASE_LCDC->LCDC_DMACON = AT91C_LCDC_DMAEN;
}

//------------------------------------------------------------------------------
/// Disables the DMA of the LCD controller.
//------------------------------------------------------------------------------
void LCD_DisableDma(void)
{
    AT91C_BASE_LCDC->LCDC_DMACON = 0;
    // wait LCDC DMA is in idle state
    while( (AT91C_BASE_LCDC->LCDC_DMACON & AT91C_LCDC_DMABUSY) == AT91C_LCDC_DMABUSY ) {
    }
    TRACE_DEBUG("LCD DMA is in IDLE state\n\r");
}

//------------------------------------------------------------------------------
/// Enables the selected LDC interrupt sources.
/// \param sources  Interrupt sources to enable.
//------------------------------------------------------------------------------
void LCD_EnableInterrupts(unsigned int sources)
{
    AT91C_BASE_LCDC->LCDC_IER = sources;
}

//------------------------------------------------------------------------------
/// Configures the internal clock of the LCD controller given the master clock of
/// the system and the desired pixel clock in MHz.
/// \param masterClock  Master clock frequency.
/// \param pixelClock  Pixel clock frequency.
//------------------------------------------------------------------------------
void LCD_SetPixelClock(unsigned int masterClock, unsigned int pixelClock)
{
    #if defined(LCDC_PIXELCLOCK_DOUBLE)
    AT91C_BASE_LCDC->LCDC_LCDCON1 = ((masterClock / pixelClock) - 1) << 12;
    #else
    AT91C_BASE_LCDC->LCDC_LCDCON1 = ((masterClock / 2 / pixelClock) - 1) << 12;
    #endif
}

//------------------------------------------------------------------------------
/// DMA reset
//------------------------------------------------------------------------------
void LCD_DMAReset(void)
{
    // DMA Module should be reset only when disabled and in idle state
    if( AT91C_LCDC_DMABUSY == (AT91C_BASE_LCDC->LCDC_DMACON & AT91C_LCDC_DMABUSY)) {
        TRACE_ERROR("LCD BUSY so NO DMA RESET\n\r");
    }
    if( AT91C_LCDC_DMAEN == (AT91C_BASE_LCDC->LCDC_DMACON & AT91C_LCDC_DMAEN)) {
        TRACE_ERROR("DMA Enabled, so NO DMA RESET\n\r");
    }
    AT91C_BASE_LCDC->LCDC_DMACON = AT91C_LCDC_DMARST;
}

//------------------------------------------------------------------------------
/// Sets the type of display used with the LCD controller.
/// \param displayType  Type of display used.
//------------------------------------------------------------------------------
void LCD_SetDisplayType(unsigned int displayType)
{
    unsigned int value;

    ASSERT((displayType & ~AT91C_LCDC_DISTYPE) == 0,
           "LCD_SetDisplayType: Wrong display type value.\n\r");

    value = AT91C_BASE_LCDC->LCDC_LCDCON2;
    value &= ~AT91C_LCDC_DISTYPE;
    value |= displayType;
    AT91C_BASE_LCDC->LCDC_LCDCON2 = value;
}

//------------------------------------------------------------------------------
/// Sets the scan mode used by the LCD (either single scan or double-scan).
/// \param scanMode  Scan mode to use.
//------------------------------------------------------------------------------
void LCD_SetScanMode(unsigned int scanMode)
{
    unsigned int value;

    ASSERT((scanMode & ~AT91C_LCDC_SCANMOD) == 0,
           "LCD_SetScanMode: Wrong scan mode value.\n\r");

    value = AT91C_BASE_LCDC->LCDC_LCDCON2;
    value &= ~AT91C_LCDC_SCANMOD;
    value |= scanMode;
    AT91C_BASE_LCDC->LCDC_LCDCON2 = value;
}

//------------------------------------------------------------------------------
/// Sets the number of bits per pixel used by the LCD display.
/// \param bitsPerPixel  Number of bits per pixel to use.
//------------------------------------------------------------------------------
void LCD_SetBitsPerPixel(unsigned int bitsPerPixel)
{
    unsigned int value;

    ASSERT((bitsPerPixel & ~AT91C_LCDC_PIXELSIZE) == 0,
           "LCD_SetScanMode: Wrong bitsPerPixel value.\n\r");

    value = AT91C_BASE_LCDC->LCDC_LCDCON2;
    value &= ~AT91C_LCDC_PIXELSIZE;
    value |= bitsPerPixel;
    AT91C_BASE_LCDC->LCDC_LCDCON2 = value;
}

//------------------------------------------------------------------------------
/// Sets the LCDD, LCDVSYNC, LCDHSYNC, LCDDOTCLK and LCDDEN signal polarities.
/// \param lcdd  LCDD signal polarity.
/// \param lcdvsync  LCDVSYNC signal polarity.
/// \param lcdhsync  LCDHSYNC signal polarity.
/// \param lcddotclk  LCDDOTCLK signal polarity.
/// \param lcdden  LCDDEN signal polarity.
//------------------------------------------------------------------------------
void LCD_SetPolarities(
    unsigned int lcdd,
    unsigned int lcdvsync,
    unsigned int lcdhsync,
    unsigned int lcddotclk,
    unsigned int lcdden)
{
    unsigned int value;

    ASSERT((lcdd & ~AT91C_LCDC_INVVD) == 0,
           "LCD_SetPolarities: Wrong lcdd value.\n\r");
    ASSERT((lcdvsync & ~AT91C_LCDC_INVFRAME) == 0,
           "LCD_SetPolarities: Wrong lcdvsync value.\n\r");
    ASSERT((lcdhsync & ~AT91C_LCDC_INVLINE) == 0,
           "LCD_SetPolarities: Wrong lcdhsync value.\n\r");
    ASSERT((lcddotclk & ~AT91C_LCDC_INVCLK) == 0,
           "LCD_SetPolarities: Wrong lcddotclk value.\n\r");
    ASSERT((lcdden & ~AT91C_LCDC_INVDVAL) == 0,
           "LCD_SetPolarities: Wrong lcdden value.\n\r");

    value = AT91C_BASE_LCDC->LCDC_LCDCON2;
    value &= 0xFFFFE0FF;
    value |= lcdd | lcdvsync | lcdhsync | lcddotclk | lcdden;
    AT91C_BASE_LCDC->LCDC_LCDCON2 = value;
}

//------------------------------------------------------------------------------
/// Sets the LCD clock mode, i.e. always active or active only during display
/// period.
/// \param clockMode  Clock mode to use.
//------------------------------------------------------------------------------
void LCD_SetClockMode(unsigned int clockMode)
{
    unsigned int value;

    ASSERT((clockMode & ~AT91C_LCDC_CLKMOD) == 0,
           "LCD_SetScanMode: Wrong scan mode value.\n\r");

    value = AT91C_BASE_LCDC->LCDC_LCDCON2;
    value &= ~AT91C_LCDC_CLKMOD;
    value |= clockMode;
    AT91C_BASE_LCDC->LCDC_LCDCON2 = value;
}

//------------------------------------------------------------------------------
/// Sets the format of the frame buffer memory.
/// \param format  Memory ordering format.
//------------------------------------------------------------------------------
void LCD_SetMemoryFormat(unsigned int format)
{
    unsigned int value;

    ASSERT((format & ~AT91C_LCDC_MEMOR) == 0,
           "LCD_SetMemoryFormat: Wrong memory format value.\n\r");

    value = AT91C_BASE_LCDC->LCDC_LCDCON2;
    value &= ~AT91C_LCDC_MEMOR;
    value |= format;
    AT91C_BASE_LCDC->LCDC_LCDCON2 = value;
}

//------------------------------------------------------------------------------
/// Sets the size in pixel of the LCD display.
/// \param width  Width in pixel of the LCD display.
/// \param height  Height in pixel of the LCD display.
//------------------------------------------------------------------------------
void LCD_SetSize(unsigned int width, unsigned int height)
{
    ASSERT(((width - 1) & 0xFFFFF800) == 0,
           "LCD_SetSize: Wrong width value.\n\r");
    ASSERT(((height - 1) & 0xFFFFF800) == 0,
           "LCD_SetSize: Wrong height value.\n\r");

    AT91C_BASE_LCDC->LCDC_LCDFRCFG = (((width - 1) << 21) &AT91C_LCDC_HOZVAL)
                                    |((height - 1) & AT91C_LCDC_LINEVAL);
}

//------------------------------------------------------------------------------
/// Sets the vertical timings of the LCD controller. Only meaningful when
/// using a TFT display.
/// \param vfp  Number of idle lines at the end of a frame.
/// \param vbp  Number of idle lines at the beginning of a frame.
/// \param vpw  Vertical synchronization pulse width in number of lines.
/// \param vhdly  Delay between LCDVSYNC edge and LCDHSYNC rising edge, in
///               LCDDOTCLK cycles.
//------------------------------------------------------------------------------
void LCD_SetVerticalTimings(
    unsigned int vfp,
    unsigned int vbp,
    unsigned int vpw,
    unsigned int vhdly)
{
    ASSERT((vfp & 0xFFFFFF00) == 0,
           "LCD_SetVerticalTimings: Wrong vfp value.\n\r");
    ASSERT((vbp & 0xFFFFFF00) == 0,
           "LCD_SetVerticalTimings: Wrong vbp value.\n\r");
    ASSERT(((vpw-1) & 0xFFFFFFC0) == 0,
           "LCD_SetVerticalTimings: Wrong vpw value.\n\r");
    ASSERT(((vhdly-1) & 0xFFFFFFF0) == 0,
           "LCD_SetVerticalTimings: Wrong vhdly value.\n\r");

    AT91C_BASE_LCDC->LCDC_TIM1 = (vfp & AT91C_LCDC_VFP)
                                 | ((vbp << 8)&AT91C_LCDC_VBP)
                                 | (((vpw-1) << 16)&AT91C_LCDC_VPW)
                                 | (((vhdly-1) << 24)&AT91C_LCDC_VHDLY);
}

//------------------------------------------------------------------------------
/// Sets the horizontal timings of the LCD controller. Meaningful for both
/// STN and TFT displays.
/// \param hbp  Number of idle LCDDOTCLK cycles at the beginning of a line.
/// \param hpw  Width of the LCDHSYNC pulse, in LCDDOTCLK cycles.
/// \param hfp  Number of idel LCDDOTCLK cycles at the end of a line.
//------------------------------------------------------------------------------
void LCD_SetHorizontalTimings(
    unsigned int hbp,
    unsigned int hpw,
    unsigned int hfp)
{
    ASSERT(((hbp-1) & 0xFFFFFF00) == 0,
           "LCD_SetHorizontalTimings: Wrong hbp value.\n\r");
    ASSERT(((hpw-1) & 0xFFFFFFC0) == 0,
           "LCD_SetHorizontalTimings: Wrong hpw value.\n\r");
    ASSERT(((hfp-1) & 0xFFFFFF00) == 0,
           "LCD_SetHorizontalTimings: Wrong hfp value.\n\r");

    AT91C_BASE_LCDC->LCDC_TIM2 = ((hbp-1)&AT91C_LCDC_HBP)
                              | (((hpw-1) << 8)&AT91C_LCDC_HPW)
                              | (((hfp-1) << 21)&AT91C_LCDC_HFP);
}

//------------------------------------------------------------------------------
/// Sets the address of the frame buffer in the LCD controller DMA. When using
/// dual-scan mode, this is the upper frame buffer.
/// \param address  Frame buffer address.
//------------------------------------------------------------------------------
void* LCD_SetFrameBufferAddress(void *address)
{
    void *pOldBuffer;
    
    pOldBuffer = (void *) AT91C_BASE_LCDC->LCDC_BA1;
    AT91C_BASE_LCDC->LCDC_BA1 = (unsigned int) address;
    
    return pOldBuffer;
}

//------------------------------------------------------------------------------
/// Sets the size in pixels of a frame (height * width * bpp).
/// \param frameSize  Size of frame in pixels.
//------------------------------------------------------------------------------
void LCD_SetFrameSize(unsigned int frameSize)
{
    ASSERT((frameSize & 0xFF800000) == 0,
           "LCD_SetFrameSize: Wrong frameSize value.\n\r");

    AT91C_BASE_LCDC->LCDC_FRMCFG = (frameSize& AT91C_LCDC_FRSIZE)
                                 | (AT91C_BASE_LCDC->LCDC_FRMCFG & AT91C_LCDC_BLENGTH);
}

//------------------------------------------------------------------------------
/// Sets the DMA controller burst length.
/// \param burstLength  Desired burst length.
//------------------------------------------------------------------------------
void LCD_SetBurstLength(unsigned int burstLength)
{
    ASSERT(((burstLength-1) & 0xFFFFFF80) == 0,
           "LCD_SetBurstLength: Wrong burstLength value.\n\r");

    AT91C_BASE_LCDC->LCDC_FRMCFG &= ~AT91C_LCDC_BLENGTH;
    AT91C_BASE_LCDC->LCDC_FRMCFG |= (((burstLength-1) << 24) & AT91C_LCDC_BLENGTH);

    AT91C_BASE_LCDC->LCDC_FIFO = (2048 - (2 * burstLength + 3)) & AT91C_LCDC_FIFOTH;
}

//------------------------------------------------------------------------------
/// Sets the prescaler value of the contrast control PWM.
/// \param prescaler  Desired prescaler value.
//------------------------------------------------------------------------------
void LCD_SetContrastPrescaler(unsigned int prescaler)
{
    ASSERT((prescaler & ~AT91C_LCDC_PS) == 0,
           "LCD_SetContrastPrescaler: Wrong prescaler value\n\r");

    AT91C_BASE_LCDC->LCDC_CTRSTCON &= ~AT91C_LCDC_PS;
    AT91C_BASE_LCDC->LCDC_CTRSTCON |= prescaler;
}

//------------------------------------------------------------------------------
/// Sets the polarity of the contrast PWM.
/// \param polarity  PWM polarity
//------------------------------------------------------------------------------
void LCD_SetContrastPolarity(unsigned int polarity)
{
    ASSERT((polarity & ~AT91C_LCDC_POL) == 0,
           "LCD_SetContrastPolarity: Wrong polarity value\n\r");

    AT91C_BASE_LCDC->LCDC_CTRSTCON &= ~AT91C_LCDC_POL;
    AT91C_BASE_LCDC->LCDC_CTRSTCON |= polarity;
}

//------------------------------------------------------------------------------
/// Sets the threshold value of the constrast PWM.
/// \param value  PWM threshold value.
//------------------------------------------------------------------------------
void LCD_SetContrastValue(unsigned int value)
{
    ASSERT((value & ~AT91C_LCDC_CVAL) == 0,
           "LCD_SetContrastValue: Wrong value.\n\r");

    AT91C_BASE_LCDC->LCDC_CTRSTVAL = value;
}

//------------------------------------------------------------------------------
/// Enables the contrast PWM generator.
//------------------------------------------------------------------------------
void LCD_EnableContrast(void)
{
    AT91C_BASE_LCDC->LCDC_CTRSTCON |= AT91C_LCDC_ENA_PWMGEMENABLED;
}

//------------------------------------------------------------------------------
/// Decode the RGB file
/// \param file  Buffer which holds the RGB file.
/// \param bufferLCD  Buffer in which to store the decoded image adapted to LCD.
/// \param width  Buffer width in pixels.
/// \param height  Buffer height in pixels.
/// \param bpp  Number of bits per pixels that the buffer stores.
//------------------------------------------------------------------------------
void LCD_DecodeRGB(
    unsigned char *file,
    unsigned char *bufferLCD,
    unsigned int width,
    unsigned int height,
    unsigned char bpp)
{
    unsigned int offsetLine=0, offsetLCD=0;
    unsigned int offset=1;

    while( offset < (BOARD_LCD_HEIGHT)) {
        //TRACE_DEBUG("LCD:%d LINE:%d off:%d\n\r", offsetLCD,  offsetLine, offset);
        if( width < BOARD_LCD_WIDTH ) {
            //TRACE_DEBUG("width < BOARD_LCD_WIDTH\n\r");
            while( offsetLine < (width*offset*(bpp/8)) ) {
                bufferLCD[offsetLCD] = file[offsetLine];
                offsetLine++;
                offsetLCD++;
            }
            //TRACE_DEBUG("add white\n\r");
            while( offsetLCD < (BOARD_LCD_WIDTH*offset*(bpp/8)) ) {
                bufferLCD[offsetLCD] = 0;
                //offsetLine++;
                offsetLCD++;
            }
        }
        else {
            //TRACE_DEBUG(">");
            while( offsetLCD < (BOARD_LCD_WIDTH*offset*(bpp/8)) ) {
                bufferLCD[offsetLCD] = file[offsetLine];
                offsetLine++;
                offsetLCD++;
            }
            //TRACE_DEBUG("r ");
            while( offsetLine < (width*offset*(bpp/8)) ) {
                offsetLine++;
            }            
        }
        offset++;
    }        
}

#endif

personal git repositories of Harald Welte. Your mileage may vary