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