EEPROM access on ATTiny424 using Assembly problems

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm got a project with an ATTiny424, a UPDI programmer port, and using Microchip Studio in assembly language.   I have searched this site and the internet in general and have found a couple routines, but cannot get any of them to work.   Can somebody point me in the right direction to a write routine that actually works?   I'm only writing 1 byte at a time, to 4 locations (its calibration data for the PCB).   I've been working this issue for a couple weeks with no success.  Here are the current versions of my routines, originally derived from either here or internet, but have been modifying them to try and get them working.   I can write and read - but I fear I am reading the buffer only, because if I reset or power down and just do a read, I get FF every time.   The data is not being saved for some reason.   HELP! 

 

Here is my current read;

 

;***************************************************************************
;* EERead
;* r21:r20    = address of data
;* r16        = data
;* Uses Z register pair
;***************************************************************************
EERead:
    lds        zl, NVMCTRL_STATUS
    sbrc    zl, NVMCTRL_EEBUSY_bp
    rjmp    EERead    
    movw    ZH:ZL, r21:r20    
    ld        r16, Z                    ; read data byte
    ret

 

Here is my current write function;

 

; EEPROM_write function
; On entry:
;    r16:r17 = Adddress of data location
;    r18 - byte to be written to EEPROM
; Clobbers:
;    zl (r30), zh (r31)
; All other registers preserved
EEPROM_write:
    ; Wait for completion of previous write
    lds        zl, NVMCTRL_STATUS
    sbrc    zl, NVMCTRL_EEBUSY_bp
    rjmp    EEPROM_write
;    sts        NVMCTRL_ADDRH, r21
;    sts        NVMCTRL_ADDRL, r20    
    movw    Z, r16                                            ;                         
    st        Z, r18                                ; Store EEPROM data (r18) to write buffer
    nop                                            ; Execute NWM erase/write command
    cli                                            ; make sure interrupts are disabled for critical section
    ldi        zl, CPU_CCP_SPM_gc                    ; unlock NVM command register
    ldi        zh, 0x03                            ; execute NVM erase/write
    out        CPU_CCP, zl
    sts        NVMCTRL_CTRLA, zh            ; 
    ret

 

 

 

This topic has a solution.

Keith

Last Edited: Sat. Jun 25, 2022 - 01:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This (useless) APP Note was the best I could find. But it's a start. (An Atmel Start actually)

AN1983 Writing to Flash and EEPROM on the tinyAVR 1-series

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How are your functions being called?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is my test code....  If I uncomment the 4 lines to perform the write and then read, I get the correct data.   If I comment the write, as shown, and only do a read, I get FF.   So the data is not getting saved.  The BLOCK0_THRESH_ADDR = 0x1410.

 

;    ldi        r16, LOW(BLOCK0_THRESH_ADDR)
;    ldi        r17, HIGH(BLOCK0_THRESH_ADDR)
;    ldi        r18, 0x55
;    rcall    EEPROM_write

    ldi        r20, LOW(BLOCK0_THRESH_ADDR)
    ldi        r21, HIGH(BLOCK0_THRESH_ADDR)    
    rcall    EERead
    
    rcall    disp_value            ; uses Z register!!!
    rjmp    forever

Keith

Last Edited: Sat. Jun 18, 2022 - 07:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've tried many variations in the code, no luck.   Is it possible a fuse setting would prevent use of the EEPROM?   I used Arduino IDE to set the fuses, as I have not come up with the command line arguments for AVRDUDE, which I use to program within Microchip Studio.   Pretty sure, the Arduino IDE only set the clock speed.   I cannot find any reason this should not be working.  I'm stumped.  I have even downloaded and run EEPROM write and read code in Ardiuno C, and still get the same results - the chip is not writing the page buffer to EEPROM.

 

Keith

Keith

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is it possible a fuse setting would prevent use of the EEPROM?

Yes usually the EESAVE fuse if programmed.

 

edit seems to be SYSCFG0.EESAVE for this chip

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Mon. Jun 20, 2022 - 05:16 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This device is well different from "traditional" AVR we know... look at this info from DS:

 

The primary task of the EEPROM memory is to store nonvolatile application data. The EEPROM memory supports
single- and multi-byte read and write.

The EEPROM is divided into a set of pages where one page consists of multiple bytes. The EEPROM has byte
granularity on the erase/write. Within one page, only the bytes marked to be updated will be erased/written. The byte
is marked by writing a new value to the page buffer for that address location.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here3's an old thread that could help: (Ignore that different device)

https://www.avrfreaks.net/forum/eeprom-attiny814

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Finally figured out the problem with my tiny424 and the eeprom writes.   Turns out, it was working the entire time.   Even though I'm using Microchip Studio, I had used the Arduino IDE to program the fuses because you could just click on the options and press program fuse - mistake for a couple reasons;  #1 I'm not a fan of the interface, to say it politely.   It does an incomplete job, especially on the subject of fuse programming a tiny424.   perhaps its only good at Arduino made boards.   #2 Even when you tell it to program the EESAVE bit (to prevent the eeprom from being erased every time you re-program) it does not do it.   I assumed :) ... it was programmed and wasted 2 weeks chasing a bug that was non-existent.

 

Solution was to research the AVRDUDE format for fuse programming for my AVR device and do it myself (which I should have done in the first place), rather than assume the Arduino IDE was doing it correctly.   After I successfully programmed the EESAVE bit in fuse5, the writes retained the data, even after reprogramming.    Success!!

 

 

Keith