rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2015 Gurjant Kalsi <me@gurjantkalsi.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include <target/m4display.h> |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <trace.h> |
| 28 | #include <err.h> |
| 29 | |
| 30 | #include <stm32f4xx.h> |
| 31 | #include <stm32f4xx_spi.h> |
| 32 | #include <stm32f4xx_gpio.h> |
| 33 | #include <stm32f4xx_rcc.h> |
| 34 | |
| 35 | #include <dev/display.h> |
| 36 | #include <dev/gpio.h> |
| 37 | #include <lib/gfx.h> |
| 38 | #include <platform/gpio.h> |
| 39 | |
| 40 | #define LOCAL_TRACE 0 |
| 41 | |
| 42 | #define CMD_DISPLAY_NULL 0x00 |
| 43 | #define CMD_DISPLAY_SET_PARAM 0x01 |
| 44 | #define CMD_DISPLAY_OFF 0x02 |
| 45 | #define CMD_DISPLAY_ON 0x03 |
| 46 | #define CMD_DISPLAY_DRAW_SCENE 0x04 |
| 47 | #define CMD_DISPLAY_FRAME_BEGIN 0x05 |
| 48 | |
| 49 | #define SCENE_BLACK 0x00 |
| 50 | #define SCENE_SPLASH 0x01 |
| 51 | #define SCENE_UPDATE 0x02 |
| 52 | #define SCENE_ERROR 0x03 |
| 53 | |
| 54 | #define M4DISPLAY_WIDTH 180 |
| 55 | #define M4DISPLAY_HEIGHT 180 |
| 56 | |
| 57 | static uint8_t framebuffer[M4DISPLAY_HEIGHT][M4DISPLAY_WIDTH]; |
| 58 | |
| 59 | static const char programming_header[] = " Lattice\0iCEcube2 2014.08.26723\0Part: iCE40LP1K-CM36\0Date: Jan 30 2015 15:11:"; |
| 60 | |
| 61 | static void chip_select(bool val) |
| 62 | { |
| 63 | if (val) { |
| 64 | gpio_set(GPIO(GPIO_PORT_G, 8), true); |
| 65 | } else { |
| 66 | gpio_set(GPIO(GPIO_PORT_G, 8), false); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | static void reset(bool val) |
| 71 | { |
| 72 | if (val) { |
| 73 | gpio_set(GPIO(GPIO_PORT_G, 15), true); |
| 74 | } else { |
| 75 | gpio_set(GPIO(GPIO_PORT_G, 15), false); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static void setup_pins(void) |
| 80 | { |
| 81 | |
| 82 | GPIO_InitTypeDef GPIO_InitStruct; |
| 83 | |
| 84 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE); |
| 85 | |
| 86 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5; |
| 87 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; |
| 88 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; |
| 89 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; |
| 90 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; |
| 91 | GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 92 | |
| 93 | // connect SPI6 pins to SPI alternate function |
| 94 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI6); |
| 95 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI6); |
| 96 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI6); |
| 97 | |
| 98 | // enable clock for used IO pins |
| 99 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE); |
| 100 | |
| 101 | /* Configure the chip select pin |
| 102 | in this case we will use PE7 */ |
| 103 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8; |
| 104 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; |
| 105 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; |
| 106 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; |
| 107 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; |
| 108 | GPIO_Init(GPIOG, &GPIO_InitStruct); |
| 109 | |
| 110 | GPIOE->BSRRL |= GPIO_Pin_7; // set PE7 high |
| 111 | |
| 112 | // Setup display CS Pin |
| 113 | gpio_config(GPIO(GPIO_PORT_G, 8), GPIO_OUTPUT); |
| 114 | |
| 115 | // Setup display reset pin |
| 116 | gpio_config(GPIO(GPIO_PORT_G, 15), GPIO_OUTPUT); |
| 117 | |
| 118 | // enable peripheral clock |
| 119 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI6, ENABLE); |
| 120 | } |
| 121 | |
| 122 | void init_display(void) |
| 123 | { |
| 124 | // Setting up pins. |
| 125 | setup_pins(); |
| 126 | |
| 127 | SPI_InitTypeDef init_struct; |
| 128 | |
| 129 | init_struct.SPI_Direction = SPI_Direction_1Line_Tx; |
| 130 | init_struct.SPI_Mode = SPI_Mode_Master; |
| 131 | init_struct.SPI_DataSize = SPI_DataSize_8b; |
| 132 | init_struct.SPI_CPOL = SPI_CPOL_Low; |
| 133 | init_struct.SPI_CPHA = SPI_CPHA_1Edge; |
| 134 | init_struct.SPI_NSS = SPI_NSS_Hard; |
| 135 | init_struct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; |
| 136 | init_struct.SPI_FirstBit = SPI_FirstBit_MSB; |
| 137 | |
| 138 | SPI_Init(SPI6, &init_struct); |
| 139 | |
| 140 | SPI_Cmd(SPI6, ENABLE); // enable SPI6 |
| 141 | |
| 142 | chip_select(true); |
| 143 | reset(true); |
| 144 | chip_select(false); |
| 145 | |
| 146 | const uint8_t draw_splash_cmds[] = { 0x04, 0x01 }; |
| 147 | |
| 148 | for (size_t i = 0; i < countof(draw_splash_cmds); i++) { |
| 149 | SPI_I2S_SendData(SPI6, draw_splash_cmds[i]); |
| 150 | } |
| 151 | |
| 152 | chip_select(true); |
| 153 | chip_select(false); |
| 154 | |
| 155 | uint8_t enable_display_cmds[] = { 0x03 }; |
| 156 | |
| 157 | for (size_t i = 0; i < countof(enable_display_cmds); i++) { |
| 158 | SPI_I2S_SendData(SPI6, enable_display_cmds[i]); |
| 159 | } |
| 160 | |
| 161 | chip_select(true); |
| 162 | reset(false); |
| 163 | chip_select(false); |
| 164 | reset(true); |
| 165 | |
| 166 | |
| 167 | for (size_t i = 0; i < countof(programming_header); i++) { |
| 168 | SPI_I2S_SendData(SPI6, programming_header[i]); |
| 169 | } |
| 170 | |
| 171 | chip_select(true); |
| 172 | } |
| 173 | |
| 174 | static void s4lcd_flush(uint starty, uint endy) |
| 175 | { |
| 176 | |
| 177 | chip_select(false); |
| 178 | |
| 179 | uint8_t frame_begin_cmd[] = { 0x05 }; |
| 180 | |
| 181 | for (size_t i = 0; i < countof(frame_begin_cmd); i++) { |
| 182 | SPI_I2S_SendData(SPI6, frame_begin_cmd[i]); |
| 183 | } |
| 184 | |
| 185 | size_t msb_idx = M4DISPLAY_WIDTH / 2; |
| 186 | |
| 187 | uint8_t scramble_buf[M4DISPLAY_WIDTH]; |
| 188 | for (int i = 0; i < M4DISPLAY_HEIGHT; i++) { |
| 189 | uint8_t *lsb = scramble_buf; |
| 190 | uint8_t *msb = &(scramble_buf[msb_idx]); |
| 191 | for (int j = 0; j < M4DISPLAY_WIDTH; j += 2) { |
| 192 | |
| 193 | *msb++ = ((framebuffer[i][j] & 0b01010100) | ((framebuffer[i][j+1] & 0b01010100) << 1)) >> 2; |
| 194 | *lsb++ = ((framebuffer[i][j] & 0b10101000) | ((framebuffer[i][j+1] & 0b10101000) >> 1)) >> 2; |
| 195 | } |
| 196 | |
| 197 | for (int j = 0; j < M4DISPLAY_WIDTH; j++) { |
| 198 | SPI_I2S_SendData(SPI6, scramble_buf[j]); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | chip_select(true); |
| 203 | } |
| 204 | |
| 205 | status_t display_get_info(struct display_info *info) |
| 206 | { |
| 207 | LTRACEF("display_info %p\n", info); |
| 208 | |
| 209 | info->framebuffer = (void *)framebuffer; |
| 210 | info->format = GFX_FORMAT_RGB_2220; |
| 211 | info->width = M4DISPLAY_WIDTH; |
| 212 | info->height = M4DISPLAY_HEIGHT; |
| 213 | info->stride = M4DISPLAY_WIDTH; |
| 214 | info->flush = s4lcd_flush; |
| 215 | |
| 216 | return NO_ERROR; |
| 217 | } |