libavr
stdint.h
typedef int8_t; typedef int16_t; typedef int32_t; typedef int64_t; typedef uint8_t; typedef uint16_t; typedef uint32_t; typedef uint64_t; typedef intptr_t; typedef uintptr_t;
[u]intN_t
are types that are exactly N bits wide.
intptr_t
and uintptr_t
are integer types
suitable for storing pointers.
libavr/baud.h
Defines a macro BAUD
to help with USART setup - relies on
F_HZ
being defined. The macro is designed to be used as
follows:
#include <libavr/device.h> #include <libavr/baud.h> /* ... */ UBRR0H = 0; UBRR0L = BAUD(57600); /* set baud rate to 57600 */ UCSR0A = 1<<U2X0; /* 2X mode needed for BAUD() */ UCSR0B = 1<<RXE0 | 1<<TXE0; /* enable TX and RX */ UCSR0C = 1<<UCSZ01 | 1<<UCSZ00; /* use 8 databits */ /* ... */
libavr/bits.h
Defines several macros to help with bit manipulation:
sbi(var, bit)
- set specified bit in var (is defined as
)var |= 1<<bit
cbi(var, bit)
- clear specified bit in vartbi(var, bit)
- toggle specified bit in varbit_is_set(expr, bit)
- returns TRUE if specified bit in expr is setbit_is_clear(expr, bit)
- returns TRUE if specified bit in expr is clear
Several helper macros are also defined:
SBI
,
CBI
,
TBI
,
BIT_IS_SET
and
BIT_IS_CLEAR
.
These macros are designed to be used as follows:
#include <libavr/device.h> #include <libavr/bits.h> #define PORT_LED_RED B #define PORT_LED_GREEN B #define PORT_BUTTON C #define BIT_LED_RED 0 #define BIT_LED_GREEN 1 #define BIT_BUTTON 0 /* ... */ SBI(DDR, LED_RED); /* sbi(DDRB, 0); */ SBI(DDR, LED_GREEN); /* sbi(DDRB, 1); */ CBI(DDR, BUTTON); /* cbi(DDRC, 0); */ if (BIT_IS_SET(PIN, BUTTON)) /* if (bit_is_set(PINC, 0)) */ SBI(PORT, LED_RED); /* sbi(PORTB, 0); */ else CBI(PORT, LED_RED); /* cbi(PORTB, 0); */ /* ... */
libavr/delay.h
Defines three macros to perform busy loops that take exactly the specified amount of time:
delay_cycles(x)
- takes x cycles to executedelay_us(x)
- takes x μs to executedelay_ms(x)
- takes x ms to execute
delay_us
and delay_ms
rely on
F_HZ
being defined.
libavr/device.h
Defines every register, bit, interrupt vector in the AVR datasheet.
The -mmcu
compiler option is used to select which MCU to
define.
Several additional constants are defined:
SPM_PAGESIZE
- the size of a flash pageRAMEND
- the last address of RAMEEPROMEND
- the last address of EEPROMFLASHEND
- the last address of FLASH
For MCUs with only one USART (for example: the mega8),
all the registers/bit/interrupt vectors for USART0 are also defined.
i.e. UDR0
is defined, as well as UDR
.
libavr/eeprom.h
#define EEPROM typedef eaddr_t; typedef EEPROM int8_t eeprom_int8_t; typedef EEPROM int16_t eeprom_int16_t; typedef EEPROM int32_t eeprom_int32_t; typedef EEPROM uint8_t eeprom_uint8_t; typedef EEPROM uint16_t eeprom_uint16_t; typedef EEPROM uint32_t eeprom_uint32_t; uint8_t eeprom_read(eaddr_t a); uint16_t eeprom_read16(eaddr_t a); uint32_t eeprom_read32(eaddr_t a); eeprom_write(eaddr_t a, uint8_t v); eeprom_write16(eaddr_t a, uint16_t v); eeprom_write32(eaddr_t a, uint32_t v);
eaddr_t
is the correct type for an address of an item
stored in eeprom. Items in eeprom only need to be byte aligned.
The annotation EEPROM
can be added to any
declaration, causing the variable to be placed in flash memory.
eeprom_[u]int8_t
,
eeprom_[u]int16_t
and
eeprom_[u]int32_t
are the recommended types for placing
integers into eeprom memory.
eeprom_read(a)
,
eeprom_read16(a)
and
eeprom_read32(a)
are used to read an 8 (or 16, or 32) bit
unsigned integer from eeprom address a.
eeprom_write(a, v)
,
eeprom_write16(a, v)
and
eeprom_write32(a, v)
are used to write an 8 (or 16, or 32) bit
unsigned integer (v) to eeprom address a.
libavr/flash.h
#define FLASH typedef faddr_t; typedef const FLASH int8_t flash_int8_t; typedef const FLASH int16_t flash_int16_t; typedef const FLASH int32_t flash_int32_t; typedef const FLASH uint8_t flash_uint8_t; typedef const FLASH uint16_t flash_uint16_t; typedef const FLASH uint32_t flash_uint32_t; uint8_t flash_read(faddr_t a); uint16_t flash_read16(faddr_t a); uint32_t flash_read32(faddr_t a); #define PSTR(s)
faddr_t
is the correct type for an address of an item
stored in flash. Items in flash only need to be byte aligned.
The annotation const FLASH
can be added to any
declaration, causing the variable to be placed in flash memory.
flash_[u]int8_t
,
flash_[u]int16_t
and
flash_[u]int32_t
are the recommended types for placing
integers into flash memory.
flash_read(a)
,
flash_read16(a)
and
flash_read32(a)
are used to read an 8 (or 16, or 32) bit
unsigned integer from flash address a.
PSTR(s)
can be used to place a literal string into flash
memory. The value of the PSTR
macro is the address of the
string.
libavr/interrupt.h
Defines several macros that compile to single assembler instructions:
cli()
,
sei()
and
reti()
.
Defines several macros used to define interrupt/signal handlers:
SIGNAL(v)
,
SIGNAL_NONBLOCK(v)
and
SIGNAL_NAKED(v)
. SIGNAL
is intended to be
used as follows:
#include <libavr/device.h> #include <libavr/interrupt.h> /* ... */ SIGNAL(USART0_TX) { CBI(LED_TX); /* switch off TX led as the transmission is complete */ } /* ... */
SIGNAL_NONBLOCK
is used to add an sei
instruction as the first thing the handler does.
libavr/watchdog.h
#define wdr() #define watchdog_reset() wdr() void watchdog_disable(void); void watchdog_enable(int t); #define WDT_16MS #define WDT_32MS #define WDT_64MS #define WDT_125MS #define WDT_250MS #define WDT_500MS #define WDT_1S #define WDT_2S #define WDT_4S #define WDT_8S
watchdog_reset()
and wdr()
compile to a single
instruction, resetting the watchdog timer.wdr
watchdog_disable()
is used to disable the watchdog timer.
watchdog_enable(t)
is used to enable the watchdog timer, at a
specific speed, the WDT_*
constants are used to specify the speed.
Defines
F_CPU
The delay_us
,
delay_ms
and
BAUD
macros rely on F_CPU
being defined as the MCU clock rate.
For example, F_HZ
should be defined as
8000000UL
for an 8MHz MCU.
NOINTERRUPTS
If interrupts are not used, define NOINTERRUPTS
. This
will speed up many of the eeprom and watchdog operations.
__AVR_ATmega164P__
libavr/device.h uses several defines like __AVR_ATmega164P__
to choose which files in libavr/device/ to include. These macros are
usually defined by the -mmcu
option to gcc.
Copyright
© 2008, 2009 Matthew Roberts <licence@mattroberts.org>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
AVR
is a trademark of Atmel Corporation.
All other marks, trade names and/or trade dress are the trademarks of
their respective owners.