Subversion Repositories NaviCtrl

Rev

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