|
| 1 | +/* Copyright (C) 2015 Roger Clark <www.rogerclark.net> |
| 2 | + * |
| 3 | + * This program is free software; you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU General Public License as published by |
| 5 | + * the Free Software Foundation; either version 2 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * |
| 9 | + * Utility to send the reset sequence on RTS and DTR and chars |
| 10 | + * which resets the libmaple and causes the bootloader to be run |
| 11 | + * |
| 12 | + * |
| 13 | + * |
| 14 | + * Terminal control code by Heiko Noordhof (see copyright below) |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +/* Copyright (C) 2003 Heiko Noordhof <heikyAusers.sf.net> |
| 20 | + * |
| 21 | + * This program is free software; you can redistribute it and/or modify |
| 22 | + * it under the terms of the GNU General Public License as published by |
| 23 | + * the Free Software Foundation; either version 2 of the License, or |
| 24 | + * (at your option) any later version. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <stdlib.h> |
| 29 | +#include <termios.h> |
| 30 | +#include <unistd.h> |
| 31 | +#include <sys/types.h> |
| 32 | +#include <sys/stat.h> |
| 33 | +#include <fcntl.h> |
| 34 | +#include <sys/ioctl.h> |
| 35 | +#include <stdbool.h> |
| 36 | + |
| 37 | +/* Function prototypes (belong in a seperate header file) */ |
| 38 | +int openserial(char *devicename); |
| 39 | +void closeserial(void); |
| 40 | +int setDTR(unsigned short level); |
| 41 | +int setRTS(unsigned short level); |
| 42 | + |
| 43 | + |
| 44 | +/* Two globals for use by this module only */ |
| 45 | +static int fd; |
| 46 | +static struct termios oldterminfo; |
| 47 | + |
| 48 | + |
| 49 | +void closeserial(void) |
| 50 | +{ |
| 51 | + tcsetattr(fd, TCSANOW, &oldterminfo); |
| 52 | + close(fd); |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +int openserial(char *devicename) |
| 57 | +{ |
| 58 | + struct termios attr; |
| 59 | + |
| 60 | + if ((fd = open(devicename, O_RDWR)) == -1) return 0; /* Error */ |
| 61 | + atexit(closeserial); |
| 62 | + |
| 63 | + if (tcgetattr(fd, &oldterminfo) == -1) return 0; /* Error */ |
| 64 | + attr = oldterminfo; |
| 65 | + attr.c_cflag |= CRTSCTS | CLOCAL; |
| 66 | + attr.c_oflag = 0; |
| 67 | + if (tcflush(fd, TCIOFLUSH) == -1) return 0; /* Error */ |
| 68 | + if (tcsetattr(fd, TCSANOW, &attr) == -1) return 0; /* Error */ |
| 69 | + |
| 70 | + /* Set the lines to a known state, and */ |
| 71 | + /* finally return non-zero is successful. */ |
| 72 | + return setRTS(0) && setDTR(0); |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +/* For the two functions below: |
| 77 | + * level=0 to set line to LOW |
| 78 | + * level=1 to set line to HIGH |
| 79 | + */ |
| 80 | + |
| 81 | +int setRTS(unsigned short level) |
| 82 | +{ |
| 83 | + int status; |
| 84 | + |
| 85 | + if (ioctl(fd, TIOCMGET, &status) == -1) { |
| 86 | + perror("setRTS(): TIOCMGET"); |
| 87 | + return 0; |
| 88 | + } |
| 89 | + if (level) status |= TIOCM_RTS; |
| 90 | + else status &= ~TIOCM_RTS; |
| 91 | + if (ioctl(fd, TIOCMSET, &status) == -1) { |
| 92 | + perror("setRTS(): TIOCMSET"); |
| 93 | + return 0; |
| 94 | + } |
| 95 | + return 1; |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +int setDTR(unsigned short level) |
| 100 | +{ |
| 101 | + int status; |
| 102 | + |
| 103 | + if (ioctl(fd, TIOCMGET, &status) == -1) { |
| 104 | + perror("setDTR(): TIOCMGET"); |
| 105 | + return 0; |
| 106 | + } |
| 107 | + if (level) status |= TIOCM_DTR; |
| 108 | + else status &= ~TIOCM_DTR; |
| 109 | + if (ioctl(fd, TIOCMSET, &status) == -1) { |
| 110 | + perror("setDTR: TIOCMSET"); |
| 111 | + return 0; |
| 112 | + } |
| 113 | + return 1; |
| 114 | +} |
| 115 | + |
| 116 | +/* This portion of code was written by Roger Clark |
| 117 | + * It was informed by various other pieces of code written by Leaflabs to reset their |
| 118 | + * Maple and Maple mini boards |
| 119 | + */ |
| 120 | + |
| 121 | +main(int argc, char *argv[]) |
| 122 | +{ |
| 123 | + |
| 124 | + if (argc<2 || argc >3) |
| 125 | + { |
| 126 | + printf("Usage upload-reset <serial_device> <Optional_delay_in_milliseconds>\n\r"); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + if (openserial(argv[1])) |
| 131 | + { |
| 132 | + // Send magic sequence of DTR and RTS followed by the magic word "1EAF" |
| 133 | + setRTS(false); |
| 134 | + setDTR(false); |
| 135 | + setDTR(true); |
| 136 | + |
| 137 | + usleep(50000L); |
| 138 | + |
| 139 | + setDTR(false); |
| 140 | + setRTS(true); |
| 141 | + setDTR(true); |
| 142 | + |
| 143 | + usleep(50000L); |
| 144 | + |
| 145 | + setDTR(false); |
| 146 | + |
| 147 | + usleep(50000L); |
| 148 | + |
| 149 | + write(fd,"1EAF",4); |
| 150 | + |
| 151 | + closeserial(); |
| 152 | + if (argc==3) |
| 153 | + { |
| 154 | + usleep(atol(argv[2])*1000L); |
| 155 | + } |
| 156 | + } |
| 157 | + else |
| 158 | + { |
| 159 | + printf("Failed to open serial device.\n\r"); |
| 160 | + } |
| 161 | +} |
0 commit comments