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