龙空技术网

【DIY数字仪表】STM32F429移植TouchGFX到RT-Thread系统(1)

嵌入式小美老师 326

前言:

目前你们对“stm32移植rtthreadlvgl”大约比较关怀,看官们都想要剖析一些“stm32移植rtthreadlvgl”的相关文章。那么小编也在网摘上搜集了一些有关“stm32移植rtthreadlvgl””的相关资讯,希望朋友们能喜欢,看官们一起来了解一下吧!

实验平台:

硬件: 野火挑战者STM32F429 V1开发版,5寸屏

软件:最新版本的STM32CubeF4固件库,TouchGFXDesigner v4.13和 STM32CubeMX v5.6.1,开发环境MDK v5.29

实验前准备工作:

1.准备一套STM32F429开发版,和下载工具

2.下载 TouchGFXDesigner v4.13

压缩包下载完后,解压如下:

Projects目录下有STM32H7B3I-DK的工程,可以用来参考。touchGFX Designer的PC端安装包在Utilities目录下,找到后并安装。

3.下载 STM32CubeMX v5.6.1

安装完STM32CubeMX v5.6.1版本后,还需要安装X_CUBE_TOUCHGFX软件包,安装路径如下:

4.下载 MDK v5.27以上版本

效果演示:

代码下载:

代码持续更新中:github代码下载地址

觉得有用的话,欢迎给个小星星

移植RT-Thread的BSP模板:1.学习RT-Thread系统(学过的可以跳过此步骤)通读RT-Thread系统官方学习文档,快速了解RT-Thread系统的内核,设备、组件和软件包的使用方法,下载并安装Env开发工工具2.移植BSP参考官方使用 Env 创建 RT-Thread 项目工程的教程选择 BSP

获取 RT-Thread 源代码后需要根据自己手上的开发板型号找到对应的 BSP,我实验所使用的是野火的STM32F29 V1开发板,所以可以选择stm32f29-fire-challenger的BSP,在此BSP的基础上移植touchgfx,首先找到如下目录:…\rt-thread\bsp\stm32\stm32f429-fire-challenger。搭建项目框架

打开 Env 工具进入 stm32f429-fire-challenger 目录,运行scons --dist 命令。使用此命令会在 stm32f429-fire-challenger目录下生成 dist 目录,这便是开发项目的目录结构,RT-Thread 源码位于项目文件夹内,仅包含stm32f429-fire-challenger 的 BSP,可以随意拷贝此 BSP 到任何目录下使用。

进入dist目录下,把工程里面的 stm32f429-fire-challenger压缩包拷贝到你的项目目录下待使用。制作BSP

也可以自己重新制作对应自己板子的BSP,可以参考官方教程3.配置CubeMX_Config

打开…\board\CubeMX_Config\CubeMX_Config.ioc,由于bsp已经配置了很多外设,所以只需要修改部分配置就可以了

打开CRC重新配置LTCD

打开freertos

打开Touchgfx

配置Touchgfx

配置SPI_FLASH管脚

生成工程通过TouchGFX 4.13.0 Designer打开ApplicationTemplate.touchgfx.part

配置TouchGFX UI,创建三个页面

4.更改操作系统的接口文件

拷贝OSWrappers.cpp,重命名为OSWrappers_RTT.cpp文件

更改代码

  1/**  2  ******************************************************************************  3  * File Name          : OSWrappers.cpp  4  ******************************************************************************  5  * @attention  6  *  7  * <h2><center>© Copyright (c) 2020 STMicroelectronics.  8  * All rights reserved.</center></h2>  9  * 10  * This software component is licensed by ST under Ultimate Liberty license 11  * SLA0044, the "License"; You may not use this file except in compliance with 12  * the License. You may obtain a copy of the License at: 13  *                              14  * 15  ****************************************************************************** 16  */ 17#include <touchgfx/hal/OSWrappers.hpp> 18#include <stm32f4xx_hal.h> 19#include <touchgfx/hal/GPIO.hpp> 20#include <touchgfx/hal/HAL.hpp> 21#include <rtthread.h> 22#include <rtdevice.h> 23#include <board.h> 24 25static rt_sem_t frame_buffer_sem; 26static rt_mq_t vsync_q = 0; 27using namespace touchgfx; 28 29// Just a dummy value to insert in the VSYNC queue. 30static uint8_t dummy = 0x5a; 31 32/* 33 * Initialize frame buffer semaphore and queue/mutex for VSYNC signal. 34 */ 35void OSWrappers::initialize() 36{ 37 38      frame_buffer_sem = rt_sem_create("gfx_sem", 1, RT_IPC_FLAG_PRIO); 39    // Create a queue of length 1 40    vsync_q = rt_mq_create("gfx_mq", 1, 1, RT_IPC_FLAG_PRIO); 41 42} 43 44/* 45 * Take the frame buffer semaphore. Blocks until semaphore is available. 46 */ 47void OSWrappers::takeFrameBufferSemaphore() 48{ 49     rt_sem_take(frame_buffer_sem, RT_WAITING_FOREVER); 50} 51 52/* 53 * Release the frame buffer semaphore. 54 */ 55void OSWrappers::giveFrameBufferSemaphore() 56{ 57    rt_sem_release(frame_buffer_sem); 58} 59 60/* 61 * Attempt to obtain the frame buffer semaphore. If semaphore is not available, do 62 * nothing. 63 * 64 * Note must return immediately! This function does not care who has the taken the semaphore, 65 * it only serves to make sure that the semaphore is taken by someone. 66 */ 67void OSWrappers::tryTakeFrameBufferSemaphore() 68{ 69    rt_sem_trytake(frame_buffer_sem); 70} 71 72/* 73 * Release the frame buffer semaphore in a way that is safe in interrupt context. Called 74 * from ISR. 75 * 76 * Release the frame buffer semaphore in a way that is safe in interrupt context. 77 * Called from ISR. 78 */ 79void OSWrappers::giveFrameBufferSemaphoreFromISR() 80{ 81    // Since this is called from an interrupt, FreeRTOS requires special handling to trigger a 82    // re-scheduling. May be applicable for other OSes as well. 83        rt_sem_release(frame_buffer_sem); 84} 85 86/* 87 * Signal that a VSYNC has occurred. Should make the vsync queue/mutex available. 88 * 89 * Note This function is called from an ISR, and should (depending on OS) trigger a 90 * scheduling. 91 */ 92void OSWrappers::signalVSync() 93{ 94    if (vsync_q) 95    { 96        rt_mq_send(vsync_q, &dummy, 1); 97    } 98} 99100/*101 * This function blocks until a VSYNC occurs.102 *103 * Note This function must first clear the mutex/queue and then wait for the next one to104 * occur.105 */106void OSWrappers::waitForVSync()107{108    // First make sure the queue is empty, by trying to remove an element with 0 timeout.109    rt_mq_recv(vsync_q, &dummy, 1, 0);110111    // Then, wait for next VSYNC to occur.112    rt_mq_recv(vsync_q, &dummy, 1, RT_WAITING_FOREVER);113}114115/*116 * A function that causes executing task to sleep for a number of milliseconds.117 *118 * A function that causes executing task to sleep for a number of milliseconds.119 * This function is OPTIONAL. It is only used by the TouchGFX in the case of120 * a specific frame refresh strategy (REFRESH_STRATEGY_OPTIM_SINGLE_BUFFER_TFT_CTRL).121 * Due to backwards compatibility, in order for this function to be useable by the HAL122 * the function must be explicitly registered:123 * hal.registerTaskDelayFunction(&OSWrappers::taskDelay)124 *125 * see HAL::setFrameRefreshStrategy(FrameRefreshStrategy s)126 * see HAL::registerTaskDelayFunction(void (*delayF)(uint16_t))127 */128void OSWrappers::taskDelay(uint16_t ms)129{130     rt_thread_mdelay(ms);131}132static rt_base_t IdleTaskHook(void* p)133{134    if ((int)p) //idle task sched out135    {136        touchgfx::HAL::getInstance()->setMCUActive(true);137    }138    else //idle task sched in139    {140        touchgfx::HAL::getInstance()->setMCUActive(false);141    }142    return RT_TRUE;143}144145// FreeRTOS specific handlers146extern "C"147{148    void vApplicationStackOverflowHook(rt_thread_t xTask,149                                       signed char* pcTaskName)150    {151        while (1);152    }153154    void vApplicationMallocFailedHook(rt_thread_t xTask,155                                      signed char* pcTaskName)156    {157        while (1);158    }159160    void vApplicationIdleHook(void)161    {162        // Set task tag in order to have the "IdleTaskHook" function called when the idle task is163        // switched in/out. Used solely for measuring MCU load, and can be removed if MCU load164        // readout is not needed.165        //vTaskSetApplicationTaskTag(NULL, IdleTaskHook);166    }167}168/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

嵌入式物联网需要学的东西真的非常多,千万不要学错了路线和内容,导致工资要不上去!

无偿分享大家一个资料包,差不多150多G。里面学习内容、面经、项目都比较新也比较全!某鱼上买估计至少要好几十。

点击这里找小助理0元领取:点击文中文领取

5.新建touchgfx的应用例程文件

拷贝main.c的部分外设初始化代码到sample_touchgfx.c,示例代码如下:

  1#include <rtthread.h>  2#include <rtdevice.h>  3#include <board.h>  4#include "app_touchgfx.h"  5  6CRC_HandleTypeDef hcrc;  7  8DMA2D_HandleTypeDef hdma2d;  9 10LTDC_HandleTypeDef hltdc; 11 12static void MX_CRC_Init(void); 13static void MX_DMA2D_Init(void); 14static void MX_LTDC_Init(void); 15 16#define DISP_Pin    GET_PIN(D, 4) 17#define CTDL_BL_Pin GET_PIN(D, 7) 18#define WIFI_Pin    GET_PIN(G, 9)//野火开发板使用LCD需要关闭WIFI电源 19 20/** 21  * @brief This function handles LTDC global interrupt. 22  */ 23void LTDC_IRQHandler(void) 24{ 25  /* USER CODE BEGIN LTDC_IRQn 0 */ 26 27  /* USER CODE END LTDC_IRQn 0 */ 28  HAL_LTDC_IRQHandler(&hltdc); 29  /* USER CODE BEGIN LTDC_IRQn 1 */ 30 31  /* USER CODE END LTDC_IRQn 1 */ 32} 33 34/** 35  * @brief This function handles DMA2D global interrupt. 36  */ 37void DMA2D_IRQHandler(void) 38{ 39  /* USER CODE BEGIN DMA2D_IRQn 0 */ 40 41  /* USER CODE END DMA2D_IRQn 0 */ 42  HAL_DMA2D_IRQHandler(&hdma2d); 43  /* USER CODE BEGIN DMA2D_IRQn 1 */ 44 45  /* USER CODE END DMA2D_IRQn 1 */ 46} 47/** 48  * @brief CRC Initialization Function 49  * @param None 50  * @retval None 51  */ 52static void MX_CRC_Init(void) 53{ 54 55  /* USER CODE BEGIN CRC_Init 0 */ 56 57  /* USER CODE END CRC_Init 0 */ 58 59  /* USER CODE BEGIN CRC_Init 1 */ 60 61  /* USER CODE END CRC_Init 1 */ 62  hcrc.Instance = CRC; 63  if (HAL_CRC_Init(&hcrc) != HAL_OK) 64  { 65    Error_Handler(); 66  } 67  /* USER CODE BEGIN CRC_Init 2 */ 68 69  /* USER CODE END CRC_Init 2 */ 70 71} 72 73/** 74  * @brief DMA2D Initialization Function 75  * @param None 76  * @retval None 77  */ 78static void MX_DMA2D_Init(void) 79{ 80 81  /* USER CODE BEGIN DMA2D_Init 0 */ 82 83  /* USER CODE END DMA2D_Init 0 */ 84 85  /* USER CODE BEGIN DMA2D_Init 1 */ 86 87  /* USER CODE END DMA2D_Init 1 */ 88  hdma2d.Instance = DMA2D; 89  hdma2d.Init.Mode = DMA2D_M2M; 90  hdma2d.Init.ColorMode = DMA2D_OUTPUT_ARGB8888; 91  hdma2d.Init.OutputOffset = 0; 92  hdma2d.LayerCfg[1].InputOffset = 0; 93  hdma2d.LayerCfg[1].InputColorMode = DMA2D_INPUT_ARGB8888; 94  hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA; 95  hdma2d.LayerCfg[1].InputAlpha = 0; 96  if (HAL_DMA2D_Init(&hdma2d) != HAL_OK) 97  { 98    Error_Handler(); 99  }100  if (HAL_DMA2D_ConfigLayer(&hdma2d, 1) != HAL_OK)101  {102    Error_Handler();103  }104  /* USER CODE BEGIN DMA2D_Init 2 */105106  /* USER CODE END DMA2D_Init 2 */107108}109110/**111  * @brief LTDC Initialization Function112  * @param None113  * @retval None114  */115static void MX_LTDC_Init(void)116{117118  /* USER CODE BEGIN LTDC_Init 0 */119120  /* USER CODE END LTDC_Init 0 */121122  LTDC_LayerCfgTypeDef pLayerCfg = {0};123124  /* USER CODE BEGIN LTDC_Init 1 */125126  /* USER CODE END LTDC_Init 1 */127  hltdc.Instance = LTDC;128  hltdc.Init.HSPolarity = LTDC_HSPOLARITY_AL;129  hltdc.Init.VSPolarity = LTDC_VSPOLARITY_AL;130  hltdc.Init.DEPolarity = LTDC_DEPOLARITY_AL;131  hltdc.Init.PCPolarity = LTDC_PCPOLARITY_IPC;132  hltdc.Init.HorizontalSync = 0;133  hltdc.Init.VerticalSync = 0;134  hltdc.Init.AccumulatedHBP = 46;135  hltdc.Init.AccumulatedVBP = 23;136  hltdc.Init.AccumulatedActiveW = 846;137  hltdc.Init.AccumulatedActiveH = 503;138  hltdc.Init.TotalWidth = 866;139  hltdc.Init.TotalHeigh = 525;140  hltdc.Init.Backcolor.Blue = 0;141  hltdc.Init.Backcolor.Green = 0;142  hltdc.Init.Backcolor.Red = 0;143  if (HAL_LTDC_Init(&hltdc) != HAL_OK)144  {145    Error_Handler();146  }147  pLayerCfg.WindowX0 = 0;148  pLayerCfg.WindowX1 = 800;149  pLayerCfg.WindowY0 = 0;150  pLayerCfg.WindowY1 = 480;151  pLayerCfg.PixelFormat = LTDC_PIXEL_FORMAT_RGB565;152  pLayerCfg.Alpha = 255;153  pLayerCfg.Alpha0 = 0;154  pLayerCfg.BlendingFactor1 = LTDC_BLENDING_FACTOR1_CA;155  pLayerCfg.BlendingFactor2 = LTDC_BLENDING_FACTOR2_CA;156  pLayerCfg.FBStartAdress = 0xd0000000;157  pLayerCfg.ImageWidth = 800;158  pLayerCfg.ImageHeight = 480;159  pLayerCfg.Backcolor.Blue = 255;160  pLayerCfg.Backcolor.Green = 255;161  pLayerCfg.Backcolor.Red = 255;162  if (HAL_LTDC_ConfigLayer(&hltdc, &pLayerCfg, 0) != HAL_OK)163  {164    Error_Handler();165  }166  /* USER CODE BEGIN LTDC_Init 2 */167    rt_pin_mode(DISP_Pin, PIN_MODE_OUTPUT);168    rt_pin_mode(CTDL_BL_Pin, PIN_MODE_OUTPUT);169    rt_pin_mode(WIFI_Pin, PIN_MODE_OUTPUT);170    rt_pin_write(DISP_Pin, 1);171    rt_pin_write(CTDL_BL_Pin, 1);172    rt_pin_write(WIFI_Pin, 0);173  /* USER CODE END LTDC_Init 2 */174175}176void touchgfx_thread_entry(void *parameter)177{178    MX_CRC_Init();179    MX_DMA2D_Init();180    MX_LTDC_Init();181    MX_TouchGFX_Init();182    MX_TouchGFX_Process();183    for(;;)184    {185        rt_thread_mdelay(100);186    }187}188int TouchGFXTask(void)189{190    rt_thread_t tid = NULL;191    tid = rt_thread_create("TouchGFX",192                            touchgfx_thread_entry, RT_NULL,193                            4096, 15, 20);194195   if (tid != RT_NULL)196        rt_thread_startup(tid);197   else198        return -1;199200    return RT_EOK;201}202INIT_APP_EXPORT(TouchGFXTask);
6.编辑 board/KConfig7.编辑 board/SConscript8.新建 board/CubeMX_Config/SConscript
 1import os 2import rtconfig 3from building import * 4 5cwd = GetCurrentDir() 6 7# add general drivers 8src = Split(''' 9Src/sample_touchgfx.c10Src/OSWrappers_RTT.cpp11Src/STM32DMA.cpp12Src/STM32TouchController.cpp13Src/TouchGFXGPIO.cpp14Src/TouchGFXConfiguration.cpp15Src/TouchGFXGeneratedHAL.cpp16Src/TouchGFXHAL.cpp17Src/app_touchgfx.c18''')1920path =  [cwd + '/Src']21path += [cwd + '/Middlewares/ST/touchgfx/framework/include']222324if rtconfig.CROSS_TOOL == 'gcc':25    src += [cwd + '/Middlewares/ST/touchgfx/lib/core/cortex_m4f/gcc/libtouchgfx.a']26elif rtconfig.CROSS_TOOL == 'keil':27    src += [cwd + '/Middlewares/ST/touchgfx/lib/core/cortex_m4f/Keil/touchgfx_core.lib']28elif rtconfig.CROSS_TOOL == 'iar':29    src += [cwd + '/Middlewares/ST/touchgfx/lib/core/cortex_m4f/IAR8.x/touchgfx_core.a']3031group = DefineGroup('TouchGFX_app', src, depend = [''], CPPPATH = path)3233# add TouchGFX generated34genSrc = Glob('./Src/generated/fonts/src/*.cpp')35genSrc += Glob('./Src/generated/gui_generated/src/*/*.cpp')36genSrc += Glob('./Src/generated/images/src/*.cpp')37genSrc += Glob('./Src/generated/texts/src/*.cpp')3839genPath = [cwd + '/Src/generated/fonts/include']40genPath += [cwd + '/Src/generated/gui_generated/include']41genPath += [cwd + '/Src/generated/images/include']42genPath += [cwd + '/Src/generated/texts/include']4344group = group + DefineGroup('TouchGFX_generated', genSrc, depend = [''], CPPPATH = genPath)4546# add TouchGFX resource47resSrc = Glob('./Src/generated/images/src/*/*.cpp')4849group = group + DefineGroup('TouchGFX_resource', resSrc, depend = [''])5051# add TouchGFX gui52guiSrc = Glob('./Src/gui/src/*/*.cpp')53guiPath = [cwd + '/Src/gui/include']5455group = group + DefineGroup('TouchGFX_gui', guiSrc, depend = [''], CPPPATH = guiPath)5657Return('group')
9.编辑libraries/STM32F4xx_HAL/SConscript

添加如下代码:

1if GetDepend(['BSP_USING_TouchGFX']):2    src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_ltdc.c']3    src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_ltdc_ex.c']4    src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma2d.c']5    src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_dma2d.c']6    src += ['STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dsi.c']
10.配置 Menuconfig

11.生成工程

使用scons --target=mdk5 命令生成 MDK工程。此时还不能编译下载,因为图片资源太大,会超过内部flash的存储空间,还需要把图片下载到外部flash,上电初始化通过spi把图片资源加载到SDRAM。

12.添加FAL软件包

配置fal_cfg.h,

tgfx分区用于touchgfx的资源存储,其他分区用于OTA、文件系统和系统参数配置。

 1/* 2 * Copyright (c) 2006-2018, RT-Thread Development Team 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Change Logs: 7 * Date           Author       Notes 8 * 2018-05-17     armink       the first version 9 */1011#ifndef _FAL_CFG_H_12#define _FAL_CFG_H_1314#include <rtconfig.h>15#include <board.h>1617#define FLASH_SIZE_GRANULARITY_16K   (4 * 16 * 1024)18#define FLASH_SIZE_GRANULARITY_64K   (64 * 1024)19#define FLASH_SIZE_GRANULARITY_128K  (7 * 128 * 1024)2021#define STM32_FLASH_START_ADRESS_16K  STM32_FLASH_START_ADRESS22#define STM32_FLASH_START_ADRESS_64K  (STM32_FLASH_START_ADRESS_16K + FLASH_SIZE_GRANULARITY_16K)23#define STM32_FLASH_START_ADRESS_128K (STM32_FLASH_START_ADRESS_64K + FLASH_SIZE_GRANULARITY_64K)24252627/* ===================== Flash device Configuration ========================= */28extern const struct fal_flash_dev stm32_onchip_flash_16k;29extern const struct fal_flash_dev stm32_onchip_flash_64k;30extern const struct fal_flash_dev stm32_onchip_flash_128k;31extern struct fal_flash_dev nor_flash0;3233/* flash device table */34#define FAL_FLASH_DEV_TABLE                                          \35{                                                                    \36    &stm32_onchip_flash_16k,                                         \37    &stm32_onchip_flash_64k,                                         \38    &stm32_onchip_flash_128k,                                        \39    &nor_flash0,                                                     \40}41/* ====================== Partition Configuration ========================== */42#ifdef FAL_PART_HAS_TABLE_CFG43/* partition table */44#define FAL_PART_TABLE                                                               \45{                                                                                    \46    {FAL_PART_MAGIC_WROD, "bootloader", "onchip_flash_16k",  0 , FLASH_SIZE_GRANULARITY_16K , 0}, \47    {FAL_PART_MAGIC_WROD, "ef",         "onchip_flash_64k",  0 , FLASH_SIZE_GRANULARITY_64K , 0}, \48    {FAL_PART_MAGIC_WROD, "app",        "onchip_flash_128k", 0 , FLASH_SIZE_GRANULARITY_128K, 0}, \49    {FAL_PART_MAGIC_WROD, "tgfx",       "W25Q128",           0 , 4 * 1024 * 1024, 0}, \50    {FAL_PART_MAGIC_WROD, "download",   "W25Q128",           4 * 1024 * 1024 , 1024 * 1024, 0}, \51    {FAL_PART_MAGIC_WROD, "factory",    "W25Q128",          (4 * 1024 + 1024) * 1024 , 1024 * 1024, 0}, \52    {FAL_PART_MAGIC_WROD, "filesystem", "W25Q128",          (4 * 1024 + 1024 + 1024) * 1024 , 2 * 1024 * 1024, 0}, \53}54#endif /* FAL_PART_HAS_TABLE_CFG */5556#endif /* _FAL_CFG_H_ */

初始化fal

1int fs_init(void)2{3    /* partition initialized */4    fal_init();5    return 0;6}7INIT_COMPONENT_EXPORT(fs_init);
13.配置SPI FLASH制作STM32F429的SPI FLASH下载算法(看附文)修改工程模版template.uvprojx

编辑链接文件

.将位图数据从外部flash复制到缓存

此时重新生成 MDK工程,便可以下载到板子上运行。

参考资料:.添加触摸软件包

配置模拟I2C

在applications文件夹下新建gtxx_ccollect.c文件,添加一下内容

  1/*  2 * Copyright (c) 2006-2018, RT-Thread Development Team  3 *  4 * SPDX-License-Identifier: Apache-2.0  5 *  6 * Change Logs:  7 * Date           Author       Notes  8 * 2019-06-01     tyustli     the first version  9 */ 10 11#include <rtthread.h> 12#include "gt9147.h" 13#define DBG_TAG "gt9147" 14#define DBG_LVL DBG_LOG 15#include <rtdbg.h> 16#define THREAD_PRIORITY   5 17#define THREAD_STACK_SIZE 1024 18#define THREAD_TIMESLICE  5 19 20#define GT9147_RST_PIN 59 21#define GT9147_IRQ_PIN 61 22 23static rt_thread_t  gt9147_thread = RT_NULL; 24static rt_sem_t     gt9147_sem = RT_NULL; 25static rt_device_t  dev = RT_NULL; 26static struct       rt_touch_data *read_data; 27static struct       rt_touch_info info; 28 29struct rt_touch_data *read_coordinate(void) 30{ 31    return &read_data[0]; 32} 33static void gt9147_entry(void *parameter) 34{ 35    rt_device_control(dev, RT_TOUCH_CTRL_GET_INFO, &info); 36 37    read_data = (struct rt_touch_data *)rt_malloc(sizeof(struct rt_touch_data) * info.point_num); 38 39    while (1) 40    { 41        rt_sem_take(gt9147_sem, RT_WAITING_FOREVER); 42 43        if (rt_device_read(dev, 0, read_data, info.point_num) == info.point_num) 44        { 45            for (rt_uint8_t i = 0; i < info.point_num; i++) 46            { 47                if (read_data[i].event == RT_TOUCH_EVENT_DOWN || read_data[i].event == RT_TOUCH_EVENT_MOVE) 48                { 49                    LOG_D("%d %d %d %d %d\n", read_data[i].track_id, 50                               read_data[i].x_coordinate, 51                               read_data[i].y_coordinate, 52                               read_data[i].timestamp, 53                               read_data[i].width); 54                } 55 56            } 57        } 58        rt_device_control(dev, RT_TOUCH_CTRL_ENABLE_INT, RT_NULL); 59    } 60} 61 62static rt_err_t rx_callback(rt_device_t dev, rt_size_t size) 63{ 64    rt_sem_release(gt9147_sem); 65    rt_device_control(dev, RT_TOUCH_CTRL_DISABLE_INT, RT_NULL); 66    return 0; 67} 68 69static int rt_hw_gt9147_port(void) 70{ 71    struct rt_touch_config config; 72    rt_uint8_t rst; 73    rst = GT9147_RST_PIN; 74    config.dev_name = "i2c1"; 75    config.irq_pin.pin  = GT9147_IRQ_PIN; 76    config.irq_pin.mode = PIN_MODE_INPUT_PULLDOWN; 77    config.user_data = &rst; 78    rt_hw_gt9147_init("gt", &config); 79    return 0; 80} 81/* Test function */ 82int gt9147_init(void) 83{ 84    void *id; 85    rt_hw_gt9147_port(); 86    dev = rt_device_find("gt"); 87    if (dev == RT_NULL) 88    { 89        rt_kprintf("can't find device gt\n"); 90        return -1; 91    } 92 93    if (rt_device_open(dev, RT_DEVICE_FLAG_INT_RX) != RT_EOK) 94    { 95        rt_kprintf("open device failed!"); 96        return -1; 97    } 98 99    id = rt_malloc(sizeof(rt_uint8_t) * 8);100    rt_device_control(dev, RT_TOUCH_CTRL_GET_ID, id);101    rt_uint8_t * read_id = (rt_uint8_t *)id;102    rt_kprintf("id = %c %c %c %c \n", read_id[0], read_id[1], read_id[2], read_id[3]);103104    rt_device_control(dev, RT_TOUCH_CTRL_GET_INFO, id);105    rt_kprintf("range_x = %d \n", (*(struct rt_touch_info*)id).range_x);106    rt_kprintf("range_y = %d \n", (*(struct rt_touch_info*)id).range_y);107    rt_kprintf("point_num = %d \n", (*(struct rt_touch_info*)id).point_num);108    rt_free(id);109    rt_device_set_rx_indicate(dev, rx_callback);110    gt9147_sem = rt_sem_create("dsem", 0, RT_IPC_FLAG_FIFO);111112    if (gt9147_sem == RT_NULL)113    {114        rt_kprintf("create dynamic semaphore failed.\n");115        return -1;116    }117118    gt9147_thread = rt_thread_create("gt9147",119                                     gt9147_entry,120                                     RT_NULL,121                                     THREAD_STACK_SIZE,122                                     THREAD_PRIORITY,123                                     THREAD_TIMESLICE);124125    if (gt9147_thread != RT_NULL)126        rt_thread_startup(gt9147_thread);127128    return 0;129}130INIT_APP_EXPORT(gt9147_init);

修改STM32TouchController.cpp

 1/** 2  ****************************************************************************** 3  * File Name          : STM32TouchController.cpp 4  ****************************************************************************** 5  * @attention 6  * 7  * <h2><center>© Copyright (c) 2020 STMicroelectronics. 8  * All rights reserved.</center></h2> 9  *10  * This software component is licensed by ST under Ultimate Liberty license11  * SLA0044, the "License"; You may not use this file except in compliance with12  * the License. You may obtain a copy of the License at:13  *                               *15  ******************************************************************************16  */1718/* USER CODE BEGIN STM32TouchController */1920#include <STM32TouchController.hpp>21#include "gt9147.h"22extern "C"23{24    struct rt_touch_data *read_coordinate(void);25}26void STM32TouchController::init()27{28    /**29     * Initialize touch controller and driver30     *31     */32}3334bool STM32TouchController::sampleTouch(int32_t& x, int32_t& y)35{36    /**37     * By default sampleTouch returns false,38     * return true if a touch has been detected, otherwise false.39     *40     * Coordinates are passed to the caller by reference by x and y.41     *42     * This function is called by the TouchGFX framework.43     * By default sampleTouch is called every tick, this can be adjusted by HAL::setTouchSampleRate(int8_t);44     *45     */46    struct  rt_touch_data *read_data;47    read_data = read_coordinate();4849    if (read_data->event == RT_TOUCH_EVENT_DOWN || read_data->event == RT_TOUCH_EVENT_MOVE)50    {51        x = read_data->x_coordinate;52        y = read_data->y_coordinate;53        return true;54    }55    else56    {57        return false;58    }5960}6162/* USER CODE END STM32TouchController */6364/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
15.让时钟动起来

打开rtc实时时钟

添加以下代码

16.让screen切换滑动起来

配置此缓存可以让screen切换具有滑动效果

转载自:AloT开源项目分享

文章来源于【DIY数字仪表】STM32F429移植TouchGFX到RT-Thread系统(1)

原文链接:

标签: #stm32移植rtthreadlvgl #stm32移植rtthread usb