Subversion Repositories NaviCtrl

Rev

Rev 473 | Rev 480 | 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"
470 killagreg 59
#include "i2c1.h"
464 ingob 60
#include "uart1.h"
61
#include "timer1.h"
62
#include "config.h"
63
#include "led.h"
473 holgerb 64
#include "ncmag.h"
464 ingob 65
 
66
 
67
volatile u8 I2C0_State = I2C_STATE_OFF;                 // only one byte, because of sync by nesting irqs
68
volatile u8 I2C0_Error = I2C_ERROR_NOACK;    // only one byte!
69
 
70
// number of bytes to send
71
volatile u8 I2C0_TxBufferSize;
72
// number of bytes to receive
73
volatile u8 I2C0_RxBufferSize;
74
// the transfer buffer
470 killagreg 75
volatile u8 I2C0_Buffer[I2C0_BUFFER_LEN];
464 ingob 76
// the transfer direction 
77
volatile u8 I2C0_Direction;
78
// the slave address
79
volatile u8 I2C0_SlaveAddress = 0x00;
80
// function pointer to process the received bytes
81
I2C_pRxHandler_t I2C0_pRxHandler = NULL;
82
// goblal timeout 
83
volatile u32 I2C0_Timeout = 0;
84
 
85
//--------------------------------------------------------------
86
void I2C0_Init(void)
87
{
88
        I2C_InitTypeDef   I2C_Struct;
89
        GPIO_InitTypeDef  GPIO_InitStructure;
90
 
91
        I2C0_State = I2C_STATE_OFF;
92
 
93
        UART1_PutString("\r\n I2C0 init...");
94
        // enable Port 2 peripherie
95
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
96
        // disable a reset state
97
        SCU_APBPeriphReset(__GPIO2, DISABLE);
98
 
99
        // free a busy bus
100
 
101
        // At switch on I2C devices can get in a state where they
102
        // are still waiting for a command due to all the bus lines bouncing
103
        // around at startup have started clocking data into the device(s).
104
        // Enable the ports as open collector port outputs
105
        // and clock out at least 9 SCL pulses, then generate a stop
106
        // condition and then leave the clock line high.
107
 
469 killagreg 108
        // configure P2.0->I2C0_CLKOUT and P2.1->I2C0_DOUT to normal port operation
464 ingob 109
        GPIO_StructInit(&GPIO_InitStructure);
110
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
465 ingob 111
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
464 ingob 112
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
113
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
114
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
115
        GPIO_Init(GPIO2, &GPIO_InitStructure);
116
 
117
        u8 i;
118
        u32 delay;
469 killagreg 119
 
464 ingob 120
        // set SCL high and then SDA to low (start condition)
121
        GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_SET);
122
        delay = SetDelay(1);
123
        while (!CheckDelay(delay));
124
        GPIO_WriteBit(GPIO2, GPIO_Pin_1, Bit_RESET);
125
        // toggle SCL at least 10 times from high to low to high
126
        for(i = 0; i < 10; i++)
127
        {
128
                delay = SetDelay(1);
129
                while (!CheckDelay(delay));
130
 
131
                GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_RESET);
132
                delay = SetDelay(1);
133
                while (!CheckDelay(delay));
134
                GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_SET);
135
        }
136
        delay = SetDelay(1);
137
        while (!CheckDelay(delay));
138
        // create stop condition setting SDA HIGH when SCL is HIGH
139
        GPIO_WriteBit(GPIO2, GPIO_Pin_1, Bit_SET);
140
 
141
 
469 killagreg 142
        // reconfigure P2.0->I2C0_CLKOUT and P2.1->I2C0_DOUT
464 ingob 143
        GPIO_StructInit(&GPIO_InitStructure);
144
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
145
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
146
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
147
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
148
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C0_CLKOUT, I2C0_DOUT
149
        GPIO_Init(GPIO2, &GPIO_InitStructure);
150
 
151
        // enable I2C peripherie
152
        SCU_APBPeriphClockConfig(__I2C0,ENABLE);
153
        // reset I2C peripherie
154
        SCU_APBPeriphReset(__I2C0,ENABLE);
155
        SCU_APBPeriphReset(__I2C0,DISABLE);
156
 
157
        I2C_DeInit(I2C0);
158
        I2C_StructInit(&I2C_Struct);
159
        I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
160
        I2C_Struct.I2C_Ack = I2C_Ack_Enable;
465 ingob 161
        I2C_Struct.I2C_CLKSpeed = I2C0_CLOCK;
464 ingob 162
        I2C_Struct.I2C_OwnAddress = 0x00;
163
        I2C_Init(I2C0, &I2C_Struct);
164
 
165
        // empty rx and tx buffer counters
166
        I2C0_TxBufferSize = 0;
167
        I2C0_RxBufferSize = 0;
168
 
169
        I2C_Cmd(I2C0, ENABLE);
170
        I2C_ITConfig(I2C0, ENABLE);
171
 
172
        VIC_Config(I2C0_ITLine, VIC_IRQ , PRIORITY_I2C0);
173
 
472 holgerb 174
        I2C0_Timeout = SetDelay(2 * I2C1_TIMEOUT);
464 ingob 175
        I2C_GenerateSTOP(I2C0, ENABLE);
176
        I2C0_State = I2C_STATE_IDLE;
177
 
178
        // start some dummy transmissions cycles
179
        // to get the irq routine to work
180
        for(i=0;i<10;i++)
181
        {
182
                I2C0_State = I2C_STATE_BUFFBUSY;
183
                I2C0_Transmission(0,1,0,1);
184
                if(I2C0_WaitForEndOfTransmission(10)) break;
185
                UART1_Putchar('.');
186
        }
187
        UART1_PutString("ok");
188
}
189
 
190
 
191
//--------------------------------------------------------------
192
void I2C0_Deinit(void)
193
{
194
        GPIO_InitTypeDef  GPIO_InitStructure;
465 ingob 195
        UART1_PutString("\r\n I2C0 deinit...");
464 ingob 196
        I2C_GenerateStart(I2C0, DISABLE);
197
        I2C_GenerateSTOP(I2C0, ENABLE);
198
        VIC_ITCmd(I2C0_ITLine, DISABLE);
199
        I2C0_State = I2C_STATE_OFF;
200
        I2C_ITConfig(I2C0, DISABLE);
201
        I2C_Cmd(I2C0, DISABLE);
202
        I2C_DeInit(I2C0);
203
        SCU_APBPeriphClockConfig(__I2C0, DISABLE);
204
 
205
        // set ports to input
206
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
207
        GPIO_StructInit(&GPIO_InitStructure);
208
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
209
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_0 | GPIO_Pin_1;
210
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
211
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
212
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
213
        GPIO_Init(GPIO2, &GPIO_InitStructure);
214
 
215
        // empty rx and tx buffer
216
        I2C0_TxBufferSize = 0;
217
        I2C0_RxBufferSize = 0;
473 holgerb 218
        I2C0_Timeout = SetDelay(5 * I2C0_TIMEOUT);
464 ingob 219
 
473 holgerb 220
  if(I2C_CompassPort == I2C_EXTERN_0)
221
   {
222
    MagRawVector.X = 0;
223
    MagRawVector.Y = 0;
224
    MagRawVector.Z = 0;
225
        AccRawVector.X = 0;
226
        AccRawVector.Y = 0;
227
        AccRawVector.Z = 0;
228
        Compass_Heading = -1;
229
   }
464 ingob 230
        UART1_PutString("ok");
231
}
232
 
233
//--------------------------------------------------------------
234
void I2C0_IRQHandler(void)
235
{
236
        static u8 Rx_Idx = 0, Tx_Idx = 0;
237
        u16 status;
474 holgerb 238
    u16 timeout = 500;
464 ingob 239
        //IENABLE;  // do not enable IRQ nesting for I2C!!!!
240
        // detemine I2C State
241
        status = I2C_GetLastEvent(I2C0);
242
        if(status & (I2C_FLAG_AF|I2C_FLAG_BERR))  // if an acknowledge failure or bus error occured
243
        {       // Set and subsequently clear the STOP bit while BTF is set.
244
                while(I2C_GetFlagStatus (I2C0, I2C_FLAG_BTF) != RESET)
245
                {
474 holgerb 246
                        I2C_GenerateSTOP(I2C0, ENABLE);  // free the bus
247
                        I2C_GenerateSTOP(I2C0, DISABLE); // free the bus
248
                        if(--timeout == 0)
249
                         {
250
                          DebugOut.Analog[14]++; // count I2C error
251
                          break;
252
                         }
464 ingob 253
                }
254
                I2C0_State = I2C_STATE_IDLE;
255
                I2C0_Error = I2C_ERROR_NOACK;
256
                VIC_ITCmd(I2C0_ITLine, DISABLE);
257
                return;
258
        }
259
        else
260
        {       // depending on current i2c state
261
                switch(status)
262
                {
263
                        // the start condition was initiated on the bus
264
                        case I2C_EVENT_MASTER_MODE_SELECT:
265
                                // update current bus state variable
266
                                // jump to rx state if there is nothing to send
267
                                switch(I2C0_Direction)
268
                                {
269
                                        case I2C_MODE_TRANSMITTER:
270
                                                I2C0_State = I2C_STATE_TX_PROGRESS;
271
                                                break;
272
 
273
                                        case I2C_MODE_RECEIVER:
274
                                                if (I2C0_RxBufferSize == 0) // nothing to send?
275
                                                {
276
                                                        I2C_GenerateSTOP (I2C0, ENABLE);
277
                                                        VIC_ITCmd(I2C0_ITLine, DISABLE);
278
                                                        I2C0_State = I2C_STATE_IDLE;
279
                                                        I2C0_Error = I2C_ERROR_NONE;
280
                                                        return;
281
                                                }
282
                                                else
283
                                                {
284
                                                        I2C0_State = I2C_STATE_RX_PROGRESS;
285
                                                }
286
                                                break;
287
 
288
                                        default: // invalid direction
289
                                                I2C_GenerateSTOP (I2C0, ENABLE);
290
                                                VIC_ITCmd(I2C0_ITLine, DISABLE);
291
                                                I2C0_State = I2C_STATE_IDLE;
292
                                                I2C0_Error = I2C_ERROR_UNKNOWN;
293
                                                return;
294
                                }
295
                                // enable acknowledge
296
                                I2C_AcknowledgeConfig (I2C0, ENABLE);
297
                                // send address/direction byte on the bus
298
                                I2C_Send7bitAddress(I2C0, I2C0_SlaveAddress, I2C0_Direction);
299
                                break;
300
 
301
                        // the address byte was send
302
                        case I2C_EVENT_MASTER_MODE_SELECTED:
303
                                // Clear EV6 by set again the PE bit
304
                                I2C_Cmd(I2C0, ENABLE);
305
                                switch(I2C0_State)
306
                                {
307
                                        case I2C_STATE_TX_PROGRESS:
308
                                        // send 1st data byte
309
                                        Tx_Idx = 0;
310
                                        I2C_SendData(I2C0, I2C0_Buffer[Tx_Idx]);
311
                                        Tx_Idx++;
312
                                        // reset timeout
465 ingob 313
                                        I2C0_Timeout = SetDelay(I2C0_TIMEOUT); // after inactivity the I2C0 bus will be reset
464 ingob 314
                                        break;
315
 
316
                                        case I2C_STATE_RX_PROGRESS:
317
                                        Rx_Idx = 0;
318
                                        // disable acknoledge if only one byte has to be read
319
                                        if(I2C0_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C0, DISABLE);              
320
                                        break;
321
 
322
                                        default: // unknown I2C state
323
                                        // should never happen
324
                                        I2C_GenerateSTOP (I2C0, ENABLE);
325
                                        VIC_ITCmd(I2C0_ITLine, DISABLE);
326
                                        I2C0_State = I2C_STATE_IDLE;
327
                                        I2C0_Error = I2C_ERROR_UNKNOWN;
328
                                        return;
329
                                        break;
330
                                }
331
                                break;
332
 
333
                        // the master has transmitted a byte and slave has been acknowledged
334
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
335
 
336
                                // some bytes have to be transmitted
337
                                if(Tx_Idx < I2C0_TxBufferSize)
338
                                {
339
                                        I2C_SendData(I2C0, I2C0_Buffer[Tx_Idx]);
340
                                        Tx_Idx++;
341
                                }
342
                                else // last byte was send
343
                                {
344
                                        // generate stop or repeated start condition
345
                                        if (I2C0_RxBufferSize > 0) // is any answer byte expected?
346
                                        {
347
                                                I2C0_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
348
                                                I2C_GenerateStart(I2C0, ENABLE);   // initiate repeated start condition on the bus
349
                                        }
350
                                        else
351
                                        {   // stop communication
352
                                                I2C_GenerateSTOP(I2C0, ENABLE); // generate stop condition to free the bus
353
                                                VIC_ITCmd(I2C0_ITLine, DISABLE);
354
                                                I2C0_State = I2C_STATE_IDLE;                    // ready for new actions
355
                                                I2C0_Error = I2C_ERROR_NONE;   
356
                                        }
357
                                }
358
                                break;
359
 
360
                        // the master has received a byte from the slave
361
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
362
                                // some bytes have to be received
363
                                if ( Rx_Idx+1 < I2C0_RxBufferSize)
364
                                {       // copy received byte  from the data register to the rx-buffer
365
                                        I2C0_Buffer[Rx_Idx] = I2C_ReceiveData(I2C0);
366
                                }
367
                                else // if the last byte was received
368
                                {
369
                                        // generate a STOP condition on the bus before reading data register
370
                                        I2C_GenerateSTOP(I2C0, ENABLE);
371
                                        I2C0_Buffer[Rx_Idx] = I2C_ReceiveData(I2C0);
372
                                        // call the rx handler function to process recieved data
373
                                        if(I2C0_pRxHandler != NULL) (*I2C0_pRxHandler)((u8*)I2C0_Buffer, I2C0_RxBufferSize);
465 ingob 374
                                        I2C0_Timeout = SetDelay(I2C0_TIMEOUT);
464 ingob 375
                                        DebugOut.Analog[15]++;
376
                                        VIC_ITCmd(I2C0_ITLine, DISABLE);
377
                                        I2C0_State = I2C_STATE_IDLE;
378
                                        I2C0_Error = I2C_ERROR_NONE;
379
                                        return;
380
                                }
381
                                Rx_Idx++;
382
                                // if the 2nd last byte was received disable acknowledge for the last one
383
                                if ( (Rx_Idx + 1) == I2C0_RxBufferSize )
384
                                {
385
                                        I2C_AcknowledgeConfig(I2C0, DISABLE);
386
                                }
387
                                break;
388
 
389
                        default:// unknown event
390
                                // should never happen
391
                                I2C_GenerateSTOP (I2C0, ENABLE);
392
                                VIC_ITCmd(I2C0_ITLine, DISABLE);
393
                                I2C0_State = I2C_STATE_IDLE;
394
                                I2C0_Error = I2C_ERROR_UNKNOWN;
395
                                break;
396
                }
397
        }
398
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
469 killagreg 399
        VIC1->VAR = 0xFF; // write any value to VIC1 Vector address register
464 ingob 400
}
401
 
402
// ----------------------------------------------------------------------------------------
403
// wait for end of transmission
404
u8 I2C0_WaitForEndOfTransmission(u32 timeout)
405
{
406
        u32 time = SetDelay(timeout);
407
        while(I2C0_State != I2C_STATE_IDLE)
408
        {
409
                if(CheckDelay(time)) return(0);
410
        }
411
        return(1);
412
}
413
 
414
// ----------------------------------------------------------------------------------------
415
// try to get access to the transfer buffer      within a timeout limit
416
// returs 1 on success and 0 on error/timeout
417
u8 I2C0_LockBuffer(u32 timeout)
418
{      
419
        if(I2C0_WaitForEndOfTransmission(timeout))
420
        {
421
                I2C0_State = I2C_STATE_BUFFBUSY;
422
                I2C0_Error = I2C_ERROR_UNKNOWN;
423
                return(1);
424
        }
425
        else return(0);
426
}
427
// ----------------------------------------------------------------------------------------
428
// initate an i2c transmission
429
u8 I2C0_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes)
430
{
431
        u8 retval = 0;
432
        if(I2C0_State == I2C_STATE_BUFFBUSY)
433
        {
470 killagreg 434
                if((RxBytes > I2C0_BUFFER_LEN) || (TxBytes > I2C0_BUFFER_LEN))
464 ingob 435
                {
436
                        I2C0_State = I2C_STATE_IDLE;
437
                        return(retval);
438
                }      
439
                I2C0_RxBufferSize = RxBytes;
440
                I2C0_TxBufferSize = TxBytes;
441
                // set direction to master transmitter
470 killagreg 442
                if( (I2C0_TxBufferSize > 0) && (I2C0_TxBufferSize < I2C0_BUFFER_LEN) ) I2C0_Direction = I2C_MODE_TRANSMITTER;
443
                else if (( I2C0_RxBufferSize > 0 ) && (I2C0_RxBufferSize < I2C0_BUFFER_LEN) ) I2C0_Direction = I2C_MODE_RECEIVER;
464 ingob 444
                else // nothing to send or receive
445
                {
446
                        I2C0_State = I2C_STATE_IDLE;
447
                        I2C0_Error = I2C_ERROR_NONE;
448
                        I2C0_TxBufferSize = 0;
449
                        I2C0_RxBufferSize = 0;
450
                        return(retval);
451
                }
452
                // update slave address and rx data handler     funbction pointer
453
                I2C0_SlaveAddress = SlaveAddr;
454
                I2C0_pRxHandler = pRxHandler;
455
                // test on busy flag and clear it
469 killagreg 456
                I2C_ClearFlag(I2C0, I2C_FLAG_BUSY);
464 ingob 457
                // enable I2C IRQ
458
                VIC_ITCmd(I2C0_ITLine, ENABLE);
459
                // initiate start condition on the bus
460
                I2C_GenerateStart(I2C0, ENABLE);
461
                retval = 1;
462
         }
463
         return(retval);
464
}