It is a sample program that bridges between ESP32(UART) and PC(USB).
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 00002 #include "mbed.h" 00003 #include "USBSerial.h" 00004 00005 USBSerial pc(0x1f00, 0x2012, 0x0001, false, 20480); // connect_blocking = false, buf_size = 20480 00006 RawSerial wifi_serial(P7_1, P0_1); 00007 00008 InterruptIn button0(USER_BUTTON0); 00009 InterruptIn button1(USER_BUTTON1); 00010 00011 DigitalOut wifi_en(P5_3); 00012 DigitalOut wifi_io0(P3_14); 00013 00014 static uint8_t usb_send_buff[64]; 00015 00016 static void settings_change(int baud, int bits, int parity, int stop) { 00017 wifi_serial.baud(baud); 00018 wifi_serial.format(bits, (SerialBase::Parity)parity, (stop == 0) ? 1 : stop); 00019 } 00020 00021 static void button0_fall(void) { 00022 wifi_en = 0; 00023 } 00024 00025 static void button0_rise(void) { 00026 wifi_en = 1; 00027 } 00028 00029 static void button1_fall(void) { 00030 wifi_io0 = 0; 00031 } 00032 00033 static void button1_rise(void) { 00034 wifi_io0 = 1; 00035 } 00036 00037 int main() { 00038 pc.attach(&settings_change); 00039 00040 // check ESP_IO0 00041 if (button1 == 0) { 00042 wifi_io0 = 0; 00043 } else { 00044 wifi_io0 = 1; 00045 } 00046 00047 // button setting 00048 button0.fall(&button0_fall); 00049 button0.rise(&button0_rise); 00050 button1.fall(&button1_fall); 00051 button1.rise(&button1_rise); 00052 00053 // reset 00054 wifi_en = 0; 00055 Thread::wait(100); 00056 wifi_en = 1; 00057 00058 while (1) { 00059 if (wifi_serial.readable()) { 00060 int idx = 0; 00061 usb_send_buff[idx++] = wifi_serial.getc(); 00062 while (wifi_serial.readable() && (idx < 64)) { 00063 usb_send_buff[idx++] = wifi_serial.getc(); 00064 } 00065 pc.writeBlock(usb_send_buff, idx); 00066 } 00067 if (pc.readable()) { 00068 wifi_serial.putc(pc._getc()); 00069 } 00070 } 00071 }
Generated on Thu Jul 21 2022 06:53:14 by
1.7.2