Subversion Repositories NaviCtrl

Rev

Rev 465 | Rev 470 | 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
 
469 killagreg 107
        // configure P2.0->I2C0_CLKOUT and P2.1->I2C0_DOUT to normal port operation
464 ingob 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;
469 killagreg 118
 
464 ingob 119
        // set SCL high and then SDA to low (start condition)
120
        GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_SET);
121
        delay = SetDelay(1);
122
        while (!CheckDelay(delay));
123
        GPIO_WriteBit(GPIO2, GPIO_Pin_1, Bit_RESET);
124
        // toggle SCL at least 10 times from high to low to high
125
        for(i = 0; i < 10; i++)
126
        {
127
                delay = SetDelay(1);
128
                while (!CheckDelay(delay));
129
 
130
                GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_RESET);
131
                delay = SetDelay(1);
132
                while (!CheckDelay(delay));
133
                GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_SET);
134
        }
135
        delay = SetDelay(1);
136
        while (!CheckDelay(delay));
137
        // create stop condition setting SDA HIGH when SCL is HIGH
138
        GPIO_WriteBit(GPIO2, GPIO_Pin_1, Bit_SET);
139
 
140
 
469 killagreg 141
        // reconfigure P2.0->I2C0_CLKOUT and P2.1->I2C0_DOUT
464 ingob 142
        GPIO_StructInit(&GPIO_InitStructure);
143
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
144
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
145
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
146
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
147
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C0_CLKOUT, I2C0_DOUT
148
        GPIO_Init(GPIO2, &GPIO_InitStructure);
149
 
150
        // enable I2C peripherie
151
        SCU_APBPeriphClockConfig(__I2C0,ENABLE);
152
        // reset I2C peripherie
153
        SCU_APBPeriphReset(__I2C0,ENABLE);
154
        SCU_APBPeriphReset(__I2C0,DISABLE);
155
 
156
        I2C_DeInit(I2C0);
157
        I2C_StructInit(&I2C_Struct);
158
        I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
159
        I2C_Struct.I2C_Ack = I2C_Ack_Enable;
465 ingob 160
        I2C_Struct.I2C_CLKSpeed = I2C0_CLOCK;
464 ingob 161
        I2C_Struct.I2C_OwnAddress = 0x00;
162
        I2C_Init(I2C0, &I2C_Struct);
163
 
164
        // empty rx and tx buffer counters
165
        I2C0_TxBufferSize = 0;
166
        I2C0_RxBufferSize = 0;
167
 
168
        I2C_Cmd(I2C0, ENABLE);
169
        I2C_ITConfig(I2C0, ENABLE);
170
 
171
        VIC_Config(I2C0_ITLine, VIC_IRQ , PRIORITY_I2C0);
172
 
469 killagreg 173
        I2C0_Timeout = SetDelay(10*I2C1_TIMEOUT);
464 ingob 174
        I2C_GenerateSTOP(I2C0, ENABLE);
175
        I2C0_State = I2C_STATE_IDLE;
176
 
177
        // start some dummy transmissions cycles
178
        // to get the irq routine to work
179
        for(i=0;i<10;i++)
180
        {
181
                I2C0_State = I2C_STATE_BUFFBUSY;
182
                I2C0_Transmission(0,1,0,1);
183
                if(I2C0_WaitForEndOfTransmission(10)) break;
184
                UART1_Putchar('.');
185
        }
186
        UART1_PutString("ok");
187
}
188
 
189
 
190
//--------------------------------------------------------------
191
void I2C0_Deinit(void)
192
{
193
        GPIO_InitTypeDef  GPIO_InitStructure;
465 ingob 194
        UART1_PutString("\r\n I2C0 deinit...");
464 ingob 195
        I2C_GenerateStart(I2C0, DISABLE);
196
        I2C_GenerateSTOP(I2C0, ENABLE);
197
        VIC_ITCmd(I2C0_ITLine, DISABLE);
198
        I2C0_State = I2C_STATE_OFF;
199
        I2C_ITConfig(I2C0, DISABLE);
200
        I2C_Cmd(I2C0, DISABLE);
201
        I2C_DeInit(I2C0);
202
        SCU_APBPeriphClockConfig(__I2C0, DISABLE);
203
 
204
        // set ports to input
205
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
206
        GPIO_StructInit(&GPIO_InitStructure);
207
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
208
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_0 | GPIO_Pin_1;
209
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
210
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
211
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
212
        GPIO_Init(GPIO2, &GPIO_InitStructure);
213
 
214
        // empty rx and tx buffer
215
        I2C0_TxBufferSize = 0;
216
        I2C0_RxBufferSize = 0;
217
 
469 killagreg 218
        I2C0_Timeout = SetDelay(10*I2C0_TIMEOUT);
464 ingob 219
 
220
        UART1_PutString("ok");
221
}
222
 
223
//--------------------------------------------------------------
224
void I2C0_IRQHandler(void)
225
{
226
        static u8 Rx_Idx = 0, Tx_Idx = 0;
227
        u16 status;
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
249
                switch(status)
250
                {
251
                        // the start condition was initiated on the bus
252
                        case I2C_EVENT_MASTER_MODE_SELECT:
253
                                // update current bus state variable
254
                                // jump to rx state if there is nothing to send
255
                                switch(I2C0_Direction)
256
                                {
257
                                        case I2C_MODE_TRANSMITTER:
258
                                                I2C0_State = I2C_STATE_TX_PROGRESS;
259
                                                break;
260
 
261
                                        case I2C_MODE_RECEIVER:
262
                                                if (I2C0_RxBufferSize == 0) // nothing to send?
263
                                                {
264
                                                        I2C_GenerateSTOP (I2C0, ENABLE);
265
                                                        VIC_ITCmd(I2C0_ITLine, DISABLE);
266
                                                        I2C0_State = I2C_STATE_IDLE;
267
                                                        I2C0_Error = I2C_ERROR_NONE;
268
                                                        return;
269
                                                }
270
                                                else
271
                                                {
272
                                                        I2C0_State = I2C_STATE_RX_PROGRESS;
273
                                                }
274
                                                break;
275
 
276
                                        default: // invalid direction
277
                                                I2C_GenerateSTOP (I2C0, ENABLE);
278
                                                VIC_ITCmd(I2C0_ITLine, DISABLE);
279
                                                I2C0_State = I2C_STATE_IDLE;
280
                                                I2C0_Error = I2C_ERROR_UNKNOWN;
281
                                                return;
282
                                }
283
                                // enable acknowledge
284
                                I2C_AcknowledgeConfig (I2C0, ENABLE);
285
                                // send address/direction byte on the bus
286
                                I2C_Send7bitAddress(I2C0, I2C0_SlaveAddress, I2C0_Direction);
287
                                break;
288
 
289
                        // the address byte was send
290
                        case I2C_EVENT_MASTER_MODE_SELECTED:
291
                                // Clear EV6 by set again the PE bit
292
                                I2C_Cmd(I2C0, ENABLE);
293
                                switch(I2C0_State)
294
                                {
295
                                        case I2C_STATE_TX_PROGRESS:
296
                                        // send 1st data byte
297
                                        Tx_Idx = 0;
298
                                        I2C_SendData(I2C0, I2C0_Buffer[Tx_Idx]);
299
                                        Tx_Idx++;
300
                                        // reset timeout
465 ingob 301
                                        I2C0_Timeout = SetDelay(I2C0_TIMEOUT); // after inactivity the I2C0 bus will be reset
464 ingob 302
                                        break;
303
 
304
                                        case I2C_STATE_RX_PROGRESS:
305
                                        Rx_Idx = 0;
306
                                        // disable acknoledge if only one byte has to be read
307
                                        if(I2C0_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C0, DISABLE);              
308
                                        break;
309
 
310
                                        default: // unknown I2C state
311
                                        // should never happen
312
                                        I2C_GenerateSTOP (I2C0, ENABLE);
313
                                        VIC_ITCmd(I2C0_ITLine, DISABLE);
314
                                        I2C0_State = I2C_STATE_IDLE;
315
                                        I2C0_Error = I2C_ERROR_UNKNOWN;
316
                                        return;
317
                                        break;
318
                                }
319
                                break;
320
 
321
                        // the master has transmitted a byte and slave has been acknowledged
322
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
323
 
324
                                // some bytes have to be transmitted
325
                                if(Tx_Idx < I2C0_TxBufferSize)
326
                                {
327
                                        I2C_SendData(I2C0, I2C0_Buffer[Tx_Idx]);
328
                                        Tx_Idx++;
329
                                }
330
                                else // last byte was send
331
                                {
332
                                        // generate stop or repeated start condition
333
                                        if (I2C0_RxBufferSize > 0) // is any answer byte expected?
334
                                        {
335
                                                I2C0_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
336
                                                I2C_GenerateStart(I2C0, ENABLE);   // initiate repeated start condition on the bus
337
                                        }
338
                                        else
339
                                        {   // stop communication
340
                                                I2C_GenerateSTOP(I2C0, ENABLE); // generate stop condition to free the bus
341
                                                VIC_ITCmd(I2C0_ITLine, DISABLE);
342
                                                I2C0_State = I2C_STATE_IDLE;                    // ready for new actions
343
                                                I2C0_Error = I2C_ERROR_NONE;   
344
                                        }
345
                                }
346
                                break;
347
 
348
                        // the master has received a byte from the slave
349
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
350
                                // some bytes have to be received
351
                                if ( Rx_Idx+1 < I2C0_RxBufferSize)
352
                                {       // copy received byte  from the data register to the rx-buffer
353
                                        I2C0_Buffer[Rx_Idx] = I2C_ReceiveData(I2C0);
354
                                }
355
                                else // if the last byte was received
356
                                {
357
                                        // generate a STOP condition on the bus before reading data register
358
                                        I2C_GenerateSTOP(I2C0, ENABLE);
359
                                        I2C0_Buffer[Rx_Idx] = I2C_ReceiveData(I2C0);
360
                                        // call the rx handler function to process recieved data
361
                                        if(I2C0_pRxHandler != NULL) (*I2C0_pRxHandler)((u8*)I2C0_Buffer, I2C0_RxBufferSize);
465 ingob 362
                                        I2C0_Timeout = SetDelay(I2C0_TIMEOUT);
464 ingob 363
                                        DebugOut.Analog[15]++;
364
                                        VIC_ITCmd(I2C0_ITLine, DISABLE);
365
                                        I2C0_State = I2C_STATE_IDLE;
366
                                        I2C0_Error = I2C_ERROR_NONE;
367
                                        return;
368
                                }
369
                                Rx_Idx++;
370
                                // if the 2nd last byte was received disable acknowledge for the last one
371
                                if ( (Rx_Idx + 1) == I2C0_RxBufferSize )
372
                                {
373
                                        I2C_AcknowledgeConfig(I2C0, DISABLE);
374
                                }
375
                                break;
376
 
377
                        default:// unknown event
378
                                // should never happen
379
                                I2C_GenerateSTOP (I2C0, ENABLE);
380
                                VIC_ITCmd(I2C0_ITLine, DISABLE);
381
                                I2C0_State = I2C_STATE_IDLE;
382
                                I2C0_Error = I2C_ERROR_UNKNOWN;
383
                                break;
384
                }
385
        }
386
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
469 killagreg 387
        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
464 ingob 388
}
389
 
390
// ----------------------------------------------------------------------------------------
391
// wait for end of transmission
392
u8 I2C0_WaitForEndOfTransmission(u32 timeout)
393
{
394
        u32 time = SetDelay(timeout);
395
        while(I2C0_State != I2C_STATE_IDLE)
396
        {
397
                if(CheckDelay(time)) return(0);
398
        }
399
        return(1);
400
}
401
 
402
// ----------------------------------------------------------------------------------------
403
// try to get access to the transfer buffer      within a timeout limit
404
// returs 1 on success and 0 on error/timeout
405
u8 I2C0_LockBuffer(u32 timeout)
406
{      
407
        if(I2C0_WaitForEndOfTransmission(timeout))
408
        {
409
                I2C0_State = I2C_STATE_BUFFBUSY;
410
                I2C0_Error = I2C_ERROR_UNKNOWN;
411
                return(1);
412
        }
413
        else return(0);
414
}
415
// ----------------------------------------------------------------------------------------
416
// initate an i2c transmission
417
u8 I2C0_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes)
418
{
419
        u8 retval = 0;
420
        if(I2C0_State == I2C_STATE_BUFFBUSY)
421
        {
422
                if((RxBytes > I2C0_Buffer_LEN) || (TxBytes > I2C0_Buffer_LEN))
423
                {
424
                        I2C0_State = I2C_STATE_IDLE;
425
                        return(retval);
426
                }      
427
                I2C0_RxBufferSize = RxBytes;
428
                I2C0_TxBufferSize = TxBytes;
429
                // set direction to master transmitter
430
                if( (I2C0_TxBufferSize > 0) && (I2C0_TxBufferSize < I2C0_Buffer_LEN) ) I2C0_Direction = I2C_MODE_TRANSMITTER;
431
                else if (( I2C0_RxBufferSize > 0 ) && (I2C0_RxBufferSize < I2C0_Buffer_LEN) ) I2C0_Direction = I2C_MODE_RECEIVER;
432
                else // nothing to send or receive
433
                {
434
                        I2C0_State = I2C_STATE_IDLE;
435
                        I2C0_Error = I2C_ERROR_NONE;
436
                        I2C0_TxBufferSize = 0;
437
                        I2C0_RxBufferSize = 0;
438
                        return(retval);
439
                }
440
                // update slave address and rx data handler     funbction pointer
441
                I2C0_SlaveAddress = SlaveAddr;
442
                I2C0_pRxHandler = pRxHandler;
443
                // test on busy flag and clear it
469 killagreg 444
                I2C_ClearFlag(I2C0, I2C_FLAG_BUSY);
464 ingob 445
                // enable I2C IRQ
446
                VIC_ITCmd(I2C0_ITLine, ENABLE);
447
                // initiate start condition on the bus
448
                I2C_GenerateStart(I2C0, ENABLE);
449
                retval = 1;
450
         }
451
         return(retval);
452
}