oled 1.0
OLED Graphics Control Library
|
Header file with declarations and macros for software TWI/I2C. More...
#include <limits.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "../../../common/defines/TWI_defines.h"
#include "../../../common/enums/TWI_enums.h"
#include "../../../common/macros/PORT_macros.h"
Go to the source code of this file.
Macros | |
#define | F_CPU 20000000UL |
System clock frequency definition. | |
#define | F_SOFT_TWI 100000UL |
TWI clock frequency definition. | |
#define | TWI_SOFT_CLOCK_PULSE_US (float)(1000000.0/(F_SOFT_TWI<<1)) |
Duration of a half clock pulse in microseconds. | |
#define | TWI_SOFT_PULLUP_ENABLE |
Enable internal pull-up resistors for TWI lines. | |
#define | TWI_SOFT_PULLUP_ENABLE |
Enable internal pull-up resistors for TWI lines. | |
#define | TWI_SOFT_PORT PORTD |
Port register for the TWI lines. | |
#define | TWI_SOFT_SCL SET_PIN(2, _bm) |
Pin number for TWI Clock line (SCL). | |
#define | TWI_SOFT_SCL_CTRL SET_PIN(2, CTRL) |
#define | TWI_SOFT_SDA SET_PIN(3, _bm) |
Pin number for TWI Data line (SDA). | |
#define | TWI_SOFT_SDA_CTRL SET_PIN(3, CTRL) |
#define | TWI_SOFT_STATUS_BUS_INIT_COMPLETE 0x00 |
#define | TWI_SOFT_STATUS_TRANSMIT 0x01 |
#define | TWI_SOFT_STATUS_RECEIVE 0x02 |
#define | TWI_SOFT_STATUS_BUS_ERROR 0xFF |
#define | TWI_SOFT_BLOCK_IE |
Enable blocking of all system interrupts during TWI operations. | |
#define | SCL_LOW() |
#define | SDA_LOW() |
#define | SCL_HIGH() |
#define | SDA_HIGH() |
Functions | |
unsigned char | twi_soft_init (void) |
Initialize the software TWI (I2C) bus. | |
void | twi_soft_disable (void) |
Disable the software TWI (I2C) interface. | |
unsigned char | twi_soft_status (void) |
Get the current status of the software TWI bus. | |
TWI_Error | twi_soft_start (void) |
Generate a TWI start or repeated start condition. | |
void | twi_soft_stop (void) |
Stop the TWI (I2C) transmission. | |
TWI_Error | twi_soft_address (unsigned char address, TWI_Operation operation) |
Sends the slave address with the read/write operation on the TWI bus. | |
TWI_Error | twi_soft_set (unsigned char data) |
Transmit a byte of data on the TWI bus. | |
TWI_Error | twi_soft_get (unsigned char *data, TWI_Acknowledge acknowledge) |
Reads a byte of data from the TWI bus and sends an ACK or NACK. |
Header file with declarations and macros for software TWI/I2C.
This file provides function prototypes, type definitions, and constants for software-based TWI (I2C) communication on AVR microcontrollers.
#define F_CPU 20000000UL |
System clock frequency definition.
This macro defines the operating frequency of the microcontroller's clock in Hertz. It is used by delay functions and timing calculations. The value should match the actual hardware clock frequency to ensure correct timing behavior in the software.
#define F_SOFT_TWI 100000UL |
TWI clock frequency definition.
This macro defines the clock frequency used by the software-emulated TWI (I2C) implementation in Hertz. It sets the speed at which the software generates clock pulses for data transfer. Modifying this value affects the timing delays and the overall bus speed.
#define TWI_SOFT_BLOCK_IE |
Enable blocking of all system interrupts during TWI operations.
When defined, this macro causes the software TWI implementation to disable all system interrupts while a TWI transmission is in progress. This ensures uninterrupted communication on the TWI bus but may affect responsiveness to other interrupt-driven events. Use with caution to balance real-time requirements of the system.
#define TWI_SOFT_CLOCK_PULSE_US (float)(1000000.0/(F_SOFT_TWI<<1)) |
Duration of a half clock pulse in microseconds.
This macro calculates the length of one half clock pulse period in microseconds based on the configured TWI clock frequency (F_SOFT_TWI). his value is used to generate timing delays in the software TWI implementation to produce the desired clock signal frequency.
#define TWI_SOFT_PORT PORTD |
Port register for the TWI lines.
This macro defines the PORT register used for the software TWI (I2C) implementation on the AVR microcontroller. It is responsible for writing output values to the GPIO pins connected to the TWI clock (SCL) and data (SDA) lines. By default, it is set to VPORTB, but can be redefined to match the actual hardware wiring in the project.
#define TWI_SOFT_PULLUP_ENABLE |
Enable internal pull-up resistors for TWI lines.
Defining this macro enables the use of internal pull-up resistors on the SDA and SCL lines of the software TWI implementation. When enabled, the software configures the GPIO pins with internal pull-ups, eliminating the need for external pull-up resistors if the hardware supports it. If this macro is not defined, the software assumes external pull-up resistors are present and handles the lines differently.
#define TWI_SOFT_PULLUP_ENABLE |
Enable internal pull-up resistors for TWI lines.
Defining this macro enables the use of internal pull-up resistors on the SDA and SCL lines of the software TWI implementation. When enabled, the software configures the GPIO pins with internal pull-ups, eliminating the need for external pull-up resistors if the hardware supports it. If this macro is not defined, the software assumes external pull-up resistors are present and handles the lines differently.
#define TWI_SOFT_SCL SET_PIN(2, _bm) |
Pin number for TWI Clock line (SCL).
This macro defines the specific pin number on the port that is used for the software TWI clock line (SCL) in the AVR microcontroller. By default, it is set to PINB0, but can be redefined to fit the hardware
#define TWI_SOFT_SCL_CTRL SET_PIN(2, CTRL) |
#define TWI_SOFT_SDA SET_PIN(3, _bm) |
Pin number for TWI Data line (SDA).
This macro defines the specific pin number on the port that is used for the software TWI data line (SDA) in the AVR microcontroller. By default, it is set to PINB1, but can be redefined to fit the hardware
#define TWI_SOFT_SDA_CTRL SET_PIN(3, CTRL) |
TWI_Error twi_soft_address | ( | unsigned char | address, |
TWI_Operation | operation ) |
Sends the slave address with the read/write operation on the TWI bus.
address | 7-bit address of the slave device. |
operation | Specifies the operation type: TWI_Write or TWI_Read. |
This function prepares and sends the 8-bit address byte on the TWI bus, combining the 7-bit slave address shifted left by 1 with the least significant bit indicating the operation (read or write). The actual transmission and status handling is performed by twi_soft_set().
void twi_soft_disable | ( | void | ) |
Disable the software TWI (I2C) interface.
This function disables the software TWI module by clearing the SDA and SCL bits in the port and data direction registers. This effectively releases control of the TWI lines and sets the pins to a low output state. Use this function to safely deactivate the TWI bus when it is no longer needed.
TWI_Error twi_soft_get | ( | unsigned char * | data, |
TWI_Acknowledge | acknowledge ) |
Reads a byte of data from the TWI bus and sends an ACK or NACK.
data | Pointer to a variable where the received data byte will be stored. |
acknowledge | Specifies whether to send an ACK (TWI_ACK) or NACK (TWI_NACK) after reception. |
This function reads one data byte from the software TWI bus by manually controlling the clock and data lines. After reception, it sends either an acknowledgment (ACK) or not-acknowledgment (NACK) bit to the transmitter depending on the 'acknowledge' parameter, following the TWI protocol.
unsigned char twi_soft_init | ( | void | ) |
Initialize the software TWI (I2C) bus.
This function initializes the software TWI bus by configuring the SDA and SCL lines to their idle high state and checking if the bus lines are free (not held low). It introduces delays to ensure proper timing according to the configured TWI clock pulse duration. If either the SCL or SDA line is stuck low, the function returns a bus error status. Otherwise, it confirms that the bus has been initialized successfully.
< Bus error occurred
< Bus initialization complete
< Bus error occurred
< Bus initialization complete
TWI_Error twi_soft_set | ( | unsigned char | data | ) |
Transmit a byte of data on the TWI bus.
data | The data byte to be transmitted. |
This function sends the given 8-bit data byte bit-by-bit over the software TWI bus, handling the clock and data line states manually. It checks for arbitration loss when transmitting 1 bits and waits for clock stretching by the slave. After transmission, it reads the acknowledgment bit from the slave to determine successful reception.
TWI_Error twi_soft_start | ( | void | ) |
Generate a TWI start or repeated start condition.
This function produces a start condition on the TWI (I2C) bus which is used to initiate communication with a slave device. It sets the data line (SDA) low while the clock line (SCL) is high, creating the required falling edge signaling the start condition.
< Transmission running
|
inline |
Get the current status of the software TWI bus.
This inline function returns the value of the internal status variable twi_soft_bus_status which reflects the current operational state or error conditions of the software TWI interface.
void twi_soft_stop | ( | void | ) |
Stop the TWI (I2C) transmission.
This function generates the TWI stop condition by first pulling both SCL and SDA lines low, then releasing SCL high, followed by releasing SDA high, with appropriate delays to ensure bus timing compliance. After sending the stop condition, the transmit status flag is cleared to indicate the end of the transmission.
< Transmission running