Pixels
Pixels_PPI8.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  * Parallel interface 8bit layer
19  */
20 
21 #include "Pixels.h"
22 
23 #ifdef PIXELS_MAIN
24 #error Pixels_PPI8.h must be included before Pixels_<CONTROLLER>.h
25 #endif
26 
27 #ifndef PIXELS_PPI8_H
28 #define PIXELS_PPI8_H
29 
30 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
31 #define DATAPORT PORTA
32 #define DATADIR DDRA
33 #else
34 #define DATAPORT PORTD
35 #define DATADIR DDRD
36 #endif
37 
38 class PPI8 {
39 private:
40  regtype *registerRD;
41  regtype *registerWR;
42  regtype *registerRST;
43  regtype *registerRS;
44 
45  regsize bitmaskRD;
46  regsize bitmaskWR;
47  regsize bitmaskRST;
48  regsize bitmaskRS;
49 
50  int16_t pinRS;
51  int16_t pinWR;
52  int16_t pinCS;
53  int16_t pinRST;
54  int16_t pinRD;
55 
56 protected:
57  void reset() {
58  sbi(registerRST, bitmaskRST);
59  delay(5);
60  cbi(registerRST, bitmaskRST);
61  delay(15);
62  sbi(registerRST, bitmaskRST);
63  delay(15);
64  }
65 
66  void initInterface();
67 
68  void writeCmd(uint8_t b) {
69  cbi(registerRS, bitmaskRS);
70  DATAPORT = 0x00; pulse_low(registerWR, bitmaskWR); DATAPORT = b; pulse_low(registerWR, bitmaskWR);
71  }
72 
73  void writeData(uint8_t data) {
74  sbi(registerRS, bitmaskRS);
75  DATAPORT = data; pulse_low(registerWR, bitmaskWR);
76  }
77 
78  void writeData(uint8_t hi, uint8_t lo) {
79  sbi(registerRS, bitmaskRS);
80  DATAPORT = hi; pulse_low(registerWR, bitmaskWR); DATAPORT = lo; pulse_low(registerWR, bitmaskWR);
81  }
82 
83  void writeDataTwice(uint8_t b) {
84  DATAPORT = b; pulse_low(registerWR, bitmaskWR); pulse_low(registerWR, bitmaskWR);
85  }
86 
87  void writeCmdData(uint8_t cmd, uint16_t data) {
88  writeCmd(cmd);
89  writeData(highByte(data), lowByte(data));
90  }
91 
92 public:
101  inline void setSpiPins(uint8_t scl, uint8_t sda, uint8_t cs, uint8_t rst, uint8_t wr) {
102  // nop
103  }
104 
109  inline void setPpiPins(uint8_t rs, uint8_t wr, uint8_t cs, uint8_t rst, uint8_t rd) {
110  pinRS = rs; // 38
111  pinWR = wr; // 39
112  pinCS = cs; // 40
113  pinRST = rst; // 41
114  pinRD = rd;
115  }
116 
117  inline void registerSelect() {
118  sbi(registerRS, bitmaskRS);
119  }
120 };
121 
122 void PPI8::initInterface() {
123 
124  DATADIR = 0xFF;
125 
126  registerRS = portOutputRegister(digitalPinToPort(pinRS));
127  registerWR = portOutputRegister(digitalPinToPort(pinWR));
128  registerCS = portOutputRegister(digitalPinToPort(pinCS));
129  registerRST = portOutputRegister(digitalPinToPort(pinRST));
130  if ( pinRD > 0 ) {
131  registerRD = portOutputRegister(digitalPinToPort(pinRD));
132  }
133 
134  bitmaskRS = digitalPinToBitMask(pinRS);
135  bitmaskWR = digitalPinToBitMask(pinWR);
136  bitmaskCS = digitalPinToBitMask(pinCS);
137  bitmaskRST = digitalPinToBitMask(pinRST);
138  if ( pinRD > 0 ) {
139  bitmaskRD = digitalPinToBitMask(pinRD);
140  }
141 
142  pinMode(pinRS,OUTPUT);
143  pinMode(pinWR,OUTPUT);
144  pinMode(pinCS,OUTPUT);
145  pinMode(pinRST,OUTPUT);
146 
147  reset();
148 }
149 #endif
void setPpiPins(uint8_t rs, uint8_t wr, uint8_t cs, uint8_t rst, uint8_t rd)
Definition: Pixels_PPI8.h:109
void setSpiPins(uint8_t scl, uint8_t sda, uint8_t cs, uint8_t rst, uint8_t wr)
Definition: Pixels_PPI8.h:101