-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathsverio_paperboard_v1.c
More file actions
348 lines (295 loc) · 9.37 KB
/
Copy pathsverio_paperboard_v1.c
File metadata and controls
348 lines (295 loc) · 9.37 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <stdint.h>
#include "epd_board.h"
#include "epdiy.h"
#include "../output_common/render_method.h"
#include "../output_lcd/lcd_driver.h"
#include "esp_log.h"
#include "pca9555.h"
#include "tps65185.h"
#include <driver/gpio.h>
#include <driver/i2c.h>
#include <sdkconfig.h>
// Make this compile von the ESP32 without ifdefing the whole file
#ifndef CONFIG_IDF_TARGET_ESP32S3
#define GPIO_NUM_40 -1
#define GPIO_NUM_41 -1
#define GPIO_NUM_42 -1
#define GPIO_NUM_43 -1
#define GPIO_NUM_44 -1
#define GPIO_NUM_45 -1
#define GPIO_NUM_46 -1
#define GPIO_NUM_47 -1
#define GPIO_NUM_48 -1
#endif
#define CFG_SCL GPIO_NUM_40
#define CFG_SDA GPIO_NUM_39
#define CFG_INTR GPIO_NUM_38
#define EPDIY_I2C_PORT I2C_NUM_0
#define CFG_PIN_OE (PCA_PIN_PC10 >> 8)
#define CFG_PIN_MODE (PCA_PIN_PC11 >> 8)
#define __CFG_PIN_STV (PCA_PIN_PC12 >> 8)
#define CFG_PIN_PWRUP (PCA_PIN_PC13 >> 8)
#define CFG_PIN_VCOM_CTRL (PCA_PIN_PC14 >> 8)
#define CFG_PIN_WAKEUP (PCA_PIN_PC15 >> 8)
#define CFG_PIN_PWRGOOD (PCA_PIN_PC16 >> 8)
#define CFG_PIN_INT (PCA_PIN_PC17 >> 8)
#define D15 GPIO_NUM_47
#define D14 GPIO_NUM_21
#define D13 GPIO_NUM_14
#define D12 GPIO_NUM_13
#define D11 GPIO_NUM_12
#define D10 GPIO_NUM_11
#define D9 GPIO_NUM_10
#define D8 GPIO_NUM_9
#define D7 GPIO_NUM_8
#define D6 GPIO_NUM_18
#define D5 GPIO_NUM_17
#define D4 GPIO_NUM_16
#define D3 GPIO_NUM_15
#define D2 GPIO_NUM_7
#define D1 GPIO_NUM_6
#define D0 GPIO_NUM_5
/* Control Lines */
#define CKV GPIO_NUM_48
#define STH GPIO_NUM_41
#define LEH GPIO_NUM_42
#define STV GPIO_NUM_45
/* Edges */
#define CKH GPIO_NUM_4
typedef struct {
i2c_port_t port;
bool pwrup;
bool vcom_ctrl;
bool wakeup;
bool others[8];
} epd_config_register_t;
/** The VCOM voltage to use. */
static int vcom = 1600;
static epd_config_register_t config_reg;
static bool interrupt_done = false;
static void IRAM_ATTR interrupt_handler(void* arg) {
interrupt_done = true;
}
static lcd_bus_config_t lcd_config = {
.clock = CKH,
.ckv = CKV,
.leh = LEH,
.start_pulse = STH,
.stv = STV,
.data[0] = D0,
.data[1] = D1,
.data[2] = D2,
.data[3] = D3,
.data[4] = D4,
.data[5] = D5,
.data[6] = D6,
.data[7] = D7,
.data[8] = D8,
.data[9] = D9,
.data[10] = D10,
.data[11] = D11,
.data[12] = D12,
.data[13] = D13,
.data[14] = D14,
.data[15] = D15,
};
static void epd_board_init(uint32_t epd_row_width) {
gpio_hold_dis(CKH); // free CKH after wakeup
i2c_config_t conf;
conf.mode = I2C_MODE_MASTER;
conf.sda_io_num = CFG_SDA;
conf.scl_io_num = CFG_SCL;
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
conf.master.clk_speed = 100000;
conf.clk_flags = 0;
ESP_ERROR_CHECK(i2c_param_config(EPDIY_I2C_PORT, &conf));
ESP_ERROR_CHECK(i2c_driver_install(EPDIY_I2C_PORT, I2C_MODE_MASTER, 0, 0, 0));
config_reg.port = EPDIY_I2C_PORT;
config_reg.pwrup = false;
config_reg.vcom_ctrl = false;
config_reg.wakeup = false;
for (int i = 0; i < 8; i++) {
config_reg.others[i] = false;
}
gpio_set_direction(CFG_INTR, GPIO_MODE_INPUT);
gpio_set_intr_type(CFG_INTR, GPIO_INTR_NEGEDGE);
ESP_ERROR_CHECK(gpio_install_isr_service(ESP_INTR_FLAG_EDGE));
ESP_ERROR_CHECK(gpio_isr_handler_add(CFG_INTR, interrupt_handler, (void*)CFG_INTR));
// set all epdiy lines to output except TPS interrupt + PWR good
ESP_ERROR_CHECK(pca9555_set_config(config_reg.port, CFG_PIN_PWRGOOD | CFG_PIN_INT, 1));
const EpdDisplay_t* display = epd_get_display();
LcdEpdConfig_t config = {
.pixel_clock = display->bus_speed * 1000 * 1000,
.ckv_high_time = 60,
.line_front_porch = 4,
.le_high_time = 4,
.bus_width = display->bus_width,
.bus = lcd_config,
};
epd_lcd_init(&config, display->width, display->height);
}
static void epd_board_deinit() {
epd_lcd_deinit();
ESP_ERROR_CHECK(pca9555_set_config(
config_reg.port, CFG_PIN_PWRGOOD | CFG_PIN_INT | CFG_PIN_VCOM_CTRL | CFG_PIN_PWRUP, 1
));
int tries = 0;
while (!((pca9555_read_input(config_reg.port, 1) & 0xC0) == 0x80)) {
if (tries >= 50) {
ESP_LOGE("epdiy", "failed to shut down TPS65185!");
break;
}
tries++;
vTaskDelay(1);
}
// Not sure why we need this delay, but the TPS65185 seems to generate an interrupt after some
// time that needs to be cleared.
vTaskDelay(50);
pca9555_read_input(config_reg.port, 0);
pca9555_read_input(config_reg.port, 1);
i2c_driver_delete(EPDIY_I2C_PORT);
gpio_uninstall_isr_service();
}
static void epd_board_set_ctrl(epd_ctrl_state_t* state, const epd_ctrl_state_t* const mask) {
uint8_t value = 0x00;
if (mask->ep_output_enable || mask->ep_mode || mask->ep_stv) {
if (state->ep_output_enable)
value |= CFG_PIN_OE;
if (state->ep_mode)
value |= CFG_PIN_MODE;
// if (state->ep_stv) value |= CFG_PIN_STV;
if (config_reg.pwrup)
value |= CFG_PIN_PWRUP;
if (config_reg.vcom_ctrl)
value |= CFG_PIN_VCOM_CTRL;
if (config_reg.wakeup)
value |= CFG_PIN_WAKEUP;
esp_err_t err = pca9555_set_value(config_reg.port, value, 1);
if (err != ESP_OK) {
ESP_LOGE("epdiy", "pca9555_set_value failed: %s", esp_err_to_name(err));
// Možná budete chtít přidat nějaké zotavení nebo vrátit chybu
ESP_ERROR_CHECK(pca9555_set_value(config_reg.port, value, 1));
}
}
}
void printPowerGoodStatus() {
// Read the Power Good Status register at address 0x0F
uint8_t pgStatus = tps_read_register(config_reg.port, TPS_REG_PG);
// Extract each Power Good status bit
uint8_t vb_pg = (pgStatus >> 7) & 0x01;
uint8_t vddh_pg = (pgStatus >> 6) & 0x01;
uint8_t vn_pg = (pgStatus >> 5) & 0x01;
uint8_t vpos_pg = (pgStatus >> 4) & 0x01;
uint8_t vee_pg = (pgStatus >> 3) & 0x01;
uint8_t vneg_pg = (pgStatus >> 1) & 0x01;
// Log the Power Good Status
ESP_LOGE("epdiy", "Power Good Status: 0x%02X", pgStatus);
ESP_LOGE("epdiy", "VB_PG: %d", vb_pg);
ESP_LOGE("epdiy", "VDDH_PG: %d", vddh_pg);
ESP_LOGE("epdiy", "VN_PG: %d", vn_pg);
ESP_LOGE("epdiy", "VPOS_PG: %d", vpos_pg);
ESP_LOGE("epdiy", "VEE_PG: %d", vee_pg);
ESP_LOGE("epdiy", "VNEG_PG: %d", vneg_pg);
}
static void epd_board_poweron(epd_ctrl_state_t* state) {
epd_ctrl_state_t mask = {
.ep_output_enable = true,
.ep_mode = true,
.ep_stv = true,
};
state->ep_stv = true;
state->ep_mode = false;
state->ep_output_enable = true;
config_reg.vcom_ctrl = true;
epd_board_set_ctrl(state, &mask);
vTaskDelay(10);
int retry_count = 0;
bool pwr_good_received = false;
while (retry_count < 30) {
// Turn on wakeup and pwrup
config_reg.wakeup = true;
config_reg.pwrup = true;
epd_board_set_ctrl(state, &mask);
// Give the IC time to power up and set lines
vTaskDelay(100);
// Check for PWR_GOOD signal within 100ms
if (pca9555_read_input(config_reg.port, 1) & CFG_PIN_PWRGOOD) {
pwr_good_received = true;
printPowerGoodStatus();
break;
}
else
{
printPowerGoodStatus();
}
// Turn off wakeup and pwrup
config_reg.wakeup = false;
config_reg.pwrup = false;
epd_board_set_ctrl(state, &mask);
// Wait for 10ms before retrying
vTaskDelay(100);
retry_count++;
}
if (!pwr_good_received) {
ESP_LOGE("epdiy", "PWR_GOOD signal not received after 30 retries, internal rails may not be OK");
printPowerGoodStatus();
return;
}
ESP_ERROR_CHECK(tps_write_register(config_reg.port, TPS_REG_ENABLE, 0x3F));
tps_set_vcom(config_reg.port, vcom);
state->ep_sth = true;
mask = (const epd_ctrl_state_t){
.ep_sth = true,
};
epd_board_set_ctrl(state, &mask);
int tries = 0;
while (!((tps_read_register(config_reg.port, TPS_REG_PG) & 0xFA) == 0xFA)) {
if (tries >= 500) {
ESP_LOGE(
"epdiy",
"Power enable failed! PG status: %X",
tps_read_register(config_reg.port, TPS_REG_PG)
);
printPowerGoodStatus();
return;
}
tries++;
vTaskDelay(1);
}
}
static void epd_board_poweroff(epd_ctrl_state_t* state) {
epd_ctrl_state_t mask = {
.ep_stv = true,
.ep_output_enable = true,
.ep_mode = true,
};
config_reg.vcom_ctrl = false;
config_reg.pwrup = false;
state->ep_stv = false;
state->ep_output_enable = false;
state->ep_mode = false;
epd_board_set_ctrl(state, &mask);
vTaskDelay(1);
config_reg.wakeup = false;
epd_board_set_ctrl(state, &mask);
}
static float epd_board_ambient_temperature() {
return 20;
}
static void set_vcom(int value) {
vcom = value;
}
const EpdBoardDefinition sverio_paperboard_v1 = {
.init = epd_board_init,
.deinit = epd_board_deinit,
.set_ctrl = epd_board_set_ctrl,
.poweron = epd_board_poweron,
.poweroff = epd_board_poweroff,
.get_temperature = epd_board_ambient_temperature,
.set_vcom = set_vcom,
// unimplemented for now, but shares v6 implementation
.gpio_set_direction = NULL,
.gpio_read = NULL,
.gpio_write = NULL,
};