Assembly code for ATtiny416

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

It's been a few years since I've designed a project using an AVR.  Back then I had Studio 6.  I've got Studio 7 now, and it seems a lot more complicated.  I notice the peripheral registers are structures now.  I'm writing this firmware in assembly language. Can someone point me to some example assembly language startup code for an ATtiny416 or ATtiny816 or something similar?  I've searched for awhile without any luck. I've attached the sort of file I'm looking for.

Attachment(s): 

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

Well, I guess for starters you will need the include file with the register names as reference. In my system, they are at C:\Program Files (x86)\Atmel\Studio\7.0\packs\atmel\ATtiny_DFP\1.4.283\avrasm\inc

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

I notice the peripheral registers are structures now.

Theoretically, there are still non-structure based definitions in both the .h files (afetr the "#endif /* !defined (__ASSEMBLER__) */" line), and in the .inc file that is still present in the Pack files for use with the Atmel assembler.

 

I bit of the code you attached would look like this, approximately:

Main:;; ldi     Temp1, 0x3F ;SP initialization not needed
     ;; out     SPH, Temp1
     ;; ldi     Temp1, 0xFF
     ;; out     SPL, Temp1

        ldi     Temp1,0b00010000 ;port A pin directions
        sts     PORTA_DIR,Temp1
        ldi     Temp1,0b00110001 ;port B pin directions
        sts     PORTB_DIR,Temp1
        ldi     Temp1,0b00000011 ;port C pin directions
        sts     PORTC_DIR,Temp1

        ldi     Temp1,PORT_PULLUPEN_bm
        sts     PORTA_PIN0CTRL,Temp1    ;enable pullups on A 0, 1, 7
        sts     PORTA_PIN1CTRL,Temp1
        sts     PORTA_PIN7CTRL,Temp1

        sts     PORTB_PIN1CTRL,Temp1 ;pullup of PB1

        sts     PORTC_PIN3CTRL,Temp1 ;pullups on PC2,3
        sts     PORTC_PIN2CTRL,Temp1

note in particular the use of "sts" instead of "out" - pretty much all of the IO SFRs are out-of-range for IN/OUT/etc instructions.  (You still have VPORT for looking at pins, but that's about it.)

Also note that IN, OUT, and pullup control are now separate registers instead of being lumped into PORT.

 

Edited: change incorrect "std" instructions to "sts"

 

Last Edited: Sat. Feb 22, 2020 - 09:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks a million.  This helps a lot.

 

Doug

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

Douglas Holub wrote:
Back then I had Studio 6.  I've got Studio 7 now, and it seems a lot more complicated. 
Wh? The only major differences from 6 to 7 (apart form a LOT of bug fixing) was that 7 added ARM support and also an "Arduino importer" but at the core (esp for AVR) 6 and 7 are virtually identical.
Douglas Holub wrote:
I notice the peripheral registers are structures now.
I fear you may be confusing two things here. Remember that AS7 comes with TWO assemblers for AVR. One is the avr-as one from GNU FSF which is part of the whole C/C++ compiler infrastucture. When used to write .S files then it's true that it would use <avr/io.h> but as westfw points out even those .h files are in "two halves" with only one half (just #defines) exposed to the avr-as assembler (which doesn't know what a struct/enum is).

 

But you appear to be using Atmel AVRASM2. That is a completely separate assembler. It has nothing to do with <avr/io.h> or any structs/defines those may contain. The avrasm2 uses XXXdef.inc files for definitions. As El Tangas says "  C:\Program Files (x86)\Atmel\Studio\7.0\packs\atmel\ATtiny_DFP\1.4.283\avrasm\inc " in there you find stuff like:

;*************************************************************************
;** AC0 - Analog Comparator
;*************************************************************************

.equ AC0_CTRLA = 1648                    ; Control A
.equ AC0_MUXCTRLA = 1650                 ; Mux Control A
.equ AC0_INTCTRL = 1654                  ; Interrupt Control
.equ AC0_STATUS = 1655                   ; Status

;*************************************************************************
;** ADC0 - Analog to Digital Converter
;*************************************************************************

.equ ADC0_CTRLA = 1536                   ; Control A
.equ ADC0_CTRLB = 1537                   ; Control B
.equ ADC0_CTRLC = 1538                   ; Control C
.equ ADC0_CTRLD = 1539                   ; Control D
.equ ADC0_CTRLE = 1540                   ; Control E
.equ ADC0_SAMPCTRL = 1541                ; Sample Control
.equ ADC0_MUXPOS = 1542                  ; Positive mux input
.equ ADC0_COMMAND = 1544                 ; Command
.equ ADC0_EVCTRL = 1545                  ; Event Control
.equ ADC0_INTCTRL = 1546                 ; Interrupt Control
.equ ADC0_INTFLAGS = 1547                ; Interrupt Flags
.equ ADC0_DBGCTRL = 1548                 ; Debug Control
.equ ADC0_TEMP = 1549                    ; Temporary Data
.equ ADC0_RES = 1552                     ; ADC Accumulator Result
.equ ADC0_RESL = 1552                    ; ADC Accumulator Result low byte
.equ ADC0_RESH = 1553                    ; ADC Accumulator Result hi byte
.equ ADC0_WINLT = 1554                   ; Window comparator low threshold
.equ ADC0_WINLTL = 1554                  ; Window comparator low threshold low byte
.equ ADC0_WINLTH = 1555                  ; Window comparator low threshold hi byte
.equ ADC0_WINHT = 1556                   ; Window comparator high threshold
.equ ADC0_WINHTL = 1556                  ; Window comparator high threshold low byte
.equ ADC0_WINHTH = 1557                  ; Window comparator high threshold hi byte
.equ ADC0_CALIB = 1558                   ; Calibration

;*************************************************************************
;** BOD - Bod interface
;*************************************************************************

.equ BOD_CTRLA = 128                     ; Control A
.equ BOD_CTRLB = 129                     ; Control B
.equ BOD_VLMCTRLA = 136                  ; Voltage level monitor Control
.equ BOD_INTCTRL = 137                   ; Voltage level monitor interrupt Control
.equ BOD_INTFLAGS = 138                  ; Voltage level monitor interrupt Flags
.equ BOD_STATUS = 139                    ; Voltage level monitor status

which is all written in avrasm2 syntax.

 

It used to be the case that in .asm files for avrasm2 you would use:

.include "tn416def.inc"

or something like that but these days the assembler is invoked with a command line switch to say "build for tiny406" and that does an implied .include for you anyway.

 

But bottom line is that assembler uses XXXdef.inc files with .equ/.def's etc and has nothing to do with structs in io.h files.

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

Shouldn't those be sts instead of std?  My AVR Instruction Set Manual is from 2016.

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

Shouldn't those be sts instead of std?

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

Probably.  Oops.

 

Edit: Definitely!  fixed in the original code.  I don't actually program asm enough to remember "STore with Displacement" vs "STore Direct" (which happens on some other CPUs, maybe?()  When I do program n ASM, the assembler is quick to correct me!

 

Last Edited: Sat. Feb 22, 2020 - 09:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Put this at the top of your file & use it...it will figure things out (IN LDS OUT STS) for you automatically

 example:   INREG  MyDog, ADCSRA   

                 OUTREG  ADMUX, LittleBilly

                 OUTREG  PORTB, UncleElbow

 

;.include "m88padef.inc" ;include predefined ATMEL assignments
; usage: InReg reg, addr 
.macro InReg 
    .if @1 < 0x40 
        in @0, @1 
    .elif ((@1 >= 0x60) && (@1 < SRAM_START)) 
        lds @0,@1 
    .else 
       .error "InReg: Invalid I/O register address" 
    .endif 
.endmacro 

; usage: OutReg addr, reg 
.macro OutReg 
    .if @0 < 0x40 
        out @0, @1 
    .elif ((@0 >= 0x60) && (@0 < SRAM_START)) 
        sts @0,@1 
    .else 
       .error "OutReg: Invalid I/O register address" 
    .endif 
.endmacro

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!