]> git.donarmstrong.com Git - qmk_firmware.git/blob - drivers/avr/i2c_master.c
f4a4bb7b0be474e9d486e5382d94042778ac2606
[qmk_firmware.git] / drivers / avr / i2c_master.c
1 /* Library made by: g4lvanix
2  * Github repository: https://github.com/g4lvanix/I2C-master-lib
3  */
4
5 #include <avr/io.h>
6 #include <util/twi.h>
7
8 #include "i2c_master.h"
9
10 #define F_SCL 400000UL // SCL frequency
11 #define Prescaler 1
12 #define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2)
13
14 void i2c_init(void)
15 {
16         TWBR = (uint8_t)TWBR_val;
17 }
18
19 uint8_t i2c_start(uint8_t address)
20 {
21         // reset TWI control register
22         TWCR = 0;
23         // transmit START condition 
24         TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
25         // wait for end of transmission
26         while( !(TWCR & (1<<TWINT)) );
27         
28         // check if the start condition was successfully transmitted
29         if((TWSR & 0xF8) != TW_START){ return 1; }
30         
31         // load slave address into data register
32         TWDR = address;
33         // start transmission of address
34         TWCR = (1<<TWINT) | (1<<TWEN);
35         // wait for end of transmission
36         while( !(TWCR & (1<<TWINT)) );
37         
38         // check if the device has acknowledged the READ / WRITE mode
39         uint8_t twst = TW_STATUS & 0xF8;
40         if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
41         
42         return 0;
43 }
44
45 uint8_t i2c_write(uint8_t data)
46 {
47         // load data into data register
48         TWDR = data;
49         // start transmission of data
50         TWCR = (1<<TWINT) | (1<<TWEN);
51         // wait for end of transmission
52         while( !(TWCR & (1<<TWINT)) );
53         
54         if( (TWSR & 0xF8) != TW_MT_DATA_ACK ){ return 1; }
55         
56         return 0;
57 }
58
59 uint8_t i2c_read_ack(void)
60 {
61         
62         // start TWI module and acknowledge data after reception
63         TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); 
64         // wait for end of transmission
65         while( !(TWCR & (1<<TWINT)) );
66         // return received data from TWDR
67         return TWDR;
68 }
69
70 uint8_t i2c_read_nack(void)
71 {
72         
73         // start receiving without acknowledging reception
74         TWCR = (1<<TWINT) | (1<<TWEN);
75         // wait for end of transmission
76         while( !(TWCR & (1<<TWINT)) );
77         // return received data from TWDR
78         return TWDR;
79 }
80
81 uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
82 {
83         if (i2c_start(address | I2C_WRITE)) return 1;
84         
85         for (uint16_t i = 0; i < length; i++)
86         {
87                 if (i2c_write(data[i])) return 1;
88         }
89         
90         i2c_stop();
91         
92         return 0;
93 }
94
95 uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length)
96 {
97         if (i2c_start(address | I2C_READ)) return 1;
98         
99         for (uint16_t i = 0; i < (length-1); i++)
100         {
101                 data[i] = i2c_read_ack();
102         }
103         data[(length-1)] = i2c_read_nack();
104         
105         i2c_stop();
106         
107         return 0;
108 }
109
110 uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
111 {
112         if (i2c_start(devaddr | 0x00)) return 1;
113
114         i2c_write(regaddr);
115
116         for (uint16_t i = 0; i < length; i++)
117         {
118                 if (i2c_write(data[i])) return 1;
119         }
120
121         i2c_stop();
122
123         return 0;
124 }
125
126 uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
127 {
128         if (i2c_start(devaddr)) return 1;
129
130         i2c_write(regaddr);
131
132         if (i2c_start(devaddr | 0x01)) return 1;
133
134         for (uint16_t i = 0; i < (length-1); i++)
135         {
136                 data[i] = i2c_read_ack();
137         }
138         data[(length-1)] = i2c_read_nack();
139
140         i2c_stop();
141
142         return 0;
143 }
144
145 void i2c_stop(void)
146 {
147         // transmit STOP condition
148         TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
149 }