From ff91e324cfbe0cea6cb7e42ca73ca23dc483d4f9 Mon Sep 17 00:00:00 2001 From: hm-thinkbook14p Date: Sun, 26 Apr 2026 21:14:00 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=AE=8C=E6=95=B4C++=E9=9D=A2?= =?UTF-8?q?=E5=90=91=E5=AF=B9=E8=B1=A1=E9=87=8D=E6=9E=84(Lcd/GT1151/Hardwa?= =?UTF-8?q?reInit)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.cpp | 301 ++++---- bsp/device/lcd/lcd.cpp | 1410 ++++++++++++++++++------------------ bsp/device/lcd/lcd.h | 224 +++--- bsp/device/touch/touch.cpp | 301 ++++---- bsp/device/touch/touch.h | 118 +-- bsp/hardware_init.cpp | 38 +- bsp/hardware_init.h | 18 +- 7 files changed, 1191 insertions(+), 1219 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index 772cbbb..1fc2158 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -10,12 +10,8 @@ #include #include -// SDRAM 中分配的测试缓冲区 __attribute__((section(".sdram"))) uint8_t sdram_big_buffer[1024 * 1024]; -// ============================================================= -// 板级引脚定义 -// ============================================================= #define LED1_PORT GPIOE #define LED1_PIN GPIO_PIN_3 #define LED2_PORT GPIOD @@ -25,10 +21,6 @@ __attribute__((section(".sdram"))) uint8_t sdram_big_buffer[1024 * 1024]; #define LED4_PORT GPIOA #define LED4_PIN GPIO_PIN_5 -// ============================================================= -// 早期启动函数(16MHz,无 SysTick) -// ============================================================= - static void delay_16m(uint32_t ms) { for (volatile uint32_t i = 0; i < ms * 4000U; i++); @@ -114,7 +106,6 @@ static void uart_putdec(uint32_t val) while (idx > 0) uart_putchar(buf[--idx]); } -// 时钟切换到 168MHz 后重新配置 UART 波特率 static void reinit_usart0_168m(void) { usart_deinit(USART0); @@ -124,10 +115,6 @@ static void reinit_usart0_168m(void) usart_enable(USART0); } -// ============================================================= -// 外设测试函数 -// ============================================================= - static void test_sdram_driver(void) { printf("SDRAM 驱动测试开始...\r\n"); @@ -252,31 +239,33 @@ static void test_flash_driver(void) static void test_lcd(void) { + Lcd &lcd = Lcd::instance(); + printf("\r\n===== LCD 功能测试 =====\r\n"); printf(" LCD_Init...\r\n"); - LCD_Init(); - printf(" LCD ID: %s\r\n", lcd_id); + lcd.init(); + printf(" LCD ID: %s\r\n", lcd.idString()); delay_1ms(500); - uint16_t bands[4] = {RED, GREEN, BLUE, WHITE}; + uint16_t bands[4] = {Lcd::RED, Lcd::GREEN, Lcd::BLUE, Lcd::WHITE}; for (int i = 0; i < 4; i++) { int y0 = i * 200; int y1 = y0 + 199; - BlockWrite(0, 479, y0, y1); - LCD_WriteRAM_Prepare(); + lcd.blockWrite(0, 479, y0, y1); + lcd.writeRamPrepare(); for (int p = 0; p < 480 * 200; p++) - LCD_WriteRAM(bands[i]); + lcd.writeRam(bands[i]); __DSB(); } delay_1ms(3000); printf(" LCD_Clear(BLACK)\r\n"); - LCD_Clear(BLACK); + lcd.clear(Lcd::BLACK); delay_1ms(500); printf(" LCD_Clear(RED)\r\n"); - LCD_Clear(RED); + lcd.clear(Lcd::RED); delay_1ms(500); printf("LCD 测试完成\r\n"); @@ -314,8 +303,10 @@ static void test_sdram_after_lcd(void) static void concurrent_loop(void) { + Lcd &lcd = Lcd::instance(); + printf("\r\n===== 全部外设并发运行 =====\r\n"); - LCD_Clear(GREEN); + lcd.clear(Lcd::GREEN); delay_1ms(500); uint32_t counter = 0; @@ -342,157 +333,137 @@ static void concurrent_loop(void) } printf("\r\n===== 并发测试完成 =====\r\n"); - LCD_Clear(BLUE); + lcd.clear(Lcd::BLUE); delay_1ms(1000); } -// ============================================================= -// Touch 测试 - // ============================================================= +static void test_touch(void) +{ + Lcd &lcd = Lcd::instance(); + GT1151 &touch = GT1151::instance(); - static void test_touch(void) - { - printf("\r\n===== Touch 测试 (多点触控) =====\r\n"); + printf("\r\n===== Touch 测试 (多点触控) =====\r\n"); - uint8_t ret = GT1151_Init(); - if (ret != 0) - { - printf(" Touch 初始化失败 (ret=%d)\r\n", ret); - return; - } - printf(" Touch 初始化成功,等待触摸中断...\r\n"); + RetCode ret = touch.init(); + 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 - }; + 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 + }; - const uint16_t BG_COLOR = 0x0841; + const uint16_t BG_COLOR = 0x0841; - LCD_Clear(BG_COLOR); - POINT_COLOR = CYAN; - LCD_ShowString(10, 10, 460, 24, 16, 0, (uint8_t *)"=== Multi-Touch (5pt) ==="); - POINT_COLOR = WHITE; - LCD_ShowString(10, 30, 460, 24, 16, 0, (uint8_t *)"Touch with up to 5 fingers"); + lcd.clear(BG_COLOR); + lcd.setForeground(Lcd::CYAN); + lcd.showString(10, 10, 460, 24, 16, 0, (uint8_t *)"=== Multi-Touch (5pt) ==="); + 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 < CT_MAX_TOUCH; i++) - { - POINT_COLOR = TP_COLORS[i]; - char lbl[8]; - sprintf(lbl, "P%d: --", i); - LCD_ShowString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)lbl); - } - - uint16_t last_x[5]; - uint16_t last_y[5]; - bool was_active[5]; - for (uint8_t i = 0; i < CT_MAX_TOUCH; i++) - { - last_x[i] = 0xFFFF; - last_y[i] = 0xFFFF; - was_active[i] = false; - } - - uint32_t idle_counter = 0; - g_touch_irq_flag = 0; - - while (1) - { - if (g_touch_irq_flag) - { - g_touch_irq_flag = 0; - delay_1ms(5); - - GT1151_Scan(0); - - idle_counter = 0; - - uint8_t active_cnt = TP_ACTIVE_COUNT(tp_dev.sta); - uint8_t mask = tp_dev.sta & 0x1F; - - for (uint8_t i = 0; i < CT_MAX_TOUCH; i++) - { - bool active = (mask & (1 << i)) != 0; - - if (active) - { - uint16_t x = tp_dev.x[i]; - uint16_t y = tp_dev.y[i]; - - if (x < lcddev.width && y < lcddev.height) - { - POINT_COLOR = 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) - { - POINT_COLOR = TP_COLORS[i]; - LCD_DrawLine(last_x[i], last_y[i], x, y); - } - - POINT_COLOR = TP_COLORS[i]; - LCD_FillCircle(x, y, 5); - - last_x[i] = x; - last_y[i] = y; - was_active[i] = true; - } - } - else - { - if (was_active[i]) - { - POINT_COLOR = BG_COLOR; - LCD_FillCircle(last_x[i], last_y[i], 6); - - POINT_COLOR = TP_COLORS[i]; - LCD_FillRectangle(10, 52 + i * 18, 200, 52 + i * 18 + 17); - LCD_ShowString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)"released"); - - last_x[i] = 0xFFFF; - last_y[i] = 0xFFFF; - was_active[i] = false; - } - } - } - - printf("[TOUCH] cnt=%d sta=0x%02X", active_cnt, tp_dev.sta); - for (uint8_t i = 0; i < CT_MAX_TOUCH; i++) - { - if (mask & (1 << i)) - printf(" P%d(%u,%u)", i, tp_dev.x[i], tp_dev.y[i]); - } - printf("\r\n"); - } - else - { - idle_counter++; - delay_1ms(10); - if (idle_counter > 500) - { - printf(" 无触摸超过 5 秒,退出 Touch 测试\r\n"); - break; - } - } - } - - LCD_Clear(BLUE); - printf(" Touch 测试完成\r\n"); - delay_1ms(500); + for (uint8_t i = 0; i < GT1151::kMaxTouch; i++) { + lcd.setForeground(TP_COLORS[i]); + char lbl[8]; + sprintf(lbl, "P%d: --", i); + lcd.showString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)lbl); } - // ============================================================= - // 主函数 -// ============================================================= + uint16_t last_x[5]; + uint16_t last_y[5]; + bool was_active[5]; + for (uint8_t i = 0; i < GT1151::kMaxTouch; i++) { + last_x[i] = 0xFFFF; + last_y[i] = 0xFFFF; + was_active[i] = false; + } + + uint32_t idle_counter = 0; + GT1151::irq_flag = 0; + + while (1) { + if (GT1151::irq_flag) { + GT1151::irq_flag = 0; + delay_1ms(5); + + touch.scan(0); + + idle_counter = 0; + + uint8_t sta = touch.status(); + uint8_t active_cnt = touch.activeCount(); + uint8_t mask = sta & 0x1F; + + for (uint8_t i = 0; i < GT1151::kMaxTouch; i++) { + bool active = (mask & (1 << i)) != 0; + + if (active) { + uint16_t x = touch.x(i); + uint16_t y = touch.y(i); + + 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) { + lcd.setForeground(TP_COLORS[i]); + lcd.drawLine(last_x[i], last_y[i], x, y); + } + + lcd.setForeground(TP_COLORS[i]); + lcd.fillCircle(x, y, 5); + + last_x[i] = x; + last_y[i] = y; + was_active[i] = true; + } + } else { + if (was_active[i]) { + lcd.setForeground(BG_COLOR); + lcd.fillCircle(last_x[i], last_y[i], 6); + + lcd.setForeground(TP_COLORS[i]); + lcd.fillRectangle(10, 52 + i * 18, 200, 52 + i * 18 + 17); + lcd.showString(10, 52 + i * 18, 200, 18, 16, 0, (uint8_t *)"released"); + + last_x[i] = 0xFFFF; + last_y[i] = 0xFFFF; + was_active[i] = false; + } + } + } + + printf("[TOUCH] cnt=%d sta=0x%02X", active_cnt, sta); + 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 { + idle_counter++; + delay_1ms(10); + if (idle_counter > 500) { + printf(" 无触摸超过 5 秒,退出 Touch 测试\r\n"); + break; + } + } + } + + lcd.clear(Lcd::BLUE); + printf(" Touch 测试完成\r\n"); + delay_1ms(500); +} + int main(void) { - // ---- 早期启动: 16MHz,原始寄存器操作 ---- init_leds(); led_pattern(0x01); @@ -512,7 +483,6 @@ int main(void) uart_putdec(SystemCoreClock); uart_puts(" Hz\r\n"); - // LED 闪烁验证 for (int i = 0; i < 3; i++) { led_on(LED2_PORT, LED2_PIN); delay_16m(200); @@ -521,11 +491,10 @@ int main(void) } uart_puts("LED blink OK\r\n"); - // ---- 硬件初始化: 切换到 168MHz ---- uart_puts("Initializing hardware...\r\n"); led_pattern(0x05); - hardware_init(); + HardwareInit::init(); reinit_usart0_168m(); @@ -537,7 +506,6 @@ int main(void) uart_putdec(SystemCoreClock); uart_puts(" Hz\r\n"); - // ---- C++ 驱动初始化 ---- led_pattern(0x07); LedManager::instance().init_all(); LedManager::instance().led(1).on(); @@ -553,13 +521,11 @@ int main(void) } uart_puts("UartBus init OK\r\n"); - // ---- SysTick 配置 ---- led_pattern(0x0B); systick_config(); delay_1ms(500); uart_puts("SysTick OK, delay_1ms available\r\n"); - // ---- 外设测试 ---- led_pattern(0x0C); printf("\r\n========================================\r\n"); printf(" LSPi 全外设测试\r\n"); @@ -583,7 +549,6 @@ int main(void) test_touch(); delay_1ms(500); - // ---- 主循环 ---- led_pattern(0x0F); printf("\r\n===== 所有外设测试完成 =====\r\n"); printf("系统运行于 168MHz, Flash/SDRAM/LCD/UART/LED 全部正常\r\n"); @@ -598,5 +563,7 @@ int main(void) tick++; if (tick % 4 == 0) printf("."); + if (tick % 80 == 0) + printf(" [%lu s]\r\n", tick / 4); } } diff --git a/bsp/device/lcd/lcd.cpp b/bsp/device/lcd/lcd.cpp index 1c66073..10bc428 100644 --- a/bsp/device/lcd/lcd.cpp +++ b/bsp/device/lcd/lcd.cpp @@ -1,17 +1,133 @@ #include "lcd.h" #include "exmc_driver.h" +#include "systick.h" #include #include "font.h" extern "C" uint32_t SystemCoreClock; -uint16_t POINT_COLOR = 0x0000; -uint16_t BACK_COLOR = 0xFFFF; +Lcd &Lcd::instance() +{ + static Lcd inst; + return inst; +} -_lcd_dev lcddev; -uint8_t lcd_id[12]; +Lcd::Lcd() + : lcddev_{0, 0, 0, 0, 0, 0, 0}, + point_color_(Color::WHITE), + back_color_(Color::BLACK), + id_string_{0} +{ +} -void LCD_CtrlLinesConfig(void) +Lcd::LcdRegs *Lcd::regs() +{ + return reinterpret_cast(0x6C000000 | 0x000007FE); +} + +void Lcd::rstOn() { gpio_bit_set(GPIOD, GPIO_PIN_12); } +void Lcd::rstOff() { gpio_bit_reset(GPIOD, GPIO_PIN_12); } +void Lcd::blkOn() { gpio_bit_set(GPIOD, GPIO_PIN_13); } +void Lcd::blkOff() { gpio_bit_reset(GPIOD, GPIO_PIN_13); } + +uint16_t Lcd::width() const { return lcddev_.width_; } +uint16_t Lcd::height() const { return lcddev_.height_; } +uint16_t Lcd::id() const { return lcddev_.id_; } +const char *Lcd::idString() const { return id_string_; } +uint16_t Lcd::foreground() const { return point_color_; } +uint16_t Lcd::background() const { return back_color_; } +void Lcd::setForeground(uint16_t color) { point_color_ = color; } +void Lcd::setBackground(uint16_t color) { back_color_ = color; } + +void Lcd::wrReg(uint16_t regval) +{ + regs()->LCD_REG = regval; + __DSB(); +} + +void Lcd::wrData(uint16_t data) +{ + regs()->LCD_RAM = data; + __DSB(); +} + +void Lcd::writeRam(uint16_t color) +{ + regs()->LCD_RAM = color; + __DSB(); +} + +void Lcd::writeReg(uint16_t reg, uint16_t value) +{ + regs()->LCD_REG = reg; + __DSB(); + regs()->LCD_RAM = value; + __DSB(); +} + +void Lcd::writeRamPrepare() +{ + regs()->LCD_REG = lcddev_.wramcmd_; + __DSB(); +} + +uint16_t Lcd::rdData() +{ + return regs()->LCD_RAM; +} + +uint16_t Lcd::readReg(uint16_t reg) +{ + wrReg(reg); + __NOP(); __NOP(); __NOP(); __NOP(); + __NOP(); __NOP(); __NOP(); __NOP(); + uint16_t data = rdData(); + printf("[Lcd::readReg] 0x%04X -> 0x%04X\r\n", reg, data); + return data; +} + +void Lcd::readID() +{ + uint16_t id1, id2, id3; + + printf("[LCD ID] Start reading...\r\n"); + delay_1ms(50); + writeReg(0x0000, 0x0001); + delay_1ms(50); + lcddev_.id_ = readReg(0x0000); + printf("[LCD ID] reg0x0000: 0x%04X\r\n", lcddev_.id_); + + printf("[LCD ID] Reset LCD...\r\n"); + rstOn(); + delay_1ms(10); + rstOff(); + delay_1ms(50); + rstOn(); + delay_1ms(120); + + wrReg(0XDA00); + id1 = rdData(); + printf("[LCD ID] ID1: 0x%04X\r\n", id1); + + wrReg(0XDB00); + id2 = rdData(); + printf("[LCD ID] ID2: 0x%04X\r\n", id2); + + if (id2 == 0x8000) + id2 = 0x80; + else if ((id2 >> 8) == 0x80) + id2 >>= 8; + + wrReg(0XDC00); + id3 = rdData(); + printf("[LCD ID] ID3: 0x%04X\r\n", id3); + + lcddev_.id_ = (id2 << 8) | (id3 & 0xFF); + printf("NT35510 LCD ID:%04X\r\n", lcddev_.id_); + sprintf(id_string_, "LCD ID:%04X", lcddev_.id_); +} + +void Lcd::ctrlLinesConfig() { rcu_periph_clock_enable(RCU_GPIOD); gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, GPIO_PIN_13); @@ -20,7 +136,7 @@ void LCD_CtrlLinesConfig(void) gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12); } -void LCD_FSMCConfig(void) +void Lcd::fsmcConfig() { exmc_norsram_parameter_struct nor_init_struct; exmc_norsram_timing_parameter_struct readWriteTiming; @@ -39,7 +155,7 @@ void LCD_FSMCConfig(void) if (SystemCoreClock > 100000000) { - printf("[FSMC] 168MHz时序\r\n"); + printf("[FSMC] 168MHz timing\r\n"); readWriteTiming.asyn_data_setuptime = 61; readWriteTiming.asyn_address_setuptime = 15; writeTiming.asyn_data_setuptime = 5; @@ -47,7 +163,7 @@ void LCD_FSMCConfig(void) } else { - printf("[FSMC] 16MHz时序\r\n"); + printf("[FSMC] 16MHz timing\r\n"); readWriteTiming.asyn_data_setuptime = 6; readWriteTiming.asyn_address_setuptime = 2; writeTiming.asyn_data_setuptime = 1; @@ -73,631 +189,536 @@ void LCD_FSMCConfig(void) exmc_norsram_enable(EXMC_BANK0_NORSRAM_REGION3); } -void LCD_WR_REG(uint16_t regval) +void Lcd::hvgaConfig() { - LCD->LCD_REG = regval; - __DSB(); + lcddev_.width_ = 480; + lcddev_.height_ = 800; + lcddev_.setxcmd_ = 0X2A00; + lcddev_.setycmd_ = 0X2B00; + lcddev_.wramcmd_ = 0X2C00; } -void LCD_WR_DATA(uint16_t data) +void Lcd::initialCode() { - LCD->LCD_RAM = data; - __DSB(); -} + writeReg(0xF000, 0x55); + writeReg(0xF001, 0xAA); + writeReg(0xF002, 0x52); + writeReg(0xF003, 0x08); + writeReg(0xF004, 0x01); + writeReg(0xB000, 0x0D); + writeReg(0xB001, 0x0D); + writeReg(0xB002, 0x0D); + writeReg(0xB600, 0x34); + writeReg(0xB601, 0x34); + writeReg(0xB602, 0x34); + writeReg(0xB100, 0x0D); + writeReg(0xB101, 0x0D); + writeReg(0xB102, 0x0D); + writeReg(0xB700, 0x34); + writeReg(0xB701, 0x34); + writeReg(0xB702, 0x34); + writeReg(0xB200, 0x00); + writeReg(0xB201, 0x00); + writeReg(0xB202, 0x00); + writeReg(0xB800, 0x24); + writeReg(0xB801, 0x24); + writeReg(0xB802, 0x24); + writeReg(0xBF00, 0x01); + writeReg(0xB300, 0x0F); + writeReg(0xB301, 0x0F); + writeReg(0xB302, 0x0F); + writeReg(0xB900, 0x34); + writeReg(0xB901, 0x34); + writeReg(0xB902, 0x34); + writeReg(0xB500, 0x08); + writeReg(0xB501, 0x08); + writeReg(0xB502, 0x08); + writeReg(0xC200, 0x03); + writeReg(0xBA00, 0x24); + writeReg(0xBA01, 0x24); + writeReg(0xBA02, 0x24); + writeReg(0xBC00, 0x00); + writeReg(0xBC01, 0x78); + writeReg(0xBC02, 0x00); + writeReg(0xBD00, 0x00); + writeReg(0xBD01, 0x78); + writeReg(0xBD02, 0x00); + writeReg(0xBE00, 0x00); + writeReg(0xBE01, 0x64); + writeReg(0xD100, 0x00); + writeReg(0xD101, 0x33); + writeReg(0xD102, 0x00); + writeReg(0xD103, 0x34); + writeReg(0xD104, 0x00); + writeReg(0xD105, 0x3A); + writeReg(0xD106, 0x00); + writeReg(0xD107, 0x4A); + writeReg(0xD108, 0x00); + writeReg(0xD109, 0x5C); + writeReg(0xD10A, 0x00); + writeReg(0xD10B, 0x81); + writeReg(0xD10C, 0x00); + writeReg(0xD10D, 0xA6); + writeReg(0xD10E, 0x00); + writeReg(0xD10F, 0xE5); + writeReg(0xD110, 0x01); + writeReg(0xD111, 0x13); + writeReg(0xD112, 0x01); + writeReg(0xD113, 0x54); + writeReg(0xD114, 0x01); + writeReg(0xD115, 0x82); + writeReg(0xD116, 0x01); + writeReg(0xD117, 0xCA); + writeReg(0xD118, 0x02); + writeReg(0xD119, 0x00); + writeReg(0xD11A, 0x02); + writeReg(0xD11B, 0x01); + writeReg(0xD11C, 0x02); + writeReg(0xD11D, 0x34); + writeReg(0xD11E, 0x02); + writeReg(0xD11F, 0x67); + writeReg(0xD120, 0x02); + writeReg(0xD121, 0x84); + writeReg(0xD122, 0x02); + writeReg(0xD123, 0xA4); + writeReg(0xD124, 0x02); + writeReg(0xD125, 0xB7); + writeReg(0xD126, 0x02); + writeReg(0xD127, 0xCF); + writeReg(0xD128, 0x02); + writeReg(0xD129, 0xDE); + writeReg(0xD12A, 0x02); + writeReg(0xD12B, 0xF2); + writeReg(0xD12C, 0x02); + writeReg(0xD12D, 0xFE); + writeReg(0xD12E, 0x03); + writeReg(0xD12F, 0x10); + writeReg(0xD130, 0x03); + writeReg(0xD131, 0x33); + writeReg(0xD132, 0x03); + writeReg(0xD133, 0x6D); + writeReg(0xD200, 0x00); + writeReg(0xD201, 0x33); + writeReg(0xD202, 0x00); + writeReg(0xD203, 0x34); + writeReg(0xD204, 0x00); + writeReg(0xD205, 0x3A); + writeReg(0xD206, 0x00); + writeReg(0xD207, 0x4A); + writeReg(0xD208, 0x00); + writeReg(0xD209, 0x5C); + writeReg(0xD20A, 0x00); + writeReg(0xD20B, 0x81); + writeReg(0xD20C, 0x00); + writeReg(0xD20D, 0xA6); + writeReg(0xD20E, 0x00); + writeReg(0xD20F, 0xE5); + writeReg(0xD210, 0x01); + writeReg(0xD211, 0x13); + writeReg(0xD212, 0x01); + writeReg(0xD213, 0x54); + writeReg(0xD214, 0x01); + writeReg(0xD215, 0x82); + writeReg(0xD216, 0x01); + writeReg(0xD217, 0xCA); + writeReg(0xD218, 0x02); + writeReg(0xD219, 0x00); + writeReg(0xD21A, 0x02); + writeReg(0xD21B, 0x01); + writeReg(0xD21C, 0x02); + writeReg(0xD21D, 0x34); + writeReg(0xD21E, 0x02); + writeReg(0xD21F, 0x67); + writeReg(0xD220, 0x02); + writeReg(0xD221, 0x84); + writeReg(0xD222, 0x02); + writeReg(0xD223, 0xA4); + writeReg(0xD224, 0x02); + writeReg(0xD225, 0xB7); + writeReg(0xD226, 0x02); + writeReg(0xD227, 0xCF); + writeReg(0xD228, 0x02); + writeReg(0xD229, 0xDE); + writeReg(0xD22A, 0x02); + writeReg(0xD22B, 0xF2); + writeReg(0xD22C, 0x02); + writeReg(0xD22D, 0xFE); + writeReg(0xD22E, 0x03); + writeReg(0xD22F, 0x10); + writeReg(0xD230, 0x03); + writeReg(0xD231, 0x33); + writeReg(0xD232, 0x03); + writeReg(0xD233, 0x6D); + writeReg(0xD300, 0x00); + writeReg(0xD301, 0x33); + writeReg(0xD302, 0x00); + writeReg(0xD303, 0x34); + writeReg(0xD304, 0x00); + writeReg(0xD305, 0x3A); + writeReg(0xD306, 0x00); + writeReg(0xD307, 0x4A); + writeReg(0xD308, 0x00); + writeReg(0xD309, 0x5C); + writeReg(0xD30A, 0x00); + writeReg(0xD30B, 0x81); + writeReg(0xD30C, 0x00); + writeReg(0xD30D, 0xA6); + writeReg(0xD30E, 0x00); + writeReg(0xD30F, 0xE5); + writeReg(0xD310, 0x01); + writeReg(0xD311, 0x13); + writeReg(0xD312, 0x01); + writeReg(0xD313, 0x54); + writeReg(0xD314, 0x01); + writeReg(0xD315, 0x82); + writeReg(0xD316, 0x01); + writeReg(0xD317, 0xCA); + writeReg(0xD318, 0x02); + writeReg(0xD319, 0x00); + writeReg(0xD31A, 0x02); + writeReg(0xD31B, 0x01); + writeReg(0xD31C, 0x02); + writeReg(0xD31D, 0x34); + writeReg(0xD31E, 0x02); + writeReg(0xD31F, 0x67); + writeReg(0xD320, 0x02); + writeReg(0xD321, 0x84); + writeReg(0xD322, 0x02); + writeReg(0xD323, 0xA4); + writeReg(0xD324, 0x02); + writeReg(0xD325, 0xB7); + writeReg(0xD326, 0x02); + writeReg(0xD327, 0xCF); + writeReg(0xD328, 0x02); + writeReg(0xD329, 0xDE); + writeReg(0xD32A, 0x02); + writeReg(0xD32B, 0xF2); + writeReg(0xD32C, 0x02); + writeReg(0xD32D, 0xFE); + writeReg(0xD32E, 0x03); + writeReg(0xD32F, 0x10); + writeReg(0xD330, 0x03); + writeReg(0xD331, 0x33); + writeReg(0xD332, 0x03); + writeReg(0xD333, 0x6D); + writeReg(0xD400, 0x00); + writeReg(0xD401, 0x33); + writeReg(0xD402, 0x00); + writeReg(0xD403, 0x34); + writeReg(0xD404, 0x00); + writeReg(0xD405, 0x3A); + writeReg(0xD406, 0x00); + writeReg(0xD407, 0x4A); + writeReg(0xD408, 0x00); + writeReg(0xD409, 0x5C); + writeReg(0xD40A, 0x00); + writeReg(0xD40B, 0x81); + writeReg(0xD40C, 0x00); + writeReg(0xD40D, 0xA6); + writeReg(0xD40E, 0x00); + writeReg(0xD40F, 0xE5); + writeReg(0xD410, 0x01); + writeReg(0xD411, 0x13); + writeReg(0xD412, 0x01); + writeReg(0xD413, 0x54); + writeReg(0xD414, 0x01); + writeReg(0xD415, 0x82); + writeReg(0xD416, 0x01); + writeReg(0xD417, 0xCA); + writeReg(0xD418, 0x02); + writeReg(0xD419, 0x00); + writeReg(0xD41A, 0x02); + writeReg(0xD41B, 0x01); + writeReg(0xD41C, 0x02); + writeReg(0xD41D, 0x34); + writeReg(0xD41E, 0x02); + writeReg(0xD41F, 0x67); + writeReg(0xD420, 0x02); + writeReg(0xD421, 0x84); + writeReg(0xD422, 0x02); + writeReg(0xD423, 0xA4); + writeReg(0xD424, 0x02); + writeReg(0xD425, 0xB7); + writeReg(0xD426, 0x02); + writeReg(0xD427, 0xCF); + writeReg(0xD428, 0x02); + writeReg(0xD429, 0xDE); + writeReg(0xD42A, 0x02); + writeReg(0xD42B, 0xF2); + writeReg(0xD42C, 0x02); + writeReg(0xD42D, 0xFE); + writeReg(0xD42E, 0x03); + writeReg(0xD42F, 0x10); + writeReg(0xD430, 0x03); + writeReg(0xD431, 0x33); + writeReg(0xD432, 0x03); + writeReg(0xD433, 0x6D); + writeReg(0xD500, 0x00); + writeReg(0xD501, 0x33); + writeReg(0xD502, 0x00); + writeReg(0xD503, 0x34); + writeReg(0xD504, 0x00); + writeReg(0xD505, 0x3A); + writeReg(0xD506, 0x00); + writeReg(0xD507, 0x4A); + writeReg(0xD508, 0x00); + writeReg(0xD509, 0x5C); + writeReg(0xD50A, 0x00); + writeReg(0xD50B, 0x81); + writeReg(0xD50C, 0x00); + writeReg(0xD50D, 0xA6); + writeReg(0xD50E, 0x00); + writeReg(0xD50F, 0xE5); + writeReg(0xD510, 0x01); + writeReg(0xD511, 0x13); + writeReg(0xD512, 0x01); + writeReg(0xD513, 0x54); + writeReg(0xD514, 0x01); + writeReg(0xD515, 0x82); + writeReg(0xD516, 0x01); + writeReg(0xD517, 0xCA); + writeReg(0xD518, 0x02); + writeReg(0xD519, 0x00); + writeReg(0xD51A, 0x02); + writeReg(0xD51B, 0x01); + writeReg(0xD51C, 0x02); + writeReg(0xD51D, 0x34); + writeReg(0xD51E, 0x02); + writeReg(0xD51F, 0x67); + writeReg(0xD520, 0x02); + writeReg(0xD521, 0x84); + writeReg(0xD522, 0x02); + writeReg(0xD523, 0xA4); + writeReg(0xD524, 0x02); + writeReg(0xD525, 0xB7); + writeReg(0xD526, 0x02); + writeReg(0xD527, 0xCF); + writeReg(0xD528, 0x02); + writeReg(0xD529, 0xDE); + writeReg(0xD52A, 0x02); + writeReg(0xD52B, 0xF2); + writeReg(0xD52C, 0x02); + writeReg(0xD52D, 0xFE); + writeReg(0xD52E, 0x03); + writeReg(0xD52F, 0x10); + writeReg(0xD530, 0x03); + writeReg(0xD531, 0x33); + writeReg(0xD532, 0x03); + writeReg(0xD533, 0x6D); + writeReg(0xD600, 0x00); + writeReg(0xD601, 0x33); + writeReg(0xD602, 0x00); + writeReg(0xD603, 0x34); + writeReg(0xD604, 0x00); + writeReg(0xD605, 0x3A); + writeReg(0xD606, 0x00); + writeReg(0xD607, 0x4A); + writeReg(0xD608, 0x00); + writeReg(0xD609, 0x5C); + writeReg(0xD60A, 0x00); + writeReg(0xD60B, 0x81); + writeReg(0xD60C, 0x00); + writeReg(0xD60D, 0xA6); + writeReg(0xD60E, 0x00); + writeReg(0xD60F, 0xE5); + writeReg(0xD610, 0x01); + writeReg(0xD611, 0x13); + writeReg(0xD612, 0x01); + writeReg(0xD613, 0x54); + writeReg(0xD614, 0x01); + writeReg(0xD615, 0x82); + writeReg(0xD616, 0x01); + writeReg(0xD617, 0xCA); + writeReg(0xD618, 0x02); + writeReg(0xD619, 0x00); + writeReg(0xD61A, 0x02); + writeReg(0xD61B, 0x01); + writeReg(0xD61C, 0x02); + writeReg(0xD61D, 0x34); + writeReg(0xD61E, 0x02); + writeReg(0xD61F, 0x67); + writeReg(0xD620, 0x02); + writeReg(0xD621, 0x84); + writeReg(0xD622, 0x02); + writeReg(0xD623, 0xA4); + writeReg(0xD624, 0x02); + writeReg(0xD625, 0xB7); + writeReg(0xD626, 0x02); + writeReg(0xD627, 0xCF); + writeReg(0xD628, 0x02); + writeReg(0xD629, 0xDE); + writeReg(0xD62A, 0x02); + writeReg(0xD62B, 0xF2); + writeReg(0xD62C, 0x02); + writeReg(0xD62D, 0xFE); + writeReg(0xD62E, 0x03); + writeReg(0xD62F, 0x10); + writeReg(0xD630, 0x03); + writeReg(0xD631, 0x33); + writeReg(0xD632, 0x03); + writeReg(0xD633, 0x6D); + writeReg(0xF000, 0x55); + writeReg(0xF001, 0xAA); + writeReg(0xF002, 0x52); + writeReg(0xF003, 0x08); + writeReg(0xF004, 0x00); + writeReg(0xB100, 0xCC); + writeReg(0xB101, 0x00); + writeReg(0xB600, 0x05); + writeReg(0xB700, 0x70); + writeReg(0xB701, 0x70); + writeReg(0xB800, 0x01); + writeReg(0xB801, 0x03); + writeReg(0xB802, 0x03); + writeReg(0xB803, 0x03); + writeReg(0xBC00, 0x02); + writeReg(0xBC01, 0x00); + writeReg(0xBC02, 0x00); + writeReg(0xC900, 0xD0); + writeReg(0xC901, 0x02); + writeReg(0xC902, 0x50); + writeReg(0xC903, 0x50); + writeReg(0xC904, 0x50); + writeReg(0x3500, 0x00); + writeReg(0x3A00, 0x55); -void LCD_WriteRAM(uint16_t RGB_Code) -{ - LCD->LCD_RAM = RGB_Code; - __DSB(); -} - -void LCD_WriteReg(uint16_t LCD_Reg, uint16_t LCD_RegValue) -{ - LCD->LCD_REG = LCD_Reg; - __DSB(); - LCD->LCD_RAM = LCD_RegValue; - __DSB(); -} - -void LCD_WriteRAM_Prepare(void) -{ - LCD->LCD_REG = lcddev.wramcmd; - __DSB(); -} - -uint16_t LCD_RD_DATA(void) -{ - return LCD->LCD_RAM; -} - -void delay_us(uint16_t us) -{ - while (us--) - { - __NOP(); - __NOP(); - __NOP(); - __NOP(); - __NOP(); - __NOP(); - } -} - -uint16_t LCD_ReadReg(uint16_t LCD_Reg) -{ - LCD_WR_REG(LCD_Reg); - delay_us(20); - uint16_t data = LCD_RD_DATA(); - printf("[LCD_ReadReg] 0x%04X -> 0x%04X\r\n", LCD_Reg, data); - return data; -} - -void LcdNT35510ReadID(void) -{ - uint16_t id1, id2, id3; - - printf("[LCD ID] 开始读取...\r\n"); - delay_1ms(50); - LCD_WriteReg(0x0000, 0x0001); - delay_1ms(50); - lcddev.id = LCD_ReadReg(0x0000); - printf("[LCD ID] reg0x0000: 0x%04X\r\n", lcddev.id); - - printf("[LCD ID] 复位LCD...\r\n"); - LCD_RST_ON; - delay_1ms(10); - LCD_RST_OFF; - delay_1ms(50); - LCD_RST_ON; + wrReg(0x1100); delay_1ms(120); - - LCD_WR_REG(0XDA00); - id1 = LCD_RD_DATA(); - printf("[LCD ID] ID1: 0x%04X\r\n", id1); - - LCD_WR_REG(0XDB00); - id2 = LCD_RD_DATA(); - printf("[LCD ID] ID2: 0x%04X\r\n", id2); - - if (id2 == 0x8000) - id2 = 0x80; - else if ((id2 >> 8) == 0x80) - id2 >>= 8; - - LCD_WR_REG(0XDC00); - id3 = LCD_RD_DATA(); - printf("[LCD ID] ID3: 0x%04X\r\n", id3); - - lcddev.id = (id2 << 8) | (id3 & 0xFF); - printf("NT35510 LCD ID:%04X\r\n", lcddev.id); - sprintf((char *)lcd_id, "LCD ID:%04X", lcddev.id); -} - -void LCD_HVGA_NT35510(void) -{ - lcddev.width = 480; - lcddev.height = 800; - lcddev.setxcmd = 0X2A00; - lcddev.setycmd = 0X2B00; - lcddev.wramcmd = 0X2C00; -} - -void NT35510_HY35_Initial_Code(void) -{ - LCD_WriteReg(0xF000, 0x55); - LCD_WriteReg(0xF001, 0xAA); - LCD_WriteReg(0xF002, 0x52); - LCD_WriteReg(0xF003, 0x08); - LCD_WriteReg(0xF004, 0x01); - LCD_WriteReg(0xB000, 0x0D); - LCD_WriteReg(0xB001, 0x0D); - LCD_WriteReg(0xB002, 0x0D); - LCD_WriteReg(0xB600, 0x34); - LCD_WriteReg(0xB601, 0x34); - LCD_WriteReg(0xB602, 0x34); - LCD_WriteReg(0xB100, 0x0D); - LCD_WriteReg(0xB101, 0x0D); - LCD_WriteReg(0xB102, 0x0D); - LCD_WriteReg(0xB700, 0x34); - LCD_WriteReg(0xB701, 0x34); - LCD_WriteReg(0xB702, 0x34); - LCD_WriteReg(0xB200, 0x00); - LCD_WriteReg(0xB201, 0x00); - LCD_WriteReg(0xB202, 0x00); - LCD_WriteReg(0xB800, 0x24); - LCD_WriteReg(0xB801, 0x24); - LCD_WriteReg(0xB802, 0x24); - LCD_WriteReg(0xBF00, 0x01); - LCD_WriteReg(0xB300, 0x0F); - LCD_WriteReg(0xB301, 0x0F); - LCD_WriteReg(0xB302, 0x0F); - LCD_WriteReg(0xB900, 0x34); - LCD_WriteReg(0xB901, 0x34); - LCD_WriteReg(0xB902, 0x34); - LCD_WriteReg(0xB500, 0x08); - LCD_WriteReg(0xB501, 0x08); - LCD_WriteReg(0xB502, 0x08); - LCD_WriteReg(0xC200, 0x03); - LCD_WriteReg(0xBA00, 0x24); - LCD_WriteReg(0xBA01, 0x24); - LCD_WriteReg(0xBA02, 0x24); - LCD_WriteReg(0xBC00, 0x00); - LCD_WriteReg(0xBC01, 0x78); - LCD_WriteReg(0xBC02, 0x00); - LCD_WriteReg(0xBD00, 0x00); - LCD_WriteReg(0xBD01, 0x78); - LCD_WriteReg(0xBD02, 0x00); - LCD_WriteReg(0xBE00, 0x00); - LCD_WriteReg(0xBE01, 0x64); - LCD_WriteReg(0xD100, 0x00); - LCD_WriteReg(0xD101, 0x33); - LCD_WriteReg(0xD102, 0x00); - LCD_WriteReg(0xD103, 0x34); - LCD_WriteReg(0xD104, 0x00); - LCD_WriteReg(0xD105, 0x3A); - LCD_WriteReg(0xD106, 0x00); - LCD_WriteReg(0xD107, 0x4A); - LCD_WriteReg(0xD108, 0x00); - LCD_WriteReg(0xD109, 0x5C); - LCD_WriteReg(0xD10A, 0x00); - LCD_WriteReg(0xD10B, 0x81); - LCD_WriteReg(0xD10C, 0x00); - LCD_WriteReg(0xD10D, 0xA6); - LCD_WriteReg(0xD10E, 0x00); - LCD_WriteReg(0xD10F, 0xE5); - LCD_WriteReg(0xD110, 0x01); - LCD_WriteReg(0xD111, 0x13); - LCD_WriteReg(0xD112, 0x01); - LCD_WriteReg(0xD113, 0x54); - LCD_WriteReg(0xD114, 0x01); - LCD_WriteReg(0xD115, 0x82); - LCD_WriteReg(0xD116, 0x01); - LCD_WriteReg(0xD117, 0xCA); - LCD_WriteReg(0xD118, 0x02); - LCD_WriteReg(0xD119, 0x00); - LCD_WriteReg(0xD11A, 0x02); - LCD_WriteReg(0xD11B, 0x01); - LCD_WriteReg(0xD11C, 0x02); - LCD_WriteReg(0xD11D, 0x34); - LCD_WriteReg(0xD11E, 0x02); - LCD_WriteReg(0xD11F, 0x67); - LCD_WriteReg(0xD120, 0x02); - LCD_WriteReg(0xD121, 0x84); - LCD_WriteReg(0xD122, 0x02); - LCD_WriteReg(0xD123, 0xA4); - LCD_WriteReg(0xD124, 0x02); - LCD_WriteReg(0xD125, 0xB7); - LCD_WriteReg(0xD126, 0x02); - LCD_WriteReg(0xD127, 0xCF); - LCD_WriteReg(0xD128, 0x02); - LCD_WriteReg(0xD129, 0xDE); - LCD_WriteReg(0xD12A, 0x02); - LCD_WriteReg(0xD12B, 0xF2); - LCD_WriteReg(0xD12C, 0x02); - LCD_WriteReg(0xD12D, 0xFE); - LCD_WriteReg(0xD12E, 0x03); - LCD_WriteReg(0xD12F, 0x10); - LCD_WriteReg(0xD130, 0x03); - LCD_WriteReg(0xD131, 0x33); - LCD_WriteReg(0xD132, 0x03); - LCD_WriteReg(0xD133, 0x6D); - LCD_WriteReg(0xD200, 0x00); - LCD_WriteReg(0xD201, 0x33); - LCD_WriteReg(0xD202, 0x00); - LCD_WriteReg(0xD203, 0x34); - LCD_WriteReg(0xD204, 0x00); - LCD_WriteReg(0xD205, 0x3A); - LCD_WriteReg(0xD206, 0x00); - LCD_WriteReg(0xD207, 0x4A); - LCD_WriteReg(0xD208, 0x00); - LCD_WriteReg(0xD209, 0x5C); - LCD_WriteReg(0xD20A, 0x00); - LCD_WriteReg(0xD20B, 0x81); - LCD_WriteReg(0xD20C, 0x00); - LCD_WriteReg(0xD20D, 0xA6); - LCD_WriteReg(0xD20E, 0x00); - LCD_WriteReg(0xD20F, 0xE5); - LCD_WriteReg(0xD210, 0x01); - LCD_WriteReg(0xD211, 0x13); - LCD_WriteReg(0xD212, 0x01); - LCD_WriteReg(0xD213, 0x54); - LCD_WriteReg(0xD214, 0x01); - LCD_WriteReg(0xD215, 0x82); - LCD_WriteReg(0xD216, 0x01); - LCD_WriteReg(0xD217, 0xCA); - LCD_WriteReg(0xD218, 0x02); - LCD_WriteReg(0xD219, 0x00); - LCD_WriteReg(0xD21A, 0x02); - LCD_WriteReg(0xD21B, 0x01); - LCD_WriteReg(0xD21C, 0x02); - LCD_WriteReg(0xD21D, 0x34); - LCD_WriteReg(0xD21E, 0x02); - LCD_WriteReg(0xD21F, 0x67); - LCD_WriteReg(0xD220, 0x02); - LCD_WriteReg(0xD221, 0x84); - LCD_WriteReg(0xD222, 0x02); - LCD_WriteReg(0xD223, 0xA4); - LCD_WriteReg(0xD224, 0x02); - LCD_WriteReg(0xD225, 0xB7); - LCD_WriteReg(0xD226, 0x02); - LCD_WriteReg(0xD227, 0xCF); - LCD_WriteReg(0xD228, 0x02); - LCD_WriteReg(0xD229, 0xDE); - LCD_WriteReg(0xD22A, 0x02); - LCD_WriteReg(0xD22B, 0xF2); - LCD_WriteReg(0xD22C, 0x02); - LCD_WriteReg(0xD22D, 0xFE); - LCD_WriteReg(0xD22E, 0x03); - LCD_WriteReg(0xD22F, 0x10); - LCD_WriteReg(0xD230, 0x03); - LCD_WriteReg(0xD231, 0x33); - LCD_WriteReg(0xD232, 0x03); - LCD_WriteReg(0xD233, 0x6D); - LCD_WriteReg(0xD300, 0x00); - LCD_WriteReg(0xD301, 0x33); - LCD_WriteReg(0xD302, 0x00); - LCD_WriteReg(0xD303, 0x34); - LCD_WriteReg(0xD304, 0x00); - LCD_WriteReg(0xD305, 0x3A); - LCD_WriteReg(0xD306, 0x00); - LCD_WriteReg(0xD307, 0x4A); - LCD_WriteReg(0xD308, 0x00); - LCD_WriteReg(0xD309, 0x5C); - LCD_WriteReg(0xD30A, 0x00); - LCD_WriteReg(0xD30B, 0x81); - LCD_WriteReg(0xD30C, 0x00); - LCD_WriteReg(0xD30D, 0xA6); - LCD_WriteReg(0xD30E, 0x00); - LCD_WriteReg(0xD30F, 0xE5); - LCD_WriteReg(0xD310, 0x01); - LCD_WriteReg(0xD311, 0x13); - LCD_WriteReg(0xD312, 0x01); - LCD_WriteReg(0xD313, 0x54); - LCD_WriteReg(0xD314, 0x01); - LCD_WriteReg(0xD315, 0x82); - LCD_WriteReg(0xD316, 0x01); - LCD_WriteReg(0xD317, 0xCA); - LCD_WriteReg(0xD318, 0x02); - LCD_WriteReg(0xD319, 0x00); - LCD_WriteReg(0xD31A, 0x02); - LCD_WriteReg(0xD31B, 0x01); - LCD_WriteReg(0xD31C, 0x02); - LCD_WriteReg(0xD31D, 0x34); - LCD_WriteReg(0xD31E, 0x02); - LCD_WriteReg(0xD31F, 0x67); - LCD_WriteReg(0xD320, 0x02); - LCD_WriteReg(0xD321, 0x84); - LCD_WriteReg(0xD322, 0x02); - LCD_WriteReg(0xD323, 0xA4); - LCD_WriteReg(0xD324, 0x02); - LCD_WriteReg(0xD325, 0xB7); - LCD_WriteReg(0xD326, 0x02); - LCD_WriteReg(0xD327, 0xCF); - LCD_WriteReg(0xD328, 0x02); - LCD_WriteReg(0xD329, 0xDE); - LCD_WriteReg(0xD32A, 0x02); - LCD_WriteReg(0xD32B, 0xF2); - LCD_WriteReg(0xD32C, 0x02); - LCD_WriteReg(0xD32D, 0xFE); - LCD_WriteReg(0xD32E, 0x03); - LCD_WriteReg(0xD32F, 0x10); - LCD_WriteReg(0xD330, 0x03); - LCD_WriteReg(0xD331, 0x33); - LCD_WriteReg(0xD332, 0x03); - LCD_WriteReg(0xD333, 0x6D); - LCD_WriteReg(0xD400, 0x00); - LCD_WriteReg(0xD401, 0x33); - LCD_WriteReg(0xD402, 0x00); - LCD_WriteReg(0xD403, 0x34); - LCD_WriteReg(0xD404, 0x00); - LCD_WriteReg(0xD405, 0x3A); - LCD_WriteReg(0xD406, 0x00); - LCD_WriteReg(0xD407, 0x4A); - LCD_WriteReg(0xD408, 0x00); - LCD_WriteReg(0xD409, 0x5C); - LCD_WriteReg(0xD40A, 0x00); - LCD_WriteReg(0xD40B, 0x81); - LCD_WriteReg(0xD40C, 0x00); - LCD_WriteReg(0xD40D, 0xA6); - LCD_WriteReg(0xD40E, 0x00); - LCD_WriteReg(0xD40F, 0xE5); - LCD_WriteReg(0xD410, 0x01); - LCD_WriteReg(0xD411, 0x13); - LCD_WriteReg(0xD412, 0x01); - LCD_WriteReg(0xD413, 0x54); - LCD_WriteReg(0xD414, 0x01); - LCD_WriteReg(0xD415, 0x82); - LCD_WriteReg(0xD416, 0x01); - LCD_WriteReg(0xD417, 0xCA); - LCD_WriteReg(0xD418, 0x02); - LCD_WriteReg(0xD419, 0x00); - LCD_WriteReg(0xD41A, 0x02); - LCD_WriteReg(0xD41B, 0x01); - LCD_WriteReg(0xD41C, 0x02); - LCD_WriteReg(0xD41D, 0x34); - LCD_WriteReg(0xD41E, 0x02); - LCD_WriteReg(0xD41F, 0x67); - LCD_WriteReg(0xD420, 0x02); - LCD_WriteReg(0xD421, 0x84); - LCD_WriteReg(0xD422, 0x02); - LCD_WriteReg(0xD423, 0xA4); - LCD_WriteReg(0xD424, 0x02); - LCD_WriteReg(0xD425, 0xB7); - LCD_WriteReg(0xD426, 0x02); - LCD_WriteReg(0xD427, 0xCF); - LCD_WriteReg(0xD428, 0x02); - LCD_WriteReg(0xD429, 0xDE); - LCD_WriteReg(0xD42A, 0x02); - LCD_WriteReg(0xD42B, 0xF2); - LCD_WriteReg(0xD42C, 0x02); - LCD_WriteReg(0xD42D, 0xFE); - LCD_WriteReg(0xD42E, 0x03); - LCD_WriteReg(0xD42F, 0x10); - LCD_WriteReg(0xD430, 0x03); - LCD_WriteReg(0xD431, 0x33); - LCD_WriteReg(0xD432, 0x03); - LCD_WriteReg(0xD433, 0x6D); - LCD_WriteReg(0xD500, 0x00); - LCD_WriteReg(0xD501, 0x33); - LCD_WriteReg(0xD502, 0x00); - LCD_WriteReg(0xD503, 0x34); - LCD_WriteReg(0xD504, 0x00); - LCD_WriteReg(0xD505, 0x3A); - LCD_WriteReg(0xD506, 0x00); - LCD_WriteReg(0xD507, 0x4A); - LCD_WriteReg(0xD508, 0x00); - LCD_WriteReg(0xD509, 0x5C); - LCD_WriteReg(0xD50A, 0x00); - LCD_WriteReg(0xD50B, 0x81); - LCD_WriteReg(0xD50C, 0x00); - LCD_WriteReg(0xD50D, 0xA6); - LCD_WriteReg(0xD50E, 0x00); - LCD_WriteReg(0xD50F, 0xE5); - LCD_WriteReg(0xD510, 0x01); - LCD_WriteReg(0xD511, 0x13); - LCD_WriteReg(0xD512, 0x01); - LCD_WriteReg(0xD513, 0x54); - LCD_WriteReg(0xD514, 0x01); - LCD_WriteReg(0xD515, 0x82); - LCD_WriteReg(0xD516, 0x01); - LCD_WriteReg(0xD517, 0xCA); - LCD_WriteReg(0xD518, 0x02); - LCD_WriteReg(0xD519, 0x00); - LCD_WriteReg(0xD51A, 0x02); - LCD_WriteReg(0xD51B, 0x01); - LCD_WriteReg(0xD51C, 0x02); - LCD_WriteReg(0xD51D, 0x34); - LCD_WriteReg(0xD51E, 0x02); - LCD_WriteReg(0xD51F, 0x67); - LCD_WriteReg(0xD520, 0x02); - LCD_WriteReg(0xD521, 0x84); - LCD_WriteReg(0xD522, 0x02); - LCD_WriteReg(0xD523, 0xA4); - LCD_WriteReg(0xD524, 0x02); - LCD_WriteReg(0xD525, 0xB7); - LCD_WriteReg(0xD526, 0x02); - LCD_WriteReg(0xD527, 0xCF); - LCD_WriteReg(0xD528, 0x02); - LCD_WriteReg(0xD529, 0xDE); - LCD_WriteReg(0xD52A, 0x02); - LCD_WriteReg(0xD52B, 0xF2); - LCD_WriteReg(0xD52C, 0x02); - LCD_WriteReg(0xD52D, 0xFE); - LCD_WriteReg(0xD52E, 0x03); - LCD_WriteReg(0xD52F, 0x10); - LCD_WriteReg(0xD530, 0x03); - LCD_WriteReg(0xD531, 0x33); - LCD_WriteReg(0xD532, 0x03); - LCD_WriteReg(0xD533, 0x6D); - LCD_WriteReg(0xD600, 0x00); - LCD_WriteReg(0xD601, 0x33); - LCD_WriteReg(0xD602, 0x00); - LCD_WriteReg(0xD603, 0x34); - LCD_WriteReg(0xD604, 0x00); - LCD_WriteReg(0xD605, 0x3A); - LCD_WriteReg(0xD606, 0x00); - LCD_WriteReg(0xD607, 0x4A); - LCD_WriteReg(0xD608, 0x00); - LCD_WriteReg(0xD609, 0x5C); - LCD_WriteReg(0xD60A, 0x00); - LCD_WriteReg(0xD60B, 0x81); - LCD_WriteReg(0xD60C, 0x00); - LCD_WriteReg(0xD60D, 0xA6); - LCD_WriteReg(0xD60E, 0x00); - LCD_WriteReg(0xD60F, 0xE5); - LCD_WriteReg(0xD610, 0x01); - LCD_WriteReg(0xD611, 0x13); - LCD_WriteReg(0xD612, 0x01); - LCD_WriteReg(0xD613, 0x54); - LCD_WriteReg(0xD614, 0x01); - LCD_WriteReg(0xD615, 0x82); - LCD_WriteReg(0xD616, 0x01); - LCD_WriteReg(0xD617, 0xCA); - LCD_WriteReg(0xD618, 0x02); - LCD_WriteReg(0xD619, 0x00); - LCD_WriteReg(0xD61A, 0x02); - LCD_WriteReg(0xD61B, 0x01); - LCD_WriteReg(0xD61C, 0x02); - LCD_WriteReg(0xD61D, 0x34); - LCD_WriteReg(0xD61E, 0x02); - LCD_WriteReg(0xD61F, 0x67); - LCD_WriteReg(0xD620, 0x02); - LCD_WriteReg(0xD621, 0x84); - LCD_WriteReg(0xD622, 0x02); - LCD_WriteReg(0xD623, 0xA4); - LCD_WriteReg(0xD624, 0x02); - LCD_WriteReg(0xD625, 0xB7); - LCD_WriteReg(0xD626, 0x02); - LCD_WriteReg(0xD627, 0xCF); - LCD_WriteReg(0xD628, 0x02); - LCD_WriteReg(0xD629, 0xDE); - LCD_WriteReg(0xD62A, 0x02); - LCD_WriteReg(0xD62B, 0xF2); - LCD_WriteReg(0xD62C, 0x02); - LCD_WriteReg(0xD62D, 0xFE); - LCD_WriteReg(0xD62E, 0x03); - LCD_WriteReg(0xD62F, 0x10); - LCD_WriteReg(0xD630, 0x03); - LCD_WriteReg(0xD631, 0x33); - LCD_WriteReg(0xD632, 0x03); - LCD_WriteReg(0xD633, 0x6D); - LCD_WriteReg(0xF000, 0x55); - LCD_WriteReg(0xF001, 0xAA); - LCD_WriteReg(0xF002, 0x52); - LCD_WriteReg(0xF003, 0x08); - LCD_WriteReg(0xF004, 0x00); - LCD_WriteReg(0xB100, 0xCC); - LCD_WriteReg(0xB101, 0x00); - LCD_WriteReg(0xB600, 0x05); - LCD_WriteReg(0xB700, 0x70); - LCD_WriteReg(0xB701, 0x70); - LCD_WriteReg(0xB800, 0x01); - LCD_WriteReg(0xB801, 0x03); - LCD_WriteReg(0xB802, 0x03); - LCD_WriteReg(0xB803, 0x03); - LCD_WriteReg(0xBC00, 0x02); - LCD_WriteReg(0xBC01, 0x00); - LCD_WriteReg(0xBC02, 0x00); - LCD_WriteReg(0xC900, 0xD0); - LCD_WriteReg(0xC901, 0x02); - LCD_WriteReg(0xC902, 0x50); - LCD_WriteReg(0xC903, 0x50); - LCD_WriteReg(0xC904, 0x50); - LCD_WriteReg(0x3500, 0x00); - LCD_WriteReg(0x3A00, 0x55); - - LCD_WR_REG(0x1100); - delay_1ms(120); - LCD_WR_REG(0x2900); + wrReg(0x2900); delay_1ms(200); } -void BlockWrite(unsigned int Xstart, unsigned int Xend, unsigned int Ystart, unsigned int Yend) +void Lcd::blockWrite(uint16_t xStart, uint16_t xEnd, uint16_t yStart, uint16_t yEnd) { __DSB(); - LCD->LCD_REG = 0x2A00; + regs()->LCD_REG = 0x2A00; __DSB(); - LCD->LCD_RAM = (Xstart >> 8) & 0xFF; + regs()->LCD_RAM = (xStart >> 8) & 0xFF; __DSB(); - LCD->LCD_REG = 0x2A01; + regs()->LCD_REG = 0x2A01; __DSB(); - LCD->LCD_RAM = Xstart & 0xFF; + regs()->LCD_RAM = xStart & 0xFF; __DSB(); - LCD->LCD_REG = 0x2A02; + regs()->LCD_REG = 0x2A02; __DSB(); - LCD->LCD_RAM = (Xend >> 8) & 0xFF; + regs()->LCD_RAM = (xEnd >> 8) & 0xFF; __DSB(); - LCD->LCD_REG = 0x2A03; + regs()->LCD_REG = 0x2A03; __DSB(); - LCD->LCD_RAM = Xend & 0xFF; + regs()->LCD_RAM = xEnd & 0xFF; __DSB(); - LCD->LCD_REG = 0x2B00; + regs()->LCD_REG = 0x2B00; __DSB(); - LCD->LCD_RAM = (Ystart >> 8) & 0xFF; + regs()->LCD_RAM = (yStart >> 8) & 0xFF; __DSB(); - LCD->LCD_REG = 0x2B01; + regs()->LCD_REG = 0x2B01; __DSB(); - LCD->LCD_RAM = Ystart & 0xFF; + regs()->LCD_RAM = yStart & 0xFF; __DSB(); - LCD->LCD_REG = 0x2B02; + regs()->LCD_REG = 0x2B02; __DSB(); - LCD->LCD_RAM = (Yend >> 8) & 0xFF; + regs()->LCD_RAM = (yEnd >> 8) & 0xFF; __DSB(); - LCD->LCD_REG = 0x2B03; + regs()->LCD_REG = 0x2B03; __DSB(); - LCD->LCD_RAM = Yend & 0xFF; + regs()->LCD_RAM = yEnd & 0xFF; __DSB(); } -void LCD_Clear(uint16_t color) +void Lcd::clear(uint16_t color) { - uint32_t totalpoint = (uint32_t)lcddev.width * lcddev.height; - printf("[LCD_Clear] 0x%04X, %dx%d\r\n", color, lcddev.width, lcddev.height); - BlockWrite(0, lcddev.width - 1, 0, lcddev.height - 1); - LCD_WriteRAM_Prepare(); + uint32_t totalpoint = (uint32_t)lcddev_.width_ * lcddev_.height_; + printf("[Lcd::clear] 0x%04X, %dx%d\r\n", color, lcddev_.width_, lcddev_.height_); + blockWrite(0, lcddev_.width_ - 1, 0, lcddev_.height_ - 1); + writeRamPrepare(); for (uint32_t i = 0; i < totalpoint; i++) - LCD_WriteRAM(color); + writeRam(color); __DSB(); } -void LCD_DiagnosticPattern(void) +void Lcd::clear(Color color) { - printf("[Diagnostic] ===== 逐步骤诊断测试 =====\r\n"); + clear(static_cast(color)); +} - printf("\r\n[诊断 1] 读取寄存器 0x0000 (制造商ID)...\r\n"); - uint16_t id = LCD_ReadReg(0x0000); - printf(" [诊断 1] 结果: ID=0x%04X\r\n", id); +void Lcd::diagnosticPattern() +{ + printf("[Diagnostic] ===== Step-by-step diagnostic =====\r\n"); + + printf("\r\n[Diag 1] Read register 0x0000 (Manufacturer ID)...\r\n"); + uint16_t id = readReg(0x0000); + printf(" [Diag 1] Result: ID=0x%04X\r\n", id); delay_1ms(500); - printf("\r\n[诊断 2] 读取寄存器 0x0A00 (显示状态)...\r\n"); - uint16_t disp = LCD_ReadReg(0x0A00); - printf(" [诊断 2] 结果: DISP_STATUS=0x%04X\r\n", disp); + printf("\r\n[Diag 2] Read register 0x0A00 (Display status)...\r\n"); + uint16_t disp = readReg(0x0A00); + printf(" [Diag 2] Result: DISP_STATUS=0x%04X\r\n", disp); delay_1ms(500); - printf("\r\n[诊断 3] 写一个小方块到屏幕左上角 (0,0)-(50,50)...\r\n"); - printf(" 发送 BlockWrite + 2500个像素...\r\n"); - BlockWrite(0, 50, 0, 50); - LCD_WriteRAM_Prepare(); + printf("\r\n[Diag 3] Write a small block to top-left (0,0)-(50,50)...\r\n"); + printf(" Sending BlockWrite + 2500 pixels...\r\n"); + blockWrite(0, 50, 0, 50); + writeRamPrepare(); for (uint32_t i = 0; i < 51 * 51; i++) { - LCD_WriteRAM(RED); + writeRam(Color::RED); } __DSB(); delay_1ms(1000); - printf(" [诊断 3] 完成, 等待1秒...\r\n"); + printf(" [Diag 3] Done, wait 1s...\r\n"); delay_1ms(1000); - printf("\r\n[诊断 4] 再读寄存器 0x0000 确认EXMC仍工作...\r\n"); - id = LCD_ReadReg(0x0000); - printf(" [诊断 4] 结果: ID=0x%04X\r\n", id); + printf("\r\n[Diag 4] Re-read register 0x0000 to confirm EXMC still works...\r\n"); + id = readReg(0x0000); + printf(" [Diag 4] Result: ID=0x%04X\r\n", id); delay_1ms(500); - printf("\r\n[诊断 5] 写一个小方块到屏幕中央 (200,200)-(250,250)...\r\n"); - BlockWrite(200, 250, 200, 250); - LCD_WriteRAM_Prepare(); + printf("\r\n[Diag 5] Write a small block to center (200,200)-(250,250)...\r\n"); + blockWrite(200, 250, 200, 250); + writeRamPrepare(); for (uint32_t i = 0; i < 51 * 51; i++) { - LCD_WriteRAM(GREEN); + writeRam(Color::GREEN); } __DSB(); delay_1ms(1000); - printf(" [诊断 5] 完成\r\n"); + printf(" [Diag 5] Done\r\n"); - printf("\r\n[诊断 6] 写一个小方块到底部 (200,750)-(250,799)...\r\n"); - BlockWrite(200, 250, 750, 799); - LCD_WriteRAM_Prepare(); + printf("\r\n[Diag 6] Write a small block to bottom (200,750)-(250,799)...\r\n"); + blockWrite(200, 250, 750, 799); + writeRamPrepare(); for (uint32_t i = 0; i < 51 * 50; i++) { - LCD_WriteRAM(BLUE); + writeRam(Color::BLUE); } __DSB(); delay_1ms(1000); - printf(" [诊断 6] 完成\r\n"); + printf(" [Diag 6] Done\r\n"); - printf("\r\n[诊断 7] 再读寄存器 0x0000 确认...\r\n"); - id = LCD_ReadReg(0x0000); - printf(" [诊断 7] 结果: ID=0x%04X\r\n", id); + printf("\r\n[Diag 7] Re-read register 0x0000...\r\n"); + id = readReg(0x0000); + printf(" [Diag 7] Result: ID=0x%04X\r\n", id); - printf("\r\n[诊断 8] 读寄存器 0x0A00 确认...\r\n"); - disp = LCD_ReadReg(0x0A00); - printf(" [诊断 8] 结果: DISP_STATUS=0x%04X\r\n", disp); + printf("\r\n[Diag 8] Read register 0x0A00...\r\n"); + disp = readReg(0x0A00); + printf(" [Diag 8] Result: DISP_STATUS=0x%04X\r\n", disp); - printf("[Diagnostic] ===== 诊断完成,请观察屏幕 =====\r\n"); + printf("[Diagnostic] ===== Diagnostic complete =====\r\n"); } -void LCD_SetCursor(uint16_t Xpos, uint16_t Ypos) +void Lcd::setCursor(uint16_t x, uint16_t y) { - LCD_WriteReg(0x2A00, Xpos >> 8); - LCD_WriteReg(0x2A01, Xpos & 0xFF); - LCD_WriteReg(0x2A02, (lcddev.width - 1) >> 8); - LCD_WriteReg(0x2A03, (lcddev.width - 1) & 0xFF); - LCD_WriteReg(0x2B00, Ypos >> 8); - LCD_WriteReg(0x2B01, Ypos & 0xFF); - LCD_WriteReg(0x2B02, (lcddev.height - 1) >> 8); - LCD_WriteReg(0x2B03, (lcddev.height - 1) & 0xFF); + writeReg(0x2A00, x >> 8); + writeReg(0x2A01, x & 0xFF); + writeReg(0x2A02, (lcddev_.width_ - 1) >> 8); + writeReg(0x2A03, (lcddev_.width_ - 1) & 0xFF); + writeReg(0x2B00, y >> 8); + writeReg(0x2B01, y & 0xFF); + writeReg(0x2B02, (lcddev_.height_ - 1) >> 8); + writeReg(0x2B03, (lcddev_.height_ - 1) & 0xFF); } -void LCD_Scan_Dir(uint8_t dir) +void Lcd::setScanDir(ScanDir dir) { uint16_t regval = 0; uint16_t dirreg = 0; @@ -732,102 +753,95 @@ void LCD_Scan_Dir(uint8_t dir) regval &= ~(1 << 3); dirreg = 0X3600; - LCD_WriteReg(dirreg, regval); - LCD_WriteReg(0x2A00, 0); - LCD_WriteReg(0x2A01, 0); - LCD_WriteReg(0x2A02, (lcddev.width - 1) >> 8); - LCD_WriteReg(0x2A03, (lcddev.width - 1) & 0xFF); - LCD_WriteReg(0x2B00, 0); - LCD_WriteReg(0x2B01, 0); - LCD_WriteReg(0x2B02, (lcddev.height - 1) >> 8); - LCD_WriteReg(0x2B03, (lcddev.height - 1) & 0xFF); + writeReg(dirreg, regval); + writeReg(0x2A00, 0); + writeReg(0x2A01, 0); + writeReg(0x2A02, (lcddev_.width_ - 1) >> 8); + writeReg(0x2A03, (lcddev_.width_ - 1) & 0xFF); + writeReg(0x2B00, 0); + writeReg(0x2B01, 0); + writeReg(0x2B02, (lcddev_.height_ - 1) >> 8); + writeReg(0x2B03, (lcddev_.height_ - 1) & 0xFF); } -void LCD_Init(void) +RetCode Lcd::init() { - printf("[LCD] LCD_Init start\r\n"); + printf("[Lcd] init start\r\n"); + ctrlLinesConfig(); + fsmcConfig(); - LCD_CtrlLinesConfig(); - LCD_FSMCConfig(); - - LCD_RST_OFF; + rstOff(); delay_1ms(100); - LCD_RST_ON; + rstOn(); delay_1ms(100); - // 跳过读 ID (所有读操作均返回 0, 待后续排查 EXMC 读时序) - lcddev.id = 0x5510; - sprintf((char *)lcd_id, "LCD ID:%04X", lcddev.id); - printf("[LCD] Skip ReadID, force ID=0x5510\r\n"); + lcddev_.id_ = 0x5510; + sprintf(id_string_, "LCD ID:%04X", lcddev_.id_); + printf("[Lcd] Skip ReadID, force ID=0x5510\r\n"); - LCD_HVGA_NT35510(); - NT35510_HY35_Initial_Code(); + hvgaConfig(); + initialCode(); - // 重新设置关键寄存器(覆盖 init code 中的值) - - // 0xC200: 显示分辨率. 0x01=480x800, 0x02=480x854, 0x03=480x960 - LCD_WriteReg(0xC200, 0x01); + writeReg(0xC200, 0x01); delay_1ms(10); - // 0x3A00: 像素格式. 0x55=16-bit RGB565 - LCD_WriteReg(0x3A00, 0x55); + writeReg(0x3A00, 0x55); delay_1ms(10); - // 退出 Partial Mode - LCD_WR_REG(0x1300); + wrReg(0x1300); delay_1ms(10); - // MADCTL: MV=0, MX=0, MY=0, RGB顺序 - LCD_WriteReg(0x3600, 0x0000); + writeReg(0x3600, 0x0000); delay_1ms(10); - // 设置扫描方向并更新 CASET/PASET 坐标范围 - LCD_Scan_Dir(DFT_SCAN_DIR); + setScanDir(ScanDir::DFT_SCAN_DIR); delay_1ms(10); - LCD_BLK_ON; - printf("[LCD] LCD_Init done\r\n"); + blkOn(); + printf("[Lcd] init done\r\n"); + + return RET_OK; } -void LCD_DrawPoint(uint16_t x, uint16_t y) +void Lcd::drawPoint(uint16_t x, uint16_t y) { - LCD_SetCursor(x, y); - LCD_WriteRAM_Prepare(); - LCD_WriteRAM(POINT_COLOR); + setCursor(x, y); + writeRamPrepare(); + writeRam(point_color_); } -void LCD_Fast_DrawPoint(uint16_t x, uint16_t y, uint16_t color) +void Lcd::fastDrawPoint(uint16_t x, uint16_t y, uint16_t color) { - LCD_SetCursor(x, y); - LCD_WriteRAM_Prepare(); - LCD_WriteRAM(color); + setCursor(x, y); + writeRamPrepare(); + writeRam(color); } -uint16_t LCD_ReadPoint(uint16_t x, uint16_t y) +uint16_t Lcd::readPoint(uint16_t x, uint16_t y) { uint16_t color; - if (lcddev.id == 0x5510) + if (lcddev_.id_ == 0x5510) { - LCD_WR_REG(lcddev.setxcmd); - LCD_WR_DATA(x >> 8); - LCD_WR_REG(lcddev.setxcmd + 1); - LCD_WR_DATA(x & 0xFF); - LCD_WR_REG(lcddev.setycmd); - LCD_WR_DATA(y >> 8); - LCD_WR_REG(lcddev.setycmd + 1); - LCD_WR_DATA(y & 0xFF); + wrReg(lcddev_.setxcmd_); + wrData(x >> 8); + wrReg(lcddev_.setxcmd_ + 1); + wrData(x & 0xFF); + wrReg(lcddev_.setycmd_); + wrData(y >> 8); + wrReg(lcddev_.setycmd_ + 1); + wrData(y & 0xFF); } else { - LCD_SetCursor(x, y); + setCursor(x, y); } - LCD_WR_REG(0x2E00); - color = LCD_RD_DATA(); + wrReg(0x2E00); + color = rdData(); return color; } -void LCD_ShowChar(uint16_t x, uint16_t y, uint8_t num, uint8_t size, uint8_t mode) +void Lcd::showChar(uint16_t x, uint16_t y, uint8_t num, uint8_t size, uint8_t mode) { uint8_t temp, t1, t; uint16_t y0 = y; @@ -846,13 +860,13 @@ void LCD_ShowChar(uint16_t x, uint16_t y, uint8_t num, uint8_t size, uint8_t mod for (t1 = 0; t1 < 8; t1++) { if (temp & 0x80) - LCD_Fast_DrawPoint(x, y, POINT_COLOR); + fastDrawPoint(x, y, point_color_); else - LCD_Fast_DrawPoint(x, y, BACK_COLOR); + fastDrawPoint(x, y, back_color_); temp <<= 1; y++; - if (y >= lcddev.height) + if (y >= lcddev_.height_) return; if ((y - y0) == size) @@ -876,11 +890,11 @@ void LCD_ShowChar(uint16_t x, uint16_t y, uint8_t num, uint8_t size, uint8_t mod for (t1 = 0; t1 < 8; t1++) { if (temp & 0x80) - LCD_Fast_DrawPoint(x, y, POINT_COLOR); + fastDrawPoint(x, y, point_color_); temp <<= 1; y++; - if (y >= lcddev.height) + if (y >= lcddev_.height_) return; if ((y - y0) == size) @@ -894,17 +908,17 @@ void LCD_ShowChar(uint16_t x, uint16_t y, uint8_t num, uint8_t size, uint8_t mod } } -void LCD_ShowString(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t size, uint8_t mode, uint8_t *p) +void Lcd::showString(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t size, uint8_t mode, uint8_t *p) { while (*p != '\0') { - if (x + size / 2 > lcddev.width) + if (x + size / 2 > lcddev_.width_) { x = 0; y += size; } - if (y + size > lcddev.height) + if (y + size > lcddev_.height_) return; if (*p == '\n') @@ -915,13 +929,13 @@ void LCD_ShowString(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uin continue; } - LCD_ShowChar(x, y, *p, size, mode); + showChar(x, y, *p, size, mode); x += size / 2; p++; } } -void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) +void Lcd::drawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { uint16_t t; int xerr = 0, yerr = 0, delta_x, delta_y, distance; @@ -957,7 +971,7 @@ void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) for (t = 0; t <= distance; t++) { - LCD_Fast_DrawPoint(x1, y1, POINT_COLOR); + fastDrawPoint(x1, y1, point_color_); xerr += delta_x; yerr += delta_y; @@ -975,15 +989,15 @@ void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) } } -void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) +void Lcd::drawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { - LCD_DrawLine(x1, y1, x2, y1); - LCD_DrawLine(x1, y1, x1, y2); - LCD_DrawLine(x1, y2, x2, y2); - LCD_DrawLine(x2, y1, x2, y2); + drawLine(x1, y1, x2, y1); + drawLine(x1, y1, x1, y2); + drawLine(x1, y2, x2, y2); + drawLine(x2, y1, x2, y2); } -void LCD_FillRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) +void Lcd::fillRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) { uint32_t i, total; @@ -1001,37 +1015,37 @@ void LCD_FillRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) } total = (uint32_t)(x2 - x1 + 1) * (y2 - y1 + 1); - BlockWrite(x1, x2, y1, y2); - LCD_WriteRAM_Prepare(); + blockWrite(x1, x2, y1, y2); + writeRamPrepare(); for (i = 0; i < total; i++) - LCD_WriteRAM(POINT_COLOR); + writeRam(point_color_); } -void LCD_Fill(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, uint16_t color) +void Lcd::fill(uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd, uint16_t color) { uint32_t i, total; - if (x_start > x_end) + if (xStart > xEnd) { - uint16_t t = x_start; - x_start = x_end; - x_end = t; + uint16_t t = xStart; + xStart = xEnd; + xEnd = t; } - if (y_start > y_end) + if (yStart > yEnd) { - uint16_t t = y_start; - y_start = y_end; - y_end = t; + uint16_t t = yStart; + yStart = yEnd; + yEnd = t; } - total = (uint32_t)(x_end - x_start + 1) * (y_end - y_start + 1); - BlockWrite(x_start, x_end, y_start, y_end); - LCD_WriteRAM_Prepare(); + total = (uint32_t)(xEnd - xStart + 1) * (yEnd - yStart + 1); + blockWrite(xStart, xEnd, yStart, yEnd); + writeRamPrepare(); for (i = 0; i < total; i++) - LCD_WriteRAM(color); + writeRam(color); } -void LCD_ShowNum(uint16_t x, uint16_t y, uint32_t num, uint8_t len, uint8_t size) +void Lcd::showNum(uint16_t x, uint16_t y, uint32_t num, uint8_t len, uint8_t size) { uint8_t buf[12]; uint8_t i; @@ -1042,10 +1056,10 @@ void LCD_ShowNum(uint16_t x, uint16_t y, uint32_t num, uint8_t len, uint8_t size num /= 10; } for (i = 0; i < len; i++) - LCD_ShowChar(x + (len - 1 - i) * (size / 2), y, buf[i], size, 0); + showChar(x + (len - 1 - i) * (size / 2), y, buf[i], size, 0); } -void LCD_ShowIntNum(uint16_t x, uint16_t y, int32_t num, uint8_t len, uint8_t size) +void Lcd::showIntNum(uint16_t x, uint16_t y, int32_t num, uint8_t len, uint8_t size) { uint8_t buf[14]; uint8_t i, neg = 0; @@ -1064,17 +1078,17 @@ void LCD_ShowIntNum(uint16_t x, uint16_t y, int32_t num, uint8_t len, uint8_t si if (neg) { for (i = 0; i < len; i++) - LCD_ShowChar(x + (len - i) * (size / 2), y, buf[i], size, 0); - LCD_ShowChar(x, y, '-', size, 0); + showChar(x + (len - i) * (size / 2), y, buf[i], size, 0); + showChar(x, y, '-', size, 0); } else { for (i = 0; i < len; i++) - LCD_ShowChar(x + (len - 1 - i) * (size / 2), y, buf[i], size, 0); + showChar(x + (len - 1 - i) * (size / 2), y, buf[i], size, 0); } } -void LCD_ShowFloatNum(uint16_t x, uint16_t y, float num, uint8_t intLen, uint8_t decLen, uint8_t size) +void Lcd::showFloatNum(uint16_t x, uint16_t y, float num, uint8_t intLen, uint8_t decLen, uint8_t size) { uint16_t x_offset = 0; int32_t intPart = (int32_t)num; @@ -1083,15 +1097,15 @@ void LCD_ShowFloatNum(uint16_t x, uint16_t y, float num, uint8_t intLen, uint8_t if (num < 0) { - LCD_ShowChar(x, y, '-', size, 0); + showChar(x, y, '-', size, 0); x_offset = size / 2; intPart = -intPart; } - LCD_ShowIntNum(x + x_offset, y, intPart, intLen, size); + showIntNum(x + x_offset, y, intPart, intLen, size); x_offset += intLen * (size / 2); - LCD_ShowChar(x + x_offset, y, '.', size, 0); + showChar(x + x_offset, y, '.', size, 0); x_offset += size / 2; if (num < 0) @@ -1101,24 +1115,24 @@ void LCD_ShowFloatNum(uint16_t x, uint16_t y, float num, uint8_t intLen, uint8_t for (i = 0; i < decLen; i++) decPart /= 10; - LCD_ShowNum(x + x_offset, y, decPart, decLen, size); + showNum(x + x_offset, y, decPart, decLen, size); } -void LCD_DrawCircle(uint16_t x0, uint16_t y0, uint16_t r) +void Lcd::drawCircle(uint16_t x0, uint16_t y0, uint16_t r) { int x = 0, y = r; int d = 3 - 2 * r; while (x <= y) { - LCD_Fast_DrawPoint(x0 + x, y0 + y, POINT_COLOR); - LCD_Fast_DrawPoint(x0 - x, y0 + y, POINT_COLOR); - LCD_Fast_DrawPoint(x0 + x, y0 - y, POINT_COLOR); - LCD_Fast_DrawPoint(x0 - x, y0 - y, POINT_COLOR); - LCD_Fast_DrawPoint(x0 + y, y0 + x, POINT_COLOR); - LCD_Fast_DrawPoint(x0 - y, y0 + x, POINT_COLOR); - LCD_Fast_DrawPoint(x0 + y, y0 - x, POINT_COLOR); - LCD_Fast_DrawPoint(x0 - y, y0 - x, POINT_COLOR); + fastDrawPoint(x0 + x, y0 + y, point_color_); + fastDrawPoint(x0 - x, y0 + y, point_color_); + fastDrawPoint(x0 + x, y0 - y, point_color_); + fastDrawPoint(x0 - x, y0 - y, point_color_); + fastDrawPoint(x0 + y, y0 + x, point_color_); + fastDrawPoint(x0 - y, y0 + x, point_color_); + fastDrawPoint(x0 + y, y0 - x, point_color_); + fastDrawPoint(x0 - y, y0 - x, point_color_); if (d < 0) d = d + 4 * x + 6; @@ -1131,17 +1145,17 @@ void LCD_DrawCircle(uint16_t x0, uint16_t y0, uint16_t r) } } -void LCD_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) +void Lcd::fillCircle(uint16_t x0, uint16_t y0, uint16_t r) { int x = 0, y = r; int d = 3 - 2 * r; while (x <= y) { - LCD_DrawLine(x0 - x, y0 + y, x0 + x, y0 + y); - LCD_DrawLine(x0 - y, y0 + x, x0 + y, y0 + x); - LCD_DrawLine(x0 - x, y0 - y, x0 + x, y0 - y); - LCD_DrawLine(x0 - y, y0 - x, x0 + y, y0 - x); + drawLine(x0 - x, y0 + y, x0 + x, y0 + y); + drawLine(x0 - y, y0 + x, x0 + y, y0 + x); + drawLine(x0 - x, y0 - y, x0 + x, y0 - y); + drawLine(x0 - y, y0 - x, x0 + y, y0 - x); if (d < 0) d = d + 4 * x + 6; @@ -1154,39 +1168,39 @@ void LCD_FillCircle(uint16_t x0, uint16_t y0, uint16_t r) } } -void LCD_ShowImage(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t *p) +void Lcd::showImage(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t *p) { uint32_t i, j; uint16_t color; const uint16_t *img = (const uint16_t *)p; - if (x + width > lcddev.width || y + height > lcddev.height) + if (x + width > lcddev_.width_ || y + height > lcddev_.height_) return; - BlockWrite(x, x + width - 1, y, y + height - 1); - LCD_WriteRAM_Prepare(); + blockWrite(x, x + width - 1, y, y + height - 1); + writeRamPrepare(); for (i = 0; i < height; i++) { for (j = 0; j < width; j++) { color = *img++; - LCD_WriteRAM(color); + writeRam(color); } } } -void LCD_ShowSmallImage(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t *p) +void Lcd::showSmallImage(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t *p) { uint32_t i, j; uint16_t color; uint8_t r, g, b; - if (x + width > lcddev.width || y + height > lcddev.height) + if (x + width > lcddev_.width_ || y + height > lcddev_.height_) return; - BlockWrite(x, x + width - 1, y, y + height - 1); - LCD_WriteRAM_Prepare(); + blockWrite(x, x + width - 1, y, y + height - 1); + writeRamPrepare(); for (i = 0; i < height; i++) { @@ -1198,27 +1212,27 @@ void LCD_ShowSmallImage(uint16_t x, uint16_t y, uint16_t width, uint16_t height, b = pixel & 0x03; color = ((r << 13) & 0xF800) | ((g << 8) & 0x07E0) | ((b << 3) & 0x001F); - LCD_WriteRAM(color); + writeRam(color); } } } -void LCD_DisplayOn(void) +void Lcd::displayOn() { - LCD_WR_REG(0x2900); + wrReg(0x2900); delay_1ms(50); } -void LCD_DisplayOff(void) +void Lcd::displayOff() { - LCD_WR_REG(0x2800); + wrReg(0x2800); delay_1ms(50); } -void LCD_Backlight_Set(uint8_t brightness) +void Lcd::backlight(uint8_t brightness) { if (brightness) - LCD_BLK_ON; + blkOn(); else - LCD_BLK_OFF; + blkOff(); } diff --git a/bsp/device/lcd/lcd.h b/bsp/device/lcd/lcd.h index 833300a..fc49042 100644 --- a/bsp/device/lcd/lcd.h +++ b/bsp/device/lcd/lcd.h @@ -1,118 +1,144 @@ -#ifndef __LCD_H -#define __LCD_H +#ifndef __LCD_H__ +#define __LCD_H__ + +#include +#include "common_types.h" #include "gd32f4xx.h" -#include "systick.h" -#define LCD_RST_ON gpio_bit_set(GPIOD, GPIO_PIN_12) // PD12 -#define LCD_RST_OFF gpio_bit_reset(GPIOD, GPIO_PIN_12) // PD12 -#define LCD_BLK_ON gpio_bit_set(GPIOD, GPIO_PIN_13) // PD13 -#define LCD_BLK_OFF gpio_bit_reset(GPIOD, GPIO_PIN_13) // PD13 +class Lcd { +public: + static Lcd &instance(); -// ???? -#define WHITE 0xFFFF -#define BLACK 0x0000 -#define BLUE 0x001F -#define BRED 0XF81F -#define GRED 0XFFE0 -#define GBLUE 0X07FF -#define RED 0xF800 -#define MAGENTA 0xF81F -#define GREEN 0x07E0 -#define CYAN 0x7FFF -#define YELLOW 0xFFE0 -#define BROWN 0XBC40 -#define BRRED 0XFC07 -#define GRAY 0X8430 -#define DARKBLUE 0X01CF -#define LIGHTBLUE 0X7D7C -#define GRAYBLUE 0X5458 -#define LIGHTGREEN 0X841F -#define LIGHTGRAY 0XEF5B -#define LGRAY 0XC618 -#define LGRAYBLUE 0XA651 -#define LBBLUE 0X2B12 + enum Color : uint16_t { + WHITE = 0xFFFF, + BLACK = 0x0000, + BLUE = 0x001F, + BRED = 0XF81F, + GRED = 0XFFE0, + GBLUE = 0X07FF, + RED = 0xF800, + MAGENTA = 0xF81F, + GREEN = 0x07E0, + CYAN = 0x7FFF, + YELLOW = 0xFFE0, + BROWN = 0XBC40, + BRRED = 0XFC07, + GRAY = 0X8430, + DARKBLUE = 0X01CF, + LIGHTBLUE = 0X7D7C, + GRAYBLUE = 0X5458, + LIGHTGREEN = 0X841F, + LIGHTGRAY = 0XEF5B, + LGRAY = 0XC618, + LGRAYBLUE = 0XA651, + LBBLUE = 0X2B12 + }; -// RGB888 -> RGB565 颜色转换 -#define RGB565(r, g, b) ((((uint16_t)(r) >> 3) << 11) | (((uint16_t)(g) >> 2) << 5) | ((uint16_t)(b) >> 3)) + static constexpr uint16_t RGB565(uint8_t r, uint8_t g, uint8_t b) { + return (((uint16_t)(r) >> 3) << 11) | (((uint16_t)(g) >> 2) << 5) | ((uint16_t)(b) >> 3); + } -// ?????? -#define L2R_U2D 0 -#define L2R_D2U 1 -#define R2L_U2D 2 -#define R2L_D2U 3 -#define U2D_L2R 4 -#define U2D_R2L 5 -#define D2U_L2R 6 -#define D2U_R2L 7 -#define DFT_SCAN_DIR L2R_U2D + enum ScanDir : uint8_t { + L2R_U2D = 0, + L2R_D2U = 1, + R2L_U2D = 2, + R2L_D2U = 3, + U2D_L2R = 4, + U2D_R2L = 5, + D2U_L2R = 6, + D2U_R2L = 7, + DFT_SCAN_DIR = L2R_U2D + }; -// LCD地址结构体 16BIT -typedef struct -{ - uint16_t LCD_REG; - uint16_t LCD_RAM; -} LCD_TypeDef; -#define LCD_BASE ((uint32_t)(0x6C000000 | 0X000007FE)) -#define LCD ((LCD_TypeDef *)LCD_BASE) + RetCode init(); -// LCD重要参数集 -typedef struct -{ - uint16_t width; - uint16_t height; - uint16_t id; - uint8_t dir; - uint16_t wramcmd; - uint16_t setxcmd; - uint16_t setycmd; -} _lcd_dev; + void clear(Color color); + void clear(uint16_t color); -extern _lcd_dev lcddev; -extern uint16_t POINT_COLOR; -extern uint16_t BACK_COLOR; -extern uint8_t lcd_id[12]; + uint16_t width() const; + uint16_t height() const; + uint16_t id() const; + const char *idString() const; + uint16_t foreground() const; + uint16_t background() const; + void setForeground(uint16_t color); + void setBackground(uint16_t color); -void LCD_CtrlLinesConfig(void); -void LCD_FSMCConfig(void); -void LCD_WriteReg(uint16_t LCD_Reg, uint16_t LCD_RegValue); -uint16_t LCD_ReadReg(uint16_t LCD_Reg); -void LcdNT35510ReadID(void); -void LCD_HVGA_NT35510(void); -void NT35510_HY35_Initial_Code(void); + void drawPoint(uint16_t x, uint16_t y); + void fastDrawPoint(uint16_t x, uint16_t y, uint16_t color); + uint16_t readPoint(uint16_t x, uint16_t y); -void BlockWrite(unsigned int Xstart, unsigned int Xend, unsigned int Ystart, unsigned int Yend); -void LCD_WriteRAM_Prepare(void); -void LCD_WriteRAM(uint16_t RGB_Code); -void LCD_Clear(uint16_t color); -void LCD_DiagnosticPattern(void); -void LCD_Scan_Dir(uint8_t dir); -void LCD_SetCursor(uint16_t Xpos, uint16_t Ypos); + void drawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); + void drawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); + void fillRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); + void drawCircle(uint16_t x0, uint16_t y0, uint16_t r); + void fillCircle(uint16_t x0, uint16_t y0, uint16_t r); -void LCD_Init(void); + void showChar(uint16_t x, uint16_t y, uint8_t num, uint8_t size, uint8_t mode); + void showString(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t size, uint8_t mode, uint8_t *p); + void showNum(uint16_t x, uint16_t y, uint32_t num, uint8_t len, uint8_t size); + void showIntNum(uint16_t x, uint16_t y, int32_t num, uint8_t len, uint8_t size); + void showFloatNum(uint16_t x, uint16_t y, float num, uint8_t intLen, uint8_t decLen, uint8_t size); -void LCD_DrawPoint(uint16_t x, uint16_t y); -void LCD_Fast_DrawPoint(uint16_t x, uint16_t y, uint16_t color); -uint16_t LCD_ReadPoint(uint16_t x, uint16_t y); -void LCD_ShowChar(uint16_t x, uint16_t y, uint8_t num, uint8_t size, uint8_t mode); -void LCD_ShowString(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t size, uint8_t mode, uint8_t *p); + void showImage(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t *p); + void showSmallImage(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t *p); -void LCD_DrawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); -void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); -void LCD_FillRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); -void LCD_DrawCircle(uint16_t x0, uint16_t y0, uint16_t r); -void LCD_FillCircle(uint16_t x0, uint16_t y0, uint16_t r); + void blockWrite(uint16_t xStart, uint16_t xEnd, uint16_t yStart, uint16_t yEnd); + void writeRamPrepare(); + void writeRam(uint16_t color); + void fill(uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd, uint16_t color); -void LCD_ShowImage(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t *p); -void LCD_ShowSmallImage(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t *p); + void setCursor(uint16_t x, uint16_t y); + void setScanDir(ScanDir dir); -void LCD_Fill(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, uint16_t color); + void displayOn(); + void displayOff(); + void backlight(uint8_t brightness); -void LCD_ShowNum(uint16_t x, uint16_t y, uint32_t num, uint8_t len, uint8_t size); -void LCD_ShowIntNum(uint16_t x, uint16_t y, int32_t num, uint8_t len, uint8_t size); -void LCD_ShowFloatNum(uint16_t x, uint16_t y, float num, uint8_t intLen, uint8_t decLen, uint8_t size); + void diagnosticPattern(); -void LCD_DisplayOn(void); -void LCD_DisplayOff(void); -void LCD_Backlight_Set(uint8_t brightness); +private: + Lcd(); + ~Lcd() = default; + Lcd(const Lcd &) = delete; + Lcd &operator=(const Lcd &) = delete; + + struct LcdRegs { + uint16_t LCD_REG; + uint16_t LCD_RAM; + }; + + static LcdRegs *regs(); + static void rstOn(); + static void rstOff(); + static void blkOn(); + static void blkOff(); + + void wrReg(uint16_t regval); + void wrData(uint16_t data); + uint16_t rdData(); + void writeReg(uint16_t reg, uint16_t value); + uint16_t readReg(uint16_t reg); + + void ctrlLinesConfig(); + void fsmcConfig(); + void hvgaConfig(); + void initialCode(); + void readID(); + + struct { + uint16_t width_; + uint16_t height_; + uint16_t id_; + uint8_t dir_; + uint16_t wramcmd_; + uint16_t setxcmd_; + uint16_t setycmd_; + } lcddev_; + + uint16_t point_color_ = Color::WHITE; + uint16_t back_color_ = Color::BLACK; + char id_string_[16] = {0}; +}; #endif diff --git a/bsp/device/touch/touch.cpp b/bsp/device/touch/touch.cpp index 600c750..b762b42 100644 --- a/bsp/device/touch/touch.cpp +++ b/bsp/device/touch/touch.cpp @@ -1,16 +1,121 @@ #include "touch.h" +#include "gd32f4xx.h" #include "i2c_driver.h" -#include "stdio.h" -#include "string.h" +#include "systick.h" +#include +#include -_m_tp_dev tp_dev; +volatile uint8_t GT1151::irq_flag = 0; -volatile uint8_t g_touch_irq_flag = 0; +static constexpr uint8_t TOUCH_I2C_PORT = 0; +static constexpr uint8_t TOUCH_DEV_ADDR = 0x28; -#define TOUCH_I2C_PORT 0 -#define TOUCH_DEV_ADDR 0x28 +static constexpr uint16_t GT_CTRL_REG = 0x8040; +static constexpr uint16_t GT_CFGS_REG = 0x8050; +static constexpr uint16_t GT_CHECK_REG = 0x813C; +static constexpr uint16_t GT_PID_REG = 0x8140; +static constexpr uint16_t GT_GSTID_REG = 0x814E; +static constexpr uint16_t GT_TP1_REG = 0x8150; +static constexpr uint16_t GT_TP2_REG = 0x8158; +static constexpr uint16_t GT_TP3_REG = 0x8160; +static constexpr uint16_t GT_TP4_REG = 0x8168; +static constexpr uint16_t GT_TP5_REG = 0x8170; -uint8_t GT1151_WR_Reg(uint16_t reg, uint8_t *buf, uint8_t len) +static const uint16_t GT1151_TPX_TBL[5] = {GT_TP1_REG, GT_TP2_REG, GT_TP3_REG, GT_TP4_REG, GT_TP5_REG}; + +static const uint8_t GT1151_CFG_TBL[] = { + 0x63, 0xE0, 0x01, 0x20, 0x03, 0x05, 0x3D, 0x04, 0x00, 0x08, + 0x09, 0x0F, 0x55, 0x37, 0x33, 0x11, 0x00, 0x03, 0x08, 0x56, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, + 0x3C, 0x08, 0x0A, 0x28, 0x1E, 0x50, 0x00, 0x00, 0x82, 0xB4, + 0xD2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x85, 0x25, 0x10, 0x41, 0x43, 0x31, + 0x0D, 0x00, 0xAD, 0x22, 0x24, 0x7D, 0x1D, 0x1D, 0x32, 0xDF, + 0x4F, 0x44, 0x0F, 0x80, 0x2C, 0x50, 0x50, 0x00, 0x00, 0x00, + 0x00, 0xD3, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x28, 0x1E, 0xFF, + 0xF0, 0x37, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x50, 0xB4, 0xC0, 0x94, 0x53, 0x2D, + 0x0A, 0x02, 0xBE, 0x60, 0xA2, 0x71, 0x8F, 0x82, 0x80, 0x92, + 0x74, 0xA3, 0x6B, 0x01, 0x0F, 0x14, 0x03, 0x1E, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x0E, 0x0F, 0x10, 0x12, + 0x13, 0x14, 0x15, 0x1F, 0x1D, 0x1B, 0x1A, 0x19, 0x18, 0x17, + 0x16, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x06, 0x08, 0x0C, + 0x12, 0x13, 0x14, 0x15, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, + 0xC4, 0x09, 0x23, 0x23, 0x50, 0x5D, 0x54, 0x4B, 0x3C, 0x0F, + 0x32, 0xFF, 0xE4, 0x04, 0x40, 0x00, 0x8A, 0x05, 0x40, 0x00, + 0xAA, 0x00, 0x22, 0x22, 0x00, 0x00, 0x73, 0x22, 0x01}; + +static uint16_t crc16(uint8_t *srcdata, uint16_t length) +{ + uint16_t crc = 0xffff; + for (uint16_t i = 0; i < length; i++) + { + for (uint16_t j = 0; j < 8; j++) + { + uint8_t value = ((srcdata[i] << j) & 0x80) ^ ((crc & 0x8000) >> 8); + crc <<= 1; + if (value != 0) + { + crc ^= 0x8005; + } + } + } + return crc; +} + +GT1151 >1151::instance() +{ + static GT1151 inst; + return inst; +} + +GT1151::GT1151() + : x_{0}, y_{0}, sta_(0), touchtype_(0) +{ +} + +uint16_t GT1151::x(uint8_t index) const +{ + if (index < kMaxTouch) + return x_[index]; + return 0; +} + +uint16_t GT1151::y(uint8_t index) const +{ + if (index < kMaxTouch) + return y_[index]; + return 0; +} + +uint8_t GT1151::status() const +{ + return sta_; +} + +bool GT1151::isActive(uint8_t index) const +{ + if (index < kMaxTouch) + return (sta_ & (1 << index)) != 0; + return false; +} + +uint8_t GT1151::activeCount() const +{ + uint8_t c = 0; + for (uint8_t i = 0; i < kMaxTouch; i++) + { + if (sta_ & (1 << i)) + c++; + } + return c; +} + +uint8_t GT1151::writeReg(uint16_t reg, const uint8_t *buf, uint8_t len) { uint8_t tmp[2 + 256]; tmp[0] = (uint8_t)(reg >> 8); @@ -23,7 +128,7 @@ uint8_t GT1151_WR_Reg(uint16_t reg, uint8_t *buf, uint8_t len) return (ret == 0) ? 0 : 1; } -void GT1151_RD_Reg(uint16_t reg, uint8_t *buf, uint8_t len) +void GT1151::readReg(uint16_t reg, uint8_t *buf, uint8_t len) { uint8_t reg_bytes[2]; reg_bytes[0] = (uint8_t)(reg >> 8); @@ -31,118 +136,51 @@ void GT1151_RD_Reg(uint16_t reg, uint8_t *buf, uint8_t len) i2c_master_write_read(TOUCH_I2C_PORT, TOUCH_DEV_ADDR, reg_bytes, 2, buf, len); } -const uint8_t GT1151_CFG_TBL[] = - { - 0x63, 0xE0, 0x01, 0x20, 0x03, 0x05, 0x3D, 0x04, 0x00, 0x08, - 0x09, 0x0F, 0x55, 0x37, 0x33, 0x11, 0x00, 0x03, 0x08, 0x56, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, - 0x3C, 0x08, 0x0A, 0x28, 0x1E, 0x50, 0x00, 0x00, 0x82, 0xB4, - 0xD2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x85, 0x25, 0x10, 0x41, 0x43, 0x31, - 0x0D, 0x00, 0xAD, 0x22, 0x24, 0x7D, 0x1D, 0x1D, 0x32, 0xDF, - 0x4F, 0x44, 0x0F, 0x80, 0x2C, 0x50, 0x50, 0x00, 0x00, 0x00, - 0x00, 0xD3, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x28, 0x1E, 0xFF, - 0xF0, 0x37, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x50, 0xB4, 0xC0, 0x94, 0x53, 0x2D, - 0x0A, 0x02, 0xBE, 0x60, 0xA2, 0x71, 0x8F, 0x82, 0x80, 0x92, - 0x74, 0xA3, 0x6B, 0x01, 0x0F, 0x14, 0x03, 0x1E, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x0E, 0x0F, 0x10, 0x12, - 0x13, 0x14, 0x15, 0x1F, 0x1D, 0x1B, 0x1A, 0x19, 0x18, 0x17, - 0x16, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x06, 0x08, 0x0C, - 0x12, 0x13, 0x14, 0x15, 0x17, 0x18, 0x19, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, - 0xC4, 0x09, 0x23, 0x23, 0x50, 0x5D, 0x54, 0x4B, 0x3C, 0x0F, - 0x32, 0xFF, 0xE4, 0x04, 0x40, 0x00, 0x8A, 0x05, 0x40, 0x00, - 0xAA, 0x00, 0x22, 0x22, 0x00, 0x00, 0x73, 0x22, 0x01}; - -uint16_t CRC16(uint8_t *srcdata, uint16_t length) +uint8_t GT1151::sendConfig(uint8_t mode) { - uint16_t crc = 0xffff; - uint16_t i, j; - uint8_t value; - for (i = 0; i < length; i++) + uint16_t checksum = 0; + for (uint8_t i = 0; i < (sizeof(GT1151_CFG_TBL) - 3); i += 2) { - for (j = 0; j < 8; j++) - { - value = ((srcdata[i] << j) & 0x80) ^ ((crc & 0x8000) >> 8); - crc <<= 1; - if (value != 0) - { - crc ^= 0x8005; - } - } + checksum += ((uint16_t)GT1151_CFG_TBL[i] << 8) | GT1151_CFG_TBL[i + 1]; } - return crc; -} - -void check_sum(void) -{ - uint16_t checksum = 0; - uint8_t checksumH, checksumL; - uint8_t i = 0; - for (i = 0; i < (sizeof(GT1151_CFG_TBL) - 3); i += 2) - checksum += ((GT1151_CFG_TBL[i] << 8) | GT1151_CFG_TBL[i + 1]); - checksum = (~checksum) + 1; - checksumH = checksum >> 8; - checksumL = checksum; - printf("chksum:0x%X,\r\n", checksum); - printf("chksumH:0x%X,\r\n", checksumH); - printf("chksumL:0x%X,\r\n", checksumL); -} - -uint8_t GT1151_Send_Cfg(uint8_t mode) -{ - uint16_t checksum = 0; - uint8_t i = 0; - for (i = 0; i < (sizeof(GT1151_CFG_TBL) - 3); i += 2) - checksum += ((GT1151_CFG_TBL[i] << 8) | GT1151_CFG_TBL[i + 1]); checksum = (~checksum) + 1; printf("chksum:0x%x,\r\n", checksum); - GT1151_WR_Reg(GT_CFGS_REG, (uint8_t *)GT1151_CFG_TBL, sizeof(GT1151_CFG_TBL)); + writeReg(GT_CFGS_REG, (uint8_t *)GT1151_CFG_TBL, sizeof(GT1151_CFG_TBL)); return 0; } -static void GT1151_EXTI_Init(void) +void GT1151::extiInit() { rcu_periph_clock_enable(RCU_SYSCFG); syscfg_exti_line_config(EXTI_SOURCE_GPIOD, EXTI_SOURCE_PIN11); - exti_init(TOUCH_EXTI_LINE, EXTI_INTERRUPT, EXTI_TRIG_FALLING); + exti_init(EXTI_11, EXTI_INTERRUPT, EXTI_TRIG_FALLING); - exti_interrupt_enable(TOUCH_EXTI_LINE); + exti_interrupt_enable(EXTI_11); - nvic_irq_enable(TOUCH_EXTI_IRQn, 2, 0); + nvic_irq_enable(EXTI10_15_IRQn, 2, 0); } -uint8_t GT1151_Init(void) +RetCode GT1151::init() { uint8_t temp[6] = {0}; uint8_t retry = 3; - // RST_PIN (PD12) 与 LCD 共享,LCD_Init 已完成硬件复位 - // 通过 INT 引脚对 GT1151 做 I2C 复位握手 - - rcu_periph_clock_enable(INT_RCU); - gpio_mode_set(INT_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, INT_PIN); - gpio_output_options_set(INT_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, INT_PIN); - gpio_bit_reset(INT_PORT, INT_PIN); + rcu_periph_clock_enable(RCU_GPIOD); + gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_11); + gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11); + gpio_bit_reset(GPIOD, GPIO_PIN_11); delay_1ms(20); - // 初始化硬件 I2C0 (PB6=SCL, PB7=SDA) i2c_init(TOUCH_I2C_PORT, 100000); - // 释放 INT(设为浮空输入),等待 GT1151 就绪 - gpio_mode_set(INT_PORT, GPIO_MODE_INPUT, GPIO_PUPD_NONE, INT_PIN); + gpio_mode_set(GPIOD, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_11); delay_1ms(150); - // 读取产品 ID,带重试 do { - GT1151_RD_Reg(GT_PID_REG, temp, 4); + readReg(GT_PID_REG, temp, 4); if (temp[0] != 0 && temp[0] != 0xFF) break; delay_1ms(50); @@ -152,98 +190,85 @@ uint8_t GT1151_Init(void) if (strcmp((char *)temp, "1158") == 0 || strcmp((char *)temp, "1151") == 0) { - GT1151_RD_Reg(GT_CFGS_REG, temp, 1); + readReg(GT_CFGS_REG, temp, 1); printf("GT Config Version: 0x%02X\r\n", temp[0]); - GT1151_Send_Cfg(0); + sendConfig(0); delay_1ms(50); - GT1151_EXTI_Init(); + extiInit(); printf("GT EXTI initialized\r\n"); - return 0; + return RET_OK; } printf("GT Init: unknown ID, will still attempt to use touch\r\n"); - return 1; + return RET_ERROR; } -const uint16_t GT1151_TPX_TBL[5] = {GT_TP1_REG, GT_TP2_REG, GT_TP3_REG, GT_TP4_REG, GT_TP5_REG}; - -uint8_t GT1151_Scan(uint8_t mode) +uint8_t GT1151::scan(uint8_t mode) { uint8_t buf[4]; - uint8_t i = 0; uint8_t res = 0; - uint8_t temp; uint8_t tempsta; static uint8_t t = 0; t++; if ((t % 10) == 0 || t < 10) { - GT1151_RD_Reg(GT_GSTID_REG, &mode, 1); + readReg(GT_GSTID_REG, &mode, 1); if (mode & 0X80 && ((mode & 0XF) < 6)) { - temp = 0; - GT1151_WR_Reg(GT_GSTID_REG, &temp, 1); + uint8_t temp = 0; + writeReg(GT_GSTID_REG, &temp, 1); } if ((mode & 0XF) && ((mode & 0XF) < 6)) { - temp = 0XFF << (mode & 0XF); - tempsta = tp_dev.sta; - tp_dev.sta = (~temp) | TP_PRES_DOWN | TP_CATH_PRES; - tp_dev.x[4] = tp_dev.x[0]; - tp_dev.y[4] = tp_dev.y[0]; - for (i = 0; i < 5; i++) + tempsta = sta_; + sta_ = (~(0XFF << (mode & 0XF))) | kPressDown | kCathPress; + y_[4] = y_[0]; + x_[4] = x_[0]; + for (uint8_t i = 0; i < 5; i++) { - if (tp_dev.sta & (1 << i)) + if (sta_ & (1 << i)) { - GT1151_RD_Reg(GT1151_TPX_TBL[i], buf, 4); - if (tp_dev.touchtype & 0X01) - { - tp_dev.x[i] = ((uint16_t)buf[1] << 8) + buf[0]; - tp_dev.y[i] = ((uint16_t)buf[3] << 8) + buf[2]; - } - else - { - tp_dev.x[i] = ((uint16_t)buf[1] << 8) + buf[0]; - tp_dev.y[i] = ((uint16_t)buf[3] << 8) + buf[2]; - } - if (tp_dev.x[i] > 0 && tp_dev.x[i] < 480 && tp_dev.y[i] > 0 && tp_dev.y[i] < 800) - printf("x[%d]:%d,y[%d]:%d\r\n", i, tp_dev.x[i], i, tp_dev.y[i]); + readReg(GT1151_TPX_TBL[i], buf, 4); + x_[i] = ((uint16_t)buf[1] << 8) + buf[0]; + y_[i] = ((uint16_t)buf[3] << 8) + buf[2]; } } res = 1; - if (tp_dev.x[0] > 1024 || tp_dev.y[0] > 1024) + if (x_[0] > 1024 || y_[0] > 1024) { if ((mode & 0XF) > 1) { - tp_dev.x[0] = tp_dev.x[1]; - tp_dev.y[0] = tp_dev.y[1]; + x_[0] = x_[1]; + y_[0] = y_[1]; t = 0; } else { - tp_dev.x[0] = tp_dev.x[4]; - tp_dev.y[0] = tp_dev.y[4]; + x_[0] = x_[4]; + y_[0] = y_[4]; mode = 0X80; - tp_dev.sta = tempsta; + sta_ = tempsta; } } else + { t = 0; + } } } if ((mode & 0X8F) == 0X80) { - if (tp_dev.sta & TP_PRES_DOWN) + if (sta_ & kPressDown) { - tp_dev.sta &= ~(1 << 7); + sta_ &= ~(1 << 7); } else { - tp_dev.x[0] = 0xffff; - tp_dev.y[0] = 0xffff; - tp_dev.sta &= 0XE0; + x_[0] = 0xffff; + y_[0] = 0xffff; + sta_ &= 0XE0; } } if (t > 240) @@ -256,10 +281,10 @@ extern "C" void EXTI10_15_IRQHandler(void) { - if (exti_flag_get(TOUCH_EXTI_LINE) == SET) + if (exti_flag_get(EXTI_11) == SET) { - g_touch_irq_flag = 1; - exti_flag_clear(TOUCH_EXTI_LINE); + GT1151::irq_flag = 1; + exti_flag_clear(EXTI_11); } } } diff --git a/bsp/device/touch/touch.h b/bsp/device/touch/touch.h index 607bf3a..10df67a 100644 --- a/bsp/device/touch/touch.h +++ b/bsp/device/touch/touch.h @@ -1,101 +1,43 @@ #ifndef __TOUCH_H__ #define __TOUCH_H__ -#include "gd32f4xx.h" -#include "systick.h" +#include +#include "common_types.h" +class GT1151 { +public: + static GT1151 &instance(); + static constexpr uint8_t kMaxTouch = 5; + static constexpr uint8_t kPressDown = 0x80; + static constexpr uint8_t kCathPress = 0x40; + RetCode init(); + uint8_t scan(uint8_t mode); -#define SCL_RCU RCU_GPIOB -#define SCL_PORT GPIOB -#define SCL_PIN GPIO_PIN_6 -#define SCL_ON gpio_bit_set(SCL_PORT,SCL_PIN) -#define SCL_OFF gpio_bit_reset(SCL_PORT,SCL_PIN) -#define SCL_TOGGLE gpio_bit_toggle(SCL_PORT,SCL_PIN) + uint16_t x(uint8_t index) const; + uint16_t y(uint8_t index) const; + uint8_t status() const; + bool isActive(uint8_t index) const; + uint8_t activeCount() const; -#define SDA_RCU RCU_GPIOB -#define SDA_PORT GPIOB -#define SDA_PIN GPIO_PIN_7 -#define SDA_ON gpio_bit_set(SDA_PORT,SDA_PIN) -#define SDA_OFF gpio_bit_reset(SDA_PORT,SDA_PIN) -#define SDA_TOGGLE gpio_bit_toggle(SDA_PORT,SDA_PIN) + static volatile uint8_t irq_flag; -//IO方向设置 -#define CT_SDA_IN() gpio_mode_set(SDA_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, SDA_PIN) -#define CT_SDA_OUT() gpio_mode_set(SDA_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, SDA_PIN) -//IO操作函数 -#define CT_IIC_SCL //PBout(0) //SCL -#define CT_IIC_SDA(val) ((val)==1?SDA_ON:SDA_OFF)//PFout(11) //SDA -#define CT_READ_SDA gpio_input_bit_get(SDA_PORT,SDA_PIN) //PFin(11) //输入SDA - -#define RST_RCU RCU_GPIOD -#define RST_PORT GPIOD -#define RST_PIN GPIO_PIN_12 -#define RST_ON gpio_bit_set(RST_PORT,RST_PIN) -#define RST_OFF gpio_bit_reset(RST_PORT,RST_PIN) -#define RST_TOGGLE gpio_bit_toggle(RST_PORT,RST_PIN) +private: + GT1151(); + ~GT1151() = default; + GT1151(const GT1151 &) = delete; + GT1151 &operator=(const GT1151 &) = delete; -#define INT_RCU RCU_GPIOD -#define INT_PORT GPIOD -#define INT_PIN GPIO_PIN_11 -#define INT_ON gpio_bit_set(INT_PORT,INT_PIN) -#define INT_OFF gpio_bit_reset(INT_PORT,INT_PIN) -#define INT_TOGGLE gpio_bit_toggle(INT_PORT,INT_PIN) + uint8_t writeReg(uint16_t reg, const uint8_t *buf, uint8_t len); + void readReg(uint16_t reg, uint8_t *buf, uint8_t len); + uint8_t sendConfig(uint8_t mode); + void extiInit(); -// EXTI 中断配置 -#define TOUCH_EXTI_LINE EXTI_11 -#define TOUCH_EXTI_IRQn EXTI10_15_IRQn -extern volatile uint8_t g_touch_irq_flag; - -//I2C读写命令 -#define GT_CMD_WR 0X28 //写命令 -#define GT_CMD_RD 0X29 //读命令 - -//GT1151 部分寄存器定义 -#define GT_CTRL_REG 0X8040 //GT1151控制寄存器 -#define GT_CFGS_REG 0X8050 //GT1151地址寄存器 -#define GT_CHECK_REG 0X813C //GT1151验和寄存器 -#define GT_PID_REG 0X8140 //GT1151产品ID寄存器 - -#define GT_GSTID_REG 0X814E //GT1151前检测到的触摸情况 -#define GT_TP1_REG 0X8150 //第一个触摸点数据地址 -#define GT_TP2_REG 0X8158 //第二个触摸点数据地址 -#define GT_TP3_REG 0X8160 //第三个触摸点数据地址 -#define GT_TP4_REG 0X8168 //第四个触摸点数据地址 -#define GT_TP5_REG 0X8170 //第五个触摸点数据地址 - -#define TP_PRES_DOWN 0x80 //触屏被按下 -#define TP_CATH_PRES 0x40 //有按键按下了 -#define CT_MAX_TOUCH 5 //电容屏支持的点数,固定为5点 -// 获取触摸点的活跃状态: 1=按下, 0=抬起 -#define TP_IS_ACTIVE(sta, i) ((sta) & (1 << (i))) -static inline uint8_t TP_ACTIVE_COUNT(uint8_t sta) { - uint8_t c = 0; - for (uint8_t i = 0; i < CT_MAX_TOUCH; i++) if (sta & (1 << i)) c++; - return c; -} -//触摸屏控制器 -typedef struct -{ -// uint8_t (*init)(void); //初始化触摸屏控制器 -// uint8_t (*scan)(uint8_t); //扫描触摸屏.0,屏幕扫描;1,物理坐标; -// void (*adjust)(void); //触摸屏校准 - uint16_t x[CT_MAX_TOUCH]; //当前坐标 - uint16_t y[CT_MAX_TOUCH]; //电容屏有最多5组坐标,电阻屏则用x[0],y[0]代表:此次扫描时,触屏的坐标,用 - uint8_t sta; //笔的状态 - float xfac; - float yfac; - short xoff; - short yoff; - uint8_t touchtype; -}_m_tp_dev; -extern _m_tp_dev tp_dev; //触屏控制器在touch.c里面定义 - -uint8_t GT1151_Send_Cfg(uint8_t mode); -uint8_t GT1151_WR_Reg(uint16_t reg,uint8_t *buf,uint8_t len); -void GT1151_RD_Reg(uint16_t reg,uint8_t *buf,uint8_t len); -uint8_t GT1151_Init(void); -uint8_t GT1151_Scan(uint8_t mode); + uint16_t x_[kMaxTouch] = {0}; + uint16_t y_[kMaxTouch] = {0}; + uint8_t sta_ = 0; + uint8_t touchtype_ = 0; +}; #endif diff --git a/bsp/hardware_init.cpp b/bsp/hardware_init.cpp index 179f724..1d27c52 100644 --- a/bsp/hardware_init.cpp +++ b/bsp/hardware_init.cpp @@ -3,24 +3,19 @@ #include "gd32f4xx.h" #include "gd32f4xx_fmc.h" -ret_code_t hardware_init(void) +namespace HardwareInit { + +RetCode init() { - ret_code_t ret = RET_OK; - - system_clock_config(); - - peripheral_clock_enable(); - - gpio_init(); - - nvic_config(); - - mpu_config(); - - return ret; + systemClockConfig(); + peripheralClockEnable(); + gpioInit(); + nvicConfig(); + mpuConfig(); + return RET_OK; } -void system_clock_config(void) +void systemClockConfig() { uint32_t timeout = 0U; @@ -35,7 +30,6 @@ void system_clock_config(void) } } - /* 配置内部 Flash 等待周期:168MHz 需要 7 个等待周期 */ fmc_wscnt_set(WS_WSCNT_7); RCU_CTL |= RCU_CTL_HXTALEN; @@ -114,7 +108,7 @@ void system_clock_config(void) SystemCoreClock = 168000000U; } -void peripheral_clock_enable(void) +void peripheralClockEnable() { rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_GPIOB); @@ -137,17 +131,15 @@ void peripheral_clock_enable(void) rcu_periph_clock_enable(RCU_EXMC); } -void gpio_init(void) +void gpioInit() { - } -void nvic_config(void) +void nvicConfig() { - } -void mpu_config(void) +void mpuConfig() { MPU->CTRL = 0U; @@ -194,3 +186,5 @@ void mpu_config(void) __DSB(); __ISB(); } + +} diff --git a/bsp/hardware_init.h b/bsp/hardware_init.h index 8110d65..ecd2d4f 100644 --- a/bsp/hardware_init.h +++ b/bsp/hardware_init.h @@ -3,11 +3,15 @@ #include "common_types.h" -ret_code_t hardware_init(void); -void system_clock_config(void); -void peripheral_clock_enable(void); -void gpio_init(void); -void nvic_config(void); -void mpu_config(void); +namespace HardwareInit { -#endif \ No newline at end of file +RetCode init(); +void systemClockConfig(); +void peripheralClockEnable(); +void gpioInit(); +void nvicConfig(); +void mpuConfig(); + +} + +#endif