-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheink.c
205 lines (162 loc) · 5.03 KB
/
eink.c
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
/*
* File: eink.c
* Author: [email protected]
* Thanks to https://github.com/adafruit/Adafruit_EPD for helping me out!
*
* Created on 1. April 2023, 20:48
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <util/delay.h>
#include "eink.h"
#include "pins.h"
#include "usart.h"
#include "sram.h"
#include "spi.h"
/**
* Does a hardware reset.
*/
static void hwReset(void) {
PORT_DISP &= ~(1 << PIN_RST);
_delay_ms(10);
PORT_DISP |= (1 << PIN_RST);
}
/**
* Waits until the display is no longer busy.
*/
static void waitBusy(void) {
loop_until_bit_is_clear(PINP_DISP, PIN_BUSY);
}
void displaySetCmd(void) {
PORT_DSPI &= ~(1 << PIN_DC);
}
void displaySetData(void) {
PORT_DSPI |= (1 << PIN_DC);
}
void displayCmd(uint8_t cmd) {
displaySetCmd();
transmit(cmd);
}
void displayData(uint8_t data) {
displaySetData();
transmit(data);
}
void initDisplay(bool fast) {
// 1. Power On
// - Supply VCI
// - Wait 10ms
// VCI already supplied, no need to wait
// _delay_ms(10);
displaySel();
// 2. Set Initial Configuration
// board selects 4-wire SPI by pulling BS1 low
// - Define SPI interface to communicate with MCU
// - HW Reset
hwReset();
// _delay_ms(100);
waitBusy();
// - SW Reset by Command 0x12
displayCmd(SW_RESET);
// - Wait 10ms
waitBusy(); // datasheet mentions BUSY is high during reset
_delay_ms(10);
printString("done setting initial configuration\r\n");
// 3. Send Initialization Code
// - Set gate driver output by Command 0x01
displayCmd(DRIVER_OUTPUT_CONTROL);
displayData(DISPLAY_WIDTH - 1);
displayData((DISPLAY_WIDTH - 1) >> 8);
displayData(0x00); // GD=0 [POR], SM=0 [POR], TB = 0 [POR]
// - Set display RAM size by Command 0x11, 0x44, 0x45
displayCmd(DATA_ENTRY_MODE_SETTING);
displayData(0x03); // A[2:0] = 011 [POR]
displayCmd(RAM_X_ADDRESS_POSITION);
displayData(0x00 + RAM_X_OFFSET);
displayData(DISPLAY_H_BYTES - 1 + RAM_X_OFFSET);
displayCmd(RAM_Y_ADDRESS_POSITION);
displayData(0x00);
displayData(0x00);
displayData(DISPLAY_WIDTH - 1);
displayData((DISPLAY_WIDTH - 1) >> 8);
// - Set panel border by Command 0x3C
displayCmd(BORDER_WAVEFORM_CONTROL);
displayData(0x05); // ?
printString("done sending initialization code\r\n");
// 4. Load Waveform LUT
// - Sense temperature by int/ext TS by Command 0x18
displayCmd(TEMP_SENSOR_CONTROL);
displayData(0x80); // A[7:0] = 80h Internal temperature sensor
if (fast) {
// Load temperature value
displayCmd(DISPLAY_UPDATE_CONTROL2);
displayData(0xb1);
displayCmd(MASTER_ACTIVATION);
waitBusy();
// Write temperature value
displayCmd(WRITE_TO_TEMP_REGISTER);
displayData(0x64);
displayData(0x00);
// Load temperature value
displayCmd(DISPLAY_UPDATE_CONTROL2);
displayData(0x91);
displayCmd(MASTER_ACTIVATION);
waitBusy();
}
// done at the end, wait for BUSY low anyway
// - Load waveform LUT from OTP by Command 0x22, 0x20 or by MCU
// - Wait BUSY Low
waitBusy();
displayDes();
}
void resetAddressCounter(void) {
displaySel();
// 5. Write Image and Drive Display Panel
// - Write image data in RAM by Command 0x4E, 0x4F, 0x24, 0x26
displayCmd(RAM_X_ADDRESS_COUNTER);
displayData(RAM_X_OFFSET);
displayCmd(RAM_Y_ADDRESS_COUNTER);
displayData(0);
displayData(0);
displayDes();
}
void imageWrite(uint8_t data) {
displaySel();
// 5. Write Image and Drive Display Panel
// - Write image data in RAM by Command 0x4E, 0x4F, 0x24, 0x26
displayCmd(WRITE_RAM_BW);
displayData(data);
displayDes();
}
void updateDisplay(bool fast) {
displaySel();
// - Set softstart setting by Command 0x0C
/*
displayCmd(BOOSTER_SOFT_START_CONTROL);
displayData(0x8b); // A[7:0] -> Soft start setting for Phase1 = 8Bh [POR]
displayData(0x9c); // B[7:0] -> Soft start setting for Phase2 = 9Ch [POR]
displayData(0x96); // C[7:0] -> Soft start setting for Phase3 = 96h [POR]
displayData(0x0f); // D[7:0] -> Duration setting = 0Fh [POR]
displayDes();
printString("done setting softstart\r\n");
*/
// - Drive display panel by Command 0x22, 0x20
// 0xf4, 0xf5, 0xf6, 0xf7 do full update (DISPLAY mode 1)
// 0xfc, 0xfd, 0xfe, 0xff do partial update (DISPLAY mode 2)
// 0xc7 does fast update
displayCmd(DISPLAY_UPDATE_CONTROL2);
displayData(fast ? 0xc7 : 0xf4);
displayCmd(MASTER_ACTIVATION);
// - Wait BUSY Low
waitBusy();
printString("done driving display\r\n");
// 6. Power Off
// - Deep sleep by Command 0x10
displayCmd(DEEP_SLEEP_MODE);
displayData(0x11); // Deep Sleep Mode 2 (no need to retain RAM data)
printString("done setting deep sleep\r\n");
// - Power OFF
// see 1. Power On
displayDes();
}