Subversion Repositories NaviCtrl

Rev

Rev 464 | Rev 469 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
464 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + www.MikroKopter.com
6
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7
// + Software Nutzungsbedingungen (english version: see below)
8
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
9
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
10
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool 
11
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
12
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
13
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
15
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
16
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
17
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
18
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
19
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
20
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
21
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand 
22
// + des Mitverschuldens offen.
23
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
24
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
25
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
26
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
27
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
28
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
29
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
30
// +  Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
31
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// + Software LICENSING TERMS
33
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
35
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware 
36
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
37
// + The Software may only be used with the Licensor's products.
38
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
39
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
40
// + agreement shall be the property of the Licensor.
41
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
42
// + features that can be used to identify the program may not be altered or defaced by the customer.
43
// + The customer shall be responsible for taking reasonable precautions
44
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
45
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
46
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
47
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
48
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
49
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
50
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
51
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
52
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
53
// + #### END OF LICENSING TERMS ####
54
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
55
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
#include <string.h>
57
#include "91x_lib.h"
58
#include "i2c0.h"
59
#include "i2c.h"
60
#include "uart1.h"
61
#include "timer1.h"
62
#include "config.h"
63
#include "led.h"
64
 
65
 
66
volatile u8 I2C0_State = I2C_STATE_OFF;                 // only one byte, because of sync by nesting irqs
67
volatile u8 I2C0_Error = I2C_ERROR_NOACK;    // only one byte!
68
 
69
// number of bytes to send
70
volatile u8 I2C0_TxBufferSize;
71
// number of bytes to receive
72
volatile u8 I2C0_RxBufferSize;
73
// the transfer buffer
74
volatile u8 I2C0_Buffer[I2C0_Buffer_LEN];
75
// the transfer direction 
76
volatile u8 I2C0_Direction;
77
// the slave address
78
volatile u8 I2C0_SlaveAddress = 0x00;
79
// function pointer to process the received bytes
80
I2C_pRxHandler_t I2C0_pRxHandler = NULL;
81
// goblal timeout 
82
volatile u32 I2C0_Timeout = 0;
83
 
84
//--------------------------------------------------------------
85
void I2C0_Init(void)
86
{
87
        I2C_InitTypeDef   I2C_Struct;
88
        GPIO_InitTypeDef  GPIO_InitStructure;
89
 
90
        I2C0_State = I2C_STATE_OFF;
91
 
92
        UART1_PutString("\r\n I2C0 init...");
93
        // enable Port 2 peripherie
94
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
95
        // disable a reset state
96
        SCU_APBPeriphReset(__GPIO2, DISABLE);
97
 
98
        // free a busy bus
99
 
100
        // At switch on I2C devices can get in a state where they
101
        // are still waiting for a command due to all the bus lines bouncing
102
        // around at startup have started clocking data into the device(s).
103
        // Enable the ports as open collector port outputs
104
        // and clock out at least 9 SCL pulses, then generate a stop
105
        // condition and then leave the clock line high.
106
 
107
        // configure P2.2->I2C0_CLKOUT and P2.3->I2C0_DOUT to normal port operation
108
        GPIO_StructInit(&GPIO_InitStructure);
109
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
465 ingob 110
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
464 ingob 111
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
112
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
113
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
114
        GPIO_Init(GPIO2, &GPIO_InitStructure);
115
 
116
        u8 i;
117
        u32 delay;
118
        // set SCL high and then SDA to low (start condition)
119
        GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_SET);
120
        delay = SetDelay(1);
121
        while (!CheckDelay(delay));
122
        GPIO_WriteBit(GPIO2, GPIO_Pin_1, Bit_RESET);
123
        // toggle SCL at least 10 times from high to low to high
124
        for(i = 0; i < 10; i++)
125
        {
126
                delay = SetDelay(1);
127
                while (!CheckDelay(delay));
128
 
129
                GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_RESET);
130
                delay = SetDelay(1);
131
                while (!CheckDelay(delay));
132
                GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_SET);
133
        }
134
        delay = SetDelay(1);
135
        while (!CheckDelay(delay));
136
        // create stop condition setting SDA HIGH when SCL is HIGH
137
        GPIO_WriteBit(GPIO2, GPIO_Pin_1, Bit_SET);
138
 
139
 
140
        // reconfigure P2.2->I2C0_CLKOUT and P2.3->I2C0_DOUT
141
        GPIO_StructInit(&GPIO_InitStructure);
142
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
143
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
144
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
145
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
146
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C0_CLKOUT, I2C0_DOUT
147
        GPIO_Init(GPIO2, &GPIO_InitStructure);
148
 
149
        // enable I2C peripherie
150
        SCU_APBPeriphClockConfig(__I2C0,ENABLE);
151
        // reset I2C peripherie
152
        SCU_APBPeriphReset(__I2C0,ENABLE);
153
        SCU_APBPeriphReset(__I2C0,DISABLE);
154
 
155
        I2C_DeInit(I2C0);
156
        I2C_StructInit(&I2C_Struct);
157
        I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
158
        I2C_Struct.I2C_Ack = I2C_Ack_Enable;
465 ingob 159
        I2C_Struct.I2C_CLKSpeed = I2C0_CLOCK;
464 ingob 160
        I2C_Struct.I2C_OwnAddress = 0x00;
161
        I2C_Init(I2C0, &I2C_Struct);
162
 
163
        // empty rx and tx buffer counters
164
        I2C0_TxBufferSize = 0;
165
        I2C0_RxBufferSize = 0;
166
 
167
        I2C_Cmd(I2C0, ENABLE);
168
        I2C_ITConfig(I2C0, ENABLE);
169
 
170
        VIC_Config(I2C0_ITLine, VIC_IRQ , PRIORITY_I2C0);
171
 
172
        I2C0_Timeout = SetDelay(10*I2C0_Timeout);
173
        I2C_GenerateSTOP(I2C0, ENABLE);
174
        I2C0_State = I2C_STATE_IDLE;
175
 
176
        // start some dummy transmissions cycles
177
        // to get the irq routine to work
178
        for(i=0;i<10;i++)
179
        {
180
                I2C0_State = I2C_STATE_BUFFBUSY;
181
                I2C0_Transmission(0,1,0,1);
182
                if(I2C0_WaitForEndOfTransmission(10)) break;
183
                UART1_Putchar('.');
184
        }
185
        UART1_PutString("ok");
186
}
187
 
188
 
189
//--------------------------------------------------------------
190
void I2C0_Deinit(void)
191
{
192
        GPIO_InitTypeDef  GPIO_InitStructure;
465 ingob 193
        UART1_PutString("\r\n I2C0 deinit...");
464 ingob 194
        I2C_GenerateStart(I2C0, DISABLE);
195
        I2C_GenerateSTOP(I2C0, ENABLE);
196
        VIC_ITCmd(I2C0_ITLine, DISABLE);
197
        I2C0_State = I2C_STATE_OFF;
198
        I2C_ITConfig(I2C0, DISABLE);
199
        I2C_Cmd(I2C0, DISABLE);
200
        I2C_DeInit(I2C0);
201
        SCU_APBPeriphClockConfig(__I2C0, DISABLE);
202
 
203
        // set ports to input
204
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
205
        GPIO_StructInit(&GPIO_InitStructure);
206
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
207
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_0 | GPIO_Pin_1;
208
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
209
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
210
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
211
        GPIO_Init(GPIO2, &GPIO_InitStructure);
212
 
213
        // empty rx and tx buffer
214
        I2C0_TxBufferSize = 0;
215
        I2C0_RxBufferSize = 0;
216
 
217
        I2C0_Timeout = SetDelay(10*I2C0_Timeout);
218
 
219
        UART1_PutString("ok");
220
}
221
 
222
//--------------------------------------------------------------
223
void I2C0_IRQHandler(void)
224
{
225
        static u8 Rx_Idx = 0, Tx_Idx = 0;
226
        u16 status;
465 ingob 227
        u8 text[20];
464 ingob 228
 
465 ingob 229
 
230
 
464 ingob 231
        //IENABLE;  // do not enable IRQ nesting for I2C!!!!
232
        // detemine I2C State
233
        status = I2C_GetLastEvent(I2C0);
234
 
235
        if(status & (I2C_FLAG_AF|I2C_FLAG_BERR))  // if an acknowledge failure or bus error occured
236
        {       // Set and subsequently clear the STOP bit while BTF is set.
237
                while(I2C_GetFlagStatus (I2C0, I2C_FLAG_BTF) != RESET)
238
                {
239
                        I2C_GenerateSTOP (I2C0, ENABLE);  // free the bus
240
                        I2C_GenerateSTOP (I2C0, DISABLE); // free the bus
241
                }
242
                I2C0_State = I2C_STATE_IDLE;
243
                I2C0_Error = I2C_ERROR_NOACK;
244
                VIC_ITCmd(I2C0_ITLine, DISABLE);
245
                return;
246
        }
247
        else
248
        {       // depending on current i2c state
465 ingob 249
 
250
                        sprintf(text, "%02X ", status);
251
                        UART1_PutString(text);
464 ingob 252
                switch(status)
253
                {
254
                        // the start condition was initiated on the bus
255
                        case I2C_EVENT_MASTER_MODE_SELECT:
256
                                // update current bus state variable
257
                                // jump to rx state if there is nothing to send
258
                                switch(I2C0_Direction)
259
                                {
260
                                        case I2C_MODE_TRANSMITTER:
261
                                                I2C0_State = I2C_STATE_TX_PROGRESS;
262
                                                break;
263
 
264
                                        case I2C_MODE_RECEIVER:
265
                                                if (I2C0_RxBufferSize == 0) // nothing to send?
266
                                                {
267
                                                        I2C_GenerateSTOP (I2C0, ENABLE);
268
                                                        VIC_ITCmd(I2C0_ITLine, DISABLE);
269
                                                        I2C0_State = I2C_STATE_IDLE;
270
                                                        I2C0_Error = I2C_ERROR_NONE;
271
                                                        return;
272
                                                }
273
                                                else
274
                                                {
275
                                                        I2C0_State = I2C_STATE_RX_PROGRESS;
276
                                                }
277
                                                break;
278
 
279
                                        default: // invalid direction
280
                                                I2C_GenerateSTOP (I2C0, ENABLE);
281
                                                VIC_ITCmd(I2C0_ITLine, DISABLE);
282
                                                I2C0_State = I2C_STATE_IDLE;
283
                                                I2C0_Error = I2C_ERROR_UNKNOWN;
284
                                                return;
285
                                }
286
                                // enable acknowledge
287
                                I2C_AcknowledgeConfig (I2C0, ENABLE);
288
                                // send address/direction byte on the bus
289
                                I2C_Send7bitAddress(I2C0, I2C0_SlaveAddress, I2C0_Direction);
290
                                break;
291
 
292
                        // the address byte was send
293
                        case I2C_EVENT_MASTER_MODE_SELECTED:
294
                                // Clear EV6 by set again the PE bit
295
                                I2C_Cmd(I2C0, ENABLE);
296
                                switch(I2C0_State)
297
                                {
298
                                        case I2C_STATE_TX_PROGRESS:
299
                                        // send 1st data byte
300
                                        Tx_Idx = 0;
301
                                        I2C_SendData(I2C0, I2C0_Buffer[Tx_Idx]);
302
                                        Tx_Idx++;
303
                                        // reset timeout
465 ingob 304
                                        I2C0_Timeout = SetDelay(I2C0_TIMEOUT); // after inactivity the I2C0 bus will be reset
464 ingob 305
                                        break;
306
 
307
                                        case I2C_STATE_RX_PROGRESS:
308
                                        Rx_Idx = 0;
309
                                        // disable acknoledge if only one byte has to be read
310
                                        if(I2C0_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C0, DISABLE);              
311
                                        break;
312
 
313
                                        default: // unknown I2C state
314
                                        // should never happen
315
                                        I2C_GenerateSTOP (I2C0, ENABLE);
316
                                        VIC_ITCmd(I2C0_ITLine, DISABLE);
317
                                        I2C0_State = I2C_STATE_IDLE;
318
                                        I2C0_Error = I2C_ERROR_UNKNOWN;
319
                                        return;
320
                                        break;
321
                                }
322
                                break;
323
 
324
                        // the master has transmitted a byte and slave has been acknowledged
325
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
326
 
327
                                // some bytes have to be transmitted
328
                                if(Tx_Idx < I2C0_TxBufferSize)
329
                                {
330
                                        I2C_SendData(I2C0, I2C0_Buffer[Tx_Idx]);
331
                                        Tx_Idx++;
332
                                }
333
                                else // last byte was send
334
                                {
335
                                        // generate stop or repeated start condition
336
                                        if (I2C0_RxBufferSize > 0) // is any answer byte expected?
337
                                        {
338
                                                I2C0_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
339
                                                I2C_GenerateStart(I2C0, ENABLE);   // initiate repeated start condition on the bus
340
                                        }
341
                                        else
342
                                        {   // stop communication
343
                                                I2C_GenerateSTOP(I2C0, ENABLE); // generate stop condition to free the bus
344
                                                VIC_ITCmd(I2C0_ITLine, DISABLE);
345
                                                I2C0_State = I2C_STATE_IDLE;                    // ready for new actions
346
                                                I2C0_Error = I2C_ERROR_NONE;   
347
                                        }
348
                                }
349
                                break;
350
 
351
                        // the master has received a byte from the slave
352
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
353
                                // some bytes have to be received
354
                                if ( Rx_Idx+1 < I2C0_RxBufferSize)
355
                                {       // copy received byte  from the data register to the rx-buffer
356
                                        I2C0_Buffer[Rx_Idx] = I2C_ReceiveData(I2C0);
357
                                }
358
                                else // if the last byte was received
359
                                {
360
                                        // generate a STOP condition on the bus before reading data register
361
                                        I2C_GenerateSTOP(I2C0, ENABLE);
362
                                        I2C0_Buffer[Rx_Idx] = I2C_ReceiveData(I2C0);
363
                                        // call the rx handler function to process recieved data
364
                                        if(I2C0_pRxHandler != NULL) (*I2C0_pRxHandler)((u8*)I2C0_Buffer, I2C0_RxBufferSize);
465 ingob 365
                                        I2C0_Timeout = SetDelay(I2C0_TIMEOUT);
464 ingob 366
                                        DebugOut.Analog[15]++;
367
                                        VIC_ITCmd(I2C0_ITLine, DISABLE);
368
                                        I2C0_State = I2C_STATE_IDLE;
369
                                        I2C0_Error = I2C_ERROR_NONE;
370
                                        return;
371
                                }
372
                                Rx_Idx++;
373
                                // if the 2nd last byte was received disable acknowledge for the last one
374
                                if ( (Rx_Idx + 1) == I2C0_RxBufferSize )
375
                                {
376
                                        I2C_AcknowledgeConfig(I2C0, DISABLE);
377
                                }
378
                                break;
379
 
380
                        default:// unknown event
381
                                // should never happen
382
                                I2C_GenerateSTOP (I2C0, ENABLE);
383
                                VIC_ITCmd(I2C0_ITLine, DISABLE);
384
                                I2C0_State = I2C_STATE_IDLE;
385
                                I2C0_Error = I2C_ERROR_UNKNOWN;
386
                                break;
387
                }
388
        }
389
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
390
        VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register
391
}
392
 
393
// ----------------------------------------------------------------------------------------
394
// wait for end of transmission
395
u8 I2C0_WaitForEndOfTransmission(u32 timeout)
396
{
397
        u32 time = SetDelay(timeout);
398
        while(I2C0_State != I2C_STATE_IDLE)
399
        {
400
                if(CheckDelay(time)) return(0);
401
        }
402
        return(1);
403
}
404
 
405
// ----------------------------------------------------------------------------------------
406
// try to get access to the transfer buffer      within a timeout limit
407
// returs 1 on success and 0 on error/timeout
408
u8 I2C0_LockBuffer(u32 timeout)
409
{      
410
        if(I2C0_WaitForEndOfTransmission(timeout))
411
        {
412
                I2C0_State = I2C_STATE_BUFFBUSY;
413
                I2C0_Error = I2C_ERROR_UNKNOWN;
414
                return(1);
415
        }
416
        else return(0);
417
}
418
// ----------------------------------------------------------------------------------------
419
// initate an i2c transmission
420
u8 I2C0_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes)
421
{
422
        u8 retval = 0;
423
        if(I2C0_State == I2C_STATE_BUFFBUSY)
424
        {
425
                if((RxBytes > I2C0_Buffer_LEN) || (TxBytes > I2C0_Buffer_LEN))
426
                {
427
                        I2C0_State = I2C_STATE_IDLE;
428
                        return(retval);
429
                }      
430
                I2C0_RxBufferSize = RxBytes;
431
                I2C0_TxBufferSize = TxBytes;
432
                // set direction to master transmitter
433
                if( (I2C0_TxBufferSize > 0) && (I2C0_TxBufferSize < I2C0_Buffer_LEN) ) I2C0_Direction = I2C_MODE_TRANSMITTER;
434
                else if (( I2C0_RxBufferSize > 0 ) && (I2C0_RxBufferSize < I2C0_Buffer_LEN) ) I2C0_Direction = I2C_MODE_RECEIVER;
435
                else // nothing to send or receive
436
                {
437
                        I2C0_State = I2C_STATE_IDLE;
438
                        I2C0_Error = I2C_ERROR_NONE;
439
                        I2C0_TxBufferSize = 0;
440
                        I2C0_RxBufferSize = 0;
441
                        return(retval);
442
                }
443
                // update slave address and rx data handler     funbction pointer
444
                I2C0_SlaveAddress = SlaveAddr;
445
                I2C0_pRxHandler = pRxHandler;
446
                // test on busy flag and clear it
447
                I2C_CheckEvent( I2C0, I2C_FLAG_BUSY );
448
                // enable I2C IRQ
449
                VIC_ITCmd(I2C0_ITLine, ENABLE);
450
                // initiate start condition on the bus
451
                I2C_GenerateStart(I2C0, ENABLE);
452
                retval = 1;
453
         }
454
         return(retval);
455
}