|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// Lee Leahy |
| 3 | +// 12 June 2023 |
| 4 | +// |
| 5 | +// Program to reset the ESP32 |
| 6 | +//------------------------------------------------------------------------------ |
| 7 | +// |
| 8 | +// Starting from the SparkFun RTK Surveyor |
| 9 | +// |
| 10 | +// https://www.sparkfun.com/products/18443 |
| 11 | +// |
| 12 | +// Schematic |
| 13 | +// |
| 14 | +// https://cdn.sparkfun.com/assets/c/1/b/d/8/SparkFun_RTK_Surveyor-v13.pdf |
| 15 | +// |
| 16 | +// ___ ___ GPIO-0 PU |
| 17 | +// DTR RTS BOOT EN |
| 18 | +// 0 0 1 1 Run flash code |
| 19 | +// 0 1 0 1 Download code, write to flash |
| 20 | +// 1 0 1 0 Power off |
| 21 | +// 1 1 0 0 Power off |
| 22 | +// |
| 23 | +// To the ESP-WROOM-32 datasheet |
| 24 | +// |
| 25 | +// https://www.espressif.com/sites/default/files/documentation/esp32-wroom-32_datasheet_en.pdf |
| 26 | +// |
| 27 | +// To the ESP-32 Series datasheet |
| 28 | +// |
| 29 | +// https://www.espressif.com/sites/default/files/documentation/esp32_datasheet_en.pdf |
| 30 | +// |
| 31 | +// PU - 0: Power off, 1: Power on/up |
| 32 | +// |
| 33 | +// GPIO-O: Saved at power up, 0 = download image, 1 = boot from flash |
| 34 | +// |
| 35 | +//------------------------------------------------------------------------------ |
| 36 | + |
| 37 | +#define MICROSECONDS 1 |
| 38 | +#define MILLISECONDS (1000 * MICROSECONDS) |
| 39 | +#define POWER_OFF_DELAY (250 * MILLISECONDS) |
| 40 | + |
| 41 | +#include <errno.h> |
| 42 | +#include <fcntl.h> |
| 43 | +#include <stdio.h> |
| 44 | +#include <sys/ioctl.h> |
| 45 | +#include <termios.h> |
| 46 | +#include <unistd.h> |
| 47 | + |
| 48 | +const int dtrPin = TIOCM_DTR; |
| 49 | +const int rtsPin = TIOCM_RTS; |
| 50 | + |
| 51 | +// GPIO-0 PU |
| 52 | +// DTR RTS BOOT EN |
| 53 | +// 1 1 1 1 Run flash code |
| 54 | +// 1 0 0 1 Download code, write to flash |
| 55 | +// 0 1 1 0 Power off |
| 56 | +// 0 0 0 0 Power off |
| 57 | + |
| 58 | +int bootFromFlash(int serialPort) |
| 59 | +{ |
| 60 | + int status; |
| 61 | + |
| 62 | + status = ioctl(serialPort, TIOCMBIS, &rtsPin); |
| 63 | + if (status) |
| 64 | + fprintf(stderr, "ERROR: Failed to select the boot from flash\n"); |
| 65 | + return status; |
| 66 | +} |
| 67 | + |
| 68 | +int downloadImage(int serialPort) |
| 69 | +{ |
| 70 | + int status; |
| 71 | + |
| 72 | + status = ioctl(serialPort, TIOCMBIC, &rtsPin); |
| 73 | + if (status) |
| 74 | + fprintf(stderr, "ERROR: Failed to select download image\n"); |
| 75 | + return status; |
| 76 | +} |
| 77 | + |
| 78 | +int powerOff(int serialPort) |
| 79 | +{ |
| 80 | + int status; |
| 81 | + |
| 82 | + status = ioctl(serialPort, TIOCMBIC, &dtrPin); |
| 83 | + if (status) |
| 84 | + fprintf(stderr, "ERROR: Failed to power off the ESP-32\n"); |
| 85 | + return status; |
| 86 | +} |
| 87 | + |
| 88 | +int powerOn(int serialPort) |
| 89 | +{ |
| 90 | + int status; |
| 91 | + |
| 92 | + status = ioctl(serialPort, TIOCMBIS, &dtrPin); |
| 93 | + if (status) |
| 94 | + fprintf(stderr, "ERROR: Failed to power on the ESP-32\n"); |
| 95 | + return status; |
| 96 | +} |
| 97 | + |
| 98 | +int main (int argc, const char ** argv) |
| 99 | +{ |
| 100 | + struct termios params; |
| 101 | + int serialPort; |
| 102 | + const char * serialPortName; |
| 103 | + int status; |
| 104 | + |
| 105 | + serialPort = -1; |
| 106 | + status = 0; |
| 107 | + do |
| 108 | + { |
| 109 | + // Display the help if necessary |
| 110 | + if (argc != 2) |
| 111 | + { |
| 112 | + status = -1; |
| 113 | + printf("%s serial_port\n", argv[0]); |
| 114 | + break; |
| 115 | + } |
| 116 | + |
| 117 | + // Open the serial port |
| 118 | + serialPortName = argv[1]; |
| 119 | + serialPort = open (serialPortName, O_RDWR); |
| 120 | + if (serialPort < 0) |
| 121 | + { |
| 122 | + status = errno; |
| 123 | + perror ("ERROR: Failed to open the terminal"); |
| 124 | + break; |
| 125 | + } |
| 126 | + |
| 127 | + // Get the terminal attributes |
| 128 | + if (tcgetattr(serialPort, ¶ms) != 0) |
| 129 | + { |
| 130 | + status = errno; |
| 131 | + perror("ERROR: tcgetattr failed!"); |
| 132 | + break; |
| 133 | + } |
| 134 | + |
| 135 | + // Power off the RTK's ESP-32 |
| 136 | + status = powerOff(serialPort); |
| 137 | + if (status) |
| 138 | + break; |
| 139 | + usleep(POWER_OFF_DELAY); |
| 140 | + |
| 141 | + // Select the boot device |
| 142 | + status = bootFromFlash(serialPort); |
| 143 | + if (status) |
| 144 | + break; |
| 145 | + |
| 146 | + // Power on the RTK's ESP-32 |
| 147 | + status = powerOn(serialPort); |
| 148 | + } while (0); |
| 149 | + |
| 150 | + // Close the terminal |
| 151 | + if (serialPort >= 0) |
| 152 | + close(serialPort); |
| 153 | + return status; |
| 154 | +} |
0 commit comments