My journey with ATTiny4313 (part 6)

Part 6: Debugging

Bugs are everywehere...

Simulating MO44.DRV

During my journey, I used my Atari with Cubase and the driver MO44.DRV. However, since my prototype is not yet recognized by the driver, my testing ability is limited. To overcome this issue, I decided to use another ATTiny to simulate the driver.

I wrote a quick program (simulmo4) and checked the result with Saleae Logic.

Great, exactly what I wanted!

Slowing down the simulation

My simulator was too fast for an easy debugging. So I decided to slow it down in 2 ways:

  1. By setting the CKDIV8 fuse (divide clock by 8) using this link https://www.engbedded.com/fusecalc/
  2. By setting the CLKPR register (clock prescaler) to 00000011 (prescaler division factor = 8, cf. datasheet page 33)
    .equ  PRESCALER, 0x03
       ...
    ; ---------------------------------
    ; Configuration of system clock prescaler
    ; ---------------------------------
       ldi    r16,    (1<<CLKPCE)
       STORE  CLKPR,  r16         ; Permit change
       ldi    r16,    PRESCALER
       STORE  CLKPR,  r16         ; Set prescaler
        
Not a good idea, since I'm not able to communicate anymore with the ATTiny :-(

Fixing a jailed ATTiny

I've added the option -F (force, i.e. continue even if signature is incorrect) to avrdude, but still unable to communicate due to the bad timing. The reading and overwriting of the fuses is also impossible.
$ make fuses
/snap/arduino/85/hardware/tools/avr/bin/avrdude -v \
    -C /snap/arduino/85/hardware/tools/avr/etc/avrdude.conf \
    -pattiny4313 -cstk500v1 -P/dev/ttyACM0 -b19200  -F -D \
    -Uefuse:w:0xff:m \
    -Uhfuse:w:0x9f:m \
    -Ulfuse:w:0x44:m

avrdude: Version 6.3-20190619
     (...)
         Using Port                    : /dev/ttyACM0
         Using Programmer              : stk500v1
         Overriding Baud Rate          : 19200
         AVR Part                      : ATtiny4313
         Chip Erase delay              : 9000 us
         PAGEL                         : PD4
         BS2                           : PD6
         RESET disposition             : possible i/o
         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     6     4    0 no        256    4      0  4000  4500 0xff 0xff
           flash         65     6    32    0 yes      4096   64     64  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  9000  9000 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          2    0      0     0     0 0x00 0x00

         Programmer Type : STK500
         Description     : Atmel STK500 Version 1.x firmware
         Hardware Version: 2
         Firmware Version: 1.18
         Topcard         : Unknown
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x000000 (retrying)
     (...)
avrdude: Yikes!  Invalid device signature.
avrdude: Expected signature for ATtiny4313 is 1E 92 0D
avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: reading input file "0xff"
avrdude: writing efuse (1 bytes):

Writing |                                                    | 0% 0.00s ***failed;  
Writing | ################################################## | 100% 0.11s

avrdude: 1 bytes of efuse written
avrdude: verifying efuse memory against 0xff:
avrdude: load data efuse data from input file 0xff:
avrdude: input file 0xff contains 1 bytes
avrdude: reading on-chip efuse data:

Reading | ################################################## | 100% 0.01s

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
         0x00 != 0xff
avrdude: verification error; content mismatch

avrdude: safemode: lfuse reads as 0
avrdude: safemode: hfuse reads as 0
avrdude: safemode: efuse reads as 0
avrdude: safemode: efuse changed! Was ff, and is now 0
Ok, let's step back a little bit:
  1. The clock speed on the ATTiny has been reduced.
  2. The ATTiny communicates via SPI with the ISP (In-System Programmer, in our case an Arduino Mini with a dedicated app).
  3. Apparently, the SPI clock must not be higher than 1/4 speed on the ATTiny clock.
On avrdude, the option -B allows to setup the clock rate between Master and Target.
  1. Unplug the board and remove the SPI connections between Arduino and ATTiny
  2. Reload the ArduinoISP sketch
  3. Uncomment the line
    #define SPI_CLOCK   (128000/6)  // uncomment this line ...
    //
    // A clock slow enough for an ATtiny85 @ 1 MHz, is a reasonable default:
    
    // #define SPI_CLOCK   (1000000/6)     // ... and comment out this one
        
    In the case of Arduino Mini as programmer, don't forget to swap PIN_MOSI (pin 12, not 11) and PIN_MISO (pin 11, not 12)
  4. Upload the sketch to the Arduino
  5. Connect back the SPI connections between Arduino and ATTiny

Follow up:  Part 1  Part 2  Part 3  Part 4  Part 5  Part 6 .

Comments

Popular Posts