Debian Codegraph @

extras/graphic_lcd/src/ssd0323.cc

raw
1 /*
2 Copyright (C) 2007 T. Scott Dattalo
3
4 This file is part of gpsim.
5
6 gpsim 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, or (at your option)
9 any later version.
10
11 gpsim 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 gpsim; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include <config.h>
22 #ifdef HAVE_GUI
23
24 #define IN_MODULE
25
26 #include <cstdio>
27 #include <cstdlib>
28
29 #include "ssd0323.h"
30
31
32 //#define DEBUG
33 #if defined(DEBUG)
34 #define Dprintf(arg) {printf("%s:%d ",__FUNCTION__,__LINE__); printf arg; }
35 #else
36 #define Dprintf(arg) {}
37 #endif
38
39 #define UNIT_TESTING 0
40
41 //========================================================================
42 //
43 // SSD0323 Solomon Systech Display controller
44 //
45 // -- 128X80, 16 Gray Scale Dot Matrix
46 //
47 //
48 // Questions
49 // 1) The command sequence involves writing multiple bytes. Under what
50 // conditions is the state of this sequence modified?
51 // -- Assume that each command write automatically advances the state
52 // -- Assume that it is okay for CS and DC to change states between writes
53 // -- Assume that a low on RES clears the state.
54 // -- Assume that a data write before the command completes aborts the command.
55
56 //------------------------------------------------------------------------
57 //------------------------------------------------------------------------
58
59 #if UNIT_TESTING == 1
60 void unitTest(SSD0323 *pSSD0323);
61 #endif
62
63 SSD0323::SSD0323()
64 : m_controlState(0),
65 m_dataBus(0x100),
66 m_commMode(SSD0323::eSPIMode),
67 m_commState(0),
68 m_SPIData(0),
69 m_commandIndex(0),
70 m_expectedCommandWords(0),
71 m_colAddr(0), m_rowAddr(0),
72 m_colStartAddr(0), m_colEndAddr(0x3f),
73 m_rowStartAddr(0), m_rowEndAddr(0x4f),
74 m_Remap(0), m_ContrastControl(0x40)
75 {
76 randomizeRAM();
77 cmdWords[0] = 0;
78 #if UNIT_TESTING == 1
79 unitTest(this);
80 #endif
81 }
82
83
84 //------------------------------------------------------------------------
85 void SSD0323::abortCurrentTransaction()
86 {
87 m_commState = 0;
88 m_commandIndex = 0;
89 }
90
91
92 //------------------------------------------------------------------------
93 // setBS -- selects the communication interface:
94 //
95 // BS Mode
96 // 000 Serial
97 // 100 6800
98 // 110 8080
99
100 void SSD0323::setBS(unsigned int BSpin, bool newBS)
101 {
102 //Dprintf(("pin:%d state:%d currentBS state:%d\n",BSpin, newBS,m_commMode));
103 if ((((1 << BSpin) & m_commMode) != 0) == newBS) {
104 return;
105 }
106
107 m_commMode ^= (1 << BSpin);
108 abortCurrentTransaction();
109 }
110
111
112 //------------------------------------------------------------------------
113 //------------------------------------------------------------------------
114 void SSD0323::setCS(bool newCS)
115 {
116 if (bBitState(eCS) == newCS) {
117 return;
118 }
119
120 m_controlState ^= eCS;
121 //Dprintf(("state:%d\n",newCS));
122 abortCurrentTransaction();
123 }
124
125
126 //------------------------------------------------------------------------
127 //------------------------------------------------------------------------
128 void SSD0323::setRES(bool newRES)
129 {
130 if (bBitState(eRES) == newRES) {
131 return;
132 }
133
134 m_controlState ^= eRES;
135 Dprintf(("state:%d\n", newRES));
136 abortCurrentTransaction();
137 }
138
139
140 //------------------------------------------------------------------------
141 //------------------------------------------------------------------------
142
143 void SSD0323::setDC(bool newDC)
144 {
145 if (bBitState(eDC) == newDC) {
146 return;
147 }
148
149 m_controlState ^= eDC;
150 //Dprintf(("state:%d\n",newDC));
151 }
152
153
154 //------------------------------------------------------------------------
155 //------------------------------------------------------------------------
156
157 void SSD0323::setE_RD(bool newE_RD)
158 {
159 if (bBitState(eE_RD) == newE_RD) {
160 return;
161 }
162
163 m_controlState ^= eE_RD;
164 Dprintf(("state:%d\n", newE_RD));
165
166 if (!bEnabled()) {
167 return;
168 }
169
170 switch (m_commMode) {
171 case e8080Mode:
172
173 // Take action on rising edge of RD#
174 //Dprintf(("8080 mode\n"));
175 if (newE_RD && bBitState(eRW)) {
176 if (bBitState(eDC)) {
177 driveDataBus(getData());
178 advanceColumnAddress();
179
180 } else {
181 driveDataBus(getStatus());
182 }
183 }
184
185 break;
186
187 case e6800Mode:
188 Dprintf(("6800 mode\n"));
189
190 // Take action on the falling edge of E#
191 if (!newE_RD) {
192 if (bBitState(eRW) && !bBitState(eDC))
193 // Read status
194 {
195 driveDataBus(getStatus());
196
197 } else if (!bBitState(eRW) && !bBitState(eDC))
198 // Write command
199 {
200 executeCommand();
201
202 } else if (bBitState(eRW) && bBitState(eDC)) {
203 // Read Data
204 driveDataBus(getData());
205 advanceColumnAddress();
206
207 } else if (!bBitState(eRW) && bBitState(eDC)) {
208 // Write Data
209 storeData();
210 }
211 }
212
213 break;
214
215 case eSPIMode:
216 //Dprintf(("SPI mode\n"));
217 break;
218
219 default:
220 break;
221 }
222 }
223
224
225 //------------------------------------------------------------------------
226 void SSD0323::setRW(bool newRW)
227 {
228 if (bBitState(eRW) == newRW) {
229 return;
230 }
231
232 m_controlState ^= eRW;
233
234 if (!bEnabled()) {
235 return;
236 }
237
238 switch (m_commMode) {
239 case e8080Mode:
240
241 // Take action on rising edge or WR#
242 //Dprintf(("8080 mode\n"));
243 if (newRW && bBitState(eE_RD)) {
244 if (bBitState(eDC)) {
245 storeData();
246
247 } else {
248 executeCommand();
249 }
250 }
251
252 break;
253
254 case e6800Mode:
255 //Dprintf(("6800 mode\n"));
256 break;
257
258 case eSPIMode:
259 //Dprintf(("SPI mode\n"));
260 break;
261
262 default:
263 break;
264 }
265 }
266
267
268 const int bSCLK = 1;
269 const int bSDIN = 2;
270
271
272 void SSD0323::setData(unsigned int d)
273 {
274 if (m_dataBus == d) {
275 return;
276 }
277
278 if (m_commMode == eSPIMode && bEnabled()) {
279 if (~m_dataBus & d & bSCLK) {
280 m_SPIData = (m_SPIData << 1) | (m_dataBus & bSDIN ? 1 : 0);
281 Dprintf(("SSD SPI new bit. So far: 0x%02X\n", m_SPIData));
282
283 if (++m_commState >= 8) {
284 // Place the contents of the SPI receiver onto the
285 // internal data bus and then perform the write operation.
286 m_dataBus = m_SPIData;
287
288 if (bBitState(eDC)) {
289 storeData();
290
291 } else {
292 executeCommand();
293 }
294
295 m_commState = 0;
296 m_SPIData = 0;
297 }
298 }
299 }
300
301 m_dataBus = d;
302 }
303
304
305 void SSD0323::setSCLK(bool bSCL)
306 {
307 setData((m_dataBus & ~bSCLK) | (bSCL ? bSCLK : 0));
308 }
309
310
311 void SSD0323::setSDIN(bool bSDA)
312 {
313 setData((m_dataBus & ~bSDIN) | (bSDA ? bSDIN : 0));
314 }
315
316
317 void SSD0323::driveDataBus(unsigned int d)
318 {
319 m_dataBus = d;
320 }
321
322
323 void SSD0323::advanceColumnAddress()
324 {
325 if (++m_colAddr <= m_colEndAddr) {
326 return;
327 }
328
329 m_colAddr = m_colStartAddr;
330
331 if (m_rowStartAddr != m_rowEndAddr) {
332 advanceRowAddress();
333 }
334 }
335
336
337 void SSD0323::advanceRowAddress()
338 {
339 if (++m_rowAddr <= m_rowEndAddr) {
340 return;
341 }
342
343 m_rowAddr = m_rowStartAddr;
344
345 if (m_colStartAddr != m_colEndAddr) {
346 advanceColumnAddress();
347 }
348 }
349
350
351 void SSD0323::storeData()
352 {
353 m_ram[m_rowAddr * 64 + m_colAddr] = m_dataBus;
354 Dprintf((" data:0x%02x row:%u col:%u\n", m_dataBus, m_rowAddr, m_colAddr));
355 const int incMode = 4;
356
357 if (m_Remap & incMode) {
358 advanceRowAddress();
359
360 } else {
361 advanceColumnAddress();
362 }
363 }
364
365
366 unsigned int SSD0323::getData()
367 {
368 m_dataBus = m_ram[m_rowAddr * 64 + m_colAddr];
369 return m_dataBus;
370 }
371
372
373 unsigned int SSD0323::getStatus()
374 {
375 return 0;
376 }
377
378
379 void SSD0323::executeCommand()
380 {
381 const int CmdSetColumnAddress = 0x15;
382 const int CmdGraphicAccleration = 0x23;
383 const int CmdDrawRectangle = 0x24;
384 const int CmdCopy = 0x25;
385 const int CmdHorizontalScroll = 0x26;
386 const int CmdStopMoving = 0x2E;
387 const int CmdStartMoving = 0x2F;
388 const int CmdSetRowAddress = 0x75;
389 const int CmdSetContrast = 0x81;
390 const int CmdSetQuarterCurrent = 0x84;
391 const int CmdSetHalfCurrent = 0x85;
392 const int CmdSetFullCurrent = 0x86;
393 const int CmdSetRemap = 0xA0;
394 const int CmdSetDisplayStartLine = 0xA1;
395 const int CmdSetDisplayOffset = 0xA2;
396 const int CmdSetNormalDisplay = 0xA4;
397 const int CmdSetAllOn = 0xA5;
398 const int CmdSetAllOff = 0xA6;
399 const int CmdSetInverse = 0xA7;
400 const int CmdSetMultiplexRatio = 0xA8;
401 const int CmdSetMasterCfg = 0xAD;
402 const int CmdSetDisplayOff = 0xAE;
403 const int CmdSetDisplayOn = 0xAF;
404 const int CmdSetPreChargeCompensationEnable = 0xB0;
405 const int CmdSetPhaseLength = 0xB1;
406 const int CmdSetRowPeriod = 0xB2;
407 const int CmdSetClockDivide = 0xB3;
408 const int CmdSetPreChargeCompensationLevel = 0xB4;
409 const int CmdSetGrayScaleTable = 0xB8;
410 const int CmdSetPreChargeVoltage = 0xBC;
411 const int CmdSetVCOMH = 0xBE;
412 const int CmdSetVSL = 0xBF;
413 const int CmdNop = 0xE3;
414 cmdWords[m_commandIndex] = m_dataBus;
415 m_commandIndex = (m_commandIndex + 1) & 0xf;
416
417 if (m_commandIndex > sizeof(cmdWords)) {
418 m_commandIndex = 0;
419 printf("Warning: SSD0323::executeCommand() - command buffer overflow\n");
420 return;
421 }
422
423 printf("%s:data=0x%x\n", __FUNCTION__, m_dataBus);
424
425 // decode the command
426 if (m_commandIndex == 1) {
427 switch (m_dataBus) {
428 // 1-word commands
429 case CmdStopMoving:
430 case CmdStartMoving:
431 case CmdSetQuarterCurrent:
432 case CmdSetHalfCurrent:
433 case CmdSetFullCurrent:
434 case CmdSetNormalDisplay:
435 case CmdSetAllOn:
436 case CmdSetAllOff:
437 case CmdSetInverse:
438 case CmdSetDisplayOff:
439 case CmdSetDisplayOn:
440 case CmdNop:
441 m_commandIndex = 0;
442 return;
443 break;
444
445 // 2-word commands
446 case CmdGraphicAccleration:
447 case CmdSetContrast:
448 case CmdSetRemap:
449 case CmdSetDisplayStartLine:
450 case CmdSetDisplayOffset:
451 case CmdSetMultiplexRatio:
452 case CmdSetMasterCfg:
453 case CmdSetPreChargeCompensationEnable:
454 case CmdSetRowPeriod:
455 case CmdSetPreChargeCompensationLevel:
456 case CmdSetPreChargeVoltage:
457 case CmdSetVCOMH:
458 case CmdSetVSL:
459 case CmdSetPhaseLength:
460 case CmdSetClockDivide:
461 m_expectedCommandWords = 2;
462 break;
463
464 // 3-word commands
465 case CmdSetColumnAddress:
466 case CmdSetRowAddress:
467 m_expectedCommandWords = 3;
468 break;
469
470 case CmdHorizontalScroll:
471 m_expectedCommandWords = 4;
472 break;
473
474 case CmdDrawRectangle:
475 m_expectedCommandWords = 6;
476 break;
477
478 case CmdCopy:
479 m_expectedCommandWords = 7;
480 break;
481
482 case CmdSetGrayScaleTable:
483 m_expectedCommandWords = 9;
484 break;
485
486 default:
487 printf("Warning: SSD received bad command 0x%x\n", m_dataBus);
488 }
489 }
490
491 if (m_commandIndex == m_expectedCommandWords) {
492 unsigned int i = cmdWords[0];
493 printf("SSD0323 - executing command:0x%x\n", i);
494
495 switch (cmdWords[0]) {
496 // 1-word commands
497 case CmdStopMoving:
498 case CmdStartMoving:
499 case CmdSetQuarterCurrent:
500 case CmdSetHalfCurrent:
501 case CmdSetFullCurrent:
502 case CmdSetNormalDisplay:
503 case CmdSetAllOn:
504 case CmdSetAllOff:
505 case CmdSetInverse:
506 case CmdSetDisplayOff:
507 case CmdSetDisplayOn:
508 case CmdNop:
509 break;
510
511 case CmdSetContrast:
512 m_ContrastControl = cmdWords[1] & 0x7f;
513 break;
514
515 case CmdSetRemap:
516 m_Remap = cmdWords[1] & 0x7f;
517 break;
518
519 // 2-word commands
520 case CmdGraphicAccleration:
521 case CmdSetDisplayStartLine:
522 case CmdSetDisplayOffset:
523 case CmdSetMultiplexRatio:
524 case CmdSetMasterCfg:
525 case CmdSetPreChargeCompensationEnable:
526 case CmdSetRowPeriod:
527 case CmdSetPreChargeCompensationLevel:
528 case CmdSetPreChargeVoltage:
529 case CmdSetVCOMH:
530 case CmdSetVSL:
531 case CmdSetPhaseLength:
532 case CmdSetClockDivide:
533 m_expectedCommandWords = 2;
534 break;
535
536 // 3-word commands
537 case CmdSetColumnAddress:
538 m_colStartAddr = cmdWords[1] & 0x3f;
539 m_colEndAddr = cmdWords[2] & 0x3f;
540 m_colAddr = m_colStartAddr;
541 break;
542
543 case CmdSetRowAddress:
544 m_rowStartAddr = cmdWords[1] & 0x7f;
545 m_rowEndAddr = cmdWords[2] & 0x7f;
546 m_rowAddr = m_rowStartAddr;
547 break;
548
549 case CmdHorizontalScroll:
550 m_expectedCommandWords = 4;
551 break;
552
553 case CmdDrawRectangle:
554 m_expectedCommandWords = 6;
555 break;
556
557 case CmdCopy:
558 m_expectedCommandWords = 7;
559 break;
560
561 case CmdSetGrayScaleTable:
562 m_expectedCommandWords = 16;
563 break;
564
565 default:
566 printf("Warning: SSD received bad command 0x%x\n", m_dataBus);
567 }
568
569 m_commandIndex = 0;
570 }
571 }
572
573
574 //------------------------------------------------------------------------
575 // dataBusDirection()
576 // return true if the controller is driving the data bus
577 bool SSD0323::dataBusDirection()
578 {
579 if (!bEnabled()) {
580 return false;
581 }
582
583 if (m_commMode == e8080Mode && !bBitState(eE_RD)) {
584 return true;
585 }
586
587 if (m_commMode == e6800Mode && bBitState(eRW)) {
588 return true;
589 }
590
591 return false;
592 }
593
594
595 //------------------------------------------------------------------------
596 unsigned int SSD0323::getDataBus()
597 {
598 return m_dataBus;
599 }
600
601
602 //------------------------------------------------------------------------
603
604 void SSD0323::randomizeRAM()
605 {
606 for (unsigned int i = 0; i < 128 * 80 / 2; i++) {
607 m_ram[i] = (rand() >> 8) & 0xff;
608 }
609 }
610
611
612 //------------------------------------------------------------------------
613 // prBadRam - private function called when ever an illegal access is made
614 // to the RAM
615 unsigned int &SSD0323::prBadRam(unsigned int index)
616 {
617 printf("WARNING SSD0323 - illegal RAM access index=%u\n", index);
618 return m_ram[(128 * 80 / 2)];
619 }
620
621
622 //------------------------------------------------------------------------
623 //------------------------------------------------------------------------
624
625 void SSD0323::showState()
626 {
627 printf("SSD0323 internal state:\n");
628
629 switch (m_commMode) {
630 case e8080Mode:
631 printf(" 8080 mode\n");
632 break;
633
634 case e6800Mode:
635 printf(" 6800 mode\n");
636 break;
637
638 case eSPIMode:
639 printf(" SPI mode\n");
640 break;
641 }
642
643 printf("remap: 0x%02x\n", m_Remap);
644 printf("column start:0x%02x end:0x%02x curr:0x%02x\n", m_colStartAddr, m_colEndAddr, m_colAddr);
645 printf("row start:0x%02x end:0x%02x curr:0x%02x\n", m_rowStartAddr, m_rowEndAddr, m_rowAddr);
646 }
647
648
649 #if UNIT_TESTING == 1
650
651 //========================================================================
652 static SSD0323 *p_gSSD0323 = nullptr;
653
654 void WriteCommand(unsigned int c)
655 {
656 if (p_gSSD0323) {
657 p_gSSD0323->setData(c);
658 p_gSSD0323->executeCommand();
659 }
660 }
661
662
663 void WriteData(unsigned int d)
664 {
665 if (p_gSSD0323) {
666 p_gSSD0323->setData(d);
667 p_gSSD0323->storeData();
668 }
669 }
670
671
672 void unitTest(SSD0323 *pSSD0323)
673 {
674 printf("Running SSD0323 unit test\n");
675 p_gSSD0323 = pSSD0323;
676 // Column Address
677 WriteCommand(0x15); /* Set Column Address */
678 WriteCommand(0x00); /* Start = 0 */
679 WriteCommand(0x3F); /* End = 127 */
680 // Row Address
681 WriteCommand(0x75); /* Set Row Address */
682 WriteCommand(0x00); /* Start = 0 */
683 WriteCommand(0x3F); /* End = 63 */
684 // Contrast Control
685 WriteCommand(0x81); /* Set Contrast Control */
686 WriteCommand(0x6D); /* 0 ~ 127 */
687 // Current Range
688 WriteCommand(0x86); /* Set Current Range 84h:Quarter, 85h:Half, 86h:Full*/
689 // Re-map
690 WriteCommand(0xA0); /* Set Re-map */
691 WriteCommand(0x41); /* [0]:MX, [1]:Nibble, [2]:H/V address [4]:MY, [6]:Com Split Odd/Even "1000010"*/
692 // Display Start Line
693 WriteCommand(0xA1); /* Set Display Start Line */
694 WriteCommand(0x00); /* Start at row 0 */
695 // Display Offset
696 WriteCommand(0xA2); /* Set Display Offset */
697 WriteCommand(0x44); /* Offset 68 rows */
698 // Display Mode
699 WriteCommand(0xA4); /* Set DisplaMode,A4:Normal, A5:All ON, A6: All OFF, A7:Inverse */
700 // Multiplex Ratio
701 WriteCommand(0xA8); /* Set Multiplex Ratio */
702 WriteCommand(0x3F); /* 64 mux*/
703 // Phase Length
704 WriteCommand(0xB1); /* Set Phase Length */
705 WriteCommand(0x22); /* [3:0]:Phase 1 period of 1~16 clocks */
706 /* [7:4]:Phase 2 period of 1~16 clocks // POR = 0111 0100 */
707 // Set Pre-charge Compensation Enable
708 WriteCommand(0xB0); /* Set Pre-charge Compensation Enable */
709 WriteCommand(0x28); /* Enable*/
710 // Set Pre-charge Compensation Level
711 WriteCommand(0xB4); /* Set Pre-charge Compensation Level */
712 WriteCommand(0x07); /* Higher level */
713 // Row Period
714 WriteCommand(0xB2); /* Set Row Period */
715 WriteCommand(0x46); /* [7:0]:18~255, K=P1+P2+GS15 (POR:4+7+29)*/
716 // Display Clock Divide
717 WriteCommand(0xB3); /* Set Clock Divide (2) */
718 WriteCommand(0x91); /* [3:0]:1~16, [7:4]:0~16, 100Hz */
719 /* POR = 0000 0001 */
720 // VSL
721 WriteCommand(0xBF); /* Set VSL */
722 WriteCommand(0x0D); /* [3:0]:VSL */
723 // VCOMH
724 WriteCommand(0xBE); /* Set VCOMH (3) */
725 WriteCommand(0x02); /* [7:0]:VCOMH, (0.53 X Vref = 0.53 X 15 V = 7.95V)*/
726 // VP
727 /* Set VP (4) */
728 WriteCommand(0xBC);
729 WriteCommand(0x10); /* [7:0]:VP, (0.67 X Vref = 0.67 X 15 V = 10.05V) */
730 // Gamma
731 WriteCommand(0xB8); /* Set Gamma with next 8 bytes */
732 WriteCommand(0x01); /* L1[2:1] */
733 WriteCommand(0x11); /* L3[6:4], L2[2:0] 0001 0001 */
734 WriteCommand(0x22); /* L5[6:4], L4[2:0] 0010 0010 */
735 WriteCommand(0x32); /* L7[6:4], L6[2:0] 0011 1011 */
736 WriteCommand(0x43); /* L9[6:4], L8[2:0] 0100 0100 */
737 WriteCommand(0x54); /* LB[6:4], LA[2:0] 0101 0101 */
738 WriteCommand(0x65); /* LD[6:4], LC[2:0] 0110 0110 */
739 WriteCommand(0x76); /* LF[6:4], LE[2:0] 1000 0111 */
740 // Set DC-DC
741 WriteCommand(0xAD); /* Set DC-DC */
742 WriteCommand(0x02); /* 03=ON, 02=Off */
743 // Display ON/OFF
744 WriteCommand(0xAF); /* AF=ON, AE=Sleep Mode */
745 //unsigned int i;
746 //for(i=0; i<4; i++)
747 WriteData(0xff);
748 WriteData(0xff);
749 }
750
751 #endif // UNIT_TESTING
752 #endif // HAVE_GUI