Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Renesas/RL78G14/Common/ST7579.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ST7579.c
3 * @brief Implementation of ST7579 Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2012(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 ********************************************************************************
40 * SVN Revision: $WCREV$
41 *******************************************************************************/
42
43 /******************************************************************************/
44 /***************************** Include Files **********************************/
45 /******************************************************************************/
46 #include "ST7579.h" // ST7579 definitions
47 #include "RDKRL78G14.h" // YRDKRL78G14 definitions
48 #include "Communication.h" // Communication definitions
49
50 /******************************************************************************/
51 /************************* Variables Definitions ******************************/
52 /******************************************************************************/
53
54 const unsigned char st7579Font[95][5] =
55 {
56 {0x00, 0x00, 0x00, 0x00, 0x00}, // sp
57 {0x00, 0x00, 0x2f, 0x00, 0x00}, // !
58 {0x00, 0x07, 0x00, 0x07, 0x00}, // "
59 {0x14, 0x7f, 0x14, 0x7f, 0x14}, // #
60 {0x24, 0x2a, 0x7f, 0x2a, 0x12}, // $
61 {0xc4, 0xc8, 0x10, 0x26, 0x46}, // %
62 {0x36, 0x49, 0x55, 0x22, 0x50}, // &
63 {0x00, 0x05, 0x03, 0x00, 0x00}, // '
64 {0x00, 0x1c, 0x22, 0x41, 0x00}, // (
65 {0x00, 0x41, 0x22, 0x1c, 0x00}, // )
66 {0x14, 0x08, 0x3E, 0x08, 0x14}, // *
67 {0x08, 0x08, 0x3E, 0x08, 0x08}, // +
68 {0x00, 0x00, 0x50, 0x30, 0x00}, // ,
69 {0x10, 0x10, 0x10, 0x10, 0x10}, // -
70 {0x00, 0x60, 0x60, 0x00, 0x00}, // .
71 {0x20, 0x10, 0x08, 0x04, 0x02}, // /
72 {0x3E, 0x51, 0x49, 0x45, 0x3E}, // 0
73 {0x00, 0x42, 0x7F, 0x40, 0x00}, // 1
74 {0x42, 0x61, 0x51, 0x49, 0x46}, // 2
75 {0x21, 0x41, 0x45, 0x4B, 0x31}, // 3
76 {0x18, 0x14, 0x12, 0x7F, 0x10}, // 4
77 {0x27, 0x45, 0x45, 0x45, 0x39}, // 5
78 {0x3C, 0x4A, 0x49, 0x49, 0x30}, // 6
79 {0x01, 0x71, 0x09, 0x05, 0x03}, // 7
80 {0x36, 0x49, 0x49, 0x49, 0x36}, // 8
81 {0x06, 0x49, 0x49, 0x29, 0x1E}, // 9
82 {0x00, 0x36, 0x36, 0x00, 0x00}, // :
83 {0x00, 0x56, 0x36, 0x00, 0x00}, // ;
84 {0x08, 0x14, 0x22, 0x41, 0x00}, // <
85 {0x14, 0x14, 0x14, 0x14, 0x14}, // =
86 {0x00, 0x41, 0x22, 0x14, 0x08}, // >
87 {0x02, 0x01, 0x51, 0x09, 0x06}, // ?
88 {0x32, 0x49, 0x59, 0x51, 0x3E}, // @
89 {0x7E, 0x11, 0x11, 0x11, 0x7E}, // A
90 {0x7F, 0x49, 0x49, 0x49, 0x36}, // B
91 {0x3E, 0x41, 0x41, 0x41, 0x22}, // C
92 {0x7F, 0x41, 0x41, 0x22, 0x1C}, // D
93 {0x7F, 0x49, 0x49, 0x49, 0x41}, // E
94 {0x7F, 0x09, 0x09, 0x09, 0x01}, // F
95 {0x3E, 0x41, 0x49, 0x49, 0x7A}, // G
96 {0x7F, 0x08, 0x08, 0x08, 0x7F}, // H
97 {0x00, 0x41, 0x7F, 0x41, 0x00}, // I
98 {0x20, 0x40, 0x41, 0x3F, 0x01}, // J
99 {0x7F, 0x08, 0x14, 0x22, 0x41}, // K
100 {0x7F, 0x40, 0x40, 0x40, 0x40}, // L
101 {0x7F, 0x02, 0x0C, 0x02, 0x7F}, // M
102 {0x7F, 0x04, 0x08, 0x10, 0x7F}, // N
103 {0x3E, 0x41, 0x41, 0x41, 0x3E}, // O
104 {0x7F, 0x09, 0x09, 0x09, 0x06}, // P
105 {0x3E, 0x41, 0x51, 0x21, 0x5E}, // Q
106 {0x7F, 0x09, 0x19, 0x29, 0x46}, // R
107 {0x46, 0x49, 0x49, 0x49, 0x31}, // S
108 {0x01, 0x01, 0x7F, 0x01, 0x01}, // T
109 {0x3F, 0x40, 0x40, 0x40, 0x3F}, // U
110 {0x1F, 0x20, 0x40, 0x20, 0x1F}, // V
111 {0x3F, 0x40, 0x38, 0x40, 0x3F}, // W
112 {0x63, 0x14, 0x08, 0x14, 0x63}, // X
113 {0x07, 0x08, 0x70, 0x08, 0x07}, // Y
114 {0x61, 0x51, 0x49, 0x45, 0x43}, // Z
115 {0x00, 0x7F, 0x41, 0x41, 0x00}, // [
116 {0x55, 0x2A, 0x55, 0x2A, 0x55}, // /
117 {0x00, 0x41, 0x41, 0x7F, 0x00}, // ]
118 {0x04, 0x02, 0x01, 0x02, 0x04}, // ^
119 {0x40, 0x40, 0x40, 0x40, 0x40}, // _
120 {0x00, 0x01, 0x02, 0x04, 0x00}, // '
121 {0x20, 0x54, 0x54, 0x54, 0x78}, // a
122 {0x7F, 0x48, 0x44, 0x44, 0x38}, // b
123 {0x38, 0x44, 0x44, 0x44, 0x20}, // c
124 {0x38, 0x44, 0x44, 0x48, 0x7F}, // d
125 {0x38, 0x54, 0x54, 0x54, 0x18}, // e
126 {0x08, 0x7E, 0x09, 0x01, 0x02}, // f
127 {0x0C, 0x52, 0x52, 0x52, 0x3E}, // g
128 {0x7F, 0x08, 0x04, 0x04, 0x78}, // h
129 {0x00, 0x44, 0x7D, 0x40, 0x00}, // i
130 {0x20, 0x40, 0x44, 0x3D, 0x00}, // j
131 {0x7F, 0x10, 0x28, 0x44, 0x00}, // k
132 {0x00, 0x41, 0x7F, 0x40, 0x00}, // l
133 {0x7C, 0x04, 0x18, 0x04, 0x78}, // m
134 {0x7C, 0x08, 0x04, 0x04, 0x78}, // n
135 {0x38, 0x44, 0x44, 0x44, 0x38}, // o
136 {0x7C, 0x14, 0x14, 0x14, 0x08}, // p
137 {0x08, 0x14, 0x14, 0x18, 0x7C}, // q
138 {0x7C, 0x08, 0x04, 0x04, 0x08}, // r
139 {0x48, 0x54, 0x54, 0x54, 0x20}, // s
140 {0x04, 0x3F, 0x44, 0x40, 0x20}, // t
141 {0x3C, 0x40, 0x40, 0x20, 0x7C}, // u
142 {0x1C, 0x20, 0x40, 0x20, 0x1C}, // v
143 {0x3C, 0x40, 0x30, 0x40, 0x3C}, // w
144 {0x44, 0x28, 0x10, 0x28, 0x44}, // x
145 {0x0C, 0x50, 0x50, 0x50, 0x3C}, // y
146 {0x44, 0x64, 0x54, 0x4C, 0x44}, // z
147 {0x08, 0x3e, 0x41, 0x41, 0x00}, // {
148 {0x00, 0x00, 0x77, 0x00, 0x00}, // |
149 {0x00, 0x41, 0x41, 0x3e, 0x08}, // }
150 {0x02, 0x01, 0x02, 0x01, 0x00} // ~
151 };
152
153 /******************************************************************************/
154 /************************ Functions Definitions *******************************/
155 /******************************************************************************/
156
157 /***************************************************************************//**
158 * @brief Transmits 8 bits to ST7579 controller.
159 *
160 * @param data - Data to transmit.
161 *
162 * @return None.
163 *******************************************************************************/
164 void ST7579_WriteByte(unsigned char data)
165 {
166 SPI_Write(3, // The ID of the selected slave device.
167 &data, // Write buffer.
168 1); // Number of bytes to write.
169 }
170
171 /***************************************************************************//**
172 * @brief Sets X and Y adress of RAM.
173 *
174 * @param yPosition - Y address of RAM.
175 * @param xPosition - X address of RAM.
176 *
177 * @return None.
178 *******************************************************************************/
179 void ST7579_GoTo(unsigned char yPosition,
180 unsigned char xPosition)
181 {
182 ST7579_RS_LOW;
183 /* Set X address of RAM. */
184 ST7579_WriteByte(0x28);
185 ST7579_WriteByte(0x80 + xPosition);
186 /* Set Y address of RAM. */
187 ST7579_WriteByte(0x28);
188 ST7579_WriteByte(0x40 + yPosition);
189 ST7579_RS_HIGH;
190 }
191
192 /***************************************************************************//**
193 * @brief Clears ST7579 RAM.
194 *
195 * @return None.
196 *******************************************************************************/
197 void ST7579_Clear(void)
198 {
199 unsigned short clearPosition = 0;
200 unsigned char yPosition = 0;
201
202 for(clearPosition = 0; clearPosition < (8 * 96); clearPosition++)
203 {
204 if(!(clearPosition % 96))
205 {
206 /* Set the X and Y address of RAM. */
207 ST7579_GoTo(yPosition, 0);
208 yPosition++;
209 }
210 ST7579_WriteByte(0x00);
211 }
212 }
213
214 /***************************************************************************//**
215 * @brief Initializes the ST7579 controller.
216 *
217 * @return status - Result of the initialization procedure.
218 * Example: 0 - if initialization was successful;
219 * -1 - if initialization was unsuccessful.
220 *******************************************************************************/
221 char ST7579_Init(void)
222 {
223 char status = -1;
224 unsigned short delay = 0;
225
226 status = SPI_Init(0, // Transfer format.
227 1000000, // SPI clock frequency.
228 0, // SPI clock polarity.
229 1); // SPI clock edge.
230 if(status != 0)
231 {
232 return status;
233 }
234 ST7579_RS_OUT;
235 ST7579_RS_LOW;
236 /* Select LCD bias ratio of the voltage required for driving the LCD. */
237 ST7579_WriteByte(0x21);
238 ST7579_WriteByte(0x16);
239 /* Select Booster efficiency and Booster stage. */
240 ST7579_WriteByte(0x23);
241 ST7579_WriteByte(0x99);
242 /* Set V0 Range. */
243 ST7579_WriteByte(0x20);
244 ST7579_WriteByte(0x04);
245 /* 50 ms delay is recommended. */
246 for(delay = 0; delay < 1000; delay++)
247 {
248 asm("NOP");
249 }
250 /* Set the frame frequency */
251 ST7579_WriteByte(0x23);
252 ST7579_WriteByte(0x0C);
253 /* Set V0. */
254 ST7579_WriteByte(0x21);
255 ST7579_WriteByte(0xE7);
256 /* Select the display mode */
257 ST7579_WriteByte(0x20);
258 ST7579_WriteByte(0x0C);
259 ST7579_RS_HIGH;
260 /* Clear RAM. */
261 ST7579_Clear();
262
263 return status;
264 }
265
266 /***************************************************************************//**
267 * @brief Sends a character to ST7579 controller.
268 *
269 * @param yPosition - Y address of RAM.
270 * @param xPosition - X address of RAM.
271 * @param character - The character.
272 * @param bigFont - Big font selection.
273 *
274 * @return None.
275 *******************************************************************************/
276 void ST7579_Char(unsigned char yPosition,
277 unsigned char xPosition,
278 unsigned char character,
279 unsigned char bigFont)
280 {
281 unsigned char column = 0;
282 unsigned char data = 0;
283
284 if(bigFont)
285 {
286 /* Set the X and Y address of RAM. */
287 ST7579_GoTo(yPosition, xPosition);
288 /* Send character data. */
289 for(column = 0; column < 5; column++)
290 {
291 data = st7579Font[character - 32][column];
292 data = ((data & 0x01) ? 0x03 : 0) |
293 ((data & 0x02) ? 0x0C : 0) |
294 ((data & 0x04) ? 0x30 : 0) |
295 ((data & 0x08) ? 0xC0 : 0);
296 ST7579_WriteByte(data);
297 ST7579_WriteByte(data);
298 ST7579_WriteByte(data);
299 }
300 ST7579_WriteByte(0);
301 ST7579_WriteByte(0);
302 ST7579_WriteByte(0);
303 yPosition++;
304 /* Set the X and Y address of RAM. */
305 ST7579_GoTo(yPosition, xPosition);
306 /* Send character data. */
307 for(column = 0; column < 5; column++)
308 {
309 data = st7579Font[character - 32][column];
310 data = ((data & 0x10) ? 0x03 : 0) |
311 ((data & 0x20) ? 0x0C : 0) |
312 ((data & 0x40) ? 0x30 : 0) |
313 ((data & 0x80) ? 0xC0 : 0);
314 ST7579_WriteByte(data);
315 ST7579_WriteByte(data);
316 ST7579_WriteByte(data);
317 }
318 ST7579_WriteByte(0);
319 ST7579_WriteByte(0);
320 ST7579_WriteByte(0);
321 }
322 else
323 {
324 /* Set the X and Y address of RAM. */
325 ST7579_GoTo(yPosition, xPosition);
326 /* Send character data. */
327 for(column = 0; column < 5; column++)
328 {
329 ST7579_WriteByte(st7579Font[character - 32][column] << 1);
330 }
331 ST7579_WriteByte(0x00);
332 }
333 }
334
335 /***************************************************************************//**
336 * @brief Sends a string to ST7579 controller.
337 *
338 * @param yPosition - Y address of RAM.
339 * @param xPosition - X address of RAM.
340 * @param string - The string.
341 * @param bigFont - Big font selection.
342 *
343 * @return None.
344 *******************************************************************************/
345 void ST7579_String(unsigned char yPosition,
346 unsigned char xPosition,
347 unsigned char* string,
348 unsigned char bigFont)
349 {
350 while(*string)
351 {
352 /* Send each character of the string. */
353 ST7579_Char(yPosition, xPosition, *string++, bigFont);
354 xPosition += 6 + (bigFont * 12);
355 }
356 }
357
358 /***************************************************************************//**
359 * @brief Sends an integer number to ST7579 controller.
360 *
361 * @param yPosition - Y address of RAM.
362 * @param xPosition - X address of RAM.
363 * @param number - The number.
364 * @param bigFont - Big font selection.
365 *
366 * @return None.
367 *******************************************************************************/
368 void ST7579_Number(unsigned char yPosition,
369 unsigned char xPosition,
370 long number,
371 unsigned char bigFont)
372 {
373 unsigned long mask = 1000000000;
374 unsigned char chNumber = 10;
375 unsigned char chIndex = 0;
376
377 if(number < 0)
378 {
379 ST7579_Char(yPosition, xPosition, '-', bigFont);
380 xPosition += 6 + (bigFont * 12);
381 number *= -1;
382 }
383 else
384 {
385 if(number == 0)
386 {
387 ST7579_String(yPosition, xPosition, " 0", bigFont);
388 }
389 else
390 {
391 ST7579_Char(yPosition, xPosition, '+', bigFont);
392 xPosition += 6 + (bigFont * 12);
393 }
394 }
395 while(mask > number)
396 {
397 mask /= 10;
398 chNumber--;
399 }
400 for(chIndex = 0; chIndex < chNumber; chIndex++)
401 {
402 ST7579_Char(yPosition, xPosition, ((number / mask) % 10) + 0x30, bigFont);
403 mask /= 10;
404 xPosition += 6 + (bigFont * 12);
405 }
406 }
407
408 /***************************************************************************//**
409 * @brief Sends a float number to ST7579 controller.
410 *
411 * @param yPosition - Y address of RAM.
412 * @param xPosition - X address of RAM.
413 * @param number - The number.
414 * @param resolution - Float resolution.
415 * @param bigFont - Big font selection.
416 *
417 * @return None.
418 *******************************************************************************/
419 void ST7579_FloatNumber(unsigned char yPosition,
420 unsigned char xPosition,
421 float number,
422 unsigned char resolution,
423 unsigned char bigFont)
424 {
425 unsigned long mask = 1000000000;
426 unsigned char chNumber = 10;
427 unsigned char index = 0;
428 long display = 1;
429 long multiplication = 1;
430
431
432 for(index = 0; index < resolution; index++)
433 {
434 multiplication *= 10;
435 }
436 display = (long)(number * multiplication);
437 if(display < 0)
438 {
439 ST7579_Char(yPosition, xPosition, '-', bigFont);
440 xPosition += 6 + (bigFont * 12);
441 display *= -1;
442 }
443 else
444 {
445 if(display == 0)
446 {
447 xPosition += 6 + (bigFont * 12);
448 ST7579_Char(yPosition, xPosition, '0', bigFont);
449 xPosition += 6 + (bigFont * 12);
450 if(resolution)
451 {
452 ST7579_Char(yPosition, xPosition, '.', bigFont);
453 xPosition += 6 + (bigFont * 12);
454 }
455 for(index = 0; index < resolution; index++)
456 {
457 ST7579_Char(yPosition, xPosition, '0', bigFont);
458 xPosition += 6 + (bigFont * 12);
459 }
460 return;
461 }
462 else
463 {
464 ST7579_Char(yPosition, xPosition, '+', bigFont);
465 xPosition += 6 + (bigFont * 12);
466 }
467 }
468 while((mask > display) && (chNumber > resolution))
469 {
470 mask /= 10;
471 chNumber--;
472 }
473 if((display > 0) && (display < multiplication))
474 {
475 ST7579_String(yPosition, xPosition, "0.", bigFont);
476 xPosition += 12 + (bigFont * 24);
477 }
478 for(index = 0; index < chNumber; index++)
479 {
480 ST7579_Char(yPosition, xPosition, ((display / mask) % 10) + 0x30, bigFont);
481 mask /= 10;
482 xPosition += 6 + (bigFont * 12);
483 if((multiplication != 1) && (mask == (multiplication / 10)))
484 {
485 ST7579_Char(yPosition, xPosition, '.', bigFont);
486 xPosition += 6 + (bigFont * 12);
487 }
488 }
489 }
490
491 /***************************************************************************//**
492 * @brief Sends an integer number in Hexa format to ST7579 controller.
493 *
494 * @param yPosition - Y address of RAM.
495 * @param xPosition - X address of RAM.
496 * @param number - The number.
497 * @param bigFont - Big font selection.
498 *
499 * @return None.
500 *******************************************************************************/
501 void ST7579_HexNumber(unsigned char yPosition,
502 unsigned char xPosition,
503 long number,
504 unsigned char bigFont)
505 {
506 unsigned long long mask = 268435456;
507 unsigned char chNumber = 8;
508 unsigned char chIndex = 0;
509 unsigned char display = 0;
510
511 ST7579_String(yPosition, xPosition, "0x", bigFont);
512 xPosition += (6 + (bigFont * 12)) * 2;
513 if(number == 0)
514 {
515 ST7579_Char(yPosition, xPosition, '0', bigFont);
516 }
517 while(mask > number)
518 {
519 mask /= 16;
520 chNumber--;
521 }
522 for(chIndex = 0; chIndex < chNumber; chIndex++)
523 {
524 display = (number / mask) % 16;
525 mask /= 16;
526 if(display <= 9)
527 {
528 display += 0x30;
529 }
530 else
531 {
532 display += 0x37;
533 }
534 ST7579_Char(yPosition, xPosition, display, bigFont);
535 xPosition += 6 + (bigFont * 12);
536 }
537 }
538
539 /***************************************************************************//**
540 * @brief Sends an image array to ST7579 controller.
541 *
542 * @param yPosition - Y address of RAM.
543 * @param xPosition - X address of RAM.
544 * @param image - The image array.
545 * @param width - The width of the image (pixels).
546 * @param height - The height of the image (pixels).
547 *
548 * @return None.
549 *******************************************************************************/
550 void ST7579_Image(unsigned char yPosition,
551 unsigned char xPosition,
552 const unsigned char image[],
553 unsigned char width,
554 unsigned char height)
555 {
556 unsigned short index = 0;
557
558 for(index = 0; index < (width * height / 8); index++)
559 {
560 if(!(index % width))
561 {
562 /* Set the X and Y address of RAM. */
563 ST7579_GoTo(yPosition, xPosition);
564 yPosition ++;
565 }
566 ST7579_WriteByte(image[index]);
567 }
568 }