AVRly - AVR Development Resources
i2c.c File Reference

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

#include <avr/interrupt.h>
#include "i2c.h"

Go to the source code of this file.

Functions

void init_i2c (uint32_t bus_speed)
 SCL frequency = F_CPU / (16 + (2 * TWBR) * PRESCALER) 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 bits in registers.

Definition in file i2c.c.

Function Documentation

◆ init_i2c()

void init_i2c ( uint32_t  bus_speed)

SCL frequency = F_CPU / (16 + (2 * TWBR) * PRESCALER)

Sets pullups and initializes bus speed to desired bus speed.

Sets pullups and initializes i2c clock to desired bus speed.

Definition at line 45 of file i2c.c.

46{
47 uint32_t base_frequency = (F_CPU / bus_speed);
48 TWBR = (uint8_t)((base_frequency - 16) / (2 * 1)); // Prescaler of 1
49 TWCR |= (1 << TWEN); // Enable
50}

◆ i2c_wait_for_complete()

void i2c_wait_for_complete ( void  )

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

Waits until the hardware sets the TWINT flag.

Definition at line 52 of file i2c.c.

53{
54 loop_until_bit_is_set(TWCR, TWINT);
55}

◆ i2c_start()

void i2c_start ( void  )

Sends a start condition (sets TWSTA).

Definition at line 57 of file i2c.c.

58{
59 TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWSTA));
61}
void i2c_wait_for_complete(void)
Loops until the i2c message is complete, as the hardware sets the TWINT flag.
Definition: i2c.c:52

◆ i2c_stop()

void i2c_stop ( void  )

Sends a stop condition (sets TWSTO).

Definition at line 63 of file i2c.c.

64{
65 TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWSTO));
66}

◆ 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 68 of file i2c.c.

69{
70 TWDR = data;
71 TWCR = (_BV(TWINT) | _BV(TWEN)); /* init and enable */
73}

◆ 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 76 of file i2c.c.

77{
78 TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWEA));
80 return (TWDR);
81}

◆ 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 83 of file i2c.c.

84{
85 TWCR = (_BV(TWINT) | _BV(TWEN));
87 return (TWDR);
88}