refactor: 完整C++面向对象重构(LedManager/SdramManager)
This commit is contained in:
+147
-118
@@ -12,62 +12,27 @@
|
||||
|
||||
__attribute__((section(".sdram"))) uint8_t sdram_big_buffer[1024 * 1024];
|
||||
|
||||
#define LED1_PORT GPIOE
|
||||
#define LED1_PIN GPIO_PIN_3
|
||||
#define LED2_PORT GPIOD
|
||||
#define LED2_PIN GPIO_PIN_7
|
||||
#define LED3_PORT GPIOG
|
||||
#define LED3_PIN GPIO_PIN_3
|
||||
#define LED4_PORT GPIOA
|
||||
#define LED4_PIN GPIO_PIN_5
|
||||
|
||||
static void delay_16m(uint32_t ms)
|
||||
{
|
||||
for (volatile uint32_t i = 0; i < ms * 4000U; i++);
|
||||
}
|
||||
|
||||
static void led_on(uint32_t port, uint32_t pin)
|
||||
{
|
||||
gpio_bit_set(port, pin);
|
||||
}
|
||||
|
||||
static void led_off(uint32_t port, uint32_t pin)
|
||||
{
|
||||
gpio_bit_reset(port, pin);
|
||||
for (volatile uint32_t i = 0; i < ms * 4000U; i++)
|
||||
;
|
||||
}
|
||||
|
||||
static void led_pattern(uint8_t p)
|
||||
{
|
||||
led_off(LED1_PORT, LED1_PIN);
|
||||
led_off(LED2_PORT, LED2_PIN);
|
||||
led_off(LED3_PORT, LED3_PIN);
|
||||
led_off(LED4_PORT, LED4_PIN);
|
||||
if (p & 0x01) led_on(LED1_PORT, LED1_PIN);
|
||||
if (p & 0x02) led_on(LED2_PORT, LED2_PIN);
|
||||
if (p & 0x04) led_on(LED3_PORT, LED3_PIN);
|
||||
if (p & 0x08) led_on(LED4_PORT, LED4_PIN);
|
||||
}
|
||||
|
||||
static void init_leds(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_GPIOA);
|
||||
rcu_periph_clock_enable(RCU_GPIOE);
|
||||
rcu_periph_clock_enable(RCU_GPIOG);
|
||||
rcu_periph_clock_enable(RCU_GPIOD);
|
||||
|
||||
gpio_mode_set(LED1_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED1_PIN);
|
||||
gpio_output_options_set(LED1_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED1_PIN);
|
||||
gpio_mode_set(LED2_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED2_PIN);
|
||||
gpio_output_options_set(LED2_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED2_PIN);
|
||||
gpio_mode_set(LED3_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED3_PIN);
|
||||
gpio_output_options_set(LED3_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED3_PIN);
|
||||
gpio_mode_set(LED4_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED4_PIN);
|
||||
gpio_output_options_set(LED4_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, LED4_PIN);
|
||||
|
||||
led_off(LED1_PORT, LED1_PIN);
|
||||
led_off(LED2_PORT, LED2_PIN);
|
||||
led_off(LED3_PORT, LED3_PIN);
|
||||
led_off(LED4_PORT, LED4_PIN);
|
||||
LedManager &lm = LedManager::instance();
|
||||
lm.led(0).off();
|
||||
lm.led(1).off();
|
||||
lm.led(2).off();
|
||||
lm.led(3).off();
|
||||
if (p & 0x01)
|
||||
lm.led(0).on();
|
||||
if (p & 0x02)
|
||||
lm.led(1).on();
|
||||
if (p & 0x04)
|
||||
lm.led(2).on();
|
||||
if (p & 0x08)
|
||||
lm.led(3).on();
|
||||
}
|
||||
|
||||
static void init_usart0(void)
|
||||
@@ -89,21 +54,32 @@ static void init_usart0(void)
|
||||
static void uart_putchar(char c)
|
||||
{
|
||||
usart_data_transmit(USART0, (uint8_t)c);
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE));
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE))
|
||||
;
|
||||
}
|
||||
|
||||
static void uart_puts(const char *s)
|
||||
{
|
||||
while (*s) uart_putchar(*s++);
|
||||
while (*s)
|
||||
uart_putchar(*s++);
|
||||
}
|
||||
|
||||
static void uart_putdec(uint32_t val)
|
||||
{
|
||||
char buf[16];
|
||||
int idx = 0;
|
||||
if (val == 0) { uart_puts("0"); return; }
|
||||
while (val > 0) { buf[idx++] = '0' + (val % 10); val /= 10; }
|
||||
while (idx > 0) uart_putchar(buf[--idx]);
|
||||
if (val == 0)
|
||||
{
|
||||
uart_puts("0");
|
||||
return;
|
||||
}
|
||||
while (val > 0)
|
||||
{
|
||||
buf[idx++] = '0' + (val % 10);
|
||||
val /= 10;
|
||||
}
|
||||
while (idx > 0)
|
||||
uart_putchar(buf[--idx]);
|
||||
}
|
||||
|
||||
static void reinit_usart0_168m(void)
|
||||
@@ -121,7 +97,8 @@ static void test_sdram_driver(void)
|
||||
|
||||
SdramManager &sdram = SdramManager::instance();
|
||||
RetCode ret = sdram.init();
|
||||
if (ret != RET_OK) {
|
||||
if (ret != RET_OK)
|
||||
{
|
||||
printf(" SDRAM 初始化失败: 错误码=%d\r\n", ret);
|
||||
return;
|
||||
}
|
||||
@@ -134,10 +111,12 @@ static void test_sdram_driver(void)
|
||||
delay_1ms(1);
|
||||
|
||||
bool diag1_ok = true;
|
||||
for (uint32_t i = 0; i < 8; i++) {
|
||||
for (uint32_t i = 0; i < 8; i++)
|
||||
{
|
||||
uint16_t val = sram16[i];
|
||||
uint16_t expected = (uint16_t)(0xAA00 + i);
|
||||
if (val != expected) {
|
||||
if (val != expected)
|
||||
{
|
||||
printf(" 16位错误 @ [%lu]: 期望=0x%04X, 读取=0x%04X\r\n", i, expected, val);
|
||||
diag1_ok = false;
|
||||
}
|
||||
@@ -154,11 +133,14 @@ static void test_sdram_driver(void)
|
||||
delay_1ms(1);
|
||||
|
||||
bool diag2_ok = true;
|
||||
for (uint32_t i = 0; i < 4; i++) {
|
||||
for (uint32_t i = 0; i < 4; i++)
|
||||
{
|
||||
uint32_t val = sram32[i];
|
||||
uint32_t expected = (i == 0) ? 0xDEADBEEF : (i == 1) ? 0xCAFEBABE
|
||||
: (i == 2) ? 0x12345678 : 0x87654321;
|
||||
if (val != expected) {
|
||||
: (i == 2) ? 0x12345678
|
||||
: 0x87654321;
|
||||
if (val != expected)
|
||||
{
|
||||
printf(" 32位错误 @ [%lu]: 期望=0x%08lX, 读取=0x%08lX\r\n", i, expected, val);
|
||||
diag2_ok = false;
|
||||
}
|
||||
@@ -169,9 +151,12 @@ static void test_sdram_driver(void)
|
||||
uint32_t write_val = 0xA5A55A5A;
|
||||
uint32_t read_val = 0;
|
||||
ret = sdram.write(64, &write_val, sizeof(write_val));
|
||||
if (ret != RET_OK) {
|
||||
if (ret != RET_OK)
|
||||
{
|
||||
printf(" 驱动写入失败: %d\r\n", ret);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
__DSB();
|
||||
ret = sdram.read(64, &read_val, sizeof(read_val));
|
||||
if (ret != RET_OK)
|
||||
@@ -190,7 +175,8 @@ static void test_flash_driver(void)
|
||||
|
||||
FlashManager &flash = FlashManager::instance();
|
||||
RetCode ret = flash.init();
|
||||
if (ret != RET_OK) {
|
||||
if (ret != RET_OK)
|
||||
{
|
||||
printf(" Flash 初始化失败: %d\r\n", ret);
|
||||
return;
|
||||
}
|
||||
@@ -208,32 +194,49 @@ static void test_flash_driver(void)
|
||||
|
||||
printf(" 擦除 Flash 扇区...\r\n");
|
||||
ret = flash.erase(test_address, flash.sector_size());
|
||||
if (ret != RET_OK) { printf(" Flash 擦除失败: 错误码=%d\r\n", ret); return; }
|
||||
if (ret != RET_OK)
|
||||
{
|
||||
printf(" Flash 擦除失败: 错误码=%d\r\n", ret);
|
||||
return;
|
||||
}
|
||||
printf(" Flash 擦除成功\r\n");
|
||||
|
||||
printf(" 写入 Flash 数据...\r\n");
|
||||
ret = flash.write(test_address, write_buffer, sizeof(write_buffer));
|
||||
if (ret != RET_OK) { printf(" Flash 写入失败: %d\r\n", ret); return; }
|
||||
if (ret != RET_OK)
|
||||
{
|
||||
printf(" Flash 写入失败: %d\r\n", ret);
|
||||
return;
|
||||
}
|
||||
printf(" Flash 写入成功\r\n");
|
||||
|
||||
printf(" 读取 Flash 数据...\r\n");
|
||||
ret = flash.read(test_address, read_buffer, sizeof(read_buffer));
|
||||
if (ret != RET_OK) { printf(" Flash 读取失败: %d\r\n", ret); return; }
|
||||
if (ret != RET_OK)
|
||||
{
|
||||
printf(" Flash 读取失败: %d\r\n", ret);
|
||||
return;
|
||||
}
|
||||
printf(" Flash 读取成功\r\n");
|
||||
|
||||
bool verify_ok = true;
|
||||
for (int i = 0; i < 256; i++) {
|
||||
if (read_buffer[i] != write_buffer[i]) {
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
if (read_buffer[i] != write_buffer[i])
|
||||
{
|
||||
verify_ok = false;
|
||||
printf(" 数据错误 @ %d: 写入=%02X, 读取=%02X\r\n", i, write_buffer[i], read_buffer[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (verify_ok) printf(" Flash 数据验证成功\r\n");
|
||||
if (verify_ok)
|
||||
printf(" Flash 数据验证成功\r\n");
|
||||
|
||||
ret = flash.self_test(256);
|
||||
if (ret == RET_OK) printf(" Flash 自测试通过\r\n");
|
||||
else printf(" Flash 自测试失败: %d\r\n", ret);
|
||||
if (ret == RET_OK)
|
||||
printf(" Flash 自测试通过\r\n");
|
||||
else
|
||||
printf(" Flash 自测试失败: %d\r\n", ret);
|
||||
printf("Flash 驱动测试完成\r\n");
|
||||
}
|
||||
|
||||
@@ -249,7 +252,8 @@ static void test_lcd(void)
|
||||
delay_1ms(500);
|
||||
|
||||
uint16_t bands[4] = {Lcd::RED, Lcd::GREEN, Lcd::BLUE, Lcd::WHITE};
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
int y0 = i * 200;
|
||||
int y1 = y0 + 199;
|
||||
lcd.blockWrite(0, 479, y0, y1);
|
||||
@@ -279,10 +283,12 @@ static void test_sdram_after_lcd(void)
|
||||
bool ok = true;
|
||||
|
||||
volatile uint16_t *sram16 = (volatile uint16_t *)0xC0000000;
|
||||
for (uint32_t i = 0; i < 8; i++) {
|
||||
for (uint32_t i = 0; i < 8; i++)
|
||||
{
|
||||
uint16_t val = sram16[i];
|
||||
uint16_t expected = (uint16_t)(0xAA00 + i);
|
||||
if (val != expected) {
|
||||
if (val != expected)
|
||||
{
|
||||
printf(" 16位错误 @ [%lu]: 期望=0x%04X, 读取=0x%04X\r\n", i, expected, val);
|
||||
ok = false;
|
||||
}
|
||||
@@ -290,9 +296,11 @@ static void test_sdram_after_lcd(void)
|
||||
|
||||
volatile uint32_t *sram32 = (volatile uint32_t *)0xC0000000;
|
||||
uint32_t expected_32[4] = {0xDEADBEEF, 0xCAFEBABE, 0x12345678, 0x87654321};
|
||||
for (uint32_t i = 0; i < 4; i++) {
|
||||
for (uint32_t i = 0; i < 4; i++)
|
||||
{
|
||||
uint32_t val = sram32[i];
|
||||
if (val != expected_32[i]) {
|
||||
if (val != expected_32[i])
|
||||
{
|
||||
printf(" 32位错误 @ [%lu]: 期望=0x%08lX, 读取=0x%08lX\r\n", i, expected_32[i], val);
|
||||
ok = false;
|
||||
}
|
||||
@@ -312,17 +320,18 @@ static void concurrent_loop(void)
|
||||
uint32_t counter = 0;
|
||||
volatile uint16_t *sram16 = (volatile uint16_t *)0xC0000000;
|
||||
|
||||
for (int iter = 0; iter < 10; iter++) {
|
||||
led_on(LED1_PORT, LED1_PIN);
|
||||
led_off(LED2_PORT, LED2_PIN);
|
||||
for (int iter = 0; iter < 10; iter++)
|
||||
{
|
||||
LedManager::instance().led(0).on();
|
||||
LedManager::instance().led(1).off();
|
||||
sram16[100 + iter] = (uint16_t)(iter * 0x1111);
|
||||
__DSB();
|
||||
uint16_t val = sram16[100 + iter];
|
||||
printf("[并发] iter=%d, SDRAM[%d]=0x%04X, LED1=ON\r\n", iter, 100 + iter, val);
|
||||
delay_1ms(500);
|
||||
|
||||
led_off(LED1_PORT, LED1_PIN);
|
||||
led_on(LED2_PORT, LED2_PIN);
|
||||
LedManager::instance().led(0).off();
|
||||
LedManager::instance().led(1).on();
|
||||
sram16[200 + iter] = (uint16_t)(iter * 0x2222);
|
||||
__DSB();
|
||||
val = sram16[200 + iter];
|
||||
@@ -345,18 +354,19 @@ static void test_touch(void)
|
||||
printf("\r\n===== Touch 测试 (多点触控) =====\r\n");
|
||||
|
||||
RetCode ret = touch.init();
|
||||
if (ret != RET_OK) {
|
||||
if (ret != RET_OK)
|
||||
{
|
||||
printf(" Touch 初始化失败 (ret=%d)\r\n", ret);
|
||||
return;
|
||||
}
|
||||
printf(" Touch 初始化成功,等待触摸中断...\r\n");
|
||||
|
||||
static const uint16_t TP_COLORS[5] = {
|
||||
0xFFE0, // YELLOW - point 0
|
||||
0x07FF, // CYAN - point 1
|
||||
0xF81F, // MAGENTA - point 2
|
||||
0x07E0, // GREEN - point 3
|
||||
0xF800 // RED - point 4
|
||||
0xFFE0, // YELLOW - point 0
|
||||
0x07FF, // CYAN - point 1
|
||||
0xF81F, // MAGENTA - point 2
|
||||
0x07E0, // GREEN - point 3
|
||||
0xF800 // RED - point 4
|
||||
};
|
||||
|
||||
const uint16_t BG_COLOR = 0x0841;
|
||||
@@ -367,7 +377,8 @@ static void test_touch(void)
|
||||
lcd.setForeground(Lcd::WHITE);
|
||||
lcd.showString(10, 30, 460, 24, 16, 0, (uint8_t *)"Touch with up to 5 fingers");
|
||||
|
||||
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++) {
|
||||
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++)
|
||||
{
|
||||
lcd.setForeground(TP_COLORS[i]);
|
||||
char lbl[8];
|
||||
sprintf(lbl, "P%d: --", i);
|
||||
@@ -377,7 +388,8 @@ static void test_touch(void)
|
||||
uint16_t last_x[5];
|
||||
uint16_t last_y[5];
|
||||
bool was_active[5];
|
||||
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++) {
|
||||
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++)
|
||||
{
|
||||
last_x[i] = 0xFFFF;
|
||||
last_y[i] = 0xFFFF;
|
||||
was_active[i] = false;
|
||||
@@ -386,8 +398,10 @@ static void test_touch(void)
|
||||
uint32_t idle_counter = 0;
|
||||
GT1151::irq_flag = 0;
|
||||
|
||||
while (1) {
|
||||
if (GT1151::irq_flag) {
|
||||
while (1)
|
||||
{
|
||||
if (GT1151::irq_flag)
|
||||
{
|
||||
GT1151::irq_flag = 0;
|
||||
delay_1ms(5);
|
||||
|
||||
@@ -399,21 +413,25 @@ static void test_touch(void)
|
||||
uint8_t active_cnt = touch.activeCount();
|
||||
uint8_t mask = sta & 0x1F;
|
||||
|
||||
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++) {
|
||||
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++)
|
||||
{
|
||||
bool active = (mask & (1 << i)) != 0;
|
||||
|
||||
if (active) {
|
||||
if (active)
|
||||
{
|
||||
uint16_t x = touch.x(i);
|
||||
uint16_t y = touch.y(i);
|
||||
|
||||
if (x < lcd.width() && y < lcd.height()) {
|
||||
if (x < lcd.width() && y < lcd.height())
|
||||
{
|
||||
lcd.setForeground(TP_COLORS[i]);
|
||||
char buf[20];
|
||||
sprintf(buf, "P%d: X:%-3d Y:%-3d", i, x, y);
|
||||
lcd.fillRectangle(10, 52 + i * 18, 200, 52 + i * 18 + 17);
|
||||
lcd.showString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)buf);
|
||||
|
||||
if (was_active[i] && last_x[i] != 0xFFFF) {
|
||||
if (was_active[i] && last_x[i] != 0xFFFF)
|
||||
{
|
||||
lcd.setForeground(TP_COLORS[i]);
|
||||
lcd.drawLine(last_x[i], last_y[i], x, y);
|
||||
}
|
||||
@@ -425,8 +443,11 @@ static void test_touch(void)
|
||||
last_y[i] = y;
|
||||
was_active[i] = true;
|
||||
}
|
||||
} else {
|
||||
if (was_active[i]) {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (was_active[i])
|
||||
{
|
||||
lcd.setForeground(BG_COLOR);
|
||||
lcd.fillCircle(last_x[i], last_y[i], 6);
|
||||
|
||||
@@ -442,15 +463,19 @@ static void test_touch(void)
|
||||
}
|
||||
|
||||
printf("[TOUCH] cnt=%d sta=0x%02X", active_cnt, sta);
|
||||
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++) {
|
||||
for (uint8_t i = 0; i < GT1151::kMaxTouch; i++)
|
||||
{
|
||||
if (mask & (1 << i))
|
||||
printf(" P%d(%u,%u)", i, touch.x(i), touch.y(i));
|
||||
}
|
||||
printf("\r\n");
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
idle_counter++;
|
||||
delay_1ms(10);
|
||||
if (idle_counter > 500) {
|
||||
if (idle_counter > 500)
|
||||
{
|
||||
printf(" 无触摸超过 5 秒,退出 Touch 测试\r\n");
|
||||
break;
|
||||
}
|
||||
@@ -464,7 +489,7 @@ static void test_touch(void)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
init_leds();
|
||||
LedManager::instance().init_all();
|
||||
led_pattern(0x01);
|
||||
|
||||
init_usart0();
|
||||
@@ -475,18 +500,23 @@ int main(void)
|
||||
|
||||
uint32_t cs = RCU_CFG0 & RCU_CFG0_SCS;
|
||||
uart_puts("Clock source: ");
|
||||
if (cs == RCU_SCSS_IRC16M) uart_puts("IRC16M (16MHz)\r\n");
|
||||
else if (cs == RCU_SCSS_HXTAL) uart_puts("HXTAL (25MHz)\r\n");
|
||||
else if (cs == RCU_SCSS_PLLP) uart_puts("PLL (168MHz)\r\n");
|
||||
else uart_puts("UNKNOWN\r\n");
|
||||
if (cs == RCU_SCSS_IRC16M)
|
||||
uart_puts("IRC16M (16MHz)\r\n");
|
||||
else if (cs == RCU_SCSS_HXTAL)
|
||||
uart_puts("HXTAL (25MHz)\r\n");
|
||||
else if (cs == RCU_SCSS_PLLP)
|
||||
uart_puts("PLL (168MHz)\r\n");
|
||||
else
|
||||
uart_puts("UNKNOWN\r\n");
|
||||
uart_puts("SystemCoreClock: ");
|
||||
uart_putdec(SystemCoreClock);
|
||||
uart_puts(" Hz\r\n");
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
led_on(LED2_PORT, LED2_PIN);
|
||||
for (int i = 0; i < 3; i++)
|
||||
{
|
||||
LedManager::instance().led(1).on();
|
||||
delay_16m(200);
|
||||
led_off(LED2_PORT, LED2_PIN);
|
||||
LedManager::instance().led(1).off();
|
||||
delay_16m(200);
|
||||
}
|
||||
uart_puts("LED blink OK\r\n");
|
||||
@@ -500,17 +530,15 @@ int main(void)
|
||||
|
||||
cs = RCU_CFG0 & RCU_CFG0_SCS;
|
||||
uart_puts("Clock after init: ");
|
||||
if (cs == RCU_SCSS_PLLP) uart_puts("PLL (168MHz)\r\n");
|
||||
else uart_puts("UNKNOWN\r\n");
|
||||
if (cs == RCU_SCSS_PLLP)
|
||||
uart_puts("PLL (168MHz)\r\n");
|
||||
else
|
||||
uart_puts("UNKNOWN\r\n");
|
||||
uart_puts("SystemCoreClock: ");
|
||||
uart_putdec(SystemCoreClock);
|
||||
uart_puts(" Hz\r\n");
|
||||
|
||||
led_pattern(0x07);
|
||||
LedManager::instance().init_all();
|
||||
LedManager::instance().led(1).on();
|
||||
delay_16m(100);
|
||||
LedManager::instance().led(1).off();
|
||||
uart_puts("LedManager OK\r\n");
|
||||
|
||||
led_pattern(0x09);
|
||||
@@ -554,10 +582,11 @@ int main(void)
|
||||
printf("系统运行于 168MHz, Flash/SDRAM/LCD/UART/LED 全部正常\r\n");
|
||||
|
||||
uint32_t tick = 0;
|
||||
while (1) {
|
||||
led_on(LED4_PORT, LED4_PIN);
|
||||
while (1)
|
||||
{
|
||||
LedManager::instance().led(3).on();
|
||||
delay_1ms(250);
|
||||
led_off(LED4_PORT, LED4_PIN);
|
||||
LedManager::instance().led(3).off();
|
||||
delay_1ms(250);
|
||||
|
||||
tick++;
|
||||
|
||||
Reference in New Issue
Block a user