]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodox/infinity/drivers/gdisp/st7565ergodox/board_ST7565.h
290571ce5b4116544c32f45d065147a09e6639e7
[qmk_firmware.git] / keyboards / ergodox / infinity / drivers / gdisp / st7565ergodox / board_ST7565.h
1 /*
2  * This file is subject to the terms of the GFX License. If a copy of
3  * the license was not distributed with this file, you can obtain one at:
4  *
5  *              http://ugfx.org/license.html
6  */
7
8 #ifndef _GDISP_LLD_BOARD_H
9 #define _GDISP_LLD_BOARD_H
10
11 #include "print.h"
12
13 #define ST7565_LCD_BIAS         ST7565_LCD_BIAS_9 // actually 6
14 #define ST7565_ADC              ST7565_ADC_NORMAL
15 #define ST7565_COM_SCAN         ST7565_COM_SCAN_DEC
16 #define ST7565_PAGE_ORDER       0,1,2,3
17 /*
18  * Custom page order for several LCD boards, e.g. HEM12864-99
19  * #define ST7565_PAGE_ORDER       4,5,6,7,0,1,2,3
20  */
21
22 #define ST7565_GPIOPORT GPIOC
23 #define ST7565_PORT PORTC
24 #define ST7565_A0_PIN 7
25 #define ST7565_RST_PIN 8
26 #define ST7565_MOSI_PIN 6
27 #define ST7565_SLCK_PIN 5
28 #define ST7565_SS_PIN 4
29
30 #define palSetPadModeRaw(portname, bits) \
31     ST7565_PORT->PCR[ST7565_##portname##_PIN] = bits
32
33 #define palSetPadModeNamed(portname, portmode) \
34     palSetPadMode(ST7565_GPIOPORT, ST7565_##portname##_PIN, portmode)
35
36 #define ST7565_SPI_MODE PORTx_PCRn_DSE | PORTx_PCRn_MUX(2)
37 // DSPI Clock and Transfer Attributes
38 // Frame Size: 8 bits
39 // MSB First
40 // CLK Low by default
41 static const SPIConfig spi1config = {
42         NULL,
43         /* HW dependent part.*/
44         ST7565_GPIOPORT,
45     ST7565_SS_PIN,
46     SPIx_CTARn_FMSZ(7)
47     | SPIx_CTARn_ASC(7)
48     | SPIx_CTARn_DT(7)
49     | SPIx_CTARn_CSSCK(7)
50     | SPIx_CTARn_PBR(0)
51     | SPIx_CTARn_BR(7)
52         //SPI_CR1_BR_0
53 };
54
55 static bool_t st7565_is_data_mode = 1;
56
57 static GFXINLINE void init_board(GDisplay *g) {
58     (void) g;
59     palSetPadModeNamed(A0, PAL_MODE_OUTPUT_PUSHPULL);
60     palSetPad(ST7565_GPIOPORT, ST7565_A0_PIN);
61     st7565_is_data_mode = 1;
62     palSetPadModeNamed(RST, PAL_MODE_OUTPUT_PUSHPULL);
63     palSetPad(ST7565_GPIOPORT, ST7565_RST_PIN);
64     palSetPadModeRaw(MOSI, ST7565_SPI_MODE);
65     palSetPadModeRaw(SLCK, ST7565_SPI_MODE);
66     palSetPadModeRaw(SS, ST7565_SPI_MODE);
67
68     spiInit();
69     spiStart(&SPID1, &spi1config);
70     spiSelect(&SPID1);
71 }
72
73 static GFXINLINE void post_init_board(GDisplay *g) {
74         (void) g;
75 }
76
77 static GFXINLINE void setpin_reset(GDisplay *g, bool_t state) {
78     (void) g;
79     if (state) {
80         palClearPad(ST7565_GPIOPORT, ST7565_RST_PIN);
81     }
82     else {
83         palSetPad(ST7565_GPIOPORT, ST7565_RST_PIN);
84     }
85 }
86
87 static GFXINLINE void acquire_bus(GDisplay *g) {
88     (void) g;
89     // Only the LCD is using the SPI bus, so no need to acquire
90     // spiAcquireBus(&SPID1);
91 }
92
93 static GFXINLINE void release_bus(GDisplay *g) {
94     (void) g;
95     // Only the LCD is using the SPI bus, so no need to release
96     //spiReleaseBus(&SPID1);
97 }
98
99 static GFXINLINE void write_cmd(GDisplay *g, uint8_t cmd) {
100         (void) g;
101         if (st7565_is_data_mode) {
102             // The sleeps need to be at lest 10 vs 25 ns respectively
103             // So let's sleep two ticks, one tick might not be enough
104             // if we are at the end of the tick
105             chThdSleep(2);
106         palClearPad(ST7565_GPIOPORT, ST7565_A0_PIN);
107         chThdSleep(2);
108         st7565_is_data_mode = 0;
109         }
110         spiSend(&SPID1, 1, &cmd);
111 }
112
113 static GFXINLINE void write_data(GDisplay *g, uint8_t* data, uint16_t length) {
114         (void) g;
115         if (!st7565_is_data_mode) {
116             // The sleeps need to be at lest 10 vs 25 ns respectively
117             // So let's sleep two ticks, one tick might not be enough
118             // if we are at the end of the tick
119             chThdSleep(2);
120         palSetPad(ST7565_GPIOPORT, ST7565_A0_PIN);
121             chThdSleep(2);
122         st7565_is_data_mode = 1;
123         }
124         spiSend(&SPID1, length, data);
125 }
126
127 #endif /* _GDISP_LLD_BOARD_H */