直接上代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
#include "esp_log.h"
#define RESET_IO 7
#define CS_IO 10
#define RS_IO 8
#define WR_IO 12
#define RD_IO 26
#define TE_IO 47
#define D0_IO 11
#define D1_IO 13
#define D2_IO 14
#define D3_IO 9
#define D4_IO 39
#define D5_IO 40
#define D6_IO 41
#define D7_IO 42
static const char* TAG = "main";
uint8_t Data_Pin_IO[8] = {D0_IO, D1_IO, D2_IO, D3_IO,
D4_IO, D5_IO, D6_IO, D7_IO};
uint64_t MCU_CTOL_IO_MASK = 0x000004001580;
uint64_t MCU_DATA_IO_MASK = 0x078000006A00;
void DM_CTRL_IO_Init(void)
{
gpio_config_t io_conf = {};
gpio_set_level(RESET_IO, 0);
gpio_set_level(CS_IO, 1);
gpio_set_level(RS_IO, 1);
gpio_set_level(WR_IO, 1);
gpio_set_level(RD_IO, 1);
io_conf.mode = GPIO_MODE_DEF_OUTPUT;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.pin_bit_mask = MCU_CTOL_IO_MASK;
gpio_config(&io_conf);
}
void DM_DATA_IO_InOut(uint8_t InOut)
{
gpio_config_t io_conf = {};
if (InOut == GPIO_MODE_DEF_OUTPUT)
{
for (uint8_t i = 0; i < 8; i++)
{
gpio_set_level(Data_Pin_IO[i], 0);
}
}
io_conf.mode = InOut;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.pin_bit_mask = MCU_DATA_IO_MASK;
gpio_config(&io_conf);
}
void Write_CMD(uint8_t cmd)
{
ESP_LOGI(TAG, "Write cmd = 0x%2X", cmd);
gpio_set_level(CS_IO, 0);
for (uint8_t i = 0; i < 8; i++)
{
gpio_set_direction(Data_Pin_IO[i], GPIO_MODE_DEF_OUTPUT) ;
}
gpio_set_level(RS_IO, 0);
gpio_set_level(WR_IO, 0);
for (uint8_t i = 0; i < 8; i++)
{
if (cmd & BIT(i))
gpio_set_level(Data_Pin_IO[i], 1);
else
gpio_set_level(Data_Pin_IO[i], 0);
}
gpio_set_level(WR_IO, 1);
gpio_set_level(RS_IO, 1);
gpio_set_level(CS_IO, 1);
for (uint8_t i = 0; i < 8; i++)
{
gpio_set_level(Data_Pin_IO[i], 0);
}
}
void Write_DATA(uint8_t data)
{
ESP_LOGI(TAG, "Write data = 0x%2X", data);
gpio_set_level(CS_IO, 0);
for (uint8_t i = 0; i < 8; i++)
{
gpio_set_direction(Data_Pin_IO[i], GPIO_MODE_DEF_OUTPUT) ;
}
gpio_set_level(RS_IO, 1);
gpio_set_level(WR_IO, 0);
for (uint8_t i = 0; i < 8; i++)
{
if (data & BIT(i))
gpio_set_level(Data_Pin_IO[i], 1);
else
gpio_set_level(Data_Pin_IO[i], 0);
}
gpio_set_level(WR_IO, 1);
gpio_set_level(RS_IO, 1);
gpio_set_level(CS_IO, 1);
for (uint8_t i = 0; i < 8; i++)
{
gpio_set_level(Data_Pin_IO[i], 0);
}
}
uint8_t Read_CMD(uint8_t cmd)
{
uint8_t ret_value = 0;
gpio_set_level(CS_IO, 0);
for (uint8_t i = 0; i < 8; i++)
{
gpio_set_direction(Data_Pin_IO[i], GPIO_MODE_DEF_OUTPUT) ;
}
gpio_set_level(RS_IO, 0);
gpio_set_level(WR_IO, 0);
for (uint8_t i = 0; i < 8; i++)
{
if (cmd & BIT(i))
gpio_set_level(Data_Pin_IO[i], 1);
else
gpio_set_level(Data_Pin_IO[i], 0);
}
gpio_set_level(WR_IO, 1);
for (uint8_t i = 0; i < 8; i++)
{
gpio_set_direction(Data_Pin_IO[i], GPIO_MODE_DEF_INPUT) ;
}
gpio_set_level(RS_IO, 1);
gpio_set_level(RD_IO, 0);
gpio_set_level(RD_IO, 0);
gpio_set_level(RD_IO, 1);
for (uint8_t i = 0; i < 8; i++)
{
if (gpio_get_level(Data_Pin_IO[i]))
ret_value |= BIT(i);
}
gpio_set_level(RS_IO, 1);
gpio_set_level(CS_IO, 1);
for (uint8_t i = 0; i < 8; i++)
{
gpio_set_direction(Data_Pin_IO[i], GPIO_MODE_DEF_OUTPUT) ;
}
for (uint8_t i = 0; i < 8; i++)
{
gpio_set_level(Data_Pin_IO[i], 0);
}
return ret_value;
}
void app_main(void)
{
uint8_t value;
DM_CTRL_IO_Init();
DM_DATA_IO_InOut(GPIO_MODE_INPUT_OUTPUT);
gpio_set_level(RESET_IO, 0);
vTaskDelay(10 / portTICK_RATE_MS);
gpio_set_level(RESET_IO, 1);
vTaskDelay(100 / portTICK_RATE_MS);
Write_CMD(0xF9);
Write_CMD(0x36); Write_DATA(0x40);
Write_CMD(0x11);
vTaskDelay(150 / portTICK_RATE_MS);
while(1) {
value = Read_CMD(0x36);
ESP_LOGI(TAG, "Read CMD 0x36 = 0x%02X", value);
value = Read_CMD(0xDA);
ESP_LOGI(TAG, "Read CMD 0xDA = 0x%02X", value);
value = Read_CMD(0xDB);
ESP_LOGI(TAG, "Read CMD 0xDB = 0x%02X", value);
vTaskDelay(1000 / portTICK_RATE_MS);
}
}