Move some define in sam3x sd implementaion. Define sd speeds macro to clarify the...
[bertos.git] / bertos / drv / lcd_32122a.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  * \author Bernie Innocenti <bernie@codewiz.org>
37  * \author Stefano Fedrigo <aleph@develer.com>
38  *
39  */
40
41
42 #include "lcd_32122a.h"
43
44 #include "hw/hw_lcd_32122a.h"
45
46 #include "cfg/cfg_lcd_32122a.h"
47
48 #include <cfg/macros.h> /* BV() */
49 #include <cfg/debug.h>
50 #include <cfg/module.h>
51
52 #include <gfx/gfx.h>
53
54 #include <drv/timer.h>
55 #include <drv/pwm.h>
56
57 #include <cpu/irq.h>
58 #include <cpu/types.h>
59
60
61 /** Number of LCD pages */
62 #define LCD_PAGES 4
63
64 /** Width of an LCD page */
65 #define LCD_PAGESIZE (LCD_WIDTH / 2)
66
67 /**
68  * \name 32122A Commands
69  * @{
70  */
71 #define LCD_CMD_DISPLAY_ON  0xAF
72 #define LCD_CMD_DISPLAY_OFF 0xAE
73 #define LCD_CMD_STARTLINE   0xC0
74 #define LCD_CMD_PAGEADDR    0xB8
75 #define LCD_CMD_COLADDR     0x00
76 #define LCD_CMD_ADC_LEFT    0xA1
77 #define LCD_CMD_ADC_RIGHT   0xA0
78 #define LCD_CMD_STATIC_OFF  0xA4
79 #define LCD_CMD_STATIC_ON   0xA5
80 #define LCD_CMD_DUTY_32     0xA9
81 #define LCD_CMD_DUTY_16     0xA8
82 #define LCD_CMD_RMW_ON      0xE0
83 #define LCD_CMD_RMW_OFF     0xEE
84 #define LCD_CMD_RESET       0xE2
85 /*@}*/
86
87
88 /* Status flags */
89 #define LCDF_BUSY BV(7)
90
91 #if CONFIG_LCD_WAIT
92         /**
93          * \code
94          *      __              __
95          * RS   __\____________/__
96          *         ____________
97          * R/W  __/            \__
98          *            _______
99          * E1   _____/       \____
100          *        ______      ____
101          * DATA X/      \====/
102          *
103          * \endcode
104          */
105         #define WAIT_LCD \
106                 do { \
107                         uint8_t status; \
108                         LCD_DB_IN; \
109                         do { \
110                                 LCD_SET_RD; \
111                                 LCD_CLR_A0; \
112                                 LCD_SET_E1; \
113                                 LCD_DELAY_READ; \
114                                 status = LCD_READ; \
115                                 LCD_CLR_E1; \
116                                 LCD_SET_A0; \
117                                 LCD_CLR_RD; \
118                         } while (status & LCDF_BUSY); \
119                         LCD_DB_OUT; \
120                 } while (0)
121 #else /* CONFIG_LCD_WAIT */
122         #define WAIT_LCD do {} while(0)
123 #endif /* CONFIG_LCD_WAIT */
124
125
126 /**
127  * Raster buffer to draw into.
128  *
129  * Bits in the bitmap bytes have vertical orientation,
130  * as required by the LCD driver.
131  */
132 static uint8_t lcd_raster[RAST_SIZE(LCD_WIDTH, LCD_HEIGHT)];
133
134 /** Default LCD bitmap */
135 struct Bitmap lcd_bitmap;
136
137
138 #if CONFIG_LCD_SOFTINT_REFRESH
139
140 /** Timer for regular LCD refresh */
141 static Timer lcd_refresh_timer;
142
143 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
144
145
146 INLINE void lcd_32122_cmd(uint8_t cmd, uint8_t chip)
147 {
148         WAIT_LCD;
149
150         /*      __              __
151          * A0   __\____________/__
152          *
153          * R/W  __________________
154          *            ______
155          * E1   _____/      \_____
156          *
157          * DATA --<============>--
158          */
159         LCD_WRITE(cmd);
160         //LCD_DB_OUT;
161         LCD_CLR_A0;
162         LCD_SET_E(chip);
163         LCD_DELAY_WRITE;
164         LCD_CLR_E(chip);
165         LCD_SET_A0;
166         //LCD_DB_IN;
167
168 }
169
170
171 INLINE uint8_t lcd_32122_read(uint8_t chip)
172 {
173         uint8_t data;
174
175         WAIT_LCD;
176
177         /**
178          * \code
179          *      __________________
180          * A0   __/            \__
181          *         ____________
182          * R/W  __/            \__
183          *            _______
184          * E1   _____/       \____
185          *
186          * DATA -------<=====>----
187          *
188          * \endcode
189          */
190         LCD_DB_IN;
191         //LCD_SET_A0;
192         LCD_SET_RD;
193         LCD_SET_E(chip);
194         LCD_DELAY_READ;
195         data = LCD_READ;
196         LCD_CLR_E(chip);
197         LCD_CLR_RD;
198         //LCD_CLR_A0;
199         LCD_DB_OUT;
200
201         return data;
202 }
203
204 INLINE void lcd_32122_write(uint8_t c, uint8_t chip)
205 {
206         WAIT_LCD;
207
208         /**
209          * \code
210          *      __________________
211          * A0   ___/          \___
212          *
213          * R/W  __________________
214          *            ______
215          * E1   _____/      \_____
216          *
217          * DATA -<==============>-
218          *
219          * \endcode
220          */
221         LCD_WRITE(c);
222         //LCD_DB_OUT;
223         //LCD_SET_A0;
224         LCD_SET_E(chip);
225         LCD_DELAY_WRITE;
226         LCD_CLR_E(chip);
227         //LCD_CLR_A0;
228         //LCD_DB_IN;
229 }
230
231 static void lcd_32122_clear(void)
232 {
233         uint8_t page, j;
234
235         for (page = 0; page < LCD_PAGES; ++page)
236         {
237                 lcd_32122_cmd(LCD_CMD_COLADDR, LCDF_E1 | LCDF_E2);
238                 lcd_32122_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
239                 for (j = 0; j < LCD_PAGESIZE; j++)
240                         lcd_32122_write(0, LCDF_E1 | LCDF_E2);
241         }
242 }
243
244
245 static void lcd_32122_writeRaster(const uint8_t *raster)
246 {
247         uint8_t page, rows;
248         const uint8_t *right_raster;
249
250         for (page = 0; page < LCD_PAGES; ++page)
251         {
252                 lcd_32122_cmd(LCD_CMD_PAGEADDR | page, LCDF_E1 | LCDF_E2);
253                 lcd_32122_cmd(LCD_CMD_COLADDR | 0, LCDF_E1 | LCDF_E2);
254
255                 /* Super optimized lamer loop */
256                 right_raster = raster + LCD_PAGESIZE;
257                 rows = LCD_PAGESIZE;
258                 do
259                 {
260                         lcd_32122_write(*raster++, LCDF_E1);
261                         lcd_32122_write(*right_raster++, LCDF_E2);
262                 }
263                 while (--rows);
264                 raster = right_raster;
265         }
266 }
267
268 #if CONFIG_LCD_SOFTINT_REFRESH
269
270 static void lcd_32122_refreshSoftint(void)
271 {
272         lcd_32122_blitBitmap(&lcd_bitmap);
273         timer_setDelay(&lcd_refresh_timer, ms_to_ticks(CONFIG_LCD_REFRESH));
274         timer_add(&lcd_refresh_timer);
275 }
276
277 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
278
279 /**
280  * Set LCD contrast PWM.
281  */
282 void lcd_32122_setPwm(int duty)
283 {
284         ASSERT(duty >= LCD_MIN_PWM);
285         ASSERT(duty <= LCD_MAX_PWM);
286
287         pwm_setDuty(LCD_PWM_CH, duty);
288         pwm_enable(LCD_PWM_CH, true);
289 }
290
291 /**
292  * Update the LCD display with data from the provided bitmap.
293  */
294 void lcd_32122_blitBitmap(const Bitmap *bm)
295 {
296         lcd_32122_writeRaster(bm->raster);
297 }
298
299
300 /**
301  * Initialize LCD subsystem.
302  *
303  * \note The PWM used for LCD contrast is initialized in drv/pwm.c
304  *       because it is the same PWM used for output attenuation.
305  */
306 void lcd_32122_init(void)
307 {
308         MOD_CHECK(timer);
309
310         pwm_init();
311
312         lcd_32122a_hw_bus_init();
313         LCD_32122_RESET();
314
315         lcd_32122_cmd(LCD_CMD_RESET, LCDF_E1 | LCDF_E2);
316         lcd_32122_cmd(LCD_CMD_DISPLAY_ON, LCDF_E1 | LCDF_E2);
317         lcd_32122_cmd(LCD_CMD_STARTLINE | 0, LCDF_E1 | LCDF_E2);
318
319
320         lcd_32122_clear();
321         lcd_32122_setPwm(LCD_DEF_PWM);
322
323         gfx_bitmapInit(&lcd_bitmap, lcd_raster, LCD_WIDTH, LCD_HEIGHT);
324         gfx_bitmapClear(&lcd_bitmap);
325
326 #if CONFIG_LCD_SOFTINT_REFRESH
327         /* Init IRQ driven LCD refresh */
328         timer_setSoftint(&lcd_refresh_timer, (Hook)lcd_32122_refreshSoftint, 0);
329         timer_setDelay(&lcd_refresh_timer, ms_to_ticks(CONFIG_LCD_REFRESH));
330         timer_add(&lcd_refresh_timer);
331 #endif /* CONFIG_LCD_SOFTINT_REFRESH */
332
333 }
334