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