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