Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | /*! \file i2csw.c \brief Software I2C interface using port pins. */ |
2 | //***************************************************************************** |
||
3 | // |
||
4 | // File Name : 'i2csw.c' |
||
5 | // Title : Software I2C interface using port pins |
||
6 | // Author : Pascal Stang |
||
7 | // Created : 11/22/2000 |
||
8 | // Revised : 5/2/2002 |
||
9 | // Version : 1.1 |
||
10 | // Target MCU : Atmel AVR series |
||
11 | // Editor Tabs : 4 |
||
12 | // |
||
13 | // This code is distributed under the GNU Public License |
||
14 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
15 | // |
||
16 | //***************************************************************************** |
||
17 | |||
18 | #include <avr/io.h> |
||
19 | |||
20 | #include "i2csw.h" |
||
21 | |||
22 | // Standard I2C bit rates are: |
||
23 | // 100KHz for slow speed |
||
24 | // 400KHz for high speed |
||
25 | |||
26 | //#define QDEL delay(5) // i2c quarter-bit delay |
||
27 | //#define HDEL delay(10) // i2c half-bit delay |
||
28 | |||
29 | // i2c quarter-bit delay |
||
30 | #define QDEL asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); |
||
31 | // i2c half-bit delay |
||
32 | #define HDEL asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); asm volatile("nop"); |
||
33 | |||
34 | #define I2C_SDL_LO cbi( SDAPORT, SDA) |
||
35 | #define I2C_SDL_HI sbi( SDAPORT, SDA) |
||
36 | |||
37 | #define I2C_SCL_LO cbi( SCLPORT, SCL); |
||
38 | #define I2C_SCL_HI sbi( SCLPORT, SCL); |
||
39 | |||
40 | #define I2C_SCL_TOGGLE HDEL; I2C_SCL_HI; HDEL; I2C_SCL_LO; |
||
41 | #define I2C_START I2C_SDL_LO; QDEL; I2C_SCL_LO; |
||
42 | #define I2C_STOP HDEL; I2C_SCL_HI; QDEL; I2C_SDL_HI; HDEL; |
||
43 | |||
44 | /* |
||
45 | void i2ct(void) |
||
46 | { |
||
47 | HDEL; I2C_SCL_HI; HDEL; I2C_SCL_LO; |
||
48 | } |
||
49 | |||
50 | void i2cstart(void) |
||
51 | { |
||
52 | I2C_SDL_LO; QDEL; I2C_SCL_LO; |
||
53 | } |
||
54 | |||
55 | void i2cstop(void) |
||
56 | { |
||
57 | HDEL; I2C_SCL_HI; QDEL; I2C_SDL_HI; HDEL; |
||
58 | } |
||
59 | |||
60 | |||
61 | #define I2C_SCL_TOGGLE i2ct(); |
||
62 | #define I2C_START i2cstart(); |
||
63 | #define I2C_STOP i2cstop(); |
||
64 | */ |
||
65 | |||
66 | UINT i2cPutbyte(u08 b) |
||
67 | { |
||
68 | int i; |
||
69 | |||
70 | for (i=7;i>=0;i--) |
||
71 | { |
||
72 | if ( b & (1<<i) ) |
||
73 | I2C_SDL_HI; |
||
74 | else |
||
75 | I2C_SDL_LO; // address bit |
||
76 | I2C_SCL_TOGGLE; // clock HI, delay, then LO |
||
77 | } |
||
78 | |||
79 | I2C_SDL_HI; // leave SDL HI |
||
80 | // added |
||
81 | cbi(SDADDR, SDA); // change direction to input on SDA line (may not be needed) |
||
82 | HDEL; |
||
83 | I2C_SCL_HI; // clock back up |
||
84 | b = inb(SDAPIN) & (1<<SDA); // get the ACK bit |
||
85 | |||
86 | HDEL; |
||
87 | I2C_SCL_LO; // not really ?? |
||
88 | sbi(SDADDR, SDA); // change direction back to output |
||
89 | HDEL; |
||
90 | return (b == 0); // return ACK value |
||
91 | } |
||
92 | |||
93 | |||
94 | u08 i2cGetbyte(UINT last) |
||
95 | { |
||
96 | int i; |
||
97 | u08 c,b = 0; |
||
98 | |||
99 | I2C_SDL_HI; // make sure pullups are ativated |
||
100 | cbi(SDADDR, SDA); // change direction to input on SDA line (may not be needed) |
||
101 | |||
102 | for(i=7;i>=0;i--) |
||
103 | { |
||
104 | HDEL; |
||
105 | I2C_SCL_HI; // clock HI |
||
106 | c = inb(SDAPIN) & (1<<SDA); |
||
107 | b <<= 1; |
||
108 | if(c) b |= 1; |
||
109 | HDEL; |
||
110 | I2C_SCL_LO; // clock LO |
||
111 | } |
||
112 | |||
113 | sbi(SDADDR, SDA); // change direction to output on SDA line |
||
114 | |||
115 | if (last) |
||
116 | I2C_SDL_HI; // set NAK |
||
117 | else |
||
118 | I2C_SDL_LO; // set ACK |
||
119 | |||
120 | I2C_SCL_TOGGLE; // clock pulse |
||
121 | I2C_SDL_HI; // leave with SDL HI |
||
122 | return b; // return received byte |
||
123 | } |
||
124 | |||
125 | |||
126 | //************************ |
||
127 | //* I2C public functions * |
||
128 | //************************ |
||
129 | |||
130 | //! Initialize I2C communication |
||
131 | void i2cInit(void) |
||
132 | { |
||
133 | sbi( SDADDR, SDA); // set SDA as output |
||
134 | sbi( SCLDDR, SCL); // set SCL as output |
||
135 | I2C_SDL_HI; // set I/O state and pull-ups |
||
136 | I2C_SCL_HI; // set I/O state and pull-ups |
||
137 | } |
||
138 | |||
139 | //! Send a byte sequence on the I2C bus |
||
140 | void i2cSend(u08 device, u08 subAddr, u08 length, u08 *data) |
||
141 | { |
||
142 | I2C_START; // do start transition |
||
143 | i2cPutbyte(device); // send DEVICE address |
||
144 | i2cPutbyte(subAddr); // and the subaddress |
||
145 | |||
146 | // send the data |
||
147 | while (length--) |
||
148 | i2cPutbyte(*data++); |
||
149 | |||
150 | I2C_SDL_LO; // clear data line and |
||
151 | I2C_STOP; // send STOP transition |
||
152 | } |
||
153 | |||
154 | //! Retrieve a byte sequence on the I2C bus |
||
155 | void i2cReceive(u08 device, u08 subAddr, u08 length, u08 *data) |
||
156 | { |
||
157 | int j = length; |
||
158 | u08 *p = data; |
||
159 | |||
160 | I2C_START; // do start transition |
||
161 | i2cPutbyte(device); // send DEVICE address |
||
162 | i2cPutbyte(subAddr); // and the subaddress |
||
163 | HDEL; |
||
164 | I2C_SCL_HI; // do a repeated START |
||
165 | I2C_START; // transition |
||
166 | |||
167 | i2cPutbyte(device | READ); // resend DEVICE, with READ bit set |
||
168 | |||
169 | // receive data bytes |
||
170 | while (j--) |
||
171 | *p++ = i2cGetbyte(j == 0); |
||
172 | |||
173 | I2C_SDL_LO; // clear data line and |
||
174 | I2C_STOP; // send STOP transition |
||
175 | } |
||
176 |
Powered by WebSVN v2.8.3