]> git.donarmstrong.com Git - qmk_firmware.git/blob - drivers/avr/i2c_master.c
revert some attempts, update i2c
[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   TWSR = 0;     /* no prescaler */
17         TWBR = (uint8_t)TWBR_val;
18   //TWBR = 10;
19 }
20
21 uint8_t i2c_start(uint8_t address)
22 {
23         // reset TWI control register
24         TWCR = 0;
25         // transmit START condition
26         TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
27         // wait for end of transmission
28         while( !(TWCR & (1<<TWINT)) );
29
30         // check if the start condition was successfully transmitted
31         if(((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)){ return 1; }
32
33         // load slave address into data register
34         TWDR = address;
35         // start transmission of address
36         TWCR = (1<<TWINT) | (1<<TWEN);
37         // wait for end of transmission
38         while( !(TWCR & (1<<TWINT)) );
39
40         // check if the device has acknowledged the READ / WRITE mode
41         uint8_t twst = TW_STATUS & 0xF8;
42         if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
43
44         return 0;
45 }
46
47 uint8_t i2c_write(uint8_t data)
48 {
49         // load data into data register
50         TWDR = data;
51         // start transmission of data
52         TWCR = (1<<TWINT) | (1<<TWEN);
53         // wait for end of transmission
54         while( !(TWCR & (1<<TWINT)) );
55
56         if( (TW_STATUS & 0xF8) != TW_MT_DATA_ACK ){ return 1; }
57
58         return 0;
59 }
60
61 uint8_t i2c_read_ack(void)
62 {
63
64         // start TWI module and acknowledge data after reception
65         TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
66         // wait for end of transmission
67         while( !(TWCR & (1<<TWINT)) );
68         // return received data from TWDR
69         return TWDR;
70 }
71
72 uint8_t i2c_read_nack(void)
73 {
74
75         // start receiving without acknowledging reception
76         TWCR = (1<<TWINT) | (1<<TWEN);
77         // wait for end of transmission
78         while( !(TWCR & (1<<TWINT)) );
79         // return received data from TWDR
80         return TWDR;
81 }
82
83 uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
84 {
85         if (i2c_start(address | I2C_WRITE)) return 1;
86
87         for (uint16_t i = 0; i < length; i++)
88         {
89                 if (i2c_write(data[i])) return 1;
90         }
91
92         i2c_stop();
93
94         return 0;
95 }
96
97 uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length)
98 {
99         if (i2c_start(address | I2C_READ)) return 1;
100
101         for (uint16_t i = 0; i < (length-1); i++)
102         {
103                 data[i] = i2c_read_ack();
104         }
105         data[(length-1)] = i2c_read_nack();
106
107         i2c_stop();
108
109         return 0;
110 }
111
112 uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
113 {
114         if (i2c_start(devaddr | 0x00)) return 1;
115
116         i2c_write(regaddr);
117
118         for (uint16_t i = 0; i < length; i++)
119         {
120                 if (i2c_write(data[i])) return 1;
121         }
122
123         i2c_stop();
124
125         return 0;
126 }
127
128 uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
129 {
130         if (i2c_start(devaddr)) return 1;
131
132         i2c_write(regaddr);
133
134         if (i2c_start(devaddr | 0x01)) return 1;
135
136         for (uint16_t i = 0; i < (length-1); i++)
137         {
138                 data[i] = i2c_read_ack();
139         }
140         data[(length-1)] = i2c_read_nack();
141
142         i2c_stop();
143
144         return 0;
145 }
146
147 void i2c_stop(void)
148 {
149         // transmit STOP condition
150         TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
151   // wait until stop condition is executed and bus released
152   while(TWCR & (1<<TWSTO));
153 }