Project

General

Profile

Revision 276

cleaned up library - dos2unix and delete-trailing-whitespace

View differences:

i2c.c
1 1
/**
2 2
 * Copyright (c) 2007 Colony Project
3
 * 
3
 *
4 4
 * Permission is hereby granted, free of charge, to any person
5 5
 * obtaining a copy of this software and associated documentation
6 6
 * files (the "Software"), to deal in the Software without
......
9 9
 * copies of the Software, and to permit persons to whom the
10 10
 * Software is furnished to do so, subject to the following
11 11
 * conditions:
12
 * 
12
 *
13 13
 * The above copyright notice and this permission notice shall be
14 14
 * included in all copies or substantial portions of the Software.
15
 * 
15
 *
16 16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18 18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
......
27 27
/**
28 28
 * @file i2c.c
29 29
 * @brief Implemenation of I2C communications protocol
30
 * 
30
 *
31 31
 * In the case where you have master sends and then a master request to the same
32 32
 * address, you will not give up control of the line because the send and
33 33
 * request addresses are seen as different addresses. In between it will send a
......
44 44
#include "ring_buffer.h"
45 45

  
46 46
/**
47
 * @defgroup i2c I2C 
47
 * @defgroup i2c I2C
48 48
 *
49 49
 * @brief Provides Inter-Interconnected-Communications (I2C)
50
 * 
51
 * Initiates I2C functions on an ATMega128 which has a fully hardware Two Wire 
50
 *
51
 * Initiates I2C functions on an ATMega128 which has a fully hardware Two Wire
52 52
 * Interface (TWI) module. Any Atmel chip with this hardware should be able to
53 53
 * use the software.
54 54
 *
55 55
 * This code will operate in a multi-master enviornment and can be either a
56 56
 * slave or a master at any time (as long as they are not one or the other at
57
 * the moment. You can queue up multiple transmission modes in the buffer up to 
57
 * the moment. You can queue up multiple transmission modes in the buffer up to
58 58
 * the buffer size. The buffer is implemented as a ring buffer.
59 59
 *
60 60
 * It is implemented using callback functions. Whenever you want to send a packet
......
65 65
 * so you must either buffer the inputs or handle each one separately.
66 66
 *
67 67
 * On errors we will simply flush the entire buffer.
68
 * 
68
 *
69 69
 * For information on how I2C operates, read the wikipedia article
70 70
 * http://en.wikipedia.org/wiki/I2c
71 71
 * for a good explanation of how it works.
72 72
 * @{
73 73
 */
74 74

  
75
/** 
75
/**
76 76
 * @brief Set bit rate 12 = 100kbit/s (max speed setting is 10 for an
77 77
 *  8 MHz clock). It is a divider, so the lower the number the faster the speed.
78 78
 */
......
111 111

  
112 112
    RING_BUFFER_CLEAR(i2c_write_buff);
113 113
    RING_BUFFER_CLEAR(i2c_addr_buff);
114
  
114

  
115 115
    /* enables twi interrupt, automatic ack sending, and all twi hardware */
116 116
    TWCR =  (_BV(TWEA) | _BV(TWEN) | _BV(TWIE));
117 117

  
......
121 121
    /* sets the address (it is stored in the 7 most significant bits) and allows
122 122
     * global messages to be accepted */
123 123
    TWAR = (addr << 1) | 1;
124
  
124

  
125 125
    return 0;
126 126
}
127 127

  
......
153 153
        RING_BUFFER_ADD(i2c_write_buff, data[i]);
154 154
        RING_BUFFER_ADD(i2c_addr_buff, dest << 1);
155 155
    }
156
    
156

  
157 157
    /* re-enable the interrupts */
158 158
    sei();
159
    
159

  
160 160
    /* send the start bit, only if this device is not currently master */
161 161
    if(!start_flag) {
162 162
        start_flag = 1;
163 163
        TWCR |= _BV(TWSTA);
164 164
        TWCR |= _BV(TWINT);
165 165
    }
166
  
166

  
167 167
    return 0;
168 168
}
169
 
169

  
170 170
/**
171 171
 * @brief Send a master request to the destination
172 172
 *
......
178 178
 * @param dest		The destination that we want to receive information from.
179 179
 *
180 180
 * @return 0 for success, nonzero for failure
181
 **/ 
181
 **/
182 182
int i2c_request(char dest) {
183 183
    if(RING_BUFFER_FULL(i2c_write_buff))
184 184
        return -1;
185
  
185

  
186 186
    RING_BUFFER_ADD(i2c_write_buff, 0);
187 187
    RING_BUFFER_ADD(i2c_addr_buff, (dest << 1) | 1);
188
  
188

  
189 189
    if(!start_flag) {
190 190
        start_flag = 1;
191 191
        TWCR |= _BV(TWSTA);
192 192
        TWCR |= _BV(TWINT);
193 193
    }
194
  
194

  
195 195
    return 0;
196 196
}
197 197

  
198 198
/** @} **/
199
 
199

  
200 200
/**
201 201
 * @brief Interrupt to handle I2C interrupts from the I2C hardware.
202
 * 
202
 *
203 203
 * Uses the status codes from the I2C register to handle the events
204 204
 * needed to advance in I2C stages. For instance, you will get a bit for
205 205
 * receiving a start ack, then a address ack, then a data ack, etc.
......
210 210
 * of what modes the send is in.
211 211
 *
212 212
 * Errors are handled here as well.
213
 **/ 
213
 **/
214 214
ISR(TWI_vect) {
215 215
	static char data_to_send;
216 216
	static char addr_to_send = -1;
217 217
	char addr, statusCode;
218
  
218

  
219 219
	//Get status code (only upper 5 bits)
220 220
	statusCode = (TWSR & 0xF8);
221 221

  
......
227 227
             * ring_buffer will not be empty */
228 228
            RING_BUFFER_REMOVE(i2c_addr_buff, addr_to_send);
229 229
            RING_BUFFER_REMOVE(i2c_write_buff, data_to_send);
230
            
230

  
231 231
            /* first send the address */
232
            TWDR = addr_to_send; 
233
            
232
            TWDR = addr_to_send;
233

  
234 234
            //Turn off start bits
235 235
            TWCR &= ~_BV(TWSTA);
236 236
            break;
......
241 241
            TWDR = data_to_send;
242 242
            PORTG &= ~_BV(PG2);
243 243
            break;
244
	 
244

  
245 245
        //Master Transmit - Data sent succesfully
246
        case TW_MT_DATA_ACK:    
246
        case TW_MT_DATA_ACK:
247 247
            //If there is still data to send
248 248
            if(!RING_BUFFER_EMPTY(i2c_write_buff)) {
249 249
                RING_BUFFER_PEEK(i2c_addr_buff, addr);
250
        
250

  
251 251
                //Still data for this address
252 252
                if (addr == addr_to_send) {
253 253
                    RING_BUFFER_REMOVE(i2c_addr_buff, addr);
......
262 262
            /* there are no bytes to send */
263 263
            TWCR |= _BV(TWSTO);
264 264
            start_flag = 0;
265
            break; 
266
            
265
            break;
266

  
267 267
        //Master Transmit - Slave sends a nack, transmit is done
268 268
        case TW_MT_DATA_NACK:
269 269
            PORTG |= _BV(PG2);
270 270
            TWCR |= _BV(TWSTO);
271 271
            start_flag = 0;
272 272
            break;
273
    
273

  
274 274
        //Master Receive - Address sent succesfully
275 275
        case TW_MR_SLA_ACK:
276 276
            PORTG |= _BV(PG2);
277 277
            break;
278
            
278

  
279 279
        //Master Receive - Data received succesfully
280 280
        case TW_MR_DATA_ACK:
281 281
            if(master_recv_function) {
......
284 284
                }
285 285
            }
286 286
            break;
287
            
288
        //Master Receive - Slave sends a nack, transmission is done    
287

  
288
        //Master Receive - Slave sends a nack, transmission is done
289 289
        case TW_MR_DATA_NACK:
290 290
            TWCR |= _BV(TWEA);
291
      
291

  
292 292
            //If there is still data to send
293 293
            if(!RING_BUFFER_EMPTY(i2c_write_buff)) {
294 294
                TWCR |= _BV(TWSTA);
295 295
                break;
296 296
            }
297
      
297

  
298 298
            /* there are no bytes to send */
299 299
            TWCR |= _BV(TWSTO);
300
            start_flag = 0;  
300
            start_flag = 0;
301 301
            break;
302
    
302

  
303 303
        //Slave Transmit - Address received
304 304
        case TW_ST_SLA_ACK:
305 305
            break;
306
    
306

  
307 307
        //Slave Transmit - Nack received, no data requsted
308 308
        case TW_ST_DATA_NACK:
309 309
            break;
310
        
310

  
311 311
        //Slave Transmit - Data requested, ack received
312 312
        case TW_ST_DATA_ACK:
313 313
            if (slave_send_function) {
314 314
                TWDR = slave_send_function();
315 315
            }
316 316
            break;
317
    
318
        //Slave Receive - Address received  
317

  
318
        //Slave Receive - Address received
319 319
        case TW_SR_SLA_ACK:
320 320
            break;
321
    
321

  
322 322
        //Slave Receive - Data received, ack returned
323 323
        case TW_SR_DATA_ACK:
324 324
            if(slave_recv_function) {
325 325
                slave_recv_function(TWDR);
326 326
            }
327
            
327

  
328 328
            break;
329
      
330
        //Stop sent  
329

  
330
        //Stop sent
331 331
        case TW_SR_STOP:
332 332
            break;
333
	
333

  
334 334
        //Problem on the bus, reset everything
335 335
        default:
336 336
            TWCR |= _BV(TWSTO);
337 337
            start_flag = 0;
338 338
            RING_BUFFER_CLEAR(i2c_write_buff);
339
            RING_BUFFER_CLEAR(i2c_addr_buff);  	
339
            RING_BUFFER_CLEAR(i2c_addr_buff);
340 340
    }
341
  
341

  
342 342
  /* Toggle TWINT so that it resets and executes the commands */
343 343
  TWCR |= _BV(TWINT);
344 344
}

Also available in: Unified diff