-
Notifications
You must be signed in to change notification settings - Fork 12
/
Pixels_PPI16.h
212 lines (179 loc) · 5.82 KB
/
Pixels_PPI16.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Pixels. Graphics library for TFT displays.
*
* Copyright (C) 2012-2013 Igor Repinetski
*
* 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.
*
* Text output methods of the library rely on Pixelmeister's font data format. See: http://pd4ml.com/pixelmeister
*
* 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/
*
* This library includes some code portions and algoritmic ideas derived from works of
* - Andreas Schiffler -- aschiffler at ferzkopp dot net (SDL_gfx Project)
* - K. Townsend http://microBuilder.eu (lpc1343codebase Project)
* - CMBSolutions git()cmbsolutions.nl
* - UTFT Library http://www.rinkydinkelectronics.com/library.php?id=51
*/
/*
* Parallel interface 16bit layer
*/
#include "Pixels.h"
#ifdef PIXELS_MAIN
#error Pixels_PPI16.h must be included before Pixels_<CONTROLLER>.h
#endif
#ifndef PIXELS_PPI16_H
#define PIXELS_PPI16_H
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define DATAPORTH PORTA // 22-29
#define DATAPORTL PORTC // 30-37
#define DATADIRH DDRA
#define DATADIRL DDRC
#else
// shortage of pins for non-Mega boards
#define DATAPORTH PORTD // 0-7
#define DATAPORTL PORTB // 8-13
#define DATADIRH DDRD
#define DATADIRL DDRB
#endif
class PPI16 {
private:
regtype *registerRD;
regtype *registerWR;
regtype *registerRST;
regtype *registerRS;
regsize bitmaskRD;
regsize bitmaskWR;
regsize bitmaskRST;
regsize bitmaskRS;
int16_t pinRS;
int16_t pinWR;
int16_t pinCS;
int16_t pinRST;
int16_t pinRD;
protected:
void reset() {
sbi(registerRST, bitmaskRST);
delay(5);
cbi(registerRST, bitmaskRST);
delay(15);
sbi(registerRST, bitmaskRST);
delay(15);
}
void initInterface();
#if defined(__arm__)
void setDirectionRegisters() {
// Copy from UTFT Lib.
REG_PIOA_OER = 0x0000c000; //PA14,PA15 enable
REG_PIOB_OER = 0x04000000; //PB26 enable
REG_PIOD_OER = 0x0000064f; //PD0-3,PD6,PD9-10 enable
REG_PIOA_OER = 0x00000080; //PA7 enable
REG_PIOC_OER = 0x0000003e; //PC1 - PC5 enable
}
// Modified version from UTFT Lib
inline void writeBus(char hi, char lo) {
REG_PIOA_CODR = 0x0000C080;
REG_PIOC_CODR = 0x0000003E;
REG_PIOD_CODR = 0x0000064F;
REG_PIOA_SODR = ((hi & 0x06) << 13) | ((lo & 0x40) << 1);
(hi & 0x01) ? REG_PIOB_SODR = 0x4000000 : REG_PIOB_CODR = 0x4000000;
REG_PIOC_SODR = ((lo & 0x01) << 5) | ((lo & 0x02) << 3) | ((lo & 0x04) << 1) | ((lo & 0x08) >> 1) | ((lo & 0x10) >> 3);
REG_PIOD_SODR = ((hi & 0x78) >> 3) | ((hi & 0x80) >> 1) | ((lo & 0x20) << 5) | ((lo & 0x80) << 2);
pulse_low(registerWR, bitmaskWR);
}
void writeCmd(uint8_t b) {
cbi(registerRS, bitmaskRS);
writeBus(0x00, b);
}
void writeData(uint8_t data) {
sbi(registerRS, bitmaskRS);
writeBus(0x00, data);
}
void writeData(uint8_t hi, uint8_t lo) {
sbi(registerRS, bitmaskRS);
writeBus(hi, lo);
}
void writeDataTwice(uint8_t b) {
sbi(registerRS, bitmaskRS);
writeBus(b, b);
}
#else
void writeCmd(uint8_t b) {
cbi(registerRS, bitmaskRS);
DATAPORTH = 0; DATAPORTL = b; pulse_low(registerWR, bitmaskWR);
}
void writeData(uint8_t data) {
sbi(registerRS, bitmaskRS);
DATAPORTH = 0; DATAPORTL = data; pulse_low(registerWR, bitmaskWR);
}
void writeData(uint8_t hi, uint8_t lo) {
sbi(registerRS, bitmaskRS);
DATAPORTH = hi; DATAPORTL = lo; pulse_low(registerWR, bitmaskWR);
}
void writeDataTwice(uint8_t b) {
sbi(registerRS, bitmaskRS);
DATAPORTH = b; DATAPORTL = b; pulse_low(registerWR, bitmaskWR);
}
#endif
void writeCmdData(uint8_t cmd, uint16_t data) {
writeCmd(cmd);
writeData(highByte(data), lowByte(data));
}
public:
/**
* Overrides SPI pins
* @param scl
* @param sda
* @param cs chip select
* @param rst reset
* @param wr write pin
*/
inline void setSpiPins(uint8_t scl, uint8_t sda, uint8_t cs, uint8_t rst, uint8_t wr) {
// nop
}
/**
* Overrides PPI pins
* @param cs chip select
*/
inline void setPpiPins(uint8_t rs, uint8_t wr, uint8_t cs, uint8_t rst, uint8_t rd) {
pinRS = rs; // 38
pinWR = wr; // 39
pinCS = cs; // 40
pinRST = rst; // 41
pinRD = rd;
}
inline void registerSelect() {
sbi(registerRS, bitmaskRS);
}
};
void PPI16::initInterface() {
#if defined(__arm__)
setDirectionRegisters();
#else
DATADIRH = 0xFF;
DATADIRL = 0xFF;
#endif
registerRS = portOutputRegister(digitalPinToPort(pinRS));
registerWR = portOutputRegister(digitalPinToPort(pinWR));
registerCS = portOutputRegister(digitalPinToPort(pinCS));
registerRST = portOutputRegister(digitalPinToPort(pinRST));
if ( pinRD > 0 ) {
registerRD = portOutputRegister(digitalPinToPort(pinRD));
}
bitmaskRS = digitalPinToBitMask(pinRS);
bitmaskWR = digitalPinToBitMask(pinWR);
bitmaskCS = digitalPinToBitMask(pinCS);
bitmaskRST = digitalPinToBitMask(pinRST);
if ( pinRD > 0 ) {
bitmaskRD = digitalPinToBitMask(pinRD);
}
pinMode(pinRS,OUTPUT);
pinMode(pinWR,OUTPUT);
pinMode(pinCS,OUTPUT);
pinMode(pinRST,OUTPUT);
#if defined(__arm__)
setDirectionRegisters();
#endif
reset();
}
#endif