2026-04-26 16:24:42 +08:00
|
|
|
#include "gd32f4xx.h"
|
|
|
|
|
#include "hardware_init.h"
|
|
|
|
|
#include "led_driver.h"
|
|
|
|
|
#include "uart_driver.h"
|
|
|
|
|
#include "systick.h"
|
|
|
|
|
#include "sdram_manager.h"
|
|
|
|
|
#include "flash_manager.h"
|
|
|
|
|
#include "lcd.h"
|
2026-04-26 20:36:54 +08:00
|
|
|
#include "touch.h"
|
2026-04-26 16:24:42 +08:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
|
|
__attribute__((section(".sdram"))) uint8_t sdram_big_buffer[1024 * 1024];
|
|
|
|
|
|
2026-04-27 11:14:22 +08:00
|
|
|
Led led0(GpioPort::E, 0x08);
|
|
|
|
|
Led led1(GpioPort::D, 0x80);
|
|
|
|
|
Led led2(GpioPort::G, 0x08);
|
|
|
|
|
Led led3(GpioPort::A, 0x20);
|
2026-04-26 16:24:42 +08:00
|
|
|
|
|
|
|
|
static void led_pattern(uint8_t p)
|
|
|
|
|
{
|
2026-04-27 11:14:22 +08:00
|
|
|
led0.off();
|
|
|
|
|
led1.off();
|
|
|
|
|
led2.off();
|
|
|
|
|
led3.off();
|
|
|
|
|
if (p & 0x01) led0.on();
|
|
|
|
|
if (p & 0x02) led1.on();
|
|
|
|
|
if (p & 0x04) led2.on();
|
|
|
|
|
if (p & 0x08) led3.on();
|
2026-04-26 16:24:42 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 11:14:22 +08:00
|
|
|
USART uart0(UartPort::_0, 9600);
|
2026-04-26 21:14:00 +08:00
|
|
|
|
2026-04-26 16:24:42 +08:00
|
|
|
int main(void)
|
|
|
|
|
{
|
2026-04-27 11:14:22 +08:00
|
|
|
led0.init();
|
|
|
|
|
led1.init();
|
|
|
|
|
led2.init();
|
|
|
|
|
led3.init();
|
2026-04-26 16:24:42 +08:00
|
|
|
led_pattern(0x01);
|
|
|
|
|
|
2026-04-27 11:14:22 +08:00
|
|
|
USART::setDefault(&uart0);
|
2026-04-26 16:24:42 +08:00
|
|
|
led_pattern(0x03);
|
|
|
|
|
|
2026-04-27 11:14:22 +08:00
|
|
|
printf("\r\n===== LSPi Board Boot =====\r\n");
|
2026-04-26 16:24:42 +08:00
|
|
|
|
|
|
|
|
uint32_t cs = RCU_CFG0 & RCU_CFG0_SCS;
|
2026-04-26 22:43:32 +08:00
|
|
|
printf("Clock source: ");
|
2026-04-26 21:25:00 +08:00
|
|
|
if (cs == RCU_SCSS_IRC16M)
|
2026-04-26 22:43:32 +08:00
|
|
|
printf("IRC16M (16MHz)\r\n");
|
2026-04-26 21:25:00 +08:00
|
|
|
else if (cs == RCU_SCSS_HXTAL)
|
2026-04-26 22:43:32 +08:00
|
|
|
printf("HXTAL (25MHz)\r\n");
|
2026-04-26 21:25:00 +08:00
|
|
|
else if (cs == RCU_SCSS_PLLP)
|
2026-04-26 22:43:32 +08:00
|
|
|
printf("PLL (168MHz)\r\n");
|
2026-04-26 21:25:00 +08:00
|
|
|
else
|
2026-04-26 22:43:32 +08:00
|
|
|
printf("UNKNOWN\r\n");
|
|
|
|
|
printf("SystemCoreClock: %lu Hz\r\n", SystemCoreClock);
|
2026-04-26 16:24:42 +08:00
|
|
|
|
2026-04-26 16:38:14 +08:00
|
|
|
led_pattern(0x05);
|
2026-04-26 21:14:00 +08:00
|
|
|
HardwareInit::init();
|
2026-04-27 11:14:22 +08:00
|
|
|
led_pattern(0x07);
|
2026-04-26 16:24:42 +08:00
|
|
|
|
2026-04-27 11:14:22 +08:00
|
|
|
uart0.reinit(9600);
|
2026-04-26 16:24:42 +08:00
|
|
|
|
|
|
|
|
cs = RCU_CFG0 & RCU_CFG0_SCS;
|
2026-04-27 11:14:22 +08:00
|
|
|
printf("System clock switched to: ");
|
2026-04-26 21:25:00 +08:00
|
|
|
if (cs == RCU_SCSS_PLLP)
|
2026-04-26 22:43:32 +08:00
|
|
|
printf("PLL (168MHz)\r\n");
|
2026-04-26 21:25:00 +08:00
|
|
|
else
|
2026-04-26 22:43:32 +08:00
|
|
|
printf("UNKNOWN\r\n");
|
|
|
|
|
printf("SystemCoreClock: %lu Hz\r\n", SystemCoreClock);
|
2026-04-26 16:24:42 +08:00
|
|
|
|
2026-04-26 16:38:14 +08:00
|
|
|
led_pattern(0x09);
|
2026-04-26 16:24:42 +08:00
|
|
|
systick_config();
|
2026-04-27 11:14:22 +08:00
|
|
|
delay_1ms(10);
|
2026-04-26 16:24:42 +08:00
|
|
|
|
2026-04-27 11:14:22 +08:00
|
|
|
printf("Initializing SDRAM...\r\n");
|
|
|
|
|
led_pattern(0x0B);
|
|
|
|
|
{
|
|
|
|
|
RetCode ret = SdramManager::instance().init();
|
|
|
|
|
if (ret != RET_OK) {
|
|
|
|
|
printf(" SDRAM init failed: %d\r\n", ret);
|
|
|
|
|
} else {
|
|
|
|
|
printf(" SDRAM init OK\r\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-26 16:24:42 +08:00
|
|
|
|
2026-04-27 11:14:22 +08:00
|
|
|
printf("Initializing Flash...\r\n");
|
|
|
|
|
led_pattern(0x0D);
|
|
|
|
|
{
|
|
|
|
|
RetCode ret = FlashManager::instance().init();
|
|
|
|
|
if (ret != RET_OK) {
|
|
|
|
|
printf(" Flash init failed: %d\r\n", ret);
|
|
|
|
|
} else {
|
|
|
|
|
printf(" Flash init OK (%lu KB total, %lu KB/sector)\r\n",
|
|
|
|
|
FlashManager::instance().total_size() / 1024,
|
|
|
|
|
FlashManager::instance().sector_size() / 1024);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-26 16:24:42 +08:00
|
|
|
|
2026-04-27 11:14:22 +08:00
|
|
|
printf("Initializing LCD...\r\n");
|
|
|
|
|
{
|
|
|
|
|
RetCode ret = Lcd::instance().init();
|
|
|
|
|
if (ret != RET_OK) {
|
|
|
|
|
printf(" LCD init failed: %d\r\n", ret);
|
|
|
|
|
} else {
|
|
|
|
|
printf(" LCD init OK (%s)\r\n", Lcd::instance().idString());
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-26 16:24:42 +08:00
|
|
|
|
2026-04-27 11:14:22 +08:00
|
|
|
printf("Initializing Touch...\r\n");
|
|
|
|
|
{
|
|
|
|
|
RetCode ret = GT1151::instance().init();
|
|
|
|
|
if (ret != RET_OK) {
|
|
|
|
|
printf(" Touch init failed: %d\r\n", ret);
|
|
|
|
|
} else {
|
|
|
|
|
printf(" Touch init OK\r\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-26 20:36:54 +08:00
|
|
|
|
2026-04-26 16:24:42 +08:00
|
|
|
led_pattern(0x0F);
|
2026-04-27 11:14:22 +08:00
|
|
|
printf("\r\n===== System Ready =====\r\n");
|
|
|
|
|
printf("CPU: 168MHz, SDRAM: 32MB, Flash: 1MB\r\n\r\n");
|
|
|
|
|
|
|
|
|
|
Lcd::instance().clear(Lcd::BLACK);
|
|
|
|
|
Lcd::instance().setForeground(Lcd::CYAN);
|
|
|
|
|
Lcd::instance().showString(10, 10, 460, 24, 16, 0, (uint8_t *)"LSPi System Ready");
|
|
|
|
|
Lcd::instance().setForeground(Lcd::GRAY);
|
|
|
|
|
Lcd::instance().showString(10, 30, 460, 20, 16, 0, (uint8_t *)"168MHz | 32MB SDRAM | 1MB Flash");
|
2026-04-26 16:24:42 +08:00
|
|
|
|
|
|
|
|
uint32_t tick = 0;
|
2026-04-26 21:25:00 +08:00
|
|
|
while (1)
|
|
|
|
|
{
|
2026-04-27 11:14:22 +08:00
|
|
|
led0.toggle();
|
|
|
|
|
delay_1ms(500);
|
2026-04-26 16:24:42 +08:00
|
|
|
|
|
|
|
|
tick++;
|
2026-04-27 11:14:22 +08:00
|
|
|
if (tick % 10 == 0) {
|
2026-04-26 16:24:42 +08:00
|
|
|
printf(".");
|
2026-04-27 11:14:22 +08:00
|
|
|
}
|
|
|
|
|
if (tick % 120 == 0) {
|
|
|
|
|
printf(" [%lu s]\r\n", tick / 2);
|
|
|
|
|
}
|
2026-04-26 16:24:42 +08:00
|
|
|
}
|
|
|
|
|
}
|