AVRly - AVR Development Resources
atmega_i2c.c
Go to the documentation of this file.
1/******************************************************************************
2 @copyright Copyright © 2022 by Jason Duffy.
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21******************************************************************************/
22
23/**
24 * @file atmega_i2c.c
25 * @ingroup i2c
26 * @author Jason Duffy
27 * @date 5th March 2022
28 * @brief I2C communications driver for AVR MCU's.
29 * @bug No known bugs.
30 *
31 * This file provides basic I2C protocol comms functionality.
32 * Please note: Blocking waits are used whilst the host waits for hardware to
33 * set bits in registers.
34 *
35 * The symbol F_CPU must be defined for the bus timing calculations - usually
36 * defined in Makefile.
37 */
38
39
40#include <avr/interrupt.h>
41
42#include "i2c.h"
43
44
45/*
46 * Sets pullups and initializes i2c clock to desired bus speed.
47 * @param bus_speed is the desired i2c clock frequency.
48 */
49void init_i2c(uint32_t bus_speed)
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}
55
56
57/*
58 * Loops until the i2c message is complete, as the hardware sets the TWINT
59 * flag.
60 */
62{
63 loop_until_bit_is_set(TWCR, TWINT);
64}
65
66
67/*
68 * Sends a start condition (sets TWSTA).
69 */
70void i2c_start(void)
71{
72 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTA);
74}
75
76
77/*
78 * Sends a stop condition (sets TWSTO).
79 */
80void i2c_stop(void)
81{
82 TWCR = (1 << TWINT) | (1 << TWEN) | (1 <<TWSTO);
83}
84
85
86/*
87 * Loads data, sends it out, waiting for completion.
88 * @param data is the byte of data to be sent.
89 */
90void i2c_send(uint8_t data)
91{
92 TWDR = data;
93 TWCR = (1 << TWINT) | (1 << TWEN); // init and enable.
95}
96
97
98/*
99 * Read in from slave, sending ACK when done (sets TWEA).
100 * @returns The data received from the peripheral device.
101 */
102uint8_t i2c_read_ack(void)
103{
104 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
106 return (TWDR);
107}
108
109
110/**
111 * Read in from slave, sending NOACK when done (no TWEA).
112 * @returns The data received from the peripheral device.
113 */
114
115uint8_t i2c_read_no_ack(void)
116{
117 TWCR = (1 << TWINT) | (1 << TWEN);
119 return (TWDR);
120}
121
122
123/*** end of file ***/
void init_i2c(uint32_t bus_speed)
Sets pullups and initializes i2c clock to desired bus speed.
Definition: atmega_i2c.c:49
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
uint8_t i2c_read_no_ack(void)
Read in from slave, sending NOACK when done (no TWEA).
Definition: atmega_i2c.c:115
uint8_t i2c_read_ack(void)
Read in from slave, sending ACK when done (sets TWEA).
Definition: atmega_i2c.c:102
void i2c_stop(void)
Sends a stop condition (sets TWSTO).
Definition: atmega_i2c.c:80
void i2c_send(uint8_t data)
Loads data, sends it out, waiting for completion.
Definition: atmega_i2c.c:90
void i2c_start(void)
Sends a start condition (sets TWSTA).
Definition: atmega_i2c.c:70
I2C communications driver for AVR MCU's.