2054e41cc1a2a2f75030d95908519e23dddc3cd7
[bertos.git] / drv / lcd_gfx.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005, 2006 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2001 Bernardo Innocenti <bernie@codewiz.org>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \author Bernardo Innocenti <bernie@develer.com>
12  * \author Stefano Fedrigo <aleph@develer.com>
13  *
14  * \brief Displaytech 32122A LCD driver
15  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.4  2006/04/27 05:40:11  bernie
20  *#* Naming convention fixes; Partial merge from project_grl.
21  *#*
22  *#* Revision 1.3  2006/02/10 12:35:31  bernie
23  *#* Enforce CONFIG_* definitions.
24  *#*
25  *#* Revision 1.2  2006/01/23 23:11:27  bernie
26  *#* Use RASTER_SIZE() to compute... err... the raster size.
27  *#*
28  *#* Revision 1.1  2006/01/16 03:50:57  bernie
29  *#* Import into DevLib.
30  *#*
31  *#*/
32
33 #include "lcd.h"
34 #include <gfx/gfx.h>
35 #include <drv/timer.h>
36
37 #include <cfg/cpu.h>
38 #include <hw.h>
39 #include <cfg/macros.h> /* BV() */
40 #include <cfg/debug.h>
41
42 #include <avr/io.h>
43 #include <stdbool.h>
44 #include <inttypes.h>
45
46 /* Configuration sanity checks */
47 #if !defined(CONFIG_LCD_SOFTINT_REFRESH) || (CONFIG_LCD_SOFTINT_REFRESH != 0 && CONFIG_LCD_SOFTINT_REFRESH != 1)
48         #error CONFIG_LCD_SOFTINT_REFRESH must be defined to either 0 or 1
49 #endif
50 #if !defined(CONFIG_LCD_SOFTINT_REFRESH) || (CONFIG_LCD_SOFTINT_REFRESH != 0 && CONFIG_LCD_SOFTINT_REFRESH != 1)
51         #error CONFIG_LCD_SOFTINT_REFRESH must be defined to either 0 or 1
52 #endif
53
54
55 #if CONFIG_LCD_SOFTINT_REFRESH
56
57         /*! Interval between softint driven lcd refresh */
58 #       define LCD_REFRESH_INTERVAL 20  /* 20ms -> 50fps */
59
60 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
61
62 /*! Number of LCD pages */
63 #define LCD_PAGES 4
64
65 /*! Width of an LCD page */
66 #define LCD_PAGESIZE (LCD_WIDTH / 2)
67
68 /*!
69  * \name LCD I/O pins/ports
70  * @{
71  */
72 #define LCD_PF_DB0   PF4
73 #define LCD_PF_DB1   PF5
74 #define LCD_PF_DB2   PF6
75 #define LCD_PF_DB3   PF7
76 #define LCD_PD_DB4   PD4
77 #define LCD_PD_DB5   PD5
78 #define LCD_PD_DB6   PD6
79 #define LCD_PD_DB7   PD7
80 #define LCD_PB_A0    PB0
81 #define LCD_PE_RW    PE7
82 #define LCD_PE_E1    PE2
83 #define LCD_PE_E2    PE6
84 /*@}*/
85
86 /*!
87  * \name DB high nibble (DB[4-7])
88  * @{
89  */
90 #define LCD_DATA_HI_PORT    PORTD
91 #define LCD_DATA_HI_PIN     PIND
92 #define LCD_DATA_HI_DDR     DDRD
93 #define LCD_DATA_HI_SHIFT   0
94 #define LCD_DATA_HI_MASK    0xF0
95 /*@}*/
96
97 /*!
98  * \name DB low nibble (DB[0-3])
99  * @{
100  */
101 #define LCD_DATA_LO_PORT    PORTF
102 #define LCD_DATA_LO_PIN     PINF
103 #define LCD_DATA_LO_DDR     DDRF
104 #define LCD_DATA_LO_SHIFT   4
105 #define LCD_DATA_LO_MASK    0xF0
106 /*@}*/
107
108 /*!
109  * \name LCD bus control macros
110  * @{
111  */
112 #define LCD_CLR_A0   (PORTB &= ~BV(LCD_PB_A0))
113 #define LCD_SET_A0   (PORTB |=  BV(LCD_PB_A0))
114 #define LCD_CLR_RD   (PORTE &= ~BV(LCD_PE_RW))
115 #define LCD_SET_RD   (PORTE |=  BV(LCD_PE_RW))
116 #define LCD_CLR_E1   (PORTE &= ~BV(LCD_PE_E1))
117 #define LCD_SET_E1   (PORTE |=  BV(LCD_PE_E1))
118 #define LCD_CLR_E2   (PORTE &= ~BV(LCD_PE_E2))
119 #define LCD_SET_E2   (PORTE |=  BV(LCD_PE_E2))
120 #define LCD_SET_E(x) (PORTE |= (x))
121 #define LCD_CLR_E(x) (PORTE &= ~(x))
122 /*@}*/
123
124 /*!
125  * \name Chip select bits for LCD_SET_E()
126  * @{
127  */
128 #define LCDF_E1 (BV(LCD_PE_E1))
129 #define LCDF_E2 (BV(LCD_PE_E2))
130 /*@}*/
131
132 /*! Read from the LCD data bus (DB[0-7]) */
133 #define LCD_READ ( \
134                 ((LCD_DATA_LO_PIN & LCD_DATA_LO_MASK) >> LCD_DATA_LO_SHIFT) | \
135                 ((LCD_DATA_HI_PIN & LCD_DATA_HI_MASK) >> LCD_DATA_HI_SHIFT) \
136         )
137
138 /*! Write to the LCD data bus (DB[0-7]) */
139 #define LCD_WRITE(d) \
140         do { \
141                 LCD_DATA_LO_PORT = (LCD_DATA_LO_PORT & ~LCD_DATA_LO_MASK) | (((d)<<LCD_DATA_LO_SHIFT) & LCD_DATA_LO_MASK); \
142                 LCD_DATA_HI_PORT = (LCD_DATA_HI_PORT & ~LCD_DATA_HI_MASK) | (((d)<<LCD_DATA_HI_SHIFT) & LCD_DATA_HI_MASK); \
143         } while (0)
144
145 /*! Set data bus direction to output (write to display) */
146 #define LCD_DB_OUT \
147         do { \
148                 LCD_DATA_LO_DDR |= LCD_DATA_LO_MASK; \
149                 LCD_DATA_HI_DDR |= LCD_DATA_HI_MASK; \
150         } while (0)
151
152 /*! Set data bus direction to input (read from display) */
153 #define LCD_DB_IN \
154         do { \
155                 LCD_DATA_LO_DDR &= ~LCD_DATA_LO_MASK; \
156                 LCD_DATA_HI_DDR &= ~LCD_DATA_HI_MASK; \
157         } while (0)
158
159 /*! Delay for tEW (160ns) */
160 #define LCD_DELAY_WRITE \
161         do { \
162                 NOP; \
163                 NOP; \
164         } while (0)
165
166 /*! Delay for tACC6 (180ns) */
167 #define LCD_DELAY_READ \
168         do { \
169                 NOP; \
170                 NOP; \
171                 NOP; \
172         } while (0)
173
174
175 /*!
176  * \name 32122A Commands
177  * @{
178  */
179 #define LCD_CMD_DISPLAY_ON  0xAF
180 #define LCD_CMD_DISPLAY_OFF 0xAE
181 #define LCD_CMD_STARTLINE   0xC0
182 #define LCD_CMD_PAGEADDR    0xB8
183 #define LCD_CMD_COLADDR     0x00
184 #define LCD_CMD_ADC_LEFT    0xA1
185 #define LCD_CMD_ADC_RIGHT   0xA0
186 #define LCD_CMD_STATIC_OFF  0xA4
187 #define LCD_CMD_STATIC_ON   0xA5
188 #define LCD_CMD_DUTY_32     0xA9
189 #define LCD_CMD_DUTY_16     0xA8
190 #define LCD_CMD_RMW_ON      0xE0
191 #define LCD_CMD_RMW_OFF     0xEE
192 #define LCD_CMD_RESET       0xE2
193 /*@}*/
194
195 MOD_DEFINE(lcd)
196
197
198 /* Status flags */
199 #define LCDF_BUSY BV(7)
200
201 #if CONFIG_LCD_WAIT
202 /*!
203  * \code
204  *      __              __
205  * RS   __\____________/__
206  *         ____________
207  * R/W  __/            \__
208  *            _______
209  * E1   _____/       \____
210  *        ______      ____
211  * DATA X/      \====/
212  *
213  * \endcode
214  */
215 #define WAIT_LCD \
216         do { \
217                 uint8_t status; \
218                 LCD_DB_IN; \
219                 do { \
220                         LCD_SET_RD; \
221                         LCD_CLR_A0; \
222                         LCD_SET_E1; \
223                         LCD_DELAY_READ; \
224                         status = LCD_READ; \
225                         LCD_CLR_E1; \
226                         LCD_SET_A0; \
227                         LCD_CLR_RD; \
228                 } while (status & LCDF_BUSY); \
229                 LCD_DB_OUT; \
230         } while (0)
231
232 #else /* CONFIG_LCD_WAIT */
233
234 #define WAIT_LCD do {} while(0)
235
236 #endif /* CONFIG_LCD_WAIT */
237
238
239 /*!
240  * Raster buffer to draw into.
241  *
242  * Bits in the bitmap bytes have vertical orientation,
243  * as required by the LCD driver.
244  */
245 DECLARE_WALL(wall_before_raster, WALL_SIZE)
246 static uint8_t lcd_raster[RASTER_SIZE(LCD_WIDTH, LCD_HEIGHT)];
247 DECLARE_WALL(wall_after_raster, WALL_SIZE)
248
249 /*! Default LCD bitmap */
250 struct Bitmap lcd_bitmap;
251
252
253 #if CONFIG_LCD_SOFTINT_REFRESH
254
255 /*! Timer for regular LCD refresh */
256 static Timer *lcd_refresh_timer;
257
258 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
259
260
261 /*
262 static bool lcd_check(void)
263 {
264         uint8_t status;
265         uint16_t retries = 32768;
266         PORTA = 0xFF;
267         DDRA = 0x00;
268         do {
269                 cbi(PORTC, PCB_LCD_RS);
270                 sbi(PORTC, PCB_LCD_RW);
271                 sbi(PORTC, PCB_LCD_E);
272                 --retries;
273                 NOP;
274                 status = PINA;
275                 cbi(PORTC, PCB_LCD_E);
276                 cbi(PORTC, PCB_LCD_RW);
277         } while ((status & LCDF_BUSY) && retries);
278
279         return (retries != 0);
280 }
281 */
282
283
284 static inline void lcd_cmd(uint8_t cmd, uint8_t chip)
285 {
286         WAIT_LCD;
287
288         /*      __              __
289          * A0   __\____________/__
290          *
291          * R/W  __________________
292          *            ______
293          * E1   _____/      \_____
294          *
295          * DATA --<============>--
296          */
297         LCD_WRITE(cmd);
298         //LCD_DB_OUT;
299         LCD_CLR_A0;
300         LCD_SET_E(chip);
301         LCD_DELAY_WRITE;
302         LCD_CLR_E(chip);
303         LCD_SET_A0;
304         //LCD_DB_IN;
305 }
306
307
308 static inline uint8_t lcd_read(uint8_t chip)
309 {
310         uint8_t data;
311
312         WAIT_LCD;
313
314         /*!
315          * \code
316          *      __________________
317          * A0   __/            \__
318          *         ____________
319          * R/W  __/            \__
320          *            _______
321          * E1   _____/       \____
322          *
323          * DATA -------<=====>----
324          *
325          * \endcode
326          */
327         LCD_DB_IN;
328         //LCD_SET_A0;
329         LCD_SET_RD;
330         LCD_SET_E(chip);
331         LCD_DELAY_READ;
332         data = LCD_READ;
333         LCD_CLR_E(chip);
334         LCD_CLR_RD;
335         //LCD_CLR_A0;
336         LCD_DB_OUT;
337
338         return data;
339 }
340
341
342 static inline void lcd_write(uint8_t c, uint8_t chip)
343 {
344         WAIT_LCD;
345
346         /*!
347          * \code
348          *      __________________
349          * A0   ___/          \___
350          *
351          * R/W  __________________
352          *            ______
353          * E1   _____/      \_____
354          *
355          * DATA -<==============>-
356          *
357          * \endcode
358          */
359         LCD_WRITE(c);
360         //LCD_DB_OUT;
361         //LCD_SET_A0;
362         LCD_SET_E(chip);
363         LCD_DELAY_WRITE;
364         LCD_CLR_E(chip);
365         //LCD_CLR_A0;
366         //LCD_DB_IN;
367 }
368
369
370 /*!
371  * Set LCD contrast PWM.
372  */
373 void lcd_setPwm(int duty)
374 {
375         ASSERT(duty >= LCD_MIN_PWM);
376         ASSERT(duty <= LCD_MAX_PWM);
377
378         OCR3C = duty;
379 }
380
381
382 static void lcd_clear(void)
383 {
384         uint8_t page, j;
385
386         for (page = 0; page < LCD_PAGES; ++page)
387         {
388                 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
389                 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
390                 for (j = 0; j < LCD_PAGESIZE; j++)
391                         lcd_write(0, LCDF_E1 | LCDF_E2);
392         }
393 }
394
395
396 static void lcd_writeRaster(const uint8_t *raster)
397 {
398         uint8_t page, rows;
399         const uint8_t *right_raster;
400
401         CHECK_WALL(wall_before_raster);
402         CHECK_WALL(wall_after_raster);
403
404         for (page = 0; page < LCD_PAGES; ++page)
405         {
406                 lcd_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
407                 lcd_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
408
409                 /* Super optimized lamer loop */
410                 right_raster = raster + LCD_PAGESIZE;
411                 rows = LCD_PAGESIZE;
412                 do
413                 {
414                         lcd_write(*raster++, LCDF_E1);
415                         lcd_write(*right_raster++, LCDF_E2);
416                 }
417                 while (--rows);
418                 raster = right_raster;
419         }
420 }
421
422 /**
423  * Update the LCD display with data from the provided bitmap.
424  */
425 void lcd_blitBitmap(Bitmap *bm)
426 {
427         MOD_CHECK(lcd);
428         lcd_writeRaster(bm->raster);
429 }
430
431
432 #if CONFIG_LCD_SOFTINT_REFRESH
433
434 static void lcd_refreshSoftint(void)
435 {
436         lcd_blit_bitmap(&lcd_bitmap);
437         timer_add(lcd_refresh_timer);
438 }
439
440 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
441
442
443 /*!
444  * Initialize LCD subsystem.
445  *
446  * \note The PWM used for LCD contrast is initialized in drv/pwm.c
447  *       because it is the same PWM used for output attenuation.
448  */
449 void lcd_init(void)
450 {
451         MOD_CHECK(timer);
452
453         // FIXME: interrupts are already disabled when we get here?!?
454         cpuflags_t flags;
455         IRQ_SAVE_DISABLE(flags);
456
457         PORTB |= BV(LCD_PB_A0);
458         DDRB |= BV(LCD_PB_A0);
459
460         PORTE &= ~(BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2));
461         DDRE |= BV(LCD_PE_RW) | BV(LCD_PE_E1) | BV(LCD_PE_E2);
462
463 /* LCD hw reset
464         LCD_RESET_PORT |= BV(LCD_RESET_BIT);
465         LCD_RESET_DDR |= BV(LCD_RESET_BIT);
466         LCD_DELAY_WRITE;
467         LCD_DELAY_WRITE;
468         LCD_RESET_PORT &= ~BV(LCD_RESET_BIT);
469         LCD_DELAY_WRITE;
470         LCD_DELAY_WRITE;
471         LCD_RESET_PORT |= BV(LCD_RESET_BIT);
472 */
473         /*
474          * Data bus is in output state most of the time:
475          * LCD r/w functions assume it is left in output state
476          */
477         LCD_DB_OUT;
478
479         // Wait for RST line to stabilize at Vcc.
480         IRQ_ENABLE;
481         timer_delay(20);
482         IRQ_SAVE_DISABLE(flags);
483
484         lcd_cmd(LCD_CMD_RESET, LCDF_E1 | LCDF_E2);
485         lcd_cmd(LCD_CMD_DISPLAY_ON, LCDF_E1 | LCDF_E2);
486         lcd_cmd(LCD_CMD_STARTLINE | 0, LCDF_E1 | LCDF_E2);
487
488         /* Initialize anti-corruption walls for raster */
489         INIT_WALL(wall_before_raster);
490         INIT_WALL(wall_after_raster);
491
492         IRQ_RESTORE(flags);
493
494         lcd_clear();
495         lcd_setpwm(LCD_DEF_PWM);
496
497         gfx_bitmapInit(&lcd_bitmap, lcd_raster, LCD_WIDTH, LCD_HEIGHT);
498         gfx_bitmapClear(&lcd_bitmap);
499
500 #if CONFIG_LCD_SOFTINT_REFRESH
501
502         /* Init IRQ driven LCD refresh */
503         lcd_refresh_timer = timer_new();
504         ASSERT(lcd_refresh_timer != NULL);
505         INITEVENT_INT(&lcd_refresh_timer->expire, (Hook)lcd_refresh_softint, 0);
506         lcd_refresh_timer->delay = LCD_REFRESH_INTERVAL;
507         timer_add(lcd_refresh_timer);
508
509 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
510
511         MOD_INIT(lcd);
512 }