/* * esp8266.c * * Created on: 2025年10月18日 * Author: LHYe200 */ #include "esp8266.h" #include "zf_driver_uart.h" #include "zf_common_fifo.h" #include "zf_driver_gpio.h" #include "zf_driver_delay.h" #include "status_led.h" const uint8 ESP8266_syncPkt[44] = {0x00, 0x08, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x07, 0x12, 0x20, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55}; static uint8 syncPkt_index = 0; static uint8 esp8266_quit_download_judge_buffer[2] = {0}; static uint8 esp8266_quit_download_judge_index = 0; static uint8 esp8266_quit_download_judge_flag = 0; static uint8 esp8266_init_flag = 0; uint8 esp8266_download_passthrough = 0; static uint8 esp8266_download_rst_flag = 0; // static uint16 esp8266_uart_pass_tx_buff_index = 0; // static uint16 esp8266_uart_pass_rx_buff_index = 0; void isr_uart0_rx_interrupt_hook_back(uint8 rev_data); // // fifo_struct esp8266_rx_fifo; // #define EU_BUFFER_SIZE 1024 // uint8 esp8266_rx_buffer[EU_BUFFER_SIZE]; // // fifo_struct esp8266_tx_fifo; // uint8 esp8266_tx_buffer[EU_BUFFER_SIZE]; void ESP8266_Init(void) { uart_init(ESP8266_UART_CHANNEL, ESP8266_UART_BAUDRATE, ESP8266_UART_TX_PIN, ESP8266_UART_RX_PIN); uart_rx_interrupt(ESP8266_UART_CHANNEL, 1); // fifo_init(&esp8266_rx_fifo, FIFO_DATA_8BIT, esp8266_rx_buffer, sizeof(esp8266_rx_buffer)); gpio_init(ESP8266_EN, GPO, 1, GPO_PUSH_PULL); gpio_init(ESP8266_RST, GPO, 1, GPO_PUSH_PULL); gpio_init(ESP8266_IO0, GPO, 1, GPO_PUSH_PULL); gpio_init(ESP8266_IO2, GPO, 1, GPO_PUSH_PULL); gpio_init(ESP8266_IO15, GPO, 0, GPO_PUSH_PULL); esp8266_init_flag = 1; } void ESP8266_send_data(uint8 *data, uint32 len) { if(esp8266_download_passthrough) { return; } uart_write_buffer(ESP8266_UART_CHANNEL, data, len); } void ESP8266_Main_Loop_Change_Mode(void) { if(esp8266_quit_download_judge_flag >= 4) { esp8266_download_passthrough = 0; esp8266_quit_download_judge_flag = 0; esp8266_download_rst_flag = 0; gpio_set_level(ESP8266_IO0, 1); gpio_set_level(ESP8266_IO2, 1); gpio_set_level(ESP8266_IO15, 0); gpio_set_level(ESP8266_EN, 0); // gpio_set_level(ESP8266_RST, 0); system_delay_ms(1); // gpio_set_level(ESP8266_RST, 1); gpio_set_level(ESP8266_EN, 1); OFF_LED(STATUS_LED_9); } if(esp8266_download_rst_flag >=3 && !esp8266_download_passthrough) { system_delay_ms(1); // gpio_set_level(ESP8266_RST, 1); gpio_set_level(ESP8266_EN, 1); esp8266_download_passthrough = 1; esp8266_download_rst_flag = 0; ON_LED(STATUS_LED_2); ON_LED(STATUS_LED_9); } } void ESP8266_Judge_Quit_Download_Mode(uint8 rev_data) { if(esp8266_download_passthrough && !esp8266_quit_download_judge_flag) { if(rev_data == 0xC0 && esp8266_quit_download_judge_buffer[0] != 0xC0) { esp8266_quit_download_judge_index = 0; } esp8266_quit_download_judge_buffer[esp8266_quit_download_judge_index++] = rev_data; if(esp8266_quit_download_judge_index >= 2 && esp8266_quit_download_judge_buffer[1] == 0x04) { esp8266_quit_download_judge_index = 0; esp8266_quit_download_judge_flag = 1; ON_LED(STATUS_LED_6); } } if(esp8266_quit_download_judge_flag == 1 && rev_data == 0xC0) { esp8266_quit_download_judge_flag = 2; ON_LED(STATUS_LED_7); } } void ESP8266_Auto_Download_Uart_Hook(void) { uint8 rev_tmp; while(uart_query_byte(UART_0, &rev_tmp)) { if((rev_tmp == ESP8266_syncPkt[syncPkt_index]) && (esp8266_download_passthrough == 0) && esp8266_init_flag) { Flash_LED(STATUS_LED_3); syncPkt_index++; if(syncPkt_index >= 44) { // if(!esp8266_download_rst_flag) // { gpio_set_level(ESP8266_IO0, 0); gpio_set_level(ESP8266_IO2, 1); gpio_set_level(ESP8266_IO15, 0); // gpio_set_level(ESP8266_RST, 0); gpio_set_level(ESP8266_EN, 0); esp8266_download_rst_flag++; ON_LED(STATUS_LED_1); // } // else // { // gpio_set_level(ESP8266_RST, 1); // esp8266_download_passthrough = 1; // ON_LED(STATUS_LED_2); // } syncPkt_index = 0; } } else { syncPkt_index = 0; } if(esp8266_download_passthrough == 0) { isr_uart0_rx_interrupt_hook_back(rev_tmp); } else { // esp8266_tx_buffer[esp8266_uart_pass_tx_buff_index++] = rev_tmp; // if(esp8266_uart_pass_tx_buff_index >= EU_BUFFER_SIZE) // { // uart_write_buffer(ESP8266_UART_CHANNEL, esp8266_tx_buffer, esp8266_uart_pass_tx_buff_index); // esp8266_uart_pass_tx_buff_index = 0; // } // if(rev_tmp == 0xC0) // { // uart_write_buffer(ESP8266_UART_CHANNEL, esp8266_tx_buffer, esp8266_uart_pass_tx_buff_index); // esp8266_uart_pass_tx_buff_index = 0; // } uart_write_byte(ESP8266_UART_CHANNEL, rev_tmp); // ON_LED(STATUS_LED_3); // ESP8266_Judge_Quit_Download_Mode(rev_tmp); } } } void ESP8266_Uart_Callback(void) { uint8 rev_tmp; // ON_LED(STATUS_LED_4); while(uart_query_byte(ESP8266_UART_CHANNEL, &rev_tmp)) { if(esp8266_download_passthrough) { // esp8266_rx_buffer[esp8266_uart_pass_rx_buff_index++] = rev_tmp; // if(esp8266_uart_pass_rx_buff_index >= EU_BUFFER_SIZE) // { // uart_write_buffer(UART_0, esp8266_rx_buffer, esp8266_uart_pass_rx_buff_index); // esp8266_uart_pass_rx_buff_index = 0; // } // if(rev_tmp == 0xC0) // { // uart_write_buffer(UART_0, esp8266_rx_buffer, esp8266_uart_pass_rx_buff_index); // esp8266_uart_pass_rx_buff_index = 0; // } uart_write_byte(UART_0, rev_tmp); // Flash_LED(STATUS_LED_5); if(esp8266_quit_download_judge_flag >= 2) { if(rev_tmp == 0xC0) { ON_LED(STATUS_LED_8); esp8266_quit_download_judge_flag ++; } } } else { // 处理其他数据 isr_uart0_rx_interrupt_hook_back(rev_tmp); } } }