Subversion Repositories NaviCtrl

Rev

Rev 184 | Rev 195 | 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"
1 ingob 63
#include "main.h"
41 ingob 64
#include "led.h"
65
#include "spi_slave.h"
1 ingob 66
 
189 killagreg 67
#define I2C_SLAVE_ADDRESS               0x50
1 ingob 68
 
189 killagreg 69
// I2C states
70
#define I2C_UNDEF               0
71
#define I2C_IDLE                1
72
#define I2C_TX_PROGRESS 2
73
#define I2C_RX_PENDING  3
74
#define I2C_RX_PROGRESS 4
75
#define I2C_OFF                 5
41 ingob 76
 
189 killagreg 77
volatile u8 I2C_State = I2C_OFF; // only on byte! because of sync by nesting irqs
78
u8 I2C_StopPolling = 1;
79
 
41 ingob 80
// rxbuffer
81
volatile u8 I2C_RxBufferSize;
82
volatile u8 *I2C_RxBuffer;
83
// txbuffer
84
volatile u8 I2C_TxBufferSize;
85
volatile u8 *I2C_TxBuffer;
86
 
87
volatile u8 I2C_Direction;
88
volatile u8 I2C_Command;
189 killagreg 89
// I2C Transfer buffers
41 ingob 90
volatile I2C_Heading_t                  I2C_Heading;
91
volatile I2C_WriteAttitude_t    I2C_WriteAttitude;
92
volatile I2C_Mag_t                              I2C_Mag;
93
volatile I2C_Version_t                  MK3MAG_Version;
94
volatile I2C_Cal_t                              I2C_WriteCal;
95
volatile I2C_Cal_t                              I2C_ReadCal;
96
 
146 killagreg 97
#define I2C1_TIMEOUT 500 // 500 ms
41 ingob 98
volatile u32 I2C1_Timeout = 0;
1 ingob 99
 
100
//--------------------------------------------------------------
101
void I2C1_Init(void)
102
{
41 ingob 103
        I2C_InitTypeDef   I2C_Struct;
104
        GPIO_InitTypeDef  GPIO_InitStructure;
149 killagreg 105
 
106
        I2C_State = I2C_OFF;
37 killagreg 107
 
110 killagreg 108
        UART1_PutString("\r\n I2C init...");
41 ingob 109
        // enable Port 2 peripherie
110
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
111
        // disable a reset state
112
        SCU_APBPeriphReset(__GPIO2, DISABLE);
1 ingob 113
 
41 ingob 114
        // free a busy bus
1 ingob 115
 
41 ingob 116
        // At switch on I2C devices can get in a state where they
117
        // are still waiting for a command due to all the bus lines bouncing
118
        // around at startup have started clocking data into the device(s).
119
        // Enable the ports as open collector port outputs
120
        // and clock out at least 9 SCL pulses, then generate a stop
121
        // condition and then leave the clock line high.
37 killagreg 122
 
41 ingob 123
        // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation
124
        GPIO_StructInit(&GPIO_InitStructure);
125
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
126
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
127
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
128
        GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable;
129
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1;
130
        GPIO_Init(GPIO2, &GPIO_InitStructure);
37 killagreg 131
 
41 ingob 132
        u8 i;
133
        u32 delay;
134
        // set SCL high and then SDA to low (start condition)
135
        GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
136
        delay = SetDelay(1);
137
        while (!CheckDelay(delay));
138
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET);
139
        // toggle SCL at least 10 times from high to low to high
140
        for(i = 0; i < 10; i++)
141
        {
142
                delay = SetDelay(1);
143
                while (!CheckDelay(delay));
1 ingob 144
 
41 ingob 145
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET);
146
                delay = SetDelay(1);
147
                while (!CheckDelay(delay));
148
                GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET);
149
        }
150
        delay = SetDelay(1);
151
        while (!CheckDelay(delay));
152
        // create stop condition setting SDA HIGH when SCL is HIGH
153
        GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET);
37 killagreg 154
 
155
 
41 ingob 156
        // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT
157
        GPIO_StructInit(&GPIO_InitStructure);
158
        GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
159
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
160
        GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector;
161
        GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
162
        GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT
163
        GPIO_Init(GPIO2, &GPIO_InitStructure);
164
 
165
        // enable I2C peripherie
166
        SCU_APBPeriphClockConfig(__I2C1,ENABLE);
167
        // reset I2C peripherie
168
        SCU_APBPeriphReset(__I2C1,ENABLE);
169
        SCU_APBPeriphReset(__I2C1,DISABLE);
170
 
171
        I2C_DeInit(I2C1);
172
        I2C_StructInit(&I2C_Struct);
173
        I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable;
174
        I2C_Struct.I2C_Ack = I2C_Ack_Enable;
136 killagreg 175
        I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK;
41 ingob 176
        I2C_Struct.I2C_OwnAddress = 0x00;
177
        I2C_Init(I2C1, &I2C_Struct);
178
 
179
        I2C_TxBuffer = NULL;
180
        I2C_TxBufferSize = 0;
181
 
182
        I2C_RxBuffer = NULL;
183
        I2C_RxBufferSize = 0;
184
 
185
        I2C_Cmd(I2C1, ENABLE);
186
        I2C_ITConfig(I2C1, ENABLE);
187
 
136 killagreg 188
        VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1);
41 ingob 189
 
189 killagreg 190
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
41 ingob 191
        I2C_Heading.Heading = -1;
192
        I2C_GenerateSTOP(I2C1, ENABLE);
193
        I2C_State = I2C_IDLE;
194
 
154 killagreg 195
        I2C_StopPolling = 0; // start polling
196
 
110 killagreg 197
        UART1_PutString("ok");
1 ingob 198
}
199
 
41 ingob 200
 
1 ingob 201
//--------------------------------------------------------------
41 ingob 202
void I2C1_Deinit(void)
1 ingob 203
{
79 killagreg 204
        GPIO_InitTypeDef  GPIO_InitStructure;
154 killagreg 205
        I2C_StopPolling = 1;// stop polling
110 killagreg 206
        UART1_PutString("\r\n I2C deinit...");
41 ingob 207
        I2C_GenerateStart(I2C1, DISABLE);
208
        I2C_GenerateSTOP(I2C1, ENABLE);
209
        VIC_ITCmd(I2C1_ITLine, DISABLE);
154 killagreg 210
        I2C_State = I2C_OFF;
41 ingob 211
        I2C_ITConfig(I2C1, DISABLE);
212
        I2C_Cmd(I2C1, DISABLE);
213
        I2C_DeInit(I2C1);
214
        SCU_APBPeriphClockConfig(__I2C1, DISABLE);
79 killagreg 215
 
41 ingob 216
        // set ports to input
217
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
218
        GPIO_StructInit(&GPIO_InitStructure);
219
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
79 killagreg 220
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2 | GPIO_Pin_3;
221
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
41 ingob 222
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Disable;
79 killagreg 223
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
224
        GPIO_Init(GPIO2, &GPIO_InitStructure);
1 ingob 225
 
41 ingob 226
        I2C_TxBuffer = NULL;
227
        I2C_TxBufferSize = 0;
1 ingob 228
 
41 ingob 229
        I2C_RxBuffer = NULL;
230
        I2C_RxBufferSize = 0;
37 killagreg 231
 
189 killagreg 232
        I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT);
41 ingob 233
        I2C_Heading.Heading = -1;
37 killagreg 234
 
110 killagreg 235
        UART1_PutString("ok");
41 ingob 236
}
1 ingob 237
 
41 ingob 238
 
239
//--------------------------------------------------------------
240
void I2C1_IRQHandler(void)
241
{
189 killagreg 242
        static u8 Rx_Idx = 0, Tx_Idx = 0, crc = 0;
243
        static u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers
41 ingob 244
        u16 status;
189 killagreg 245
 
41 ingob 246
        // detemine I2C State
247
        status = I2C_GetLastEvent(I2C1);
248
 
249
        if(status & (I2C_FLAG_AF|I2C_FLAG_BERR))  // if an acknowledge failure or bus error occured
79 killagreg 250
        {       // Set and subsequently clear the STOP bit while BTF is set.
41 ingob 251
                while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
252
                {
189 killagreg 253
                        I2C_GenerateSTOP (I2C1, ENABLE);  // free the bus
41 ingob 254
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
255
                }
256
                I2C_State = I2C_IDLE;
189 killagreg 257
                VIC_ITCmd(I2C1_ITLine, DISABLE);
41 ingob 258
                LED_GRN_OFF;
189 killagreg 259
                return;
41 ingob 260
        }
261
        else
262
        {       // depending on current i2c state
263
                switch (status)
264
                {
265
                        // the start condition was initiated on the bus
266
                        case I2C_EVENT_MASTER_MODE_SELECT:
267
                                LED_GRN_ON;
268
                                // update current bus state variable
269
                                switch(I2C_Direction)
270
                                {
271
                                        case I2C_MODE_TRANSMITTER:
272
                                                I2C_State = I2C_TX_PROGRESS;
273
                                                break;
79 killagreg 274
 
41 ingob 275
                                        case I2C_MODE_RECEIVER:
276
                                                if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0))
277
                                                {
278
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
189 killagreg 279
                                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
280
                                                        LED_GRN_OFF;
41 ingob 281
                                                        I2C_State = I2C_IDLE;
282
                                                        return;
283
                                                }
284
                                                else
285
                                                {
286
                                                        I2C_State = I2C_RX_PROGRESS;
287
                                                }
288
                                                break;
79 killagreg 289
 
41 ingob 290
                                        default: // invalid direction
291
                                                I2C_GenerateSTOP (I2C1, ENABLE);
189 killagreg 292
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
293
                                                LED_GRN_OFF;
41 ingob 294
                                                I2C_State = I2C_IDLE;
295
                                                return;
296
                                }
297
                                // enable acknowledge
298
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
299
                                // send address/direction byte on the bus
300
                                I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction);
301
                                break;
79 killagreg 302
 
41 ingob 303
                        // the address byte was send
304
                        case I2C_EVENT_MASTER_MODE_SELECTED:
305
                                // Clear EV6 by set again the PE bit
79 killagreg 306
                                I2C_Cmd(I2C1, ENABLE);
41 ingob 307
                                // reset checksum
308
                                crc = 0;
309
                                switch(I2C_State)
310
                                {
311
                                        case I2C_TX_PROGRESS:
79 killagreg 312
                                        // send command 1st data byte (allways the command id)
41 ingob 313
                                        I2C_SendData(I2C1, I2C_Command);
314
                                        crc += I2C_Command;
315
                                        Tx_Idx = 0;
316
                                        // reset timeout
189 killagreg 317
                                        I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset
41 ingob 318
                                        break;
79 killagreg 319
 
41 ingob 320
                                        case I2C_RX_PROGRESS:
321
                                        Rx_Idx = 0;
322
                                        break;
79 killagreg 323
 
41 ingob 324
                                        default: // unknown I2C state
325
                                        // should never happen
326
                                        I2C_GenerateSTOP (I2C1, ENABLE);
189 killagreg 327
                                        LED_GRN_OFF;
328
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
41 ingob 329
                                        I2C_State = I2C_IDLE;
189 killagreg 330
                                        return;
41 ingob 331
                                        break;
332
                                }
333
                                break;
79 killagreg 334
 
41 ingob 335
                        // the master has transmitted a byte and slave has been acknowledged
336
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
79 killagreg 337
 
41 ingob 338
                                // some bytes have to be transmitted
339
                                if(Tx_Idx < I2C_TxBufferSize)
340
                                {
341
                                        if(I2C_TxBuffer != NULL)
342
                                        {
343
                                                I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]);
344
                                                crc += I2C_TxBuffer[Tx_Idx];
345
                                        }
346
                                        else
347
                                        {
348
                                                I2C_SendData(I2C1, 0x00);
349
                                        }
350
                                }
189 killagreg 351
                                else if(Tx_Idx == I2C_TxBufferSize) // the last tx buffer byte was send
41 ingob 352
                                {
353
                                        // send crc byte at the end
137 killagreg 354
                                        crc = ~crc; // flip all bits in the checksum
41 ingob 355
                                        I2C_SendData(I2C1, crc);
189 killagreg 356
                                }
357
                                else if(Tx_Idx == (I2C_TxBufferSize+1) )
358
                                {
359
                                        I2C_SendData(I2C1, 0xAA); // send a dummybyte
360
                                }
361
                                else // last byte was send
362
                                {
41 ingob 363
                                        // generate stop or repeated start condition
364
                                        if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected?
365
                                        {
79 killagreg 366
                                                I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
367
                                                I2C_GenerateStart(I2C1, ENABLE);   // initiate repeated start condition on the bus
41 ingob 368
                                        }
369
                                        else
370
                                        {   // stop communication
371
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
189 killagreg 372
                                                VIC_ITCmd(I2C1_ITLine, DISABLE);
41 ingob 373
                                                LED_GRN_OFF;
374
                                                DebugOut.Analog[15]++;
189 killagreg 375
                                                I2C_State = I2C_IDLE;                   // ready for new actions
376
 
41 ingob 377
                                        }
378
                                }
79 killagreg 379
                                Tx_Idx++;
41 ingob 380
                                break;
79 killagreg 381
 
41 ingob 382
                        // the master has received a byte from the slave
79 killagreg 383
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
41 ingob 384
                                // some bytes have to be received
385
                                if (Rx_Idx < I2C_RxBufferSize)
386
                                {       // copy received byte  from the data register to the rx-buffer
387
                                        I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
388
                                        // update checksum
389
                                        crc += I2C_PrimRxBuffer[Rx_Idx];
390
                                }
391
                                // if the last byte (crc) was received
79 killagreg 392
                                else if ( Rx_Idx == I2C_RxBufferSize)
41 ingob 393
                                {
394
                                        // generate a STOP condition on the bus before reading data register
395
                                        I2C_GenerateSTOP(I2C1, ENABLE);
396
                                        // compare last byte with checksum
137 killagreg 397
                                        crc = ~crc;// flip all bits in calulated checksum
41 ingob 398
                                        if(crc == I2C_ReceiveData(I2C1))
189 killagreg 399
                                        {      
400
                                                // copy primary rx buffer content to rx buffer if exist
41 ingob 401
                                                if(I2C_RxBuffer != NULL)
402
                                                {
403
                                                        memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize);
404
                                                }
189 killagreg 405
                                                I2C1_Timeout = SetDelay(I2C1_TIMEOUT);
41 ingob 406
                                                DebugOut.Analog[15]++;
407
                                        }
408
                                        else // checksum error detected
409
                                        {
189 killagreg 410
                                                DebugOut.Analog[14]++;
41 ingob 411
                                        }
189 killagreg 412
                                        VIC_ITCmd(I2C1_ITLine, DISABLE);
413
                                        LED_GRN_OFF;
41 ingob 414
                                        I2C_State = I2C_IDLE;
189 killagreg 415
                                        return;
41 ingob 416
                                }
417
                                Rx_Idx++;
418
                                // if the 2nd last byte was received disable acknowledge for the last one
419
                                if ( Rx_Idx == I2C_RxBufferSize )
420
                                {
421
                                        I2C_AcknowledgeConfig (I2C1, DISABLE);
422
                                }
423
                                break;
79 killagreg 424
 
41 ingob 425
                        default:
426
                                break;
427
                }
428
        }
1 ingob 429
}
430
//----------------------------------------------------------------
41 ingob 431
void I2C1_SendCommand(u8 command)
1 ingob 432
{
189 killagreg 433
        // disable I2C IRQ to check state
434
        VIC_ITCmd(I2C1_ITLine, DISABLE);
41 ingob 435
        // If I2C transmission is in progress
189 killagreg 436
        if(I2C_State == I2C_IDLE)
41 ingob 437
        {
189 killagreg 438
                // update current command id
439
                I2C_Command = command;
440
                // set pointers to data area with respect to the command id
441
                switch (command)
442
                {
443
                        case I2C_CMD_VERSION:
444
                                I2C_RxBuffer = (u8 *)&MK3MAG_Version;
445
                                I2C_RxBufferSize = sizeof(MK3MAG_Version);
446
                                I2C_TxBuffer = NULL;
447
                                I2C_TxBufferSize = 0;
448
                                break;
449
                        case I2C_CMD_WRITE_CAL:
450
                                I2C_RxBuffer = (u8 *)&I2C_ReadCal;
451
                                I2C_RxBufferSize = sizeof(I2C_ReadCal);
452
                                I2C_TxBuffer = (u8 *)&I2C_WriteCal;
453
                                I2C_TxBufferSize = sizeof(I2C_WriteCal);
454
                                break;
455
                        case I2C_CMD_READ_MAG:
456
                                I2C_RxBuffer = (u8 *)&I2C_Mag;
457
                                I2C_RxBufferSize = sizeof(I2C_Mag);
458
                                I2C_TxBuffer = NULL;
459
                                I2C_TxBufferSize = 0;
460
                                break;
461
                        case I2C_CMD_READ_HEADING:
462
                                I2C_RxBuffer = (u8 *)&I2C_Heading;
463
                                I2C_RxBufferSize = sizeof(I2C_Heading);
464
                                I2C_TxBuffer =  (u8 *)&I2C_WriteAttitude;
465
                                I2C_TxBufferSize = sizeof(I2C_WriteAttitude);
466
                                // update attitude from spi rx buffer
467
                                VIC_ITCmd(SSP0_ITLine, DISABLE); // avoid spi buffer update during copy
468
                                I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll;
469
                                I2C_WriteAttitude.Nick = FromFlightCtrl.AngleNick;
470
                                VIC_ITCmd(SSP0_ITLine, ENABLE);
471
                                break;
472
                        default: // unknown command id
473
                                I2C_RxBuffer = NULL;
474
                                I2C_RxBufferSize = 0;
475
                                I2C_TxBuffer =  NULL;
476
                                I2C_TxBufferSize = 0;
477
                                break;
478
                }
479
                // set direction to master transmitter
480
                I2C_Direction = I2C_MODE_TRANSMITTER;
481
                // test on busy flag and clear it
482
                I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
483
                // enable I2C IRQ again
484
                VIC_ITCmd(I2C1_ITLine, ENABLE);
485
                // initiate start condition on the bus
486
                I2C_GenerateStart(I2C1, ENABLE);
487
                // to be continued in the I2C1_IRQHandler() above
488
        } // EOF I2C_State == I2C_IDLE
489
        else // I2C_State != I2C_IDLE
490
        {
491
                // re-enable I2C IRQ again
492
                VIC_ITCmd(I2C1_ITLine, ENABLE);
493
        }      
41 ingob 494
}
1 ingob 495
 
41 ingob 496
//----------------------------------------------------------------
497
void I2C1_GetMK3MagVersion(void)
498
{
78 holgerb 499
        u8 msg[64];
154 killagreg 500
        u8 repeat;
149 killagreg 501
        u32 timeout;
150 killagreg 502
 
154 killagreg 503
        UART1_PutString("\r\n Getting Version from MK3MAG");
504
        // stop polling of other commands
150 killagreg 505
        I2C_StopPolling = 1;
1 ingob 506
 
41 ingob 507
        MK3MAG_Version.Major = 0xFF;
508
        MK3MAG_Version.Minor = 0xFF;
509
        MK3MAG_Version.Patch = 0xFF;
510
        MK3MAG_Version.Compatible = 0xFF;
150 killagreg 511
        // polling of version info
154 killagreg 512
        repeat = 0;
513
        do
41 ingob 514
        {
79 killagreg 515
                I2C1_SendCommand(I2C_CMD_VERSION);
154 killagreg 516
                timeout = SetDelay(250);
517
                do
518
                {
519
                        if (MK3MAG_Version.Major != 0xFF) break; // break loop on success
520
                }while (!CheckDelay(timeout));
521
                UART1_PutString(".");
522
                repeat++;
523
        }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s
524
        // if we got it
41 ingob 525
        if (MK3MAG_Version.Major != 0xFF)
79 killagreg 526
        {
154 killagreg 527
                sprintf(msg, "\r\n MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch);
110 killagreg 528
                UART1_PutString(msg);
41 ingob 529
                sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible);
110 killagreg 530
                UART1_PutString(msg);
41 ingob 531
        }
154 killagreg 532
        else UART1_PutString("\n\r No version information from MK3Mag.");
150 killagreg 533
 
154 killagreg 534
        I2C_StopPolling = 0; // enable polling of heading command
146 killagreg 535
}
1 ingob 536
 
146 killagreg 537
 
538
//----------------------------------------------------------------
539
void I2C1_UpdateCompass(void)
540
{
541
        static u32 TimerCompassUpdate = 0;
542
 
154 killagreg 543
        if( (I2C_State == I2C_OFF) || I2C_StopPolling ) return;
149 killagreg 544
 
545
        if(CheckDelay(TimerCompassUpdate))
146 killagreg 546
        {
149 killagreg 547
                // check for incomming compass calibration request
548
                // update CalByte from spi input queue
549
                fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte));
550
                // send new calstate
551
                if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte)
146 killagreg 552
                {
149 killagreg 553
                        I2C1_SendCommand(I2C_CMD_WRITE_CAL);
146 killagreg 554
                }
149 killagreg 555
                else // request current heading
556
                {
557
                        I2C1_SendCommand(I2C_CMD_READ_HEADING);
558
                }
189 killagreg 559
                TimerCompassUpdate = SetDelay(40);    // every 40 ms are 25 Hz
146 killagreg 560
        }
1 ingob 561
}