--- /dev/null
+/*
+ * Copyright © 2023 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <ao.h>
+#include <ao_st7565.h>
+
+static void
+ao_st7565_reset(void)
+{
+ ao_gpio_set(AO_ST7565_RESET_PORT, AO_ST7565_RESET_PIN, 0);
+ ao_delay(AO_MS_TO_TICKS(100));
+ ao_gpio_set(AO_ST7565_RESET_PORT, AO_ST7565_RESET_PIN, 1);
+ ao_delay(AO_MS_TO_TICKS(100));
+}
+
+
+static void
+ao_st7565_start(uint8_t a0)
+{
+ ao_gpio_set(AO_ST7565_A0_PORT, AO_ST7565_A0_PIN, a0);
+ ao_spi_get_bit(AO_ST7565_CS_PORT,
+ AO_ST7565_CS_PIN,
+ AO_ST7565_SPI_BUS,
+ AO_ST7565_SPI_SPEED);
+}
+
+static void
+ao_st7565_stop(void)
+{
+ ao_spi_put_bit(AO_ST7565_CS_PORT,
+ AO_ST7565_CS_PIN,
+ AO_ST7565_SPI_BUS);
+ ao_gpio_set(AO_ST7565_A0_PORT, AO_ST7565_A0_PIN, 1);
+}
+
+
+static void
+ao_st7565_instruction(uint8_t cmd)
+{
+ ao_st7565_start(0);
+ ao_spi_send(&cmd, 1, AO_ST7565_SPI_BUS);
+ ao_st7565_stop();
+}
+
+static void
+ao_st7565_instruction_param(uint8_t cmd, uint8_t param)
+{
+ uint8_t b[2] = { cmd, param };
+ ao_st7565_start(0);
+ ao_spi_send(b, 2, AO_ST7565_SPI_BUS);
+ ao_st7565_stop();
+}
+
+static void
+ao_st7565_instructions(uint8_t *cmd, uint16_t len)
+{
+ ao_st7565_start(0);
+ ao_spi_send(cmd, len, AO_ST7565_SPI_BUS);
+ ao_st7565_stop();
+}
+
+static void
+ao_st7565_data(const void *base, uint16_t len)
+{
+ ao_st7565_start(1);
+ ao_spi_send(base, len, AO_ST7565_SPI_BUS);
+ ao_st7565_stop();
+}
+
+static void
+ao_st7565_set_brightness(uint8_t val)
+{
+ ao_st7565_instruction_param(AO_ST7565_ELECTRONIC_VOLUME_SET, val);
+}
+
+static bool setup_done;
+
+static void
+ao_st7565_setup(void)
+{
+ if (setup_done)
+ return;
+ setup_done = true;
+ ao_st7565_reset();
+ ao_st7565_instruction(AO_ST7565_BIAS);
+ ao_st7565_instruction(AO_ST7565_ADC_SELECT_NORMAL);
+
+ ao_st7565_instruction(AO_ST7565_COMMON_MODE_NORMAL);
+ ao_st7565_instruction(AO_ST7565_DISPLAY_START_LINE_SET(0));
+ ao_st7565_instruction(AO_ST7565_POWER_CONTROL_SET(0x4));
+ ao_delay(AO_MS_TO_TICKS(50));
+ ao_st7565_instruction(AO_ST7565_POWER_CONTROL_SET(0x6));
+ ao_delay(AO_MS_TO_TICKS(50));
+ ao_st7565_instruction(AO_ST7565_POWER_CONTROL_SET(0x7));
+ ao_delay(AO_MS_TO_TICKS(10));
+ ao_st7565_instruction(AO_ST7565_RESISTOR_RATIO_SET(5));
+ ao_st7565_instruction(AO_ST7565_DISPLAY_ON);
+ ao_st7565_set_brightness(0x10);
+}
+
+static uint8_t rotbuf[AO_ST7565_WIDTH];
+
+void
+ao_st7565_update(struct ao_bitmap *bitmap)
+{
+ uint8_t col, c, page;
+ int row;
+ uint32_t *line;
+ uint8_t *r;
+
+ ao_st7565_setup();
+
+ line = bitmap->base;
+ for (page = 0; page < 8; page++) {
+ uint8_t i[4] = {
+ AO_ST7565_PAGE_ADDRESS_SET(7-page),
+ AO_ST7565_COLUMN_ADDRESS_SET_MSN(0 >> 4),
+ AO_ST7565_COLUMN_ADDRESS_SET_MSN(0 & 0xf),
+ AO_ST7565_RMW
+ };
+ memset(rotbuf, 0, sizeof(rotbuf));
+ for (row = 7; row >= 0; row--) {
+ r = rotbuf;
+ for (col = 0; col < AO_BITMAP_STRIDE(AO_ST7565_WIDTH); col++) {
+ uint32_t bits = ~*line++;
+ for (c = 0; c < 32; c++) {
+ *r++ |= ((bits >> c) & 1) << row;
+ }
+ }
+ }
+ ao_st7565_instructions(i, 4);
+ ao_st7565_data(rotbuf, AO_ST7565_WIDTH);
+ }
+}
+
+void
+ao_st7565_init(void)
+{
+ ao_enable_output(AO_ST7565_RESET_PORT, AO_ST7565_RESET_PIN, 1);
+ ao_enable_output(AO_ST7565_A0_PORT, AO_ST7565_A0_PIN, 1);
+
+ ao_enable_cs(AO_ST7565_CS_PORT, AO_ST7565_CS_PIN);
+}
--- /dev/null
+/*
+ * Copyright © 2023 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#ifndef _AO_ST7565_H_
+#define _AO_ST7565_H_
+
+#include <ao_draw.h>
+
+#define AO_ST7565_DISPLAY_OFF 0xae
+#define AO_ST7565_DISPLAY_ON 0xaf
+#define AO_ST7565_DISPLAY_START_LINE_SET(line) (0x40 | (line))
+#define AO_ST7565_PAGE_ADDRESS_SET(page) (0xb0 | (page))
+#define AO_ST7565_COLUMN_ADDRESS_SET_MSN(nib) (0x10 | (nib))
+#define AO_ST7565_COLUMN_ADDRESS_SET_LSN(nib) (0x00 | (nib))
+#define AO_ST7565_ADC_SELECT_NORMAL 0xa0
+#define AO_ST7565_ADC_SELECT_REVERSE 0xa1
+#define AO_ST7565_DISPLAY_NORMAL 0xa6
+#define AO_ST7565_DISPLAY_REVERSE 0xa7
+#define AO_ST7565_DISPLAY_ALL_POINTS_OFF 0xa4
+#define AO_ST7565_DISPLAY_ALL_POINTS_ON 0xa5
+#define AO_ST7565_LCD_BIAS_1_9 0xa2
+#define AO_ST7565_LCD_BIAS_1_7 0xa3
+#define AO_ST7565_RMW 0xe0
+#define AO_ST7565_RMW_END 0xee
+#define AO_ST7565_RESET 0xe2
+#define AO_ST7565_COMMON_MODE_NORMAL 0xc0
+#define AO_ST7565_COMMON_MODE_REVERSE 0xc8
+#define AO_ST7565_POWER_CONTROL_SET(pc) (0x28 | (pc))
+#define AO_ST7565_RESISTOR_RATIO_SET(rr) (0x20 | (rr))
+#define AO_ST7565_ELECTRONIC_VOLUME_SET 0x81
+#define AO_ST7565_SLEEP_MODE 0xac
+#define AO_ST7565_BOOSTER_RATIO_SET 0xf8
+#define AO_ST7565_NOP 0xe3
+
+#define AO_ST7565_SPI_SPEED ao_spi_speed(20000000)
+
+void
+ao_st7565_update(struct ao_bitmap *bitmap);
+
+void
+ao_st7565_init(void);
+
+#endif /* _AO_ST7565_H_ */