优化工程目录
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
# 收集当前目录下所有 .c 文件
|
||||
file(GLOB_RECURSE SRC_LIST "*.c")
|
||||
|
||||
idf_component_register(SRCS ${SRC_LIST}
|
||||
PRIV_REQUIRES spi_flash esp_driver_spi esp_driver_gpio
|
||||
INCLUDE_DIRS "")
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2010-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: CC0-1.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/timers.h"
|
||||
#include "esp_chip_info.h"
|
||||
#include "esp_flash.h"
|
||||
#include "esp_system.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
|
||||
#include "spi.h"
|
||||
|
||||
#include "lcd.h"
|
||||
|
||||
TimerHandle_t periodic_timer = NULL;
|
||||
|
||||
// 定时器回调函数(在定时器服务任务中执行)
|
||||
void periodic_timer_callback(TimerHandle_t xTimer)
|
||||
{
|
||||
//printf("每秒执行一次的函数\n");
|
||||
// 在这里执行你的周期性任务
|
||||
// 注意:此回调函数应尽快返回,避免阻塞定时器服务任务
|
||||
|
||||
lcd_one_second_task();
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
printf("Hello world!\n");
|
||||
|
||||
/* Print chip information */
|
||||
esp_chip_info_t chip_info;
|
||||
uint32_t flash_size;
|
||||
esp_chip_info(&chip_info);
|
||||
printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
|
||||
CONFIG_IDF_TARGET,
|
||||
chip_info.cores,
|
||||
(chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
|
||||
(chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
|
||||
(chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
|
||||
(chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : "");
|
||||
|
||||
unsigned major_rev = chip_info.revision / 100;
|
||||
unsigned minor_rev = chip_info.revision % 100;
|
||||
printf("silicon revision v%d.%d, ", major_rev, minor_rev);
|
||||
if (esp_flash_get_size(NULL, &flash_size) != ESP_OK)
|
||||
{
|
||||
printf("Get flash size failed");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
|
||||
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");
|
||||
|
||||
printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());
|
||||
|
||||
printf("sizeof(int) ==%d\n", sizeof(int));
|
||||
|
||||
spi_init();
|
||||
lcd_init();
|
||||
|
||||
// 配置一个每隔一秒运行一次的函数 用于计算fps等
|
||||
// 创建周期性软件定时器
|
||||
// 参数: 定时器名称, 周期(单位: Tick), 自动重载, 回调参数, 回调函数
|
||||
periodic_timer = xTimerCreate(
|
||||
"PeriodicTimer", // 定时器名称
|
||||
pdMS_TO_TICKS(1000), // 周期:1000毫秒 (转换为Tick)
|
||||
pdTRUE, // 自动重载 (pdTRUE为周期性,pdFALSE为单次)
|
||||
(void *)0, // 传递给回调函数的参数
|
||||
periodic_timer_callback // 回调函数
|
||||
);
|
||||
|
||||
if (periodic_timer != NULL)
|
||||
{
|
||||
// 启动定时器 (0 ticks后启动)
|
||||
xTimerStart(periodic_timer, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("创建定时器失败!\n");
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
lcd_clear_buf(rand());
|
||||
lcd_send_full_buf();
|
||||
vTaskDelay(1); // 必须让出CPU
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
#include "lcd.h"
|
||||
|
||||
static const char *TAG = "LCD";
|
||||
// uint16_t dis_buff[LCD_WW][LCD_HH];
|
||||
|
||||
lcd_t lcd_main;
|
||||
|
||||
// SPI 写数据
|
||||
esp_err_t lcd_spi_send_data_8(uint8_t data)
|
||||
{
|
||||
spi_transaction_t trans = {
|
||||
.length = 8, // 数据位数
|
||||
.flags = SPI_TRANS_USE_TXDATA, // 必须加这个标志
|
||||
};
|
||||
trans.tx_data[0] = (data);
|
||||
|
||||
esp_err_t err = spi_device_transmit(lcd_spi, &trans);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
// SPI 写数据
|
||||
esp_err_t lcd_spi_send_data_16(uint16_t data)
|
||||
{
|
||||
spi_transaction_t trans = {
|
||||
.length = 16, // 数据位数
|
||||
.flags = SPI_TRANS_USE_TXDATA, // 必须加这个标志
|
||||
};
|
||||
trans.tx_data[0] = (data >> 8);
|
||||
trans.tx_data[1] = (data);
|
||||
esp_err_t err = spi_device_transmit(lcd_spi, &trans);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t lcd_spi_send_data_any(uint8_t *data, uint16_t len)
|
||||
{
|
||||
spi_transaction_t trans = {
|
||||
.length = len * 8,
|
||||
.tx_buffer = data,
|
||||
};
|
||||
esp_err_t err = spi_device_transmit(lcd_spi, &trans);
|
||||
return err;
|
||||
}
|
||||
|
||||
// SPI 写命令
|
||||
void lcd_spi_send_cmd(uint8_t data)
|
||||
{
|
||||
esp_err_t err;
|
||||
gpio_set_level(LCD_DS, 0);
|
||||
err = lcd_spi_send_data_8(data);
|
||||
gpio_set_level(LCD_DS, 1);
|
||||
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "命令发送失败:%x", err);
|
||||
}
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
函数说明:设置起始和结束地址
|
||||
入口数据:x1,x2 设置列的起始和结束地址
|
||||
y1,y2 设置行的起始和结束地址
|
||||
返回值: 无
|
||||
******************************************************************************/
|
||||
void lcd_set_window(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)
|
||||
{
|
||||
|
||||
#if USE_HORIZONTAL == 0
|
||||
lcd_spi_send_cmd(0x2a); // 列地址设置
|
||||
lcd_spi_send_data_16(x1);
|
||||
lcd_spi_send_data_16(x2);
|
||||
lcd_spi_send_cmd(0x2b); // 行地址设置
|
||||
lcd_spi_send_data_16(y1);
|
||||
lcd_spi_send_data_16(y2);
|
||||
lcd_spi_send_cmd(0x2c); // 储存器写
|
||||
#elif USE_HORIZONTAL == 1
|
||||
lcd_spi_send_cmd(0x2a); // 列地址设置
|
||||
lcd_spi_send_data_16(x1);
|
||||
lcd_spi_send_data_16(x2);
|
||||
lcd_spi_send_cmd(0x2b); // 行地址设置
|
||||
lcd_spi_send_data_16(y1 + 80);
|
||||
lcd_spi_send_data_16(y2 + 80);
|
||||
lcd_spi_send_cmd(0x2c); // 储存器写
|
||||
#elif USE_HORIZONTAL == 2
|
||||
lcd_spi_send_cmd(0x2a); // 列地址设置
|
||||
lcd_spi_send_data_16(x1);
|
||||
lcd_spi_send_data_16(x2);
|
||||
lcd_spi_send_cmd(0x2b); // 行地址设置
|
||||
lcd_spi_send_data_16(y1);
|
||||
lcd_spi_send_data_16(y2);
|
||||
lcd_spi_send_cmd(0x2c); // 储存器写
|
||||
#elif USE_HORIZONTAL == 3
|
||||
lcd_spi_send_cmd(0x2a); // 列地址设置
|
||||
lcd_spi_send_data_16(x1 + 80);
|
||||
lcd_spi_send_data_16(x2 + 80);
|
||||
lcd_spi_send_cmd(0x2b); // 行地址设置
|
||||
lcd_spi_send_data_16(y1);
|
||||
lcd_spi_send_data_16(y2);
|
||||
lcd_spi_send_cmd(0x2c); // 储存器写
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void lcd_one_second_task()
|
||||
{
|
||||
lcd_main.fps=lcd_main.fps_count;
|
||||
lcd_main.fps_count=0;
|
||||
|
||||
ESP_LOGI(TAG, "FPS:%d",lcd_main.fps);
|
||||
}
|
||||
|
||||
void lcd_send_full_buf()
|
||||
{
|
||||
gpio_set_level(LCD_CS, 0);
|
||||
lcd_set_window(0, 0, LCD_WW - 1, LCD_HH - 1);
|
||||
// 分包发送 单次9600 分16次发送
|
||||
spi_transaction_t trans = {
|
||||
.length = 9600 * 8, // 数据位数
|
||||
};
|
||||
uint8_t *d_buf = (uint8_t *)(lcd_main.buf);
|
||||
for (uint16_t i = 0; i < 16; i++)
|
||||
{
|
||||
// trans.length=9600*8;
|
||||
trans.tx_buffer = d_buf + (i * 9600);
|
||||
spi_device_transmit(lcd_spi, &trans);
|
||||
}
|
||||
gpio_set_level(LCD_CS, 1);
|
||||
|
||||
//发送成功 帧计数+1
|
||||
lcd_main.fps_count+=1;
|
||||
}
|
||||
|
||||
void lcd_clear_buf(uint16_t color)
|
||||
{
|
||||
for (uint32_t i = 0; i < (LCD_WW * LCD_HH); i++)
|
||||
{
|
||||
lcd_main.buf[i] = color;
|
||||
}
|
||||
}
|
||||
|
||||
void lcd_init()
|
||||
{
|
||||
ESP_LOGI(TAG, "配置GPIO");
|
||||
|
||||
// 初始化屏幕IO
|
||||
// 配置GPIO
|
||||
gpio_config_t io_conf = {
|
||||
.pin_bit_mask = (1ULL << LCD_DS) | (1ULL << LCD_CS), // LCD_DS LCD_CS
|
||||
.mode = GPIO_MODE_OUTPUT, // 输出模式
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE, //
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE, //
|
||||
.intr_type = GPIO_INTR_DISABLE // 禁用中断
|
||||
};
|
||||
|
||||
// 应用配置
|
||||
gpio_config(&io_conf);
|
||||
|
||||
// 配置 GPIO 驱动能力 (ESP32-C3 支持)
|
||||
// gpio_set_drive_capability(VSPI_MISO, GPIO_DRIVE_CAP_3); // 最大驱动
|
||||
gpio_set_drive_capability(VSPI_MOSI, GPIO_DRIVE_CAP_3);
|
||||
gpio_set_drive_capability(VSPI_SCLK, GPIO_DRIVE_CAP_3);
|
||||
|
||||
ESP_LOGI(TAG, "配置GPIO完成");
|
||||
|
||||
// 设置初始电平
|
||||
gpio_set_level(LCD_DS, 0); // 输出低电平
|
||||
gpio_set_level(LCD_CS, 0);
|
||||
|
||||
ESP_LOGI(TAG, "等待初始化LCD");
|
||||
vTaskDelay(25); //
|
||||
|
||||
// uint8_t data_tmp[16];//数据缓存
|
||||
ESP_LOGI(TAG, "初始化LCD");
|
||||
lcd_spi_send_cmd(0x36);
|
||||
if (USE_HORIZONTAL == 0)
|
||||
lcd_spi_send_data_8(0x00);
|
||||
else if (USE_HORIZONTAL == 1)
|
||||
lcd_spi_send_data_8(0xC0);
|
||||
else if (USE_HORIZONTAL == 2)
|
||||
lcd_spi_send_data_8(0x70);
|
||||
else
|
||||
lcd_spi_send_data_8(0xA0);
|
||||
|
||||
lcd_spi_send_cmd(0x3A);
|
||||
lcd_spi_send_data_8(0x05);
|
||||
|
||||
lcd_spi_send_cmd(0xB2);
|
||||
lcd_spi_send_data_8(0x0C);
|
||||
lcd_spi_send_data_8(0x0C);
|
||||
lcd_spi_send_data_8(0x00);
|
||||
lcd_spi_send_data_8(0x33);
|
||||
lcd_spi_send_data_8(0x33);
|
||||
|
||||
lcd_spi_send_cmd(0xB7);
|
||||
lcd_spi_send_data_8(0x35);
|
||||
|
||||
lcd_spi_send_cmd(0xBB);
|
||||
lcd_spi_send_data_8(0x19);
|
||||
|
||||
lcd_spi_send_cmd(0xC0);
|
||||
lcd_spi_send_data_8(0x2C);
|
||||
|
||||
lcd_spi_send_cmd(0xC2);
|
||||
lcd_spi_send_data_8(0x01);
|
||||
|
||||
lcd_spi_send_cmd(0xC3);
|
||||
lcd_spi_send_data_8(0x12);
|
||||
|
||||
lcd_spi_send_cmd(0xC4);
|
||||
lcd_spi_send_data_8(0x20);
|
||||
|
||||
lcd_spi_send_cmd(0xC6);
|
||||
lcd_spi_send_data_8(0x0F);
|
||||
|
||||
lcd_spi_send_cmd(0xD0);
|
||||
lcd_spi_send_data_8(0xA4);
|
||||
lcd_spi_send_data_8(0xA1);
|
||||
|
||||
lcd_spi_send_cmd(0xE0);
|
||||
lcd_spi_send_data_8(0xD0);
|
||||
lcd_spi_send_data_8(0x04);
|
||||
lcd_spi_send_data_8(0x0D);
|
||||
lcd_spi_send_data_8(0x11);
|
||||
lcd_spi_send_data_8(0x13);
|
||||
lcd_spi_send_data_8(0x2B);
|
||||
lcd_spi_send_data_8(0x3F);
|
||||
lcd_spi_send_data_8(0x54);
|
||||
lcd_spi_send_data_8(0x4C);
|
||||
lcd_spi_send_data_8(0x18);
|
||||
lcd_spi_send_data_8(0x0D);
|
||||
lcd_spi_send_data_8(0x0B);
|
||||
lcd_spi_send_data_8(0x1F);
|
||||
lcd_spi_send_data_8(0x23);
|
||||
|
||||
lcd_spi_send_cmd(0xE1);
|
||||
lcd_spi_send_data_8(0xD0);
|
||||
lcd_spi_send_data_8(0x04);
|
||||
lcd_spi_send_data_8(0x0C);
|
||||
lcd_spi_send_data_8(0x11);
|
||||
lcd_spi_send_data_8(0x13);
|
||||
lcd_spi_send_data_8(0x2C);
|
||||
lcd_spi_send_data_8(0x3F);
|
||||
lcd_spi_send_data_8(0x44);
|
||||
lcd_spi_send_data_8(0x51);
|
||||
lcd_spi_send_data_8(0x2F);
|
||||
lcd_spi_send_data_8(0x1F);
|
||||
lcd_spi_send_data_8(0x1F);
|
||||
lcd_spi_send_data_8(0x20);
|
||||
lcd_spi_send_data_8(0x23);
|
||||
|
||||
lcd_spi_send_cmd(0x21);
|
||||
|
||||
lcd_spi_send_cmd(0x11);
|
||||
vTaskDelay(12);
|
||||
|
||||
lcd_spi_send_cmd(0x29);
|
||||
|
||||
lcd_clear_buf(0xffff);
|
||||
|
||||
gpio_set_level(LCD_CS, 1);
|
||||
ESP_LOGI(TAG, "初始化LCD完成");
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef LCD_H
|
||||
#define LCD_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "esp_log.h"
|
||||
#include "spi.h"
|
||||
|
||||
#define LCD_CS 7
|
||||
#define LCD_DS 6
|
||||
|
||||
#define USE_HORIZONTAL 2 //设置横屏或者竖屏显示 0或1为竖屏 2或3为横屏
|
||||
#define LCD_WW 320
|
||||
#define LCD_HH 240
|
||||
|
||||
typedef struct{
|
||||
uint16_t buf[LCD_WW*LCD_HH];
|
||||
uint16_t fps_count;
|
||||
uint16_t fps;
|
||||
}lcd_t;
|
||||
|
||||
extern lcd_t lcd_main;
|
||||
|
||||
|
||||
void LCD_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2);
|
||||
void lcd_init();
|
||||
|
||||
void lcd_clear_buf(uint16_t Color);
|
||||
void lcd_send_full_buf();
|
||||
void lcd_one_second_task();
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,60 @@
|
||||
#include "spi.h"
|
||||
|
||||
#define SPI_HOST SPI2_HOST
|
||||
|
||||
static const char *TAG = "SPI_2";
|
||||
|
||||
spi_device_handle_t lcd_spi;
|
||||
|
||||
void spi_init()
|
||||
{
|
||||
ESP_LOGI(TAG, "初始化SPI IO总线");
|
||||
esp_err_t ret;
|
||||
|
||||
// 1. SPI 总线配置
|
||||
spi_bus_config_t buscfg = {
|
||||
.miso_io_num = VSPI_MISO, // MISO 引脚
|
||||
.mosi_io_num = VSPI_MOSI, // MOSI 引脚
|
||||
.sclk_io_num = VSPI_SCLK, // 时钟引脚
|
||||
.quadwp_io_num = -1, // 不使用 QWP
|
||||
.quadhd_io_num = -1, // 不使用 QHD
|
||||
.max_transfer_sz = 9600, // 最大传输大小
|
||||
// .flags = 0,
|
||||
// .intr_flags = 0,
|
||||
};
|
||||
|
||||
// 2. 初始化 SPI 总线
|
||||
ret = spi_bus_initialize(SPI_HOST, &buscfg, SPI_DMA_CH_AUTO);
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "IO总线初始化失败");
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "SPI IO总线初始化成功");
|
||||
|
||||
ESP_LOGI(TAG, "SPI 设备配置");
|
||||
|
||||
spi_device_interface_config_t devcfg = {
|
||||
.command_bits = 0, // 无命令位
|
||||
.address_bits = 0, // 无地址位
|
||||
.dummy_bits = 0,
|
||||
.mode = 0, // SPI 模式 0
|
||||
.clock_speed_hz = 80000000, //
|
||||
.spics_io_num = -1,
|
||||
.queue_size = 7, // 队列深度
|
||||
.flags = SPI_DEVICE_NO_DUMMY, // 禁用 dummy 周期
|
||||
.input_delay_ns = 0,
|
||||
.pre_cb = NULL,
|
||||
.post_cb = NULL,
|
||||
|
||||
};
|
||||
|
||||
ret = spi_bus_add_device(SPI_HOST, &devcfg, &lcd_spi);
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "SPI 设备配置失败");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef SPI_H
|
||||
#define SPI_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/spi_master.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
|
||||
#define VSPI_MISO -1 //接收引脚不使用,仅发送
|
||||
#define VSPI_MOSI 3
|
||||
#define VSPI_SCLK 2
|
||||
|
||||
|
||||
|
||||
void spi_init();
|
||||
|
||||
extern spi_device_handle_t lcd_spi;
|
||||
|
||||
|
||||
#endif /* SPI_H */
|
||||
Reference in New Issue
Block a user