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