-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.h
123 lines (110 loc) · 3.26 KB
/
i2c.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// i2c.c
//
//
// Created by NullWolf on 09/07/17.
// Copyright © 2017 NullWolf. All rights reserved.
//
#ifndef i2c_h
#define i2c_h
#ifndef F_CPU
#define F_CPU 1000000
#endif
#define F_I2C 25000L
#define TRANSMISSION_SUCCESS -1
#define TRANSMISSION_ERROR -2
#define BUS_CONNECTED -3
#define BUS_DISCONNECTED -4
#define MASTER_TRANSMITTER 0
#define MASTER_RECEIVER 1
#define ACK 0
#define TIMEOUT 50
typedef uint8_t bool;
/*
* Initialize I2C as bus master
*
* I2C frequency should be set at least 16 times less than the slave CPU
* frequency.
*
* F_I2C = F_CPU / (16 + (2 * TWBR * TWSR_prescalar))
*/
void i2c_init(void);
/*
* Transmit start condition
*
* I2C commands are initiated by the master with a START condition. A START
* condition is performed by the master actively pulling SDA low while SCL
* remains high. After the START condition the bus is busy and can only be used
* by another master only after a STOP condition is detected.
*
* Arguments:
* mode The request control state of the master (transmit or receive).
* State of mode will control the R/W bit value.
*
* Returns:
* Transmission status code.
*/
uint8_t i2c_tx_start(bool mode);
/*
* Transmit slave address
*
* Following the START condition the 7-bit slave address is transmitted along
* with master mode/data direction bit (R/W) as the 8th bit. If the R/W bit is
* 0 (SDA active low) then the master will write to the slave device. If the
* R/W bit is 1 (SDA passive high) then the master will read from slave device.
* The address/direction byte is acknowledged by the slave as the 9th bit.
*
* Arguments:
* address 7-bit address of slave
*
* Returns:
* Transmission status code
*/
uint8_t i2c_tx_address(uint8_t address);
/*
* Transmit byte of data
*
* Data on the I2C bus is transferred in 8-bit packets. Each byte that is
* transmitted is acknowledged by the slave in the form of the 9th bit. This
* bit indicates that the slave device is ready to proceed with the next byte.
* If the slave device does not acknowledges transfer this means that there is
* no more data or the device is not ready for the transfer yet.
*
* Arguments:
* byteData Data byte transmitted to slave
*
* Returns:
* Transmission status code
*/
uint8_t i2c_tx_byte(uint8_t byteData);
/*
* I2C Timeout
*
* Returns:
* Timeout status
*/
bool i2c_timeout(void);
/*
* Receive byte of data
*
* Acknowledgement of the data transfer from the slave to master is stored as
* an ACK (data received) or NACK (data not received). As long as ACK is being
* reported to the slave after each byte then the slave will continue sending
* additional data to the master until NACK is transmitted.
*
* Arguments:
* acknack Enable or disable of ACK generation
*
* Returns:
* One byte of data, or transmission status code
*/
uint8_t i2c_rx_byte(bool acknack);
/*
* Transmit stop condition
*
* Once data frames have been sent the master will issue a STOP condition. Stop
* conditions are defined by a low to high transition on SDA after a low to
* high transition on SCL, with SCL remaining high.
*/
void i2c_tx_stop(void);
#endif