diff options
| author | Ryan <fauxpark@gmail.com> | 2020-11-26 23:44:17 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-26 23:44:17 +1100 |
| commit | 3afe0ea9b9f67ae33f54c1393b15d988764241a2 (patch) | |
| tree | eb2c1527bd7ed3e4f06d8b742afab564bd8168e8 /drivers | |
| parent | 1c0e8a6bb4718c693e24d87c19ccca435d2f1f58 (diff) | |
| download | qmk_firmware-3afe0ea9b9f67ae33f54c1393b15d988764241a2.tar.gz qmk_firmware-3afe0ea9b9f67ae33f54c1393b15d988764241a2.zip | |
ST7565 tidyup (#10907)
Diffstat (limited to 'drivers')
| -rw-r--r-- | drivers/ugfx/gdisp/st7565/board_st7565_template.h | 58 | ||||
| -rw-r--r-- | drivers/ugfx/gdisp/st7565/driver.mk | 2 | ||||
| -rw-r--r-- | drivers/ugfx/gdisp/st7565/gdisp_lld_ST7565.c | 76 |
3 files changed, 56 insertions, 80 deletions
diff --git a/drivers/ugfx/gdisp/st7565/board_st7565_template.h b/drivers/ugfx/gdisp/st7565/board_st7565_template.h index 194f8ea24..875ed9e65 100644 --- a/drivers/ugfx/gdisp/st7565/board_st7565_template.h +++ b/drivers/ugfx/gdisp/st7565/board_st7565_template.h | |||
| @@ -8,8 +8,9 @@ | |||
| 8 | #ifndef _GDISP_LLD_BOARD_H | 8 | #ifndef _GDISP_LLD_BOARD_H |
| 9 | #define _GDISP_LLD_BOARD_H | 9 | #define _GDISP_LLD_BOARD_H |
| 10 | 10 | ||
| 11 | #define ST7565_LCD_BIAS ST7565_LCD_BIAS_9 // actually 6 | 11 | #include "quantum.h" |
| 12 | #define ST7565_ADC ST7565_ADC_NORMAL | 12 | |
| 13 | #define ST7565_LCD_BIAS ST7565_LCD_BIAS_7 | ||
| 13 | #define ST7565_COM_SCAN ST7565_COM_SCAN_DEC | 14 | #define ST7565_COM_SCAN ST7565_COM_SCAN_DEC |
| 14 | #define ST7565_PAGE_ORDER 0, 1, 2, 3 | 15 | #define ST7565_PAGE_ORDER 0, 1, 2, 3 |
| 15 | /* | 16 | /* |
| @@ -17,19 +18,12 @@ | |||
| 17 | * #define ST7565_PAGE_ORDER 4,5,6,7,0,1,2,3 | 18 | * #define ST7565_PAGE_ORDER 4,5,6,7,0,1,2,3 |
| 18 | */ | 19 | */ |
| 19 | 20 | ||
| 20 | #define ST7565_GPIOPORT GPIOC | 21 | #define ST7565_A0_PIN C7 |
| 21 | #define ST7565_PORT PORTC | 22 | #define ST7565_RST_PIN C8 |
| 22 | #define ST7565_A0_PIN 7 | 23 | #define ST7565_MOSI_PIN C6 |
| 23 | #define ST7565_RST_PIN 8 | 24 | #define ST7565_SCLK_PIN C5 |
| 24 | #define ST7565_MOSI_PIN 6 | 25 | #define ST7565_SS_PIN C4 |
| 25 | #define ST7565_SLCK_PIN 5 | ||
| 26 | #define ST7565_SS_PIN 4 | ||
| 27 | |||
| 28 | #define palSetPadModeRaw(portname, bits) ST7565_PORT->PCR[ST7565_##portname##_PIN] = bits | ||
| 29 | 26 | ||
| 30 | #define palSetPadModeNamed(portname, portmode) palSetPadMode(ST7565_GPIOPORT, ST7565_##portname##_PIN, portmode) | ||
| 31 | |||
| 32 | #define ST7565_SPI_MODE PORTx_PCRn_DSE | PORTx_PCRn_MUX(2) | ||
| 33 | // DSPI Clock and Transfer Attributes | 27 | // DSPI Clock and Transfer Attributes |
| 34 | // Frame Size: 8 bits | 28 | // Frame Size: 8 bits |
| 35 | // MSB First | 29 | // MSB First |
| @@ -38,9 +32,9 @@ static const SPIConfig spi1config = { | |||
| 38 | // Operation complete callback or @p NULL. | 32 | // Operation complete callback or @p NULL. |
| 39 | .end_cb = NULL, | 33 | .end_cb = NULL, |
| 40 | // The chip select line port - when not using pcs. | 34 | // The chip select line port - when not using pcs. |
| 41 | .ssport = ST7565_GPIOPORT, | 35 | .ssport = PAL_PORT(ST7565_SS_PIN), |
| 42 | // brief The chip select line pad number - when not using pcs. | 36 | // brief The chip select line pad number - when not using pcs. |
| 43 | .sspad = ST7565_SS_PIN, | 37 | .sspad = PAL_PAD(ST7565_SS_PIN), |
| 44 | // SPI initialization data. | 38 | // SPI initialization data. |
| 45 | .tar0 = SPIx_CTARn_FMSZ(7) // Frame size = 8 bytes | 39 | .tar0 = SPIx_CTARn_FMSZ(7) // Frame size = 8 bytes |
| 46 | | SPIx_CTARn_ASC(1) // After SCK Delay Scaler (min 50 ns) = 55.56ns | 40 | | SPIx_CTARn_ASC(1) // After SCK Delay Scaler (min 50 ns) = 55.56ns |
| @@ -66,13 +60,14 @@ static GFXINLINE void release_bus(GDisplay *g) { | |||
| 66 | 60 | ||
| 67 | static GFXINLINE void init_board(GDisplay *g) { | 61 | static GFXINLINE void init_board(GDisplay *g) { |
| 68 | (void)g; | 62 | (void)g; |
| 69 | palSetPadModeNamed(A0, PAL_MODE_OUTPUT_PUSHPULL); | 63 | setPinOutput(ST7565_A0_PIN); |
| 70 | palSetPad(ST7565_GPIOPORT, ST7565_A0_PIN); | 64 | writePinHigh(ST7565_A0_PIN); |
| 71 | palSetPadModeNamed(RST, PAL_MODE_OUTPUT_PUSHPULL); | 65 | setPinOutput(ST7565_RST_PIN); |
| 72 | palSetPad(ST7565_GPIOPORT, ST7565_RST_PIN); | 66 | writePinHigh(ST7565_RST_PIN); |
| 73 | palSetPadModeRaw(MOSI, ST7565_SPI_MODE); | 67 | setPinOutput(ST7565_SS_PIN); |
| 74 | palSetPadModeRaw(SLCK, ST7565_SPI_MODE); | 68 | |
| 75 | palSetPadModeNamed(SS, PAL_MODE_OUTPUT_PUSHPULL); | 69 | palSetPadMode(PAL_PORT(ST7565_MOSI_PIN), PAL_PAD(ST7565_MOSI_PIN), PAL_MODE_ALTERNATIVE_2); |
| 70 | palSetPadMode(PAL_PORT(ST7565_SCLK_PIN), PAL_PAD(ST7565_SCLK_PIN), PAL_MODE_ALTERNATIVE_2); | ||
| 76 | 71 | ||
| 77 | spiInit(); | 72 | spiInit(); |
| 78 | spiStart(&SPID1, &spi1config); | 73 | spiStart(&SPID1, &spi1config); |
| @@ -83,19 +78,18 @@ static GFXINLINE void post_init_board(GDisplay *g) { (void)g; } | |||
| 83 | 78 | ||
| 84 | static GFXINLINE void setpin_reset(GDisplay *g, bool_t state) { | 79 | static GFXINLINE void setpin_reset(GDisplay *g, bool_t state) { |
| 85 | (void)g; | 80 | (void)g; |
| 86 | if (state) { | 81 | writePin(ST7565_RST_PIN, !state); |
| 87 | palClearPad(ST7565_GPIOPORT, ST7565_RST_PIN); | ||
| 88 | } else { | ||
| 89 | palSetPad(ST7565_GPIOPORT, ST7565_RST_PIN); | ||
| 90 | } | ||
| 91 | } | 82 | } |
| 92 | 83 | ||
| 93 | static GFXINLINE void enter_data_mode(GDisplay *g) { palSetPad(ST7565_GPIOPORT, ST7565_A0_PIN); } | 84 | static GFXINLINE void write_cmd(GDisplay *g, gU8 cmd) { |
| 94 | 85 | (void)g; | |
| 95 | static GFXINLINE void enter_cmd_mode(GDisplay *g) { palClearPad(ST7565_GPIOPORT, ST7565_A0_PIN); } | 86 | writePinLow(ST7565_A0_PIN); |
| 87 | spiSend(&SPID1, 1, &cmd); | ||
| 88 | } | ||
| 96 | 89 | ||
| 97 | static GFXINLINE void write_data(GDisplay *g, uint8_t *data, uint16_t length) { | 90 | static GFXINLINE void write_data(GDisplay *g, gU8 *data, gU16 length) { |
| 98 | (void)g; | 91 | (void)g; |
| 92 | writePinHigh(ST7565_A0_PIN); | ||
| 99 | spiSend(&SPID1, length, data); | 93 | spiSend(&SPID1, length, data); |
| 100 | } | 94 | } |
| 101 | 95 | ||
diff --git a/drivers/ugfx/gdisp/st7565/driver.mk b/drivers/ugfx/gdisp/st7565/driver.mk index 31fc8f1c7..799a986b0 100644 --- a/drivers/ugfx/gdisp/st7565/driver.mk +++ b/drivers/ugfx/gdisp/st7565/driver.mk | |||
| @@ -1,3 +1,3 @@ | |||
| 1 | GFXINC += drivers/ugfx/gdisp/st7565 | 1 | GFXINC += drivers/ugfx/gdisp/st7565 |
| 2 | GFXSRC += drivers/ugfx/gdisp/st7565/gdisp_lld_ST7565.c | 2 | GFXSRC += drivers/ugfx/gdisp/st7565/gdisp_lld_ST7565.c |
| 3 | GDISP_DRIVER_LIST += GDISPVMT_ST7565_QMK \ No newline at end of file | 3 | GDISP_DRIVER_LIST += GDISPVMT_ST7565_QMK |
diff --git a/drivers/ugfx/gdisp/st7565/gdisp_lld_ST7565.c b/drivers/ugfx/gdisp/st7565/gdisp_lld_ST7565.c index fde320981..f586f97e3 100644 --- a/drivers/ugfx/gdisp/st7565/gdisp_lld_ST7565.c +++ b/drivers/ugfx/gdisp/st7565/gdisp_lld_ST7565.c | |||
| @@ -49,31 +49,15 @@ | |||
| 49 | # define ST7565_COM_SCAN ST7565_COM_SCAN_INC | 49 | # define ST7565_COM_SCAN ST7565_COM_SCAN_INC |
| 50 | # endif | 50 | # endif |
| 51 | # ifndef ST7565_PAGE_ORDER | 51 | # ifndef ST7565_PAGE_ORDER |
| 52 | # define ST7565_PAGE_ORDER 0, 1, 2, 3 | 52 | # define ST7565_PAGE_ORDER 0, 1, 2, 3, 4, 5, 6, 7 |
| 53 | # endif | 53 | # endif |
| 54 | 54 | ||
| 55 | /*===========================================================================*/ | 55 | /*===========================================================================*/ |
| 56 | /* Driver local functions. */ | 56 | /* Driver local functions. */ |
| 57 | /*===========================================================================*/ | 57 | /*===========================================================================*/ |
| 58 | 58 | ||
| 59 | typedef struct { | ||
| 60 | bool_t buffer2; | ||
| 61 | uint8_t data_pos; | ||
| 62 | uint8_t data[16]; | ||
| 63 | uint8_t ram[GDISP_SCREEN_HEIGHT * GDISP_SCREEN_WIDTH / 8]; | ||
| 64 | } PrivData; | ||
| 65 | |||
| 66 | // Some common routines and macros | 59 | // Some common routines and macros |
| 67 | # define PRIV(g) ((PrivData *)g->priv) | 60 | # define RAM(g) ((gU8 *)g->priv) |
| 68 | # define RAM(g) (PRIV(g)->ram) | ||
| 69 | |||
| 70 | static GFXINLINE void write_cmd(GDisplay *g, uint8_t cmd) { PRIV(g)->data[PRIV(g)->data_pos++] = cmd; } | ||
| 71 | |||
| 72 | static GFXINLINE void flush_cmd(GDisplay *g) { | ||
| 73 | write_data(g, PRIV(g)->data, PRIV(g)->data_pos); | ||
| 74 | PRIV(g)->data_pos = 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | # define write_cmd2(g, cmd1, cmd2) \ | 61 | # define write_cmd2(g, cmd1, cmd2) \ |
| 78 | { \ | 62 | { \ |
| 79 | write_cmd(g, cmd1); \ | 63 | write_cmd(g, cmd1); \ |
| @@ -106,9 +90,10 @@ static GFXINLINE void flush_cmd(GDisplay *g) { | |||
| 106 | 90 | ||
| 107 | LLDSPEC bool_t gdisp_lld_init(GDisplay *g) { | 91 | LLDSPEC bool_t gdisp_lld_init(GDisplay *g) { |
| 108 | // The private area is the display surface. | 92 | // The private area is the display surface. |
| 109 | g->priv = gfxAlloc(sizeof(PrivData)); | 93 | g->priv = gfxAlloc(GDISP_SCREEN_HEIGHT * GDISP_SCREEN_WIDTH / 8); |
| 110 | PRIV(g)->buffer2 = false; | 94 | if (!g->priv) { |
| 111 | PRIV(g)->data_pos = 0; | 95 | return gFalse; |
| 96 | } | ||
| 112 | 97 | ||
| 113 | // Initialise the board interface | 98 | // Initialise the board interface |
| 114 | init_board(g); | 99 | init_board(g); |
| @@ -119,25 +104,33 @@ LLDSPEC bool_t gdisp_lld_init(GDisplay *g) { | |||
| 119 | setpin_reset(g, FALSE); | 104 | setpin_reset(g, FALSE); |
| 120 | gfxSleepMilliseconds(20); | 105 | gfxSleepMilliseconds(20); |
| 121 | acquire_bus(g); | 106 | acquire_bus(g); |
| 122 | enter_cmd_mode(g); | ||
| 123 | 107 | ||
| 124 | write_cmd(g, ST7565_RESET); | ||
| 125 | write_cmd(g, ST7565_LCD_BIAS); | 108 | write_cmd(g, ST7565_LCD_BIAS); |
| 126 | write_cmd(g, ST7565_ADC); | 109 | write_cmd(g, ST7565_ADC); |
| 127 | write_cmd(g, ST7565_COM_SCAN); | 110 | write_cmd(g, ST7565_COM_SCAN); |
| 128 | 111 | ||
| 112 | write_cmd(g, ST7565_START_LINE | 0); | ||
| 113 | |||
| 114 | write_cmd2(g, ST7565_CONTRAST, GDISP_INITIAL_CONTRAST * 64 / 101); | ||
| 129 | write_cmd(g, ST7565_RESISTOR_RATIO | 0x1); | 115 | write_cmd(g, ST7565_RESISTOR_RATIO | 0x1); |
| 130 | write_cmd2(g, ST7565_CONTRAST, GDISP_INITIAL_CONTRAST); | ||
| 131 | 116 | ||
| 132 | // turn on internal power supply (VC=1, VR=1, VF=1) | 117 | // turn on voltage converter (VC=1, VR=0, VF=0) |
| 118 | write_cmd(g, ST7565_POWER_CONTROL | 0x04); | ||
| 119 | delay_ms(50); | ||
| 120 | |||
| 121 | // turn on voltage regulator (VC=1, VR=1, VF=0) | ||
| 122 | write_cmd(g, ST7565_POWER_CONTROL | 0x06); | ||
| 123 | delay_ms(50); | ||
| 124 | |||
| 125 | // turn on voltage follower (VC=1, VR=1, VF=1) | ||
| 133 | write_cmd(g, ST7565_POWER_CONTROL | 0x07); | 126 | write_cmd(g, ST7565_POWER_CONTROL | 0x07); |
| 127 | delay_ms(50); | ||
| 134 | 128 | ||
| 135 | write_cmd(g, ST7565_INVERT_DISPLAY); | 129 | write_cmd(g, ST7565_DISPLAY_ON); |
| 136 | write_cmd(g, ST7565_ALLON_NORMAL); | 130 | write_cmd(g, ST7565_ALLON_NORMAL); |
| 131 | write_cmd(g, ST7565_INVERT_DISPLAY); // Disable Inversion of display. | ||
| 137 | 132 | ||
| 138 | write_cmd(g, ST7565_START_LINE | 0); | ||
| 139 | write_cmd(g, ST7565_RMW); | 133 | write_cmd(g, ST7565_RMW); |
| 140 | flush_cmd(g); | ||
| 141 | 134 | ||
| 142 | // Finish Init | 135 | // Finish Init |
| 143 | post_init_board(g); | 136 | post_init_board(g); |
| @@ -163,22 +156,14 @@ LLDSPEC void gdisp_lld_flush(GDisplay *g) { | |||
| 163 | if (!(g->flags & GDISP_FLG_NEEDFLUSH)) return; | 156 | if (!(g->flags & GDISP_FLG_NEEDFLUSH)) return; |
| 164 | 157 | ||
| 165 | acquire_bus(g); | 158 | acquire_bus(g); |
| 166 | enter_cmd_mode(g); | 159 | gU8 pagemap[] = {ST7565_PAGE_ORDER}; |
| 167 | unsigned dstOffset = (PRIV(g)->buffer2 ? 4 : 0); | 160 | for (p = 0; p < sizeof(pagemap); p++) { |
| 168 | for (p = 0; p < 4; p++) { | 161 | write_cmd(g, ST7565_PAGE | pagemap[p]); |
| 169 | write_cmd(g, ST7565_PAGE | (p + dstOffset)); | ||
| 170 | write_cmd(g, ST7565_COLUMN_MSB | 0); | 162 | write_cmd(g, ST7565_COLUMN_MSB | 0); |
| 171 | write_cmd(g, ST7565_COLUMN_LSB | 0); | 163 | write_cmd(g, ST7565_COLUMN_LSB | 0); |
| 172 | write_cmd(g, ST7565_RMW); | 164 | write_cmd(g, ST7565_RMW); |
| 173 | flush_cmd(g); | ||
| 174 | enter_data_mode(g); | ||
| 175 | write_data(g, RAM(g) + (p * GDISP_SCREEN_WIDTH), GDISP_SCREEN_WIDTH); | 165 | write_data(g, RAM(g) + (p * GDISP_SCREEN_WIDTH), GDISP_SCREEN_WIDTH); |
| 176 | enter_cmd_mode(g); | ||
| 177 | } | 166 | } |
| 178 | unsigned line = (PRIV(g)->buffer2 ? 32 : 0); | ||
| 179 | write_cmd(g, ST7565_START_LINE | line); | ||
| 180 | flush_cmd(g); | ||
| 181 | PRIV(g)->buffer2 = !PRIV(g)->buffer2; | ||
| 182 | release_bus(g); | 167 | release_bus(g); |
| 183 | 168 | ||
| 184 | g->flags &= ~GDISP_FLG_NEEDFLUSH; | 169 | g->flags &= ~GDISP_FLG_NEEDFLUSH; |
| @@ -243,6 +228,7 @@ LLDSPEC color_t gdisp_lld_get_pixel_color(GDisplay *g) { | |||
| 243 | } | 228 | } |
| 244 | # endif | 229 | # endif |
| 245 | 230 | ||
| 231 | # if GDISP_HARDWARE_BITFILLS | ||
| 246 | LLDSPEC void gdisp_lld_blit_area(GDisplay *g) { | 232 | LLDSPEC void gdisp_lld_blit_area(GDisplay *g) { |
| 247 | uint8_t *buffer = (uint8_t *)g->p.ptr; | 233 | uint8_t *buffer = (uint8_t *)g->p.ptr; |
| 248 | int linelength = g->p.cx; | 234 | int linelength = g->p.cx; |
| @@ -268,6 +254,7 @@ LLDSPEC void gdisp_lld_blit_area(GDisplay *g) { | |||
| 268 | } | 254 | } |
| 269 | g->flags |= GDISP_FLG_NEEDFLUSH; | 255 | g->flags |= GDISP_FLG_NEEDFLUSH; |
| 270 | } | 256 | } |
| 257 | # endif | ||
| 271 | 258 | ||
| 272 | # if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL | 259 | # if GDISP_NEED_CONTROL && GDISP_HARDWARE_CONTROL |
| 273 | LLDSPEC void gdisp_lld_control(GDisplay *g) { | 260 | LLDSPEC void gdisp_lld_control(GDisplay *g) { |
| @@ -279,16 +266,12 @@ LLDSPEC void gdisp_lld_control(GDisplay *g) { | |||
| 279 | case powerSleep: | 266 | case powerSleep: |
| 280 | case powerDeepSleep: | 267 | case powerDeepSleep: |
| 281 | acquire_bus(g); | 268 | acquire_bus(g); |
| 282 | enter_cmd_mode(g); | ||
| 283 | write_cmd(g, ST7565_DISPLAY_OFF); | 269 | write_cmd(g, ST7565_DISPLAY_OFF); |
| 284 | flush_cmd(g); | ||
| 285 | release_bus(g); | 270 | release_bus(g); |
| 286 | break; | 271 | break; |
| 287 | case powerOn: | 272 | case powerOn: |
| 288 | acquire_bus(g); | 273 | acquire_bus(g); |
| 289 | enter_cmd_mode(g); | ||
| 290 | write_cmd(g, ST7565_DISPLAY_ON); | 274 | write_cmd(g, ST7565_DISPLAY_ON); |
| 291 | flush_cmd(g); | ||
| 292 | release_bus(g); | 275 | release_bus(g); |
| 293 | break; | 276 | break; |
| 294 | default: | 277 | default: |
| @@ -318,12 +301,11 @@ LLDSPEC void gdisp_lld_control(GDisplay *g) { | |||
| 318 | return; | 301 | return; |
| 319 | 302 | ||
| 320 | case GDISP_CONTROL_CONTRAST: | 303 | case GDISP_CONTROL_CONTRAST: |
| 321 | g->g.Contrast = (unsigned)g->p.ptr & 63; | 304 | if ((unsigned)g->p.ptr > 100) g->p.ptr = (void *)100; |
| 322 | acquire_bus(g); | 305 | acquire_bus(g); |
| 323 | enter_cmd_mode(g); | 306 | write_cmd2(g, ST7565_CONTRAST, ((((unsigned)g->p.ptr) << 6) / 101) & 0x3F); |
| 324 | write_cmd2(g, ST7565_CONTRAST, g->g.Contrast); | ||
| 325 | flush_cmd(g); | ||
| 326 | release_bus(g); | 307 | release_bus(g); |
| 308 | g->g.Contrast = (unsigned)g->p.ptr; | ||
| 327 | return; | 309 | return; |
| 328 | } | 310 | } |
| 329 | } | 311 | } |
