Subversion Repositories NaviCtrl

Rev

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