diff options
Diffstat (limited to 'firmware/drivers/i2c.c')
-rw-r--r-- | firmware/drivers/i2c.c | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/firmware/drivers/i2c.c b/firmware/drivers/i2c.c index f0b5907be8..6530227ad7 100644 --- a/firmware/drivers/i2c.c +++ b/firmware/drivers/i2c.c | |||
@@ -21,22 +21,23 @@ | |||
21 | #include "kernel.h" | 21 | #include "kernel.h" |
22 | #include "thread.h" | 22 | #include "thread.h" |
23 | #include "debug.h" | 23 | #include "debug.h" |
24 | #include "system.h" | ||
24 | 25 | ||
25 | #define PB13 0x2000 | 26 | #define PB13 0x2000 |
26 | #define PB7 0x0080 | 27 | #define PB7 0x0080 |
27 | #define PB5 0x0020 | 28 | #define PB5 0x0020 |
28 | 29 | ||
29 | /* cute little functions */ | 30 | /* cute little functions, atomic read-modify-write */ |
30 | #define SDA_LO (PBDR &= ~PB7) | 31 | #define SDA_LO __clear_bit_constant(7, &PBDRL) |
31 | #define SDA_HI (PBDR |= PB7) | 32 | #define SDA_HI __set_bit_constant(7, &PBDRL) |
32 | #define SDA_INPUT (PBIOR &= ~PB7) | 33 | #define SDA_INPUT __clear_bit_constant(7, &PBIORL) |
33 | #define SDA_OUTPUT (PBIOR |= PB7) | 34 | #define SDA_OUTPUT __set_bit_constant(7, &PBIORL) |
34 | #define SDA (PBDR & PB7) | 35 | #define SDA (PBDR & PB7) |
35 | 36 | ||
36 | #define SCL_INPUT (PBIOR &= ~PB13) | 37 | #define SCL_INPUT __clear_bit_constant(13-8, &PBIORH) |
37 | #define SCL_OUTPUT (PBIOR |= PB13) | 38 | #define SCL_OUTPUT __set_bit_constant(13-8, &PBIORH) |
38 | #define SCL_LO (PBDR &= ~PB13) | 39 | #define SCL_LO __clear_bit_constant(13-8, &PBDRH) |
39 | #define SCL_HI (PBDR |= PB13) | 40 | #define SCL_HI __set_bit_constant(13-8, &PBDRH) |
40 | #define SCL (PBDR & PB13) | 41 | #define SCL (PBDR & PB13) |
41 | 42 | ||
42 | /* arbitrary delay loop */ | 43 | /* arbitrary delay loop */ |
@@ -81,11 +82,11 @@ void i2c_init(void) | |||
81 | PBCR2 &= ~0xcc00; /* PB5 abd PB7 */ | 82 | PBCR2 &= ~0xcc00; /* PB5 abd PB7 */ |
82 | 83 | ||
83 | /* PB5 is "MAS enable". make it output and high */ | 84 | /* PB5 is "MAS enable". make it output and high */ |
84 | PBIOR |= PB5; | 85 | __set_bit_constant(5, &PBIORL); |
85 | PBDR |= PB5; | 86 | __set_bit_constant(5, &PBDRL); |
86 | 87 | ||
87 | /* Set the clock line to an output */ | 88 | /* Set the clock line PB13 to an output */ |
88 | PBIOR |= PB13; | 89 | __set_bit_constant(13-8, &PBIORH); |
89 | 90 | ||
90 | SDA_OUTPUT; | 91 | SDA_OUTPUT; |
91 | SDA_HI; | 92 | SDA_HI; |
@@ -103,9 +104,13 @@ void i2c_ack(int bit) | |||
103 | 104 | ||
104 | SCL_LO; /* Set the clock low */ | 105 | SCL_LO; /* Set the clock low */ |
105 | if ( bit ) | 106 | if ( bit ) |
107 | { | ||
106 | SDA_HI; | 108 | SDA_HI; |
109 | } | ||
107 | else | 110 | else |
111 | { | ||
108 | SDA_LO; | 112 | SDA_LO; |
113 | } | ||
109 | 114 | ||
110 | SCL_INPUT; /* Set the clock to input */ | 115 | SCL_INPUT; /* Set the clock to input */ |
111 | while(!SCL) /* and wait for the MAS to release it */ | 116 | while(!SCL) /* and wait for the MAS to release it */ |
@@ -153,9 +158,13 @@ void i2c_outb(unsigned char byte) | |||
153 | /* clock out each bit, MSB first */ | 158 | /* clock out each bit, MSB first */ |
154 | for ( i=0x80; i; i>>=1 ) { | 159 | for ( i=0x80; i; i>>=1 ) { |
155 | if ( i & byte ) | 160 | if ( i & byte ) |
161 | { | ||
156 | SDA_HI; | 162 | SDA_HI; |
163 | } | ||
157 | else | 164 | else |
165 | { | ||
158 | SDA_LO; | 166 | SDA_LO; |
167 | } | ||
159 | SCL_HI; | 168 | SCL_HI; |
160 | SCL_LO; | 169 | SCL_LO; |
161 | } | 170 | } |