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