-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrafCommsSPI.cpp
146 lines (123 loc) · 3.15 KB
/
GrafCommsSPI.cpp
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
#include <HardSPI.h>
#include "GrafCommsSPI.h"
#include <pins_arduino.h>
GrafCommsSPI::GrafCommsSPI () : GrafComms::GrafComms()
{
}
void GrafCommsSPI::begin (byte power, byte reset, byte dc, byte cs)
{
pinPOW = power ;
pinRES = reset ;
pinDC = dc ; DC_PORT = portOutputRegister (digitalPinToPort (dc)) ; DC_BIT = digitalPinToBitMask (dc) ;
pinCS = cs ; CS_PORT = portOutputRegister (digitalPinToPort (cs)) ; CS_BIT = digitalPinToBitMask (cs) ;
// currently we assume cs is pin 10, due to HardSPI issue
digitalWrite (pinPOW, LOW) ;
pinMode (pinPOW, OUTPUT) ;
digitalWrite (pinRES, HIGH) ; pinMode (pinRES, OUTPUT) ;
digitalWrite (pinCS, HIGH) ; pinMode (pinCS, OUTPUT) ;
digitalWrite (pinDC, LOW) ; pinMode (pinDC, OUTPUT) ;
delay (10) ;
resetChip () ;
spi.begin () ; // force initialization of SPI modes.
spi.end () ;
}
void GrafCommsSPI::resetChip ()
{
digitalWrite (pinRES, LOW) ; // reset
delay (2) ;
digitalWrite (pinRES, HIGH) ;
}
// Define an inline SPI routine to speed transfers up. Still limited by
// speed of digitalWrite on CS and DC pins of course.
#define spitrans(data) \
SPDR = (data) ; \
while (!(SPSR & (1<<SPIF))) \
{} \
SPDR ;
#define spibegin() * CS_PORT &= ~CS_BIT
#define spiend() * CS_PORT |= CS_BIT
void GrafCommsSPI::wr_comm (byte command)
{
* DC_PORT &= ~DC_BIT ; // digitalWrite (pinDC, LOW) ;
spibegin () ;
spitrans (command) ;
spiend () ;
}
void GrafCommsSPI::wr_data (byte data)
{
* DC_PORT |= DC_BIT ; // digitalWrite (pinDC, HIGH) ;
spibegin () ;
spitrans (data) ;
spiend () ;
}
void GrafCommsSPI::wr_data_16 (unsigned int data)
{
* DC_PORT |= DC_BIT ; // digitalWrite (pinDC, HIGH) ;
spibegin () ;
spitrans (data >> 8) ;
spitrans (data & 0xFF) ;
spiend () ;
}
void GrafCommsSPI::wr_data_16xN (unsigned int data, byte count)
{
* DC_PORT |= DC_BIT ; // digitalWrite (pinDC, HIGH) ;
byte hi = data >> 8 ;
byte lo = data & 0xFF ;
spibegin () ;
for (byte b = 0 ; b < count ; b++)
{
spitrans (hi) ;
spitrans (lo) ;
}
spiend () ;
}
byte GrafCommsSPI::rd_data ()
{
* DC_PORT |= DC_BIT ; // digitalWrite (pinDC, HIGH) ;
spibegin () ;
byte res = spitrans (0) ;
spiend () ;
return res ;
}
unsigned int GrafCommsSPI::rd_data_16 ()
{
* DC_PORT |= DC_BIT ; // digitalWrite (pinDC, HIGH) ;
spibegin () ;
byte hi = spitrans (0) ;
byte lo = spitrans (0) ;
spiend () ;
return (((unsigned int) hi) << 8) | lo ;
}
unsigned int GrafCommsSPI::read_reg (byte reg)
{
spibegin () ;
wr_comm (reg) ;
return rd_data_16 () ;
}
void GrafCommsSPI::write_reg (byte reg, unsigned int val)
{
spibegin () ;
wr_comm (reg) ;
wr_data_16 (val) ;
}
void GrafCommsSPI::gen_start_ram (byte ram_comm)
{
* DC_PORT &= ~DC_BIT ; // digitalWrite (pinDC, LOW) ;
spibegin () ;
spitrans (ram_comm) ;
* DC_PORT |= DC_BIT ; // digitalWrite (pinDC, HIGH) ;
}
void GrafCommsSPI::start_ram ()
{
* DC_PORT |= DC_BIT ; // digitalWrite (pinDC, HIGH) ;
spibegin () ;
}
void GrafCommsSPI::write_ram (unsigned int data)
{
spitrans (data >> 8) ;
spitrans (data) ;
}
void GrafCommsSPI::stop_ram ()
{
spiend () ;
}