2 * Copyright © 2023 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <ao_st7565.h>
25 ao_gpio_set(AO_ST7565_RESET_PORT, AO_ST7565_RESET_PIN, 0);
26 ao_delay(AO_MS_TO_TICKS(100));
27 ao_gpio_set(AO_ST7565_RESET_PORT, AO_ST7565_RESET_PIN, 1);
28 ao_delay(AO_MS_TO_TICKS(100));
33 ao_st7565_start(uint8_t a0)
35 ao_gpio_set(AO_ST7565_A0_PORT, AO_ST7565_A0_PIN, a0);
36 ao_spi_get_bit(AO_ST7565_CS_PORT,
45 ao_spi_put_bit(AO_ST7565_CS_PORT,
48 ao_gpio_set(AO_ST7565_A0_PORT, AO_ST7565_A0_PIN, 1);
53 ao_st7565_instruction(uint8_t cmd)
56 ao_spi_send(&cmd, 1, AO_ST7565_SPI_BUS);
61 ao_st7565_instruction_param(uint8_t cmd, uint8_t param)
63 uint8_t b[2] = { cmd, param };
65 ao_spi_send(b, 2, AO_ST7565_SPI_BUS);
70 ao_st7565_instructions(uint8_t *cmd, uint16_t len)
73 ao_spi_send(cmd, len, AO_ST7565_SPI_BUS);
78 ao_st7565_data(const void *base, uint16_t len)
81 ao_spi_send(base, len, AO_ST7565_SPI_BUS);
86 ao_st7565_set_brightness(uint8_t val)
88 ao_st7565_instruction_param(AO_ST7565_ELECTRONIC_VOLUME_SET, val);
91 static bool setup_done;
101 * Should be set to one of AO_ST7565_LCD_BIAS_1_9 or
102 * AO_ST7565_LCD_BIAS_1_7
104 ao_st7565_instruction(AO_ST7565_BIAS);
105 ao_st7565_instruction(AO_ST7565_ADC_SELECT_NORMAL);
107 ao_st7565_instruction(AO_ST7565_COMMON_MODE_NORMAL);
108 ao_st7565_instruction(AO_ST7565_DISPLAY_START_LINE_SET(0));
109 ao_st7565_instruction(AO_ST7565_POWER_CONTROL_SET(0x4));
110 ao_delay(AO_MS_TO_TICKS(50));
111 ao_st7565_instruction(AO_ST7565_POWER_CONTROL_SET(0x6));
112 ao_delay(AO_MS_TO_TICKS(50));
113 ao_st7565_instruction(AO_ST7565_POWER_CONTROL_SET(0x7));
114 ao_delay(AO_MS_TO_TICKS(10));
115 ao_st7565_instruction(AO_ST7565_RESISTOR_RATIO_SET(5));
116 ao_st7565_instruction(AO_ST7565_DISPLAY_ON);
117 ao_st7565_set_brightness(0x10);
120 static uint8_t rotbuf[AO_ST7565_WIDTH];
123 ao_st7565_update(struct ao_bitmap *bitmap)
125 uint8_t col, c, page;
133 for (page = 0; page < 8; page++) {
135 AO_ST7565_PAGE_ADDRESS_SET(7-page),
136 AO_ST7565_COLUMN_ADDRESS_SET_MSN(0 >> 4),
137 AO_ST7565_COLUMN_ADDRESS_SET_MSN(0 & 0xf),
140 memset(rotbuf, 0, sizeof(rotbuf));
141 for (row = 7; row >= 0; row--) {
143 for (col = 0; col < AO_BITMAP_STRIDE(AO_ST7565_WIDTH); col++) {
144 uint32_t bits = ~*line++;
145 for (c = 0; c < 32; c++) {
146 *r++ |= ((bits >> c) & 1) << row;
150 ao_st7565_instructions(i, 4);
151 ao_st7565_data(rotbuf, AO_ST7565_WIDTH);
158 ao_enable_output(AO_ST7565_RESET_PORT, AO_ST7565_RESET_PIN, 1);
159 ao_enable_output(AO_ST7565_A0_PORT, AO_ST7565_A0_PIN, 1);
161 ao_enable_cs(AO_ST7565_CS_PORT, AO_ST7565_CS_PIN);