This commit is contained in:
qiyongshuang
2026-08-10 10:47:09 +08:00
parent 804daf39a2
commit dfe5ceea43
150 changed files with 20271 additions and 58 deletions
+383
View File
@@ -0,0 +1,383 @@
/*********************************************************************
* File Name : lcd.c
* Author : WCH
* Version : V1.0.0
* Date : 2023/8/15
* Description : file for lcd screen.
*********************************************************************************
* Copyright (c) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "lcd.h"
#include "debug.h"
#include "LCD_Font.h"
#define LCD_SOFT_RESET (0x01)
#define LCD_READ_ID (0x04)
#define LCD_READ_DISPLAY_STATUS (0x09)
#define LCD_ON (0x28)
#define LCD_OFF (0x29)
#define LCD_SET_X (0x2a)
#define LCD_SET_Y (0x2b)
#define LCD_MEM_WRITE (0x2C)
#define LCD_MEM_READ (0x2e)
static void LCD_WR_REG(uint8_t data)
{
LCD_DC_CLR();
SPI2_Write(data);
}
static void LCD_WR_DATA(uint8_t data)
{
LCD_DC_SET();
SPI2_Write(data);
}
/*********************************************************************
* @fn Lcd_WriteData_16Bit
*
* @brief write two bytes to the lcd screen
*
* @param Data - the data to write
*
* @return none
*/
void Lcd_WriteData_16Bit(uint16_t Data)
{
SPI2_Write(Data >> 8);
SPI2_Write(Data);
}
/*********************************************************************
* @fn LCD_GPIOInit
*
* @brief Configuring the control gpio for the lcd screen
*
* @return none
*/
void LCD_GPIOInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(LCD_LED_RCC, ENABLE);
RCC_APB2PeriphClockCmd(LCD_DC_RCC, ENABLE);
RCC_APB2PeriphClockCmd(LCD_RST_RCC, ENABLE);
RCC_APB2PeriphClockCmd(LCD_CS_RCC, ENABLE);
GPIO_InitStructure.GPIO_Pin = LCD_LED_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LCD_LED_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LCD_DC_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LCD_DC_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LCD_RST_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LCD_RST_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LCD_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LCD_CS_PORT, &GPIO_InitStructure);
LCD_LED_CLR();
LCD_CS_SET();
}
/*********************************************************************
* @fn LCD_RESET
*
* @brief reset the lcd screen by the rst pin
*
* @return none
*/
void LCD_RESET(void)
{
Delay_Ms(50);
LCD_RST_CLR();
Delay_Ms(100);
LCD_RST_SET();
Delay_Ms(50);
}
static const uint8_t lcd_st7789_init_cmds[] = {
1,
0x3A,
0x05,
2,
0xB0,
0x00,
0xF0,
5,
0xB2,
0x0C,
0x0C,
0x00,
0x33,
0x33,
1,
0xB7,
0x35,
1,
0xBB,
0x19,
1,
0xC0,
0x2C,
1,
0xC2,
0x01,
1,
0xC3,
0x12,
1,
0xC4,
0x20,
1,
0xC6,
0x0F,
2,
0xD0,
0xA4,
0xA1,
14,
0xE0,
0xD0,
0x04,
0x0D,
0x11,
0x13,
0x2B,
0x3F,
0x54,
0x4C,
0x18,
0x0D,
0x0B,
0x1F,
0x23,
14,
0xE1,
0xD0,
0x04,
0x0C,
0x11,
0x13,
0x2C,
0x3F,
0x44,
0x51,
0x2F,
0x1F,
0x1F,
0x20,
0x23,
0,
0x21,
1,
0x36,
#if USE_HORIZONTAL == 0
0,
#elif USE_HORIZONTAL == 1
(1 << 7) | (1 << 6) | (0 << 5),
#elif USE_HORIZONTAL == 2
(1 << 6) | (0 << 7) | (1 << 5),
#elif USE_HORIZONTAL == 3
(1 << 7) | (1 << 5),
#endif
0,
0x11,
0,
0x29,
0xff,
0xff,
};
void LCD_exec_init(uint8_t* init_code)
{
uint8_t* p = init_code;
uint8_t data_len;
uint8_t cmd;
LCD_CS_CLR();
while (1)
{
data_len = *p++;
cmd = *p++;
if (cmd == 0xFF)
{
break;
}
LCD_WR_REG(cmd);
for (uint8_t i = 0; i < data_len; i++)
{
LCD_WR_DATA(*p++);
}
}
LCD_CS_SET();
}
void LCD_Init(void)
{
SPI2_Init();
SPI2_DMA_Init();
LCD_GPIOInit();
LCD_RESET();
LCD_CS_CLR();
LCD_WR_REG(0x28);
LCD_exec_init((uint8_t*)lcd_st7789_init_cmds);
Delay_Ms(10);
LCD_Clear(LCD_BLACK);
Delay_Ms(10);
LCD_LED_SET();
}
/*********************************************************************
* @fn LCD_SetWindows
*
* @brief Setting LCD display window
*
* @param xStar - the bebinning x coordinate of the LCD display window
* yStar - the bebinning y coordinate of the LCD display window
* xEnd - the endning x coordinate of the LCD display window
* yEnd - the endning y coordinate of the LCD display window
*
* @return none
*/
void LCD_SetWindows(uint16_t xStar, uint16_t yStar, uint16_t xEnd, uint16_t yEnd)
{
LCD_CS_CLR();
LCD_WR_REG(LCD_SET_X);
LCD_WR_DATA(xStar >> 8);
LCD_WR_DATA(0x00FF & xStar);
LCD_WR_DATA(xEnd >> 8);
LCD_WR_DATA(0x00FF & xEnd);
LCD_WR_REG(LCD_SET_Y);
LCD_WR_DATA(yStar >> 8);
LCD_WR_DATA(0x00FF & yStar);
LCD_WR_DATA(yEnd >> 8);
LCD_WR_DATA(0x00FF & yEnd);
LCD_WR_REG(LCD_MEM_WRITE);
LCD_CS_SET();
}
// 设置引脚为高电平
void LCD_LED_SET(void)
{
LCD_LED_PORT->BSHR = LCD_LED_PIN;
}
void LCD_CS_SET(void)
{
LCD_CS_PORT->BSHR = LCD_CS_PIN;
}
void LCD_DC_SET(void)
{
LCD_DC_PORT->BSHR = LCD_DC_PIN;
}
void LCD_RST_SET(void)
{
LCD_RST_PORT->BSHR = LCD_RST_PIN;
}
// 设置引脚为低电平
void LCD_LED_CLR(void)
{
LCD_LED_PORT->BCR = LCD_LED_PIN;
}
void LCD_CS_CLR(void)
{
LCD_CS_PORT->BCR = LCD_CS_PIN;
}
void LCD_DC_CLR(void)
{
LCD_DC_PORT->BCR = LCD_DC_PIN;
}
void LCD_RST_CLR(void)
{
LCD_RST_PORT->BCR = LCD_RST_PIN;
}
void SPI1_wait_for_bsy()
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET)
;
}
void SPI2_wait_for_bsy()
{
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET)
;
}
void SPI2_wait_for_txe()
{
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET)
;
}
void SPI1_wait_for_DMA()
{
while (SPI1_DMA_TX_CH->CNTR > 0)
{
}
}
void SPI2_wait_for_DMA()
{
while (SPI2_DMA_TX_CH->CNTR > 0)
{
}
}
void LCD_Clear(uint16_t color)
{
LCD_Fill(0, 0, LCD_W, LCD_H, color);
}
@@ -0,0 +1,84 @@
/*********************************************************************
* File Name : lcd.h
* Author : WCH
* Version : V1.0.0
* Date : 2023/8/15
* Description : file for lcd screen.
*********************************************************************************
* Copyright (c) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __LCD_H
#define __LCD_H
#include "spi.h"
#include "stdio.h"
#include "stdlib.h"
#include "flash.h"
#include "config.h"
#define USE_HORIZONTAL 2
#if USE_HORIZONTAL == 0 || USE_HORIZONTAL == 1
#define LCD_W 240
#define LCD_H 320
#endif
#if USE_HORIZONTAL == 2 || USE_HORIZONTAL == 3
#define LCD_W 320
#define LCD_H 240
#endif
#define LCD_WIDTH LCD_W
#define LCD_HEIGHT LCD_H
#define LCD_SWAP16(x) ((((x) & 0xFF) << 8) | (((x) >> 8) & 0xFF))
#define RGB888_TO_RGB565(r, g, b) LCD_SWAP16((((r) & 0xF8) << 8) | (((g) & 0xFC) << 3) | (((b) & 0xF8) >> 3))
#define RGB24_TO_RGB565(rgb) LCD_SWAP16((((rgb) >> 8) & 0xF800) | (((rgb) >> 5) & 0x07E0) | (((rgb) >> 3) & 0x001F))
#define LCD_red RGB888_TO_RGB565(255, 0, 0)
#define LCD_green RGB888_TO_RGB565(0, 255, 0)
#define LCD_blue RGB888_TO_RGB565(0, 0, 255)
#define LCD_white RGB888_TO_RGB565(255, 255, 255)
#define LCD_gray RGB888_TO_RGB565(128, 128, 128)
#define LCD_black RGB888_TO_RGB565(0, 0, 0)
#define LCD_yellow RGB888_TO_RGB565(255, 255, 0)
#define LCD_WHITE LCD_white
#define LCD_BLACK LCD_black
#define LCD_BLUE LCD_blue
#define LCD_RED LCD_red
#define LCD_MAGENTA RGB888_TO_RGB565(255, 0, 255)
#define LCD_GREEN LCD_green
#define LCD_CYAN RGB888_TO_RGB565(0, 255, 255)
#define LCD_YELLOW LCD_yellow
#define LCD_BROWN RGB888_TO_RGB565(165, 42, 42)
#define LCD_GRAY LCD_gray
void LCD_LED_SET(void);
void LCD_CS_SET(void);
void LCD_DC_SET(void);
void LCD_RST_SET(void);
void LCD_LED_CLR(void);
void LCD_CS_CLR(void);
void LCD_DC_CLR(void);
void LCD_RST_CLR(void);
void SPI1_wait_for_bsy();
void SPI2_wait_for_bsy();
void SPI2_wait_for_txe();
void SPI1_wait_for_DMA();
void SPI2_wait_for_DMA();
void LCD_Init(void);
void LCD_Clear(uint16_t Color);
void Lcd_WriteData_16Bit(uint16_t Data);
void LCD_SetWindows(uint16_t xStar, uint16_t yStar, uint16_t xEnd, uint16_t yEnd);
void LCD_Fill(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t Color);
#endif
@@ -0,0 +1,200 @@
#include "LCD_Font.h"
#include "wui.h"
//12*12
const unsigned char asc2_1206[95][12]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/
{0x00,0x00,0x00,0x00,0x3F,0x40,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/
{0x00,0x00,0x30,0x00,0x40,0x00,0x30,0x00,0x40,0x00,0x00,0x00},/*""",2*/
{0x09,0x00,0x0B,0xC0,0x3D,0x00,0x0B,0xC0,0x3D,0x00,0x09,0x00},/*"#",3*/
{0x18,0xC0,0x24,0x40,0x7F,0xE0,0x22,0x40,0x31,0x80,0x00,0x00},/*"$",4*/
{0x18,0x00,0x24,0xC0,0x1B,0x00,0x0D,0x80,0x32,0x40,0x01,0x80},/*"%",5*/
{0x03,0x80,0x1C,0x40,0x27,0x40,0x1C,0x80,0x07,0x40,0x00,0x40},/*"&",6*/
{0x10,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",7*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x80,0x20,0x40,0x40,0x20},/*"(",8*/
{0x00,0x00,0x40,0x20,0x20,0x40,0x1F,0x80,0x00,0x00,0x00,0x00},/*")",9*/
{0x09,0x00,0x06,0x00,0x1F,0x80,0x06,0x00,0x09,0x00,0x00,0x00},/*"*",10*/
{0x04,0x00,0x04,0x00,0x3F,0x80,0x04,0x00,0x04,0x00,0x00,0x00},/*"+",11*/
{0x00,0x10,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",12*/
{0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x00,0x00},/*"-",13*/
{0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",14*/
{0x00,0x20,0x01,0xC0,0x06,0x00,0x38,0x00,0x40,0x00,0x00,0x00},/*"/",15*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"0",16*/
{0x00,0x00,0x10,0x40,0x3F,0xC0,0x00,0x40,0x00,0x00,0x00,0x00},/*"1",17*/
{0x18,0xC0,0x21,0x40,0x22,0x40,0x24,0x40,0x18,0x40,0x00,0x00},/*"2",18*/
{0x10,0x80,0x20,0x40,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"3",19*/
{0x02,0x00,0x0D,0x00,0x11,0x00,0x3F,0xC0,0x01,0x40,0x00,0x00},/*"4",20*/
{0x3C,0x80,0x24,0x40,0x24,0x40,0x24,0x40,0x23,0x80,0x00,0x00},/*"5",21*/
{0x1F,0x80,0x24,0x40,0x24,0x40,0x34,0x40,0x03,0x80,0x00,0x00},/*"6",22*/
{0x30,0x00,0x20,0x00,0x27,0xC0,0x38,0x00,0x20,0x00,0x00,0x00},/*"7",23*/
{0x1B,0x80,0x24,0x40,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"8",24*/
{0x1C,0x00,0x22,0xC0,0x22,0x40,0x22,0x40,0x1F,0x80,0x00,0x00},/*"9",25*/
{0x00,0x00,0x00,0x00,0x08,0x40,0x00,0x00,0x00,0x00,0x00,0x00},/*":",26*/
{0x00,0x00,0x00,0x00,0x04,0x60,0x00,0x00,0x00,0x00,0x00,0x00},/*";",27*/
{0x00,0x00,0x04,0x00,0x0A,0x00,0x11,0x00,0x20,0x80,0x40,0x40},/*"<",28*/
{0x09,0x00,0x09,0x00,0x09,0x00,0x09,0x00,0x09,0x00,0x00,0x00},/*"=",29*/
{0x00,0x00,0x40,0x40,0x20,0x80,0x11,0x00,0x0A,0x00,0x04,0x00},/*">",30*/
{0x18,0x00,0x20,0x00,0x23,0x40,0x24,0x00,0x18,0x00,0x00,0x00},/*"?",31*/
{0x1F,0x80,0x20,0x40,0x27,0x40,0x29,0x40,0x1F,0x40,0x00,0x00},/*"@",32*/
{0x00,0x40,0x07,0xC0,0x39,0x00,0x0F,0x00,0x01,0xC0,0x00,0x40},/*"A",33*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"B",34*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x30,0x80,0x00,0x00},/*"C",35*/
{0x20,0x40,0x3F,0xC0,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"D",36*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x2E,0x40,0x30,0xC0,0x00,0x00},/*"E",37*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x2E,0x00,0x30,0x00,0x00,0x00},/*"F",38*/
{0x0F,0x00,0x10,0x80,0x20,0x40,0x22,0x40,0x33,0x80,0x02,0x00},/*"G",39*/
{0x20,0x40,0x3F,0xC0,0x04,0x00,0x04,0x00,0x3F,0xC0,0x20,0x40},/*"H",40*/
{0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,0x20,0x40,0x00,0x00},/*"I",41*/
{0x00,0x60,0x20,0x20,0x20,0x20,0x3F,0xC0,0x20,0x00,0x20,0x00},/*"J",42*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x0B,0x00,0x30,0xC0,0x20,0x40},/*"K",43*/
{0x20,0x40,0x3F,0xC0,0x20,0x40,0x00,0x40,0x00,0x40,0x00,0xC0},/*"L",44*/
{0x3F,0xC0,0x3C,0x00,0x03,0xC0,0x3C,0x00,0x3F,0xC0,0x00,0x00},/*"M",45*/
{0x20,0x40,0x3F,0xC0,0x0C,0x40,0x23,0x00,0x3F,0xC0,0x20,0x00},/*"N",46*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"O",47*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x24,0x00,0x18,0x00,0x00,0x00},/*"P",48*/
{0x1F,0x80,0x21,0x40,0x21,0x40,0x20,0xE0,0x1F,0xA0,0x00,0x00},/*"Q",49*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x26,0x00,0x19,0xC0,0x00,0x40},/*"R",50*/
{0x18,0xC0,0x24,0x40,0x24,0x40,0x22,0x40,0x31,0x80,0x00,0x00},/*"S",51*/
{0x30,0x00,0x20,0x40,0x3F,0xC0,0x20,0x40,0x30,0x00,0x00,0x00},/*"T",52*/
{0x20,0x00,0x3F,0x80,0x00,0x40,0x00,0x40,0x3F,0x80,0x20,0x00},/*"U",53*/
{0x20,0x00,0x3E,0x00,0x01,0xC0,0x07,0x00,0x38,0x00,0x20,0x00},/*"V",54*/
{0x38,0x00,0x07,0xC0,0x3C,0x00,0x07,0xC0,0x38,0x00,0x00,0x00},/*"W",55*/
{0x20,0x40,0x39,0xC0,0x06,0x00,0x39,0xC0,0x20,0x40,0x00,0x00},/*"X",56*/
{0x20,0x00,0x38,0x40,0x07,0xC0,0x38,0x40,0x20,0x00,0x00,0x00},/*"Y",57*/
{0x30,0x40,0x21,0xC0,0x26,0x40,0x38,0x40,0x20,0xC0,0x00,0x00},/*"Z",58*/
{0x00,0x00,0x00,0x00,0x7F,0xE0,0x40,0x20,0x40,0x20,0x00,0x00},/*"[",59*/
{0x00,0x00,0x70,0x00,0x0C,0x00,0x03,0x80,0x00,0x40,0x00,0x00},/*"\",60*/
{0x00,0x00,0x40,0x20,0x40,0x20,0x7F,0xE0,0x00,0x00,0x00,0x00},/*"]",61*/
{0x00,0x00,0x20,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00},/*"^",62*/
{0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10},/*"_",63*/
{0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"`",64*/
{0x00,0x00,0x02,0x80,0x05,0x40,0x05,0x40,0x03,0xC0,0x00,0x40},/*"a",65*/
{0x20,0x00,0x3F,0xC0,0x04,0x40,0x04,0x40,0x03,0x80,0x00,0x00},/*"b",66*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x40,0x06,0x40,0x00,0x00},/*"c",67*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x24,0x40,0x3F,0xC0,0x00,0x40},/*"d",68*/
{0x00,0x00,0x03,0x80,0x05,0x40,0x05,0x40,0x03,0x40,0x00,0x00},/*"e",69*/
{0x00,0x00,0x04,0x40,0x1F,0xC0,0x24,0x40,0x24,0x40,0x20,0x00},/*"f",70*/
{0x00,0x00,0x02,0xE0,0x05,0x50,0x05,0x50,0x06,0x50,0x04,0x20},/*"g",71*/
{0x20,0x40,0x3F,0xC0,0x04,0x40,0x04,0x00,0x03,0xC0,0x00,0x40},/*"h",72*/
{0x00,0x00,0x04,0x40,0x27,0xC0,0x00,0x40,0x00,0x00,0x00,0x00},/*"i",73*/
{0x00,0x10,0x00,0x10,0x04,0x10,0x27,0xE0,0x00,0x00,0x00,0x00},/*"j",74*/
{0x20,0x40,0x3F,0xC0,0x01,0x40,0x07,0x00,0x04,0xC0,0x04,0x40},/*"k",75*/
{0x20,0x40,0x20,0x40,0x3F,0xC0,0x00,0x40,0x00,0x40,0x00,0x00},/*"l",76*/
{0x07,0xC0,0x04,0x00,0x07,0xC0,0x04,0x00,0x03,0xC0,0x00,0x00},/*"m",77*/
{0x04,0x40,0x07,0xC0,0x04,0x40,0x04,0x00,0x03,0xC0,0x00,0x40},/*"n",78*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x40,0x03,0x80,0x00,0x00},/*"o",79*/
{0x04,0x10,0x07,0xF0,0x04,0x50,0x04,0x40,0x03,0x80,0x00,0x00},/*"p",80*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x50,0x07,0xF0,0x00,0x10},/*"q",81*/
{0x04,0x40,0x07,0xC0,0x02,0x40,0x04,0x00,0x04,0x00,0x00,0x00},/*"r",82*/
{0x00,0x00,0x06,0x40,0x05,0x40,0x05,0x40,0x04,0xC0,0x00,0x00},/*"s",83*/
{0x00,0x00,0x04,0x00,0x1F,0x80,0x04,0x40,0x00,0x40,0x00,0x00},/*"t",84*/
{0x04,0x00,0x07,0x80,0x00,0x40,0x04,0x40,0x07,0xC0,0x00,0x40},/*"u",85*/
{0x04,0x00,0x07,0x00,0x04,0xC0,0x01,0x80,0x06,0x00,0x04,0x00},/*"v",86*/
{0x06,0x00,0x01,0xC0,0x07,0x00,0x01,0xC0,0x06,0x00,0x00,0x00},/*"w",87*/
{0x04,0x40,0x06,0xC0,0x01,0x00,0x06,0xC0,0x04,0x40,0x00,0x00},/*"x",88*/
{0x04,0x10,0x07,0x10,0x04,0xE0,0x01,0x80,0x06,0x00,0x04,0x00},/*"y",89*/
{0x00,0x00,0x04,0x40,0x05,0xC0,0x06,0x40,0x04,0x40,0x00,0x00},/*"z",90*/
{0x00,0x00,0x00,0x00,0x04,0x00,0x7B,0xE0,0x40,0x20,0x00,0x00},/*"{",91*/
{0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xF0,0x00,0x00,0x00,0x00},/*"|",92*/
{0x00,0x00,0x40,0x20,0x7B,0xE0,0x04,0x00,0x00,0x00,0x00,0x00},/*"}",93*/
{0x40,0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x20,0x00,0x40,0x00},/*"~",94*/
};
//16*16
const unsigned char asc2_1608[95][16]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xCC,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/
{0x00,0x00,0x08,0x00,0x30,0x00,0x60,0x00,0x08,0x00,0x30,0x00,0x60,0x00,0x00,0x00},/*""",2*/
{0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x00,0x00},/*"#",3*/
{0x00,0x00,0x0E,0x18,0x11,0x04,0x3F,0xFF,0x10,0x84,0x0C,0x78,0x00,0x00,0x00,0x00},/*"$",4*/
{0x0F,0x00,0x10,0x84,0x0F,0x38,0x00,0xC0,0x07,0x78,0x18,0x84,0x00,0x78,0x00,0x00},/*"%",5*/
{0x00,0x78,0x0F,0x84,0x10,0xC4,0x11,0x24,0x0E,0x98,0x00,0xE4,0x00,0x84,0x00,0x08},/*"&",6*/
{0x08,0x00,0x68,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",7*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x18,0x18,0x20,0x04,0x40,0x02,0x00,0x00},/*"(",8*/
{0x00,0x00,0x40,0x02,0x20,0x04,0x18,0x18,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00},/*")",9*/
{0x02,0x40,0x02,0x40,0x01,0x80,0x0F,0xF0,0x01,0x80,0x02,0x40,0x02,0x40,0x00,0x00},/*"*",10*/
{0x00,0x80,0x00,0x80,0x00,0x80,0x0F,0xF8,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x00},/*"+",11*/
{0x00,0x01,0x00,0x0D,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",12*/
{0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80},/*"-",13*/
{0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",14*/
{0x00,0x00,0x00,0x06,0x00,0x18,0x00,0x60,0x01,0x80,0x06,0x00,0x18,0x00,0x20,0x00},/*"/",15*/
{0x00,0x00,0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"0",16*/
{0x00,0x00,0x08,0x04,0x08,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"1",17*/
{0x00,0x00,0x0E,0x0C,0x10,0x14,0x10,0x24,0x10,0x44,0x11,0x84,0x0E,0x0C,0x00,0x00},/*"2",18*/
{0x00,0x00,0x0C,0x18,0x10,0x04,0x11,0x04,0x11,0x04,0x12,0x88,0x0C,0x70,0x00,0x00},/*"3",19*/
{0x00,0x00,0x00,0xE0,0x03,0x20,0x04,0x24,0x08,0x24,0x1F,0xFC,0x00,0x24,0x00,0x00},/*"4",20*/
{0x00,0x00,0x1F,0x98,0x10,0x84,0x11,0x04,0x11,0x04,0x10,0x88,0x10,0x70,0x00,0x00},/*"5",21*/
{0x00,0x00,0x07,0xF0,0x08,0x88,0x11,0x04,0x11,0x04,0x18,0x88,0x00,0x70,0x00,0x00},/*"6",22*/
{0x00,0x00,0x1C,0x00,0x10,0x00,0x10,0xFC,0x13,0x00,0x1C,0x00,0x10,0x00,0x00,0x00},/*"7",23*/
{0x00,0x00,0x0E,0x38,0x11,0x44,0x10,0x84,0x10,0x84,0x11,0x44,0x0E,0x38,0x00,0x00},/*"8",24*/
{0x00,0x00,0x07,0x00,0x08,0x8C,0x10,0x44,0x10,0x44,0x08,0x88,0x07,0xF0,0x00,0x00},/*"9",25*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x03,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},/*":",26*/
{0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*";",27*/
{0x00,0x00,0x00,0x80,0x01,0x40,0x02,0x20,0x04,0x10,0x08,0x08,0x10,0x04,0x00,0x00},/*"<",28*/
{0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x02,0x20,0x00,0x00},/*"=",29*/
{0x00,0x00,0x10,0x04,0x08,0x08,0x04,0x10,0x02,0x20,0x01,0x40,0x00,0x80,0x00,0x00},/*">",30*/
{0x00,0x00,0x0E,0x00,0x12,0x00,0x10,0x0C,0x10,0x6C,0x10,0x80,0x0F,0x00,0x00,0x00},/*"?",31*/
{0x03,0xE0,0x0C,0x18,0x13,0xE4,0x14,0x24,0x17,0xC4,0x08,0x28,0x07,0xD0,0x00,0x00},/*"@",32*/
{0x00,0x04,0x00,0x3C,0x03,0xC4,0x1C,0x40,0x07,0x40,0x00,0xE4,0x00,0x1C,0x00,0x04},/*"A",33*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x11,0x04,0x0E,0x88,0x00,0x70,0x00,0x00},/*"B",34*/
{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x04,0x10,0x08,0x1C,0x10,0x00,0x00},/*"C",35*/
{0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"D",36*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x17,0xC4,0x10,0x04,0x08,0x18,0x00,0x00},/*"E",37*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x17,0xC0,0x10,0x00,0x08,0x00,0x00,0x00},/*"F",38*/
{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x44,0x1C,0x78,0x00,0x40,0x00,0x00},/*"G",39*/
{0x10,0x04,0x1F,0xFC,0x10,0x84,0x00,0x80,0x00,0x80,0x10,0x84,0x1F,0xFC,0x10,0x04},/*"H",40*/
{0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x00,0x00,0x00,0x00},/*"I",41*/
{0x00,0x03,0x00,0x01,0x10,0x01,0x10,0x01,0x1F,0xFE,0x10,0x00,0x10,0x00,0x00,0x00},/*"J",42*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x03,0x80,0x14,0x64,0x18,0x1C,0x10,0x04,0x00,0x00},/*"K",43*/
{0x10,0x04,0x1F,0xFC,0x10,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x0C,0x00,0x00},/*"L",44*/
{0x10,0x04,0x1F,0xFC,0x1F,0x00,0x00,0xFC,0x1F,0x00,0x1F,0xFC,0x10,0x04,0x00,0x00},/*"M",45*/
{0x10,0x04,0x1F,0xFC,0x0C,0x04,0x03,0x00,0x00,0xE0,0x10,0x18,0x1F,0xFC,0x10,0x00},/*"N",46*/
{0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"O",47*/
{0x10,0x04,0x1F,0xFC,0x10,0x84,0x10,0x80,0x10,0x80,0x10,0x80,0x0F,0x00,0x00,0x00},/*"P",48*/
{0x07,0xF0,0x08,0x18,0x10,0x24,0x10,0x24,0x10,0x1C,0x08,0x0A,0x07,0xF2,0x00,0x00},/*"Q",49*/
{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x11,0xC0,0x11,0x30,0x0E,0x0C,0x00,0x04},/*"R",50*/
{0x00,0x00,0x0E,0x1C,0x11,0x04,0x10,0x84,0x10,0x84,0x10,0x44,0x1C,0x38,0x00,0x00},/*"S",51*/
{0x18,0x00,0x10,0x00,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x00,0x18,0x00,0x00,0x00},/*"T",52*/
{0x10,0x00,0x1F,0xF8,0x10,0x04,0x00,0x04,0x00,0x04,0x10,0x04,0x1F,0xF8,0x10,0x00},/*"U",53*/
{0x10,0x00,0x1E,0x00,0x11,0xE0,0x00,0x1C,0x00,0x70,0x13,0x80,0x1C,0x00,0x10,0x00},/*"V",54*/
{0x1F,0xC0,0x10,0x3C,0x00,0xE0,0x1F,0x00,0x00,0xE0,0x10,0x3C,0x1F,0xC0,0x00,0x00},/*"W",55*/
{0x10,0x04,0x18,0x0C,0x16,0x34,0x01,0xC0,0x01,0xC0,0x16,0x34,0x18,0x0C,0x10,0x04},/*"X",56*/
{0x10,0x00,0x1C,0x00,0x13,0x04,0x00,0xFC,0x13,0x04,0x1C,0x00,0x10,0x00,0x00,0x00},/*"Y",57*/
{0x08,0x04,0x10,0x1C,0x10,0x64,0x10,0x84,0x13,0x04,0x1C,0x04,0x10,0x18,0x00,0x00},/*"Z",58*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFE,0x40,0x02,0x40,0x02,0x40,0x02,0x00,0x00},/*"[",59*/
{0x00,0x00,0x30,0x00,0x0C,0x00,0x03,0x80,0x00,0x60,0x00,0x1C,0x00,0x03,0x00,0x00},/*"\",60*/
{0x00,0x00,0x40,0x02,0x40,0x02,0x40,0x02,0x7F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},/*"]",61*/
{0x00,0x00,0x00,0x00,0x20,0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00},/*"^",62*/
{0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01},/*"_",63*/
{0x00,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"`",64*/
{0x00,0x00,0x00,0x98,0x01,0x24,0x01,0x44,0x01,0x44,0x01,0x44,0x00,0xFC,0x00,0x04},/*"a",65*/
{0x10,0x00,0x1F,0xFC,0x00,0x88,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},/*"b",66*/
{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x00},/*"c",67*/
{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x11,0x08,0x1F,0xFC,0x00,0x04},/*"d",68*/
{0x00,0x00,0x00,0xF8,0x01,0x44,0x01,0x44,0x01,0x44,0x01,0x44,0x00,0xC8,0x00,0x00},/*"e",69*/
{0x00,0x00,0x01,0x04,0x01,0x04,0x0F,0xFC,0x11,0x04,0x11,0x04,0x11,0x00,0x18,0x00},/*"f",70*/
{0x00,0x00,0x00,0xD6,0x01,0x29,0x01,0x29,0x01,0x29,0x01,0xC9,0x01,0x06,0x00,0x00},/*"g",71*/
{0x10,0x04,0x1F,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},/*"h",72*/
{0x00,0x00,0x01,0x04,0x19,0x04,0x19,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"i",73*/
{0x00,0x00,0x00,0x03,0x00,0x01,0x01,0x01,0x19,0x01,0x19,0xFE,0x00,0x00,0x00,0x00},/*"j",74*/
{0x10,0x04,0x1F,0xFC,0x00,0x24,0x00,0x40,0x01,0xB4,0x01,0x0C,0x01,0x04,0x00,0x00},/*"k",75*/
{0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"l",76*/
{0x01,0x04,0x01,0xFC,0x01,0x04,0x01,0x00,0x01,0xFC,0x01,0x04,0x01,0x00,0x00,0xFC},/*"m",77*/
{0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},/*"n",78*/
{0x00,0x00,0x00,0xF8,0x01,0x04,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0xF8,0x00,0x00},/*"o",79*/
{0x01,0x01,0x01,0xFF,0x00,0x85,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},/*"p",80*/
{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x05,0x01,0xFF,0x00,0x01},/*"q",81*/
{0x01,0x04,0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x04,0x01,0x00,0x01,0x80,0x00,0x00},/*"r",82*/
{0x00,0x00,0x00,0xCC,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x98,0x00,0x00},/*"s",83*/
{0x00,0x00,0x01,0x00,0x01,0x00,0x07,0xF8,0x01,0x04,0x01,0x04,0x00,0x00,0x00,0x00},/*"t",84*/
{0x01,0x00,0x01,0xF8,0x00,0x04,0x00,0x04,0x00,0x04,0x01,0x08,0x01,0xFC,0x00,0x04},/*"u",85*/
{0x01,0x00,0x01,0x80,0x01,0x70,0x00,0x0C,0x00,0x10,0x01,0x60,0x01,0x80,0x01,0x00},/*"v",86*/
{0x01,0xF0,0x01,0x0C,0x00,0x30,0x01,0xC0,0x00,0x30,0x01,0x0C,0x01,0xF0,0x01,0x00},/*"w",87*/
{0x00,0x00,0x01,0x04,0x01,0x8C,0x00,0x74,0x01,0x70,0x01,0x8C,0x01,0x04,0x00,0x00},/*"x",88*/
{0x01,0x01,0x01,0x81,0x01,0x71,0x00,0x0E,0x00,0x18,0x01,0x60,0x01,0x80,0x01,0x00},/*"y",89*/
{0x00,0x00,0x01,0x84,0x01,0x0C,0x01,0x34,0x01,0x44,0x01,0x84,0x01,0x0C,0x00,0x00},/*"z",90*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x3E,0xFC,0x40,0x02,0x40,0x02},/*"{",91*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},/*"|",92*/
{0x00,0x00,0x40,0x02,0x40,0x02,0x3E,0xFC,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"}",93*/
{0x00,0x00,0x60,0x00,0x80,0x00,0x80,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x20,0x00},/*"~",94*/
};
@@ -0,0 +1,18 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : lcd_font.h
* Author : WCH
* Version : V1.0.1
* Date : 2025/09/15
* Description : ASCII character library.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __LCD_FONT_H
#define __LCD_FONT_H
extern const unsigned char asc2_1206[95][12];
extern const unsigned char asc2_1608[95][16];
#endif
@@ -0,0 +1,36 @@
/*********************************************************************
* File Name : config.h
* Author : WCH
* Version : V1.0.0
* Date : 2023/8/15
* Description : file for Config.
*********************************************************************************
* Copyright (c) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __CONFIG_H__
#define __CONFIG_H__
/* SPI FLASH and LCD_SCREEN GPIO Definition */
#define FLASH_CS_PORT GPIOA
#define FLASH_CS_RCC RCC_APB2Periph_GPIOA
#define FLASH_CS_PIN (GPIO_Pin_4)
#define LCD_LED_RCC RCC_APB2Periph_GPIOB
#define LCD_LED_PORT GPIOB
#define LCD_LED_PIN (GPIO_Pin_9)
#define LCD_DC_RCC RCC_APB2Periph_GPIOB
#define LCD_DC_PORT GPIOB
#define LCD_DC_PIN (GPIO_Pin_10)
#define LCD_RST_RCC RCC_APB2Periph_GPIOB
#define LCD_RST_PORT GPIOB
#define LCD_RST_PIN (GPIO_Pin_11)
#define LCD_CS_RCC RCC_APB2Periph_GPIOB
#define LCD_CS_PORT GPIOB
#define LCD_CS_PIN (GPIO_Pin_12)
#endif
@@ -0,0 +1,465 @@
/*********************************************************************
* File Name : flash.c
* Author : WCH
* Version : V1.0.0
* Date : 2023/8/15
* Description : file for spi flash.
*********************************************************************************
* Copyright (c) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "flash.h"
#include "spi.h"
static uint8_t FLASH_readWrite(uint8_t TxData)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
;
SPI_I2S_SendData(SPI1, TxData);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
;
return SPI_I2S_ReceiveData(SPI1);
}
/*********************************************************************
* @fn cs_pin_init
*
* @brief gpio cs pin init
*
* @return none
*/
static void cs_pin_init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(FLASH_CS_RCC, ENABLE);
GPIO_InitStructure.GPIO_Pin = FLASH_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(FLASH_CS_PORT, &GPIO_InitStructure);
}
/*********************************************************************
* @fn SPI_FLASH_Init
*
* @brief SPI FLASH Init
*
* @return none
*/
void SPI_FLASH_Init(void)
{
SPI1_Init();
SPI1_Read_DMA_Init();
cs_pin_init();
FLASH_CS_SET;
}
/*********************************************************************
* @fn SPI_Flash_ReadSR
*
* @brief Read W25Qxx status register.
* !!BIT7 6 5 4 3 2 1 0
* !!SPR RV TB BP2 BP1 BP0 WEL BUSY
*
* @return byte - status register value.
*/
uint8_t SPI_Flash_ReadSR(void)
{
uint8_t byte = 0;
FLASH_CS_CLR;
FLASH_readWrite(W25X_ReadStatusReg);
byte = FLASH_readWrite(0Xff);
FLASH_CS_SET;
return byte;
}
/*********************************************************************
* @fn SPI_FLASH_Write_SR
*
* @brief Write W25Qxx status register.
*
* @param sr - status register value.
*
* @return none
*/
void SPI_FLASH_Write_SR(uint8_t sr)
{
FLASH_CS_CLR;
FLASH_readWrite(W25X_WriteStatusReg);
FLASH_readWrite(sr);
FLASH_CS_SET;
}
/*********************************************************************
* @fn SPI_Flash_Wait_Busy
*
* @brief Wait flash free.
*
* @return none
*/
void SPI_Flash_Wait_Busy(void)
{
while ((SPI_Flash_ReadSR() & 0x01) == 0x01)
;
}
/*********************************************************************
* @fn SPI_FLASH_Write_Enable
*
* @brief Enable flash write.
*
* @return none
*/
void SPI_FLASH_Write_Enable(void)
{
FLASH_CS_CLR;
FLASH_readWrite(W25X_WriteEnable);
FLASH_CS_SET;
}
/*********************************************************************
* @fn SPI_FLASH_Write_Disable
*
* @brief Disable flash write.
*
* @return none
*/
void SPI_FLASH_Write_Disable(void)
{
FLASH_CS_CLR;
FLASH_readWrite(W25X_WriteDisable);
FLASH_CS_SET;
}
/*********************************************************************
* @fn SPI_Flash_ReadID
*
* @brief Read flash ID.
*
* @return Temp - FLASH ID.
*/
uint16_t SPI_Flash_ReadID(void)
{
uint16_t Temp = 0;
FLASH_CS_CLR;
FLASH_readWrite(W25X_ManufactDeviceID);
FLASH_readWrite(0x00);
FLASH_readWrite(0x00);
FLASH_readWrite(0x00);
Temp |= FLASH_readWrite(0xFF) << 8;
Temp |= FLASH_readWrite(0xFF);
FLASH_CS_SET;
return Temp;
}
/*********************************************************************
* @fn SPI_Flash_ReadJedecDeviceID
*
* @brief Read JedecDevice ID.
*
* @return JedecDevice ID.
*/
uint32_t SPI_Flash_ReadJedecDeviceID(void)
{
uint32_t Temp = 0;
FLASH_CS_CLR;
FLASH_readWrite(W25X_JedecDeviceID);
Temp |= FLASH_readWrite(0xFF) << 16;
Temp |= FLASH_readWrite(0xFF) << 8;
Temp |= FLASH_readWrite(0xFF);
FLASH_CS_SET;
return Temp;
}
/*********************************************************************
* @fn SPI_Flash_Erase_Sector
*
* @brief Erase one sector(4Kbyte).
*
* @param Dst_Addr - 0 !! 2047
*
* @return none
*/
void SPI_Flash_Erase_Sector(uint32_t Dst_Addr)
{
Dst_Addr *= 4096;
SPI_FLASH_Write_Enable();
SPI_Flash_Wait_Busy();
FLASH_CS_CLR;
FLASH_readWrite(W25X_SectorErase);
FLASH_readWrite((uint8_t)((Dst_Addr) >> 16));
FLASH_readWrite((uint8_t)((Dst_Addr) >> 8));
FLASH_readWrite((uint8_t)Dst_Addr);
FLASH_CS_SET;
SPI_Flash_Wait_Busy();
}
/*********************************************************************
* @fn SPI_Flash_Read
*
* @brief Read data from flash.
*
* @param pBuffer -
* ReadAddr -Initial address(24bit).
* size - Data length.
*
* @return none
*/
void SPI_Flash_Read(uint8_t *pBuffer, uint32_t ReadAddr, uint16_t size)
{
uint16_t i;
FLASH_CS_CLR;
FLASH_readWrite(W25X_ReadData);
FLASH_readWrite((uint8_t)((ReadAddr) >> 16));
FLASH_readWrite((uint8_t)((ReadAddr) >> 8));
FLASH_readWrite((uint8_t)ReadAddr);
for (i = 0; i < size; i++)
{
pBuffer[i] = FLASH_readWrite(0XFF);
}
FLASH_CS_SET;
}
/*********************************************************************
* @fn SPI_Flash_Write_Page
*
* @brief Write data by one page.
*
* @param pBuffer -
* WriteAddr - Initial address(24bit).
* size - Data length.
*
* @return none
*/
void SPI_Flash_Write_Page(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t size)
{
uint16_t i;
SPI_FLASH_Write_Enable();
FLASH_CS_CLR;
FLASH_readWrite(W25X_PageProgram);
FLASH_readWrite((uint8_t)((WriteAddr) >> 16));
FLASH_readWrite((uint8_t)((WriteAddr) >> 8));
FLASH_readWrite((uint8_t)WriteAddr);
for (i = 0; i < size; i++)
{
FLASH_readWrite(pBuffer[i]);
}
FLASH_CS_SET;
SPI_Flash_Wait_Busy();
}
/*********************************************************************
* @fn SPI_Flash_Write_NoCheck
*
* @brief Write data to flash.(need Erase)
* All data in address rang is 0xFF.
*
* @param pBuffer -
* WriteAddr - Initial address(24bit).
* size - Data length.
*
* @return none
*/
void SPI_Flash_Write_NoCheck(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t size)
{
uint16_t pageremain;
pageremain = 256 - WriteAddr % 256;
if (size <= pageremain)
pageremain = size;
while (1)
{
SPI_Flash_Write_Page(pBuffer, WriteAddr, pageremain);
if (size == pageremain)
{
break;
}
else
{
pBuffer += pageremain;
WriteAddr += pageremain;
size -= pageremain;
if (size > 256)
pageremain = 256;
else
pageremain = size;
}
}
}
static uint8_t W25QXX_BUFFER[4096];
/*********************************************************************
* @fn SPI_Flash_Write
*
* @brief Write data to flash.(no need Erase)
*
* @param pBuffer -
* WriteAddr - Initial address(24bit).
* size - Data length.
*
* @return none
*/
void SPI_Flash_Write(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t size)
{
uint32_t secpos;
uint16_t secoff;
uint16_t secremain;
uint16_t i;
secpos = WriteAddr / 4096;
secoff = WriteAddr % 4096;
secremain = 4096 - secoff;
if (size <= secremain)
secremain = size;
while (1)
{
SPI_Flash_Read(W25QXX_BUFFER, secpos * 4096, 4096);
for (i = 0; i < secremain; i++)
{
if (W25QXX_BUFFER[secoff + i] != 0XFF)
break;
}
if (i < secremain)
{
SPI_Flash_Erase_Sector(secpos);
for (i = 0; i < secremain; i++)
{
W25QXX_BUFFER[i + secoff] = pBuffer[i];
}
SPI_Flash_Write_NoCheck(W25QXX_BUFFER, secpos * 4096, 4096);
}
else
{
SPI_Flash_Write_NoCheck(pBuffer, WriteAddr, secremain);
}
if (size == secremain)
{
break;
}
else
{
secpos++;
secoff = 0;
pBuffer += secremain;
WriteAddr += secremain;
size -= secremain;
if (size > 4096)
{
secremain = 4096;
}
else
{
secremain = size;
}
}
}
}
/*********************************************************************
* @fn SPI_Flash_Erase_Block
*
* @brief Erase all FLASH pages.
*
*
* @return none
*/
void SPI_Flash_Erase_Block(uint32_t Dst_Addr)
{
SPI_FLASH_Write_Enable();
SPI_Flash_Wait_Busy();
FLASH_CS_CLR;
FLASH_readWrite(W25X_BlockErase);
FLASH_readWrite((uint8_t)((Dst_Addr) >> 16));
FLASH_readWrite((uint8_t)((Dst_Addr) >> 8));
FLASH_readWrite((uint8_t)Dst_Addr);
FLASH_CS_SET;
SPI_Flash_Wait_Busy();
}
/*********************************************************************
* @fn SPI_Flash_PowerDown
*
* @brief Enter power down mode.
*
* @return none
*/
void SPI_Flash_PowerDown(void)
{
FLASH_CS_CLR;
FLASH_readWrite(W25X_PowerDown);
FLASH_CS_SET;
Delay_Us(3);
}
/*********************************************************************
* @fn SPI_Flash_WAKEUP
*
* @brief Power down wake up.
*
* @return none
*/
void SPI_Flash_WAKEUP(void)
{
FLASH_CS_CLR;
FLASH_readWrite(W25X_ReleasePowerDown);
FLASH_CS_SET;
Delay_Us(3);
}
/*********************************************************************
* @fn SPI_Flash_Read_dma_start
*
* @brief enable spi dma and set read address
*
* @param ReadAddr - the address in spi flash
*
* @return none
*/
void SPI_Flash_Read_dma_start(uint32_t ReadAddr)
{
FLASH_CS_CLR;
FLASH_readWrite(W25X_FastReadData);
FLASH_readWrite((uint8_t)((ReadAddr) >> 16));
FLASH_readWrite((uint8_t)((ReadAddr) >> 8));
FLASH_readWrite((uint8_t)ReadAddr);
FLASH_readWrite(0xFF);
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, ENABLE);
}
/*********************************************************************
* @fn SPI_Flash_Read_dma_end
*
* @brief disable spi dma
*
* @return none
*/
void SPI_Flash_Read_dma_end(void)
{
FLASH_CS_SET;
SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx | SPI_I2S_DMAReq_Rx, DISABLE);
}
@@ -0,0 +1,57 @@
/*********************************************************************
* File Name : flash.h
* Author : WCH
* Version : V1.0.0
* Date : 2023/8/15
* Description : file for spi flash.
*********************************************************************************
* Copyright (c) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __SPI_FLASH_H
#define __SPI_FLASH_H
#include "debug.h"
#include "spi.h"
#include "config.h"
#define W25X_WriteEnable 0x06
#define W25X_WriteDisable 0x04
#define W25X_ReadStatusReg 0x05
#define W25X_WriteStatusReg 0x01
#define W25X_ReadData 0x03
#define W25X_FastReadData 0x0B
#define W25X_FastReadDual 0x3B
#define W25X_PageProgram 0x02
#define W25X_BlockErase 0xD8
#define W25X_SectorErase 0x20
#define W25X_ChipErase 0xC7
#define W25X_PowerDown 0xB9
#define W25X_ReleasePowerDown 0xAB
#define W25X_DeviceID 0xAB
#define W25X_ManufactDeviceID 0x90
#define W25X_JedecDeviceID 0x9F
#define FLASH_CS_SET (FLASH_CS_PORT->BSHR = FLASH_CS_PIN)
#define FLASH_CS_CLR (FLASH_CS_PORT->BCR = FLASH_CS_PIN)
void SPI_FLASH_Init(void);
uint8_t SPI_Flash_ReadSR(void);
void SPI_FLASH_Write_SR(uint8_t sr);
void SPI_Flash_Wait_Busy(void);
void SPI_FLASH_Write_Enable(void);
void SPI_FLASH_Write_Disable(void);
uint16_t SPI_Flash_ReadID(void);
uint32_t SPI_Flash_ReadJedecDeviceID(void);
void SPI_Flash_Erase_Sector(uint32_t Dst_Addr);
void SPI_Flash_Read(uint8_t *pBuffer, uint32_t ReadAddr, uint16_t size);
void SPI_Flash_Write_Page(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t size);
void SPI_Flash_Write_NoCheck(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t size);
void SPI_Flash_Write(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t size);
void SPI_Flash_Erase_Chip(void);
void SPI_Flash_PowerDown(void);
void SPI_Flash_WAKEUP(void);
void SPI_Flash_Read_dma_start(uint32_t ReadAddr);
void SPI_Flash_Read_dma_end(void);
#endif
+247
View File
@@ -0,0 +1,247 @@
/*********************************************************************
* File Name : spi.c
* Author : WCH
* Version : V1.0.0
* Date : 2023/8/15
* Description : file for spi.
*********************************************************************************
* Copyright (c) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "spi.h"
/*********************************************************************
* @fn SPI1_Init
*
* @brief Initialize SPI1
*
* @return none
*/
void SPI1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
SPI_InitTypeDef SPI_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
SPI1->HSCR = 1;
}
/*********************************************************************
* @fn SPI1_Read_DMA_Init
*
* @brief Initialize DMA for SPI1, In this condition every time send 0XFF to read data
*
* @return none
*/
void SPI1_Read_DMA_Init()
{
static uint32_t DummyByte = 0xff;
DMA_InitTypeDef DMA_InitStructure = {0};
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
// Tx
DMA_DeInit(SPI1_DMA_TX_CH);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DATAR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&DummyByte;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 0;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(SPI1_DMA_TX_CH, &DMA_InitStructure);
// Rx
DMA_DeInit(SPI1_DMA_RX_CH);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI1->DATAR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)NULL;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 0;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(SPI1_DMA_RX_CH, &DMA_InitStructure);
}
/*********************************************************************
* @fn SPI1_Read_DMA
*
* @brief read data from SPI1 via DMA
*
* @param data - the data to recive
* len - read length
*
* @return none
*/
void SPI1_Read_DMA(uint8_t* data, uint16_t len)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET)
;
DMA_Cmd(SPI1_DMA_TX_CH, DISABLE);
DMA_Cmd(SPI1_DMA_RX_CH, DISABLE);
DMA_ClearFlag(DMA1_FLAG_TC3);
DMA_ClearFlag(DMA1_FLAG_TC3);
SPI1_DMA_TX_CH->CNTR = len;
SPI1_DMA_RX_CH->MADDR = (uint32_t)data;
SPI1_DMA_RX_CH->CNTR = len;
DMA_Cmd(SPI1_DMA_TX_CH, ENABLE);
DMA_Cmd(SPI1_DMA_RX_CH, ENABLE);
}
/*********************************************************************
* @fn SPI2_Init
*
* @brief Initialize SPI2
*
* @return none
*/
void SPI2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
SPI_InitTypeDef SPI_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
}
/*********************************************************************
* @fn SPI2_DMA_Init
*
* @brief Initialize DMA for SPI2
*
* @return none
*/
void SPI2_DMA_Init()
{
DMA_InitTypeDef DMA_InitStructure = {0};
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(SPI2_DMA_TX_CH);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&SPI2->DATAR;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)NULL;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 0;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(SPI2_DMA_TX_CH, &DMA_InitStructure);
SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
}
/*********************************************************************
* @fn SPI2_Write
*
* @brief send one byte
*
* @param data - the data to send
*
* @return none
*/
void SPI2_Write(uint8_t data)
{
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET)
;
SPI_I2S_SendData(SPI2, data);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET)
;
}
/*********************************************************************
* @fn SPI2_Write_DMA
*
* @brief send a lot of data
*
* @param data - the data to be send
* len - the length of data
*
* @return none
*/
void SPI2_Write_DMA(uint8_t* data, uint16_t len)
{
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET)
;
DMA_Cmd(SPI2_DMA_TX_CH, DISABLE);
DMA_ClearFlag(SPI2_DMA_TC_FLAG);
SPI2_DMA_TX_CH->MADDR = (uint32_t)data;
SPI2_DMA_TX_CH->CNTR = len;
SPI2_DMA_TX_CH->CFGR = 0x2090;
DMA_Cmd(SPI2_DMA_TX_CH, ENABLE);
}
@@ -0,0 +1,39 @@
/*********************************************************************
* File Name : spi.h
* Author : WCH
* Version : V1.0.0
* Date : 2023/8/15
* Description : file for spi.
*********************************************************************************
* Copyright (c) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __SPI_H__
#define __SPI_H__
#include "debug.h"
#include "config.h"
#define SPI1_DMA_RX_CH DMA1_Channel2
#define SPI1_DMA_TX_CH DMA1_Channel3
#define SPI1_DMA_TC_FLAG DMA1_FLAG_TC3
#define SPI1_DMA_RC_FLAG DMA1_FLAG_TC2
#define SPI2_DMA_RX_CH DMA1_Channel4
#define SPI2_DMA_TX_CH DMA1_Channel5
#define SPI2_DMA_TC_FLAG DMA1_FLAG_TC5
#define SPI2_DMA_RC_FLAG DMA1_FLAG_TC4
void SPI1_Init(void);
void SPI1_Read_DMA_Init(void);
void SPI1_Read_DMA(uint8_t *data, uint16_t len);
void SPI2_Init(void);
void SPI2_DMA_Init(void);
void SPI2_Write(uint8_t data);
void SPI2_Write_DMA(uint8_t *data, uint16_t len);
#endif
@@ -0,0 +1,42 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : ch32v20x_conf.h
* Author : WCH
* Version : V1.0.0
* Date : 2021/06/06
* Description : Library configuration file.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __CH32V20x_CONF_H
#define __CH32V20x_CONF_H
#include "ch32v20x_adc.h"
#include "ch32v20x_bkp.h"
#include "ch32v20x_can.h"
#include "ch32v20x_crc.h"
#include "ch32v20x_dbgmcu.h"
#include "ch32v20x_dma.h"
#include "ch32v20x_exti.h"
#include "ch32v20x_flash.h"
#include "ch32v20x_gpio.h"
#include "ch32v20x_i2c.h"
#include "ch32v20x_iwdg.h"
#include "ch32v20x_pwr.h"
#include "ch32v20x_rcc.h"
#include "ch32v20x_rtc.h"
#include "ch32v20x_spi.h"
#include "ch32v20x_tim.h"
#include "ch32v20x_usart.h"
#include "ch32v20x_wwdg.h"
#include "ch32v20x_it.h"
#include "ch32v20x_misc.h"
#endif /* __CH32V20x_CONF_H */
@@ -0,0 +1,46 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : ch32v20x_it.c
* Author : WCH
* Version : V1.0.0
* Date : 2023/12/29
* Description : Main Interrupt Service Routines.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "ch32v20x_it.h"
void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
/*********************************************************************
* @fn NMI_Handler
*
* @brief This function handles NMI exception.
*
* @return none
*/
void NMI_Handler(void)
{
while (1)
{
}
}
/*********************************************************************
* @fn HardFault_Handler
*
* @brief This function handles Hard Fault exception.
*
* @return none
*/
void HardFault_Handler(void)
{
NVIC_SystemReset();
while (1)
{
}
}
@@ -0,0 +1,20 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : ch32v20x_it.h
* Author : WCH
* Version : V1.0.0
* Date : 2021/06/06
* Description : This file contains the headers of the interrupt handlers.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __CH32V20x_IT_H
#define __CH32V20x_IT_H
#include "debug.h"
#endif /* __CH32V20x_IT_H */
+304
View File
@@ -0,0 +1,304 @@
/*********************************************************************
* File Name : main.c
* Author : WCH
* Version : V1.0.0
* Date : 2026/08/06
* Description : Main program body.
*********************************************************************************
* Copyright (c) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/*
*@Note
A demo for wui
pins:
FLASH_CS -- PA4
FLASH_DO -- PA6
FLASH_DI -- PA7
FLASH_CLK -- PA5
LCD_LED -- PB9
LCD_DC -- PB10
LCD_RST -- PB11
LCD_CS -- PB12
LCD_SCK -- PB13
LCD_MOSI -- PB15
*/
#include <math.h>
#include "debug.h"
#include "string.h"
#include "lcd.h"
#include "flash.h"
#include "config.h"
#include "wui.h"
#include "wui_port.h"
#include "wui_user/wui_page.h"
/* Global define */
/* Global Variable */
#define TIM_PCK (8000ul)
/*********************************************************************
* @fn TIM4_init
*
* @brief Initialize TIM2 for WCHGUI
*
* @return none.
*/
void TIM4_init()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};
TIM_Cmd(TIM4, DISABLE);
TIM_TimeBaseInitStructure.TIM_Period = 0xffff;
TIM_TimeBaseInitStructure.TIM_Prescaler = ((SystemCoreClock) / TIM_PCK) - 1;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);
TIM_ARRPreloadConfig(TIM4, DISABLE);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_Cmd(TIM4, ENABLE);
}
/*********************************************************************
* @fn TIM3_Encoder_Init
*
* @brief Initialize TIM1 for encoder
*
* @return none.
*/
void TIM3_Encoder_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
{
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_Period = 160;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 10;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, ENABLE);
}
/*********************************************************************
* @fn check_direction
*
* @brief Compute the absolute value of a signed 32-bit integer.
*
* @param a Signed 32-bit integer.
*
* @return Absolute value of a
*/
uint32_t static inline iabs(int32_t a)
{
if (a < 0)
{
return -a;
}
return a;
}
/*********************************************************************
* @fn check_direction
*
* @brief Detect the rotation direction of the encoder
*
* @param current_value The current count value
*
* @return direction The direction of the encoder's rotation
*/
int32_t check_direction(int current_value)
{
static int last_value = 0;
if (current_value == last_value)
{
return 0;
}
int diff = current_value - last_value;
if (iabs(diff) < 4)
{
return 0;
}
int32_t direction;
if (iabs(diff) < 80)
{
if (diff > 0)
{
direction = 1;
}
else
{
direction = -1;
}
}
else
{
if (diff > 0)
{
direction = -1;
}
else
{
direction = 1;
}
}
last_value = current_value;
return direction;
}
#define ENC_KEY_PORT GPIOA
#define ENC_KEY_PIN (GPIO_Pin_15)
#define ENC_KEY() ((ENC_KEY_PORT->INDR & ENC_KEY_PIN) == 0)
uint32_t key3_state = 0;
/*********************************************************************
* @fn Key_GPIOInit
*
* @brief Initialization of the key pins of the encoder
*
* @return none.
*/
void Key_GPIOInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
GPIO_InitStructure.GPIO_Pin = ENC_KEY_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(ENC_KEY_PORT, &GPIO_InitStructure);
}
static uint8_t wui_heap_buffer[4 * 1024];
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @return none
*/
int main(void)
{
SystemCoreClockUpdate();
Delay_Init();
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n", SystemCoreClock);
printf(__TIME__ "\n");
SPI_FLASH_Init();
uint32_t id = SPI_Flash_ReadJedecDeviceID();
printf("flash_id %x\n", id);
LCD_Init();
TIM3_Encoder_Init();
TIM4_init();
Key_GPIOInit();
wui_init_data_t data = {
.page_count = WUI_PAGE_COUNT,
.width = LCD_WIDTH,
.height = LCD_HEIGHT,
.heap_addr = wui_heap_buffer,
.heap_size = sizeof(wui_heap_buffer),
};
wui_init(&data);
wui_register_hal(wui_get_hal());
wui_page_switch(WUI_START_PAGE);
while (1)
{
uint16_t now = TIM4->CNT;
if (now > 16)
{
now = now / 8;
TIM4->CNT = 0;
wui_page_ui_tick(now);
}
wui_page_ui_update();
uint32_t tcnt = TIM3->CNT;
int32_t dir = check_direction(tcnt);
if (dir > 0)
{
HW_Key_event('a');
}
else if (dir < 0)
{
HW_Key_event('d');
}
uint32_t k3 = ENC_KEY();
if (k3)
{
if (key3_state)
{
}
else
{
key3_state = 1;
HW_Key_event('b');
}
}
else
{
key3_state = 0;
}
}
while (1)
{
}
}
+305
View File
@@ -0,0 +1,305 @@
/*********************************************************************
* File Name : main.c
* Author : WCH
* Version : V1.0.0
* Date : 2026/08/06
* Description : Main program body.
*********************************************************************************
* Copyright (c) 2023 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/*
*@Note
A demo for wui
pins:
pins:
FLASH_CS -- PA4
FLASH_DO -- PA6
FLASH_DI -- PA7
FLASH_CLK -- PA5
LCD_LED -- PB9
LCD_DC -- PB10
LCD_RST -- PB11
LCD_CS -- PB12
LCD_SCK -- PB13
LCD_MOSI -- PB15
*/
#include <math.h>
#include "debug.h"
#include "string.h"
#include "lcd.h"
#include "flash.h"
#include "config.h"
#include "wui.h"
#include "wui_port.h"
#include "wui_user/wui_page.h"
/* Global define */
/* Global Variable */
#define TIM_PCK (8000ul)
/*********************************************************************
* @fn TIM4_init
*
* @brief Initialize TIM2 for WCHGUI
*
* @return none.
*/
void TIM4_init()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure = {0};
TIM_Cmd(TIM4, DISABLE);
TIM_TimeBaseInitStructure.TIM_Period = 0xffff;
TIM_TimeBaseInitStructure.TIM_Prescaler = ((SystemCoreClock) / TIM_PCK) - 1;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure);
TIM_ARRPreloadConfig(TIM4, DISABLE);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_Cmd(TIM4, ENABLE);
}
/*********************************************************************
* @fn TIM3_Encoder_Init
*
* @brief Initialize TIM1 for encoder
*
* @return none.
*/
void TIM3_Encoder_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
{
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_Period = 160;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 10;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, ENABLE);
}
/*********************************************************************
* @fn check_direction
*
* @brief Compute the absolute value of a signed 32-bit integer.
*
* @param a Signed 32-bit integer.
*
* @return Absolute value of a
*/
uint32_t static inline iabs(int32_t a)
{
if (a < 0)
{
return -a;
}
return a;
}
/*********************************************************************
* @fn check_direction
*
* @brief Detect the rotation direction of the encoder
*
* @param current_value The current count value
*
* @return direction The direction of the encoder's rotation
*/
int32_t check_direction(int current_value)
{
static int last_value = 0;
if (current_value == last_value)
{
return 0;
}
int diff = current_value - last_value;
if (iabs(diff) < 4)
{
return 0;
}
int32_t direction;
if (iabs(diff) < 80)
{
if (diff > 0)
{
direction = 1;
}
else
{
direction = -1;
}
}
else
{
if (diff > 0)
{
direction = -1;
}
else
{
direction = 1;
}
}
last_value = current_value;
return direction;
}
#define ENC_KEY_PORT GPIOA
#define ENC_KEY_PIN (GPIO_Pin_15)
#define ENC_KEY() ((ENC_KEY_PORT->INDR & ENC_KEY_PIN) == 0)
uint32_t key3_state = 0;
/*********************************************************************
* @fn Key_GPIOInit
*
* @brief Initialization of the key pins of the encoder
*
* @return none.
*/
void Key_GPIOInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
GPIO_InitStructure.GPIO_Pin = ENC_KEY_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(ENC_KEY_PORT, &GPIO_InitStructure);
}
static uint8_t wui_heap_buffer[4 * 1024];
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @return none
*/
int main(void)
{
SystemCoreClockUpdate();
Delay_Init();
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n", SystemCoreClock);
printf(__TIME__ "\n");
SPI_FLASH_Init();
uint32_t id = SPI_Flash_ReadJedecDeviceID();
printf("flash_id %x\n", id);
LCD_Init();
TIM3_Encoder_Init();
TIM4_init();
Key_GPIOInit();
wui_init_data_t data = {
.page_count = WUI_PAGE_COUNT,
.width = LCD_WIDTH,
.height = LCD_HEIGHT,
.heap_addr = wui_heap_buffer,
.heap_size = sizeof(wui_heap_buffer),
};
wui_init(&data);
wui_register_hal(wui_get_hal());
wui_page_switch(WUI_START_PAGE);
while (1)
{
uint16_t now = TIM4->CNT;
if (now > 16)
{
now = now / 8;
TIM4->CNT = 0;
wui_page_ui_tick(now);
}
wui_page_ui_update();
uint32_t tcnt = TIM3->CNT;
int32_t dir = check_direction(tcnt);
if (dir > 0)
{
HW_Key_event('a');
}
else if (dir < 0)
{
HW_Key_event('d');
}
uint32_t k3 = ENC_KEY();
if (k3)
{
if (key3_state)
{
}
else
{
key3_state = 1;
HW_Key_event('b');
}
}
else
{
key3_state = 0;
}
}
while (1)
{
}
}
@@ -0,0 +1,987 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : system_ch32v20x.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/06/06
* Description : CH32V20x Device Peripheral Access Layer System Source File.
* For HSE = 32Mhz (CH32V208x/CH32V203RBT)
* For HSE = 8Mhz (other CH32V203x)
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "ch32v20x.h"
/*
* Uncomment the line corresponding to the desired System clock (SYSCLK) frequency (after
* reset the HSI is used as SYSCLK source).
* If none of the define below is enabled, the HSI is used as System clock source.
*/
//#define SYSCLK_FREQ_HSE HSE_VALUE
//#define SYSCLK_FREQ_48MHz_HSE 48000000
//#define SYSCLK_FREQ_56MHz_HSE 56000000
//#define SYSCLK_FREQ_72MHz_HSE 72000000
#define SYSCLK_FREQ_96MHz_HSE 96000000
//#define SYSCLK_FREQ_120MHz_HSE 120000000
//#define SYSCLK_FREQ_144MHz_HSE 144000000
//#define SYSCLK_FREQ_HSI HSI_VALUE
//#define SYSCLK_FREQ_48MHz_HSI 48000000
//#define SYSCLK_FREQ_56MHz_HSI 56000000
//#define SYSCLK_FREQ_72MHz_HSI 72000000
//#define SYSCLK_FREQ_96MHz_HSI 96000000
//#define SYSCLK_FREQ_120MHz_HSI 120000000
//#define SYSCLK_FREQ_144MHz_HSI 144000000
/* Clock Definitions */
#ifdef SYSCLK_FREQ_HSE
uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_48MHz_HSE
uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSE; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_56MHz_HSE
uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz_HSE; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_72MHz_HSE
uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz_HSE; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_96MHz_HSE
uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz_HSE; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_120MHz_HSE
uint32_t SystemCoreClock = SYSCLK_FREQ_120MHz_HSE; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_144MHz_HSE
uint32_t SystemCoreClock = SYSCLK_FREQ_144MHz_HSE; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_48MHz_HSI
uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz_HSI; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_56MHz_HSI
uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz_HSI; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_72MHz_HSI
uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz_HSI; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_96MHz_HSI
uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz_HSI; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_120MHz_HSI
uint32_t SystemCoreClock = SYSCLK_FREQ_120MHz_HSI; /* System Clock Frequency (Core Clock) */
#elif defined SYSCLK_FREQ_144MHz_HSI
uint32_t SystemCoreClock = SYSCLK_FREQ_144MHz_HSI; /* System Clock Frequency (Core Clock) */
#else
uint32_t SystemCoreClock = HSI_VALUE; /* System Clock Frequency (Core Clock) */
#endif
__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
/* system_private_function_proto_types */
static void SetSysClock(void);
#ifdef SYSCLK_FREQ_HSE
static void SetSysClockToHSE( void );
#elif defined SYSCLK_FREQ_48MHz_HSE
static void SetSysClockTo48_HSE( void );
#elif defined SYSCLK_FREQ_56MHz_HSE
static void SetSysClockTo56_HSE( void );
#elif defined SYSCLK_FREQ_72MHz_HSE
static void SetSysClockTo72_HSE( void );
#elif defined SYSCLK_FREQ_96MHz_HSE
static void SetSysClockTo96_HSE( void );
#elif defined SYSCLK_FREQ_120MHz_HSE
static void SetSysClockTo120_HSE( void );
#elif defined SYSCLK_FREQ_144MHz_HSE
static void SetSysClockTo144_HSE( void );
#elif defined SYSCLK_FREQ_48MHz_HSI
static void SetSysClockTo48_HSI( void );
#elif defined SYSCLK_FREQ_56MHz_HSI
static void SetSysClockTo56_HSI( void );
#elif defined SYSCLK_FREQ_72MHz_HSI
static void SetSysClockTo72_HSI( void );
#elif defined SYSCLK_FREQ_96MHz_HSI
static void SetSysClockTo96_HSI( void );
#elif defined SYSCLK_FREQ_120MHz_HSI
static void SetSysClockTo120_HSI( void );
#elif defined SYSCLK_FREQ_144MHz_HSI
static void SetSysClockTo144_HSI( void );
#endif
/*********************************************************************
* @fn SystemInit
*
* @brief Setup the microcontroller system Initialize the Embedded Flash Interface,
* the PLL and update the SystemCoreClock variable.
*
* @return none
*/
void SystemInit (void)
{
RCC->CTLR |= (uint32_t)0x00000001;
RCC->CFGR0 &= (uint32_t)0xF0FF0000;
RCC->CTLR &= (uint32_t)0xFEF6FFFF;
RCC->CTLR &= (uint32_t)0xFFFBFFFF;
RCC->CFGR0 &= (uint32_t)0xFF00FFFF;
RCC->INTR = 0x009F0000;
SetSysClock();
}
/*********************************************************************
* @fn SystemCoreClockUpdate
*
* @brief Update SystemCoreClock variable according to Clock Register Values.
*
* @return none
*/
void SystemCoreClockUpdate (void)
{
uint32_t tmp = 0, pllmull = 0, pllsource = 0, Pll_6_5 = 0;
tmp = RCC->CFGR0 & RCC_SWS;
switch (tmp)
{
case 0x00:
SystemCoreClock = HSI_VALUE;
break;
case 0x04:
SystemCoreClock = HSE_VALUE;
break;
case 0x08:
pllmull = RCC->CFGR0 & RCC_PLLMULL;
pllsource = RCC->CFGR0 & RCC_PLLSRC;
pllmull = ( pllmull >> 18) + 2;
if(pllmull == 17) pllmull = 18;
if (pllsource == 0x00)
{
if(EXTEN->EXTEN_CTR & EXTEN_PLL_HSI_PRE){
SystemCoreClock = HSI_VALUE * pllmull;
}
else{
SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
}
}
else
{
#if defined (CH32V20x_D8W) || defined (CH32V20x_D8)
if(((RCC->CFGR0 & (3<<22)) == (3<<22)) && (RCC_USB5PRE_JUDGE()== SET))
{
SystemCoreClock = ((HSE_VALUE>>1)) * pllmull;
}
else
#endif
if ((RCC->CFGR0 & RCC_PLLXTPRE) != (uint32_t)RESET)
{
#if defined (CH32V20x_D8) || defined (CH32V20x_D8W)
SystemCoreClock = ((HSE_VALUE>>2) >> 1) * pllmull;
#else
SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
#endif
}
else
{
#if defined (CH32V20x_D8) || defined (CH32V20x_D8W)
SystemCoreClock = (HSE_VALUE>>2) * pllmull;
#else
SystemCoreClock = HSE_VALUE * pllmull;
#endif
}
}
if(Pll_6_5 == 1) SystemCoreClock = (SystemCoreClock / 2);
break;
default:
SystemCoreClock = HSI_VALUE;
break;
}
tmp = AHBPrescTable[((RCC->CFGR0 & RCC_HPRE) >> 4)];
SystemCoreClock >>= tmp;
}
/*********************************************************************
* @fn SetSysClock
*
* @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClock(void)
{
//GPIO_IPD_Unused();
#ifdef SYSCLK_FREQ_HSE
SetSysClockToHSE();
#elif defined SYSCLK_FREQ_48MHz_HSE
SetSysClockTo48_HSE();
#elif defined SYSCLK_FREQ_56MHz_HSE
SetSysClockTo56_HSE();
#elif defined SYSCLK_FREQ_72MHz_HSE
SetSysClockTo72_HSE();
#elif defined SYSCLK_FREQ_96MHz_HSE
SetSysClockTo96_HSE();
#elif defined SYSCLK_FREQ_120MHz_HSE
SetSysClockTo120_HSE();
#elif defined SYSCLK_FREQ_144MHz_HSE
SetSysClockTo144_HSE();
#elif defined SYSCLK_FREQ_48MHz_HSI
SetSysClockTo48_HSI();
#elif defined SYSCLK_FREQ_56MHz_HSI
SetSysClockTo56_HSI();
#elif defined SYSCLK_FREQ_72MHz_HSI
SetSysClockTo72_HSI();
#elif defined SYSCLK_FREQ_96MHz_HSI
SetSysClockTo96_HSI();
#elif defined SYSCLK_FREQ_120MHz_HSI
SetSysClockTo120_HSI();
#elif defined SYSCLK_FREQ_144MHz_HSI
SetSysClockTo144_HSI();
#endif
/* If none of the define above is enabled, the HSI is used as System clock
* source (default after reset)
*/
}
#ifdef SYSCLK_FREQ_HSE
/*********************************************************************
* @fn SetSysClockToHSE
*
* @brief Sets HSE as System clock source and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockToHSE(void)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
RCC->CTLR |= ((uint32_t)RCC_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CTLR & RCC_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CTLR & RCC_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV1;
/* Select HSE as system clock source
* CH32V20x_D6 (HSE=8MHZ)
* CH32V20x_D8 (HSE=32MHZ)
* CH32V20x_D8W (HSE=32MHZ)
*/
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
/* Wait till HSE is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
{
}
}
else
{
/* If HSE fails to start-up, the application will have wrong clock
* configuration. User can add here some code to deal with this error
*/
}
}
#elif defined SYSCLK_FREQ_48MHz_HSE
/*********************************************************************
* @fn SetSysClockTo48_HSE
*
* @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo48_HSE(void)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
RCC->CTLR |= ((uint32_t)RCC_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CTLR & RCC_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CTLR & RCC_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 6 = 48 MHz (HSE=8MHZ)
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 6 = 48 MHz (HSE=32MHZ)
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 6 = 48 MHz (HSE=32MHZ)
*/
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL6);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
else
{
/*
* If HSE fails to start-up, the application will have wrong clock
* configuration. User can add here some code to deal with this error
*/
}
}
#elif defined SYSCLK_FREQ_56MHz_HSE
/*********************************************************************
* @fn SetSysClockTo56_HSE
*
* @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo56_HSE(void)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
RCC->CTLR |= ((uint32_t)RCC_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CTLR & RCC_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CTLR & RCC_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 7 = 56 MHz (HSE=8MHZ)
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 7 = 56 MHz (HSE=32MHZ)
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 7 = 56 MHz (HSE=32MHZ)
*/
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL7);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
else
{
/*
* If HSE fails to start-up, the application will have wrong clock
* configuration. User can add here some code to deal with this error
*/
}
}
#elif defined SYSCLK_FREQ_72MHz_HSE
/*********************************************************************
* @fn SetSysClockTo72_HSE
*
* @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo72_HSE(void)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
RCC->CTLR |= ((uint32_t)RCC_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CTLR & RCC_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CTLR & RCC_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 9 = 72 MHz (HSE=8MHZ)
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 9 = 72 MHz (HSE=32MHZ)
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 9 = 72 MHz (HSE=32MHZ)
*/
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL9);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
else
{
/*
* If HSE fails to start-up, the application will have wrong clock
* configuration. User can add here some code to deal with this error
*/
}
}
#elif defined SYSCLK_FREQ_96MHz_HSE
/*********************************************************************
* @fn SetSysClockTo96_HSE
*
* @brief Sets System clock frequency to 96MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo96_HSE(void)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
RCC->CTLR |= ((uint32_t)RCC_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CTLR & RCC_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CTLR & RCC_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 12 = 96 MHz (HSE=8MHZ)
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 12 = 96 MHz (HSE=32MHZ)
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 12 = 96 MHz (HSE=32MHZ)
*/
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL12);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
else
{
/*
* If HSE fails to start-up, the application will have wrong clock
* configuration. User can add here some code to deal with this error
*/
}
}
#elif defined SYSCLK_FREQ_120MHz_HSE
/*********************************************************************
* @fn SetSysClockTo120_HSE
*
* @brief Sets System clock frequency to 120MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo120_HSE(void)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
RCC->CTLR |= ((uint32_t)RCC_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CTLR & RCC_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if((RCC->CTLR & RCC_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if(HSEStatus == (uint32_t)0x01)
{
#if defined (CH32V20x_D8W)
RCC->CFGR0 |= (uint32_t)(3<<22);
/* HCLK = SYSCLK/2 */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV2;
#else
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
#endif
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 15 = 120 MHz (HSE=8MHZ)
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 15 = 120 MHz (HSE=32MHZ)
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/2 * 15 = 240 MHz (HSE=32MHZ)
*/
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_PLLSRC | RCC_PLLXTPRE |
RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL15);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
else
{
/*
* If HSE fails to start-up, the application will have wrong clock
* configuration. User can add here some code to deal with this error
*/
}
}
#elif defined SYSCLK_FREQ_144MHz_HSE
/*********************************************************************
* @fn SetSysClockTo144_HSE
*
* @brief Sets System clock frequency to 144MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo144_HSE(void)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
RCC->CTLR |= ((uint32_t)RCC_HSEON);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CTLR & RCC_HSERDY;
StartUpCounter++;
} while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CTLR & RCC_HSERDY) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* CH32V20x_D6-PLL configuration: PLLCLK = HSE * 18 = 144 MHz (HSE=8MHZ)
* CH32V20x_D8-PLL configuration: PLLCLK = HSE/4 * 18 = 144 MHz (HSE=32MHZ)
* CH32V20x_D8W-PLL configuration: PLLCLK = HSE/4 * 18 = 144 MHz (HSE=32MHZ)
*/
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL18);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
else
{
/*
* If HSE fails to start-up, the application will have wrong clock
* configuration. User can add here some code to deal with this error
*/
}
}
#elif defined SYSCLK_FREQ_48MHz_HSI
/*********************************************************************
* @fn SetSysClockTo48_HSI
*
* @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo48_HSI(void)
{
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* PLL configuration: PLLCLK = HSI * 6 = 48 MHz */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL6);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
#elif defined SYSCLK_FREQ_56MHz_HSI
/*********************************************************************
* @fn SetSysClockTo56_HSI
*
* @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo56_HSI(void)
{
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* PLL configuration: PLLCLK = HSI * 7 = 48 MHz */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL7);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
#elif defined SYSCLK_FREQ_72MHz_HSI
/*********************************************************************
* @fn SetSysClockTo72_HSI
*
* @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo72_HSI(void)
{
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* PLL configuration: PLLCLK = HSI * 9 = 72 MHz */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL9);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
#elif defined SYSCLK_FREQ_96MHz_HSI
/*********************************************************************
* @fn SetSysClockTo96_HSI
*
* @brief Sets System clock frequency to 96MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo96_HSI(void)
{
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* PLL configuration: PLLCLK = HSI * 12 = 96 MHz */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL12);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
#elif defined SYSCLK_FREQ_120MHz_HSI
/*********************************************************************
* @fn SetSysClockTo120_HSI
*
* @brief Sets System clock frequency to 120MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo120_HSI(void)
{
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* PLL configuration: PLLCLK = HSI * 15 = 120 MHz */
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_PLLSRC | RCC_PLLXTPRE |
RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL15);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t) ~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
#elif defined SYSCLK_FREQ_144MHz_HSI
/*********************************************************************
* @fn SetSysClockTo144_HSI
*
* @brief Sets System clock frequency to 144MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
*
* @return none
*/
static void SetSysClockTo144_HSI(void)
{
EXTEN->EXTEN_CTR |= EXTEN_PLL_HSI_PRE;
/* HCLK = SYSCLK */
RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
/* PCLK2 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
/* PCLK1 = HCLK */
RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
/* PLL configuration: PLLCLK = HSI * 18 = 144 MHz */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSI_Div2 | RCC_PLLMULL18);
/* Enable PLL */
RCC->CTLR |= RCC_PLLON;
/* Wait till PLL is ready */
while((RCC->CTLR & RCC_PLLRDY) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
{
}
}
#endif
@@ -0,0 +1,32 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : system_ch32v20x.h
* Author : WCH
* Version : V1.0.0
* Date : 2021/06/06
* Description : CH32V20x Device Peripheral Access Layer System Header File.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#ifndef __SYSTEM_ch32v20x_H
#define __SYSTEM_ch32v20x_H
#ifdef __cplusplus
extern "C" {
#endif
extern uint32_t SystemCoreClock; /* System Clock Frequency (Core Clock) */
/* System_Exported_Functions */
extern void SystemInit(void);
extern void SystemCoreClockUpdate(void);
#ifdef __cplusplus
}
#endif
#endif /*__CH32V20x_SYSTEM_H */
Binary file not shown.
+205
View File
@@ -0,0 +1,205 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : wui.h
* Author : WCH
* Version : V1.2.0
* Date : 2026/07/30
* Description : This file contains core type definitions, HAL interface,
* event system, and main API functions.
*********************************************************************************/
#ifndef WUI_H
#define WUI_H
#include <stdint.h>
#include <stddef.h>
#include "LCD.h"
/*******************************************************************************
* TYPE DEFINITIONS
******************************************************************************/
typedef struct {
uint32_t addr;
uint16_t width;
uint16_t height;
uint32_t data_size;
} image_info_t;
//
#define IMAGE_TYPE_RAW 0x0000
#define IMAGE_TYPE_RLE 0x2000
#define IMAGE_TYPE_CLUT 0x4000
#define IMAGE_TYPE_CLUT4 0x6000
#define IMAGE_TYPE_MASK 0xE000
#define IMAGE_INDEX_MASK 0x1FFF
#define MAKE_IMAGE_ID(type, idx) ((type) | ((idx) & IMAGE_INDEX_MASK))
#define IMAGE_GET_TYPE(id) ((id) & IMAGE_TYPE_MASK)
#define IMAGE_GET_INDEX(id) ((id) & IMAGE_INDEX_MASK)
/*******************************************************************************
* HAL CALLBACK TYPES
******************************************************************************/
typedef void (*wui_touch_read_cb_t)(uint16_t* x, uint16_t* y, uint8_t* pressed);
typedef void (*wui_flash_read_cb_t)(uint8_t* buf, uint32_t addr, uint32_t size);
typedef void (*wui_lcd_set_windows_cb_t)(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2);
typedef void (*wui_lcd_dma_start_cb_t)(void);
typedef void (*wui_lcd_dma_send_cb_t)(const uint8_t* buf, uint32_t len);
typedef void (*wui_lcd_dma_wait_cb_t)(void);
typedef void (*wui_lcd_dma_end_cb_t)(void);
/*******************************************************************************
* HAL INTERFACE
******************************************************************************/
/* Contains all hardware abstraction layer function pointers */
typedef struct
{
wui_touch_read_cb_t touch_read; /* Touch screen read function */
wui_flash_read_cb_t flash_read; /* Flash read function */
wui_lcd_set_windows_cb_t lcd_set_windows; /* LCD window set function */
wui_lcd_dma_start_cb_t lcd_dma_start; /* LCD DMA start function */
wui_lcd_dma_send_cb_t lcd_dma_send; /* LCD DMA send function */
wui_lcd_dma_wait_cb_t lcd_dma_wait; /* LCD DMA wait function */
wui_lcd_dma_end_cb_t lcd_dma_end; /* LCD DMA end function */
} wui_hal_t;
/*******************************************************************************
* PAGE DEFINITIONS
******************************************************************************/
/* Page descriptor structure */
typedef struct
{
uint16_t bg_color; /* Background color */
const char* page_name; /* Page name string */
void (*page_init)(void); /* Page initialization function */
void (*page_loop)(void); /* Page loop function */
void* user_data; /* User data pointer */
} wui_page_desc_t;
/*******************************************************************************
* INITIALIZATION
******************************************************************************/
/* WUI initialization data structure */
typedef struct
{
uint16_t width; /* Display width */
uint16_t height; /* Display height */
uint16_t page_count; /* Number of pages */
uint8_t* heap_addr; /* Heap memory address */
size_t heap_size; /* Heap memory size */
} wui_init_data_t;
/*******************************************************************************
* FONT SYSTEM
******************************************************************************/
/* Font structure */
typedef struct
{
uint16_t height; /* Font height */
uint16_t width; /* Font width */
uint8_t cnt; /* Character count */
uint8_t start_char; /* Start character */
uint16_t first_img_id; /* First image ID */
} wui_font_t;
/* Bitmap font structure */
typedef struct
{
uint8_t width; /* Font width */
uint8_t height; /* Font height */
uint8_t start_char; /* Start character */
uint8_t cnt; /* Character count */
const uint8_t* data; /* Font data pointer */
} wui_bitfont_t;
/*******************************************************************************
* EVENT SYSTEM
******************************************************************************/
/* Event type enumeration */
typedef enum
{
WUI_EVENT_NONE = 0, /* No event */
WUI_EVENT_TOUCH_DOWN = 1, /* Touch down event */
WUI_EVENT_TOUCH_MOVE = 2, /* Touch move event */
WUI_EVENT_TOUCH_LONGPRESS = 3, /* Long press event */
WUI_EVENT_TOUCH_UP = 4, /* Touch up event */
WUI_EVENT_WIDGET_CLICK = 0x10, /* Click event */
WUI_EVENT_WIDGET_LONG_PRESS = 0x11, /* Long press event */
WUI_EVENT_WIDGET_CHANGE = 0x20, /* Value change event */
} _wui_event_t;
typedef uint16_t wui_event_t;
/*******************************************************************************
* TOUCH AREA
******************************************************************************/
typedef struct
{
uint16_t widget_id; /* Widget ID */
uint16_t x, y, w, h; /* Touch area position and size */
} wui_touch_area_t;
/*******************************************************************************
* VERSION API
******************************************************************************/
const char* wui_get_version(void);
/*******************************************************************************
* CORE INITIALIZATION API
******************************************************************************/
void wui_init(wui_init_data_t* data);
void wui_register_hal(const wui_hal_t* hal);
/*******************************************************************************
* PAGE MANAGEMENT API
******************************************************************************/
void wui_page_switch(uint16_t page);
void wui_page_ui_init(void);
void wui_page_ui_loop(void);
const wui_page_desc_t* wui_page_get_desc(uint16_t page);
const char* wui_page_get_name(uint16_t page);
/*******************************************************************************
* UI UPDATE API
******************************************************************************/
uint32_t wui_page_ui_update(void);
void wui_page_ui_tick(uint32_t delta_ms);
/*******************************************************************************
* KEY EVENT API
******************************************************************************/
void wui_register_key_handler(void (*handler)(uint32_t key));
void wui_dispatch_key(void);
void wui_set_key_event(uint32_t key);
/*******************************************************************************
* TOUCH LAYER API
******************************************************************************/
void wui_register_touch_layer(const wui_touch_area_t* areas, uint8_t count, void (*on_event)(uint16_t widget_id, wui_event_t event));
void wui_touch_area_set_enabled(uint16_t widget_id, uint8_t enabled);
#endif /* WUI_H */
@@ -0,0 +1,304 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : wui_widget.h
* Author : WCH
* Version : V1.2.0
* Date : 2026/07/30
* Description : This file contains widget type definitions, properties structures,
* and API functions for widget manipulation.
*********************************************************************************/
#ifndef WUI_WIDGET_H
#define WUI_WIDGET_H
#include <stdint.h>
#include "wui.h"
/*******************************************************************************
* TYPE DEFINITIONS
******************************************************************************/
/* Widget ID types */
typedef uint16_t wui_str_id_t; /* String resource ID */
typedef uint16_t wui_font_id_t; /* Font resource ID */
typedef uint16_t wui_bitmap_font_id_t; /* Bitmap font resource ID */
typedef uint16_t wui_imgtable_id_t; /* Image table resource ID */
typedef uint16_t wui_swgroup_id_t; /* Switch group resource ID */
/*******************************************************************************
* WIDGET TYPES
******************************************************************************/
/**
* @brief Widget type enumeration
*/
typedef enum
{
Wui_w_None, /* Invalid widget type */
Wui_w_Anime, /* Animation widget */
Wui_w_Switch, /* Switch widget */
Wui_w_Res0, /* Reserved 0 */
Wui_w_MultiState, /* Multi-state widget */
Wui_w_StaticImage, /* Static image widget */
Wui_w_QRCode, /* QR code widget */
Wui_w_Rect, /* Rectangle widget */
Wui_w_Res1, /* Reserved 1 */
Wui_w_Res2, /* Reserved 2 */
Wui_w_Res3, /* Reserved 3 */
Wui_w_ProgressBar, /* Progress bar widget */
Wui_w_Button, /* Button widget */
Wui_w_ImgSlider, /* Image slider widget */
Wui_w_SimpleString, /* Simple string widget */
Wui_w_ExString, /* Extended string widget */
Wui_w_COUNT /* Widget type count */
} _wui_widget_type_t;
typedef uint16_t wui_widget_type_t;
/*******************************************************************************
* CONSTANTS & MACROS
******************************************************************************/
/* Progress bar direction */
#define WUI_PB_HORIZONTAL 0 /* Horizontal progress bar */
#define WUI_PB_VERTICAL 1 /* Vertical progress bar */
/* Image slider direction */
#define WUI_IMGSLIDER_HORIZONTAL 0 /* Horizontal image slider */
#define WUI_IMGSLIDER_VERTICAL 1 /* Vertical image slider */
/* Simple string alignment */
#define WUI_SIMPLESTRING_ALIGN_LEFT 0 /* Left alignment */
#define WUI_SIMPLESTRING_ALIGN_RIGHT 1 /* Right alignment */
#define WUI_SIMPLESTRING_ALIGN_CENTER 2 /* Center alignment */
/* Extended string type */
#define WUI_EXSTRING_TYPE_NORMAL 0 /* Normal type */
/* Extended string alignment */
#define WUI_EXSTRING_ALIGN_LEFT 0 /* Left alignment */
#define WUI_EXSTRING_ALIGN_RIGHT 1 /* Right alignment */
#define WUI_EXSTRING_ALIGN_CENTER 2 /* Center alignment */
/*******************************************************************************
* WIDGET PROPERTIES
******************************************************************************/
/* Animation widget properties */
typedef struct
{
uint16_t start_img_id; /* Start image ID */
uint16_t frame_count; /* Total frame count */
uint8_t fps; /* Frames per second */
uint8_t loop; /* Loop mode (0: no loop, 1: loop) */
} wui_widget_anime_props_t;
/* Switch widget properties*/
typedef struct
{
uint16_t on_img_id; /* ON state image ID */
uint16_t off_img_id; /* OFF state image ID */
uint8_t is_on; /* Current state (0: OFF, 1: ON) */
} wui_widget_switch_props_t;
/* Multi-state widget properties */
typedef struct
{
wui_imgtable_id_t imgtable_id; /* Image table ID */
uint8_t state_count; /* Number of states */
uint8_t default_state; /* Default state index */
} wui_widget_multistate_props_t;
/* Static image widget properties */
typedef struct
{
uint16_t img_id; /* Image ID */
} wui_widget_staticimg_props_t;
/* QR code widget properties */
typedef struct
{
uint16_t img_id; /* Image ID */
} wui_widget_qrcode_props_t;
/* Rectangle widget properties */
typedef struct
{
uint16_t color; /* Rectangle color */
} wui_widget_rect_props_t;
/* Button widget properties */
typedef struct
{
uint16_t normal_img_id; /* Normal state image ID */
uint16_t pressed_img_id; /* Pressed state image ID */
uint8_t is_pressed; /* Current state (0: released, 1: pressed) */
} wui_widget_button_props_t;
/* Progress bar widget properties */
typedef struct
{
uint8_t direction; /* Direction (HORIZONTAL/VERTICAL) */
uint8_t min_value; /* Minimum value */
uint8_t max_value; /* Maximum value */
uint8_t current_value; /* Current value */
uint16_t bg_color; /* Background color */
uint16_t fill_color; /* Fill color */
uint16_t thumb_color; /* Thumb color */
uint16_t thumb_w; /* Thumb width */
} wui_widget_pb_props_t;
/* Image slider widget properties */
typedef struct
{
uint16_t full_img_id; /* Full image ID , Must be in raw format */
uint16_t empty_img_id; /* Empty image ID , Must be in raw format*/
uint8_t current_value; /* Initialization percentage (0-100) */
uint8_t direction; /* Direction (HORIZONTAL/VERTICAL) */
} wui_widget_imgslider_props_t;
/* Simple string widget properties */
typedef struct
{
wui_str_id_t str_id; /* String resource ID */
uint16_t fg_color; /* Foreground color */
uint16_t bg_color; /* Background color */
uint8_t align; /* Alignment (LEFT/RIGHT/CENTER) */
wui_bitmap_font_id_t font_id; /* Bitmap font ID */
} wui_widget_simplestring_props_t;
/* Extended string widget properties */
typedef struct
{
uint8_t type; /* String type */
uint8_t align; /* Alignment (LEFT/RIGHT/CENTER) */
union
{
struct
{
uint16_t fg_color; /* Foreground color */
uint16_t bg_color; /* Background color */
};
void* res; /* Reserved pointer */
};
wui_str_id_t str_id; /* String resource ID */
wui_font_id_t font_id; /* Font ID */
} wui_widget_exstring_props_t;
/*******************************************************************************
* WIDGET DESCRIPTOR
******************************************************************************/
/* This is the main structure for defining a widget */
typedef struct
{
uint16_t widget_id; /* Unique widget ID */
uint16_t type; /* Widget type */
uint16_t x, y, w, h; /* Widget position and size */
uint8_t flags; /* Widget flags */
union
{
wui_widget_anime_props_t anime; /* Animation properties */
wui_widget_switch_props_t switch_; /* Switch properties */
// wui_widget_swgroup_props_t swgroup; /* Switch group properties */
wui_widget_multistate_props_t multistate; /* Multi-state properties */
wui_widget_staticimg_props_t staticimg; /* Static image properties */
wui_widget_qrcode_props_t qrcode; /* QR code properties */
wui_widget_rect_props_t rect; /* Rectangle properties */
wui_widget_pb_props_t progress; /* Progress bar properties */
wui_widget_button_props_t button; /* Button properties */
wui_widget_imgslider_props_t imgslider; /* Image slider properties */
wui_widget_simplestring_props_t simplestring; /* Simple string properties */
wui_widget_exstring_props_t exstring; /* Extended string properties */
} props;
} wui_widget_desc_t;
/*******************************************************************************
* API FUNCTIONS
******************************************************************************/
void wui_page_init(const wui_widget_desc_t* widgets, uint8_t widget_count);
/*******************************************************************************
* ANIMATION API
******************************************************************************/
void wui_widget_anime_play(uint16_t widget_id);
void wui_widget_anime_pause(uint16_t widget_id);
void wui_widget_anime_reset(uint16_t widget_id);
uint8_t wui_widget_anime_is_completed(uint16_t widget_id);
/*******************************************************************************
* SWITCH API
******************************************************************************/
void wui_widget_switch_toggle(uint16_t widget_id);
void wui_widget_switch_set_on(uint16_t widget_id, uint8_t on);
uint8_t wui_widget_switch_is_on(uint16_t widget_id);
/*******************************************************************************
* MULTI-STATE API
******************************************************************************/
void wui_widget_multistate_next(uint16_t widget_id);
void wui_widget_multistate_prev(uint16_t widget_id);
void wui_widget_multistate_set_index(uint16_t widget_id, uint8_t index);
uint8_t wui_widget_multistate_get_index(uint16_t widget_id);
/*******************************************************************************
* STATIC IMAGE API
******************************************************************************/
uint16_t wui_widget_staticimg_get_img_id(uint16_t widget_id);
void wui_widget_staticimg_set_img_id(uint16_t widget_id, uint16_t img_id);
/*******************************************************************************
* RECTANGLE API
******************************************************************************/
void wui_widget_rect_set_color(uint16_t widget_id, uint16_t color);
uint16_t wui_widget_rect_get_color(uint16_t widget_id);
/*******************************************************************************
* PROGRESS BAR API
******************************************************************************/
void wui_widget_pb_set_value(uint16_t widget_id, uint8_t value);
uint8_t wui_widget_pb_get_value(uint16_t widget_id);
/*******************************************************************************
* IMAGE SLIDER API
******************************************************************************/
void wui_widget_imgslider_set_value(uint16_t widget_id, uint8_t current_value);
uint8_t wui_widget_imgslider_get_value(uint16_t widget_id);
/*******************************************************************************
* BUTTON API
******************************************************************************/
void wui_widget_button_press(uint16_t widget_id);
void wui_widget_button_release(uint16_t widget_id);
uint8_t wui_widget_button_is_pressed(uint16_t widget_id);
/*******************************************************************************
* SIMPLE STRING API
******************************************************************************/
void wui_widget_simplestring_set_str(uint16_t widget_id, const char* str);
const char* wui_widget_simplestring_get_str(uint16_t widget_id);
void wui_widget_simplestring_set_align(uint16_t widget_id, uint8_t align);
uint8_t wui_widget_simplestring_get_align(uint16_t widget_id);
void wui_widget_simplestring_set_color(uint16_t widget_id, uint16_t fg_color, uint16_t bg_color);
/*******************************************************************************
* EXTENDED STRING API
******************************************************************************/
void wui_widget_exstring_set_str(uint16_t widget_id, const char* str);
const char* wui_widget_exstring_get_str(uint16_t widget_id);
void wui_widget_exstring_set_align(uint16_t widget_id, uint8_t align);
uint8_t wui_widget_exstring_get_align(uint16_t widget_id);
#endif /* WUI_WIDGET_H */
@@ -0,0 +1,150 @@
#include "wui.h"
#include "wui_port.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "flash.h"
#include "images.h"
#include "LCD.h"
#include "spi.h"
void HW_Key_event(uint32_t _key_code)
{
printf("[KEY] press %c\n", _key_code);
wui_set_key_event(_key_code);
}
/*********************************************************************
* @fn touch_read
*
* @brief Read touch screen coordinates and pressed state.
* v203 has no touch screen, so touch is disabled.
*
* @param x - Pointer to store X coordinate.
* @param y - Pointer to store Y coordinate.
* @param pressed - Pointer to store pressed state.
*
* @return none
*/
static void touch_read(uint16_t* x, uint16_t* y, uint8_t* pressed)
{
*x = 0;
*y = 0;
*pressed = 0;
}
/*********************************************************************
* @fn spi_flash_read
*
* @brief Read data from SPI Flash.
*
* @param outBuffer - Pointer to data buffer to store read data.
* @param ReadAddr - Start address to read from.
* @param size - Number of bytes to read.
*
* @return none
*/
static void spi_flash_read(uint8_t* outBuffer, uint32_t ReadAddr, uint32_t size)
{
SPI_Flash_Read_dma_start(ReadAddr);
SPI1_Read_DMA(outBuffer, (uint16_t)size);
SPI1_wait_for_bsy();
SPI_Flash_Read_dma_end();
}
/*********************************************************************
* @fn lcd_set_windows
*
* @brief Set LCD display window.
*
* @param x_start - Start X coordinate.
* @param y_start - Start Y coordinate.
* @param x_end - End X coordinate.
* @param y_end - End Y coordinate.
*
* @return none
*/
static void lcd_set_windows(uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end)
{
LCD_SetWindows(x_start, y_start, x_end, y_end);
}
/*********************************************************************
* @fn lcd_dma_start
*
* @brief Start LCD data transfer.
*
* @return none
*/
static void lcd_dma_start(void)
{
LCD_DC_SET();
LCD_CS_CLR();
}
/*********************************************************************
* @fn lcd_dma_send
*
* @brief Send data via LCD DMA.
*
* @param buf - Pointer to data buffer to send.
* @param len - Length of data in bytes.
*
* @return none
*/
static void lcd_dma_send(const uint8_t* buf, uint32_t len)
{
SPI2_Write_DMA((uint8_t*)buf, (uint16_t)len);
}
/*********************************************************************
* @fn lcd_dma_wait
*
* @brief Wait for LCD DMA transfer to complete.
*
* @return none
*/
static void lcd_dma_wait(void)
{
while (SPI2_DMA_TX_CH->CNTR > 0)
{
}
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET)
;
}
/*********************************************************************
* @fn lcd_dma_end
*
* @brief End LCD DMA transfer.
*
* @return none
*/
static void lcd_dma_end(void)
{
LCD_CS_SET();
}
/*********************************************************************
* @fn wui_get_hal
*
* @brief Get WUI HAL interface structure.
*
* @return Pointer to HAL interface structure.
*/
const wui_hal_t* wui_get_hal(void)
{
static const wui_hal_t hal = {
.touch_read = touch_read,
.flash_read = spi_flash_read,
.lcd_set_windows = lcd_set_windows,
.lcd_dma_start = lcd_dma_start,
.lcd_dma_send = lcd_dma_send,
.lcd_dma_wait = lcd_dma_wait,
.lcd_dma_end = lcd_dma_end,
};
return &hal;
}
@@ -0,0 +1,11 @@
#ifndef __WUI_PORT_H
#define __WUI_PORT_H
#include <stdint.h>
#include "wui.h"
void HW_Key_event(uint32_t _key_code);
const wui_hal_t* wui_get_hal(void);
#endif // __WUI_PORT_H
Binary file not shown.
@@ -0,0 +1,377 @@
/**
* @file images.h
* @brief Image resource table generated by WCHGUIDesigner.
* @note Byte order: Big Endian
*/
#ifndef _IMAGES_H_
#define _IMAGES_H_
#include <stdint.h>
#include <stddef.h>
typedef enum {
IMAGE_NONE = 0xFFFF,
// === Raw images (0b000) ===
IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0030 = MAKE_IMAGE_ID(IMAGE_TYPE_RAW, 0),
IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0031 = MAKE_IMAGE_ID(IMAGE_TYPE_RAW, 1),
IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0032 = MAKE_IMAGE_ID(IMAGE_TYPE_RAW, 2),
IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0033 = MAKE_IMAGE_ID(IMAGE_TYPE_RAW, 3),
IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0034 = MAKE_IMAGE_ID(IMAGE_TYPE_RAW, 4),
IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0035 = MAKE_IMAGE_ID(IMAGE_TYPE_RAW, 5),
IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0036 = MAKE_IMAGE_ID(IMAGE_TYPE_RAW, 6),
IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0037 = MAKE_IMAGE_ID(IMAGE_TYPE_RAW, 7),
IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0038 = MAKE_IMAGE_ID(IMAGE_TYPE_RAW, 8),
IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0039 = MAKE_IMAGE_ID(IMAGE_TYPE_RAW, 9),
IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_003A = MAKE_IMAGE_ID(IMAGE_TYPE_RAW, 10),
// === CLUT images (0b010) ===
IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCFG1 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 11),
IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCFG2 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 12),
IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCOLOR1 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 13),
IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCOLOR2 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 14),
IMAGE_RES_IMG_TPL_MAIN_PAGERELDATA1 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 15),
IMAGE_RES_IMG_TPL_MAIN_PAGERELDATA2 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 16),
IMAGE_RES_IMG_TPL_MAIN_PAGEANIMAL1 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 17),
IMAGE_RES_IMG_TPL_MAIN_PAGEANIMAL2 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 18),
IMAGE_RES_IMG_TPL_MAIN_PAGEMODEL1 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 19),
IMAGE_RES_IMG_TPL_MAIN_PAGEMODEL2 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 20),
IMAGE_RES_IMG_TPL_MAIN_PAGESETTING1 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 21),
IMAGE_RES_IMG_TPL_MAIN_PAGESETTING2 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 22),
IMAGE_RES_IMG_TPL_MAIN_BAT00 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 23),
IMAGE_RES_IMG_TPL_MAIN_BAT01 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 24),
IMAGE_RES_IMG_TPL_MAIN_BAT02 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 25),
IMAGE_RES_IMG_TPL_MAIN_BAT03 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 26),
IMAGE_RES_IMG_TPL_MAIN_BAT04 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 27),
IMAGE_RES_IMG_TPL_MAIN_BAT05 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 28),
IMAGE_RES_IMG_TPL_MAIN_BAT06 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 29),
IMAGE_RES_IMG_TPL_MAIN_BAT07 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 30),
IMAGE_RES_IMG_TPL_MAIN_BAT08 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 31),
IMAGE_RES_IMG_TPL_MAIN_BAT09 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 32),
IMAGE_RES_IMG_TPL_MAIN_BAT10 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 33),
IMAGE_RES_IMG_TPL_MAIN_BAT11 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 34),
IMAGE_RES_IMG_TPL_MAIN_BAT12 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 35),
IMAGE_RES_IMG_TPL_MAIN_BAT13 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 36),
IMAGE_RES_IMG_TPL_MAIN_BAT14 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 37),
IMAGE_RES_IMG_TPL_MAIN_BAT15 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 38),
IMAGE_RES_IMG_TPL_MAIN_BAT16 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 39),
IMAGE_RES_IMG_TPL_MAIN_BAT17 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 40),
IMAGE_RES_IMG_TPL_MAIN_BAT18 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 41),
IMAGE_RES_IMG_TPL_MAIN_BAT19 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 42),
IMAGE_RES_IMG_TPL_MAIN_BAT20 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 43),
IMAGE_RES_IMG_TPL_MAIN_BAT21 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 44),
IMAGE_RES_IMG_TPL_MAIN_CON1 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 45),
IMAGE_RES_IMG_TPL_MAIN_CON2 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 46),
IMAGE_RES_IMG_TPL_MAIN_CON3 = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 47),
IMAGE_RES_IMG_TPL_MAIN_BG = MAKE_IMAGE_ID(IMAGE_TYPE_CLUT, 48),
} image_index_t;
static const image_info_t image_table[] = {
{ // IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0030
.addr = 0x0001DED4,
.width = 10,
.height = 26,
.data_size = 520,
},
{ // IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0031
.addr = 0x0001E0DC,
.width = 10,
.height = 26,
.data_size = 520,
},
{ // IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0032
.addr = 0x0001E2E4,
.width = 10,
.height = 26,
.data_size = 520,
},
{ // IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0033
.addr = 0x0001E4EC,
.width = 10,
.height = 26,
.data_size = 520,
},
{ // IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0034
.addr = 0x0001E6F4,
.width = 10,
.height = 26,
.data_size = 520,
},
{ // IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0035
.addr = 0x0001E8FC,
.width = 10,
.height = 26,
.data_size = 520,
},
{ // IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0036
.addr = 0x0001EB04,
.width = 10,
.height = 26,
.data_size = 520,
},
{ // IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0037
.addr = 0x0001ED0C,
.width = 10,
.height = 26,
.data_size = 520,
},
{ // IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0038
.addr = 0x0001EF14,
.width = 10,
.height = 26,
.data_size = 520,
},
{ // IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0039
.addr = 0x0001F11C,
.width = 10,
.height = 26,
.data_size = 520,
},
{ // IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_003A
.addr = 0x0001F324,
.width = 10,
.height = 26,
.data_size = 520,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCFG1
.addr = 0x00000000,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCFG2
.addr = 0x00001E3C,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCOLOR1
.addr = 0x00003C78,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCOLOR2
.addr = 0x00005AB4,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGERELDATA1
.addr = 0x000078F0,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGERELDATA2
.addr = 0x0000972C,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGEANIMAL1
.addr = 0x0000B568,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGEANIMAL2
.addr = 0x0000D3A4,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGEMODEL1
.addr = 0x0000F1E0,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGEMODEL2
.addr = 0x0001101C,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGESETTING1
.addr = 0x00012E58,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_PAGESETTING2
.addr = 0x00014C94,
.width = 73,
.height = 99,
.data_size = 7739,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT00
.addr = 0x00016AD0,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT01
.addr = 0x00016F74,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT02
.addr = 0x00017418,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT03
.addr = 0x000178BC,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT04
.addr = 0x00017D60,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT05
.addr = 0x00018204,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT06
.addr = 0x000186A8,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT07
.addr = 0x00018B4C,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT08
.addr = 0x00018FF0,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT09
.addr = 0x00019494,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT10
.addr = 0x00019938,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT11
.addr = 0x00019DDC,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT12
.addr = 0x0001A280,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT13
.addr = 0x0001A724,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT14
.addr = 0x0001ABC8,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT15
.addr = 0x0001B06C,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT16
.addr = 0x0001B510,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT17
.addr = 0x0001B9B4,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT18
.addr = 0x0001BE58,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT19
.addr = 0x0001C2FC,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT20
.addr = 0x0001C7A0,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BAT21
.addr = 0x0001CC44,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_CON1
.addr = 0x0001D0E8,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_CON2
.addr = 0x0001D58C,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_CON3
.addr = 0x0001DA30,
.width = 26,
.height = 26,
.data_size = 1188,
},
{ // IMAGE_RES_IMG_TPL_MAIN_BG
.addr = 0x0001F52C,
.width = 320,
.height = 240,
.data_size = 77312,
},
};
#define RAW_IMAGE_COUNT 11
#define RLE_IMAGE_COUNT 0
#define CLUT_IMAGE_COUNT 38
#define CLUT4_IMAGE_COUNT 0
#define IMAGE_COUNT 49
static inline const image_info_t* get_image(image_index_t index) {
uint16_t idx = IMAGE_GET_INDEX(index);
if (idx >= IMAGE_COUNT) return NULL;
return &image_table[idx];
}
#endif /* _IMAGES_H_ */
@@ -0,0 +1,170 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : wui_asset.c
* Author : WCH
* Version : V1.2.0
* Date : 2026/07/30
* Description : This file is auto-generated by WUI Designer Tool.
* : Contains WUI asset resources (strings, images, fonts).
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "wui_asset.h"
#include <stddef.h>
#include "LCD_Font.h"
const wui_bitfont_t wui_font_6x12 = { 6, 12, 0x20, 95, (const uint8_t*)asc2_1206 };
const wui_bitfont_t wui_font_8x16 = { 8, 16, 0x20, 95, (const uint8_t*)asc2_1608 };
/*******************************************************************************
* STRING RESOURCES
******************************************************************************/
/* Maps string IDs to string pointers */
static const char* wui_string_table[WUI_STR_COUNT] = {
[WUI_STR_NONE] = NULL,
[WUI_STR_MAIN_MAIN_TIME] = "12:34",
};
/**
* @brief Get string by ID
* @param id - String ID
* @return String pointer, or NULL if not found
*/
const char* wui_get_string(wui_str_id_t id)
{
if (id > 0 && id < WUI_STR_COUNT)
{
return wui_string_table[id];
}
return NULL;
}
/*******************************************************************************
* IMAGE TABLE RESOURCES
******************************************************************************/
static const uint16_t wui_imgtable_main_main_battery[] = {
IMAGE_RES_IMG_TPL_MAIN_BAT00,
IMAGE_RES_IMG_TPL_MAIN_BAT01,
IMAGE_RES_IMG_TPL_MAIN_BAT02,
IMAGE_RES_IMG_TPL_MAIN_BAT03,
IMAGE_RES_IMG_TPL_MAIN_BAT04,
IMAGE_RES_IMG_TPL_MAIN_BAT05,
IMAGE_RES_IMG_TPL_MAIN_BAT06,
IMAGE_RES_IMG_TPL_MAIN_BAT07,
IMAGE_RES_IMG_TPL_MAIN_BAT08,
IMAGE_RES_IMG_TPL_MAIN_BAT09,
IMAGE_RES_IMG_TPL_MAIN_BAT10,
IMAGE_RES_IMG_TPL_MAIN_BAT11,
IMAGE_RES_IMG_TPL_MAIN_BAT12,
IMAGE_RES_IMG_TPL_MAIN_BAT13,
IMAGE_RES_IMG_TPL_MAIN_BAT14,
IMAGE_RES_IMG_TPL_MAIN_BAT15,
IMAGE_RES_IMG_TPL_MAIN_BAT16,
IMAGE_RES_IMG_TPL_MAIN_BAT17,
IMAGE_RES_IMG_TPL_MAIN_BAT18,
IMAGE_RES_IMG_TPL_MAIN_BAT19,
IMAGE_RES_IMG_TPL_MAIN_BAT20,
IMAGE_RES_IMG_TPL_MAIN_BAT21,
};
static const uint16_t wui_imgtable_main_main_connection[] = {
IMAGE_RES_IMG_TPL_MAIN_CON1,
IMAGE_RES_IMG_TPL_MAIN_CON2,
IMAGE_RES_IMG_TPL_MAIN_CON3,
};
/* Maps image table IDs to image ID arrays */
static const uint16_t* wui_imgtable_list[WUI_IMGTABLE_COUNT] = {
[WUI_IMGTABLE_NONE] = NULL,
[WUI_IMGTABLE_MAIN_MAIN_BATTERY] = wui_imgtable_main_main_battery,
[WUI_IMGTABLE_MAIN_MAIN_CONNECTION] = wui_imgtable_main_main_connection,
};
/**
* @brief Get image table by ID
* @param id - Image table ID
* @return Image table pointer, or NULL if not found
*/
const uint16_t* wui_get_imgtable(wui_imgtable_id_t id)
{
if (id > 0 && id < WUI_IMGTABLE_COUNT)
{
return wui_imgtable_list[id];
}
return NULL;
}
/*******************************************************************************
* FONT RESOURCES
******************************************************************************/
/**
* @brief Font table for WUI
* @details Maps font IDs to font structures
*/
static const wui_font_t wui_font_table[WUI_FONT_COUNT] = {
[WUI_FONT_NONE] = {0},
[WUI_FONT_WUI_PAGE_MAIN_MAIN_TIME] = {
.cnt = 10,
.start_char = '0',
.width = 10,
.height = 26,
.first_img_id = IMAGE_ART_WUI_PAGE_MAIN_MAIN_TIME_0030,
},
};
const wui_font_t* wui_get_font(wui_font_id_t id)
{
if (id > 0 && id < WUI_FONT_COUNT)
{
return &wui_font_table[id];
}
return NULL;
}
/*******************************************************************************
* BITMAP FONT RESOURCES
******************************************************************************/
/* Maps bitmap font IDs to font structures */
static const wui_bitfont_t* wui_bitmap_font_table[WUI_BITMAP_FONT_COUNT] = {
[WUI_BITMAP_FONT_DEFAULT] = &wui_font_8x16,
[WUI_BITMAP_FONT_6X12] = &wui_font_6x12,
[WUI_BITMAP_FONT_8X16] = &wui_font_8x16,
};
/**
* @brief Get bitmap font by ID
* @param id - Bitmap font ID
* @return Bitmap font structure pointer, or default font if not found
*/
const wui_bitfont_t* wui_get_bitmap_font(wui_bitmap_font_id_t id)
{
if (id > 0 && id < WUI_BITMAP_FONT_COUNT) {
return wui_bitmap_font_table[id];
}
return wui_bitmap_font_table[WUI_BITMAP_FONT_DEFAULT];
}
/*********************************************************************
* @fn wui_get_image
*
* @brief Get image information by index.
*
* @param index - Image index.
*
* @return Pointer to image information structure.
*/
const image_info_t* wui_get_image(uint32_t index)
{
// return get_image(index);
uint16_t idx = IMAGE_GET_INDEX(index);
if (idx >= IMAGE_COUNT) return NULL;
return &image_table[idx];
}
@@ -0,0 +1,58 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : wui_asset.h
* Author : WCH
* Version : V1.2.0
* Date : 2026/07/30
* Description : Asset management functions
*********************************************************************************/
#ifndef WUI_ASSET_H
#define WUI_ASSET_H
#include <stdint.h>
#include "wui.h"
#include "images.h"
#include "wui_widget.h"
enum
{
WUI_STR_NONE = 0,
WUI_STR_MAIN_MAIN_TIME,
WUI_STR_COUNT
};
const char* wui_get_string(wui_str_id_t id);
enum
{
WUI_FONT_NONE = 0,
WUI_FONT_WUI_PAGE_MAIN_MAIN_TIME,
WUI_FONT_COUNT
};
const wui_font_t* wui_get_font(wui_font_id_t id);
enum
{
WUI_IMGTABLE_NONE = 0,
WUI_IMGTABLE_MAIN_MAIN_BATTERY,
WUI_IMGTABLE_MAIN_MAIN_CONNECTION,
WUI_IMGTABLE_COUNT
};
const uint16_t* wui_get_imgtable(wui_imgtable_id_t id);
enum {
WUI_BITMAP_FONT_DEFAULT = 0,
WUI_BITMAP_FONT_6X12,
WUI_BITMAP_FONT_8X16,
WUI_BITMAP_FONT_COUNT
};
const wui_bitfont_t* wui_get_bitmap_font(wui_bitmap_font_id_t id);
const image_info_t* wui_get_image(uint32_t index);
#endif
@@ -0,0 +1,22 @@
#include "wui_page.h"
#include "wui.h"
#include "images.h"
#include <stdio.h>
const wui_page_desc_t wui_pages[WUI_PAGE_COUNT] = {
[WUI_PAGE_NONE] = {
.bg_color = RGB888_TO_RGB565(0, 0, 0),
.page_name = "None",
.page_init = wui_page_none_init,
.page_loop = wui_page_none_loop,
.user_data = NULL,
},
[WUI_PAGE_MAIN] = {
.bg_color = RGB888_TO_RGB565(0, 0, 0),
.page_name = "Menu Page",
.page_init = wui_page_main_init,
.page_loop = wui_page_main_loop,
.user_data = NULL,
},
};
@@ -0,0 +1,25 @@
#ifndef WUI_PAGE_H
#define WUI_PAGE_H
#include <stdint.h>
#include "stddef.h"
#include "wui.h"
typedef enum
{
WUI_PAGE_NONE = 0,
WUI_PAGE_MAIN,
WUI_PAGE_COUNT,
} wui_page_t;
#define WUI_START_PAGE WUI_PAGE_MAIN
void wui_page_none_init(void);
void wui_page_none_loop(void);
void wui_page_main_init(void);
void wui_page_main_loop(void);
extern const wui_page_desc_t wui_pages[WUI_PAGE_COUNT];
#endif
@@ -0,0 +1,80 @@
#include "wui_user/wui_page_main.h"
// ================= Template Page =================
// Template: main menu
// Template interaction logic is generated from known template IDs.
// ================= Template Logic Hooks =================
#define MENU_COUNT 6
static const uint8_t menu_widget_ids[MENU_COUNT] = {
MAIN_MAIN_SETTING,
MAIN_MAIN_MODE,
MAIN_MAIN_ANIM,
MAIN_MAIN_REAL_DATA,
MAIN_MAIN_LIGHT_COLOR,
MAIN_MAIN_COLOR_CFG,
};
static const wui_page_t menu_pages[MENU_COUNT] = {
WUI_PAGE_MAIN,
WUI_PAGE_MAIN,
WUI_PAGE_MAIN,
WUI_PAGE_MAIN,
WUI_PAGE_MAIN,
WUI_PAGE_MAIN,
};
static uint8_t main_saved_selection = 0;
static uint8_t current_menu = 0;
static void menu_set(uint8_t index, uint8_t on)
{
if (index < MENU_COUNT)
{
if (on)
{
wui_widget_button_press(menu_widget_ids[index]);
}
else
{
wui_widget_button_release(menu_widget_ids[index]);
}
}
}
static void main_on_key(uint32_t key)
{
if (key == 'a')
{
menu_set(current_menu, 0);
current_menu = (current_menu == 0) ? MENU_COUNT - 1 : current_menu - 1;
menu_set(current_menu, 1);
}
else if (key == 'd')
{
menu_set(current_menu, 0);
current_menu = (current_menu + 1 >= MENU_COUNT) ? 0 : current_menu + 1;
menu_set(current_menu, 1);
}
else if (key == 'b')
{
main_saved_selection = current_menu;
wui_page_switch(menu_pages[current_menu]);
}
}
// ================= Page Functions =================
void wui_page_main_init(void)
{
wui_page_init(widgets_main, sizeof(widgets_main) / sizeof(widgets_main[0]));
current_menu = main_saved_selection;
menu_set(current_menu, 1);
wui_register_key_handler(main_on_key);
}
void wui_page_main_loop(void)
{
}
@@ -0,0 +1,183 @@
#ifndef WUI_PAGE_MAIN_H
#define WUI_PAGE_MAIN_H
#include "wui.h"
#include "wui_user/wui_page.h"
#include "wui_widget.h"
#include "images.h"
#include "wui_asset.h"
#include <stdio.h>
// ================= Widget ID Enum =================
typedef enum
{
MAIN_MAIN_BG_BLACK,
MAIN_MAIN_BG,
MAIN_MAIN_TIME,
MAIN_MAIN_CONNECTION,
MAIN_MAIN_BATTERY,
MAIN_MAIN_SETTING,
MAIN_MAIN_MODE,
MAIN_MAIN_ANIM,
MAIN_MAIN_REAL_DATA,
MAIN_MAIN_LIGHT_COLOR,
MAIN_MAIN_COLOR_CFG,
} main_widget_id_t;
// ================= Declarative Widgets =================
static const wui_widget_desc_t widgets_main[] = {
[MAIN_MAIN_BG_BLACK] = {
.widget_id = MAIN_MAIN_BG_BLACK,
.type = Wui_w_Rect,
.x = 0,
.y = 0,
.w = 320,
.h = 240,
.props.rect = {
.color = RGB888_TO_RGB565(0, 0, 0),
},
},
[MAIN_MAIN_BG] = {
.widget_id = MAIN_MAIN_BG,
.type = Wui_w_StaticImage,
.x = 0,
.y = 0,
.w = 320,
.h = 240,
.props.staticimg = {
.img_id = IMAGE_RES_IMG_TPL_MAIN_BG,
},
},
[MAIN_MAIN_TIME] = {
.widget_id = MAIN_MAIN_TIME,
.type = Wui_w_ExString,
.x = 12,
.y = 4,
.w = 50,
.h = 26,
.props.exstring = {
.str_id = WUI_STR_MAIN_MAIN_TIME,
.font_id = WUI_FONT_WUI_PAGE_MAIN_MAIN_TIME,
.bg_color = RGB888_TO_RGB565(0, 0, 0),
.align = WUI_EXSTRING_ALIGN_LEFT,
},
},
[MAIN_MAIN_CONNECTION] = {
.widget_id = MAIN_MAIN_CONNECTION,
.type = Wui_w_MultiState,
.x = 119,
.y = 4,
.w = 26,
.h = 26,
.props.multistate = {
.imgtable_id = WUI_IMGTABLE_MAIN_MAIN_CONNECTION,
.state_count = 3,
.default_state = 2,
},
},
[MAIN_MAIN_BATTERY] = {
.widget_id = MAIN_MAIN_BATTERY,
.type = Wui_w_MultiState,
.x = 280,
.y = 4,
.w = 26,
.h = 26,
.props.multistate = {
.imgtable_id = WUI_IMGTABLE_MAIN_MAIN_BATTERY,
.state_count = 22,
.default_state = 0,
},
},
[MAIN_MAIN_SETTING] = {
.widget_id = MAIN_MAIN_SETTING,
.type = Wui_w_Button,
.x = 24,
.y = 42,
.w = 73,
.h = 99,
.props.button = {
.pressed_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGESETTING2,
.normal_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGESETTING1,
.is_pressed = 0,
},
},
[MAIN_MAIN_MODE] = {
.widget_id = MAIN_MAIN_MODE,
.type = Wui_w_Button,
.x = 127,
.y = 42,
.w = 73,
.h = 99,
.props.button = {
.pressed_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGEMODEL2,
.normal_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGEMODEL1,
.is_pressed = 0,
},
},
[MAIN_MAIN_ANIM] = {
.widget_id = MAIN_MAIN_ANIM,
.type = Wui_w_Button,
.x = 226,
.y = 42,
.w = 73,
.h = 99,
.props.button = {
.pressed_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGEANIMAL2,
.normal_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGEANIMAL1,
.is_pressed = 0,
},
},
[MAIN_MAIN_REAL_DATA] = {
.widget_id = MAIN_MAIN_REAL_DATA,
.type = Wui_w_Button,
.x = 24,
.y = 141,
.w = 73,
.h = 99,
.props.button = {
.pressed_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGERELDATA2,
.normal_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGERELDATA1,
.is_pressed = 0,
},
},
[MAIN_MAIN_LIGHT_COLOR] = {
.widget_id = MAIN_MAIN_LIGHT_COLOR,
.type = Wui_w_Button,
.x = 127,
.y = 141,
.w = 73,
.h = 99,
.props.button = {
.pressed_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCOLOR2,
.normal_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCOLOR1,
.is_pressed = 0,
},
},
[MAIN_MAIN_COLOR_CFG] = {
.widget_id = MAIN_MAIN_COLOR_CFG,
.type = Wui_w_Button,
.x = 226,
.y = 141,
.w = 73,
.h = 99,
.props.button = {
.pressed_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCFG2,
.normal_img_id = IMAGE_RES_IMG_TPL_MAIN_PAGELIGHTCFG1,
.is_pressed = 0,
},
},
};
#endif // WUI_PAGE_MAIN_H
@@ -0,0 +1,16 @@
#include "wui_user/wui_page_none.h"
static void none_on_key(uint32_t key)
{
wui_page_switch(WUI_START_PAGE);
}
void wui_page_none_init()
{
wui_page_init(widgets_none, sizeof(widgets_none) / sizeof(widgets_none[0]));
wui_register_key_handler(none_on_key);
}
void wui_page_none_loop()
{
}
@@ -0,0 +1,27 @@
#ifndef WUI_PAGE_NONE_H
#define WUI_PAGE_NONE_H
#include "wui.h"
#include "wui_user/wui_page.h"
#include "wui_widget.h"
typedef enum
{
WIDGET_NONE_BG = 0,
} none_widget_id_t;
static const wui_widget_desc_t widgets_none[] = {
[WIDGET_NONE_BG] = {
.widget_id = WIDGET_NONE_BG,
.type = Wui_w_Rect,
.x = 0,
.y = 0,
.w = LCD_WIDTH,
.h = LCD_HEIGHT,
.props.rect = {
.color = LCD_black,
},
},
};
#endif // WUI_PAGE_NONE_H