randomport.com

AVR library

Documentation | Source on GitHub

The design goal of the library was simplicity and not completeness. This means that only functionality I actually required at one time exists. One example of this fact is the SPI module's assumption of 1st edge phase, low clock polarity and 8 bit MSB first transfers. Since I never needed anything else, these SPI options never got parameterized. Another example are the I2C and SPI modules where transfers are not interrupt driven. This is because they're both synchronous protocols and transfer time is predictable and constant. The UART module in contrast is interrupt driven and probably my most developed module reflecting the fact it's the interface I use most.

I've worked with big and small AVRs over the years. Since program space of small devices can quickly become a limiting factor, the library is not designed to be used in the usual way, that is compiled once and then linked against different projects. The library is designed to be configured and compiled for each project individually. To achieve this, each project using it must define two files: hwdefs.h and swdefs.h. Library modules include either none, one or both of these files.

hwdefs.h is used to define the hardware layout of your project (pin mapping) for modules that need one. At minimim hwdefs.h must define DDR and PIN macros like this:

#define DDR(x) (*(&x - 1))
#define PIN(x) (*(&x - 2))

swdefs.h is used to define software options, like which functions to include.

Module Description hwdefs.h swdefs.h
adc ADC peripheral ADC_AVG_SAMP
circbuf8 Circular byte buffer
cmt Cooperative multitasking CMT_NEED_MINSP, CMT_MUTEX_FUNC
i2c I2C peripheral I2C_USE_CMT
i2c_usi I2C over USI peripheral PORT_USI, PORT_USI_SCL, PORT_USI_SDA
ee_24 24Cxxx EEPROM routines (2 byte address)
ee_95 ST95P08 SPI EEPROM routines EE95_CS_PORT, EE95_CS_BIT, EE95_WP_PORT, EE95_WP_BIT
lcd HD44780 high level routines. Requires exactly one low level implementation LCD_WIDTH, LCD_HEIGHT, LCD_USE_FB, LCD_NEED_func
lcd_io HD44780 low level (IO pins) LCD IO pin map
lcd_pcf8574 HD44780 low level (PCF8574) PCF LCD pin map LCD_I2C_SPEED
mfrc522 MFRC522 control (modules avail. on eBay) MFRC522_SS_PORT, MFRC522_SS_BIT
rtc.h RTC routines common header. Requires exactly one rtc implementation.
rtc_mcp79410 RTC impl. with MCP79410
rtc_timer2 RTC impl. with Timer2
serque UART peripheral
spi SPI peripheral SPI_USE_CMT
swi2c Bitbang I2C SCL_PORT, SCL_BIT, SDA_PORT, SDA_BIT SWI2C_RAWADR, SWI2C_REVBITS
swspi Bitbang SPI MOSI_PORT, MOSI_BIT, MISO_PORT, MISO_BIT, SCK_PORT, SCK_BIT
tm1637 Tian Micro 7 segment LED driver (modules avail. on eBay)
time Time routines