-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax6921.c
More file actions
371 lines (299 loc) · 9.03 KB
/
max6921.c
File metadata and controls
371 lines (299 loc) · 9.03 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* @file max6921.c
* @brief MAX6921 VFD controller driver implementation
*/
#include "max6921.h"
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/spi.h"
/* Internal driver state */
typedef struct {
bool initialized;
vfd_config_t config;
vfd_display_buffer_t display_buffer;
spi_inst_t *spi_port;
uint8_t spi_data[3];
} vfd_driver_state_t;
/* Grid control patterns (one grid active at a time) */
static const uint16_t GRID_PATTERNS[9] = {
0b100000000, /* Grid 0 */
0b010000000, /* Grid 1 */
0b001000000, /* Grid 2 */
0b000100000, /* Grid 3 */
0b000010000, /* Grid 4 */
0b000001000, /* Grid 5 */
0b000000100, /* Grid 6 */
0b000000010, /* Grid 7 */
0b000000001 /* Grid 8 */
};
/* Digit to segment mapping */
static const uint8_t DIGIT_PATTERNS[13] = {
VFD_DIGIT_0,
VFD_DIGIT_1,
VFD_DIGIT_2,
VFD_DIGIT_3,
VFD_DIGIT_4,
VFD_DIGIT_5,
VFD_DIGIT_6,
VFD_DIGIT_7,
VFD_DIGIT_8,
VFD_DIGIT_9,
VFD_SYMBOL_DOT,
VFD_SYMBOL_DASH,
VFD_BLANK
};
static vfd_driver_state_t g_vfd_state = {
.initialized = false,
.config = {0},
.display_buffer = {0},
.spi_port = spi1
};
/* Validate grid index */
static bool _is_valid_grid(uint8_t grid) {
return grid < 9;
}
/* Validate segment pattern */
static bool _is_valid_segment(uint8_t segment) {
(void)segment;
return true;
}
/* Write raw data to VFD chip
* Constructs the 20-bit control word: [COMMAND(3) | GRID(9) | SEGMENTS(8)]
* Command bits (19-17) default to 0 for display-only operation
*
* The MAX6921 is a 20-bit shift register. Since SPI operates on whole bytes,
* we transmit 3 bytes (24 bits) total: 4 padding bits + 20-bit control word.
* The padding bits are transmitted first (MSB-first), shifting the 20-bit word
* into the correct position in the shift register.
*
* Format: [4-bit padding | COMMAND(3) | GRID(9) | SEGMENTS(8)]
*/
static void _write_vfd_raw(uint8_t grid, uint8_t segments) {
if (!_is_valid_grid(grid)) {
return;
}
uint32_t combined_data = ((uint32_t)GRID_PATTERNS[grid] << 8) | segments;
g_vfd_state.spi_data[0] = (combined_data >> 16) & 0xFF;
g_vfd_state.spi_data[1] = (combined_data >> 8) & 0xFF;
g_vfd_state.spi_data[2] = combined_data & 0xFF;
spi_write_blocking(g_vfd_state.spi_port, g_vfd_state.spi_data, 3);
gpio_put(g_vfd_state.config.pin_latch, 1);
sleep_us(1);
gpio_put(g_vfd_state.config.pin_latch, 0);
}
/* Initialize GPIO pins */
static vfd_error_t _init_gpio(const vfd_config_t *config) {
uint actual_baudrate = spi_init(g_vfd_state.spi_port, config->spi_baudrate);
if (actual_baudrate == 0) {
return VFD_ERR_HARDWARE;
}
gpio_set_function(config->pin_spi_clk, GPIO_FUNC_SPI);
gpio_set_function(config->pin_spi_tx, GPIO_FUNC_SPI);
gpio_init(config->pin_latch);
gpio_set_dir(config->pin_latch, GPIO_OUT);
gpio_put(config->pin_latch, 0);
return VFD_OK;
}
/* Public API */
vfd_config_t vfd_default_config(void) {
vfd_config_t config = {
.spi_baudrate = 2000000,
.pin_spi_tx = 11,
.pin_spi_clk = 10,
.pin_latch = 13,
.refresh_interval_us = 1500
};
return config;
}
vfd_error_t vfd_init(const vfd_config_t *config) {
if (g_vfd_state.initialized) {
return VFD_OK;
}
if (config == NULL) {
g_vfd_state.config = vfd_default_config();
} else {
if (config->spi_baudrate == 0 || config->refresh_interval_us == 0) {
return VFD_ERR_INVALID_PARAM;
}
g_vfd_state.config = *config;
}
stdio_init_all();
vfd_error_t err = _init_gpio(&g_vfd_state.config);
if (err != VFD_OK) {
return err;
}
vfd_clear();
g_vfd_state.initialized = true;
return VFD_OK;
}
bool vfd_is_initialized(void) {
return g_vfd_state.initialized;
}
vfd_error_t vfd_deinit(void) {
if (!g_vfd_state.initialized) {
return VFD_ERR_NOT_INITIALIZED;
}
vfd_clear();
vfd_refresh();
spi_deinit(g_vfd_state.spi_port);
g_vfd_state.initialized = false;
return VFD_OK;
}
vfd_error_t vfd_write_segments(uint8_t grid, uint8_t segments) {
if (!g_vfd_state.initialized) {
return VFD_ERR_NOT_INITIALIZED;
}
if (!_is_valid_grid(grid)) {
return VFD_ERR_INVALID_GRID;
}
if (!_is_valid_segment(segments)) {
return VFD_ERR_INVALID_SEGMENT;
}
g_vfd_state.display_buffer[grid] = segments;
return VFD_OK;
}
vfd_error_t vfd_read_segments(uint8_t grid, uint8_t *segments) {
if (!g_vfd_state.initialized) {
return VFD_ERR_NOT_INITIALIZED;
}
if (!_is_valid_grid(grid)) {
return VFD_ERR_INVALID_GRID;
}
if (segments == NULL) {
return VFD_ERR_INVALID_PARAM;
}
*segments = g_vfd_state.display_buffer[grid];
return VFD_OK;
}
vfd_error_t vfd_write_digit(uint8_t grid, uint8_t digit) {
if (!g_vfd_state.initialized) {
return VFD_ERR_NOT_INITIALIZED;
}
if (!_is_valid_grid(grid)) {
return VFD_ERR_INVALID_GRID;
}
if (digit > 9) {
return VFD_ERR_INVALID_PARAM;
}
g_vfd_state.display_buffer[grid] = DIGIT_PATTERNS[digit];
return VFD_OK;
}
vfd_error_t vfd_clear(void) {
memset(g_vfd_state.display_buffer, VFD_BLANK, sizeof(vfd_display_buffer_t));
return VFD_OK;
}
vfd_error_t vfd_refresh(void) {
if (!g_vfd_state.initialized) {
return VFD_ERR_NOT_INITIALIZED;
}
for (uint8_t grid = 0; grid < 9; grid++) {
_write_vfd_raw(grid, g_vfd_state.display_buffer[grid]);
sleep_us(g_vfd_state.config.refresh_interval_us);
}
return VFD_OK;
}
vfd_error_t vfd_write_string(const char *str) {
if (!g_vfd_state.initialized) {
return VFD_ERR_NOT_INITIALIZED;
}
if (str == NULL) {
return VFD_ERR_INVALID_PARAM;
}
vfd_clear();
uint8_t grid = 0;
for (size_t i = 0; str[i] != '\0' && grid < 9; i++) {
char c = str[i];
vfd_error_t err;
if (c >= '0' && c <= '9') {
err = vfd_write_digit(grid, c - '0');
if (err != VFD_OK) {
return err;
}
grid++;
} else if (c == '-') {
g_vfd_state.display_buffer[grid] = VFD_SYMBOL_DASH;
grid++;
} else if (c == '.') {
if (grid > 0) {
g_vfd_state.display_buffer[grid - 1] |= VFD_SYMBOL_DOT;
}
} else if (c == ' ') {
g_vfd_state.display_buffer[grid] = VFD_BLANK;
grid++;
}
}
return VFD_OK;
}
vfd_display_buffer_t *vfd_get_buffer(void) {
if (!g_vfd_state.initialized) {
return NULL;
}
return &g_vfd_state.display_buffer;
}
vfd_error_t vfd_fill_buffer(uint8_t segments) {
if (!g_vfd_state.initialized) {
return VFD_ERR_NOT_INITIALIZED;
}
memset(g_vfd_state.display_buffer, segments, sizeof(vfd_display_buffer_t));
return VFD_OK;
}
vfd_error_t vfd_send_control_command(const vfd_control_command_t *cmd) {
if (!g_vfd_state.initialized) {
return VFD_ERR_NOT_INITIALIZED;
}
if (cmd == NULL) {
return VFD_ERR_INVALID_PARAM;
}
if (cmd->command > 7) {
return VFD_ERR_INVALID_PARAM;
}
/* Encode command in bits 19-17 and send with zero grid/segment data.
* User can define what each command code (0-7) does in their application.
* Transmits 3 bytes (24 bits): 4 padding bits + 20-bit control word.
* The padding bits position the command in the shift register correctly.
*/
uint32_t control_word = ((uint32_t)cmd->command << 17);
g_vfd_state.spi_data[0] = (control_word >> 16) & 0xFF;
g_vfd_state.spi_data[1] = (control_word >> 8) & 0xFF;
g_vfd_state.spi_data[2] = control_word & 0xFF;
spi_write_blocking(g_vfd_state.spi_port, g_vfd_state.spi_data, 3);
gpio_put(g_vfd_state.config.pin_latch, 1);
sleep_us(1);
gpio_put(g_vfd_state.config.pin_latch, 0);
return VFD_OK;
}
int vfd_segments_to_string(uint8_t segments, char *buffer, int buffer_size) {
if (buffer == NULL || buffer_size < 1) {
return 0;
}
static const char *segment_names[] = {"A", "B", "C", "D", "E", "F", "G", "H"};
int offset = 0;
buffer[0] = '\0';
for (int i = 0; i < 8; i++) {
if (segments & (1 << i)) {
if (offset > 0) {
offset += snprintf(buffer + offset, buffer_size - offset, " ");
}
offset += snprintf(buffer + offset, buffer_size - offset, "%s", segment_names[i]);
if (offset >= buffer_size - 1) {
break;
}
}
}
return offset;
}
const char *vfd_strerror(vfd_error_t error) {
static const char *error_messages[] = {
"Operation successful",
"Invalid parameter provided",
"VFD not initialized",
"Grid index out of range",
"Segment value out of range",
"Hardware initialization failed"
};
if (error >= 0 && error < 6) {
return error_messages[error];
}
return "Unknown error";
}