184 lines
4.3 KiB
Python
184 lines
4.3 KiB
Python
|
|
import os
|
||
|
|
|
||
|
|
content = r'''#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"
|
||
|
|
#include "monitor_task.h"
|
||
|
|
#include "work_task.h"
|
||
|
|
#include <cstdio>
|
||
|
|
#include <cstring>
|
||
|
|
#include <rthw.h>
|
||
|
|
#include <rtthread.h>
|
||
|
|
|
||
|
|
extern "C" void rt_hw_board_init(void);
|
||
|
|
|
||
|
|
__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);
|
||
|
|
|
||
|
|
static void led_pattern(uint8_t p)
|
||
|
|
{
|
||
|
|
led0.off();
|
||
|
|
if (p & 0x01) led0.on();
|
||
|
|
led1.off();
|
||
|
|
if (p & 0x02) led1.on();
|
||
|
|
led2.off();
|
||
|
|
if (p & 0x04) led2.on();
|
||
|
|
led3.off();
|
||
|
|
if (p & 0x08) led3.on();
|
||
|
|
}
|
||
|
|
|
||
|
|
static void led_task_entry(void *parameter)
|
||
|
|
{
|
||
|
|
(void)parameter;
|
||
|
|
uint32_t tick = 0;
|
||
|
|
while (1)
|
||
|
|
{
|
||
|
|
led0.toggle();
|
||
|
|
rt_thread_mdelay(500);
|
||
|
|
|
||
|
|
tick++;
|
||
|
|
if (tick % 10 == 0)
|
||
|
|
{
|
||
|
|
printf(".");
|
||
|
|
}
|
||
|
|
if (tick % 120 == 0)
|
||
|
|
{
|
||
|
|
printf(" [%lu s]\r\n", tick / 2);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
extern "C" int main(void)
|
||
|
|
{
|
||
|
|
led0.init();
|
||
|
|
led1.init();
|
||
|
|
led2.init();
|
||
|
|
led3.init();
|
||
|
|
led_pattern(0x05);
|
||
|
|
|
||
|
|
HardwareInit::init();
|
||
|
|
led_pattern(0x07);
|
||
|
|
|
||
|
|
uart_0.reinit(9600);
|
||
|
|
USART::setDefault(&uart_0);
|
||
|
|
|
||
|
|
uint32_t 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);
|
||
|
|
|
||
|
|
led_pattern(0x09);
|
||
|
|
systick_config();
|
||
|
|
delay_1ms(10);
|
||
|
|
|
||
|
|
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");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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");
|
||
|
|
|
||
|
|
printf("Initializing RT-Thread...\r\n");
|
||
|
|
rt_hw_board_init();
|
||
|
|
rt_system_timer_init();
|
||
|
|
rt_system_scheduler_init();
|
||
|
|
|
||
|
|
monitor_task_init();
|
||
|
|
work_task_init();
|
||
|
|
|
||
|
|
rt_thread_t led_thread = rt_thread_create("led",
|
||
|
|
led_task_entry,
|
||
|
|
RT_NULL,
|
||
|
|
512,
|
||
|
|
RT_THREAD_PRIORITY_MAX - 4,
|
||
|
|
10);
|
||
|
|
if (led_thread != RT_NULL)
|
||
|
|
{
|
||
|
|
rt_thread_startup(led_thread);
|
||
|
|
}
|
||
|
|
|
||
|
|
printf("Starting RT-Thread scheduler...\r\n");
|
||
|
|
rt_system_scheduler_start();
|
||
|
|
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
'''
|
||
|
|
|
||
|
|
path = r'd:\20_AI\LSPi\app\main.cpp'
|
||
|
|
with open(path, 'w', newline='\n') as f:
|
||
|
|
f.write(content)
|
||
|
|
print(f'Written {len(content)} bytes to {path}')
|
||
|
|
print(f'Lines: {content.count(chr(10))}')
|