LibDriver MAX7219
Loading...
Searching...
No Matches
driver_max7219.c
Go to the documentation of this file.
1
37
38#include "driver_max7219.h"
39
43#define CHIP_NAME "Maxim Integrated MAX7219"
44#define MANUFACTURER_NAME "Maxim Integrated"
45#define SUPPLY_VOLTAGE_MIN 4.0f
46#define SUPPLY_VOLTAGE_MAX 5.5f
47#define MAX_CURRENT 320.0f
48#define TEMPERATURE_MIN -40.0f
49#define TEMPERATURE_MAX 85.0f
50#define DRIVER_VERSION 2000
51
55#define MAX7219_REG_DECODE 0x09
56#define MAX7219_REG_INTENSITY 0x0A
57#define MAX7219_REG_SCAN_LIMIT 0x0B
58#define MAX7219_REG_SHUT_DOWN 0x0C
59#define MAX7219_REG_DISPLAY_TEST 0x0F
60
73uint8_t max7219_set_display(max7219_handle_t *handle, max7219_digital_t digital, uint8_t data)
74{
75 uint8_t res;
76
77 if (handle == NULL) /* check handle */
78 {
79 return 2; /* return error */
80 }
81 if (handle->inited != 1) /* check handle initialization */
82 {
83 return 3; /* return error */
84 }
85
86 res = handle->spi_write(digital, &data, 1); /* spi write data */
87 if (res != 0) /* check result */
88 {
89 handle->debug_print("max7219: write failed.\n"); /* write failed */
90
91 return 1; /* return error */
92 }
93
94 return 0; /* success return 0 */
95}
96
108uint8_t max7219_set_matrix(max7219_handle_t *handle, uint8_t matrix[8])
109{
110 uint8_t res;
111 uint8_t i;
112
113 if (handle == NULL) /* check handle */
114 {
115 return 2; /* return error */
116 }
117 if (handle->inited != 1) /* check handle initialization */
118 {
119 return 3; /* return error */
120 }
121
122 for (i = 0; i < 8; i++)
123 {
124 res = handle->spi_write(i + 1, &matrix[i], 1); /* spi write data */
125 if (res != 0) /* check result */
126 {
127 handle->debug_print("max7219: write failed.\n"); /* write failed */
128
129 return 1; /* return error */
130 }
131 }
132
133 return 0; /* success return 0 */
134}
135
148uint8_t max7219_set_cascade(max7219_handle_t *handle, max7219_cascade_t *cascade, uint16_t len)
149{
150 uint8_t res;
151 uint16_t i;
152
153 if (handle == NULL) /* check handle */
154 {
155 return 2; /* return error */
156 }
157 if (handle->inited != 1) /* check handle initialization */
158 {
159 return 3; /* return error */
160 }
161
162 if (len > MAX7219_MAX_CASCADE_SIZE) /* check length */
163 {
164 handle->debug_print("max7219: len is over MAX7219_MAX_CASCADE_SIZE.\n"); /* write failed */
165
166 return 3; /* return error */
167 }
168
169 for (i = 0; i < len; i++) /* set buf */
170 {
171 handle->buf[i * 2 + 0] = cascade[i].command; /* set command */
172 handle->buf[i * 2 + 1] = cascade[i].data; /* set data */
173 }
174 res = handle->spi_write_cmd(handle->buf, len * 2); /* write command */
175 if (res != 0) /* check result */
176 {
177 handle->debug_print("max7219: write command failed.\n"); /* write failed */
178
179 return 1; /* return error */
180 }
181
182 return 0; /* success return 0 */
183}
184
197{
198 uint8_t res;
199 uint8_t d;
200
201 if (handle == NULL) /* check handle */
202 {
203 return 2; /* return error */
204 }
205 if (handle->inited != 1) /* check handle initialization */
206 {
207 return 3; /* return error */
208 }
209
210 d = (uint8_t)decode; /* set the decode */
211 res = handle->spi_write(MAX7219_REG_DECODE, (uint8_t *)&d, 1); /* spi write data */
212 if (res != 0) /* check result */
213 {
214 handle->debug_print("max7219: set decode failed.\n"); /* write failed */
215
216 return 1; /* return error */
217 }
218
219 return 0; /* success return 0 */
220}
221
234{
235 uint8_t res;
236 uint8_t m;
237
238 if (handle == NULL) /* check handle */
239 {
240 return 2; /* return error */
241 }
242 if (handle->inited != 1) /* check handle initialization */
243 {
244 return 3; /* return error */
245 }
246
247 m = (uint8_t)mode; /* set the mode */
248 res = handle->spi_write(MAX7219_REG_SHUT_DOWN, (uint8_t *)&m, 1); /* spi write data */
249 if (res != 0) /* check result */
250 {
251 handle->debug_print("max7219: set mode failed.\n"); /* write failed */
252
253 return 1; /* return error */
254 }
255
256 return 0; /* success return 0 */
257}
258
271{
272 uint8_t res;
273 uint8_t m;
274
275 if (handle == NULL) /* check handle */
276 {
277 return 2; /* return error */
278 }
279 if (handle->inited != 1) /* check handle initialization */
280 {
281 return 3; /* return error */
282 }
283
284 m = (uint8_t)mode; /* set the mode */
285 res = handle->spi_write(MAX7219_REG_DISPLAY_TEST, (uint8_t *)&m, 1); /* spi write data */
286 if (res != 0) /* check result */
287 {
288 handle->debug_print("max7219: set mode failed.\n"); /* write failed */
289
290 return 1; /* return error */
291 }
292
293 return 0; /* success return 0 */
294}
295
308{
309 uint8_t res;
310 uint8_t in;
311
312 if (handle == NULL) /* check handle */
313 {
314 return 2; /* return error */
315 }
316 if (handle->inited != 1) /* check handle initialization */
317 {
318 return 3; /* return error */
319 }
320
321 in = (uint8_t)intensity; /* set the intensity */
322 res = handle->spi_write(MAX7219_REG_INTENSITY, (uint8_t *)&in, 1); /* spi write data */
323 if (res != 0) /* check result */
324 {
325 handle->debug_print("max7219: set intensity failed.\n"); /* write failed */
326
327 return 1; /* return error */
328 }
329
330 return 0; /* success return 0 */
331}
332
345{
346 uint8_t res;
347 uint8_t l;
348
349 if (handle == NULL) /* check handle */
350 {
351 return 2; /* return error */
352 }
353 if (handle->inited != 1) /* check handle initialization */
354 {
355 return 3; /* return error */
356 }
357
358 l = (uint8_t)limit; /* set the limit */
359 res = handle->spi_write(MAX7219_REG_SCAN_LIMIT, (uint8_t *)&l, 1); /* spi write data */
360 if (res != 0) /* check result */
361 {
362 handle->debug_print("max7219: set scan limit failed.\n"); /* write failed */
363
364 return 1; /* return error */
365 }
366
367 return 0; /* success return 0 */
368}
369
381{
382 if (handle == NULL) /* check handle */
383 {
384 return 2; /* return error */
385 }
386 if (handle->debug_print == NULL) /* check debug_print */
387 {
388 return 3; /* return error */
389 }
390 if (handle->spi_init == NULL) /* check spi_init */
391 {
392 handle->debug_print("max7219: spi_init is null.\n"); /* spi_init is null */
393
394 return 3; /* return error */
395 }
396 if (handle->spi_deinit == NULL) /* check spi_deinit */
397 {
398 handle->debug_print("max7219: spi_deinit is null.\n"); /* spi_deinit is null */
399
400 return 3; /* return error */
401 }
402 if (handle->spi_write_cmd == NULL) /* check spi_write_cmd */
403 {
404 handle->debug_print("max7219: spi_write_cmd is null.\n"); /* spi_write_cmd is null */
405
406 return 3; /* return error */
407 }
408 if (handle->spi_write == NULL) /* check spi_write */
409 {
410 handle->debug_print("max7219: spi_write is null.\n"); /* spi_write is null */
411
412 return 3; /* return error */
413 }
414 if (handle->delay_ms == NULL) /* check delay_ms */
415 {
416 handle->debug_print("max7219: delay_ms is null.\n"); /* delay_ms is null */
417
418 return 3; /* return error */
419 }
420
421 if (handle->spi_init() != 0) /* spi init */
422 {
423 handle->debug_print("max7219: spi init failed.\n"); /* spi init failed */
424
425 return 1; /* return error */
426 }
427 handle->inited = 1; /* flag finish initialization */
428
429 return 0; /* success return 0 */
430}
431
444{
445 uint8_t res;
446 uint8_t mode = (uint8_t)MAX7219_MODE_SHUT_DOWN;
447
448 if (handle == NULL) /* check handle */
449 {
450 return 2; /* return error */
451 }
452 if (handle->inited != 1) /* check handle initialization */
453 {
454 return 3; /* return error */
455 }
456
457 res = handle->spi_write(MAX7219_REG_SHUT_DOWN, (uint8_t *)&mode, 1); /* spi write data */
458 if (res != 0) /* check result */
459 {
460 handle->debug_print("max7219: set mode failed.\n"); /* write failed */
461
462 return 4; /* return error */
463 }
464 res = handle->spi_deinit(); /* spi deinit */
465 if (res != 0) /* check result */
466 {
467 handle->debug_print("max7219: spi deinit failed.\n"); /* spi deinit failed */
468
469 return 1; /* return error */
470 }
471 handle->inited = 0; /* flag close */
472
473 return 0; /* success return 0 */
474}
475
489uint8_t max7219_set_reg(max7219_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
490{
491 if (handle == NULL) /* check handle */
492 {
493 return 2; /* return error */
494 }
495 if (handle->inited != 1) /* check handle initialization */
496 {
497 return 3; /* return error */
498 }
499
500 if (handle->spi_write(reg, buf, len) != 0) /* write data */
501 {
502 return 1; /* return error */
503 }
504 else
505 {
506 return 0; /* success return 0 */
507 }
508}
509
519{
520 if (info == NULL) /* check handle */
521 {
522 return 2; /* return error */
523 }
524
525 memset(info, 0, sizeof(max7219_info_t)); /* initialize max7219 info structure */
526 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
527 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
528 strncpy(info->interface, "SPI", 8); /* copy interface name */
529 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
530 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
531 info->max_current_ma = MAX_CURRENT; /* set maximum current */
532 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
533 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
534 info->driver_version = DRIVER_VERSION; /* set driver version */
535
536 return 0; /* success return 0 */
537}
#define MAX7219_REG_INTENSITY
#define MAX_CURRENT
#define MAX7219_REG_DECODE
chip register definition
#define MAX7219_REG_SHUT_DOWN
#define MAX7219_REG_SCAN_LIMIT
#define SUPPLY_VOLTAGE_MAX
#define MAX7219_REG_DISPLAY_TEST
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver max7219 header file
max7219_display_test_mode_t
max7219 display test mode enumeration definition
max7219_decode_t
max7219 decode enumeration definition
uint8_t max7219_set_display(max7219_handle_t *handle, max7219_digital_t digital, uint8_t data)
set the display content
uint8_t max7219_set_decode(max7219_handle_t *handle, max7219_decode_t decode)
set the decode mode
uint8_t max7219_set_scan_limit(max7219_handle_t *handle, max7219_scan_limit_t limit)
set the scan limit
max7219_scan_limit_t
max7219 scan limit enumeration definition
uint8_t max7219_info(max7219_info_t *info)
get chip's information
uint8_t max7219_init(max7219_handle_t *handle)
initialize the chip
max7219_digital_t
max7219 digital enumeration definition
max7219_mode_t
max7219 mode enumeration definition
uint8_t max7219_set_intensity(max7219_handle_t *handle, max7219_intensity_t intensity)
set the display intensity
uint8_t max7219_set_mode(max7219_handle_t *handle, max7219_mode_t mode)
set the chip mode
uint8_t max7219_deinit(max7219_handle_t *handle)
close the chip
struct max7219_info_s max7219_info_t
max7219 info structure definition
struct max7219_handle_s max7219_handle_t
max7219 handle structure definition
uint8_t max7219_set_matrix(max7219_handle_t *handle, uint8_t matrix[8])
set the content matrix
max7219_intensity_t
max7219 intensity enumeration definition
uint8_t max7219_set_display_test_mode(max7219_handle_t *handle, max7219_display_test_mode_t mode)
set the display test mode
@ MAX7219_MODE_SHUT_DOWN
#define MAX7219_MAX_CASCADE_SIZE
max7219 max cascade size definition
uint8_t max7219_set_cascade(max7219_handle_t *handle, max7219_cascade_t *cascade, uint16_t len)
set the cascade data
struct max7219_cascade_s max7219_cascade_t
max7219 cascade structure definition
uint8_t max7219_set_reg(max7219_handle_t *handle, uint8_t reg, uint8_t *buf, uint16_t len)
set the chip register
max7219_cascade_command_t command
uint8_t(* spi_init)(void)
uint8_t(* spi_write)(uint8_t reg, uint8_t *buf, uint16_t len)
void(* debug_print)(const char *const fmt,...)
uint8_t(* spi_deinit)(void)
uint8_t buf[MAX7219_MAX_CASCADE_SIZE *2]
void(* delay_ms)(uint32_t us)
uint8_t(* spi_write_cmd)(uint8_t *buf, uint16_t len)
uint32_t driver_version
char manufacturer_name[32]