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