Subversion Repositories NaviCtrl

Rev

Rev 146 | Rev 150 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 146 Rev 149
Line 85... Line 85...
85
volatile I2C_Mag_t                              I2C_Mag;
85
volatile I2C_Mag_t                              I2C_Mag;
86
volatile I2C_Version_t                  MK3MAG_Version;
86
volatile I2C_Version_t                  MK3MAG_Version;
87
volatile I2C_Cal_t                              I2C_WriteCal;
87
volatile I2C_Cal_t                              I2C_WriteCal;
88
volatile I2C_Cal_t                              I2C_ReadCal;
88
volatile I2C_Cal_t                              I2C_ReadCal;
Line 89... Line -...
89
 
-
 
90
volatile u8 I2C_ReadRequest = 0;
89
 
91
#define I2C1_TIMEOUT 500 // 500 ms
90
#define I2C1_TIMEOUT 500 // 500 ms
92
volatile u32 I2C1_Timeout = 0;
91
volatile u32 I2C1_Timeout = 0;
Line 93... Line 92...
93
volatile u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers
92
volatile u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers
94
 
93
 
95
 
94
 
96
//--------------------------------------------------------------
95
//--------------------------------------------------------------
97
void I2C1_Init(void)
96
void I2C1_Init(void)
-
 
97
{
-
 
98
        I2C_InitTypeDef   I2C_Struct;
Line 98... Line 99...
98
{
99
        GPIO_InitTypeDef  GPIO_InitStructure;
99
        I2C_InitTypeDef   I2C_Struct;
100
       
100
        GPIO_InitTypeDef  GPIO_InitStructure;
101
        I2C_State = I2C_OFF;
101
 
102
 
Line 195... Line 196...
195
 
196
 
196
//--------------------------------------------------------------
197
//--------------------------------------------------------------
197
void I2C1_Deinit(void)
198
void I2C1_Deinit(void)
198
{
199
{
199
        GPIO_InitTypeDef  GPIO_InitStructure;
-
 
-
 
200
        GPIO_InitTypeDef  GPIO_InitStructure;
200
 
201
        I2C_State = I2C_OFF;
201
        UART1_PutString("\r\n I2C deinit...");
202
        UART1_PutString("\r\n I2C deinit...");
202
        I2C_GenerateStart(I2C1, DISABLE);
203
        I2C_GenerateStart(I2C1, DISABLE);
203
        I2C_GenerateSTOP(I2C1, ENABLE);
204
        I2C_GenerateSTOP(I2C1, ENABLE);
204
        VIC_ITCmd(I2C1_ITLine, DISABLE);
205
        VIC_ITCmd(I2C1_ITLine, DISABLE);
Line 223... Line 224...
223
 
224
 
224
        I2C_RxBuffer = NULL;
225
        I2C_RxBuffer = NULL;
225
        Rx_Idx = 0;
226
        Rx_Idx = 0;
Line 226... Line -...
226
        I2C_RxBufferSize = 0;
-
 
227
 
-
 
228
        I2C_State = I2C_OFF;
227
        I2C_RxBufferSize = 0;
229
 
228
 
Line 230... Line 229...
230
        I2C1_Timeout = 0;
229
        I2C1_Timeout = 0;
231
        I2C_Heading.Heading = -1;
230
        I2C_Heading.Heading = -1;
Line 405... Line 404...
405
}
404
}
406
//----------------------------------------------------------------
405
//----------------------------------------------------------------
407
void I2C1_SendCommand(u8 command)
406
void I2C1_SendCommand(u8 command)
408
{
407
{
409
        // If I2C transmission is in progress
408
        // If I2C transmission is in progress
410
        while (I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress
409
        if(I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress
411
        // disable I2C IRQ to avoid read/write access to the tx/rx buffer pointers during
410
        // disable I2C IRQ to avoid read/write access to the tx/rx buffer pointers during
412
        // update of that buffer pointers and length
411
        // update of that buffer pointers and length
413
        I2C_ITConfig(I2C1, DISABLE);
412
        I2C_ITConfig(I2C1, DISABLE);
414
        // update current command id
413
        // update current command id
415
        I2C_Command = command;
414
        I2C_Command = command;
Line 449... Line 448...
449
                        I2C_RxBufferSize = 0;
448
                        I2C_RxBufferSize = 0;
450
                        I2C_TxBuffer =  NULL;
449
                        I2C_TxBuffer =  NULL;
451
                        I2C_TxBufferSize = 0;
450
                        I2C_TxBufferSize = 0;
452
                        break;
451
                        break;
453
        }
452
        }
454
        // enable I2C IRQ again
-
 
455
        I2C_ITConfig(I2C1, ENABLE);
-
 
456
        // set direction to master transmitter
453
        // set direction to master transmitter
457
        I2C_Direction = I2C_MODE_TRANSMITTER;
454
        I2C_Direction = I2C_MODE_TRANSMITTER;
458
        // test on busy flag and clear it
455
        // test on busy flag and clear it
459
        I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
456
        I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
-
 
457
        // enable I2C IRQ again
-
 
458
        I2C_ITConfig(I2C1, ENABLE);
460
        // initiale start condition on the bus
459
        // initiale start condition on the bus
461
        I2C_GenerateStart(I2C1, ENABLE);
460
        I2C_GenerateStart(I2C1, ENABLE);
462
        // to be continued in the I2C1_IRQHandler() above
461
        // to be continued in the I2C1_IRQHandler() above
463
}
462
}
Line 464... Line 463...
464
 
463
 
465
//----------------------------------------------------------------
464
//----------------------------------------------------------------
466
void I2C1_GetMK3MagVersion(void)
465
void I2C1_GetMK3MagVersion(void)
467
{
466
{
468
        u8 msg[64];
467
        u8 msg[64];
-
 
468
        u8 repeat = 20;
Line 469... Line 469...
469
        u8 repeat = 10;
469
        u32 timeout;
470
 
470
 
471
        MK3MAG_Version.Major = 0xFF;
471
        MK3MAG_Version.Major = 0xFF;
472
        MK3MAG_Version.Minor = 0xFF;
472
        MK3MAG_Version.Minor = 0xFF;
Line 473... Line 473...
473
        MK3MAG_Version.Patch = 0xFF;
473
        MK3MAG_Version.Patch = 0xFF;
474
        MK3MAG_Version.Compatible = 0xFF;
474
        MK3MAG_Version.Compatible = 0xFF;
475
 
475
 
476
        while ((MK3MAG_Version.Major == 0xFF)  && (repeat != 0))
476
        while ((MK3MAG_Version.Major == 0xFF)  && (repeat != 0))
477
        {
477
        {
478
                I2C1_SendCommand(I2C_CMD_VERSION);
478
                I2C1_SendCommand(I2C_CMD_VERSION);
479
                I2C1_Timeout = SetDelay(500);
479
                timeout = SetDelay(200);
Line 480... Line 480...
480
                while (!CheckDelay(I2C1_Timeout)) if (MK3MAG_Version.Major != 0xFF) break;
480
                while (!CheckDelay(timeout)) if (MK3MAG_Version.Major != 0xFF) break;
481
                repeat--;
481
                repeat--;
Line 495... Line 495...
495
//----------------------------------------------------------------
495
//----------------------------------------------------------------
496
void I2C1_UpdateCompass(void)
496
void I2C1_UpdateCompass(void)
497
{
497
{
498
        static u32 TimerCompassUpdate = 0;
498
        static u32 TimerCompassUpdate = 0;
Line 499... Line 499...
499
 
499
 
-
 
500
        if(I2C_State == I2C_OFF) return;
-
 
501
       
500
        if(I2C_State != I2C_OFF)
502
        if(CheckDelay(TimerCompassUpdate))
-
 
503
        {
501
        {
504
                // check for incomming compass calibration request
-
 
505
                // update CalByte from spi input queue
-
 
506
                fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte));
-
 
507
                // send new calstate
502
                if (CheckDelay(TimerCompassUpdate))
508
                if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte)
503
                {
-
 
504
                        if(CheckDelay(I2C1_Timeout))
-
 
505
                        {
-
 
506
                                // reset I2C
-
 
507
                                I2C1_Deinit();
-
 
508
                                I2C1_Init();           
-
 
509
                        }
-
 
510
                        else
-
 
511
                        {
-
 
512
                                // check for incomming compass calibration request
-
 
513
                                // update CalByte from spi input queue
-
 
514
                                fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte));
-
 
515
                                // send new calstate
-
 
516
                                if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte)
-
 
517
                                {
509
                {
518
                                        I2C1_SendCommand(I2C_CMD_WRITE_CAL);
510
                        I2C1_SendCommand(I2C_CMD_WRITE_CAL);
519
                                }
511
                }
520
                                else // request current heading
512
                else // request current heading
521
                                {
513
                {
522
                                        I2C1_SendCommand(I2C_CMD_READ_HEADING);
-
 
523
                                }
-
 
524
                        }
-
 
525
                        TimerCompassUpdate = SetDelay(25);    // every 25 ms
514
                        I2C1_SendCommand(I2C_CMD_READ_HEADING);
-
 
515
                }
526
                }
516
                TimerCompassUpdate = SetDelay(25);    // every 25 ms
527
        }
517
        }