AVRly - AVR Development Resources
i2c.h
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.h
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 flags in registers.
34 */
35
36// Functions for i2c communication
37#ifndef I_TWO_C_DOT_H
38#define I_TWO_C_DOT_H
39
40#include <avr/io.h>
41
42
43/**
44 * Sets pullups and initializes bus speed to desired bus speed.
45 */
46void init_i2c(uint32_t bus_speed);
47
48
49/**
50 * Waits until the hardware sets the TWINT flag.
51 */
52void i2c_wait_for_complete(void);
53
54
55/**
56 * Sends a start condition (sets TWSTA).
57 */
58void i2c_start(void);
59
60
61/**
62 * Sends a stop condition (sets TWSTO).
63 */
64void i2c_stop(void);
65
66
67/**
68 * Loads data, sends it out, waiting for completion.
69 */
70void i2c_send(uint8_t data);
71
72
73/**
74 * Read in from slave, sending ACK when done (sets TWEA).
75 */
76uint8_t i2c_read_ack(void);
77
78
79/**
80 * Read in from slave, sending NOACK when done (no TWEA).
81 */
82uint8_t i2c_read_no_ack(void);
83
84
85#endif // I_TWO_C_DOT_H
86
87
88/*** end of file ***/
void init_i2c(uint32_t bus_speed)
Sets pullups and initializes bus speed to desired bus speed.
Definition: atmega_i2c.c:49
void i2c_wait_for_complete(void)
Waits until 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