altos: Add AS1107 LED display driver
authorKeith Packard <keithp@keithp.com>
Mon, 3 Apr 2017 00:39:05 +0000 (17:39 -0700)
committerKeith Packard <keithp@keithp.com>
Mon, 3 Apr 2017 00:39:05 +0000 (17:39 -0700)
Signed-off-by: Keith Packard <keithp@keithp.com>
src/drivers/ao_as1107.c [new file with mode: 0644]
src/drivers/ao_as1107.h [new file with mode: 0644]

diff --git a/src/drivers/ao_as1107.c b/src/drivers/ao_as1107.c
new file mode 100644 (file)
index 0000000..0b83ab2
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * Copyright © 2017 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#include <ao.h>
+#include <ao_as1107.h>
+
+static uint8_t as1107_configured;
+static uint8_t as1107_mutex;
+
+static void
+ao_as1107_start(void) {
+       ao_spi_get_bit(AO_AS1107_CS_PORT, AO_AS1107_CS_PIN, AO_AS1107_CS, AO_AS1107_SPI_INDEX, AO_AS1107_SPI_SPEED);
+}
+
+static void
+ao_as1107_stop(void) {
+       ao_spi_put_bit(AO_AS1107_CS_PORT, AO_AS1107_CS_PIN, AO_AS1107_CS, AO_AS1107_SPI_INDEX);
+}
+
+static void
+_ao_as1107_cmd(uint8_t addr, uint8_t value)
+{
+       uint8_t packet[2] = { addr, value };
+
+       ao_as1107_start();
+       ao_spi_send(packet, 2, AO_AS1107_SPI_INDEX);
+       ao_as1107_stop();
+}
+
+static void
+_ao_as1107_setup(void)
+{
+       if (!as1107_configured) {
+               as1107_configured = 1;
+               _ao_as1107_cmd(AO_AS1107_SHUTDOWN, AO_AS1107_SHUTDOWN_SHUTDOWN_RESET);
+               _ao_as1107_cmd(AO_AS1107_DECODE_MODE, AO_AS1107_DECODE);
+               _ao_as1107_cmd(AO_AS1107_SCAN_LIMIT, AO_AS1107_NUM_DIGITS - 1);
+               _ao_as1107_cmd(AO_AS1107_FEATURE,
+                              (0 << AO_AS1107_FEATURE_CLK_EN) |
+                              (0 << AO_AS1107_FEATURE_REG_RES) |
+                              (1 << AO_AS1107_FEATURE_DECODE_SEL) |
+                              (1 << AO_AS1107_FEATURE_SPI_EN) |
+                              (0 << AO_AS1107_FEATURE_BLINK_EN) |
+                              (0 << AO_AS1107_FEATURE_BLINK_FREQ) |
+                              (0 << AO_AS1107_FEATURE_SYNC) |
+                              (0 << AO_AS1107_FEATURE_BLINK_START));
+       }
+}
+
+void
+ao_as1107_write(uint8_t start, uint8_t count, uint8_t *values)
+{
+       uint8_t i;
+       ao_mutex_get(&as1107_mutex);
+       _ao_as1107_setup();
+       for (i = 0; i < count; i++)
+       {
+               _ao_as1107_cmd(AO_AS1107_DIGIT(start + i),
+                              values[i]);
+       }
+       ao_mutex_put(&as1107_mutex);
+}
+
+void
+ao_as1107_write_8(uint8_t start, uint8_t value)
+{
+       uint8_t values[2];
+
+       values[0] = (value >> 4);
+       values[1] = value & 0xf;
+       ao_as1107_write(start, 2, values);
+}
+
+void
+ao_as1107_write_16(uint8_t start, uint16_t value)
+{
+       uint8_t values[4];
+
+       values[0] = (value >> 12);
+       values[1] = (value >> 8) & 0xf;
+       values[2] = (value >> 4) & 0xf;
+       values[3] = (value) & 0xf;
+       ao_as1107_write(start, 4, values);
+}
+
+void
+ao_as1107_init(void)
+{
+       as1107_configured = 0;
+       ao_spi_init_cs(AO_AS1107_CS_PORT, (1 << AO_AS1107_CS_PIN));
+}
diff --git a/src/drivers/ao_as1107.h b/src/drivers/ao_as1107.h
new file mode 100644 (file)
index 0000000..a22b17d
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright © 2017 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#ifndef _AO_AS1107_H_
+#define _AO_AS1107_H_
+
+#define AO_AS1107_NO_OP                0x00
+#define AO_AS1107_DIGIT(n)     (0x01 + (n))
+#define AO_AS1107_DECODE_MODE  0x09
+#define AO_AS1107_INTENSITY    0x0a
+#define AO_AS1107_SCAN_LIMIT   0x0b
+#define AO_AS1107_SHUTDOWN     0x0c
+#define  AO_AS1107_SHUTDOWN_SHUTDOWN_RESET     0x00
+#define  AO_AS1107_SHUTDOWN_SHUTDOWN_NOP       0x80
+#define  AO_AS1107_SHUTDOWN_NORMAL_RESET       0x01
+#define  AO_AS1107_SHUTDOWN_NORMAL_NOP         0x81
+
+#define AO_AS1107_FEATURE      0x0e
+#define  AO_AS1107_FEATURE_CLK_EN      0       /* external clock enable */
+#define  AO_AS1107_FEATURE_REG_RES     1
+#define  AO_AS1107_FEATURE_DECODE_SEL  2       /* select HEX decode */
+#define  AO_AS1107_FEATURE_SPI_EN      3
+#define  AO_AS1107_FEATURE_BLINK_EN    4
+#define  AO_AS1107_FEATURE_BLINK_FREQ  5
+#define  AO_AS1107_FEATURE_SYNC                6
+#define  AO_AS1107_FEATURE_BLINK_START 7
+#define AO_AS1107_DISPLAY_TEST 0x0f
+
+void ao_as1107_init(void);
+
+void
+ao_as1107_write(uint8_t start, uint8_t count, uint8_t *values);
+
+void
+ao_as1107_write_8(uint8_t start, uint8_t value);
+
+void
+ao_as1107_write_16(uint8_t start, uint16_t value);
+
+#ifndef AO_AS1107_DECODE
+#error "must define AO_AS1107_DECODE"
+#endif
+
+#ifndef AO_AS1107_NUM_DIGITS
+#error "must define AO_AS1107_NUM_DIGITS"
+#endif
+
+#endif /* _AO_AS1107_H_ */