Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Have a directory called AVR in ~
- Have two folders in AVR, Files and Setup
- In Files, have the folder in the name of your AVR uCs, like ATmega328-PU or ATtiny85 (a different folder for each uC)
- In the folder (like ATtiny85) have your .c or .cpp files like Hello_World.cpp or Buzzer.cpp
- In setup make a new folder called AVR_Database,
- Make a new file called current-programmer (no extension) and put this in it:
- (Current) Programmer name
- (Current) Directory where programmer is located
- (Current) Baud rate
- Here is an example of how the file should look like:
- arduino
- /dev/ttyACM0
- 9600
- 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:
- ATtiny85
- ATmega328-PU
- 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:
- Proper name
- avrdude name
- Proper name in lowercase
- Here is what the file would look like:
- ATtiny45
- t45
- attiny45
- Then just for reference make a file called Database_Format, put this in the file:
- AVR Microcontrollers:
- Name
- avrdude_name
- AVR Type
- Programmer:
- (Current) Programmer Type
- (Current) Location
- (Current) Baud rate
- Then have a .sh file called Upload.sh
- put this code in Upload.sh:
- #!/bin/bash
- cd ~
- java Upload_HEX
- Then type this in a new terminal:
- cd AVR; cd Setup; chmod +x Upload.sh
- Then copy Upload.sh, paste in /home/your_username/, and rename the copy of Upload.sh to upload
- then in another terminal type in:
- sudo mv upload /bin/
- Then compile this java file in another new terminal:
- javac Upload_HEX.java
- Then move the Upload_HEX.class file to the ~ directory
- Then when you want to upload a .c or .cpp file to your AVR:
- In a new terminal type in:
- upload
- then select the AVR you will program (with y and n), hit enter,
- then type in the name of your .c or .cpp file (with extension), hit enter,
- and the rest is history!
- Enjoy!
- */
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- public class Upload_HEX {
- public static void main(String[] args) {
- double version = 1.0;
- System.out.println("\nHEX Uploader " + version + "\n");
- System.out.println("Welcome!");
- Scanner scan = new Scanner(System.in);
- String[] avr_chips = getAVRChips();
- String avr = "";
- int length = avr_chips.length;
- for (int i = 0; i < length; i++) {
- System.out.println("Is this the chip you are going to program? (y/n)\n" + avr_chips[i]);
- String response = scan.nextLine();
- if (response.equals("y")) {
- avr = avr_chips[i];
- break;
- } else if (response.equals("n")) {
- System.out.println("Okay...");
- } else if (response.equals("exit")) {
- System.out.println("Exiting...");
- System.exit(1);
- } else {
- System.out.println("\'" + response + "\' is not a verified command!\nContinuing...");
- }
- if (i == (length - 1)) {
- System.err.println("You did not select a chip to program!!!\nGoodbye!");
- System.exit(1);
- }
- }
- String avr_file = avr.toLowerCase();
- System.out.println("Please enter the file name you will upload to your AVR:");
- String file = scan.nextLine();
- String[] avr_chip_info = getAVRChipInfo("/home/developer/AVR/Setup/AVR_Database/" + avr_file);
- String[] programmer_info = getCurrentProgrammerType();
- String[] info = new String[7];
- info[0] = avr_chip_info[0];
- info[1] = avr_chip_info[1];
- info[2] = avr_chip_info[2];
- info[3] = programmer_info[2];
- info[4] = programmer_info[0];
- info[5] = programmer_info[1];
- info[6] = file;
- upload(info);
- System.out.println("HEX Uploader " + version + " is done, thank you!");
- System.out.println("Goodbye!");
- System.exit(1);
- }
- public static String[] getAVRChips() {
- String[] avr_chips = {};
- BufferedReader fiolc = null;
- BufferedReader fio = null;
- String file = "/home/developer/AVR/Setup/AVR_Database/AVR Microcontrollers";
- int lines = 0;
- try {
- String line;
- String line_counter;
- fiolc = new BufferedReader(new FileReader(file));
- while ((line_counter = fiolc.readLine()) != null) {
- lines = lines + 1;
- }
- } catch (Exception exc) {
- System.err.println("Could not find database file!!!\nReason: " + exc);
- } finally {
- try {
- if (fiolc != null) {
- fiolc.close();
- }
- } catch (Exception exc) {
- System.err.println("Could not close file I/O stream from " + file + "!\nReason: " + exc);
- }
- }
- try {
- fio = new BufferedReader(new FileReader(file));
- avr_chips = new String[lines];
- for (int i = 0; i < lines; i++) {
- avr_chips[i] = (fio.readLine());
- }
- } catch (Exception exc) {
- System.err.println("Could not read from database!\nReason: " + exc);
- } finally {
- try {
- if (fio != null) {
- fio.close();
- }
- } catch (Exception exc) {
- System.err.println("Could not close file I/O stream from " + file + "!\nReason: " + exc);
- }
- }
- return avr_chips;
- }
- public static String[] getAVRChipInfo(String filepath) {
- String[] info = new String[3];
- BufferedReader in = null;
- try {
- in = new BufferedReader(new FileReader(filepath));
- for (int i = 0; i < 3; i++) {
- info[i] = (in.readLine());
- }
- } catch (Exception exc) {
- System.err.println("Could not read from file (containing info on AVR uC)!\nReason: " + exc);
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (Exception exc) {
- System.err.println("Could not close file I/O stream from " + filepath + "!\nReason: " + exc);
- }
- }
- return info;
- }
- public static String[] getCurrentProgrammerType() {
- String[] type = new String[3];
- String filepath = "/home/developer/AVR/Setup/AVR_Database/current-programmer";
- BufferedReader fio = null;
- try {
- fio = new BufferedReader(new FileReader(filepath));
- type[0] = (fio.readLine());
- type[1] = (fio.readLine());
- type[2] = (fio.readLine());
- fio.close();
- } catch (Exception exc) {
- System.err.println("Could not read from file (containing info on current programmer)!\nReason: " + exc);
- } finally {
- try {
- if (fio != null) {
- fio.close();
- }
- } catch (Exception exc) {
- System.err.println("Could not close file I/O stream from " + filepath + "!\nReason: " + exc);
- }
- }
- return type;
- }
- public static void upload(String[] info) {
- System.out.println("\nUploading...\n");
- try {
- File source = new File("AVR/Files/" + info[0] + "/" + info[6]);
- if(source.renameTo(new File(info[6]))) {
- System.out.println("Acquired source of \'" + info[6] + "\'...");
- } else {
- System.err.println("Failed to get source of \'" + info[6] + "\'!");
- System.exit(1);
- }
- } catch(Exception exc) {
- System.err.println("Failed to get source of \'" + info[6] + "\'! Reason:\n" + exc);
- System.exit(1);
- }
- String cmd1 = "avr-gcc -mmcu=" + info[2] + " -Wall -Os -o src.elf " + info[6];
- String cmd2 = "avr-objcopy -j .text -j .data -O ihex src.elf src.hex";
- String cmd3 = "sudo avrdude -p " + info[1] + " -c " + info[4] + " -P " + info[5] + " -b " + info[3] + " -U flash:w:src.hex";
- String[] CMD1 = cmd1.split(" ");
- String[] CMD2 = cmd2.split(" ");
- String[] CMD3 = cmd3.split(" ");
- System.out.println("Compiling \'" + info[6] + "\'...");
- executeCommand(CMD1);
- System.out.println("Creating ELF file...\nMaking HEX file...");
- executeCommand(CMD2);
- System.out.println("Uploading HEX file...");
- executeCommand(CMD3);
- try {
- File source = new File(info[6]);
- if(source.renameTo(new File("AVR/Files/" + info[0] + "/" + info[6]))) {
- System.out.println("Moved source \'" + info[6] + "\' back...");
- } else {
- System.err.println("Failed to move source (\'" + info[6] + "\') back!");
- }
- } catch(Exception exc) {
- System.err.println("Failed to move source (\'" + info[6] + "\') back! Reason:\n" + exc);
- }
- File elf = new File("src.elf");
- elf.delete();
- File hex = new File("src.hex");
- hex.delete();
- System.out.println("Did a little house keeping...");
- System.out.println("\nDone!");
- }
- public static void executeCommand(String[] cmd) {
- Process process = null;
- System.out.print("Executing command \'");
- for (int i = 0; i < (cmd.length); i++) {
- if (i == (cmd.length - 1)) {
- System.out.print(cmd[i]);
- } else {
- System.out.print(cmd[i] + " ");
- }
- }
- System.out.print("\'...\n");
- try {
- process = Runtime.getRuntime().exec(cmd);
- BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
- BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
- String line;
- System.out.println("Output: ");
- while ((line = in.readLine()) != null) {
- System.out.println(line);
- }
- System.out.println("Error[s]: ");
- while ((line = err.readLine()) != null) {
- System.out.println(line);
- }
- } catch (Exception exc) {
- System.err.println("An error occurred while executing command! Error:\n" + exc);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement