1 /*
2 * Copyright (c) 2015 Gurjant Kalsi <me@gurjantkalsi.com>
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8
9 #include <target/m4display.h>
10
11 #include <stdio.h>
12 #include <lk/trace.h>
13 #include <lk/err.h>
14
15 #include <stm32f4xx.h>
16 #include <stm32f4xx_spi.h>
17 #include <stm32f4xx_gpio.h>
18 #include <stm32f4xx_rcc.h>
19
20 #include <dev/display.h>
21 #include <dev/gpio.h>
22 #include <platform/gpio.h>
23 #include <assert.h>
24
25 #define LOCAL_TRACE 0
26
27 #define CMD_DISPLAY_NULL 0x00
28 #define CMD_DISPLAY_SET_PARAM 0x01
29 #define CMD_DISPLAY_OFF 0x02
30 #define CMD_DISPLAY_ON 0x03
31 #define CMD_DISPLAY_DRAW_SCENE 0x04
32 #define CMD_DISPLAY_FRAME_BEGIN 0x05
33
34 #define SCENE_BLACK 0x00
35 #define SCENE_SPLASH 0x01
36 #define SCENE_UPDATE 0x02
37 #define SCENE_ERROR 0x03
38
39 #define M4DISPLAY_WIDTH 180
40 #define M4DISPLAY_HEIGHT 180
41
42 static uint8_t framebuffer[M4DISPLAY_HEIGHT][M4DISPLAY_WIDTH];
43
44 static const char programming_header[] = " Lattice\0iCEcube2 2014.08.26723\0Part: iCE40LP1K-CM36\0Date: Jan 30 2015 15:11:";
45
chip_select(bool val)46 static void chip_select(bool val) {
47 if (val) {
48 gpio_set(GPIO(GPIO_PORT_G, 8), true);
49 } else {
50 gpio_set(GPIO(GPIO_PORT_G, 8), false);
51 }
52 }
53
reset(bool val)54 static void reset(bool val) {
55 if (val) {
56 gpio_set(GPIO(GPIO_PORT_G, 15), true);
57 } else {
58 gpio_set(GPIO(GPIO_PORT_G, 15), false);
59 }
60 }
61
setup_pins(void)62 static void setup_pins(void) {
63
64 GPIO_InitTypeDef GPIO_InitStruct;
65
66 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
67
68 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
69 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
70 GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
71 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
72 GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
73 GPIO_Init(GPIOA, &GPIO_InitStruct);
74
75 // connect SPI6 pins to SPI alternate function
76 GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI6);
77 GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI6);
78 GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI6);
79
80 // enable clock for used IO pins
81 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
82
83 /* Configure the chip select pin
84 in this case we will use PE7 */
85 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
86 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
87 GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
88 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
89 GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
90 GPIO_Init(GPIOG, &GPIO_InitStruct);
91
92 GPIOE->BSRR |= GPIO_Pin_7; // set PE7 high
93
94 // Setup display CS Pin
95 gpio_config(GPIO(GPIO_PORT_G, 8), GPIO_OUTPUT);
96
97 // Setup display reset pin
98 gpio_config(GPIO(GPIO_PORT_G, 15), GPIO_OUTPUT);
99
100 // enable peripheral clock
101 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI6, ENABLE);
102 }
103
init_display(void)104 void init_display(void) {
105 // Setting up pins.
106 setup_pins();
107
108 SPI_InitTypeDef init_struct;
109
110 init_struct.SPI_Direction = SPI_Direction_1Line_Tx;
111 init_struct.SPI_Mode = SPI_Mode_Master;
112 init_struct.SPI_DataSize = SPI_DataSize_8b;
113 init_struct.SPI_CPOL = SPI_CPOL_Low;
114 init_struct.SPI_CPHA = SPI_CPHA_1Edge;
115 init_struct.SPI_NSS = SPI_NSS_Hard;
116 init_struct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
117 init_struct.SPI_FirstBit = SPI_FirstBit_MSB;
118
119 SPI_Init(SPI6, &init_struct);
120
121 SPI_Cmd(SPI6, ENABLE); // enable SPI6
122
123 chip_select(true);
124 reset(true);
125 chip_select(false);
126
127 const uint8_t draw_splash_cmds[] = { 0x04, 0x01 };
128
129 for (size_t i = 0; i < countof(draw_splash_cmds); i++) {
130 SPI_I2S_SendData(SPI6, draw_splash_cmds[i]);
131 }
132
133 chip_select(true);
134 chip_select(false);
135
136 uint8_t enable_display_cmds[] = { 0x03 };
137
138 for (size_t i = 0; i < countof(enable_display_cmds); i++) {
139 SPI_I2S_SendData(SPI6, enable_display_cmds[i]);
140 }
141
142 chip_select(true);
143 reset(false);
144 chip_select(false);
145 reset(true);
146
147
148 for (size_t i = 0; i < countof(programming_header); i++) {
149 SPI_I2S_SendData(SPI6, programming_header[i]);
150 }
151
152 chip_select(true);
153 }
154
s4lcd_flush(uint starty,uint endy)155 static void s4lcd_flush(uint starty, uint endy) {
156
157 chip_select(false);
158
159 uint8_t frame_begin_cmd[] = { 0x05 };
160
161 for (size_t i = 0; i < countof(frame_begin_cmd); i++) {
162 SPI_I2S_SendData(SPI6, frame_begin_cmd[i]);
163 }
164
165 size_t msb_idx = M4DISPLAY_WIDTH / 2;
166
167 uint8_t scramble_buf[M4DISPLAY_WIDTH];
168 for (int i = 0; i < M4DISPLAY_HEIGHT; i++) {
169 uint8_t *lsb = scramble_buf;
170 uint8_t *msb = &(scramble_buf[msb_idx]);
171 for (int j = 0; j < M4DISPLAY_WIDTH; j += 2) {
172
173 *msb++ = ((framebuffer[i][j] & 0b01010100) | ((framebuffer[i][j+1] & 0b01010100) << 1)) >> 2;
174 *lsb++ = ((framebuffer[i][j] & 0b10101000) | ((framebuffer[i][j+1] & 0b10101000) >> 1)) >> 2;
175 }
176
177 for (int j = 0; j < M4DISPLAY_WIDTH; j++) {
178 SPI_I2S_SendData(SPI6, scramble_buf[j]);
179 }
180 }
181
182 chip_select(true);
183 }
184
display_get_framebuffer(struct display_framebuffer * fb)185 status_t display_get_framebuffer(struct display_framebuffer *fb) {
186 DEBUG_ASSERT(fb);
187 LTRACEF("display_get_framebuffer %p\n", fb);
188
189 fb->image.pixels = (void *)framebuffer;
190 fb->image.format = IMAGE_FORMAT_RGB_2220;
191 fb->image.width = M4DISPLAY_WIDTH;
192 fb->image.height = M4DISPLAY_HEIGHT;
193 fb->image.stride = M4DISPLAY_WIDTH;
194 fb->image.rowbytes = M4DISPLAY_WIDTH;
195 fb->flush = s4lcd_flush;
196 fb->format = DISPLAY_FORMAT_UNKNOWN; //TODO
197
198 return NO_ERROR;
199 }
200
display_get_info(struct display_info * info)201 status_t display_get_info(struct display_info *info) {
202 DEBUG_ASSERT(info);
203 LTRACEF("display_info %p\n", info);
204
205 info->format = DISPLAY_FORMAT_UNKNOWN; //TODO
206 info->width = M4DISPLAY_WIDTH;
207 info->height = M4DISPLAY_HEIGHT;
208
209 return NO_ERROR;
210 }
211
display_present(struct display_image * image,uint starty,uint endy)212 status_t display_present(struct display_image *image, uint starty, uint endy) {
213 TRACEF("display_present - not implemented");
214 DEBUG_ASSERT(false);
215 return NO_ERROR;
216 }
217