I am programming my Atmega328P chip via a USB to TTL converter (https://www.amazon.com/gp/product/B075N82CDL/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1)
I am able to upload hex files via the command line and the Arduino IDE when I have the RTS pin of the adapter and a capacitor attached to the !RESET pin of the Atmega. (http://www.electronics-lab.com/wp-content/uploads/2018/09/programming-Uno-on-breadboard.png)
I am also able to upload via the command line when I have a push button attached to the !RESET pin and manually pull it low then begin uploading the firmware within a few moments. (https://cdn.instructables.com/F0L/2F6U/J7AQSHD1/F0L2F6UJ7AQSHD1.LARGE.jpg)
My problem is that for my application I will only have the TX/RX/Vcc/GND pins available (no RTS or push button) and need to reset the micro controller so the bootloader runs and listens to be programmed with updated firmware.
So far I have written a batch file that sends a command to restart the Arduino, and then calls avrdude to upload a hex file. I have tried two methods of restarting the micro controller. I have told it to go to memory location zero, and I have attached an IO pin to the reset and dropped it low (although it stays low until reset, so the reset doesn't really work until I manually remove the wire between the IO pin and the !RESET pin)
My sequence is as follows:
- Have Arduino running code:
int recvByte;
int delayS = 400;
int resetPin = 9; //bttom right pin of uC
void setup() {
// put your setup code here, to run once:
pinMode(txPin, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(resetPin, OUTPUT);
digitalWrite(resetPin, HIGH);
Serial.begin(57600);
Serial.write("Booted/reset");
Serial.write("This setup was uploaded with batch");
}
void(* resetFunc) (void) = 0;
void loop() {
if (Serial.available() )
{ // get incoming byte:
recvByte = Serial.read();
if (recvByte==int('r'))
{
Serial.write("resetting");
delay(10); //wait for last message to send
//digitalWrite(resetPin, LOW); //commented out because this method stays low until wire is removed
resetFunc();
Serial.write("should not see if reset properly");
}
}
delay(100);
}
2.I then run my batch file.
echo Running upload bat, sending reset command then calling avrdude
cd "C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\"
python "path\sendR.py"
.\avrdude.exe -C"C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega328p -carduino -PCOM6 -b57600 -D -Uflash:w:"path\Atmega328code.hex":i
The python sendR.py is to send the "r" character, which the Arduino views as the reset command.
import serial
ser = serial.Serial("COM6", baudrate=57600) # open serial port
bytes = "r".encode()
ser.write(bytes)
ser.close()
print("End python script")
When I send "r" via Tera Term or python the setup code runs again, but I have not been able to upload with the .\avrdude.exe -C"C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega328p -carduino -PCOM6 -b57600 -D -Uflash:w:"path\Atmega328code.hex":i
command inside the batch file. If I use the pushbutton to do a hardware reset, this command works without issue.
How can I upload new firmware via the RX and TX pins using software to reset the microcontroller and listen for new code as the bootloader runs?
New ideas tried:
Use a uniform baud rate of 57600 in python and atmega serial setup. I chose this value because the Arduino IDE output this baudrate in avrdude.exe -C"C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega328p -carduino -PCOM6 -b57600 -D -Uflash:w:"path\Atmega328code.hex":i