lm3s1968: RIT128x96x4 OLED display driver.
[bertos.git] / bertos / cpu / cortex-m3 / drv / lcd_lm3s.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 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief LM3S1968 OLED display driver.
34  *
35  * \author Andrea Righi <arighi@develer.com>
36  */
37
38 #include <cfg/debug.h>
39 #include <cfg/macros.h>
40 #include "io/lm3s.h"
41 #include "clock_lm3s.h"
42 #include "ssi_lm3s.h"
43 #include "gpio_lm3s.h"
44 #include "lcd_lm3s.h"
45
46 #define GPIO_OLEDDC_BASE            GPIO_PORTH_BASE
47 #define GPIO_OLEDDC_PIN             BV(2)
48 #define GPIO_OLEDEN_PIN             BV(3)
49
50 /*
51  * Hard-coded command initialization sequence.
52  *
53  * NOTE: the first byte is the size of the command.
54  */
55 static const uint8_t init_cmd[] =
56 {
57         /* Unlock commands */
58         3, 0xfd, 0x12, 0xe3,
59         /* Display off */
60         2, 0xae, 0xe3,
61         /* Icon off */
62         3, 0x94, 0, 0xe3,
63         /* Multiplex ratio */
64         3, 0xa8, 95, 0xe3,
65         /* Contrast */
66         3, 0x81, 0xb7, 0xe3,
67         /* Pre-charge current */
68         3, 0x82, 0x3f, 0xe3,
69         /* Display Re-map */
70         3, 0xa0, 0x52, 0xe3,
71         /* Display Start Line */
72         3, 0xa1, 0, 0xe3,
73         /* Display Offset */
74         3, 0xa2, 0x00, 0xe3,
75         /* Display Mode Normal */
76         2, 0xa4, 0xe3,
77         /* Phase Length */
78         3, 0xb1, 0x11, 0xe3,
79         /* Frame frequency */
80         3, 0xb2, 0x23, 0xe3,
81         /* Front Clock Divider */
82         3, 0xb3, 0xe2, 0xe3,
83         /* Set gray scale table */
84         17, 0xb8, 1, 2, 3, 4, 5, 6, 8, 10, 12, 14, 16, 19, 22, 26, 30, 0xe3,
85         /* Second pre-charge period */
86         3, 0xbb, 0x01, 0xe3,
87         /* Pre-charge voltage */
88         3, 0xbc, 0x3f, 0xe3,
89         /* Display ON */
90         2, 0xaf, 0xe3,
91 };
92
93 /*
94  * Hard-coded command shutdown sequence.
95  */
96 static const uint8_t exit_cmd[] =
97 {
98         /* Display OFF */
99         0xae, 0xe3
100 };
101
102 /*
103  * Hard-coded horizontal increment command.
104  */
105 static const uint8_t horizontal_inc[] =
106 {
107         0xa0, 0x52
108 };
109
110 /**
111  * Write a sequence of data bytes to the LCD SSD1329 controller
112  */
113 static void lcd_writeData(const uint8_t *buf, size_t count)
114 {
115         while (count--)
116                 lm3s_ssi_write_frame(SSI0_BASE, *buf++);
117 }
118
119 /* Turn on the OLED display */
120 void lm3s_lcd_on(void)
121 {
122         unsigned int i;
123
124         /* Loop through the SSD1329 controller initialization sequence */
125         for (i = 0; i < sizeof(init_cmd); i += init_cmd[i] + 1)
126         {
127                 lm3s_gpio_pin_write(GPIO_OLEDDC_BASE, GPIO_OLEDDC_PIN, 0);
128                 lcd_writeData(init_cmd + i + 1, init_cmd[i] - 1);
129         }
130 }
131
132 /* Turn off the OLED display */
133 void lm3s_lcd_off(void)
134 {
135         lm3s_gpio_pin_write(GPIO_OLEDDC_BASE, GPIO_OLEDDC_PIN, 0);
136         lcd_writeData(exit_cmd, sizeof(exit_cmd));
137 }
138
139 /* Refresh a bitmap on screen */
140 void lm3s_lcd_blitBitmap(const Bitmap *bm)
141 {
142         uint8_t lcd_row[bm->width / 2];
143         uint8_t buffer[8];
144         uint8_t mask;
145         int i, l;
146
147         ASSERT(bm->width == LCD_WIDTH && bm->height == LCD_HEIGHT);
148
149         /* Enter command mode */
150         lm3s_gpio_pin_write(GPIO_OLEDDC_BASE, GPIO_OLEDDC_PIN, 0);
151
152         buffer[0] = 0x15;
153         buffer[1] = 0;
154         buffer[2] = (bm->width - 2) / 2;
155         lcd_writeData(buffer, 3);
156
157         buffer[0] = 0x75;
158         buffer[1] = 0;
159         buffer[2] = bm->height - 1;
160         lcd_writeData(buffer, 3);
161         lcd_writeData((const uint8_t *)&horizontal_inc, sizeof(horizontal_inc));
162
163         /*
164          * Enter data mode and send the encoded image data to the OLED display,
165          * over the SSI bus.
166          */
167         lm3s_gpio_pin_write(GPIO_OLEDDC_BASE, GPIO_OLEDDC_PIN, GPIO_OLEDDC_PIN);
168         for (l = 0; l < bm->height / 8; l++)
169         {
170                 for (mask = 1; mask; mask <<= 1)
171                 {
172                         for (i = 0; i < bm->width; i++)
173                         {
174                                 if (bm->raster[l * bm->width + i] & mask)
175                                         lcd_row[i / 2] |= i & 1 ? 0x0f : 0xf0;
176                                 else
177                                         lcd_row[i / 2] &= i & 1 ? 0xf0 : 0x0f;
178                         }
179                         /* Write an entire row at once */
180                         lcd_writeData(lcd_row, sizeof(lcd_row));
181                 }
182         }
183 }
184
185 /**
186  * Initialize the OLED display
187  *
188  * \param freq The SSI Clock Frequency
189  */
190 void lm3s_lcd_init(unsigned long freq)
191 {
192         uint32_t dummy;
193
194         /* Enable the peripheral clock */
195         SYSCTL_RCGC1_R |= SYSCTL_RCGC1_SSI0;
196         SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOA;
197         SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOH;
198         __delay(512);
199
200         /* Configure the SSI0CLK and SSIOTX pins for SSI operation. */
201         lm3s_gpio_pin_config(GPIO_PORTA_BASE, BV(2) | BV(3) | BV(5),
202                 GPIO_DIR_MODE_HW, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD_WPU);
203         /*
204          * Configure the GPIO port pin used as a D/Cn signal for OLED device,
205          * and the port pin used to enable power to the OLED panel.
206          */
207         lm3s_gpio_pin_config(GPIO_OLEDDC_BASE, GPIO_OLEDDC_PIN | GPIO_OLEDEN_PIN,
208                 GPIO_DIR_MODE_OUT, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD);
209         lm3s_gpio_pin_write(GPIO_OLEDDC_BASE, GPIO_OLEDDC_PIN | GPIO_OLEDEN_PIN,
210                         GPIO_OLEDDC_PIN | GPIO_OLEDEN_PIN);
211
212         /* Configure the SSI0 port for master mode */
213         lm3s_ssi_init(SSI0_BASE, SSI_FRF_MOTO_MODE_2, SSI_MODE_MASTER, freq, 8);
214         /*
215          * Configure the GPIO port pin used as a D/Cn signal for OLED device,
216          * and the port pin used to enable power to the OLED panel.
217          */
218         lm3s_gpio_pin_config(GPIO_PORTA_BASE, GPIO_OLEDEN_PIN,
219                 GPIO_DIR_MODE_HW, GPIO_STRENGTH_8MA, GPIO_PIN_TYPE_STD_WPU);
220         /* Enable the SSI port */
221         lm3s_ssi_enable(SSI0_BASE);
222
223         /* Drain the RX fifo */
224         while (lm3s_ssi_read_frame_nonblocking(SSI0_BASE, &dummy));
225
226         /* Turn on the OLED display */
227         lm3s_lcd_on();
228 }