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