AVRly - AVR Development Resources
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 i2c.c
25 * @ingroup ccs811
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
36
37#include <avr/interrupt.h>
38
39#include "i2c.h"
40
41
42/**
43 * SCL frequency = F_CPU / (16 + (2 * TWBR) * PRESCALER)
44 */
45void init_i2c(uint32_t bus_speed)
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}
51
53{
54 loop_until_bit_is_set(TWCR, TWINT);
55}
56
57void i2c_start(void)
58{
59 TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWSTA));
61}
62
63void i2c_stop(void)
64{
65 TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWSTO));
66}
67
68void i2c_send(uint8_t data)
69{
70 TWDR = data;
71 TWCR = (_BV(TWINT) | _BV(TWEN)); /* init and enable */
73}
74
75
76uint8_t i2c_read_ack(void)
77{
78 TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWEA));
80 return (TWDR);
81}
82
83uint8_t i2c_read_no_ack(void)
84{
85 TWCR = (_BV(TWINT) | _BV(TWEN));
87 return (TWDR);
88}
89
90
91/*** end of file ***/
void init_i2c(uint32_t bus_speed)
SCL frequency = F_CPU / (16 + (2 * TWBR) * PRESCALER)
Definition: i2c.c:45
void i2c_wait_for_complete(void)
Loops until the i2c message is complete, as the hardware sets the TWINT flag.
Definition: i2c.c:52
uint8_t i2c_read_no_ack(void)
Read in from slave, sending NOACK when done (no TWEA).
Definition: i2c.c:83
uint8_t i2c_read_ack(void)
Read in from slave, sending ACK when done (sets TWEA).
Definition: i2c.c:76
void i2c_stop(void)
Sends a stop condition (sets TWSTO).
Definition: i2c.c:63
void i2c_send(uint8_t data)
Loads data, sends it out, waiting for completion.
Definition: i2c.c:68
void i2c_start(void)
Sends a start condition (sets TWSTA).
Definition: i2c.c:57
I2C communications driver for AVR MCU's.