aboutsummaryrefslogtreecommitdiff
path: root/drivers/avr/i2c_master.c
diff options
context:
space:
mode:
authoryiancar <yiangosyiangou@cytanet.com.cy>2018-05-14 15:17:24 +0100
committerJack Humbert <jack.humb@gmail.com>2018-05-14 10:17:24 -0400
commita98a91cf1b923107e9f26df316c1ef2192ff14f7 (patch)
tree112b22b9ee2f212b4a58e57d2e4b4906d1ef7636 /drivers/avr/i2c_master.c
parentf42ec8aa866386ed0ab8faf7acf9c396aa482519 (diff)
downloadqmk_firmware-a98a91cf1b923107e9f26df316c1ef2192ff14f7.tar.gz
qmk_firmware-a98a91cf1b923107e9f26df316c1ef2192ff14f7.zip
Rgb matrix fixes, I2C library can now retry if it has failed (#2943)
* Added Modular keyboards L,R and NUM Created code modules for the 3 modules of the modular keyboard. Original idea by MechboardsUK. Uses i2c implementation similar to lets split * Remove modular from master This is to fix incorrect branching * General fixes for RGB_matrix - Complited speed support for all effects - Fixed raindrop effects to initialized after toggle - Fixed raindrop effects to use all available LEDs - Fixed effect step reverse function - Moved RGB_MATRIX_SOLID_REACTIVE under correct flag * Documentation update for RGBmatrix * More doc updates * I2C library can now retry if it has failed - Replaced the original TWIlib by LFKeyboard's modified version - Allows for an extra argument on TWITransmitData, if blocking is set to 1 function will retry to transmit on failure. Good for noisy boards. * RGB Matrix, use alternative I2C library TWIlib seems to be hanging for me sometimes probably due to ISR routine. I have used i2c_master as a good alternative. Note: this commit is for Wilba6582 to verify before merge * Update rgb_matrix.c * RGB matrix cleanup - Remove TWIlib
Diffstat (limited to 'drivers/avr/i2c_master.c')
-rwxr-xr-xdrivers/avr/i2c_master.c149
1 files changed, 149 insertions, 0 deletions
diff --git a/drivers/avr/i2c_master.c b/drivers/avr/i2c_master.c
new file mode 100755
index 000000000..f4a4bb7b0
--- /dev/null
+++ b/drivers/avr/i2c_master.c
@@ -0,0 +1,149 @@
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
14void i2c_init(void)
15{
16 TWBR = (uint8_t)TWBR_val;
17}
18
19uint8_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
45uint8_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
59uint8_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
70uint8_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
81uint8_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
95uint8_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
110uint8_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
126uint8_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
145void i2c_stop(void)
146{
147 // transmit STOP condition
148 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
149}