Subversion Repositories NaviCtrl

Rev

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

Rev Author Line No. Line
1 ingob 1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2008 Ingo Busker, Holger Buss
171 ingob 6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
1 ingob 7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
37 killagreg 10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
11
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
1 ingob 14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
37 killagreg 17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
1 ingob 18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
171 ingob 28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1 ingob 29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
37 killagreg 33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
1 ingob 34
// + this list of conditions and the following disclaimer.
35
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
36
// +     from this software without specific prior written permission.
37 killagreg 37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
1 ingob 38
// +     for non-commercial use (directly or indirectly)
37 killagreg 39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1 ingob 40
// +     with our written permission
37 killagreg 41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
171 ingob 43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
1 ingob 44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 killagreg 55
// +  POSSIBILITY OF SUCH DAMAGE.
1 ingob 56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
41 ingob 57
#include <string.h>
58
#include "91x_lib.h"
59
#include "i2c.h"
60
#include "uart1.h"
119 killagreg 61
#include "timer1.h"
136 killagreg 62
#include "config.h"
41 ingob 63
#include "led.h"
1 ingob 64
 
248 killagreg 65
 
252 killagreg 66
volatile u8 I2C_State = I2C_STATE_OFF;          // only one byte, because of sync by nesting irqs
67
volatile u8 I2C_Error = I2C_ERROR_NOACK;    // only one byte!
1 ingob 68
 
248 killagreg 69
// number of bytes to send
70
volatile u8 I2C_TxBufferSize;
71
// number of bytes to receive
41 ingob 72
volatile u8 I2C_RxBufferSize;
248 killagreg 73
// the transfer buffer
74
volatile u8 I2C_Buffer[I2C_BUFFER_LEN];
75
// the transfer direction 
41 ingob 76
volatile u8 I2C_Direction;
248 killagreg 77
// the slave address
241 killagreg 78
volatile u8 I2C_SlaveAddress = 0x00;
248 killagreg 79
// function pointer to process the received bytes
241 killagreg 80
I2C_pRxHandler_t I2C_pRxHandler = NULL;
248 killagreg 81
// goblal timeout 
41 ingob 82
volatile u32 I2C1_Timeout = 0;
1 ingob 83
 
84
//--------------------------------------------------------------
85
void I2C1_Init(void)
86
{
41 ingob 87
        I2C_InitTypeDef   I2C_Struct;
88
        GPIO_InitTypeDef  GPIO_InitStructure;
149 killagreg 89
 
241 killagreg 90
        I2C_State = I2C_STATE_OFF;
37 killagreg 91
 
110 killagreg 92
        UART1_PutString("\r\n I2C init...");
41 ingob 93
        // enable Port 2 peripherie
94
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
95
        // disable a reset state
96
        SCU_APBPeriphReset(__GPIO2, DISABLE);
1 ingob 97
 
41 ingob 98
        // free a busy bus
1 ingob 99
 
41 ingob 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.
37 killagreg 106
 
41 ingob 107
        // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation
108
        GPIO_StructInit(&GPIO_InitStructure);
109
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
110
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
111
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
196 killagreg 112
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable;
41 ingob 113
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
114
        GPIO_Init(GPIO2, &GPIO_InitStructure);
37 killagreg 115
 
41 ingob 116
        u8 i;
117
        u32 delay;
118
        // set SCL high and then SDA to low (start condition)
119
        GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
120
        delay = SetDelay(1);
121
        while (!CheckDelay(delay));
122
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, 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));
1 ingob 128
 
41 ingob 129
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET);
130
                delay = SetDelay(1);
131
                while (!CheckDelay(delay));
132
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, 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_3, Bit_SET);
37 killagreg 138
 
139
 
41 ingob 140
        // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT
141
        GPIO_StructInit(&GPIO_InitStructure);
142
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
143
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
144
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
196 killagreg 145
        GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable;
41 ingob 146
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT
147
        GPIO_Init(GPIO2, &GPIO_InitStructure);
148
 
149
        // enable I2C peripherie
150
        SCU_APBPeriphClockConfig(__I2C1,ENABLE);
151
        // reset I2C peripherie
152
        SCU_APBPeriphReset(__I2C1,ENABLE);
153
        SCU_APBPeriphReset(__I2C1,DISABLE);
154
 
155
        I2C_DeInit(I2C1);
156
        I2C_StructInit(&I2C_Struct);
157
        I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
158
        I2C_Struct.I2C_Ack = I2C_Ack_Enable;
136 killagreg 159
        I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK;
41 ingob 160
        I2C_Struct.I2C_OwnAddress = 0x00;
161
        I2C_Init(I2C1, &I2C_Struct);
162
 
248 killagreg 163
        // empty rx and tx buffer counters
41 ingob 164
        I2C_TxBufferSize = 0;
165
        I2C_RxBufferSize = 0;
166
 
167
        I2C_Cmd(I2C1, ENABLE);
168
        I2C_ITConfig(I2C1, ENABLE);
169
 
136 killagreg 170
        VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1);
41 ingob 171
 
189 killagreg 172
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
41 ingob 173
        I2C_GenerateSTOP(I2C1, ENABLE);
241 killagreg 174
        I2C_State = I2C_STATE_IDLE;
41 ingob 175
 
248 killagreg 176
        // start some dummy transmissions cycles
177
        // to get the irq routine to work
178
        for(i=0;i<10;i++)
179
        {
180
                I2C_State = I2C_STATE_BUFFBUSY;
181
                I2C_Transmission(0,1,0,1);
182
                if(I2C_WaitForEndOfTransmission(10)) break;
183
                UART1_Putchar('.');
184
        }
110 killagreg 185
        UART1_PutString("ok");
1 ingob 186
}
187
 
41 ingob 188
 
1 ingob 189
//--------------------------------------------------------------
41 ingob 190
void I2C1_Deinit(void)
1 ingob 191
{
79 killagreg 192
        GPIO_InitTypeDef  GPIO_InitStructure;
110 killagreg 193
        UART1_PutString("\r\n I2C deinit...");
41 ingob 194
        I2C_GenerateStart(I2C1, DISABLE);
195
        I2C_GenerateSTOP(I2C1, ENABLE);
196
        VIC_ITCmd(I2C1_ITLine, DISABLE);
241 killagreg 197
        I2C_State = I2C_STATE_OFF;
41 ingob 198
        I2C_ITConfig(I2C1, DISABLE);
199
        I2C_Cmd(I2C1, DISABLE);
200
        I2C_DeInit(I2C1);
201
        SCU_APBPeriphClockConfig(__I2C1, DISABLE);
79 killagreg 202
 
41 ingob 203
        // set ports to input
204
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
205
        GPIO_StructInit(&GPIO_InitStructure);
206
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
79 killagreg 207
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2 | GPIO_Pin_3;
208
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
196 killagreg 209
        GPIO_InitStructure.GPIO_IPInputConnected =      GPIO_IPInputConnected_Disable;
79 killagreg 210
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
211
        GPIO_Init(GPIO2, &GPIO_InitStructure);
1 ingob 212
 
241 killagreg 213
        // empty rx and tx buffer
41 ingob 214
        I2C_TxBufferSize = 0;
215
        I2C_RxBufferSize = 0;
37 killagreg 216
 
189 killagreg 217
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
37 killagreg 218
 
110 killagreg 219
        UART1_PutString("ok");
41 ingob 220
}
1 ingob 221
 
41 ingob 222
//--------------------------------------------------------------
223
void I2C1_IRQHandler(void)
224
{
241 killagreg 225
        static u8 Rx_Idx = 0, Tx_Idx = 0;
41 ingob 226
        u16 status;
189 killagreg 227
 
195 killagreg 228
        //IENABLE;  // do not enable IRQ nesting for I2C!!!!
41 ingob 229
        // detemine I2C State
230
        status = I2C_GetLastEvent(I2C1);
231
 
232
        if(status & (I2C_FLAG_AF|I2C_FLAG_BERR))  // if an acknowledge failure or bus error occured
79 killagreg 233
        {       // Set and subsequently clear the STOP bit while BTF is set.
41 ingob 234
                while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
235
                {
189 killagreg 236
                        I2C_GenerateSTOP (I2C1, ENABLE);  // free the bus
41 ingob 237
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
238
                }
241 killagreg 239
                I2C_State = I2C_STATE_IDLE;
252 killagreg 240
                I2C_Error = I2C_ERROR_NOACK;
189 killagreg 241
                VIC_ITCmd(I2C1_ITLine, DISABLE);
41 ingob 242
                LED_GRN_OFF;
189 killagreg 243
                return;
41 ingob 244
        }
245
        else
246
        {       // depending on current i2c state
241 killagreg 247
                switch(status)
41 ingob 248
                {
249
                        // the start condition was initiated on the bus
250
                        case I2C_EVENT_MASTER_MODE_SELECT:
251
                                LED_GRN_ON;
252
                                // update current bus state variable
241 killagreg 253
                                // jump to rx state if there is nothing to send
41 ingob 254
                                switch(I2C_Direction)
255
                                {
256
                                        case I2C_MODE_TRANSMITTER:
241 killagreg 257
                                                I2C_State = I2C_STATE_TX_PROGRESS;
41 ingob 258
                                                break;
79 killagreg 259
 
41 ingob 260
                                        case I2C_MODE_RECEIVER:
248 killagreg 261
                                                if (I2C_RxBufferSize == 0) // nothing to send?
41 ingob 262
                                                {
263
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
189 killagreg 264
                                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
326 holgerb 265
//                                                      LED_GRN_OFF;
241 killagreg 266
                                                        I2C_State = I2C_STATE_IDLE;
252 killagreg 267
                                                        I2C_Error = I2C_ERROR_NONE;
41 ingob 268
                                                        return;
269
                                                }
270
                                                else
271
                                                {
241 killagreg 272
                                                        I2C_State = I2C_STATE_RX_PROGRESS;
41 ingob 273
                                                }
274
                                                break;
79 killagreg 275
 
41 ingob 276
                                        default: // invalid direction
277
                                                I2C_GenerateSTOP (I2C1, ENABLE);
189 killagreg 278
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
279
                                                LED_GRN_OFF;
241 killagreg 280
                                                I2C_State = I2C_STATE_IDLE;
252 killagreg 281
                                                I2C_Error = I2C_ERROR_UNKNOWN;
41 ingob 282
                                                return;
283
                                }
284
                                // enable acknowledge
285
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
286
                                // send address/direction byte on the bus
241 killagreg 287
                                I2C_Send7bitAddress(I2C1, I2C_SlaveAddress, I2C_Direction);
41 ingob 288
                                break;
79 killagreg 289
 
41 ingob 290
                        // the address byte was send
291
                        case I2C_EVENT_MASTER_MODE_SELECTED:
292
                                // Clear EV6 by set again the PE bit
79 killagreg 293
                                I2C_Cmd(I2C1, ENABLE);
41 ingob 294
                                switch(I2C_State)
295
                                {
241 killagreg 296
                                        case I2C_STATE_TX_PROGRESS:
297
                                        // send 1st data byte
41 ingob 298
                                        Tx_Idx = 0;
248 killagreg 299
                                        I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]);
241 killagreg 300
                                        Tx_Idx++;
41 ingob 301
                                        // reset timeout
189 killagreg 302
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset
41 ingob 303
                                        break;
79 killagreg 304
 
241 killagreg 305
                                        case I2C_STATE_RX_PROGRESS:
41 ingob 306
                                        Rx_Idx = 0;
241 killagreg 307
                                        // disable acknoledge if only one byte has to be read
308
                                        if(I2C_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C1, DISABLE);               
41 ingob 309
                                        break;
79 killagreg 310
 
41 ingob 311
                                        default: // unknown I2C state
312
                                        // should never happen
313
                                        I2C_GenerateSTOP (I2C1, ENABLE);
189 killagreg 314
                                        LED_GRN_OFF;
315
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
241 killagreg 316
                                        I2C_State = I2C_STATE_IDLE;
252 killagreg 317
                                        I2C_Error = I2C_ERROR_UNKNOWN;
189 killagreg 318
                                        return;
41 ingob 319
                                        break;
320
                                }
321
                                break;
79 killagreg 322
 
41 ingob 323
                        // the master has transmitted a byte and slave has been acknowledged
324
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
79 killagreg 325
 
41 ingob 326
                                // some bytes have to be transmitted
327
                                if(Tx_Idx < I2C_TxBufferSize)
328
                                {
248 killagreg 329
                                        I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]);
241 killagreg 330
                                        Tx_Idx++;
41 ingob 331
                                }
189 killagreg 332
                                else // last byte was send
333
                                {
41 ingob 334
                                        // generate stop or repeated start condition
241 killagreg 335
                                        if (I2C_RxBufferSize > 0) // is any answer byte expected?
41 ingob 336
                                        {
79 killagreg 337
                                                I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
338
                                                I2C_GenerateStart(I2C1, ENABLE);   // initiate repeated start condition on the bus
41 ingob 339
                                        }
340
                                        else
341
                                        {   // stop communication
342
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
189 killagreg 343
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
326 holgerb 344
//                                              LED_GRN_OFF;
252 killagreg 345
                                                I2C_State = I2C_STATE_IDLE;                     // ready for new actions
346
                                                I2C_Error = I2C_ERROR_NONE;    
41 ingob 347
                                        }
348
                                }
349
                                break;
79 killagreg 350
 
41 ingob 351
                        // the master has received a byte from the slave
79 killagreg 352
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
41 ingob 353
                                // some bytes have to be received
241 killagreg 354
                                if ( Rx_Idx+1 < I2C_RxBufferSize)
41 ingob 355
                                {       // copy received byte  from the data register to the rx-buffer
248 killagreg 356
                                        I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1);
41 ingob 357
                                }
241 killagreg 358
                                else // if the last byte was received
41 ingob 359
                                {
360
                                        // generate a STOP condition on the bus before reading data register
361
                                        I2C_GenerateSTOP(I2C1, ENABLE);
248 killagreg 362
                                        I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1);
241 killagreg 363
                                        // call the rx handler function to process recieved data
248 killagreg 364
                                        if(I2C_pRxHandler != NULL) (*I2C_pRxHandler)((u8*)I2C_Buffer, I2C_RxBufferSize);
241 killagreg 365
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT);
366
                                        DebugOut.Analog[15]++;
189 killagreg 367
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
326 holgerb 368
//                                      LED_GRN_OFF;
241 killagreg 369
                                        I2C_State = I2C_STATE_IDLE;
252 killagreg 370
                                        I2C_Error = I2C_ERROR_NONE;
189 killagreg 371
                                        return;
41 ingob 372
                                }
373
                                Rx_Idx++;
374
                                // if the 2nd last byte was received disable acknowledge for the last one
241 killagreg 375
                                if ( (Rx_Idx + 1) == I2C_RxBufferSize )
41 ingob 376
                                {
241 killagreg 377
                                        I2C_AcknowledgeConfig(I2C1, DISABLE);
41 ingob 378
                                }
379
                                break;
79 killagreg 380
 
241 killagreg 381
                        default:// unknown event
382
                                // should never happen
383
                                I2C_GenerateSTOP (I2C1, ENABLE);
384
                                LED_GRN_OFF;
385
                                VIC_ITCmd(I2C1_ITLine, DISABLE);
386
                                I2C_State = I2C_STATE_IDLE;
252 killagreg 387
                                I2C_Error = I2C_ERROR_UNKNOWN;
41 ingob 388
                                break;
389
                }
390
        }
195 killagreg 391
        //IDISABLE;      // do not enable IRQ nesting for I2C!!!!
1 ingob 392
}
248 killagreg 393
 
241 killagreg 394
// ----------------------------------------------------------------------------------------
248 killagreg 395
// wait for end of transmission
396
u8 I2C_WaitForEndOfTransmission(u32 timeout)
397
{
398
        u32 time = SetDelay(timeout);
399
        while(I2C_State != I2C_STATE_IDLE)
400
        {
401
                if(CheckDelay(time)) return(0);
402
        }
403
        return(1);
404
}
405
 
406
// ----------------------------------------------------------------------------------------
407
// try to get access to the transfer buffer      within a timeout limit
408
// returs 1 on success and 0 on error/timeout
409
u8 I2C_LockBuffer(u32 timeout)
410
{      
411
        if(I2C_WaitForEndOfTransmission(timeout))
412
        {
413
                I2C_State = I2C_STATE_BUFFBUSY;
252 killagreg 414
                I2C_Error = I2C_ERROR_UNKNOWN;
248 killagreg 415
                return(1);
416
        }
417
        else return(0);
418
}
419
// ----------------------------------------------------------------------------------------
241 killagreg 420
// initate an i2c transmission
248 killagreg 421
u8 I2C_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes)
1 ingob 422
{
241 killagreg 423
        u8 retval = 0;
248 killagreg 424
        if(I2C_State == I2C_STATE_BUFFBUSY)
425
        {
426
                if((RxBytes > I2C_BUFFER_LEN) || (TxBytes > I2C_BUFFER_LEN))
427
                {
428
                        I2C_State = I2C_STATE_IDLE;
429
                        return(retval);
430
                }      
241 killagreg 431
                I2C_RxBufferSize = RxBytes;
248 killagreg 432
                I2C_TxBufferSize = TxBytes;
241 killagreg 433
                // set direction to master transmitter
248 killagreg 434
                if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER;
435
                else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER;
241 killagreg 436
                else // nothing to send or receive
189 killagreg 437
                {
248 killagreg 438
                        I2C_State = I2C_STATE_IDLE;
252 killagreg 439
                        I2C_Error = I2C_ERROR_NONE;
241 killagreg 440
                        I2C_TxBufferSize = 0;
441
                        I2C_RxBufferSize = 0;
442
                        return(retval);
189 killagreg 443
                }
241 killagreg 444
                // update slave address and rx data handler     funbction pointer
445
                I2C_SlaveAddress = SlaveAddr;
446
                I2C_pRxHandler = pRxHandler;
189 killagreg 447
                // test on busy flag and clear it
448
                I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
242 killagreg 449
                // enable I2C IRQ
450
                VIC_ITCmd(I2C1_ITLine, ENABLE);
189 killagreg 451
                // initiate start condition on the bus
452
                I2C_GenerateStart(I2C1, ENABLE);
241 killagreg 453
                retval = 1;
454
         }
455
         return(retval);
41 ingob 456
}