Pixels
Pixels_SPIsw.h
1 /*
2  * Pixels. Graphics library for TFT displays.
3  *
4  * Copyright (C) 2012-2013 Igor Repinetski
5  *
6  * The code is written in C/C++ for Arduino and can be easily ported to any microcontroller by rewritting the low level pin access functions.
7  *
8  * Text output methods of the library rely on Pixelmeister's font data format. See: http://pd4ml.com/pixelmeister
9  *
10  * This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
11  *
12  * This library includes some code portions and algoritmic ideas derived from works of
13  * - Andreas Schiffler -- aschiffler at ferzkopp dot net (SDL_gfx Project)
14  * - K. Townsend http://microBuilder.eu (lpc1343codebase Project)
15  */
16 
17 /*
18  * Software (bit bang) SPI layer
19  */
20 
21 #include "Pixels.h"
22 
23 #ifdef PIXELS_MAIN
24 #error Pixels_SPIsw.h must be included before Pixels_<CONTROLLER>.h
25 #endif
26 
27 #ifndef PIXELS_SPISW_H
28 #define PIXELS_SPISW_H
29 
30 bool eightBit = true;
31 
32 class SPIsw {
33 private:
34  uint8_t pinSCL;
35  uint8_t pinSDA;
36  uint8_t pinWR;
37  uint8_t pinCS;
38  uint8_t pinRST;
39 
40  regtype *registerSCL;
41  regtype *registerSDA;
42  regtype *registerWR;
43  regsize bitmaskSCL;
44  regsize bitmaskSDA;
45  regsize bitmaskWR;
46 
47  void busWrite(uint8_t data);
48 
49 protected:
50  void reset() {
51  digitalWrite(pinRST,LOW);
52  delay(100);
53  digitalWrite(pinRST,HIGH);
54  delay(100);
55  }
56 
57  void writeCmd(uint8_t b);
58  void writeData(uint8_t data);
59 
60  void writeData(uint8_t hi, uint8_t lo) {
61  writeData(hi);
62  writeData(lo);
63  }
64 
65  void writeDataTwice(uint8_t b) {
66  writeData(b);
67  writeData(b);
68  }
69 
70  void writeCmdData(uint8_t cmd, uint16_t data) {
71  writeCmd(cmd);
72  writeData(highByte(data));
73  writeData(lowByte(data));
74  }
75 
76 public:
77  void initInterface();
78 
79 // void setSPIEightBit(bool bits) {
80 // eightBit = bits;
81 // }
82 
91  inline void setSpiPins(uint8_t scl, uint8_t sda, uint8_t cs, uint8_t rst, uint8_t wr = 255) {
92  pinSCL = scl;
93  pinSDA = sda;
94  pinCS = cs;
95  pinRST = rst;
96  pinWR = wr;
97  eightBit = wr != 255;
98  }
99 
104  inline void setPpiPins(uint8_t rs, uint8_t wr, uint8_t cs, uint8_t rst, uint8_t rd) {
105  }
106 
107  inline void registerSelect() {
108  }
109 };
110 
111 void SPIsw::initInterface() {
112  registerSCL = portOutputRegister(digitalPinToPort(pinSCL));
113  bitmaskSCL = digitalPinToBitMask(pinSCL);
114  registerSDA = portOutputRegister(digitalPinToPort(pinSDA));
115  bitmaskSDA = digitalPinToBitMask(pinSDA);
116  registerWR = portOutputRegister(digitalPinToPort(pinWR));
117  bitmaskWR = digitalPinToBitMask(pinWR);
118  registerCS = portOutputRegister(digitalPinToPort(pinCS));
119  bitmaskCS = digitalPinToBitMask(pinCS);
120 
121  pinMode(pinSCL,OUTPUT);
122  pinMode(pinSDA,OUTPUT);
123  pinMode(pinWR,OUTPUT);
124  pinMode(pinRST,OUTPUT);
125  pinMode(pinCS,OUTPUT);
126 
127  reset();
128 }
129 
130 void SPIsw::busWrite(uint8_t data) {
131  uint8_t c = 8;
132  do {
133  if (data & 0x80) {
134  *registerSDA |= bitmaskSDA;
135  } else {
136  *registerSDA &= ~bitmaskSDA;
137  }
138  data = data<<1;
139  *registerSCL &= ~bitmaskSCL;
140  asm ("nop");
141  *registerSCL |= bitmaskSCL;
142  } while (--c > 0);
143 }
144 
145 void SPIsw::writeCmd(uint8_t cmd) {
146  if ( eightBit ) {
147  *registerWR &= ~bitmaskWR;
148  } else {
149  cbi(registerSDA, bitmaskSDA);
150  cbi(registerSCL, bitmaskSCL); // Pull SPI SCK high
151  sbi(registerSCL, bitmaskSCL); // Pull SPI SCK low
152  }
153  busWrite(cmd);
154 }
155 
156 void SPIsw::writeData(uint8_t data) {
157  if ( eightBit ) {
158  *registerWR |= bitmaskWR;
159  } else {
160  sbi(registerSDA, bitmaskSDA);
161  cbi(registerSCL, bitmaskSCL); // Pull SPI SCK high
162  sbi(registerSCL, bitmaskSCL); // Pull SPI SCK low
163  }
164  busWrite(data);
165 }
166 #endif
void setPpiPins(uint8_t rs, uint8_t wr, uint8_t cs, uint8_t rst, uint8_t rd)
Definition: Pixels_SPIsw.h:104
void setSpiPins(uint8_t scl, uint8_t sda, uint8_t cs, uint8_t rst, uint8_t wr=255)
Definition: Pixels_SPIsw.h:91