Spieler splodiert.
[hackover2013-badge-firmware.git] / drivers / displays / tft / hw / s6b33b6x.c
1 /**************************************************************************/
2 /*!
3 @file s6b33b6x.c
4 @author K. Townsend (www.adafruit.com)
5
6 @section DESCRIPTION
7
8 Driver for S6B33B6X 128x128 pixel RGB OLED displays.
9
10 This driver uses a 3 or 4-pin SPI interface and 16-bit RGB565 colours.
11
12 @section LICENSE
13
14 Software License Agreement (BSD License)
15
16 Copyright (c) 2012, Adafruit Industries
17 All rights reserved.
18
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions are met:
21 1. Redistributions of source code must retain the above copyright
22 notice, this list of conditions and the following disclaimer.
23 2. Redistributions in binary form must reproduce the above copyright
24 notice, this list of conditions and the following disclaimer in the
25 documentation and/or other materials provided with the distribution.
26 3. Neither the name of the copyright holders nor the
27 names of its contributors may be used to endorse or promote products
28 derived from this software without specific prior written permission.
29
30 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
31 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
34 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
37 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41 /**************************************************************************/
42 #include "s6b33b6x.h"
43 #include "core/systick/systick.h"
44
45 static volatile lcdOrientation_t lcdOrientation = LCD_ORIENTATION_PORTRAIT;
46 static lcdProperties_t s6b33b6xProperties = { 128, 128, false, false, false, false, false };
47
48 /*************************************************/
49 /* Private Methods */
50 /*************************************************/
51
52 #define CMD(c) do { CLR_DC; CLR_CS; s6b33b6xSendByte( c, 1 ); SET_CS; } while (0)
53 #define DATA(c) do { SET_DC; CLR_CS; s6b33b6xSendByte( c, 0 ); SET_CS; } while (0)
54 #define DELAY(ms) do { systickDelay( ms / CFG_SYSTICK_DELAY_IN_MS ); } while(0)
55
56 /**************************************************************************/
57 /*!
58 @brief Simulates an SPI write using GPIO.
59
60 @param[in] byte
61 The byte to send
62 @param[in] command
63 1 if this is a command, 0 if it is data
64 */
65 /**************************************************************************/
66 void s6b33b6xSendByte(uint8_t byte, uint8_t command)
67 {
68 int8_t i;
69
70 // Make sure clock pin starts high (CPOL = 1)
71 SET_SCK;
72
73 #if defined S6B33B6X_BUS_SPI3
74 CLR_SCK;
75 if (command)
76 {
77 CLR_SID;
78 }
79 else
80 {
81 SET_SID;
82 }
83 // Read on rising edge (CPHA = 1)
84 SET_SCK
85 #endif
86
87 for (i=0; i<8; i++)
88 {
89 // Set clock pin low
90 CLR_SCK;
91 // Set data pin high or low depending on the value of the current bit
92 if((byte & 0x80) == 0x80)
93 {
94 SET_SID;
95 }
96 else
97 {
98 CLR_SID;
99 }
100 // Set clock pin high (CPHA = 1, read on rising edge)
101 SET_SCK;
102 byte = (byte << 1);
103 }
104 }
105
106 /**************************************************************************/
107 /*!
108 @brief Sets the cursor to the specified X/Y position
109 */
110 /**************************************************************************/
111 void s6b33b6xSetCursor(uint8_t x, uint8_t y)
112 {
113 if ((x >= s6b33b6xProperties.width) || (y >= s6b33b6xProperties.height))
114 return;
115
116 CMD(S6B33B6X_CMD_ROWADDRESSAREASET);
117 CMD(2+y); // Start Address
118 CMD(129); // End Address (129)
119
120 CMD(S6B33B6X_CMD_COLUMNADDRESSAREASET);
121 CMD(2+x); // Start Address
122 CMD(129); // End Address (129)
123 }
124
125 /*************************************************/
126 /* Public Methods */
127 /*************************************************/
128
129 /**************************************************************************/
130 /*!
131 @brief Configures any pins or HW and initialises the LCD controller
132 */
133 /**************************************************************************/
134 void lcdInit(void)
135 {
136 gpioInit();
137
138 // Make sure CS starts high
139 SET_CS;
140
141 // Make sure clock pin starts high (CPOL = 1)
142 SET_SCK;
143
144 // Set all pins to output
145 gpioSetDir(S6B33B6X_SCK_PORT, S6B33B6X_SCK_PIN, gpioDirection_Output);
146 gpioSetDir(S6B33B6X_SID_PORT, S6B33B6X_SID_PIN, gpioDirection_Output);
147 gpioSetDir(S6B33B6X_RST_PORT, S6B33B6X_RST_PIN, gpioDirection_Output);
148 gpioSetDir(S6B33B6X_CS_PORT, S6B33B6X_CS_PIN, gpioDirection_Output);
149
150 #if !defined S6B33B6X_BUS_SPI3
151 gpioSetDir(S6B33B6X_DC_PORT, S6B33B6X_DC_PIN, gpioDirection_Output);
152 #endif
153
154 // Reset the LCD
155 SET_RST;
156 DELAY(20);
157 CLR_RST;
158 DELAY(50);
159 SET_RST;
160 DELAY(20);
161
162 // Init sequence
163 CMD(S6B33B6X_CMD_STANDBYMODEOFF);
164 DELAY(50);
165 CMD(S6B33B6X_CMD_OSCILLATIONMODESET);
166 CMD(0x01);
167 CMD(S6B33B6X_CMD_DCDCCONTROL);
168 CMD(0x0f);
169 CMD(S6B33B6X_CMD_DCDCAMPONOFFSET);
170 CMD(0x0f);
171 DELAY(50);
172 CMD(S6B33B6X_CMD_TEMPCOMPENSATIONSET);
173 CMD(0x01);
174 CMD(0x2e);
175 CMD(S6B33B6X_CMD_RAMSKIPAREASET);
176 CMD(0x00);
177 CMD(S6B33B6X_CMD_SPECIFIEDDISPLAYPATTERN);
178 CMD(0x00);
179 CMD(S6B33B6X_CMD_DRIVEROUTPUTMODESET);
180 CMD(0x03);
181 CMD(S6B33B6X_CMD_DCDCCLOCKDIVISIONSET);
182 CMD(0x22);
183 CMD(S6B33B6X_CMD_ADDRESSINGMODESET);
184 CMD(0x1d);
185 CMD(S6B33B6X_CMD_ROWVECTORMODESET);
186 CMD(0x0e);
187 CMD(S6B33B6X_CMD_ENTRYMODESET);
188 CMD(0x80);
189 CMD(S6B33B6X_CMD_ROWADDRESSAREASET);
190 CMD(0x00);
191 CMD(0x83);
192 CMD(S6B33B6X_CMD_COLUMNADDRESSAREASET);
193 CMD(0x00);
194 CMD(0x83);
195 CMD(S6B33B6X_CMD_NBLOCKINVERSIONSET);
196 CMD(0x8d);
197 CMD(S6B33B6X_CMD_CONTRASTCONTROL);
198 CMD(0x45);
199 CMD(0x2b);
200 CMD(0x69);
201 CMD(0x22);
202 CMD(0x11);
203 CMD(S6B33B6X_CMD_PARTIALDISPLAYMODESET);
204 CMD(0x00);
205 CMD(S6B33B6X_CMD_PARTIALDISPLAYSTARTLINE);
206 CMD(0x00);
207 CMD(S6B33B6X_CMD_PARTIALDISPLAYENDLINE);
208 CMD(0x9f);
209 CMD(0x59);
210 CMD(0x00);
211 CMD(0x00);
212 CMD(0x9f);
213 CMD(0x00);
214 CMD(S6B33B6X_CMD_DISPLAYON);
215 DELAY(500);
216
217 // Clear screen
218 lcdFillRGB(COLOR_RED);
219 }
220
221 /**************************************************************************/
222 /*!
223 @brief Enables or disables the LCD backlight
224 */
225 /**************************************************************************/
226 void lcdBacklight(bool state)
227 {
228 // ToDo: Add BL pin and toggle here
229 }
230
231 /**************************************************************************/
232 /*!
233 @brief Renders a simple test pattern on the LCD
234 */
235 /**************************************************************************/
236 void lcdTest(void)
237 {
238 uint32_t i,j;
239 s6b33b6xSetCursor(0, 0);
240
241 for(i=0;i<128;i++)
242 {
243 for(j=0;j<128;j++)
244 {
245 if(i>111){DATA(COLOR_WHITE>>8);DATA((uint8_t)COLOR_WHITE);}
246 else if(i>95){DATA(COLOR_BLUE>>8);DATA((uint8_t)COLOR_BLUE);}
247 else if(i>79){DATA(COLOR_GREEN>>8);DATA((uint8_t)COLOR_GREEN);}
248 else if(i>63){DATA(COLOR_CYAN>>8);DATA((uint8_t)COLOR_CYAN);}
249 else if(i>47){DATA(COLOR_RED>>8);DATA((uint8_t)COLOR_RED);}
250 else if(i>31){DATA(COLOR_MAGENTA>>8);DATA((uint8_t)COLOR_MAGENTA);}
251 else if(i>15){DATA(COLOR_YELLOW>>8);DATA((uint8_t)COLOR_YELLOW);}
252 else {DATA(COLOR_BLACK>>8);DATA((uint8_t)COLOR_BLACK);}
253 }
254 }
255 }
256
257 /**************************************************************************/
258 /*!
259 @brief Fills the LCD with the specified 16-bit color
260 */
261 /**************************************************************************/
262 void lcdFillRGB(uint16_t data)
263 {
264 uint16_t x,y;
265
266 s6b33b6xSetCursor(0, 0);
267
268 for(x=0;x<128;x++)
269 {
270 for(y=0;y<128;y++)
271 {
272 DATA(data>>8);
273 DATA(data & 0xFF);
274 }
275 }
276 }
277
278 /**************************************************************************/
279 /*!
280 @brief Draws a single pixel at the specified X/Y location
281 */
282 /**************************************************************************/
283 void lcdDrawPixel(uint16_t x, uint16_t y, uint16_t color)
284 {
285 if ((x >= s6b33b6xProperties.width) || (y >= s6b33b6xProperties.height))
286 return;
287
288 s6b33b6xSetCursor((uint8_t)x, (uint8_t)y);
289 DATA(color >> 8);
290 DATA(color);
291 }
292
293 /**************************************************************************/
294 /*!
295 @brief Draws an array of consecutive RGB565 pixels (much
296 faster than addressing each pixel individually)
297 */
298 /**************************************************************************/
299 void lcdDrawPixels(uint16_t x, uint16_t y, uint16_t *data, uint32_t len)
300 {
301 // ToDo
302 }
303
304 /**************************************************************************/
305 /*!
306 @brief Optimised routine to draw a horizontal line faster than
307 setting individual pixels
308 */
309 /**************************************************************************/
310 void lcdDrawHLine(uint16_t x0, uint16_t x1, uint16_t y, uint16_t color)
311 {
312 // ToDo
313 }
314
315 /**************************************************************************/
316 /*!
317 @brief Optimised routine to draw a vertical line faster than
318 setting individual pixels
319 */
320 /**************************************************************************/
321 void lcdDrawVLine(uint16_t x, uint16_t y0, uint16_t y1, uint16_t color)
322 {
323 // Not supported
324 }
325
326 /**************************************************************************/
327 /*!
328 @brief Gets the 16-bit color of the pixel at the specified location
329 */
330 /**************************************************************************/
331 uint16_t lcdGetPixel(uint16_t x, uint16_t y)
332 {
333 // Not supported
334 return 0;
335 }
336
337 /**************************************************************************/
338 /*!
339 @brief Sets the LCD orientation to horizontal and vertical
340 */
341 /**************************************************************************/
342 void lcdSetOrientation(lcdOrientation_t orientation)
343 {
344 // Not supported
345 }
346
347 /**************************************************************************/
348 /*!
349 @brief Gets the current screen orientation (horizontal or vertical)
350 */
351 /**************************************************************************/
352 lcdOrientation_t lcdGetOrientation(void)
353 {
354 return lcdOrientation;
355 }
356
357 /**************************************************************************/
358 /*!
359 @brief Gets the width in pixels of the LCD screen (varies depending
360 on the current screen orientation)
361 */
362 /**************************************************************************/
363 uint16_t lcdGetWidth(void)
364 {
365 return s6b33b6xProperties.width;
366 }
367
368 /**************************************************************************/
369 /*!
370 @brief Gets the height in pixels of the LCD screen (varies depending
371 on the current screen orientation)
372 */
373 /**************************************************************************/
374 uint16_t lcdGetHeight(void)
375 {
376 return s6b33b6xProperties.height;
377 }
378
379 /**************************************************************************/
380 /*!
381 @brief Scrolls the contents of the LCD screen vertically the
382 specified number of pixels using a HW optimised routine
383 */
384 /**************************************************************************/
385 void lcdScroll(int16_t pixels, uint16_t fillColor)
386 {
387 // Not Supported
388 }
389
390 /**************************************************************************/
391 /*!
392 @brief Gets the controller's 16-bit (4 hexdigit) ID
393 */
394 /**************************************************************************/
395 uint16_t lcdGetControllerID(void)
396 {
397 return 0x33b6;
398 }
399
400 /**************************************************************************/
401 /*!
402 @brief Returns the LCDs 'lcdProperties_t' that describes the LCDs
403 generic capabilities and dimensions
404 */
405 /**************************************************************************/
406 lcdProperties_t lcdGetProperties(void)
407 {
408 return s6b33b6xProperties;
409 }
This page took 0.069068 seconds and 5 git commands to generate.