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