Subversion Repositories NaviCtrl

Rev

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

Rev 78 Rev 79
Line 193... Line 193...
193
 
193
 
194
 
194
 
195
//--------------------------------------------------------------
195
//--------------------------------------------------------------
196
void I2C1_Deinit(void)
196
void I2C1_Deinit(void)
Line 197... Line 197...
197
{
197
{
198
        GPIO_InitTypeDef  GPIO_InitStructure;  
198
        GPIO_InitTypeDef  GPIO_InitStructure;
199
 
199
 
200
        SerialPutString("\r\n I2C deinit...");
200
        SerialPutString("\r\n I2C deinit...");
201
        I2C_GenerateStart(I2C1, DISABLE);
201
        I2C_GenerateStart(I2C1, DISABLE);
202
        I2C_GenerateSTOP(I2C1, ENABLE);
202
        I2C_GenerateSTOP(I2C1, ENABLE);
203
        VIC_ITCmd(I2C1_ITLine, DISABLE);
203
        VIC_ITCmd(I2C1_ITLine, DISABLE);
204
        I2C_ITConfig(I2C1, DISABLE);
204
        I2C_ITConfig(I2C1, DISABLE);
205
        I2C_Cmd(I2C1, DISABLE);
205
        I2C_Cmd(I2C1, DISABLE);
206
        I2C_DeInit(I2C1);
206
        I2C_DeInit(I2C1);
207
        SCU_APBPeriphClockConfig(__I2C1, DISABLE);
207
        SCU_APBPeriphClockConfig(__I2C1, DISABLE);
208
       
208
 
209
        // set ports to input
209
        // set ports to input
210
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
210
        SCU_APBPeriphClockConfig(__GPIO2, ENABLE);
211
        GPIO_StructInit(&GPIO_InitStructure);
211
        GPIO_StructInit(&GPIO_InitStructure);
212
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
212
        GPIO_InitStructure.GPIO_Direction =     GPIO_PinInput;
213
    GPIO_InitStructure.GPIO_Pin =                       GPIO_Pin_2 | GPIO_Pin_3;
213
        GPIO_InitStructure.GPIO_Pin =                   GPIO_Pin_2 | GPIO_Pin_3;
214
    GPIO_InitStructure.GPIO_Type =                      GPIO_Type_PushPull;
214
        GPIO_InitStructure.GPIO_Type =                  GPIO_Type_PushPull;
Line 215... Line 215...
215
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Disable;
215
        GPIO_InitStructure.GPIO_IPConnected =   GPIO_IPConnected_Disable;
216
    GPIO_InitStructure.GPIO_Alternate =         GPIO_InputAlt1;
216
        GPIO_InitStructure.GPIO_Alternate =     GPIO_InputAlt1;
217
    GPIO_Init(GPIO2, &GPIO_InitStructure);
217
        GPIO_Init(GPIO2, &GPIO_InitStructure);
Line 218... Line 218...
218
 
218
 
219
        I2C_TxBuffer = NULL;
219
        I2C_TxBuffer = NULL;
220
        Tx_Idx = 0;
220
        Tx_Idx = 0;
Line 240... Line 240...
240
        static u8 crc;
240
        static u8 crc;
241
        // detemine I2C State
241
        // detemine I2C State
242
        status = I2C_GetLastEvent(I2C1);
242
        status = I2C_GetLastEvent(I2C1);
Line 243... Line 243...
243
 
243
 
244
        if(status & (I2C_FLAG_AF|I2C_FLAG_BERR))  // if an acknowledge failure or bus error occured
244
        if(status & (I2C_FLAG_AF|I2C_FLAG_BERR))  // if an acknowledge failure or bus error occured
245
        {   // Set and subsequently clear the STOP bit while BTF is set.
245
        {       // Set and subsequently clear the STOP bit while BTF is set.
246
                while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
246
                while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET)
247
                {
247
                {
248
                        I2C_GenerateSTOP (I2C1, ENABLE); // free the bus
248
                        I2C_GenerateSTOP (I2C1, ENABLE); // free the bus
249
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
249
                        I2C_GenerateSTOP (I2C1, DISABLE); // free the bus
Line 262... Line 262...
262
                                switch(I2C_Direction)
262
                                switch(I2C_Direction)
263
                                {
263
                                {
264
                                        case I2C_MODE_TRANSMITTER:
264
                                        case I2C_MODE_TRANSMITTER:
265
                                                I2C_State = I2C_TX_PROGRESS;
265
                                                I2C_State = I2C_TX_PROGRESS;
266
                                                break;
266
                                                break;
267
       
267
 
268
                                        case I2C_MODE_RECEIVER:
268
                                        case I2C_MODE_RECEIVER:
269
                                                if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0))
269
                                                if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0))
270
                                                {
270
                                                {
271
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
271
                                                        I2C_GenerateSTOP (I2C1, ENABLE);
272
                                                        I2C_State = I2C_IDLE;
272
                                                        I2C_State = I2C_IDLE;
Line 275... Line 275...
275
                                                else
275
                                                else
276
                                                {
276
                                                {
277
                                                        I2C_State = I2C_RX_PROGRESS;
277
                                                        I2C_State = I2C_RX_PROGRESS;
278
                                                }
278
                                                }
279
                                                break;
279
                                                break;
280
       
280
 
281
                                        default: // invalid direction
281
                                        default: // invalid direction
282
                                                I2C_GenerateSTOP (I2C1, ENABLE);
282
                                                I2C_GenerateSTOP (I2C1, ENABLE);
283
                                                I2C_State = I2C_IDLE;
283
                                                I2C_State = I2C_IDLE;
284
                                                LED_GRN_OFF;
284
                                                LED_GRN_OFF;
285
                                                return;
285
                                                return;
Line 287... Line 287...
287
                                // enable acknowledge
287
                                // enable acknowledge
288
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
288
                                I2C_AcknowledgeConfig (I2C1, ENABLE);
289
                                // send address/direction byte on the bus
289
                                // send address/direction byte on the bus
290
                                I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction);
290
                                I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction);
291
                                break;
291
                                break;
292
       
292
 
293
                        // the address byte was send
293
                        // the address byte was send
294
                        case I2C_EVENT_MASTER_MODE_SELECTED:
294
                        case I2C_EVENT_MASTER_MODE_SELECTED:
295
                                // Clear EV6 by set again the PE bit
295
                                // Clear EV6 by set again the PE bit
296
                                I2C_Cmd(I2C1, ENABLE);
296
                                I2C_Cmd(I2C1, ENABLE);
297
                                // reset checksum
297
                                // reset checksum
298
                                crc = 0;
298
                                crc = 0;
299
                                switch(I2C_State)
299
                                switch(I2C_State)
300
                                {
300
                                {
301
                                        case I2C_TX_PROGRESS:
301
                                        case I2C_TX_PROGRESS:
302
                                        // send command 1st data byte (allways the command id) 
302
                                        // send command 1st data byte (allways the command id)
303
                                        I2C_SendData(I2C1, I2C_Command);
303
                                        I2C_SendData(I2C1, I2C_Command);
304
                                        crc += I2C_Command;
304
                                        crc += I2C_Command;
305
                                        Tx_Idx = 0;
305
                                        Tx_Idx = 0;
306
                                        // reset timeout
306
                                        // reset timeout
307
                                        I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset
307
                                        I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset
308
                                        break;
308
                                        break;
309
       
309
 
310
                                        case I2C_RX_PROGRESS:
310
                                        case I2C_RX_PROGRESS:
311
                                        Rx_Idx = 0;
311
                                        Rx_Idx = 0;
312
                                        break;
312
                                        break;
313
       
313
 
314
                                        default: // unknown I2C state
314
                                        default: // unknown I2C state
315
                                        // should never happen
315
                                        // should never happen
316
                                        I2C_GenerateSTOP (I2C1, ENABLE);
316
                                        I2C_GenerateSTOP (I2C1, ENABLE);
317
                                        I2C_State = I2C_IDLE;
317
                                        I2C_State = I2C_IDLE;
318
                                        break;
318
                                        break;
319
                                }
319
                                }
320
                                break;
320
                                break;
321
       
321
 
322
                        // the master has transmitted a byte and slave has been acknowledged
322
                        // the master has transmitted a byte and slave has been acknowledged
323
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
323
                        case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
324
                                       
324
 
325
                                // some bytes have to be transmitted
325
                                // some bytes have to be transmitted
326
                                if(Tx_Idx < I2C_TxBufferSize)
326
                                if(Tx_Idx < I2C_TxBufferSize)
327
                                {
327
                                {
328
                                        if(I2C_TxBuffer != NULL)
328
                                        if(I2C_TxBuffer != NULL)
329
                                        {
329
                                        {
Line 340... Line 340...
340
                                        // send crc byte at the end
340
                                        // send crc byte at the end
341
                                        I2C_SendData(I2C1, crc);
341
                                        I2C_SendData(I2C1, crc);
342
                                        // generate stop or repeated start condition
342
                                        // generate stop or repeated start condition
343
                                        if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected?
343
                                        if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected?
344
                                        {
344
                                        {
345
                                                I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition 
345
                                                I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition
346
                                                I2C_GenerateStart(I2C1, ENABLE);   // initiate repeated start condition on the bus
346
                                                I2C_GenerateStart(I2C1, ENABLE);   // initiate repeated start condition on the bus
347
                                        }
347
                                        }
348
                                        else
348
                                        else
349
                                        {   // stop communication
349
                                        {   // stop communication
350
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
350
                                                I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus
351
                                                I2C_State = I2C_IDLE;                   // ready for new actions
351
                                                I2C_State = I2C_IDLE;                   // ready for new actions
352
                                                LED_GRN_OFF;
352
                                                LED_GRN_OFF;
353
                                                DebugOut.Analog[15]++;
353
                                                DebugOut.Analog[15]++;
354
                                        }
354
                                        }
355
                                }
355
                                }
356
                                Tx_Idx++;      
356
                                Tx_Idx++;
357
                                break;
357
                                break;
358
       
358
 
359
                        // the master has received a byte from the slave
359
                        // the master has received a byte from the slave
360
                case I2C_EVENT_MASTER_BYTE_RECEIVED:
360
                        case I2C_EVENT_MASTER_BYTE_RECEIVED:
361
                                // some bytes have to be received
361
                                // some bytes have to be received
362
                                if (Rx_Idx < I2C_RxBufferSize)
362
                                if (Rx_Idx < I2C_RxBufferSize)
363
                                {       // copy received byte  from the data register to the rx-buffer
363
                                {       // copy received byte  from the data register to the rx-buffer
364
                                        I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
364
                                        I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1);
365
                                        // update checksum
365
                                        // update checksum
366
                                        crc += I2C_PrimRxBuffer[Rx_Idx];
366
                                        crc += I2C_PrimRxBuffer[Rx_Idx];
367
                                }
367
                                }
368
                                // if the last byte (crc) was received
368
                                // if the last byte (crc) was received
369
                                else if ( Rx_Idx == I2C_RxBufferSize)
369
                                else if ( Rx_Idx == I2C_RxBufferSize)
370
                                {
370
                                {
371
                                        // generate a STOP condition on the bus before reading data register
371
                                        // generate a STOP condition on the bus before reading data register
372
                                        I2C_GenerateSTOP(I2C1, ENABLE);
372
                                        I2C_GenerateSTOP(I2C1, ENABLE);
373
                                        // compare last byte with checksum
373
                                        // compare last byte with checksum
374
                                        if(crc == I2C_ReceiveData(I2C1))
374
                                        if(crc == I2C_ReceiveData(I2C1))
Line 383... Line 383...
383
                                        else // checksum error detected
383
                                        else // checksum error detected
384
                                        {
384
                                        {
385
                                                DebugOut.Analog[14]++;
385
                                                DebugOut.Analog[14]++;
386
                                        }
386
                                        }
387
                                        I2C_State = I2C_IDLE;
387
                                        I2C_State = I2C_IDLE;
388
                                        LED_GRN_OFF;   
388
                                        LED_GRN_OFF;
389
                                }
389
                                }
390
                                Rx_Idx++;
390
                                Rx_Idx++;
391
                                // if the 2nd last byte was received disable acknowledge for the last one
391
                                // if the 2nd last byte was received disable acknowledge for the last one
392
                                if ( Rx_Idx == I2C_RxBufferSize )
392
                                if ( Rx_Idx == I2C_RxBufferSize )
393
                                {
393
                                {
394
                                        I2C_AcknowledgeConfig (I2C1, DISABLE);
394
                                        I2C_AcknowledgeConfig (I2C1, DISABLE);
395
                                }
395
                                }
396
                                break;
396
                                break;
397
       
397
 
398
                        default:
398
                        default:
399
                                break;
399
                                break;
400
                }
400
                }
401
        }
401
        }
402
}
402
}
Line 411... Line 411...
411
        // update current command id
411
        // update current command id
412
        I2C_Command = command;
412
        I2C_Command = command;
413
        // set pointers to data area with respect to the command id
413
        // set pointers to data area with respect to the command id
414
        switch (command)
414
        switch (command)
415
        {
415
        {
416
            case I2C_CMD_VERSION:
416
                case I2C_CMD_VERSION:
417
                        I2C_RxBuffer = (u8 *)&MK3MAG_Version;
417
                        I2C_RxBuffer = (u8 *)&MK3MAG_Version;
418
                        I2C_RxBufferSize = sizeof(MK3MAG_Version);
418
                        I2C_RxBufferSize = sizeof(MK3MAG_Version);
419
                        I2C_TxBuffer = NULL;
419
                        I2C_TxBuffer = NULL;
420
                        I2C_TxBufferSize = 0;
420
                        I2C_TxBufferSize = 0;
421
                        break;
421
                        break;
Line 423... Line 423...
423
                        I2C_RxBuffer = (u8 *)&I2C_ReadCal;
423
                        I2C_RxBuffer = (u8 *)&I2C_ReadCal;
424
                        I2C_RxBufferSize = sizeof(I2C_ReadCal);
424
                        I2C_RxBufferSize = sizeof(I2C_ReadCal);
425
                        I2C_TxBuffer = (u8 *)&I2C_WriteCal;
425
                        I2C_TxBuffer = (u8 *)&I2C_WriteCal;
426
                        I2C_TxBufferSize = sizeof(I2C_WriteCal);
426
                        I2C_TxBufferSize = sizeof(I2C_WriteCal);
427
                        break;
427
                        break;
428
            case I2C_CMD_READ_MAG:
428
                case I2C_CMD_READ_MAG:
429
                        I2C_RxBuffer = (u8 *)&I2C_Mag;
429
                        I2C_RxBuffer = (u8 *)&I2C_Mag;
430
                        I2C_RxBufferSize = sizeof(I2C_Mag);
430
                        I2C_RxBufferSize = sizeof(I2C_Mag);
431
                        I2C_TxBuffer = NULL;
431
                        I2C_TxBuffer = NULL;
432
                        I2C_TxBufferSize = 0;
432
                        I2C_TxBufferSize = 0;
433
                        break;
433
                        break;
434
            case I2C_CMD_READ_HEADING:
434
                case I2C_CMD_READ_HEADING:
435
                        DebugOut.Analog[10] = I2C_Heading.Heading;
435
                        DebugOut.Analog[10] = I2C_Heading.Heading;
436
                        I2C_RxBuffer = (u8 *)&I2C_Heading;
436
                        I2C_RxBuffer = (u8 *)&I2C_Heading;
437
                        I2C_RxBufferSize = sizeof(I2C_Heading);
437
                        I2C_RxBufferSize = sizeof(I2C_Heading);
438
                        // updat atitude from spi rx buffer
438
                        // updat atitude from spi rx buffer
439
                        I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll;
439
                        I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll;
Line 450... Line 450...
450
        }
450
        }
451
        // enable I2C IRQ again
451
        // enable I2C IRQ again
452
        I2C_ITConfig(I2C1, ENABLE);
452
        I2C_ITConfig(I2C1, ENABLE);
453
        // set direction to master transmitter
453
        // set direction to master transmitter
454
        I2C_Direction = I2C_MODE_TRANSMITTER;
454
        I2C_Direction = I2C_MODE_TRANSMITTER;
455
        // test on busy flag and clear it 
455
        // test on busy flag and clear it
456
        I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
456
        I2C_CheckEvent( I2C1, I2C_FLAG_BUSY );
457
        // initiale start condition on the bus 
457
        // initiale start condition on the bus
458
        I2C_GenerateStart(I2C1, ENABLE);
458
        I2C_GenerateStart(I2C1, ENABLE);
459
        // to be continued in the I2C1_IRQHandler() above
459
        // to be continued in the I2C1_IRQHandler() above
460
 
-
 
461
       
-
 
462
   
-
 
463
}
460
}
Line 464... Line 461...
464
 
461
 
465
//----------------------------------------------------------------
462
//----------------------------------------------------------------
466
void I2C1_GetMK3MagVersion(void)
463
void I2C1_GetMK3MagVersion(void)
467
{
464
{
468
        u8 msg[64];
465
        u8 msg[64];
Line 469... Line 466...
469
    u8 repeat = 10;
466
        u8 repeat = 10;
470
 
467
 
471
        MK3MAG_Version.Major = 0xFF;
468
        MK3MAG_Version.Major = 0xFF;
472
        MK3MAG_Version.Minor = 0xFF;
469
        MK3MAG_Version.Minor = 0xFF;
Line 473... Line 470...
473
        MK3MAG_Version.Patch = 0xFF;
470
        MK3MAG_Version.Patch = 0xFF;
474
        MK3MAG_Version.Compatible = 0xFF;
471
        MK3MAG_Version.Compatible = 0xFF;
475
 
472
 
476
        while ((MK3MAG_Version.Major == 0xFF)  && (repeat != 0))
473
        while ((MK3MAG_Version.Major == 0xFF)  && (repeat != 0))
477
        {
474
        {
478
                I2C1_SendCommand(I2C_CMD_VERSION);     
475
                I2C1_SendCommand(I2C_CMD_VERSION);
479
                I2C1_Timeout = SetDelay(500);
476
                I2C1_Timeout = SetDelay(500);
480
                while (!CheckDelay(I2C1_Timeout)) if (MK3MAG_Version.Major != 0xFF) break;
477
                while (!CheckDelay(I2C1_Timeout)) if (MK3MAG_Version.Major != 0xFF) break;
481
                repeat--;
478
                repeat--;
482
        }
479
        }
483
   
480
 
484
        if (MK3MAG_Version.Major != 0xFF)
481
        if (MK3MAG_Version.Major != 0xFF)
485
        {
482
        {
486
                sprintf(msg, "\n\r MK3Mag V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch);
483
                sprintf(msg, "\n\r MK3Mag V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch);
487
                SerialPutString(msg);
484
                SerialPutString(msg);
488
                sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible);
485
                sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible);
Line 489... Line 486...
489
                SerialPutString(msg);
486
                SerialPutString(msg);