upload-reset.c 3.7 KB

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