-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathxaar128.cpp
135 lines (114 loc) · 3.26 KB
/
xaar128.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
#include <SPI.h>
#include "xaar128.h"
#include "image.h"
#include "Arduino.h"
Xaar128::Xaar128() {}
void Xaar128::init() {
pinMode(nSS1, OUTPUT);
pinMode(nSS2, OUTPUT);
pinMode(nCLK, OUTPUT);
pinMode(SCK, OUTPUT);
//pinMode(PHO, OUTPUT);
pinMode(nRESET, OUTPUT);
pinMode(xVDD, OUTPUT);
pinMode(nFIRE, OUTPUT);
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
// Relays (active LOW)
pinMode(relayVHCH, OUTPUT);
digitalWrite(relayVHCH, HIGH);
pinMode(relayVHCL, OUTPUT);
digitalWrite(relayVHCL, HIGH);
pinMode(READY, INPUT);
pinMode(MISO, INPUT);
// https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Clock_polarity_and_phase
// Xaar: Data is clocked into the shift register on the leading edge of SCK and clocked
// out on the falling edge of the SCK. (CPHA=0)
// Idle clock is high. (pg. 33) (CPOL=1)
SPI.setDataMode (SPI_MODE2);
// XAAR SPI clock speed must be 1mhz. SCK is sample clock, not same as CLK.
SPI.setClockDivider (SPI_CLOCK_DIV16); //16
SPI.begin ();
// Set initial state for phase order to NORMAL
//digitalWrite(PHO, LOW);
// Set initial state for nFIRE
digitalWrite(nFIRE, HIGH);
// Chip select disable (=HIGH)
digitalWrite(nSS2, HIGH);
digitalWrite(nSS1, HIGH);
// Reset is active low, set inactive.
digitalWrite(nRESET, HIGH);
// xVDD LOW
digitalWrite(xVDD, LOW);
readyState = LOW;
delay(10);
}
void Xaar128::powerUp() {
// Enable VDD HIGH.
digitalWrite(xVDD, HIGH);
delay(50);
// nRESET pulse width = 500ns
digitalWrite(nRESET, LOW);
delayMicroseconds(1);
digitalWrite(relayVHCH, LOW);
delay(50);
digitalWrite(relayVHCL, LOW);
delay(50);
// Reset HIGH.
digitalWrite(nRESET, HIGH);
delay(10);
}
void Xaar128::powerDown() {
// nRESET pulse width = 500ns
digitalWrite(nRESET, HIGH);
delayMicroseconds(1);
// Reset LOW?
digitalWrite(nRESET, LOW);
delay(50);
digitalWrite(relayVHCH, HIGH);
delay(50);
digitalWrite(relayVHCL, HIGH);
// reset RESET to inactive
digitalWrite(nRESET, HIGH);
// Set VDD LOW.
digitalWrite(xVDD, LOW);
}
void Xaar128::loadBuffer64(byte *val) {
// SPI.transfer is a destructive operation. Make a copy of the array.
byte B[64];
memcpy(B, val, 64);
for (int i = 0; i < 8; i++) {
SPI.transfer(B[i]);
}
}
void Xaar128::loadData(int pending) {
int pos = (pending % (COLS/2)) * 2;
// Load image from Flash
byte c1[8], c2[8];
for (int i=0; i<8; i++) {
c1[i] = pgm_read_byte(&IMAGE[pos][i]);
c2[i] = pgm_read_byte(&IMAGE[pos+1][i]);
}
// SCK is used to clock data into the shift register on the rising edge. The nSS(x) signal
// should be pulled low to enable the target chip to load the new print data.
digitalWrite(nSS2, LOW);
digitalWrite(nSS1, HIGH);
this->loadBuffer64(c2);
digitalWrite(nSS2, HIGH);
digitalWrite(nSS1, LOW);
this->loadBuffer64(c1);
// Exit with both chips disabled (HIGH).
digitalWrite(nSS1, HIGH);
}
bool Xaar128::fire() {
// Wait for start of READY active cycle
while (digitalRead(READY) == LOW) {}
digitalWrite(nFIRE, LOW);
// Check state. If READY does not go low, we failed to fire.
delayMicroseconds(5); // T3 (max: 3.35micros)
bool okay = (digitalRead(READY) == LOW);
delayMicroseconds(100);
digitalWrite(nFIRE, HIGH);
delayMicroseconds(1);
return okay;
}