Commit | Line | Data |
---|---|---|
69ec54f3 M |
1 | /* |
2 | Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com> | |
3 | ||
4 | This program is free software: you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation, either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | */ | |
17 | ||
18 | // Please do not modify this file | |
19 | ||
20 | #include <avr/io.h> | |
21 | #include <util/twi.h> | |
22 | ||
23 | #include "i2c.h" | |
24 | ||
25 | void i2c_set_bitrate(uint16_t bitrate_khz) { | |
26 | uint8_t bitrate_div = ((F_CPU / 1000l) / bitrate_khz); | |
27 | if (bitrate_div >= 16) { | |
28 | bitrate_div = (bitrate_div - 16) / 2; | |
29 | } | |
30 | TWBR = bitrate_div; | |
31 | } | |
32 | ||
33 | void i2c_init(void) { | |
34 | // set pull-up resistors on I2C bus pins | |
35 | PORTC |= 0b11; | |
36 | ||
37 | i2c_set_bitrate(400); | |
38 | ||
39 | // enable TWI (two-wire interface) | |
40 | TWCR |= (1 << TWEN); | |
41 | ||
42 | // enable TWI interrupt and slave address ACK | |
43 | TWCR |= (1 << TWIE); | |
44 | TWCR |= (1 << TWEA); | |
45 | } | |
46 | ||
47 | uint8_t i2c_start(uint8_t address) { | |
48 | // reset TWI control register | |
49 | TWCR = 0; | |
50 | ||
51 | // begin transmission and wait for it to end | |
52 | TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); | |
53 | while (!(TWCR & (1<<TWINT))); | |
54 | ||
55 | // check if the start condition was successfully transmitted | |
56 | if ((TWSR & 0xF8) != TW_START) { | |
57 | return 1; | |
58 | } | |
59 | ||
60 | // transmit address and wait | |
61 | TWDR = address; | |
62 | TWCR = (1<<TWINT) | (1<<TWEN); | |
63 | while (!(TWCR & (1<<TWINT))); | |
64 | ||
65 | // check if the device has acknowledged the READ / WRITE mode | |
66 | uint8_t twst = TW_STATUS & 0xF8; | |
67 | if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) { | |
68 | return 1; | |
69 | } | |
70 | ||
71 | return 0; | |
72 | } | |
73 | ||
74 | void i2c_stop(void) { | |
75 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); | |
76 | } | |
77 | ||
78 | uint8_t i2c_write(uint8_t data) { | |
79 | TWDR = data; | |
80 | ||
81 | // transmit data and wait | |
82 | TWCR = (1<<TWINT) | (1<<TWEN); | |
83 | while (!(TWCR & (1<<TWINT))); | |
84 | ||
85 | if ((TWSR & 0xF8) != TW_MT_DATA_ACK) { | |
86 | return 1; | |
87 | } | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
92 | uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length) { | |
93 | if (i2c_start(address)) { | |
94 | return 1; | |
95 | } | |
96 | ||
97 | for (uint16_t i = 0; i < length; i++) { | |
98 | if (i2c_write(data[i])) { | |
99 | return 1; | |
100 | } | |
101 | } | |
102 | ||
103 | i2c_stop(); | |
104 | ||
105 | return 0; | |
106 | } |