Subversion Repositories NaviCtrl

Rev

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

Rev 469 Rev 470
Line 53... Line 53...
53
// + #### END OF LICENSING TERMS ####
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
#include <string.h>
56
#include <string.h>
57
#include "91x_lib.h"
57
#include "91x_lib.h"
58
#include "i2c.h"
58
#include "i2c1.h"
59
#include "uart1.h"
59
#include "uart1.h"
60
#include "timer1.h"
60
#include "timer1.h"
61
#include "config.h"
61
#include "config.h"
62
#include "led.h"
62
#include "led.h"
Line 63... Line 63...
63
 
63
 
64
 
64
 
Line 65... Line 65...
65
volatile u8 I2C_State = I2C_STATE_OFF;          // only one byte, because of sync by nesting irqs
65
volatile u8 I2C1_State = I2C_STATE_OFF;          // only one byte, because of sync by nesting irqs
66
volatile u8 I2C_Error = I2C_ERROR_NOACK;    // only one byte!
66
volatile u8 I2C1_Error = I2C_ERROR_NOACK;    // only one byte!
67
 
67
 
68
// number of bytes to send
68
// number of bytes to send
69
volatile u8 I2C_TxBufferSize;
69
volatile u8 I2C1_TxBufferSize;
70
// number of bytes to receive
70
// number of bytes to receive
71
volatile u8 I2C_RxBufferSize;
71
volatile u8 I2C1_RxBufferSize;
72
// the transfer buffer
72
// the transfer buffer
73
volatile u8 I2C_Buffer[I2C_BUFFER_LEN];
73
volatile u8 I2C1_Buffer[I2C1_BUFFER_LEN];
74
// the transfer direction 
74
// the transfer direction 
75
volatile u8 I2C_Direction;
75
volatile u8 I2C1_Direction;
76
// the slave address
76
// the slave address
77
volatile u8 I2C_SlaveAddress = 0x00;
77
volatile u8 I2C1_SlaveAddress = 0x00;
78
// function pointer to process the received bytes
78
// function pointer to process the received bytes
Line 79... Line 79...
79
I2C_pRxHandler_t I2C_pRxHandler = NULL;
79
I2C_pRxHandler_t I2C1_pRxHandler = NULL;
80
// goblal timeout 
80
// goblal timeout 
81
volatile u32 I2C1_Timeout = 0;
81
volatile u32 I2C1_Timeout = 0;
82
 
82
 
83
//--------------------------------------------------------------
83
//--------------------------------------------------------------
Line 84... Line 84...
84
void I2C1_Init(void)
84
void I2C1_Init(void)
Line 85... Line 85...
85
{
85
{
86
        I2C_InitTypeDef   I2C_Struct;
86
        I2C_InitTypeDef   I2C_Struct;
87
        GPIO_InitTypeDef  GPIO_InitStructure;
87
        GPIO_InitTypeDef  GPIO_InitStructure;
88
       
88
       
Line 158... Line 158...
158
        I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK;
158
        I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK;
159
        I2C_Struct.I2C_OwnAddress = 0x00;
159
        I2C_Struct.I2C_OwnAddress = 0x00;
160
        I2C_Init(I2C1, &I2C_Struct);
160
        I2C_Init(I2C1, &I2C_Struct);
Line 161... Line 161...
161
 
161
 
162
        // empty rx and tx buffer counters
162
        // empty rx and tx buffer counters
163
        I2C_TxBufferSize = 0;
163
        I2C1_TxBufferSize = 0;
Line 164... Line 164...
164
        I2C_RxBufferSize = 0;
164
        I2C1_RxBufferSize = 0;
165
 
165
 
Line 166... Line 166...
166
        I2C_Cmd(I2C1, ENABLE);
166
        I2C_Cmd(I2C1, ENABLE);
Line 167... Line 167...
167
        I2C_ITConfig(I2C1, ENABLE);
167
        I2C_ITConfig(I2C1, ENABLE);
168
 
168
 
169
        VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1);
169
        VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1);
Line 170... Line 170...
170
 
170
 
171
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
171
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
172
        I2C_GenerateSTOP(I2C1, ENABLE);
172
        I2C_GenerateSTOP(I2C1, ENABLE);
173
        I2C_State = I2C_STATE_IDLE;
173
        I2C1_State = I2C_STATE_IDLE;
174
 
174
 
175
        // start some dummy transmissions cycles
175
        // start some dummy transmissions cycles
176
        // to get the irq routine to work
176
        // to get the irq routine to work
177
        for(i=0;i<10;i++)
177
        for(i=0;i<10;i++)
178
        {
178
        {
179
                I2C_State = I2C_STATE_BUFFBUSY;
179
                I2C1_State = I2C_STATE_BUFFBUSY;
180
                I2C_Transmission(0,1,0,1);
180
                I2C1_Transmission(0,1,0,1);
Line 191... Line 191...
191
        GPIO_InitTypeDef  GPIO_InitStructure;
191
        GPIO_InitTypeDef  GPIO_InitStructure;
192
        UART1_PutString("\r\n I2C1 deinit...");
192
        UART1_PutString("\r\n I2C1 deinit...");
193
        I2C_GenerateStart(I2C1, DISABLE);
193
        I2C_GenerateStart(I2C1, DISABLE);
194
        I2C_GenerateSTOP(I2C1, ENABLE);
194
        I2C_GenerateSTOP(I2C1, ENABLE);
195
        VIC_ITCmd(I2C1_ITLine, DISABLE);
195
        VIC_ITCmd(I2C1_ITLine, DISABLE);
196
        I2C_State = I2C_STATE_OFF;
196
        I2C1_State = I2C_STATE_OFF;
197
        I2C_ITConfig(I2C1, DISABLE);
197
        I2C_ITConfig(I2C1, DISABLE);
198
        I2C_Cmd(I2C1, DISABLE);
198
        I2C_Cmd(I2C1, DISABLE);
199
        I2C_DeInit(I2C1);
199
        I2C_DeInit(I2C1);
200
        SCU_APBPeriphClockConfig(__I2C1, DISABLE);
200
        SCU_APBPeriphClockConfig(__I2C1, DISABLE);
Line 208... Line 208...
208
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
208
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
209
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
209
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
210
        GPIO_Init(GPIO2, &GPIO_InitStructure);
210
        GPIO_Init(GPIO2, &GPIO_InitStructure);
Line 211... Line 211...
211
 
211
 
212
        // empty rx and tx buffer
212
        // empty rx and tx buffer
213
        I2C_TxBufferSize = 0;
213
        I2C1_TxBufferSize = 0;
Line 214... Line 214...
214
        I2C_RxBufferSize = 0;
214
        I2C1_RxBufferSize = 0;
Line 215... Line 215...
215
 
215
 
216
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
216
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
Line 233... Line 233...
233
                while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
233
                while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
234
                {
234
                {
235
                        I2C_GenerateSTOP (I2C1, ENABLE);  // free the bus
235
                        I2C_GenerateSTOP (I2C1, ENABLE);  // free the bus
236
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
236
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
237
                }
237
                }
238
                I2C_State = I2C_STATE_IDLE;
238
                I2C1_State = I2C_STATE_IDLE;
239
                I2C_Error = I2C_ERROR_NOACK;
239
                I2C1_Error = I2C_ERROR_NOACK;
240
                VIC_ITCmd(I2C1_ITLine, DISABLE);
240
                VIC_ITCmd(I2C1_ITLine, DISABLE);
241
                return;
241
                return;
242
        }
242
        }
243
        else
243
        else
244
        {       // depending on current i2c state
244
        {       // depending on current i2c state
Line 246... Line 246...
246
                {
246
                {
247
                        // the start condition was initiated on the bus
247
                        // the start condition was initiated on the bus
248
                        case I2C_EVENT_MASTER_MODE_SELECT:
248
                        case I2C_EVENT_MASTER_MODE_SELECT:
249
                                // update current bus state variable
249
                                // update current bus state variable
250
                                // jump to rx state if there is nothing to send
250
                                // jump to rx state if there is nothing to send
251
                                switch(I2C_Direction)
251
                                switch(I2C1_Direction)
252
                                {
252
                                {
253
                                        case I2C_MODE_TRANSMITTER:
253
                                        case I2C_MODE_TRANSMITTER:
254
                                                I2C_State = I2C_STATE_TX_PROGRESS;
254
                                                I2C1_State = I2C_STATE_TX_PROGRESS;
255
                                                break;
255
                                                break;
Line 256... Line 256...
256
 
256
 
257
                                        case I2C_MODE_RECEIVER:
257
                                        case I2C_MODE_RECEIVER:
258
                                                if (I2C_RxBufferSize == 0) // nothing to send?
258
                                                if (I2C1_RxBufferSize == 0) // nothing to send?
259
                                                {
259
                                                {
260
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
260
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
261
                                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
261
                                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
262
                                                        I2C_State = I2C_STATE_IDLE;
262
                                                        I2C1_State = I2C_STATE_IDLE;
263
                                                        I2C_Error = I2C_ERROR_NONE;
263
                                                        I2C1_Error = I2C_ERROR_NONE;
264
                                                        return;
264
                                                        return;
265
                                                }
265
                                                }
266
                                                else
266
                                                else
267
                                                {
267
                                                {
268
                                                        I2C_State = I2C_STATE_RX_PROGRESS;
268
                                                        I2C1_State = I2C_STATE_RX_PROGRESS;
269
                                                }
269
                                                }
Line 270... Line 270...
270
                                                break;
270
                                                break;
271
 
271
 
272
                                        default: // invalid direction
272
                                        default: // invalid direction
273
                                                I2C_GenerateSTOP (I2C1, ENABLE);
273
                                                I2C_GenerateSTOP (I2C1, ENABLE);
274
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
274
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
275
                                                I2C_State = I2C_STATE_IDLE;
275
                                                I2C1_State = I2C_STATE_IDLE;
276
                                                I2C_Error = I2C_ERROR_UNKNOWN;
276
                                                I2C1_Error = I2C_ERROR_UNKNOWN;
277
                                                return;
277
                                                return;
278
                                }
278
                                }
279
                                // enable acknowledge
279
                                // enable acknowledge
280
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
280
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
281
                                // send address/direction byte on the bus
281
                                // send address/direction byte on the bus
Line 282... Line 282...
282
                                I2C_Send7bitAddress(I2C1, I2C_SlaveAddress, I2C_Direction);
282
                                I2C_Send7bitAddress(I2C1, I2C1_SlaveAddress, I2C1_Direction);
283
                                break;
283
                                break;
284
 
284
 
285
                        // the address byte was send
285
                        // the address byte was send
286
                        case I2C_EVENT_MASTER_MODE_SELECTED:
286
                        case I2C_EVENT_MASTER_MODE_SELECTED:
287
                                // Clear EV6 by set again the PE bit
287
                                // Clear EV6 by set again the PE bit
288
                                I2C_Cmd(I2C1, ENABLE);
288
                                I2C_Cmd(I2C1, ENABLE);
289
                                switch(I2C_State)
289
                                switch(I2C1_State)
290
                                {
290
                                {
291
                                        case I2C_STATE_TX_PROGRESS:
291
                                        case I2C_STATE_TX_PROGRESS:
292
                                        // send 1st data byte
292
                                        // send 1st data byte
293
                                        Tx_Idx = 0;
293
                                        Tx_Idx = 0;
294
                                        I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]);
294
                                        I2C_SendData(I2C1, I2C1_Buffer[Tx_Idx]);
295
                                        Tx_Idx++;
295
                                        Tx_Idx++;
Line 296... Line 296...
296
                                        // reset timeout
296
                                        // reset timeout
297
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset
297
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset
298
                                        break;
298
                                        break;
299
 
299
 
300
                                        case I2C_STATE_RX_PROGRESS:
300
                                        case I2C_STATE_RX_PROGRESS:
Line 301... Line 301...
301
                                        Rx_Idx = 0;
301
                                        Rx_Idx = 0;
302
                                        // disable acknoledge if only one byte has to be read
302
                                        // disable acknoledge if only one byte has to be read
303
                                        if(I2C_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C1, DISABLE);               
303
                                        if(I2C1_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C1, DISABLE);              
304
                                        break;
304
                                        break;
305
 
305
 
306
                                        default: // unknown I2C state
306
                                        default: // unknown I2C state
307
                                        // should never happen
307
                                        // should never happen
308
                                        I2C_GenerateSTOP (I2C1, ENABLE);
308
                                        I2C_GenerateSTOP (I2C1, ENABLE);
309
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
309
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
310
                                        I2C_State = I2C_STATE_IDLE;
310
                                        I2C1_State = I2C_STATE_IDLE;
Line 311... Line 311...
311
                                        I2C_Error = I2C_ERROR_UNKNOWN;
311
                                        I2C1_Error = I2C_ERROR_UNKNOWN;
312
                                        return;
312
                                        return;
Line 313... Line 313...
313
                                        break;
313
                                        break;
314
                                }
314
                                }
315
                                break;
315
                                break;
316
 
316
 
317
                        // the master has transmitted a byte and slave has been acknowledged
317
                        // the master has transmitted a byte and slave has been acknowledged
318
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
318
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
319
 
319
 
320
                                // some bytes have to be transmitted
320
                                // some bytes have to be transmitted
321
                                if(Tx_Idx < I2C_TxBufferSize)
321
                                if(Tx_Idx < I2C1_TxBufferSize)
322
                                {
322
                                {
323
                                        I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]);
323
                                        I2C_SendData(I2C1, I2C1_Buffer[Tx_Idx]);
324
                                        Tx_Idx++;
324
                                        Tx_Idx++;
325
                                }
325
                                }
326
                                else // last byte was send
326
                                else // last byte was send
327
                                {
327
                                {
328
                                        // generate stop or repeated start condition
328
                                        // generate stop or repeated start condition
329
                                        if (I2C_RxBufferSize > 0) // is any answer byte expected?
329
                                        if (I2C1_RxBufferSize > 0) // is any answer byte expected?
330
                                        {
330
                                        {
331
                                                I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
331
                                                I2C1_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
332
                                                I2C_GenerateStart(I2C1, ENABLE);   // initiate repeated start condition on the bus
332
                                                I2C_GenerateStart(I2C1, ENABLE);   // initiate repeated start condition on the bus
333
                                        }
333
                                        }
334
                                        else
334
                                        else
335
                                        {   // stop communication
335
                                        {   // stop communication
Line 336... Line 336...
336
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
336
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
337
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
337
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
338
                                                I2C_State = I2C_STATE_IDLE;                     // ready for new actions
338
                                                I2C1_State = I2C_STATE_IDLE;                    // ready for new actions
339
                                                I2C_Error = I2C_ERROR_NONE;    
339
                                                I2C1_Error = I2C_ERROR_NONE;   
340
                                        }
340
                                        }
341
                                }
341
                                }
342
                                break;
342
                                break;
343
 
343
 
344
                        // the master has received a byte from the slave
344
                        // the master has received a byte from the slave
345
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
345
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
346
                                // some bytes have to be received
346
                                // some bytes have to be received
347
                                if ( Rx_Idx+1 < I2C_RxBufferSize)
347
                                if ( Rx_Idx+1 < I2C1_RxBufferSize)
348
                                {       // copy received byte  from the data register to the rx-buffer
348
                                {       // copy received byte  from the data register to the rx-buffer
349
                                        I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1);
349
                                        I2C1_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1);
350
                                }
350
                                }
351
                                else // if the last byte was received
351
                                else // if the last byte was received
352
                                {
352
                                {
353
                                        // generate a STOP condition on the bus before reading data register
353
                                        // generate a STOP condition on the bus before reading data register
354
                                        I2C_GenerateSTOP(I2C1, ENABLE);
354
                                        I2C_GenerateSTOP(I2C1, ENABLE);
355
                                        I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1);
355
                                        I2C1_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1);
356
                                        // call the rx handler function to process recieved data
356
                                        // call the rx handler function to process recieved data
357
                                        if(I2C_pRxHandler != NULL) (*I2C_pRxHandler)((u8*)I2C_Buffer, I2C_RxBufferSize);
357
                                        if(I2C1_pRxHandler != NULL) (*I2C1_pRxHandler)((u8*)I2C1_Buffer, I2C1_RxBufferSize);
358
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT);
358
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT);
359
                                        DebugOut.Analog[15]++;
359
                                        DebugOut.Analog[15]++;
360
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
360
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
361
                                        I2C_State = I2C_STATE_IDLE;
361
                                        I2C1_State = I2C_STATE_IDLE;
362
                                        I2C_Error = I2C_ERROR_NONE;
362
                                        I2C1_Error = I2C_ERROR_NONE;
363
                                        return;
363
                                        return;
Line 364... Line 364...
364
                                }
364
                                }
365
                                Rx_Idx++;
365
                                Rx_Idx++;
366
                                // if the 2nd last byte was received disable acknowledge for the last one
366
                                // if the 2nd last byte was received disable acknowledge for the last one
367
                                if ( (Rx_Idx + 1) == I2C_RxBufferSize )
367
                                if ( (Rx_Idx + 1) == I2C1_RxBufferSize )
368
                                {
368
                                {
369
                                        I2C_AcknowledgeConfig(I2C1, DISABLE);
369
                                        I2C_AcknowledgeConfig(I2C1, DISABLE);
370
                                }
370
                                }
371
                                break;
371
                                break;
372
 
372
 
373
                        default:// unknown event
373
                        default:// unknown event
374
                                // should never happen
374
                                // should never happen
375
                                I2C_GenerateSTOP (I2C1, ENABLE);
375
                                I2C_GenerateSTOP (I2C1, ENABLE);
Line 376... Line 376...
376
                                VIC_ITCmd(I2C1_ITLine, DISABLE);
376
                                VIC_ITCmd(I2C1_ITLine, DISABLE);
377
                                I2C_State = I2C_STATE_IDLE;
377
                                I2C1_State = I2C_STATE_IDLE;
378
                                I2C_Error = I2C_ERROR_UNKNOWN;
378
                                I2C1_Error = I2C_ERROR_UNKNOWN;
379
                                break;
379
                                break;
380
                }
380
                }
381
        }
381
        }
382
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
382
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
383
        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
383
        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
384
}
384
}
385
 
385
 
386
// ----------------------------------------------------------------------------------------
386
// ----------------------------------------------------------------------------------------
Line 387... Line 387...
387
// wait for end of transmission
387
// wait for end of transmission
388
u8 I2C_WaitForEndOfTransmission(u32 timeout)
388
u8 I2C1_WaitForEndOfTransmission(u32 timeout)
389
{
389
{
390
        u32 time = SetDelay(timeout);
390
        u32 time = SetDelay(timeout);
391
        while(I2C_State != I2C_STATE_IDLE)
391
        while(I2C1_State != I2C_STATE_IDLE)
392
        {
392
        {
393
                if(CheckDelay(time)) return(0);
393
                if(CheckDelay(time)) return(0);
394
        }
394
        }
395
        return(1);
395
        return(1);
396
}
396
}
397
 
397
 
398
// ----------------------------------------------------------------------------------------
398
// ----------------------------------------------------------------------------------------
399
// try to get access to the transfer buffer      within a timeout limit
399
// try to get access to the transfer buffer      within a timeout limit
400
// returs 1 on success and 0 on error/timeout
400
// returs 1 on success and 0 on error/timeout
401
u8 I2C_LockBuffer(u32 timeout)
401
u8 I2C1_LockBuffer(u32 timeout)
402
{      
402
{      
403
        if(I2C_WaitForEndOfTransmission(timeout))
403
        if(I2C1_WaitForEndOfTransmission(timeout))
404
        {
404
        {
405
                I2C_State = I2C_STATE_BUFFBUSY;
405
                I2C1_State = I2C_STATE_BUFFBUSY;
406
                I2C_Error = I2C_ERROR_UNKNOWN;
406
                I2C1_Error = I2C_ERROR_UNKNOWN;
407
                return(1);
407
                return(1);
408
        }
408
        }
409
        else return(0);
409
        else return(0);
410
}
410
}
411
// ----------------------------------------------------------------------------------------
411
// ----------------------------------------------------------------------------------------
412
// initate an i2c transmission
412
// initate an i2c transmission
413
u8 I2C_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes)
413
u8 I2C1_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes)
414
{
414
{
415
        u8 retval = 0;
415
        u8 retval = 0;
416
        if(I2C_State == I2C_STATE_BUFFBUSY)
416
        if(I2C1_State == I2C_STATE_BUFFBUSY)
417
        {
417
        {
418
                if((RxBytes > I2C_BUFFER_LEN) || (TxBytes > I2C_BUFFER_LEN))
418
                if((RxBytes > I2C1_BUFFER_LEN) || (TxBytes > I2C1_BUFFER_LEN))
419
                {
419
                {
420
                        I2C_State = I2C_STATE_IDLE;
420
                        I2C1_State = I2C_STATE_IDLE;
421
                        return(retval);
421
                        return(retval);
422
                }      
422
                }      
423
                I2C_RxBufferSize = RxBytes;
423
                I2C1_RxBufferSize = RxBytes;
424
                I2C_TxBufferSize = TxBytes;
424
                I2C1_TxBufferSize = TxBytes;
425
                // set direction to master transmitter
425
                // set direction to master transmitter
426
                if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER;
426
                if( (I2C1_TxBufferSize > 0) && (I2C1_TxBufferSize < I2C1_BUFFER_LEN) ) I2C1_Direction = I2C_MODE_TRANSMITTER;
427
                else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER;
427
                else if (( I2C1_RxBufferSize > 0 ) && (I2C1_RxBufferSize < I2C1_BUFFER_LEN) ) I2C1_Direction = I2C_MODE_RECEIVER;
428
                else // nothing to send or receive
428
                else // nothing to send or receive
429
                {
429
                {
430
                        I2C_State = I2C_STATE_IDLE;
430
                        I2C1_State = I2C_STATE_IDLE;
431
                        I2C_Error = I2C_ERROR_NONE;
431
                        I2C1_Error = I2C_ERROR_NONE;
432
                        I2C_TxBufferSize = 0;
432
                        I2C1_TxBufferSize = 0;