参考:https://blog.csdn.net/Brendon_Tan/article/details/107688264
开发板使用正点原子STM32F429阿波罗,屏幕也是正点原子800*480
一、TouchGFXDesigner 下载及安装教程
软件下载
官网下载
下载地址: https://www.st.com/content/st_com/zh/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-configurators-and-code-generators/touchgfxdesigner.html
下载方法:
软件安装
安装步骤:
打开 TouchGFX-4.13.0.msi 文件;(文件目录:\Utilities\PC_Software\TouchGFXDesigner)
然后都是下一步下一步,这里不一步步截图了
二、STM32CubeMX配置
配置FMC(SDRAM)
配置DMA2D
DMA2D打开中断
配置LTDC
LTDC打开中断
使能CRC校验
ST使用TouchGFX必须使能CRC
配置touchgfx
安装touchgfx软件包
打开软件包
设置touchgfx参数
RAM计算方法:80048016/8=0XBB80,所以用0X10000
时钟配置
配置SDIO
后期touchgfx的图片从SD卡读取,所以这里先配置好SD卡的管脚
SDIO开中断
SDIO开DMA
生成工程
不需要勾选
点GENERATECODE,提示报错信息,忽略就可以了
三、rtthread移植TouchGFX
1.env配置
添加屏幕配置选项
修改/board/Kconfig文件,添加LTDC和LCD选项
config BSP_USING_LTDC
bool "Enable LTDC"
default n
config BSP_USING_LCD
bool "Enable LCD"
default n
照着I2C1添加I2C2选项,I2C2用于触摸屏通讯
menuconfig BSP_USING_I2C2
bool "Enable I2C2 BUS (software simulation)"
default n
select RT_USING_I2C
select RT_USING_I2C_BITOPS
select RT_USING_PIN
if BSP_USING_I2C2
comment "Notice: "
config BSP_I2C2_SCL_PIN
int "I2C2 scl pin number"
range 1 176
default 118
config BSP_I2C2_SDA_PIN
int "I2C2 sda pin number"
range 1 176
default 131
endif
打开env->menuconfig
硬件配置打开SDRAM,LTDC,LCD和I2C2
开启屏幕触摸功能
打开touchgfx软件包
开启C++支持
下载软件包pkgs --update
生成keil工程scons --target=mdk5
2.代码修改
修改LTDC时钟
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_LTDC;
PeriphClkInitStruct.PLLSAI.PLLSAIN = 60;
PeriphClkInitStruct.PLLSAI.PLLSAIR = 2;
PeriphClkInitStruct.PLLSAIDivR = RCC_PLLSAIDIVR_2;
开启CRC外设
默认已经开启
drv_lcd.c
屏蔽结构体,和lcd_port.h重复,lcd_port.h在packages\touchgfx2rtt-latest\port文件中,需要添加头文件
屏蔽回调函数
example_ft5426.c
屏蔽开启线程的代码
3.设计UI
文件目录\packages\touchgfx2rtt-latest\TouchGFX
先修改屏幕配置文件(默认不需要修改),再打开工程
添加GUI,选一个适合直接屏幕大小的例程,然后点击生成代码按钮
env重新, scons --target=mdk5
相关文件就加入到工程中
Color.hpp
static colortype getColorFromRGB(uint8_t red, uint8_t green, uint8_t blue)
{
return ((red << 8) & 0xF800) | ((green << 3) & 0x07E0) | ((blue >> 3) & 0x001F);
}
STM32TouchController.cpp
添加FT5426的相关代码
/**
******************************************************************************
* File Name : STM32TouchController.cpp
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* USER CODE BEGIN STM32TouchController */
#include <STM32TouchController.hpp>
#include <rtthread.h>
#ifdef PKG_USING_GT9147
#include "gt9147.h"
#define GT9147_RST_PIN 51
#define GT9147_IRQ_PIN 108
static rt_device_t dev = RT_NULL;
static struct rt_touch_data *read_data = NULL;
static struct rt_touch_info info;
int rt_hw_gt9147_port(void)
{
struct rt_touch_config config;
rt_uint8_t rst;
rst = GT9147_RST_PIN;
config.dev_name = "i2c1";
config.irq_pin.pin = GT9147_IRQ_PIN;
config.irq_pin.mode = PIN_MODE_INPUT_PULLDOWN;
config.user_data = &rst;
rt_hw_gt9147_init("gt", &config);
return 0;
}
int gt9147_init(void)
{
void *id;
rt_hw_gt9147_port();
dev = rt_device_find("gt");
if (dev == RT_NULL)
{
rt_kprintf("can't find device gt\n");
return -1;
}
if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK)
{
rt_kprintf("open device failed!");
return -1;
}
read_data = (struct rt_touch_data *)rt_malloc(sizeof(struct rt_touch_data) * info.point_num);
id = rt_malloc(sizeof(rt_uint8_t) * 8);
rt_device_control(dev, RT_TOUCH_CTRL_GET_ID, id);
rt_uint8_t * read_id = (rt_uint8_t *)id;
rt_kprintf("id = %c %c %c %c \n", read_id[0], read_id[1], read_id[2], read_id[3]);
rt_device_control(dev, RT_TOUCH_CTRL_GET_INFO, &info);
rt_kprintf("range_x = %d \n", info.range_x);
rt_kprintf("range_y = %d \n", info.range_y);
rt_kprintf("point_num = %d \n", info.point_num);
rt_free(id);
return 0;
}
#endif
#ifdef PKG_USING_FT5426
#include "ft5426.h"
#include "touch.h"
rt_device_t dev = RT_NULL;
struct rt_touch_data *read_data;
int ft5426_init(void)
{
dev = rt_device_find("touch");
rt_device_open(dev, RT_DEVICE_FLAG_RDONLY);
struct rt_touch_info info;
rt_device_control(dev, RT_TOUCH_CTRL_GET_INFO, &info);
rt_kprintf("type :%d\r\n", info.type);
rt_kprintf("vendor :%d\r\n", info.vendor);
rt_kprintf("point_num :%d\r\n", info.point_num);
rt_kprintf("range_x :%d\r\n", info.range_x);
rt_kprintf("range_y :%d\r\n", info.range_y);
read_data = (struct rt_touch_data *)rt_calloc(1, sizeof(struct rt_touch_data));
return 0;
}
#endif
void STM32TouchController::init()
{
/**
* Initialize touch controller and driver
*
*/
#ifdef PKG_USING_GT9147
gt9147_init();
#endif
#ifdef PKG_USING_FT5426
ft5426_init();
#endif
}
bool STM32TouchController::sampleTouch(int32_t& x, int32_t& y)
{
/**
* By default sampleTouch returns false,
* return true if a touch has been detected, otherwise false.
*
* Coordinates are passed to the caller by reference by x and y.
*
* This function is called by the TouchGFX framework.
* By default sampleTouch is called every tick, this can be adjusted by HAL::setTouchSampleRate(int8_t);
*
*/
#ifdef PKG_USING_GT9147
rt_device_read(dev, 0, read_data, info.point_num);
if (read_data[0].event == RT_TOUCH_EVENT_DOWN || read_data[0].event == RT_TOUCH_EVENT_MOVE)
{
x = 800 - read_data[0].x_coordinate;
y = 480 - read_data[0].y_coordinate;
return true;
}
else
#endif
#ifdef PKG_USING_FT5426
rt_device_read(dev, 0, read_data, 1);
if (read_data->event == RT_TOUCH_EVENT_DOWN || read_data->event == RT_TOUCH_EVENT_MOVE)
{
y = read_data->x_coordinate;
x = read_data->y_coordinate;
rt_kprintf("down x: %03d y: %03d", read_data->x_coordinate, read_data->y_coordinate);
return true;
}
#endif
return false;
}
/* USER CODE END STM32TouchController */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
如果使用mdk开发,Micro LIB库不要打勾,Micro LIB库不支持C++
编译,烧写到开发板