Last active
November 8, 2018 21:38
-
-
Save cobrce/221a50ea80d52c9b8598d5d06a26c75f to your computer and use it in GitHub Desktop.
Upload a hex file to RASPBERRY PI via FTP then flash it into AVR microcontroller with AVRDUDE (python 3.6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ftplib import FTP | |
from os import path | |
import spur # install it with PIP | |
address = "ip_address_of_raspberry_pi" | |
user_name = "user_name" | |
password = "password" | |
hex_file = r"absolute/relative_path_to_hex_file" | |
remote_hex_file = path.basename(hex_file) | |
eeprom_file = r"relative_path_to_eeprom_file" | |
remote_eeprom_file = path.basename(eeprom_file) | |
mcpu = "micro_controller" # example : atmega32 | |
class MyFtp(FTP): | |
def __init__(self, host='', user='', passwd=''): | |
super().__init__(host=host, user=user, passwd=passwd) | |
def send_file(self,path,remote_name): | |
with open(path,"rb") as file: | |
return "successfully" in super().storbinary(f"STOR {remote_name}",file) | |
def program_over_ssh(): | |
shell = spur.SshShell(address,user_name,password,missing_host_key=spur.ssh.MissingHostKey.accept) | |
result = shell.run(f"avrdude -p {mcpu} -c linuxgpio -U flash:w:{remote_hex_file}:i -U eeprom:w:{remote_eeprom_file}:i".split(" ")) | |
print(result.output.decode('ascii')) | |
print(result.stderr_output.decode('ascii')) | |
input() | |
def main(): | |
try : | |
session = MyFtp(address,user_name,password) | |
if session.send_file(hex_file,remote_hex_file) and session.send_file(eeprom_file,remote_eeprom_file): | |
print("Files sent, open ssh session") | |
program_over_ssh() | |
else: | |
print("Files not sent, programming aborted") | |
except Exception as e: | |
print("Exception occured") | |
repr(e) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment