Files
lishanpi/app/main.cpp
T

146 lines
3.6 KiB
C++
Raw Normal View History

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"
#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];
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)
{
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
}
USART uart0(UartPort::_0, 9600);
2026-04-26 16:24:42 +08:00
int main(void)
{
led0.init();
led1.init();
led2.init();
led3.init();
2026-04-26 16:24:42 +08:00
led_pattern(0x01);
USART::setDefault(&uart0);
2026-04-26 16:24:42 +08:00
led_pattern(0x03);
printf("\r\n===== LSPi Board Boot =====\r\n");
2026-04-26 16:24:42 +08:00
uint32_t cs = RCU_CFG0 & RCU_CFG0_SCS;
printf("Clock source: ");
if (cs == RCU_SCSS_IRC16M)
printf("IRC16M (16MHz)\r\n");
else if (cs == RCU_SCSS_HXTAL)
printf("HXTAL (25MHz)\r\n");
else if (cs == RCU_SCSS_PLLP)
printf("PLL (168MHz)\r\n");
else
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);
HardwareInit::init();
led_pattern(0x07);
2026-04-26 16:24:42 +08:00
uart0.reinit(9600);
2026-04-26 16:24:42 +08:00
cs = RCU_CFG0 & RCU_CFG0_SCS;
printf("System clock switched to: ");
if (cs == RCU_SCSS_PLLP)
printf("PLL (168MHz)\r\n");
else
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();
delay_1ms(10);
2026-04-26 16:24:42 +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
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
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
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 16:24:42 +08:00
led_pattern(0x0F);
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;
while (1)
{
led0.toggle();
delay_1ms(500);
2026-04-26 16:24:42 +08:00
tick++;
if (tick % 10 == 0) {
2026-04-26 16:24:42 +08:00
printf(".");
}
if (tick % 120 == 0) {
printf(" [%lu s]\r\n", tick / 2);
}
2026-04-26 16:24:42 +08:00
}
}