2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <aversive/wait.h>
32 #include <util/delay.h>
40 volatile i2cm_state g_i2cm_NUM_state = NOT_INIT;
41 volatile uint8_t g_i2cm_NUM_byte;
43 void (*g_i2cm_NUM_event)(i2cm_state state);
52 #define I2CM_NUM_SCL_PORT PORTC
53 #define I2CM_NUM_SCL_BIT 5
55 #define I2CM_NUM_SDA_PORT PORTC
56 #define I2CM_NUM_SDA_BIT 4
58 #define I2CM_NUM_SCL I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT
59 #define I2CM_NUM_SDA I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT
64 void i2cm_NUM_init(void)
67 - port SCL/SDA is set to 0
68 - i2c is then driven using DDR 0 (Hi-Z) or 1 (For Low)
72 cbi(DDR(I2CM_NUM_SCL_PORT),I2CM_NUM_SCL_BIT);
73 I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
76 cbi(DDR(I2CM_NUM_SDA_PORT),I2CM_NUM_SDA_BIT);
77 I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
80 g_i2cm_NUM_state = I2CM_READY;
84 void i2cm_NUM_manage(void)
86 if ( (g_i2cm_NUM_event != NULL) && (g_i2cm_NUM_state != I2CM_READY) ) {
87 g_i2cm_NUM_event( g_i2cm_NUM_state );
89 if (g_i2cm_NUM_state != I2CM_BUSY)
90 g_i2cm_NUM_state = I2CM_READY;
94 uint8_t i2cm_NUM_get_state(void)
96 return g_i2cm_NUM_state;
100 uint8_t i2cm_NUM_get_received_byte(void)
102 g_i2cm_NUM_state = I2CM_READY;
103 return g_i2cm_NUM_byte;
106 void i2cm_NUM_register_event(void (*func)(i2cm_state state))
110 g_i2cm_NUM_event = func;
115 uint8_t i2cm_NUM_send_byte(uint8_t byte)
121 g_i2cm_NUM_state = I2CM_BUSY;
123 // SCL should already be low
124 // I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
133 I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
135 I2C_LOW(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
143 I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
145 while ( bit_is_clear(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) );// slave handshake
148 I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
155 I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
156 I2CM_NUM_BIT_DELAY();
159 I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
161 while ( bit_is_clear(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) );// slave handshake
164 if (bit_is_set(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT))
165 err = I2CM_SENT_NO_ACK;
168 I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
175 uint8_t i2cm_NUM_send_start(uint8_t sla_w)
179 g_i2cm_NUM_state = I2CM_BUSY;
181 I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
182 I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
186 /* wait for bus realese */
187 for (i=0;i<0x8000; i++){
188 if( bit_is_set(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) && \
189 bit_is_set(PIN(I2CM_NUM_SDA_PORT), I2CM_NUM_SDA_BIT) )
193 // while ( bit_is_clear(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) );// slave handshake
196 I2C_LOW(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
198 I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
201 err = i2cm_NUM_send_byte(sla_w);
207 uint8_t i2cm_NUM_send_stop(void)
209 g_i2cm_NUM_state = I2CM_BUSY;
212 I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
213 I2C_LOW(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
217 I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
219 I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
222 g_i2cm_NUM_state = I2CM_SENT_STOP;
227 uint8_t i2cm_NUM_receive_byte(uint8_t last)
231 g_i2cm_NUM_state = I2CM_BUSY;
233 I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
241 I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
243 while ( bit_is_clear(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) );// slave handshake
245 if (bit_is_set(PIN(I2CM_NUM_SDA_PORT),I2CM_NUM_SDA_BIT))
249 I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
257 I2C_LOW(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
258 I2CM_NUM_BIT_DELAY();
262 I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
264 while ( bit_is_clear(PIN(I2CM_NUM_SCL_PORT), I2CM_NUM_SCL_BIT) );// slave handshake
267 I2C_LOW(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
272 I2C_HIGH(I2CM_NUM_SDA_PORT, I2CM_NUM_SDA_BIT);
273 I2CM_NUM_BIT_DELAY();
276 g_i2cm_NUM_byte = data;
277 g_i2cm_NUM_state = I2CM_RECEIVED_BYTE;
279 I2C_HIGH(I2CM_NUM_SCL_PORT, I2CM_NUM_SCL_BIT);
286 uint8_t i2c_send(uint8_t addr, uint8_t* data, uint8_t len, uint8_t dummy)
291 err = i2cm_NUM_send_start((addr<<1) | 0);
295 for (i=0; i<len; i++) {
296 err = i2cm_NUM_send_byte(data[i]);
301 i2cm_NUM_send_stop();
306 uint8_t i2c_buf[0x20];
308 uint8_t i2c_recv(uint8_t addr, uint8_t len, uint8_t dummy)
313 err = i2cm_NUM_send_start((addr<<1) | 1);
319 for (i=0; i<len; i++){
320 err =i2cm_NUM_receive_byte(i == len-1);
321 i2c_buf[i] = g_i2cm_NUM_byte;
325 i2cm_NUM_send_stop();
330 uint8_t i2c_get_recv_buffer(uint8_t* buf, uint8_t len)
333 for (i=0; i<len; i++)