.TH "avr_sfr_notes" 3avr "Fri Jan 1 2021" "Version 2.0.0" "avr-libc" \" -*- nroff -*- .ad l .nh .SH NAME avr_sfr_notes \- Additional notes from .SH SYNOPSIS .br .PP .SH "Detailed Description" .PP The \fC<\fBavr/sfr_defs\&.h\fP>\fP file is included by all of the \fC\fP files, which use macros defined here to make the special function register definitions look like C variables or simple constants, depending on the \fC_SFR_ASM_COMPAT\fP define\&. Some examples from \fC\fP to show how to define such macros: .PP .PP .nf #define PORTA _SFR_IO8(0x02) #define EEAR _SFR_IO16(0x21) #define UDR0 _SFR_MEM8(0xC6) #define TCNT3 _SFR_MEM16(0x94) #define CANIDT _SFR_MEM32(0xF0) .fi .PP .PP If \fC_SFR_ASM_COMPAT\fP is not defined, C programs can use names like \fCPORTA\fP directly in C expressions (also on the left side of assignment operators) and GCC will do the right thing (use short I/O instructions if possible)\&. The \fC__SFR_OFFSET\fP definition is not used in any way in this case\&. .PP Define \fC_SFR_ASM_COMPAT\fP as 1 to make these names work as simple constants (addresses of the I/O registers)\&. This is necessary when included in preprocessed assembler (*\&.S) source files, so it is done automatically if \fC__ASSEMBLER__\fP is defined\&. By default, all addresses are defined as if they were memory addresses (used in \fClds/sts\fP instructions)\&. To use these addresses in \fCin/out\fP instructions, you must subtract 0x20 from them\&. .PP For more backwards compatibility, insert the following at the start of your old assembler source file: .PP .PP .nf #define __SFR_OFFSET 0 .fi .PP .PP This automatically subtracts 0x20 from I/O space addresses, but it's a hack, so it is recommended to change your source: wrap such addresses in macros defined here, as shown below\&. After this is done, the \fC__SFR_OFFSET\fP definition is no longer necessary and can be removed\&. .PP Real example - this code could be used in a boot loader that is portable between devices with \fCSPMCR\fP at different addresses\&. .PP .PP .nf : #define SPMCR _SFR_IO8(0x37) : #define SPMCR _SFR_MEM8(0x68) .fi .PP .PP .PP .nf #if _SFR_IO_REG_P(SPMCR) out _SFR_IO_ADDR(SPMCR), r24 #else sts _SFR_MEM_ADDR(SPMCR), r24 #endif .fi .PP .PP You can use the \fCin/out/cbi/sbi/sbic/sbis\fP instructions, without the \fC_SFR_IO_REG_P\fP test, if you know that the register is in the I/O space (as with \fCSREG\fP, for example)\&. If it isn't, the assembler will complain (I/O address out of range 0\&.\&.\&.0x3f), so this should be fairly safe\&. .PP If you do not define \fC__SFR_OFFSET\fP (so it will be 0x20 by default), all special register addresses are defined as memory addresses (so \fCSREG\fP is 0x5f), and (if code size and speed are not important, and you don't like the ugly #if above) you can always use lds/sts to access them\&. But, this will not work if \fC__SFR_OFFSET\fP != 0x20, so use a different macro (defined only if \fC__SFR_OFFSET\fP == 0x20) for safety: .PP .PP .nf sts _SFR_ADDR(SPMCR), r24 .fi .PP .PP In C programs, all 3 combinations of \fC_SFR_ASM_COMPAT\fP and \fC__SFR_OFFSET\fP are supported - the \fC_SFR_ADDR(SPMCR)\fP macro can be used to get the address of the \fCSPMCR\fP register (0x57 or 0x68 depending on device)\&. .SH "Author" .PP Generated automatically by Doxygen for avr-libc from the source code\&.