cassemblyinlineavratmel

impossible constraint in asm while compilation


In my Atmel ASF project, I am trying to build following inline asm code. However I am getting impossible constraint while compilation.

Compiler points to this line __asm__ __volatile__, am I missing anything?

#define OUTPORT PORTD
#define OUTBIT 3      // PD.3   
uint8_t rport ,rbits;
uint8_t *buf = message;
asm volatile(   "in     __tmp_reg__, __SREG__             \n\t" // 1  Save SREG for later
                "cli                                      \n\t" // 1  Clear interrupts
                "in     %[rport], %[port]                 \n\t" // 1  Move PORTB adress to rport

                : //Outputs
                [rport] "=&r" (rport)

                : //Inputs
                [port]    "I" (_SFR_IO_ADDR(OUTPORT))     // Adress to port register, 6-bit positive constant

                : //Clobber list (compiler must restore)

                "r0"                                       // This is __tmp_reg__
);
  1. What is destroying this build?
  2. I wonder if asm syntax is incorrect? I have been following this manual

Solution

  • PORTD, on the ATxmega128A4U, is at address 0x0660, as stated in its datasheet, page 62. The port is therefore not accessible to the in instruction. You should use lds instead, with the constraint

    [port]  "i" (_SFR_MEM_ADDR(OUTPORT))
    

    Notice the lowercase "i".

    Addendum: I just tried compiling the following:

    #include <avr/io.h>
    
    void test(void)
    {
        uint8_t rport;
    
        asm volatile(
            "in __tmp_reg__, __SREG__  \n\t"
            "cli                       \n\t"
            "lds %[rport], %[port]     \n\t"
            : [rport] "=&r" (rport)               // output
            : [port]  "i" (_SFR_MEM_ADDR(PORTD))  // input
            : "r0"                                // clobber
        );
    }
    

    Using avr-gcc 4.9.2 with the options -mmcu=atxmega128a4u -c I get the correct generated code and no warnings, even with -Wall -Wextra.

    The "i" constraints is documented to mean an “immediate integer operand”, whereas "I" means “Constant greater than −1, less than 64”.