
hal-avr-mega-spi - AVR ATmega SPI Hardware Abstraction

The hal-avr-mega-spi is a lightweight spi hardware abstraction library for AVR ATmega microcontrollers. It provides a clean interface for spi initialization and communication while hiding direct register-level interaction from higher software layers. The library is intended for projects that want to separate low-level device startup code from application logic and establish a small, reusable system layer for AVR targets.
Features
- SPI master configuration for AVR ATmega devices.
- SPI master communication for AVR ATmega devices.
- Encapsulation of low-level register access.
- Compact and reusable API for embedded projects.
- Foundation for layered HAL architectures.
The hal-avr-mega-spi library reduces coupling by providing a focused interface for essential spi system services. This improves portability inside a project, keeps startup code organized, and makes higher-level modules easier to maintain.
File Structure

hal/
├── common/
| ├── enums/
| | └── SPI_enums.h
| └── macros/
| └── PORT_macros.h
└── avr/
└── spi/
├── spi.c
└── spi.h
Downloads
The library can be downloaded (zip or tar), cloned or used as submodule in a project.
| Type | File | Description |
| Library | zip / tar | AVR spi library |
Using with git clone
mkdir -p ./hal/
git clone https://github.com/0x007E/hal-common.git ./hal
mv ./hal/hal-common ./hal/common
mkdir -p ./hal/avr
git clone https://github.com/0x007E/hal-avr-mega-spi.git ./hal/avr
mv ./hal/avr/hal-avr-mega-spi ./hal/avr/spi
Using as git submodule
git submodule add https://github.com/0x007E/hal-common.git ./hal/common
git submodule add https://github.com/0x007E/hal-avr-mega-spi.git ./hal/avr/spi
Programming
Additional parameters like spi clock speed, prescaler, pin definitions and many more can be setup in the header file. A user friendly description can be found here.
#include "../hal/avr/spi/spi.h"
int main(void)
{
while (1)
{
unsigned char data = 0x00;
}
}
@ SPI_Disable
Definition SPI_enums.h:78
@ SPI_Enable
Definition SPI_enums.h:79
@ SPI_Rising
Definition SPI_enums.h:54
@ SPI_MSB
Definition SPI_enums.h:33
unsigned char spi_transfer(unsigned char data)
Transfer a single byte of data over the SPI bus.
Definition spi.c:160
SPI_Status spi_init(SPI_Direction direction, SPI_Polarity setup, SPI_Phase sample)
Initialize the SPI hardware interface in master mode with specified configuration.
Definition spi.c:48
void spi_select(SPI_Select mode)
Control the SPI Slave Select (SS) pin to enable or disable the SPI slave device.
Definition spi.c:138
Additional Information
| Type | Link | Description |
| AVR-Series | pdf | ATmega16A datasheet |
R. GAECHTER