qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

st7565.c (15029B)


      1 /*
      2 Copyright 2021
      3 
      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.
      8 
      9 This program is distributed in the hope that it will be useful,
     10 but WITHOUT ANY WARRANTY; without even the implied warranty of
     11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12 GNU General Public License for more details.
     13 
     14 You should have received a copy of the GNU General Public License
     15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16 */
     17 
     18 #include "st7565.h"
     19 
     20 #include <string.h>
     21 
     22 #include "compiler_support.h"
     23 #include "keyboard.h"
     24 #include "progmem.h"
     25 #include "timer.h"
     26 #include "wait.h"
     27 
     28 #include ST7565_FONT_H
     29 
     30 // Fundamental Commands
     31 #define CONTRAST 0x81
     32 #define DISPLAY_ALL_ON 0xA5
     33 #define DISPLAY_ALL_ON_RESUME 0xA4
     34 #define NORMAL_DISPLAY 0xA6
     35 #define INVERT_DISPLAY 0xA7
     36 #define DISPLAY_ON 0xAF
     37 #define DISPLAY_OFF 0xAE
     38 #define NOP 0xE3
     39 
     40 // Addressing Setting Commands
     41 #define PAM_SETCOLUMN_LSB 0x00
     42 #define PAM_SETCOLUMN_MSB 0x10
     43 #define PAM_PAGE_ADDR 0xB0 // 0xb0 -- 0xb7
     44 
     45 // Hardware Configuration Commands
     46 #define DISPLAY_START_LINE 0x40
     47 #define SEGMENT_REMAP 0xA0
     48 #define SEGMENT_REMAP_INV 0xA1
     49 #define COM_SCAN_INC 0xC0
     50 #define COM_SCAN_DEC 0xC8
     51 #define LCD_BIAS_7 0xA3
     52 #define LCD_BIAS_9 0xA2
     53 #define RESISTOR_RATIO 0x20
     54 #define POWER_CONTROL 0x28
     55 
     56 // Misc defines
     57 #ifndef ST7565_BLOCK_COUNT
     58 #    define ST7565_BLOCK_COUNT (sizeof(ST7565_BLOCK_TYPE) * 8)
     59 #endif
     60 #ifndef ST7565_BLOCK_SIZE
     61 #    define ST7565_BLOCK_SIZE (ST7565_MATRIX_SIZE / ST7565_BLOCK_COUNT)
     62 #endif
     63 
     64 #define ST7565_ALL_BLOCKS_MASK (((((ST7565_BLOCK_TYPE)1 << (ST7565_BLOCK_COUNT - 1)) - 1) << 1) | 1)
     65 
     66 #define HAS_FLAGS(bits, flags) ((bits & flags) == flags)
     67 
     68 // Display buffer's is the same as the display memory layout
     69 // this is so we don't end up with rounding errors with
     70 // parts of the display unusable or don't get cleared correctly
     71 // and also allows for drawing & inverting
     72 uint8_t            st7565_buffer[ST7565_MATRIX_SIZE];
     73 uint8_t           *st7565_cursor;
     74 ST7565_BLOCK_TYPE  st7565_dirty       = 0;
     75 bool               st7565_initialized = false;
     76 bool               st7565_active      = false;
     77 bool               st7565_inverted    = false;
     78 display_rotation_t st7565_rotation    = DISPLAY_ROTATION_0;
     79 #if ST7565_TIMEOUT > 0
     80 uint32_t st7565_timeout;
     81 #endif
     82 #if ST7565_UPDATE_INTERVAL > 0
     83 uint16_t st7565_update_timeout;
     84 #endif
     85 
     86 // Flips the rendering bits for a character at the current cursor position
     87 static void InvertCharacter(uint8_t *cursor) {
     88     const uint8_t *end = cursor + ST7565_FONT_WIDTH;
     89     while (cursor < end) {
     90         *cursor = ~(*cursor);
     91         cursor++;
     92     }
     93 }
     94 
     95 bool st7565_init(display_rotation_t rotation) {
     96     gpio_set_pin_output(ST7565_A0_PIN);
     97     gpio_write_pin_high(ST7565_A0_PIN);
     98     gpio_set_pin_output(ST7565_RST_PIN);
     99     gpio_write_pin_high(ST7565_RST_PIN);
    100 
    101     st7565_rotation = st7565_init_user(rotation);
    102 
    103     spi_init();
    104     spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
    105 
    106     st7565_reset();
    107 
    108     st7565_send_cmd(LCD_BIAS_7);
    109     if (!HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
    110         st7565_send_cmd(SEGMENT_REMAP);
    111         st7565_send_cmd(COM_SCAN_DEC);
    112     } else {
    113         st7565_send_cmd(SEGMENT_REMAP_INV);
    114         st7565_send_cmd(COM_SCAN_INC);
    115     }
    116     st7565_send_cmd(DISPLAY_START_LINE | 0x00);
    117     st7565_send_cmd(CONTRAST);
    118     st7565_send_cmd(ST7565_CONTRAST);
    119     st7565_send_cmd(RESISTOR_RATIO | 0x01);
    120     st7565_send_cmd(POWER_CONTROL | 0x04);
    121     wait_ms(50);
    122     st7565_send_cmd(POWER_CONTROL | 0x06);
    123     wait_ms(50);
    124     st7565_send_cmd(POWER_CONTROL | 0x07);
    125     wait_ms(10);
    126     st7565_send_cmd(DISPLAY_ON);
    127     st7565_send_cmd(DISPLAY_ALL_ON_RESUME);
    128     st7565_send_cmd(NORMAL_DISPLAY);
    129 
    130     spi_stop();
    131 
    132 #if ST7565_TIMEOUT > 0
    133     st7565_timeout = timer_read32() + ST7565_TIMEOUT;
    134 #endif
    135 
    136     st7565_clear();
    137     st7565_initialized = true;
    138     st7565_active      = true;
    139     return true;
    140 }
    141 
    142 __attribute__((weak)) display_rotation_t st7565_init_user(display_rotation_t rotation) {
    143     return rotation;
    144 }
    145 
    146 void st7565_clear(void) {
    147     memset(st7565_buffer, 0, sizeof(st7565_buffer));
    148     st7565_cursor = &st7565_buffer[0];
    149     st7565_dirty  = ST7565_ALL_BLOCKS_MASK;
    150 }
    151 
    152 uint8_t crot(uint8_t a, int8_t n) {
    153     const uint8_t mask = 0x7;
    154     n &= mask;
    155     return a << n | a >> (-n & mask);
    156 }
    157 
    158 void st7565_render(void) {
    159     if (!st7565_initialized) {
    160         return;
    161     }
    162 
    163     // Do we have work to do?
    164     st7565_dirty &= ST7565_ALL_BLOCKS_MASK;
    165     if (!st7565_dirty) {
    166         return;
    167     }
    168 
    169     // Find first dirty block
    170     uint8_t update_start = 0;
    171     while (!(st7565_dirty & ((ST7565_BLOCK_TYPE)1 << update_start))) {
    172         ++update_start;
    173     }
    174 
    175     // Calculate commands to set memory addressing bounds.
    176     uint8_t start_page   = ST7565_BLOCK_SIZE * update_start / ST7565_DISPLAY_WIDTH;
    177     uint8_t start_column = ST7565_BLOCK_SIZE * update_start % ST7565_DISPLAY_WIDTH;
    178     // IC has 132 segment drivers, for panels with less width we need to offset the starting column
    179     if (HAS_FLAGS(st7565_rotation, DISPLAY_ROTATION_180)) {
    180         start_column += (132 - ST7565_DISPLAY_WIDTH);
    181     }
    182 
    183     spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
    184 
    185     st7565_send_cmd(PAM_PAGE_ADDR | start_page);
    186     st7565_send_cmd(PAM_SETCOLUMN_LSB | ((ST7565_COLUMN_OFFSET + start_column) & 0x0f));
    187     st7565_send_cmd(PAM_SETCOLUMN_MSB | ((ST7565_COLUMN_OFFSET + start_column) >> 4 & 0x0f));
    188 
    189     st7565_send_data(&st7565_buffer[ST7565_BLOCK_SIZE * update_start], ST7565_BLOCK_SIZE);
    190 
    191     spi_stop();
    192 
    193     // Turn on display if it is off
    194     st7565_on();
    195 
    196     // Clear dirty flag
    197     st7565_dirty &= ~((ST7565_BLOCK_TYPE)1 << update_start);
    198 }
    199 
    200 void st7565_set_cursor(uint8_t col, uint8_t line) {
    201     uint16_t index = line * ST7565_DISPLAY_WIDTH + col * ST7565_FONT_WIDTH;
    202 
    203     // Out of bounds?
    204     if (index >= ST7565_MATRIX_SIZE) {
    205         index = 0;
    206     }
    207 
    208     st7565_cursor = &st7565_buffer[index];
    209 }
    210 
    211 void st7565_advance_page(bool clearPageRemainder) {
    212     uint16_t index     = st7565_cursor - &st7565_buffer[0];
    213     uint8_t  remaining = ST7565_DISPLAY_WIDTH - (index % ST7565_DISPLAY_WIDTH);
    214 
    215     if (clearPageRemainder) {
    216         // Remaining Char count
    217         remaining = remaining / ST7565_FONT_WIDTH;
    218 
    219         // Write empty character until next line
    220         while (remaining--)
    221             st7565_write_char(' ', false);
    222     } else {
    223         // Next page index out of bounds?
    224         if (index + remaining >= ST7565_MATRIX_SIZE) {
    225             index     = 0;
    226             remaining = 0;
    227         }
    228 
    229         st7565_cursor = &st7565_buffer[index + remaining];
    230     }
    231 }
    232 
    233 void st7565_advance_char(void) {
    234     uint16_t nextIndex      = st7565_cursor - &st7565_buffer[0] + ST7565_FONT_WIDTH;
    235     uint8_t  remainingSpace = ST7565_DISPLAY_WIDTH - (nextIndex % ST7565_DISPLAY_WIDTH);
    236 
    237     // Do we have enough space on the current line for the next character
    238     if (remainingSpace < ST7565_FONT_WIDTH) {
    239         nextIndex += remainingSpace;
    240     }
    241 
    242     // Did we go out of bounds
    243     if (nextIndex >= ST7565_MATRIX_SIZE) {
    244         nextIndex = 0;
    245     }
    246 
    247     // Update cursor position
    248     st7565_cursor = &st7565_buffer[nextIndex];
    249 }
    250 
    251 // Main handler that writes character data to the display buffer
    252 void st7565_write_char(const char data, bool invert) {
    253     // Advance to the next line if newline
    254     if (data == '\n') {
    255         // Old source wrote ' ' until end of line...
    256         st7565_advance_page(true);
    257         return;
    258     }
    259 
    260     if (data == '\r') {
    261         st7565_advance_page(false);
    262         return;
    263     }
    264 
    265     // copy the current render buffer to check for dirty after
    266     static uint8_t st7565_temp_buffer[ST7565_FONT_WIDTH];
    267     memcpy(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH);
    268 
    269     STATIC_ASSERT(sizeof(font) >= ((ST7565_FONT_END + 1 - ST7565_FONT_START) * ST7565_FONT_WIDTH), "ST7565_FONT_END references outside array");
    270 
    271     // set the reder buffer data
    272     uint8_t cast_data = (uint8_t)data; // font based on unsigned type for index
    273     if (cast_data < ST7565_FONT_START || cast_data > ST7565_FONT_END) {
    274         memset(st7565_cursor, 0x00, ST7565_FONT_WIDTH);
    275     } else {
    276         const uint8_t *glyph = &font[(cast_data - ST7565_FONT_START) * ST7565_FONT_WIDTH];
    277         memcpy_P(st7565_cursor, glyph, ST7565_FONT_WIDTH);
    278     }
    279 
    280     // Invert if needed
    281     if (invert) {
    282         InvertCharacter(st7565_cursor);
    283     }
    284 
    285     // Dirty check
    286     if (memcmp(&st7565_temp_buffer, st7565_cursor, ST7565_FONT_WIDTH)) {
    287         uint16_t index = st7565_cursor - &st7565_buffer[0];
    288         st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
    289         // Edgecase check if the written data spans the 2 chunks
    290         st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << ((index + ST7565_FONT_WIDTH - 1) / ST7565_BLOCK_SIZE));
    291     }
    292 
    293     // Finally move to the next char
    294     st7565_advance_char();
    295 }
    296 
    297 void st7565_write(const char *data, bool invert) {
    298     const char *end = data + strlen(data);
    299     while (data < end) {
    300         st7565_write_char(*data, invert);
    301         data++;
    302     }
    303 }
    304 
    305 void st7565_write_ln(const char *data, bool invert) {
    306     st7565_write(data, invert);
    307     st7565_advance_page(true);
    308 }
    309 
    310 void st7565_pan(bool left) {
    311     uint16_t i = 0;
    312     for (uint16_t y = 0; y < ST7565_DISPLAY_HEIGHT / 8; y++) {
    313         if (left) {
    314             for (uint16_t x = 0; x < ST7565_DISPLAY_WIDTH - 1; x++) {
    315                 i                = y * ST7565_DISPLAY_WIDTH + x;
    316                 st7565_buffer[i] = st7565_buffer[i + 1];
    317             }
    318         } else {
    319             for (uint16_t x = ST7565_DISPLAY_WIDTH - 1; x > 0; x--) {
    320                 i                = y * ST7565_DISPLAY_WIDTH + x;
    321                 st7565_buffer[i] = st7565_buffer[i - 1];
    322             }
    323         }
    324     }
    325     st7565_dirty = ST7565_ALL_BLOCKS_MASK;
    326 }
    327 
    328 display_buffer_reader_t st7565_read_raw(uint16_t start_index) {
    329     if (start_index > ST7565_MATRIX_SIZE) start_index = ST7565_MATRIX_SIZE;
    330     display_buffer_reader_t ret_reader;
    331     ret_reader.current_element         = &st7565_buffer[start_index];
    332     ret_reader.remaining_element_count = ST7565_MATRIX_SIZE - start_index;
    333     return ret_reader;
    334 }
    335 
    336 void st7565_write_raw_byte(const char data, uint16_t index) {
    337     if (index > ST7565_MATRIX_SIZE) index = ST7565_MATRIX_SIZE;
    338     if (st7565_buffer[index] == data) return;
    339     st7565_buffer[index] = data;
    340     st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
    341 }
    342 
    343 void st7565_write_raw(const char *data, uint16_t size) {
    344     uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
    345     if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
    346     for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
    347         uint8_t c = *data++;
    348         if (st7565_buffer[i] == c) continue;
    349         st7565_buffer[i] = c;
    350         st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
    351     }
    352 }
    353 
    354 void st7565_write_pixel(uint8_t x, uint8_t y, bool on) {
    355     if (x >= ST7565_DISPLAY_WIDTH) {
    356         return;
    357     }
    358     uint16_t index = x + (y / 8) * ST7565_DISPLAY_WIDTH;
    359     if (index >= ST7565_MATRIX_SIZE) {
    360         return;
    361     }
    362     uint8_t data = st7565_buffer[index];
    363     if (on) {
    364         data |= (1 << (y % 8));
    365     } else {
    366         data &= ~(1 << (y % 8));
    367     }
    368     if (st7565_buffer[index] != data) {
    369         st7565_buffer[index] = data;
    370         st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (index / ST7565_BLOCK_SIZE));
    371     }
    372 }
    373 
    374 #if defined(__AVR__)
    375 void st7565_write_P(const char *data, bool invert) {
    376     uint8_t c = pgm_read_byte(data);
    377     while (c != 0) {
    378         st7565_write_char(c, invert);
    379         c = pgm_read_byte(++data);
    380     }
    381 }
    382 
    383 void st7565_write_ln_P(const char *data, bool invert) {
    384     st7565_write_P(data, invert);
    385     st7565_advance_page(true);
    386 }
    387 
    388 void st7565_write_raw_P(const char *data, uint16_t size) {
    389     uint16_t cursor_start_index = st7565_cursor - &st7565_buffer[0];
    390     if ((size + cursor_start_index) > ST7565_MATRIX_SIZE) size = ST7565_MATRIX_SIZE - cursor_start_index;
    391     for (uint16_t i = cursor_start_index; i < cursor_start_index + size; i++) {
    392         uint8_t c = pgm_read_byte(data++);
    393         if (st7565_buffer[i] == c) continue;
    394         st7565_buffer[i] = c;
    395         st7565_dirty |= ((ST7565_BLOCK_TYPE)1 << (i / ST7565_BLOCK_SIZE));
    396     }
    397 }
    398 #endif // defined(__AVR__)
    399 
    400 bool st7565_on(void) {
    401     if (!st7565_initialized) {
    402         return st7565_active;
    403     }
    404 
    405 #if ST7565_TIMEOUT > 0
    406     st7565_timeout = timer_read32() + ST7565_TIMEOUT;
    407 #endif
    408 
    409     if (!st7565_active) {
    410         spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
    411         st7565_send_cmd(DISPLAY_ON);
    412         spi_stop();
    413         st7565_active = true;
    414         st7565_on_user();
    415     }
    416     return st7565_active;
    417 }
    418 
    419 __attribute__((weak)) void st7565_on_user(void) {}
    420 
    421 bool st7565_off(void) {
    422     if (!st7565_initialized) {
    423         return !st7565_active;
    424     }
    425 
    426     if (st7565_active) {
    427         spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
    428         st7565_send_cmd(DISPLAY_OFF);
    429         spi_stop();
    430         st7565_active = false;
    431         st7565_off_user();
    432     }
    433     return !st7565_active;
    434 }
    435 
    436 __attribute__((weak)) void st7565_off_user(void) {}
    437 
    438 bool st7565_is_on(void) {
    439     return st7565_active;
    440 }
    441 
    442 bool st7565_invert(bool invert) {
    443     if (!st7565_initialized) {
    444         return st7565_inverted;
    445     }
    446 
    447     if (invert != st7565_inverted) {
    448         spi_start(ST7565_SS_PIN, false, 0, ST7565_SPI_CLK_DIVISOR);
    449         st7565_send_cmd(invert ? INVERT_DISPLAY : NORMAL_DISPLAY);
    450         spi_stop();
    451         st7565_inverted = invert;
    452     }
    453     return st7565_inverted;
    454 }
    455 
    456 uint8_t st7565_max_chars(void) {
    457     return ST7565_DISPLAY_WIDTH / ST7565_FONT_WIDTH;
    458 }
    459 
    460 uint8_t st7565_max_lines(void) {
    461     return ST7565_DISPLAY_HEIGHT / ST7565_FONT_HEIGHT;
    462 }
    463 
    464 void st7565_task(void) {
    465     if (!st7565_initialized) {
    466         return;
    467     }
    468 
    469 #if ST7565_UPDATE_INTERVAL > 0
    470     if (timer_elapsed(st7565_update_timeout) >= ST7565_UPDATE_INTERVAL) {
    471         st7565_update_timeout = timer_read();
    472         st7565_set_cursor(0, 0);
    473         st7565_task_user();
    474     }
    475 #else
    476     st7565_set_cursor(0, 0);
    477     st7565_task_user();
    478 #endif
    479 
    480     // Smart render system, no need to check for dirty
    481     st7565_render();
    482 
    483     // Display timeout check
    484 #if ST7565_TIMEOUT > 0
    485     if (st7565_active && timer_expired32(timer_read32(), st7565_timeout)) {
    486         st7565_off();
    487     }
    488 #endif
    489 }
    490 
    491 __attribute__((weak)) void st7565_task_user(void) {}
    492 
    493 void st7565_reset(void) {
    494     gpio_write_pin_low(ST7565_RST_PIN);
    495     wait_ms(20);
    496     gpio_write_pin_high(ST7565_RST_PIN);
    497     wait_ms(20);
    498 }
    499 
    500 spi_status_t st7565_send_cmd(uint8_t cmd) {
    501     gpio_write_pin_low(ST7565_A0_PIN);
    502     return spi_write(cmd);
    503 }
    504 
    505 spi_status_t st7565_send_data(uint8_t *data, uint16_t length) {
    506     gpio_write_pin_high(ST7565_A0_PIN);
    507     return spi_transmit(data, length);
    508 }