Advertisement
Guest User

Upload_HEX

a guest
May 6th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.90 KB | None | 0 0
  1. /*
  2.  
  3. Have a directory called AVR in ~
  4. Have two folders in AVR, Files and Setup
  5.  
  6. In Files, have the folder in the name of your AVR uCs, like ATmega328-PU or ATtiny85 (a different folder for each uC)
  7. In the folder (like ATtiny85) have your .c or .cpp files like Hello_World.cpp or Buzzer.cpp
  8.  
  9. In setup make a new folder called AVR_Database,
  10.  
  11. Make a new file called current-programmer (no extension) and put this in it:
  12.  
  13. (Current) Programmer name
  14. (Current) Directory where programmer is located
  15. (Current) Baud rate
  16.  
  17. Here is an example of how the file should look like:
  18.  
  19. arduino
  20. /dev/ttyACM0
  21. 9600
  22.  
  23. Then make a new file called AVR Microcontrollers and put the (proper names) of all the AVR the micro-controllers you have (each in a new line), here is an example of how the file should look like:
  24.  
  25. ATtiny85
  26. ATmega328-PU
  27.  
  28. Then make a file named in the proper name (like ATtiny45) in lowercase, so it would be named attiny45, then this is how the file should look like:
  29.  
  30. Proper name
  31. avrdude name
  32. Proper name in lowercase
  33.  
  34. Here is what the file would look like:
  35.  
  36. ATtiny45
  37. t45
  38. attiny45
  39.  
  40. Then just for reference make a file called Database_Format, put this in the file:
  41.  
  42. AVR Microcontrollers:
  43.  
  44. Name
  45. avrdude_name
  46. AVR Type
  47.  
  48. Programmer:
  49.  
  50. (Current) Programmer Type
  51. (Current) Location
  52. (Current) Baud rate
  53.  
  54. Then have a .sh file called Upload.sh
  55. put this code in Upload.sh:
  56.  
  57. #!/bin/bash
  58.  
  59. cd ~
  60. java Upload_HEX
  61.  
  62. Then type this in a new terminal:
  63.  
  64. cd AVR; cd Setup; chmod +x Upload.sh
  65.  
  66. Then copy Upload.sh, paste in /home/your_username/, and rename the copy of Upload.sh to upload
  67.  
  68. then in another terminal type in:
  69.  
  70. sudo mv upload /bin/
  71.  
  72. Then compile this java file in another new terminal:
  73.  
  74. javac Upload_HEX.java
  75.  
  76. Then move the Upload_HEX.class file to the ~ directory
  77.  
  78. Then when you want to upload a .c or .cpp file to your AVR:
  79. In a new terminal type in:
  80.  
  81. upload
  82.  
  83. then select the AVR you will program (with y and n), hit enter,
  84. then type in the name of your .c or .cpp file (with extension), hit enter,
  85. and the rest is history!
  86.  
  87. Enjoy!
  88.  
  89. */
  90.  
  91. import java.io.BufferedReader;
  92. import java.io.File;
  93. import java.io.FileReader;
  94. import java.io.InputStreamReader;
  95. import java.util.Scanner;
  96.  
  97. public class Upload_HEX {
  98.  
  99. public static void main(String[] args) {
  100.  
  101. double version = 1.0;
  102. System.out.println("\nHEX Uploader " + version + "\n");
  103. System.out.println("Welcome!");
  104.  
  105. Scanner scan = new Scanner(System.in);
  106. String[] avr_chips = getAVRChips();
  107. String avr = "";
  108. int length = avr_chips.length;
  109.  
  110. for (int i = 0; i < length; i++) {
  111.  
  112. System.out.println("Is this the chip you are going to program? (y/n)\n" + avr_chips[i]);
  113. String response = scan.nextLine();
  114.  
  115. if (response.equals("y")) {
  116.  
  117. avr = avr_chips[i];
  118. break;
  119.  
  120. } else if (response.equals("n")) {
  121.  
  122. System.out.println("Okay...");
  123.  
  124. } else if (response.equals("exit")) {
  125.  
  126. System.out.println("Exiting...");
  127. System.exit(1);
  128.  
  129. } else {
  130.  
  131. System.out.println("\'" + response + "\' is not a verified command!\nContinuing...");
  132.  
  133. }
  134.  
  135. if (i == (length - 1)) {
  136.  
  137. System.err.println("You did not select a chip to program!!!\nGoodbye!");
  138. System.exit(1);
  139.  
  140. }
  141.  
  142. }
  143.  
  144. String avr_file = avr.toLowerCase();
  145. System.out.println("Please enter the file name you will upload to your AVR:");
  146. String file = scan.nextLine();
  147.  
  148. String[] avr_chip_info = getAVRChipInfo("/home/developer/AVR/Setup/AVR_Database/" + avr_file);
  149.  
  150. String[] programmer_info = getCurrentProgrammerType();
  151.  
  152. String[] info = new String[7];
  153. info[0] = avr_chip_info[0];
  154. info[1] = avr_chip_info[1];
  155. info[2] = avr_chip_info[2];
  156. info[3] = programmer_info[2];
  157. info[4] = programmer_info[0];
  158. info[5] = programmer_info[1];
  159. info[6] = file;
  160.  
  161. upload(info);
  162.  
  163. System.out.println("HEX Uploader " + version + " is done, thank you!");
  164. System.out.println("Goodbye!");
  165. System.exit(1);
  166.  
  167. }
  168.  
  169. public static String[] getAVRChips() {
  170.  
  171. String[] avr_chips = {};
  172.  
  173. BufferedReader fiolc = null;
  174. BufferedReader fio = null;
  175. String file = "/home/developer/AVR/Setup/AVR_Database/AVR Microcontrollers";
  176. int lines = 0;
  177.  
  178. try {
  179.  
  180. String line;
  181. String line_counter;
  182.  
  183. fiolc = new BufferedReader(new FileReader(file));
  184.  
  185. while ((line_counter = fiolc.readLine()) != null) {
  186.  
  187. lines = lines + 1;
  188.  
  189. }
  190.  
  191. } catch (Exception exc) {
  192.  
  193. System.err.println("Could not find database file!!!\nReason: " + exc);
  194.  
  195. } finally {
  196.  
  197. try {
  198. if (fiolc != null) {
  199.  
  200. fiolc.close();
  201.  
  202. }
  203.  
  204. } catch (Exception exc) {
  205.  
  206. System.err.println("Could not close file I/O stream from " + file + "!\nReason: " + exc);
  207.  
  208. }
  209.  
  210. }
  211.  
  212. try {
  213.  
  214. fio = new BufferedReader(new FileReader(file));
  215. avr_chips = new String[lines];
  216.  
  217. for (int i = 0; i < lines; i++) {
  218.  
  219. avr_chips[i] = (fio.readLine());
  220.  
  221. }
  222.  
  223. } catch (Exception exc) {
  224.  
  225. System.err.println("Could not read from database!\nReason: " + exc);
  226.  
  227. } finally {
  228.  
  229. try {
  230. if (fio != null) {
  231.  
  232. fio.close();
  233.  
  234. }
  235.  
  236. } catch (Exception exc) {
  237.  
  238. System.err.println("Could not close file I/O stream from " + file + "!\nReason: " + exc);
  239.  
  240. }
  241.  
  242. }
  243.  
  244.  
  245. return avr_chips;
  246.  
  247. }
  248.  
  249. public static String[] getAVRChipInfo(String filepath) {
  250.  
  251. String[] info = new String[3];
  252.  
  253. BufferedReader in = null;
  254.  
  255. try {
  256.  
  257. in = new BufferedReader(new FileReader(filepath));
  258.  
  259. for (int i = 0; i < 3; i++) {
  260.  
  261. info[i] = (in.readLine());
  262.  
  263. }
  264.  
  265. } catch (Exception exc) {
  266.  
  267. System.err.println("Could not read from file (containing info on AVR uC)!\nReason: " + exc);
  268.  
  269. } finally {
  270.  
  271. try {
  272. if (in != null) {
  273.  
  274. in.close();
  275.  
  276. }
  277.  
  278. } catch (Exception exc) {
  279.  
  280. System.err.println("Could not close file I/O stream from " + filepath + "!\nReason: " + exc);
  281.  
  282. }
  283.  
  284. }
  285.  
  286. return info;
  287.  
  288. }
  289.  
  290. public static String[] getCurrentProgrammerType() {
  291.  
  292. String[] type = new String[3];
  293. String filepath = "/home/developer/AVR/Setup/AVR_Database/current-programmer";
  294.  
  295. BufferedReader fio = null;
  296.  
  297. try {
  298.  
  299. fio = new BufferedReader(new FileReader(filepath));
  300. type[0] = (fio.readLine());
  301. type[1] = (fio.readLine());
  302. type[2] = (fio.readLine());
  303. fio.close();
  304.  
  305. } catch (Exception exc) {
  306.  
  307. System.err.println("Could not read from file (containing info on current programmer)!\nReason: " + exc);
  308.  
  309. } finally {
  310.  
  311. try {
  312.  
  313. if (fio != null) {
  314.  
  315. fio.close();
  316.  
  317. }
  318.  
  319. } catch (Exception exc) {
  320.  
  321. System.err.println("Could not close file I/O stream from " + filepath + "!\nReason: " + exc);
  322.  
  323. }
  324.  
  325. }
  326.  
  327. return type;
  328.  
  329. }
  330.  
  331. public static void upload(String[] info) {
  332.  
  333. System.out.println("\nUploading...\n");
  334.  
  335. try {
  336.  
  337. File source = new File("AVR/Files/" + info[0] + "/" + info[6]);
  338.  
  339. if(source.renameTo(new File(info[6]))) {
  340.  
  341. System.out.println("Acquired source of \'" + info[6] + "\'...");
  342.  
  343. } else {
  344.  
  345. System.err.println("Failed to get source of \'" + info[6] + "\'!");
  346. System.exit(1);
  347.  
  348. }
  349.  
  350. } catch(Exception exc) {
  351.  
  352. System.err.println("Failed to get source of \'" + info[6] + "\'! Reason:\n" + exc);
  353. System.exit(1);
  354.  
  355. }
  356.  
  357. String cmd1 = "avr-gcc -mmcu=" + info[2] + " -Wall -Os -o src.elf " + info[6];
  358. String cmd2 = "avr-objcopy -j .text -j .data -O ihex src.elf src.hex";
  359. String cmd3 = "sudo avrdude -p " + info[1] + " -c " + info[4] + " -P " + info[5] + " -b " + info[3] + " -U flash:w:src.hex";
  360.  
  361. String[] CMD1 = cmd1.split(" ");
  362. String[] CMD2 = cmd2.split(" ");
  363. String[] CMD3 = cmd3.split(" ");
  364.  
  365. System.out.println("Compiling \'" + info[6] + "\'...");
  366. executeCommand(CMD1);
  367. System.out.println("Creating ELF file...\nMaking HEX file...");
  368. executeCommand(CMD2);
  369. System.out.println("Uploading HEX file...");
  370. executeCommand(CMD3);
  371.  
  372. try {
  373.  
  374. File source = new File(info[6]);
  375.  
  376. if(source.renameTo(new File("AVR/Files/" + info[0] + "/" + info[6]))) {
  377.  
  378. System.out.println("Moved source \'" + info[6] + "\' back...");
  379.  
  380. } else {
  381.  
  382. System.err.println("Failed to move source (\'" + info[6] + "\') back!");
  383.  
  384. }
  385.  
  386. } catch(Exception exc) {
  387.  
  388. System.err.println("Failed to move source (\'" + info[6] + "\') back! Reason:\n" + exc);
  389.  
  390. }
  391. File elf = new File("src.elf");
  392. elf.delete();
  393. File hex = new File("src.hex");
  394. hex.delete();
  395. System.out.println("Did a little house keeping...");
  396.  
  397. System.out.println("\nDone!");
  398.  
  399. }
  400.  
  401. public static void executeCommand(String[] cmd) {
  402.  
  403. Process process = null;
  404. System.out.print("Executing command \'");
  405.  
  406. for (int i = 0; i < (cmd.length); i++) {
  407.  
  408. if (i == (cmd.length - 1)) {
  409.  
  410. System.out.print(cmd[i]);
  411.  
  412. } else {
  413.  
  414. System.out.print(cmd[i] + " ");
  415.  
  416. }
  417.  
  418. }
  419.  
  420. System.out.print("\'...\n");
  421.  
  422. try {
  423.  
  424. process = Runtime.getRuntime().exec(cmd);
  425. BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
  426. BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  427. String line;
  428.  
  429. System.out.println("Output: ");
  430. while ((line = in.readLine()) != null) {
  431.  
  432. System.out.println(line);
  433.  
  434. }
  435.  
  436. System.out.println("Error[s]: ");
  437. while ((line = err.readLine()) != null) {
  438.  
  439. System.out.println(line);
  440.  
  441. }
  442.  
  443. } catch (Exception exc) {
  444.  
  445. System.err.println("An error occurred while executing command! Error:\n" + exc);
  446.  
  447. }
  448.  
  449. }
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement