-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart_bridge.c
141 lines (122 loc) · 3.14 KB
/
uart_bridge.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <stdlib.h>
#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/stdio.h"
#include "hardware/uart.h"
#include "device/usbd_pvt.h"
#define PIN_TXD0 0
#define PIN_RXD0 1
#define PIN_ESP32_EN 13
#define PIN_ESP32_FLASH 14
const char ESP_SYNC[] = {
0xc0, 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, 0xC0
};
uint32_t current_baud_rate;
static void uart0_init(uint32_t baud_rate){
current_baud_rate = uart_init(uart0, baud_rate);
uart_set_format(uart0, 8, 1, UART_PARITY_NONE);
uart_set_translate_crlf(uart0, false);
}
static void uart0_deinit(){
uart_deinit(uart0);
current_baud_rate = 0;
}
static void pin_init(void){
gpio_set_function(PIN_TXD0, GPIO_FUNC_UART);
gpio_set_function(PIN_RXD0, GPIO_FUNC_UART);
gpio_init(PIN_ESP32_FLASH);
gpio_init(PIN_ESP32_EN);
gpio_pull_down(PIN_ESP32_FLASH);
sleep_ms(1);
gpio_pull_up(PIN_ESP32_EN);
sleep_ms(1);
gpio_pull_down(PIN_ESP32_EN);
//This resets ESP32
}
/* From ESPTOOL:
* # issue reset-to-bootloader:
* # RTS = either CH_PD/EN or nRESET (both active low = chip in reset
* # DTR = GPIO0 (active low = boot to flasher)
* #
* # DTR & RTS are active low signals,
* # ie True = pin @ 0V, False = pin @ VCC.
*
* (Note: 2bn LED MCU uses inverter for EN and FLASH pins between RP2040 and ESP32).
*
* gpio_pull_up(PIN_ESP32_FLASH); == bootloader mode
* gpio_pull_up(PIN_ESP32_EN); == chip disable
* gpio_pull_down(PIN_ESP32_EN); == chip enable
*/
/* Sequencing from ESPTOOL (seconds):
* DTR: 0.0000000 False == TINYUSB 0 ?
* RTS: 0.0007728 True == TINYUSB 1
* DTR: 0.1010019 True
* RTS: 0.1040874 False
* DTR: 0.1542244 False
*/
int main(){
stdio_usb_init();
uart0_init(115200);
pin_init();
char read_buf[128];
int32_t read_buf_write = 0;
bool match_found = false;
while(true){
while(uart_is_readable(uart0)){
int32_t buf = uart_getc(uart0);
if(buf >= 0){
putchar_raw(buf);
}
}
stdio_flush();
uart_tx_wait_blocking(uart0);
while(uart_is_writable(uart0)){
int32_t buf;
buf = getchar_timeout_us(0);
if(buf < 0){
break;
}
uart_putc_raw(uart0, buf);
read_buf[read_buf_write] = buf;
read_buf_write++;
if(read_buf_write >= sizeof(read_buf)){
read_buf_write = 0;
}
if(!match_found){
for(int32_t i = 0; i < sizeof(read_buf); i++){
if(read_buf[i] == 0xC0){
int32_t i_orig = i;
int32_t ii = 0;
for(; ii < sizeof(ESP_SYNC); ii++){
if(read_buf[i] != ESP_SYNC[ii]){
break;
}else{
i++;
if(i >= sizeof(read_buf)){
i = 0;
}
}
}
if(ii == sizeof(ESP_SYNC)-1){
//ESP Sync magic found, enter flash mode
gpio_pull_up(PIN_ESP32_FLASH);
gpio_pull_up(PIN_ESP32_EN);
sleep_ms(1);
gpio_pull_down(PIN_ESP32_EN);
match_found = true;
break;
}else{
i = i_orig;
}
}
}
}
}
stdio_flush();
}
}