oled 1.0
OLED Graphics Control Library
|
Header file with declarations and macros for hardware TWI/I2C. More...
#include <avr/io.h>
#include "../../../common/defines/TWI_defines.h"
#include "../../../common/enums/TWI_enums.h"
Go to the source code of this file.
Macros | |
#define | F_CPU 12000000UL |
System clock frequency definition. | |
#define | F_TWI 100000UL |
TWI clock frequency definition. | |
#define | TWI_PRESCALER 0 |
TWI clock prescaler setting. | |
#define | TWI_BITRATE ((F_CPU/F_TWI) - 16UL) / (2 * (1<<TWI_PRESCALER) * (1<<TWI_PRESCALER)) |
Calculation of the TWI bus clock bitrate value. | |
#define | TWI_ADDRESS 0x42 |
Slave address of the TWI controller. | |
#define | TWI_BROADCAST 0x00 |
Enable or disable listening to general call addresses on the TWI bus. | |
#define | TWI_STATUS_START 0x08 |
Status code indicating START condition has been transmitted. | |
#define | TWI_STATUS_REPEATED_START 0x10 |
Status code indicating a repeated START condition has been transmitted. | |
#define | TWI_STATUS_ADDRESS_WRITE_NACK 0x18 |
Status code indicating SLA+W has been transmitted and NOT acknowledged. | |
#define | TWI_STATUS_ADDRESS_WRITE_ACK 0x20 |
Status code indicating SLA+W has been transmitted and acknowledged. | |
#define | TWI_STATUS_DATA_WRITE_NACK 0x28 |
Status code indicating data byte has been transmitted and NOT acknowledged. | |
#define | TWI_STATUS_DATA_WRITE_ACK 0x30 |
Status code indicating data byte has been transmitted and acknowledged. | |
#define | TWI_STATUS_ARBITRATION_LOST 0x38 |
Status code indicating arbitration has been lost in SLA+W or data bytes. | |
#define | TWI_STATUS_ADDRESS_READ_ACK 0x40 |
Status code indicating SLA+R has been transmitted and acknowledged. | |
#define | TWI_STATUS_ADDRESS_READ_NACK 0x48 |
Status code indicating SLA+R has been transmitted and NOT acknowledged. | |
#define | TWI_STATUS_DATA_READ_ACK 0x50 |
Status code indicating data byte has been received and acknowledged. | |
#define | TWI_STATUS_DATA_READ_NACK 0x58 |
Status code indicating data byte has been received and NOT acknowledged. |
Functions | |
unsigned char | twi_init (void) |
Initialize the TWI (I2C) hardware interface in master mode. | |
void | twi_disable (void) |
Disable the TWI (I2C) interface. | |
unsigned char | twi_status (void) |
Retrieves the current TWI status byte. | |
TWI_Error | twi_start (void) |
Initiates a TWI (I2C) (repeated) start condition on the bus. | |
void | twi_stop (void) |
Stop the TWI (I2C) transmission by generating a stop condition. | |
TWI_Error | twi_address (unsigned char address, TWI_Operation operation) |
Send a TWI (I2C) slave address combined with the read/write operation bit. | |
TWI_Error | twi_set (unsigned char data) |
Transmit a data byte over the TWI (I2C) bus. | |
TWI_Error | twi_get (unsigned char *data, TWI_Acknowledge acknowledge) |
Receive a data byte from the TWI (I2C) bus. |
Header file with declarations and macros for hardware TWI/I2C.
This file provides function prototypes, type definitions, and constants for hardware-based TWI (I2C) communication on AVR microcontrollers.
#define F_CPU 12000000UL |
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_TWI 100000UL |
TWI clock frequency definition.
This macro defines the clock frequency used by the TWI (I2C) bus in Hertz. It sets the speed at which the hardware generates clock pulses for data transfer. Modifying this value affects timing and the overall bus speed.
#define TWI_ADDRESS 0x42 |
Slave address of the TWI controller.
This macro defines the 7-bit address of the TWI (I2C) controller when operating as a slave device on the bus. The address is used by other devices (masters) to communicate with this slave.
#define TWI_BITRATE ((F_CPU/F_TWI) - 16UL) / (2 * (1<<TWI_PRESCALER) * (1<<TWI_PRESCALER)) |
Calculation of the TWI bus clock bitrate value.
This macro calculates the value for the TWI bitrate register used to set the clock frequency on the TWI bus. The calculation is based on the CPU clock frequency (F_CPU), the desired TWI clock frequency (F_TWI), and the prescaler value (TWI_PRESCALER).
#define TWI_BROADCAST 0x00 |
Enable or disable listening to general call addresses on the TWI bus.
This macro configures whether the TWI controller, when acting as a slave, responds to general call addresses.
Setting this to 1 enables the controller to listen and respond to general call messages (address 0x00). Setting to 0 disables general call response, so the controller only reacts to its specific slave address.
#define TWI_PRESCALER 0 |
TWI clock prescaler setting.
This macro defines the prescaler value for the TWI hardware clock. It sets the division factor applied to the TWI clock frequency (F_TWI) by configuring the prescaler bits (TWPS) in the TWSR register.
|
inline |
Send a TWI (I2C) slave address combined with the read/write operation bit.
address | 7-bit slave address to be transmitted. |
operation | Specifies the operation type: 0 for write, 1 for read. |
This inline function constructs the address byte by shifting the 7-bit slave address left by one and combining it with the least significant bit representing the operation (read/write). It then calls twi_set to transmit this address byte over the TWI bus and returns the transmission status.
address | 7-bit slave address to be transmitted. |
operation | Specifies the operation type: 0 for write, 1 for read. |
This inline function constructs the address byte by shifting the 7-bit slave address left by one and combining it with the least significant bit representing the operation (read/write). It then calls twi_set to transmit this address byte over the TWI bus and returns the transmission status.
|
inline |
Disable the TWI (I2C) interface.
This function disables the hardware TWI module. This effectively releases control of the TWI lines and sets the pins to reset state. Use this function to safely deactivate the TWI bus when it is no longer needed.
This inline function disables the hardware TWI module. This effectively releases control of the TWI lines and sets the pins to reset state. Use this function to safely deactivate the TWI bus when it is no longer needed.
TWI_Error twi_get | ( | unsigned char * | data, |
TWI_Acknowledge | acknowledge ) |
Receive a data byte from the TWI (I2C) bus.
data | Pointer to a variable where the received data byte will be stored. |
acknowledge | Specifies whether the master should send an ACK or NACK after receiving the byte:
|
This function configures the TWI hardware to receive a byte from the bus and decides whether to send an ACK or NACK based on the acknowledge parameter.
The function checks the TWI status register (TWSR) for arbitration loss or to verify that the appropriate ACK/NACK status was received. If the received status matches the expected acknowledge mode, it returns success; otherwise, it indicates an acknowledgment error.
data | Pointer to a variable where the received data byte will be stored. |
acknowledge | Specifies whether the master should send an ACK or NACK after receiving the byte:
|
This function configures the TWI hardware to receive a byte from the bus and decides whether to send an ACK or NACK based on the acknowledge parameter.
Depending on the acknowledge mode, the function sets the MCTRLB register to either send an ACK and prepare for more data reception or send a NACK and stop the communication.
Finally, the function returns the status code from the reception process.
unsigned char twi_init | ( | void | ) |
Initialize the TWI (I2C) hardware interface in master mode.
This function configures the TWI hardware registers to initialize the bus in master mode.
In master mode, it sets the bitrate and prescaler according to predefined macros.
The function returns the initial TWI status for further status checking after initialization.
This function configures the TWI hardware registers to initialize the bus exclusively in master mode.
It sets the bitrate and activates the TWI controller using the predefined macros.
The function returns the initial TWI status for further status checking after initialization.
TWI_Error twi_set | ( | unsigned char | data | ) |
Transmit a data byte over the TWI (I2C) bus.
data | The data byte to be transmitted to the bus. |
This function writes the provided data byte into the TWI data register and triggers the transmission by setting the TWINT and TWEN bits in the control register.
After transmission, the function reads the TWI status register (TWSR) to determine the outcome of the transmission and returns the corresponding status code.
data | The data byte to be transmitted to the bus. |
This function writes the provided data byte into the TWI data register. It then waits for the transmission to complete by repeatedly checking the TWI status.
The function returns the final TWI status code indicating the result of the transmission.
|
inline |
Initiates a TWI (I2C) (repeated) start condition on the bus.
This function triggers the TWI hardware to send a START condition on the bus, signaling the beginning of a new communication frame. It sets the necessary control bits in the TWCR register to initiate the start and enables the TWI interface.
After completion, the function checks the TWI status register (TWSR) to verify if the start or repeated start condition was acknowledged by the bus. If successful, it returns TWI_None; otherwise, it returns TWI_Start indicating failure to send the (repeated) start condition.
It does not perform any operation.
On modern AVR hardware, start conditions are managed internally by the TWI module, so this function is no longer necessary and does not initiate any bus activity.
|
inline |
Retrieves the current TWI status byte.
Reads and returns the TWI status byte from the TWSR register, including the write collision flag.
void twi_stop | ( | void | ) |
Stop the TWI (I2C) transmission by generating a stop condition.
This function initiates the TWI stop condition on the bus, signaling the end of the current communication frame. It sets the appropriate bits in the TWCR register to transmit the stop signal and keeps the TWI hardware enabled.
This function initiates the TWI stop condition on the bus, signaling the end of the current communication frame. It sets the appropriate bits in the MCTRLB register to transmit the stop signal.