| 1 |
/* |
| 2 |
****************************************************************************************** |
| 3 |
* @file main.c |
| 4 |
* @author GowinSemiconductor |
| 5 |
* @device Gowin_EMPU(GW1NS-4C) |
| 6 |
* @brief Main program body. |
| 7 |
****************************************************************************************** |
| 8 |
*/ |
| 9 |
|
| 10 |
/* Includes ------------------------------------------------------------------*/ |
| 11 |
#include "gw1ns4c.h" |
| 12 |
#include <stdint.h> |
| 13 |
#include <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
#include "image-apicula.h" |
| 18 |
|
| 19 |
void initialize_GPIO(); |
| 20 |
void delay_millis(uint32_t ms); |
| 21 |
|
| 22 |
// ============================ SPI ======================= |
| 23 |
|
| 24 |
// First APB master address |
| 25 |
uint32_t const apb2_periph_base = 0x40002500; |
| 26 |
|
| 27 |
struct APB2_spi_peripheral_status { |
| 28 |
volatile uint32_t status : 1; // not memory - read the bus ;) |
| 29 |
}; |
| 30 |
struct APB2_spi_peripheral_data { |
| 31 |
volatile uint32_t data; // not memory - write the bus ;) |
| 32 |
}; |
| 33 |
struct APB2_spi_peripheral_control { |
| 34 |
volatile uint32_t control : 1; // not memory - write the bus ;) |
| 35 |
}; |
| 36 |
|
| 37 |
struct APB2_spi_peripheral_status *spi_status = (struct APB2_spi_peripheral_status *) apb2_periph_base; |
| 38 |
struct APB2_spi_peripheral_data *spi_data = (struct APB2_spi_peripheral_data *) (apb2_periph_base + 4); |
| 39 |
struct APB2_spi_peripheral_control *spi_control = (struct APB2_spi_peripheral_control *) apb2_periph_base; |
| 40 |
|
| 41 |
uint8_t spi_busy(void) { |
| 42 |
return spi_status->status & 1; |
| 43 |
} |
| 44 |
|
| 45 |
void initialize_spi(uint8_t msb) { |
| 46 |
spi_control->control = msb; |
| 47 |
} |
| 48 |
|
| 49 |
void spi_send_byte(uint8_t byte) { |
| 50 |
while (spi_busy()); |
| 51 |
spi_data->data = byte; |
| 52 |
} |
| 53 |
|
| 54 |
void spi_send_array(uint8_t *buf, uint32_t len) { |
| 55 |
while(len) { |
| 56 |
spi_send_byte(*buf++); |
| 57 |
--len; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// =========================== Display ========================== |
| 62 |
#define BLACK 0x0000 |
| 63 |
#define WHITE 0xffff |
| 64 |
#define RED 0xf800 |
| 65 |
#define GREEN 0x07e0 |
| 66 |
#define BLUE 0x001f |
| 67 |
#define YELLOW 0xffe0 |
| 68 |
|
| 69 |
#define DSP_WIDTH 240 |
| 70 |
#define DSP_HEIGHT 240 |
| 71 |
|
| 72 |
#define DSP_RST0 GPIO_ResetBit(GPIO0, GPIO_Pin_0) |
| 73 |
#define DSP_RST1 GPIO_SetBit(GPIO0, GPIO_Pin_0) |
| 74 |
#define DSP_DC0 GPIO_ResetBit(GPIO0, GPIO_Pin_1) |
| 75 |
#define DSP_DC1 GPIO_SetBit(GPIO0, GPIO_Pin_1) |
| 76 |
void initialize_dsp(void); |
| 77 |
void dsp_send_cmd(uint8_t cmd); |
| 78 |
void dsp_send_data(uint8_t data); |
| 79 |
void dsp_send_array(uint8_t *buf, uint32_t len); |
| 80 |
void dsp_fillcolor(uint16_t color); |
| 81 |
void dsp_set_address_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); |
| 82 |
void dsp_put_point(uint16_t color); |
| 83 |
|
| 84 |
void initialize_timer(); |
| 85 |
void initialize_UART(); |
| 86 |
void delay_millis(uint32_t ms); |
| 87 |
|
| 88 |
int main(void) { |
| 89 |
SystemInit(); |
| 90 |
initialize_timer(); |
| 91 |
initialize_UART(); |
| 92 |
initialize_GPIO(); |
| 93 |
initialize_spi(1); |
| 94 |
|
| 95 |
UART_SendString(UART0, "Apicula Cortex-M3 APB SPI\r\n"); |
| 96 |
|
| 97 |
uint16_t *rowbuf = malloc(image_width * sizeof(uint16_t)); |
| 98 |
|
| 99 |
initialize_dsp(); |
| 100 |
int phase = 0; |
| 101 |
while(1) { |
| 102 |
|
| 103 |
int16_t cur_color; |
| 104 |
int cur_image_idx = 0; |
| 105 |
int16_t len = 0; |
| 106 |
|
| 107 |
dsp_set_address_window(0, 0, image_width - 1, image_height - 1); |
| 108 |
for (int i = 0; i < image_height; ++i) { |
| 109 |
memset(rowbuf, 0, sizeof(rowbuf)); |
| 110 |
for (int j = 0; j < image_width; ++j) { |
| 111 |
if (len == 0) { |
| 112 |
len = image_data[cur_image_idx++]; |
| 113 |
cur_color = image_colors[0x1ff & (len >> 7)]; // color 9 bit, count 7 bit |
| 114 |
len = (len & 0x7f) + 1; |
| 115 |
} |
| 116 |
--len; |
| 117 |
if (phase >= j) { |
| 118 |
rowbuf[(phase - j) % image_width] = cur_color; |
| 119 |
} |
| 120 |
} |
| 121 |
for (uint16_t k = 0; k < image_width; ++k) { |
| 122 |
dsp_put_point(rowbuf[k]); |
| 123 |
} |
| 124 |
} |
| 125 |
phase += 5; |
| 126 |
//delay_millis(5); |
| 127 |
} |
| 128 |
free(rowbuf); |
| 129 |
} |
| 130 |
|
| 131 |
// ============================ Display ======================= |
| 132 |
void dsp_send_cmd(uint8_t cmd) { |
| 133 |
DSP_DC0; |
| 134 |
spi_send_byte(cmd); |
| 135 |
} |
| 136 |
|
| 137 |
void dsp_send_data(uint8_t data) { |
| 138 |
DSP_DC1; |
| 139 |
spi_send_byte(data); |
| 140 |
} |
| 141 |
|
| 142 |
void dsp_send_array(uint8_t *buf, uint32_t len) { |
| 143 |
DSP_DC1; |
| 144 |
spi_send_array(buf, len); |
| 145 |
} |
| 146 |
|
| 147 |
#define DSP_ROTATION 3 |
| 148 |
#define DSP_X_SHIFT 0 |
| 149 |
#define DSP_Y_SHIFT 0 |
| 150 |
|
| 151 |
#define DSP_MADCTRL 0x36 |
| 152 |
#define DSP_MAD_RGB 0x00 |
| 153 |
#define DSP_MAD_BGR 0x08 |
| 154 |
|
| 155 |
#define DSP_SWRESET 0x01 |
| 156 |
#define DSP_SLPIN 0x10 |
| 157 |
#define DSP_SLPOUT 0x11 |
| 158 |
#define DSP_NORON 0x13 |
| 159 |
#define DSP_INVOFF 0x20 |
| 160 |
#define DSP_INVON 0x21 |
| 161 |
#define DSP_DISPOFF 0x28 |
| 162 |
#define DSP_DISPON 0x29 |
| 163 |
#define DSP_CASET 0x2a |
| 164 |
#define DSP_RASET 0x2b |
| 165 |
#define DSP_RAMWR 0x2c |
| 166 |
#define DSP_COLMOD 0x3a |
| 167 |
#define DSP_COLOR16BIT 0x55 |
| 168 |
#define DSP_PORCTRL 0xb2 |
| 169 |
#define DSP_GCTRL 0xb7 |
| 170 |
#define DSP_VCOMS 0xbb |
| 171 |
#define DSP_LCMCTRL 0xc0 |
| 172 |
#define DSP_VDVVRHEN 0xc2 |
| 173 |
#define DSP_VRHS 0xc3 |
| 174 |
#define DSP_VDVS 0xc4 |
| 175 |
#define DSP_FRCTRL2 0xc6 |
| 176 |
#define DSP_PWRCTL1 0xd0 |
| 177 |
#define DSP_PVGAMCTRL 0xe0 |
| 178 |
#define DSP_NVGAMCTRL 0xe1 |
| 179 |
|
| 180 |
void dsp_set_address_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { |
| 181 |
uint16_t x_start = x0 + DSP_X_SHIFT, x_end = x1 + DSP_X_SHIFT; |
| 182 |
uint16_t y_start = y0 + DSP_Y_SHIFT, y_end = y1 + DSP_Y_SHIFT; |
| 183 |
|
| 184 |
dsp_send_cmd(DSP_CASET); |
| 185 |
uint8_t buf[] = {x_start >> 8, x_start & 0xff, x_end >> 8, x_end & 0xff}; |
| 186 |
dsp_send_array(buf, sizeof(buf)); |
| 187 |
|
| 188 |
dsp_send_cmd(DSP_RASET); |
| 189 |
uint8_t buf1[] = {y_start >> 8, y_start & 0xff, y_end >> 8, y_end & 0xff}; |
| 190 |
dsp_send_array(buf1, sizeof(buf1)); |
| 191 |
|
| 192 |
dsp_send_cmd(DSP_RAMWR); |
| 193 |
} |
| 194 |
|
| 195 |
void dsp_put_point(uint16_t color) { |
| 196 |
uint8_t buf[] = {color >> 8, color & 0xff}; |
| 197 |
dsp_send_array(buf, sizeof(buf)); |
| 198 |
} |
| 199 |
|
| 200 |
void dsp_fillcolor(uint16_t color) { |
| 201 |
dsp_set_address_window(0, 0, DSP_WIDTH - 1, DSP_HEIGHT - 1); |
| 202 |
for (uint16_t i = 0; i < DSP_WIDTH; ++i) { |
| 203 |
for (uint16_t j = 0; j < DSP_HEIGHT; ++j) { |
| 204 |
uint8_t buf[] = {color >> 8, color & 0xff}; |
| 205 |
dsp_send_array(buf, sizeof(buf)); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
void initialize_dsp(void) { |
| 211 |
DSP_RST0; |
| 212 |
delay_millis(50); |
| 213 |
DSP_RST1; |
| 214 |
delay_millis(50); |
| 215 |
dsp_send_cmd(DSP_SWRESET); |
| 216 |
delay_millis(100); |
| 217 |
dsp_send_cmd(DSP_MADCTRL); |
| 218 |
dsp_send_data(DSP_MAD_BGR); |
| 219 |
dsp_send_cmd(DSP_COLMOD); |
| 220 |
dsp_send_data(DSP_COLOR16BIT); |
| 221 |
delay_millis(10); |
| 222 |
dsp_send_cmd(DSP_PORCTRL); |
| 223 |
uint8_t buf[] = {0xb, 0xb, 0, 0x33, 0x33}; |
| 224 |
dsp_send_array(buf, sizeof(buf)); |
| 225 |
dsp_send_cmd(DSP_GCTRL); |
| 226 |
dsp_send_data(0x35); |
| 227 |
dsp_send_cmd(DSP_VCOMS); |
| 228 |
dsp_send_data(0x20); |
| 229 |
dsp_send_cmd(DSP_LCMCTRL); |
| 230 |
dsp_send_data(0x2c); |
| 231 |
dsp_send_cmd(DSP_VDVVRHEN); |
| 232 |
dsp_send_data(0x01); |
| 233 |
dsp_send_cmd(DSP_VRHS); |
| 234 |
dsp_send_data(0x0b); |
| 235 |
dsp_send_cmd(DSP_VDVS); |
| 236 |
dsp_send_data(0x20); |
| 237 |
dsp_send_cmd(DSP_FRCTRL2); |
| 238 |
dsp_send_data(0x0f); |
| 239 |
dsp_send_cmd(DSP_PWRCTL1); |
| 240 |
dsp_send_data(0xa4); |
| 241 |
dsp_send_data(0xa1); |
| 242 |
dsp_send_cmd(DSP_PVGAMCTRL); |
| 243 |
uint8_t buf1[] = {0xd0, 0x4, 0xd, 0x11, 0x13, 0x2b, 0x3f, 0x54, 0x4c, 0x18, 0xd, 0xb, 0x1f, 0x23}; |
| 244 |
dsp_send_array(buf1, sizeof(buf1)); |
| 245 |
dsp_send_cmd(DSP_NVGAMCTRL); |
| 246 |
uint8_t buf2[] = {0xd0, 0x4, 0xc, 0x11, 0x13, 0x2c, 0x3f, 0x44, 0x51, 0x2f, 0x1f, 0x1f, 0x20, 0x23}; |
| 247 |
dsp_send_array(buf2, sizeof(buf2)); |
| 248 |
dsp_send_cmd(DSP_INVON); |
| 249 |
dsp_send_cmd(DSP_SLPOUT); |
| 250 |
dsp_send_cmd(DSP_NORON); |
| 251 |
dsp_send_cmd(DSP_DISPON); |
| 252 |
|
| 253 |
delay_millis(50); |
| 254 |
dsp_fillcolor(BLACK); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
// ============================= UART =========================== |
| 260 |
// Initializes UART0 |
| 261 |
void initialize_UART() { |
| 262 |
UART_InitTypeDef uartInitStruct; |
| 263 |
// Enable transmission |
| 264 |
uartInitStruct.UART_Mode.UARTMode_Tx = ENABLE; |
| 265 |
// Disable reception |
| 266 |
uartInitStruct.UART_Mode.UARTMode_Rx = DISABLE; |
| 267 |
// 9600 baud rate typical of Arduinos |
| 268 |
uartInitStruct.UART_BaudRate = 9600; |
| 269 |
// Initialize UART0 using the struct configs |
| 270 |
UART_Init(UART0, &uartInitStruct); |
| 271 |
} |
| 272 |
|
| 273 |
// ============================= GPIO =========================== |
| 274 |
void initialize_GPIO() { |
| 275 |
GPIO_InitTypeDef gpioInitStruct; |
| 276 |
|
| 277 |
// --------- output |
| 278 |
//Select pin0, you can OR pins together to initialize them at the same time |
| 279 |
gpioInitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; |
| 280 |
|
| 281 |
//Set selected pins as output (see GPIOMode_TypeDef in gw1ns4c_gpio.h) |
| 282 |
gpioInitStruct.GPIO_Mode = GPIO_Mode_OUT; |
| 283 |
|
| 284 |
//Disable interrupts on selected pins (see GPIOInt_TypeDef) |
| 285 |
gpioInitStruct.GPIO_Int = GPIO_Int_Disable; |
| 286 |
|
| 287 |
//Initialize the GPIO using the configured init struct |
| 288 |
GPIO_Init(GPIO0, &gpioInitStruct); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
void initialize_timer() { |
| 293 |
TIMER_InitTypeDef timerInitStruct; |
| 294 |
|
| 295 |
timerInitStruct.Reload = 0; |
| 296 |
|
| 297 |
// Disable interrupt requests from timer for now |
| 298 |
timerInitStruct.TIMER_Int = DISABLE; |
| 299 |
|
| 300 |
// Disable timer enabling/clocking from external pins (GPIO) |
| 301 |
timerInitStruct.TIMER_Exti = TIMER_DISABLE; |
| 302 |
|
| 303 |
TIMER_Init(TIMER0, &timerInitStruct); |
| 304 |
TIMER_StopTimer(TIMER0); |
| 305 |
} |
| 306 |
|
| 307 |
#define CYCLES_PER_MILLISEC (SystemCoreClock / 1000) |
| 308 |
void delay_millis(uint32_t ms) { |
| 309 |
TIMER_StopTimer(TIMER0); |
| 310 |
// Reset timer just in case it was modified elsewhere |
| 311 |
TIMER_SetValue(TIMER0, 0); |
| 312 |
TIMER_EnableIRQ(TIMER0); |
| 313 |
|
| 314 |
uint32_t reloadVal = CYCLES_PER_MILLISEC * ms; |
| 315 |
// Timer interrupt will trigger when it reaches the reload value |
| 316 |
TIMER_SetReload(TIMER0, reloadVal); |
| 317 |
|
| 318 |
TIMER_StartTimer(TIMER0); |
| 319 |
// Block execution until timer wastes the calculated amount of cycles |
| 320 |
while (TIMER_GetIRQStatus(TIMER0) != SET) |
| 321 |
; |
| 322 |
|
| 323 |
TIMER_StopTimer(TIMER0); |
| 324 |
TIMER_ClearIRQ(TIMER0); |
| 325 |
TIMER_SetValue(TIMER0, 0); |
| 326 |
} |
| 327 |
|