aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2018-05-15 22:30:58 -0400
committerJack Humbert <jack.humb@gmail.com>2018-05-15 22:30:58 -0400
commit682555faac8a67deff5688b956165cd1c39389cb (patch)
tree00a0991aed23eee4e2ad3bbcfab202476c7c2676
parent6dc215cd679d54ca2ab985c21c9bb299aca74b01 (diff)
downloadqmk_firmware-682555faac8a67deff5688b956165cd1c39389cb.tar.gz
qmk_firmware-682555faac8a67deff5688b956165cd1c39389cb.zip
i2c fix
-rwxr-xr-xdrivers/avr/i2c_master.c42
-rw-r--r--drivers/avr/is31fl3731.c7
-rw-r--r--keyboards/ergodox_ez/ergodox_ez.c4
-rw-r--r--keyboards/ergodox_ez/ergodox_ez.h2
-rw-r--r--keyboards/ergodox_ez/matrix.c3
-rw-r--r--keyboards/ergodox_ez/rules.mk4
6 files changed, 33 insertions, 29 deletions
diff --git a/drivers/avr/i2c_master.c b/drivers/avr/i2c_master.c
index f4a4bb7b0..bcf92153c 100755
--- a/drivers/avr/i2c_master.c
+++ b/drivers/avr/i2c_master.c
@@ -13,32 +13,34 @@
13 13
14void i2c_init(void) 14void i2c_init(void)
15{ 15{
16 TWSR = 0; /* no prescaler */
16 TWBR = (uint8_t)TWBR_val; 17 TWBR = (uint8_t)TWBR_val;
18 //TWBR = 10;
17} 19}
18 20
19uint8_t i2c_start(uint8_t address) 21uint8_t i2c_start(uint8_t address)
20{ 22{
21 // reset TWI control register 23 // reset TWI control register
22 TWCR = 0; 24 //TWCR = 0;
23 // transmit START condition 25 // transmit START condition
24 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); 26 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
25 // wait for end of transmission 27 // wait for end of transmission
26 while( !(TWCR & (1<<TWINT)) ); 28 while( !(TWCR & (1<<TWINT)) );
27 29
28 // check if the start condition was successfully transmitted 30 // check if the start condition was successfully transmitted
29 if((TWSR & 0xF8) != TW_START){ return 1; } 31 if(((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)){ return 1; }
30 32
31 // load slave address into data register 33 // load slave address into data register
32 TWDR = address; 34 TWDR = address;
33 // start transmission of address 35 // start transmission of address
34 TWCR = (1<<TWINT) | (1<<TWEN); 36 TWCR = (1<<TWINT) | (1<<TWEN);
35 // wait for end of transmission 37 // wait for end of transmission
36 while( !(TWCR & (1<<TWINT)) ); 38 while( !(TWCR & (1<<TWINT)) );
37 39
38 // check if the device has acknowledged the READ / WRITE mode 40 // check if the device has acknowledged the READ / WRITE mode
39 uint8_t twst = TW_STATUS & 0xF8; 41 uint8_t twst = TW_STATUS & 0xF8;
40 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1; 42 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
41 43
42 return 0; 44 return 0;
43} 45}
44 46
@@ -50,17 +52,17 @@ uint8_t i2c_write(uint8_t data)
50 TWCR = (1<<TWINT) | (1<<TWEN); 52 TWCR = (1<<TWINT) | (1<<TWEN);
51 // wait for end of transmission 53 // wait for end of transmission
52 while( !(TWCR & (1<<TWINT)) ); 54 while( !(TWCR & (1<<TWINT)) );
53 55
54 if( (TWSR & 0xF8) != TW_MT_DATA_ACK ){ return 1; } 56 if( (TW_STATUS & 0xF8) != TW_MT_DATA_ACK ){ return 1; }
55 57
56 return 0; 58 return 0;
57} 59}
58 60
59uint8_t i2c_read_ack(void) 61uint8_t i2c_read_ack(void)
60{ 62{
61 63
62 // start TWI module and acknowledge data after reception 64 // start TWI module and acknowledge data after reception
63 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); 65 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
64 // wait for end of transmission 66 // wait for end of transmission
65 while( !(TWCR & (1<<TWINT)) ); 67 while( !(TWCR & (1<<TWINT)) );
66 // return received data from TWDR 68 // return received data from TWDR
@@ -69,7 +71,7 @@ uint8_t i2c_read_ack(void)
69 71
70uint8_t i2c_read_nack(void) 72uint8_t i2c_read_nack(void)
71{ 73{
72 74
73 // start receiving without acknowledging reception 75 // start receiving without acknowledging reception
74 TWCR = (1<<TWINT) | (1<<TWEN); 76 TWCR = (1<<TWINT) | (1<<TWEN);
75 // wait for end of transmission 77 // wait for end of transmission
@@ -81,29 +83,29 @@ uint8_t i2c_read_nack(void)
81uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length) 83uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
82{ 84{
83 if (i2c_start(address | I2C_WRITE)) return 1; 85 if (i2c_start(address | I2C_WRITE)) return 1;
84 86
85 for (uint16_t i = 0; i < length; i++) 87 for (uint16_t i = 0; i < length; i++)
86 { 88 {
87 if (i2c_write(data[i])) return 1; 89 if (i2c_write(data[i])) return 1;
88 } 90 }
89 91
90 i2c_stop(); 92 i2c_stop();
91 93
92 return 0; 94 return 0;
93} 95}
94 96
95uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length) 97uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length)
96{ 98{
97 if (i2c_start(address | I2C_READ)) return 1; 99 if (i2c_start(address | I2C_READ)) return 1;
98 100
99 for (uint16_t i = 0; i < (length-1); i++) 101 for (uint16_t i = 0; i < (length-1); i++)
100 { 102 {
101 data[i] = i2c_read_ack(); 103 data[i] = i2c_read_ack();
102 } 104 }
103 data[(length-1)] = i2c_read_nack(); 105 data[(length-1)] = i2c_read_nack();
104 106
105 i2c_stop(); 107 i2c_stop();
106 108
107 return 0; 109 return 0;
108} 110}
109 111
@@ -146,4 +148,6 @@ void i2c_stop(void)
146{ 148{
147 // transmit STOP condition 149 // transmit STOP condition
148 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); 150 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
151 // wait until stop condition is executed and bus released
152 while(TWCR & (1<<TWSTO));
149} 153}
diff --git a/drivers/avr/is31fl3731.c b/drivers/avr/is31fl3731.c
index c7a99e3a3..13dfe6eaf 100644
--- a/drivers/avr/is31fl3731.c
+++ b/drivers/avr/is31fl3731.c
@@ -84,7 +84,8 @@ void IS31FL3731_write_register( uint8_t addr, uint8_t reg, uint8_t data )
84 g_twi_transfer_buffer[1] = data; 84 g_twi_transfer_buffer[1] = data;
85 85
86 //Transmit data until succesful 86 //Transmit data until succesful
87 while(i2c_transmit(addr << 1, g_twi_transfer_buffer,2) != 0); 87 //while(i2c_transmit(addr << 1, g_twi_transfer_buffer,2) != 0);
88 i2c_transmit(addr << 1, g_twi_transfer_buffer,2);
88} 89}
89 90
90void IS31FL3731_write_pwm_buffer( uint8_t addr, uint8_t *pwm_buffer ) 91void IS31FL3731_write_pwm_buffer( uint8_t addr, uint8_t *pwm_buffer )
@@ -108,7 +109,9 @@ void IS31FL3731_write_pwm_buffer( uint8_t addr, uint8_t *pwm_buffer )
108 } 109 }
109 110
110 //Transmit buffer until succesful 111 //Transmit buffer until succesful
111 while(i2c_transmit(addr << 1, g_twi_transfer_buffer,17) != 0); 112 //while(i2c_transmit(addr << 1, g_twi_transfer_buffer,17) != 0);
113 i2c_transmit(addr << 1, g_twi_transfer_buffer,17);
114
112 } 115 }
113} 116}
114 117
diff --git a/keyboards/ergodox_ez/ergodox_ez.c b/keyboards/ergodox_ez/ergodox_ez.c
index 21854ef77..36eb58a3c 100644
--- a/keyboards/ergodox_ez/ergodox_ez.c
+++ b/keyboards/ergodox_ez/ergodox_ez.c
@@ -1,6 +1,4 @@
1#include QMK_KEYBOARD_H 1#include QMK_KEYBOARD_H
2#include "i2cmaster.h"
3
4 2
5extern inline void ergodox_board_led_on(void); 3extern inline void ergodox_board_led_on(void);
6extern inline void ergodox_right_led_1_on(void); 4extern inline void ergodox_right_led_1_on(void);
@@ -329,7 +327,7 @@ const rgb_led g_rgb_leds[DRIVER_LED_TOTAL] = {
329 {{3|(9<<4)}, {24.9*0, 16*2}, 0}, // LED 20 327 {{3|(9<<4)}, {24.9*0, 16*2}, 0}, // LED 20
330 328
331 {{4|(6<<4)}, {24.9*3, 16*2}, 0}, // LED 21 329 {{4|(6<<4)}, {24.9*3, 16*2}, 0}, // LED 21
332 {{4|(7<<4)}, {24.9*3, 16*2}, 0}, // LED 22 330 {{4|(7<<4)}, {24.9*2, 16*2}, 0}, // LED 22
333 {{4|(8<<4)}, {24.9*1, 16*2}, 0}, // LED 23 331 {{4|(8<<4)}, {24.9*1, 16*2}, 0}, // LED 23
334 {{4|(9<<4)}, {24.9*0, 16*2}, 0}, // LED 24 332 {{4|(9<<4)}, {24.9*0, 16*2}, 0}, // LED 24
335}; 333};
diff --git a/keyboards/ergodox_ez/ergodox_ez.h b/keyboards/ergodox_ez/ergodox_ez.h
index eda6d767c..3ffc32553 100644
--- a/keyboards/ergodox_ez/ergodox_ez.h
+++ b/keyboards/ergodox_ez/ergodox_ez.h
@@ -4,7 +4,7 @@
4#include "quantum.h" 4#include "quantum.h"
5#include <stdint.h> 5#include <stdint.h>
6#include <stdbool.h> 6#include <stdbool.h>
7#include "i2cmaster.h" 7#include "i2c_master.h"
8#include <util/delay.h> 8#include <util/delay.h>
9 9
10#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) 10#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
diff --git a/keyboards/ergodox_ez/matrix.c b/keyboards/ergodox_ez/matrix.c
index e10171133..2aad99781 100644
--- a/keyboards/ergodox_ez/matrix.c
+++ b/keyboards/ergodox_ez/matrix.c
@@ -34,7 +34,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
34#include "util.h" 34#include "util.h"
35#include "matrix.h" 35#include "matrix.h"
36#include QMK_KEYBOARD_H 36#include QMK_KEYBOARD_H
37#include "i2cmaster.h"
38#ifdef DEBUG_MATRIX_SCAN_RATE 37#ifdef DEBUG_MATRIX_SCAN_RATE
39#include "timer.h" 38#include "timer.h"
40#endif 39#endif
@@ -297,7 +296,7 @@ static matrix_row_t read_cols(uint8_t row)
297 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out; 296 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
298 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out; 297 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
299 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out; 298 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
300 data = i2c_readNak(); 299 data = i2c_read_nack();
301 data = ~data; 300 data = ~data;
302 out: 301 out:
303 i2c_stop(); 302 i2c_stop();
diff --git a/keyboards/ergodox_ez/rules.mk b/keyboards/ergodox_ez/rules.mk
index a3b2ba5ca..0e0b3cdef 100644
--- a/keyboards/ergodox_ez/rules.mk
+++ b/keyboards/ergodox_ez/rules.mk
@@ -15,8 +15,8 @@
15#---------------------------------------------------------------------------- 15#----------------------------------------------------------------------------
16 16
17# # project specific files 17# # project specific files
18SRC = twimaster.c \ 18SRC = matrix.c \
19 matrix.c 19 i2c_master.c
20 20
21# MCU name 21# MCU name
22MCU = atmega32u4 22MCU = atmega32u4