Subversion Repositories NaviCtrl

Rev

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

Rev 470 Rev 472
Line 70... Line 70...
70
#include "spi_slave.h"
70
#include "spi_slave.h"
Line 71... Line 71...
71
 
71
 
72
u8 NCMAG_Present = 0;
72
u8 NCMAG_Present = 0;
Line -... Line 73...
-
 
73
u8 NCMAG_IsCalibrated = 0;
-
 
74
 
73
u8 NCMAG_IsCalibrated = 0;
75
u8 I2C_CompassPort = 1;
74
 
76
 
75
u8 *I2C_BufferPnt;
77
u8 *I2C_BufferPnt;
76
u8 *I2C_ErrorPnt;
78
u8 *I2C_ErrorPnt;
77
I2C_TransmissionFunc_t                          I2C_TransmissionFunc;
79
I2C_TransmissionFunc_t                          I2C_TransmissionFunc;
Line 85... Line 87...
85
#define TYPE_LSM303DLH          2
87
#define TYPE_LSM303DLH          2
86
#define TYPE_LSM303DLM          3
88
#define TYPE_LSM303DLM          3
Line 87... Line 89...
87
 
89
 
Line -... Line 90...
-
 
90
u8 NCMAG_SensorType = TYPE_NONE;
88
u8 NCMAG_SensorType = TYPE_NONE;
91
 
-
 
92
#define EEPROM_ADR_MAG_CALIBRATION_INTERN       50      // two calibrtion sets for extern and intern sensor
89
 
93
#define EEPROM_ADR_MAG_CALIBRATION_EXTERN   70
90
#define EEPROM_ADR_MAG_CALIBRATION              50
94
 
Line 91... Line 95...
91
#define CALIBRATION_VERSION                     1
95
#define CALIBRATION_VERSION                     1
92
#define MAG_CALIBRATION_COMPATIBLE              0xA2
96
#define MAG_CALIBRATION_COMPATIBLE              0xA2
Line 304... Line 308...
304
        u8 ctrl_5;
308
        u8 ctrl_5;
305
} __attribute__((packed)) AccConfig_t;
309
} __attribute__((packed)) AccConfig_t;
Line 306... Line 310...
306
 
310
 
Line 307... Line 311...
307
volatile AccConfig_t AccConfig;
311
volatile AccConfig_t AccConfig;
308
 
312
 
-
 
313
u8 NCMag_CalibrationWrite(u8 intern) // two calibrtion sets for extern and intern sensor
309
u8 NCMag_CalibrationWrite(void)
314
{
310
{
315
        u16 address;
311
        u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
316
        u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
Line -... Line 317...
-
 
317
        EEPROM_Result_t eres;
-
 
318
        u8 *pBuff = (u8*)&Calibration;
-
 
319
 
312
        EEPROM_Result_t eres;
320
        if(intern == I2C_INTERN_1) address = EEPROM_ADR_MAG_CALIBRATION_INTERN;
313
        u8 *pBuff = (u8*)&Calibration;
321
        else address = EEPROM_ADR_MAG_CALIBRATION_EXTERN;
314
 
322
 
315
        Calibration.Version = CALIBRATION_VERSION;
323
        Calibration.Version = CALIBRATION_VERSION;
316
        for(i = 0; i<(sizeof(Calibration)-1); i++)
324
        for(i = 0; i<(sizeof(Calibration)-1); i++)
317
        {
325
        {
318
                crc += pBuff[i];        
326
                crc += pBuff[i];        
319
        }
327
        }
320
        Calibration.crc = ~crc;
328
        Calibration.crc = ~crc;
321
        eres = EEPROM_WriteBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration));
329
        eres = EEPROM_WriteBlock(address, pBuff, sizeof(Calibration));
322
        if(EEPROM_SUCCESS == eres) i = 1;
330
        if(EEPROM_SUCCESS == eres) i = 1;
Line 323... Line 331...
323
        else i = 0;
331
        else i = 0;
324
        return(i);     
332
        return(i);     
325
}
333
}
326
 
334
 
-
 
335
u8 NCMag_CalibrationRead(u8 intern)     // two calibrtion sets for extern and intern sensor
Line -... Line 336...
-
 
336
{
-
 
337
        u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
-
 
338
        u8 *pBuff = (u8*)&Calibration;
327
u8 NCMag_CalibrationRead(void)
339
        u16 address;
328
{
340
 
329
        u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
341
        if(intern == I2C_INTERN_1) address = EEPROM_ADR_MAG_CALIBRATION_INTERN;
330
        u8 *pBuff = (u8*)&Calibration;
342
        else address = EEPROM_ADR_MAG_CALIBRATION_EXTERN;
331
 
343
 
332
        if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)))
344
        if(EEPROM_SUCCESS == EEPROM_ReadBlock(address, pBuff, sizeof(Calibration)))
Line 423... Line 435...
423
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
435
                                Calibration.MagY.Offset = (Ymin + Ymax) / 2;
424
                                Calibration.MagZ.Range = Zmax - Zmin;
436
                                Calibration.MagZ.Range = Zmax - Zmin;
425
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
437
                                Calibration.MagZ.Offset = (Zmin + Zmax) / 2;
426
                                if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration))
438
                                if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration))
427
                                {
439
                                {
428
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite();
440
                                        NCMAG_IsCalibrated = NCMag_CalibrationWrite(I2C_CompassPort);
429
                                        BeepTime = 2500;
441
                                        BeepTime = 2500;
430
                                        UART1_PutString("\r\n-> Calibration okay <-\n\r");
442
                                        UART1_PutString("\r\n-> Calibration okay <-\n\r");
431
SpeakHoTT = SPEAK_MIKROKOPTER;
443
SpeakHoTT = SPEAK_MIKROKOPTER;
432
                                }
444
                                }
433
                                else
445
                                else
Line 438... Line 450...
438
                                    if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! ");
450
                                    if(Calibration.MagY.Range < MinCalibration) UART1_PutString("Y! ");
439
                                    if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! ");
451
                                    if(Calibration.MagZ.Range < MinCalibration) UART1_PutString("Z! ");
440
                                        UART1_PutString("\r\n");
452
                                        UART1_PutString("\r\n");
Line 441... Line 453...
441
 
453
 
442
                                        // restore old calibration data from eeprom
454
                                        // restore old calibration data from eeprom
443
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead();
455
                                        NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort);
444
                                }
456
                                }
445
                                        sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin);
457
                                        sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin);
446
                                        UART1_PutString(msg);
458
                                        UART1_PutString(msg);
447
                                        sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin);
459
                                        sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin);
Line 754... Line 766...
754
{
766
{
755
        static u32 TimerUpdate = 0;
767
        static u32 TimerUpdate = 0;
756
        static s8 send_config = 0;
768
        static s8 send_config = 0;
757
        u32 delay = 20;
769
        u32 delay = 20;
758
        // todo State Handling for both busses  !!
770
        // todo State Handling for both busses  !!
759
        if( (I2C1_State == I2C_STATE_OFF) || !NCMAG_Present )
771
        if((I2C1_State == I2C_STATE_OFF) || (I2C_CompassPort == 0 && I2C0_State == I2C_STATE_OFF) || !NCMAG_Present )
760
        {
772
        {
761
                Compass_Heading = -1;
773
                Compass_Heading = -1;
762
                DebugOut.Analog[14]++; // count I2C error
774
                DebugOut.Analog[14]++; // count I2C error
763
                return;
775
                return;
764
        }
776
        }
Line 941... Line 953...
941
{
953
{
942
        u8 msg[64];
954
        u8 msg[64];
943
        u8 retval = 0;
955
        u8 retval = 0;
944
        u8 repeat;
956
        u8 repeat;
Line -... Line 957...
-
 
957
 
-
 
958
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
959
// Search external sensor
-
 
960
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
961
        I2C_CompassPort = I2C_EXTERN_0;
-
 
962
        NCMAG_SelectI2CBus(I2C_CompassPort);
-
 
963
 
-
 
964
        // get id bytes
-
 
965
        retval = 0;
-
 
966
        do
-
 
967
        {
-
 
968
                retval = NCMAG_GetIdentification();
-
 
969
                if(retval) break; // break loop on success
-
 
970
//              UART1_PutString("+");
-
 
971
                repeat++;
-
 
972
        } while(repeat < 5);
-
 
973
 
-
 
974
        if(!retval)
945
 
975
        {
-
 
976
         UART1_PutString(" internal sensor");
946
        // extern suchen, dann intern suchen
977
         I2C_CompassPort = I2C_INTERN_1;
-
 
978
         NCMAG_SelectI2CBus(I2C_CompassPort);
-
 
979
        }
-
 
980
        else
-
 
981
        {
-
 
982
         UART1_PutString(" external sensor ");
-
 
983
        }
Line 947... Line 984...
947
        NCMAG_SelectI2CBus(0);
984
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
948
 
985
 
949
        NCMAG_Present = 0;
986
        NCMAG_Present = 0;
950
        NCMAG_SensorType = TYPE_HMC5843;        // assuming having an HMC5843
987
        NCMAG_SensorType = TYPE_HMC5843;        // assuming having an HMC5843
951
        // polling for LSM302DLH/DLM option by ACC address ack
988
        // polling for LSM302DLH/DLM option by ACC address ack
952
        repeat = 0;
989
        repeat = 0;
953
        do
990
        do
954
        {
991
        {
955
                retval = NCMAG_GetAccConfig();
992
                retval = NCMAG_GetAccConfig();
956
                if(retval) break; // break loop on success
993
                if(retval) break; // break loop on success
957
                UART1_PutString("*");
994
//              UART1_PutString("*");
958
                repeat++;
995
                repeat++;
959
        }while(repeat < 3);
996
        } while(repeat < 3);
960
        if(retval)
997
        if(retval)
961
        {
998
        {
Line 967... Line 1004...
967
                repeat = 0;
1004
                repeat = 0;
968
                do
1005
                do
969
                {
1006
                {
970
                        retval = NCMAG_GetIdentification_Sub();
1007
                        retval = NCMAG_GetIdentification_Sub();
971
                        if(retval) break; // break loop on success
1008
                        if(retval) break; // break loop on success
972
                        UART1_PutString(".");
1009
//                      UART1_PutString(".");
973
                        repeat++;
1010
                        repeat++;
974
                }while(repeat < 12);
1011
                }while(repeat < 12);
975
                if(retval)
1012
                if(retval)
976
                {
1013
                {
977
                        if(NCMAG_Identification2.Sub == MAG_IDF_LSM303DLM)      NCMAG_SensorType = TYPE_LSM303DLM;
1014
                        if(NCMAG_Identification2.Sub == MAG_IDF_LSM303DLM)      NCMAG_SensorType = TYPE_LSM303DLM;
Line 981... Line 1018...
981
        retval = 0;
1018
        retval = 0;
982
        do
1019
        do
983
        {
1020
        {
984
                retval = NCMAG_GetIdentification();
1021
                retval = NCMAG_GetIdentification();
985
                if(retval) break; // break loop on success
1022
                if(retval) break; // break loop on success
986
                UART1_PutString("#");
1023
//              UART1_PutString("#");
987
                repeat++;
1024
                repeat++;
988
        }while(repeat < 12);
1025
        } while(repeat < 12);
Line 989... Line 1026...
989
 
1026
 
990
        // if we got an answer to id request
1027
        // if we got an answer to id request
991
        if(retval)
1028
        if(retval)
992
        {
1029
        {
Line 1016... Line 1053...
1016
                {
1053
                {
1017
                        NCMAG_Present = 1;
1054
                        NCMAG_Present = 1;
Line 1018... Line 1055...
1018
 
1055
 
1019
                        if(EEPROM_Init())
1056
                        if(EEPROM_Init())
1020
                        {
1057
                        {
1021
                                NCMAG_IsCalibrated = NCMag_CalibrationRead();
1058
                                NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort);
1022
                                if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
1059
                                if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
1023
                        }
1060
                        }
1024
                        else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
1061
                        else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
1025
                        // perform self test
1062
                        // perform self test
1026
                        if(!NCMAG_SelfTest())
1063
                        if(!NCMAG_SelfTest())
1027
                        {
1064
                        {
1028
                                UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n");
1065
                                UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n");
1029
                                LED_RED_ON;
1066
                                LED_RED_ON;
1030
                                NCMAG_IsCalibrated = 0;
1067
//                              NCMAG_IsCalibrated = 0;
1031
                        }
1068
                        }
Line 1032... Line 1069...
1032
                        else UART1_PutString("\r\n Selftest ok");
1069
                        else UART1_PutString("\r\n Selftest ok");
1033
 
1070