AVRly - AVR Development Resources

I2C communications driver for AVR MCU's. More...

#include <avr/io.h>

Go to the source code of this file.

Functions

void init_i2c (uint32_t bus_speed)
 Sets pullups and initializes i2c clock to desired bus speed. More...
 
void i2c_wait_for_complete (void)
 Loops until the i2c message is complete, as the hardware sets the TWINT flag. More...
 
void i2c_start (void)
 Sends a start condition (sets TWSTA). More...
 
void i2c_stop (void)
 Sends a stop condition (sets TWSTO). More...
 
void i2c_send (uint8_t data)
 Loads data, sends it out, waiting for completion. More...
 
uint8_t i2c_read_ack (void)
 Read in from slave, sending ACK when done (sets TWEA). More...
 
uint8_t i2c_read_no_ack (void)
 Read in from slave, sending NOACK when done (no TWEA). More...
 

Detailed Description

I2C communications driver for AVR MCU's.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Author
Jason Duffy
Date
5th March 2022

This file provides basic I2C protocol comms functionality. Please note: Blocking waits are used whilst the host waits for hardware to set flags in registers.

The symbol F_CPU must be defined for the bus timing calculations - usually defined in Makefile.

Definition in file atmega_i2c.h.

Function Documentation

◆ init_i2c()

void init_i2c ( uint32_t  bus_speed)

Sets pullups and initializes i2c clock to desired bus speed.

Parameters
bus_speedis the desired i2c clock frequency.

Sets pullups and initializes i2c clock to desired bus speed.

Definition at line 49 of file atmega_i2c.c.

50{
51 uint32_t base_frequency = (F_CPU / bus_speed);
52 TWBR = (uint8_t)((base_frequency - 16) / (2 * 1)); // Prescaler of 1
53 TWCR |= (1 << TWEN); // Enable I2C.
54}

◆ i2c_wait_for_complete()

void i2c_wait_for_complete ( void  )

Loops until the i2c message is complete, as the hardware sets the TWINT flag.

Definition at line 61 of file atmega_i2c.c.

62{
63 loop_until_bit_is_set(TWCR, TWINT);
64}

◆ i2c_start()

void i2c_start ( void  )

Sends a start condition (sets TWSTA).

Definition at line 70 of file atmega_i2c.c.

71{
72 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTA);
74}
void i2c_wait_for_complete(void)
Loops until the i2c message is complete, as the hardware sets the TWINT flag.
Definition: atmega_i2c.c:61

◆ i2c_stop()

void i2c_stop ( void  )

Sends a stop condition (sets TWSTO).

Definition at line 80 of file atmega_i2c.c.

81{
82 TWCR = (1 << TWINT) | (1 << TWEN) | (1 <<TWSTO);
83}

◆ i2c_send()

void i2c_send ( uint8_t  data)

Loads data, sends it out, waiting for completion.

Parameters
datais the byte of data to be sent.

Definition at line 90 of file atmega_i2c.c.

91{
92 TWDR = data;
93 TWCR = (1 << TWINT) | (1 << TWEN); // init and enable.
95}

◆ i2c_read_ack()

uint8_t i2c_read_ack ( void  )

Read in from slave, sending ACK when done (sets TWEA).

Returns
The data received from the peripheral device.

Definition at line 102 of file atmega_i2c.c.

103{
104 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
106 return (TWDR);
107}

◆ i2c_read_no_ack()

uint8_t i2c_read_no_ack ( void  )

Read in from slave, sending NOACK when done (no TWEA).

Returns
The data received from the peripheral device.

Definition at line 115 of file atmega_i2c.c.

116{
117 TWCR = (1 << TWINT) | (1 << TWEN);
119 return (TWDR);
120}