Glitch with AVRDUDE when using Arduino as ISP if Terminal is Opened (then closed)

I have ‘kludged’ together the Arduino as ISP code and AVR High-voltage Serial Fuse Reprogrammer for ISP HV Programmer.
I am using a Pro Mini (5v 328P) board so I can talk to it with my 6 pin serial card (the same one I use for my (HV) UPDI programmer.

I have successfully built the hardware on breadboard and can cycle between ISP mode and HV mode with a push button. I am using extra pins on Pro Mini to control HV source and source of reset signal.

I need to open a terminal when in HV mode so I may enter the required programming commands - whether to remove the target reset pin or just reset the fuse values to default.

I need to close the terminal before attempting ISP programming as the same serial port is used for the ‘Arduino as ISP’ programmer.

Both work but there is an annoying boundary condition that’s bugging me.

My unit starts up in ISP mode. If I use it immediately as ‘Arduino as ISP’ it’s fine.

If I switch the unit into HV mode and back into ISP mode, the ‘Arduino as ISP’ is still fine.

But if I temporarily start the PlatformIO terminal on the same port and then shut this down (even if I never switch the unit into HV mode), the first time I try to use it as ‘Arduino as ISP’, I get issues with AVRDUDE synchronising. It works subsequent times.

I have included the verbose AVRDUDE output from the initial attempt.

Sure I could just live with the initial glitch after closing the terminal - or have the unit start up in one mode or the other but I’d like to correct the glitch.

Do you have any ideas what I need to do to prevent this first time glitch?

I was wondering if there is some background serial message I could transmit to get the port into the right state so the initial AVRDUDE interaction does not glitch.I have code to block my ‘Arduino as ISP’ to ‘HV mode’ command push button while the unit is mid ISP session or HV fuse blowing session. I could use this to gate any background serial message I might send to the serial port just while waiting for an AVRDUDE ISP program command to start.

The AVRDUDE command line options don’t look promising.

My problem is confounded by my lack of knowledge - I am no skilled programmer.

If you can think of a way of making my serial port ‘go back to default’ after the terminal has been closed, I’d be very keen to try it.

Here’s the AVRDUDE output from the code trying to use ‘Arduino as ISP’ showing it falling over at the synch stage

Configuring upload protocol...
AVAILABLE: custom
CURRENT: upload_protocol = custom
Uploading .pio\build\attiny13a\firmware.hex

avrdude: Version 6.3, compiled on Sep 12 2016 at 17:24:16
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "C:\Users\Peter\.platformio\packages/tool-avrdude/avrdude.conf"

         Using Port                    : COM16
         Using Programmer              : stk500v1
         Overriding Baud Rate          : 19200
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x15
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x15
         AVR Part                      : ATtiny13a
         Chip Erase delay              : 4000 us
         PAGEL                         : P00
         BS2                           : P00
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65     5     4    0 no         64    4      0  4000  4000 0xff 0xff
           flash         65     6    32    0 yes      1024   32     32  4500  4500 0xff 0xff
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00
           lock           0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           calibration    0     0     0    0 no          2    0      0     0     0 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  4500  4500 0x00 0x00

         Programmer Type : STK500
         Description     : Atmel STK500 Version 1.x firmware

avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x14

avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x02

avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x10
         Hardware Version: 4728160
         Firmware Version: 0.4598883
         Topcard         : STK502
         Vtarget         : 1.8 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: stk500_initialize(): (b) protocol error, expect=0x10, resp=0x01
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.

avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x10

avrdude done.  Thank you.

*** [upload] Error 1
============================================================================================================= [FAILED] Took 4.04 seconds =============================================================================================================

The platformio.ini on the code trying to use the Arduino as ISP

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:attiny13a]
platform = atmelavr
board = attiny13a
framework = arduino

; device is Arduino as ISP
upload_protocol = custom
upload_port = COM16
upload_speed = 19200
upload_flags =
    -C
    ; use "tool-avrdude-megaavr" for the atmelmegaavr platform
    ${platformio.packages_dir}/tool-avrdude/avrdude.conf
    -p
    $BOARD_MCU
    -P
    $UPLOAD_PORT
    -b
    $UPLOAD_SPEED
    -c
    stk500v1
    -v
upload_command = avrdude $UPLOAD_FLAGS -U flash:w:$SOURCE:i

Issue Resolved.

My 6 pin USB to Serial card has a DTR signal. (This is used to good effect on the HV UPDI programmer to give a 12v pulse to the UPDI pin to kick it into HV mode - but it is not applicable here with this ISP programmer).

In this ISP configuration, the DTR signal is dropping very briefly the first time a connected session sends an AVRDUDE command and my Pro Mini does a reset. Doh! It must miss a bunch of the AVRDUDE traffic during this reset.

When it gets going, it send some responses but they are mis formed and AVRDUDE throws an error and aborts.

If a subsequent AVRDUDE command is sent, DTR does not drop so there is no reset and the AVRDUDE interaction happens successfully.

My solution was to slug the reset pin on the Pro Mini with a large value electrolytic capacitor (33uF) so the very brief drop in DTR would not have time to bring the reset pin low enough to trigger a reset.

I also found I needed a similar sized capacitor on the Vcc supply to the target devices for decoupling to guard against incorrect fuse readings. It can’t be too big (mine is 33uF) or the specified Vcc power on time for HV programming will not be met. Life is a compromise. (BTW - I do a lot of ‘select on test’).

But now I need to press the reset button on my Pro Mini briefly to reprogram this - maybe that’s a good thing to stop it accidentally being re-programmed as something else.

But time now to join the Christmas celebrations…