187 lines
4.3 KiB
C++
187 lines
4.3 KiB
C++
#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;
|
|
uint8_t step = 0;
|
|
uint8_t led_map[4] = {1, 2, 4, 8};
|
|
uint32_t job_id = 0;
|
|
|
|
led0.on();
|
|
led1.off();
|
|
led2.off();
|
|
led3.off();
|
|
|
|
while (1)
|
|
{
|
|
rt_thread_mdelay(250);
|
|
tick++;
|
|
|
|
step = (step + 1) & 3;
|
|
led_pattern(led_map[step]);
|
|
|
|
if (tick % 8 == 0)
|
|
{
|
|
printf("\r[%08lu] *** system alive @ %lu ticks ",
|
|
(unsigned long)(tick / 4),
|
|
(unsigned long)rt_tick_get());
|
|
}
|
|
|
|
if (tick % 120 == 0)
|
|
{
|
|
job_id++;
|
|
printf("\r\n[MAIN] Dispatching job #%lu to worker...\r\n", (unsigned long)job_id);
|
|
work_task_send(job_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
|
|
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;
|
|
}
|