-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdriver.c
More file actions
executable file
·209 lines (158 loc) · 5.22 KB
/
driver.c
File metadata and controls
executable file
·209 lines (158 loc) · 5.22 KB
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
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <strings.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <errno.h>
#include <stdio.h>
#include <time.h>
#include "gpio.h"
#include "spi.h"
#include "driver.h"
static int lcd_reset(int reset_pin) {
if (gpio_setup() != 0) {
perror("Couldn't setup GPIO");
exit(1);
}
gpio_set_output(reset_pin);
int reset_pin_mask = 1 << reset_pin;
int delay_between_commands = 200 * 1000;
gpio_clear(reset_pin_mask);
usleep(delay_between_commands);
gpio_set(reset_pin_mask);
usleep(delay_between_commands);
return 0;
}
inline static void send(LCD *lcd, uint16_t word) __attribute__((always_inline));
inline static void send_cmd(LCD *lcd, uint8_t cmd) __attribute__((always_inline));
inline static void send_data(LCD *lcd, uint8_t data) __attribute__((always_inline));
static void flush(LCD *lcd);
static void send(LCD *lcd, uint16_t word) {
lcd->buffer[lcd->buffer_pos++] = word;
if (lcd->buffer_pos == MAX_BUFFER_SIZE) {
flush(lcd);
}
}
static void send_cmd(LCD *lcd, uint8_t cmd) {
send(lcd, cmd);
}
static void send_data(LCD *lcd, uint8_t data) {
send(lcd, (uint16_t) data | 0x100);
}
static void flush(LCD *lcd) {
spi_send_buffer(lcd->fd, lcd->buffer_pos, lcd->buffer);
lcd->buffer_pos = 0;
}
void lcd_clear(LCD *lcd, int color) {
uint8_t paset, caset, ramwr;
uint32_t i;
if (lcd->type == TYPE_EPSON) {
paset = PASET;
caset = CASET;
ramwr = RAMWR;
} else {
paset = PASETP;
caset = CASETP;
ramwr = RAMWRP;
}
send_cmd(lcd, paset);
send_data(lcd, 0);
send_data(lcd, 132);
send_cmd(lcd, caset);
send_data(lcd, 0);
send_data(lcd, 132);
flush(lcd);
send_cmd(lcd, ramwr);
flush(lcd);
for(i = 0; i < (132 * 132) / 2; i++) {
send_data(lcd, (color >> 4) & 0xFF);
send_data(lcd, ((color & 0x0F) << 4) | (color >> 8));
send_data(lcd, color & 0x0FF);
}
flush(lcd);
}
void lcd_set_pixel(LCD *lcd, uint8_t x, uint8_t y, uint16_t color) {
if (lcd->type == TYPE_EPSON) {
send_cmd(lcd, PASET); // page start/end ram
send_data(lcd, x);
send_data(lcd, ENDPAGE);
send_cmd(lcd, CASET); // column start/end ram
send_data(lcd, y);
send_data(lcd, ENDCOL);
send_cmd(lcd, RAMWR);
send_data(lcd, (color >> 4) & 0x00ff);
send_data(lcd, ((color & 0x0f) << 4) | (color >> 8));
send_data(lcd, color & 0x0ff);
} else if (lcd->type == TYPE_PHILIPS) {
send_cmd(lcd, PASETP); // page start/end ram
send_data(lcd, x);
send_data(lcd, x);
send_cmd(lcd, CASETP); // column start/end ram
send_data(lcd, y);
send_data(lcd, y);
send_cmd(lcd, RAMWRP);
send_data(lcd, (uint8_t) ((color >> 4) & 0x00FF));
send_data(lcd, (uint8_t) (((color & 0x0F) << 4) | 0x00));
}
flush(lcd);
}
int lcd_init(LCD *lcd, char *dev, int reset_pin, int type) {
int res = lcd_reset(reset_pin);
if (res < 0) {
perror("Failed to reset LCD.");
return 1;
}
bzero(lcd, sizeof(LCD));
lcd->dev = dev;
lcd->type = type;
lcd->fd = spi_init(dev);
if (lcd->fd < 0)
return lcd->fd;
if (type == TYPE_EPSON) {
send_cmd(lcd, DISCTL); // Display control (0xCA)
send_data(lcd, 0x0C); // 12 = 1100 - CL dividing ratio [don't divide] switching period 8H (default)
send_data(lcd, 0x20); // nlines/4 - 1 = 132/4 - 1 = 32 duty
send_data(lcd, 0x00); // No inversely highlighted lines
send_cmd(lcd, COMSCN); // common scanning direction (0xBB)
send_data(lcd, 0x01); // 1->68, 132<-69 scan direction
send_cmd(lcd, OSCON); // internal oscialltor ON (0xD1)
send_cmd(lcd, SLPOUT); // sleep out (0x94)
send_cmd(lcd, PWRCTR); // power ctrl (0x20)
send_data(lcd, 0x0F); // everything on, no external reference resistors
send_cmd(lcd, DISINV); // invert display mode (0xA7)
send_cmd(lcd, DATCTL); // data control (0xBC)
send_data(lcd, 0x03); // Inverse page address, reverse rotation column address, column scan-direction !!! try 0x01
send_data(lcd, 0x00); // normal RGB arrangement
send_data(lcd, 0x02); // 16-bit Grayscale Type A (12-bit color)
send_cmd(lcd, VOLCTR); // electronic volume, this is the contrast/brightness (0x81)
send_data(lcd, 32); // volume (contrast) setting - fine tuning, original (0-63)
send_data(lcd, 3); // internal resistor ratio - coarse adjustment (0-7)
send_cmd(lcd, NOP); // nop (0x25)
usleep(100 * 1000);
send_cmd(lcd, DISON); // display on (0xAF)
} else if (type == TYPE_PHILIPS) {
send_cmd(lcd, SLEEPOUT); // Sleep Out (0x11)
send_cmd(lcd, BSTRON); // Booster voltage on (0x03)
send_cmd(lcd, DISPON); // Display on (0x29)
// send_cmd(lcd, INVON); // Inversion on (0x20)
// 12-bit color pixel format:
send_cmd(lcd, COLMOD); // Color interface format (0x3A)
send_data(lcd, 0x03); // 0b011 is 12-bit/pixel mode
send_cmd(lcd, MADCTL); // Memory Access Control(PHILLIPS)
// if (colorSwap)
// send_data(lcd, 0x08);
// else
send_data(lcd, 0x00);
send_cmd(lcd, SETCON); // Set Contrast(PHILLIPS)
send_data(lcd, 0x30);
send_cmd(lcd, NOPP); // nop(PHILLIPS)
}
flush(lcd);
return 0;
}
void lcd_dispose(LCD *lcd) {
close(lcd->fd);
gpio_shutdown();
}