rcc 1.0
RCC - RGB LED Color Cube Documentation
|
Header file with declarations and macros for hardware SPI. More...
#include <avr/io.h>
#include "../../../common/enums/SPI_enums.h"
#include "../../../common/macros/PORT_macros.h"
Go to the source code of this file.
Macros | |
#define | SPI2X_ENABLE |
Enables the SPI double speed mode. | |
#define | SPI_CLOCK SPI_PRESC_DIV4_gc |
Default SPI clock prescaler setting. | |
#define | SPI_PORTMUX PORTMUX_SPI0_DEFAULT_gc |
Selects the alternate port location for SPI pins. | |
#define | SPI_PORTMUX PORTMUX_SPI0_DEFAULT_gc |
Selects the alternate port location for SPI pins. | |
#define | SPI_PORT PORTA |
Specifies the port register for the SPI interface. | |
#define | SPI_MOSI PIN1_bm |
Specifies the bit mask for the SPI MOSI (Master Out Slave In) pin. | |
#define | SPI_MISO SET_PIN(2, _bm) |
Specifies the bit mask for the SPI MISO (Master In Slave Out) pin. | |
#define | SPI_MISO_PINCTRL SET_PIN(2, CTRL) |
Specifies the pin control register for the SPI MISO (Master In Slave Out) pin. | |
#define | SPI_SCK PIN3_bm |
Specifies the bit mask for the SPI SCK (Serial Clock) pin. | |
#define | SPI_SS SET_PIN(4, _bm) |
#define | SPI_ENABLE_MASTER_ABORT |
Enables the SPI master abort feature. | |
#define | SPI_SS_PINCTRL SET_PIN(4, CTRL) |
Specifies the pin control register for the SPI SS (Slave Select) pin. |
Functions | |
SPI_Status | spi_init (SPI_Direction direction, SPI_Polarity setup, SPI_Phase sample) |
Initialize the SPI hardware interface in master mode with specified configuration. | |
void | spi_disable (void) |
Disable the SPI hardware interface and reset related pins. | |
void | spi_select (SPI_Select mode) |
Control the SPI Slave Select (SS) pin to enable or disable the SPI slave device. | |
unsigned char | spi_transfer (unsigned char data) |
Transfer a single byte of data over the SPI bus. |
Header file with declarations and macros for hardware SPI.
This file provides function prototypes, type definitions, and constants for hardware-based SPI communication on AVR0 microcontrollers.
#define SPI2X_ENABLE |
Enables the SPI double speed mode.
When defined, this macro activates the SPI_CLK2X bit, which reduces the SPI clock prescaler from 4 to 2, effectively doubling the SPI clock speed. The SPI2X_ENABLE mode is useful when higher SPI data transfer speeds are required. It modifies the SPI clock frequency by changing the division factor applied to the peripheral clock (F_PER).
#define SPI_CLOCK SPI_PRESC_DIV4_gc |
Default SPI clock prescaler setting.
This macro sets the SPI clock prescaler to configure the SPI clock frequency. If not previously defined, it defaults to SPI_PRESC_DIV4_gc. The prescaler divides the peripheral clock frequency (F_PER) according to the table below, influenced by the SPI2X bit (double speed mode):
+---------------------------—+-------—+-------—+ | SPI2X | 0 | 1 | +---------------------------—+-------—+-------—+ | SPI_PRESC_DIV4_gc -> F_PER | F_PER/4 | F_PER/2 | | SPI_PRESC_DIV16_gc -> F_PER | F_PER/16 | F_PER/8 | | SPI_PRESC_DIV64_gc -> F_PER | F_PER/64 | F_PER/32 | | SPI_PRESC_DIV128_gc -> F_PER | F_PER/128| F_PER/64 | +---------------------------—+-------—+-------—+
Where F_PER is the peripheral clock frequency.
#define SPI_ENABLE_MASTER_ABORT |
Enables the SPI master abort feature.
This macro activates the SPI master abort functionality, which provides a mechanism to safely initialize the master mode. If not defined elsewhere, this macro is automatically defined, along with the SPI_SS_PINCTRL macro, which specifies the pin control register for the SPI Slave Select (SS) pin. The SPI_SS_PINCTRL macro defines settings such as pull-up resistors, input inversion, and interrupt triggering on the SS pin, ensuring reliable communication and proper SPI master abort behavior on initialization.
#define SPI_MISO SET_PIN(2, _bm) |
Specifies the bit mask for the SPI MISO (Master In Slave Out) pin.
This macro defines the pin mask used to identify the MISO signal in SPI operations. The MISO pin is used for data transmission from the slave to the master device in SPI communication. By default, SPI_MISO is set to PIN2_bm. Override this macro before including the SPI module if alternate pin assignment enabled for MISO is needed.
#define SPI_MISO_PINCTRL SET_PIN(2, CTRL) |
Specifies the pin control register for the SPI MISO (Master In Slave Out) pin.
This macro defines the pin control configuration register associated with the MISO signal in SPI communication. The MISO pin control register is used to configure settings such as pull-up resistors, invertion of input and interrupt triggering. By default, SPI_MISO_PINCTRL is set to PIN2CTRL. Override this macro before including the SPI module if a different pin control configuration is needed.
#define SPI_MOSI PIN1_bm |
Specifies the bit mask for the SPI MOSI (Master Out Slave In) pin.
This macro defines the pin mask used to identify the MOSI signal in SPI operations. The MOSI pin transmits data from the master to the slave device in SPI communication. By default, SPI_MOSI is set to PIN1_bm. Override this macro before including the SPI module if a different pin assignment for MOSI is needed.
#define SPI_PORT PORTA |
Specifies the port register for the SPI interface.
This macro defines the default port register used by the SPI bus It determines which hardware port is used for SPI pin control and direction settings.
By default, SPI_PORT is set to PORTA. Override this macro before including the SPI module if an alternate port configuration is needed.
#define SPI_PORTMUX PORTMUX_SPI0_DEFAULT_gc |
Selects the alternate port location for SPI pins.
This macro configures which physical port pins are used for the SPI interface. It determines the pin mapping of SPI signals such as MOSI, MISO, SCK, and SS.
The value can be set to predefined routing codes: ATTINY:
By default, SPI_ALTERNATE_PORT is set to PORTMUX_SPI0_ALTERNATE_gc. Override this macro prior to including the SPI module to select a different port mapping.
#define SPI_PORTMUX PORTMUX_SPI0_DEFAULT_gc |
Selects the alternate port location for SPI pins.
This macro configures which physical port pins are used for the SPI interface. It determines the pin mapping of SPI signals such as MOSI, MISO, SCK, and SS.
The value can be set to predefined routing codes: ATTINY:
By default, SPI_ALTERNATE_PORT is set to PORTMUX_SPI0_ALTERNATE_gc. Override this macro prior to including the SPI module to select a different port mapping.
#define SPI_SCK PIN3_bm |
Specifies the bit mask for the SPI SCK (Serial Clock) pin.
This macro defines the pin mask used to identify the SCK (Serial Clock) signal in SPI operations. The SCK pin provides the clock signal used to synchronize data transfer between the master and slave devices. By default, SPI_SCK is set to PIN3_bm. Override this macro before including the SPI module if a different pin assignment for SCK is required.
#define SPI_SS SET_PIN(4, _bm) |
#define SPI_SS_PINCTRL SET_PIN(4, CTRL) |
Specifies the pin control register for the SPI SS (Slave Select) pin.
This macro defines the pin control configuration register associated with the SS signal in SPI communication. The SS pin control register is used to configure settings such as pull-up resistors, invertion of input and interrupt triggering. By default, SPI_SS_PINCTRL is set to PIN4CTRL. Override this macro before including the SPI module if a different pin control configuration is needed.
void spi_disable | ( | void | ) |
Disable the SPI hardware interface and reset related pins.
This function disables the SPI peripheral on the AVR ATTiny series device by clearing the master and enable bits in the SPI control register. It also resets the SPI mode bits to their default state. The function configures the SPI-related pins (MOSI, MISO, SCK, SS) by clearing their direction bits, effectively disabling SPI pin control, and drives these pins low by clearing the output register. Additionally, the pull-up resistor on the SS pin is disabled. If SPI interrupts are enabled via the SPI_SPIE macro, this function disables SPI interrupt requests. The port multiplexer configuration for SPI is cleared and the SPI interrupt flags are reset.
SPI_Status spi_init | ( | SPI_Direction | direction, |
SPI_Polarity | setup, | ||
SPI_Phase | sample ) |
Initialize the SPI hardware interface in master mode with specified configuration.
direction | Specifies the bit order for SPI data transmission (MSB or LSB first). |
setup | Specifies the clock polarity (SPI_Polarity) to configure the clock idle state. |
sample | Specifies the clock phase (SPI_Phase) to configure the clock sampling edge. |
This function configures the SPI peripheral registers on the AVR ATTiny series device to initialize the SPI bus in master mode. It sets the SPI port multiplexer, configures the data direction for SPI pins, and sets up the SPI clock phase and polarity according to the specified parameters.
The function also sets pull-up resistors on the MISO and SS pins according to configuration, and configures SPI interrupts if the SPI_SPIE macro is defined.
void spi_select | ( | SPI_Select | mode | ) |
Control the SPI Slave Select (SS) pin to enable or disable the SPI slave device.
mode | Specifies the SPI select state, either SPI_Enable or SPI_Disable. |
This function manages the SPI Slave Select (SS) pin using the configured SPI port. When SPI_Enable is passed, the SS pin is driven low to select (activate) the SPI slave device. For any other value, the SS pin is driven high, deselecting (deactivating) the slave. This control is essential for ensuring that only one SPI slave device communicates with the master at any given time on a shared SPI bus.
unsigned char spi_transfer | ( | unsigned char | data | ) |
Transfer a single byte of data over the SPI bus.
data | The byte value to be sent via SPI. |
This function writes the provided data byte to the SPI data register, initiating the SPI transmission. It then waits in a busy loop until the SPI interrupt flag indicates that the transfer is complete. Upon completion, the function reads and returns the received byte from the SPI data register. SPI communication is full-duplex, so while sending a byte, a byte is received simultaneously. This function therefore effectively performs a combined send/receive operation.