Subversion Repositories NaviCtrl

Rev

Rev 248 | Rev 313 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 248 Rev 252
Line 61... Line 61...
61
#include "timer1.h"
61
#include "timer1.h"
62
#include "config.h"
62
#include "config.h"
63
#include "led.h"
63
#include "led.h"
Line 64... Line 64...
64
 
64
 
-
 
65
 
Line 65... Line 66...
65
 
66
volatile u8 I2C_State = I2C_STATE_OFF;          // only one byte, because of sync by nesting irqs
66
volatile u8 I2C_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs
67
volatile u8 I2C_Error = I2C_ERROR_NOACK;    // only one byte!
67
 
68
 
68
// number of bytes to send
69
// number of bytes to send
Line 234... Line 235...
234
                {
235
                {
235
                        I2C_GenerateSTOP (I2C1, ENABLE);  // free the bus
236
                        I2C_GenerateSTOP (I2C1, ENABLE);  // free the bus
236
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
237
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
237
                }
238
                }
238
                I2C_State = I2C_STATE_IDLE;
239
                I2C_State = I2C_STATE_IDLE;
-
 
240
                I2C_Error = I2C_ERROR_NOACK;
239
                VIC_ITCmd(I2C1_ITLine, DISABLE);
241
                VIC_ITCmd(I2C1_ITLine, DISABLE);
240
                LED_GRN_OFF;
242
                LED_GRN_OFF;
241
                return;
243
                return;
242
        }
244
        }
243
        else
245
        else
Line 260... Line 262...
260
                                                {
262
                                                {
261
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
263
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
262
                                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
264
                                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
263
                                                        LED_GRN_OFF;
265
                                                        LED_GRN_OFF;
264
                                                        I2C_State = I2C_STATE_IDLE;
266
                                                        I2C_State = I2C_STATE_IDLE;
-
 
267
                                                        I2C_Error = I2C_ERROR_NONE;
265
                                                        return;
268
                                                        return;
266
                                                }
269
                                                }
267
                                                else
270
                                                else
268
                                                {
271
                                                {
269
                                                        I2C_State = I2C_STATE_RX_PROGRESS;
272
                                                        I2C_State = I2C_STATE_RX_PROGRESS;
Line 273... Line 276...
273
                                        default: // invalid direction
276
                                        default: // invalid direction
274
                                                I2C_GenerateSTOP (I2C1, ENABLE);
277
                                                I2C_GenerateSTOP (I2C1, ENABLE);
275
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
278
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
276
                                                LED_GRN_OFF;
279
                                                LED_GRN_OFF;
277
                                                I2C_State = I2C_STATE_IDLE;
280
                                                I2C_State = I2C_STATE_IDLE;
-
 
281
                                                I2C_Error = I2C_ERROR_UNKNOWN;
278
                                                return;
282
                                                return;
279
                                }
283
                                }
280
                                // enable acknowledge
284
                                // enable acknowledge
281
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
285
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
282
                                // send address/direction byte on the bus
286
                                // send address/direction byte on the bus
Line 308... Line 312...
308
                                        // should never happen
312
                                        // should never happen
309
                                        I2C_GenerateSTOP (I2C1, ENABLE);
313
                                        I2C_GenerateSTOP (I2C1, ENABLE);
310
                                        LED_GRN_OFF;
314
                                        LED_GRN_OFF;
311
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
315
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
312
                                        I2C_State = I2C_STATE_IDLE;
316
                                        I2C_State = I2C_STATE_IDLE;
-
 
317
                                        I2C_Error = I2C_ERROR_UNKNOWN;
313
                                        return;
318
                                        return;
314
                                        break;
319
                                        break;
315
                                }
320
                                }
316
                                break;
321
                                break;
Line 335... Line 340...
335
                                        else
340
                                        else
336
                                        {   // stop communication
341
                                        {   // stop communication
337
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
342
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
338
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
343
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
339
                                                LED_GRN_OFF;
344
                                                LED_GRN_OFF;
340
                                                I2C_State = I2C_STATE_IDLE;                     // ready for new actions        
345
                                                I2C_State = I2C_STATE_IDLE;                     // ready for new actions
-
 
346
                                                I2C_Error = I2C_ERROR_NONE;    
341
                                        }
347
                                        }
342
                                }
348
                                }
343
                                break;
349
                                break;
Line 344... Line 350...
344
 
350
 
Line 359... Line 365...
359
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT);
365
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT);
360
                                        DebugOut.Analog[15]++;
366
                                        DebugOut.Analog[15]++;
361
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
367
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
362
                                        LED_GRN_OFF;
368
                                        LED_GRN_OFF;
363
                                        I2C_State = I2C_STATE_IDLE;
369
                                        I2C_State = I2C_STATE_IDLE;
-
 
370
                                        I2C_Error = I2C_ERROR_NONE;
364
                                        return;
371
                                        return;
365
                                }
372
                                }
366
                                Rx_Idx++;
373
                                Rx_Idx++;
367
                                // if the 2nd last byte was received disable acknowledge for the last one
374
                                // if the 2nd last byte was received disable acknowledge for the last one
368
                                if ( (Rx_Idx + 1) == I2C_RxBufferSize )
375
                                if ( (Rx_Idx + 1) == I2C_RxBufferSize )
Line 375... Line 382...
375
                                // should never happen
382
                                // should never happen
376
                                I2C_GenerateSTOP (I2C1, ENABLE);
383
                                I2C_GenerateSTOP (I2C1, ENABLE);
377
                                LED_GRN_OFF;
384
                                LED_GRN_OFF;
378
                                VIC_ITCmd(I2C1_ITLine, DISABLE);
385
                                VIC_ITCmd(I2C1_ITLine, DISABLE);
379
                                I2C_State = I2C_STATE_IDLE;
386
                                I2C_State = I2C_STATE_IDLE;
-
 
387
                                I2C_Error = I2C_ERROR_UNKNOWN;
380
                                break;
388
                                break;
381
                }
389
                }
382
        }
390
        }
383
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
391
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
384
}
392
}
Line 401... Line 409...
401
u8 I2C_LockBuffer(u32 timeout)
409
u8 I2C_LockBuffer(u32 timeout)
402
{      
410
{      
403
        if(I2C_WaitForEndOfTransmission(timeout))
411
        if(I2C_WaitForEndOfTransmission(timeout))
404
        {
412
        {
405
                I2C_State = I2C_STATE_BUFFBUSY;
413
                I2C_State = I2C_STATE_BUFFBUSY;
-
 
414
                I2C_Error = I2C_ERROR_UNKNOWN;
406
                return(1);
415
                return(1);
407
        }
416
        }
408
        else return(0);
417
        else return(0);
409
}
418
}
410
// ----------------------------------------------------------------------------------------
419
// ----------------------------------------------------------------------------------------
Line 425... Line 434...
425
                if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER;
434
                if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER;
426
                else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER;
435
                else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER;
427
                else // nothing to send or receive
436
                else // nothing to send or receive
428
                {
437
                {
429
                        I2C_State = I2C_STATE_IDLE;
438
                        I2C_State = I2C_STATE_IDLE;
-
 
439
                        I2C_Error = I2C_ERROR_NONE;
430
                        I2C_TxBufferSize = 0;
440
                        I2C_TxBufferSize = 0;
431
                        I2C_RxBufferSize = 0;
441
                        I2C_RxBufferSize = 0;
432
                        return(retval);
442
                        return(retval);
433
                }
443
                }
434
                // update slave address and rx data handler     funbction pointer
444
                // update slave address and rx data handler     funbction pointer