avr_sfr(3)

πŸ“¦ version 2.2.1, in fedora 43, fedora 44, opensuse-leap 16.0

πŸ’– avr_sfr πŸ’–

okay bestie, welcome to avr_sfr β€” this is the page from avr-libc that's literally just the group chat for <avr/sfr_defs.h>. sfr = special function register, and no, that's not a personality trait, it's memory-mapped IO. let's go ✨


🌸 SYNOPSIS 🌸

πŸŽ€ Modules

  • Additional notes from <avr/sfr_defs.h>

πŸ¦‹ Bit manipulation

#define _BV(bit)   (1 << (bit))

πŸ’… IO register bit manipulation

#define bit_is_set(sfr,  bit)   (_SFR_BYTE(sfr) & _BV(bit))
#define bit_is_clear(sfr,  bit)   (!(_SFR_BYTE(sfr) & _BV(bit)))
#define loop_until_bit_is_set(sfr,  bit)   do { } while
    (bit_is_clear(sfr, bit))
#define loop_until_bit_is_clear(sfr,  bit)   do { } while
    (bit_is_set(sfr, bit))

✨ Detailed Description ✨

so when you're out here working with microcontrollers, most of your tasks are honestly just you controlling internal peripherals, or external peripherals that are wired up to the device. and the entire IO address space is handed to you as memory-mapped IO, i.e. you can get at it using all the MCU instructions that work on normal data memory. slay.

for most AVR devices, the IO register space is mapped into the data memory address space with an offset of 0x20, because the bottom of that space is reserved for direct access to the MCU registers. (and the actual SRAM? it only lives behind the IO register area, starting at some specific address depending on the device. iykyk.)

so for example, you can access memory-mapped IO registers as if they were globally defined variables, like this:

PORTA = 0x33;
unsigned char foo = PINA;

the compiler will pick the correct instruction sequence to generate based on the address of the register being accessed. it's giving "i got you" energy fr fr.

the whole advantage of using the memory-mapped registers in C programs is that it makes your programs more portable to other C compilers for the AVR platform. portable = iconic.

⚠️ note: you need to take special care when accessing some of the 16-bit timer IO registers, because access can happen from both the main program and within an interrupt context. see Why do some 16-bit timer registers sometimes get trashed?. do not ignore this one, bestie 😭

🌈 Porting programs that use the deprecated sbi/cbi macros

access to the AVR single bit set and clear instructions is provided via the standard C bit manipulation commands. the sbi and cbi macros are no longer directly supported. sbi (sfr,bit) can be replaced by sfr |= _BV(bit).

i.e.: sbi(PORTB, PB1); is now PORTB |= _BV(PB1);

this is actually more flexible than having sbi directly, because the optimizer will use a hardware sbi if appropriate, or a read/or/write operation if not appropriate. you do not need to keep track of which registers sbi/cbi will operate on. no cap, that's a glow-up.

likewise, cbi (sfr,bit) is now sfr &= ~(_BV(bit));


πŸ‘‘ Macro Definition Documentation πŸ‘‘

🌟 #define _BV(bit) (1 << (bit))

#include <avr/io.h>

converts a bit number into a byte value. that's it, that's the whole job πŸ’…

Note: the bit shift is performed by the compiler, which then inserts the result into the code. so there is no run-time overhead when using _BV(). zero. none. free. 🫢

🌟 #define bit_is_clear(sfr, bit) (!(_SFR_BYTE(sfr) & _BV(bit)))

#include <avr/io.h>

tests whether bit bit in IO register sfr is clear. this will return non-zero if the bit is clear, and a 0 if the bit is set. yes the naming is like that, don't come for me πŸ™ˆ


🌟 #define bit_is_set(sfr, bit) (_SFR_BYTE(sfr) & _BV(bit))

#include <avr/io.h>

tests whether bit bit in IO register sfr is set. this will return a 0 if the bit is clear, and non-zero if the bit is set. symmetry, we love to see it πŸ’–


🌟 #define loop_until_bit_is_clear(sfr, bit) do { } while (bit_is_set(sfr, bit))

#include <avr/io.h>

waits until bit bit in IO register sfr is clear. just vibing in a loop until it happens, lowkey πŸ¦‹


🌟 #define loop_until_bit_is_set(sfr, bit) do { } while (bit_is_clear(sfr, bit))

#include <avr/io.h>

waits until bit bit in IO register sfr is set. same energy, opposite condition, main character loop πŸ”₯


πŸŽ€ Author πŸŽ€

generated automatically by Doxygen for avr-libc from the source code. yeah it's a bot but it ate πŸ’€


and that's the page bestie!! 🌸 this was Version 2.1.0, dated Sat Jan 29 2022, from the avr-libc girlies, and your resident Unix teen is signing off β€” go flash something cute, mwah πŸ’œβœ¨

πŸ“¦ where this page comes from