Subversion Repositories NaviCtrl

Rev

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

Rev 454 Rev 465
Line 57... Line 57...
57
#include <stdio.h>
57
#include <stdio.h>
58
#include <string.h>
58
#include <string.h>
59
#include "91x_lib.h"
59
#include "91x_lib.h"
60
#include "ncmag.h"
60
#include "ncmag.h"
61
#include "i2c.h"
61
#include "i2c.h"
-
 
62
#include "i2c0.h"
-
 
63
 
62
#include "timer1.h"
64
#include "timer1.h"
63
#include "led.h"
65
#include "led.h"
64
#include "uart1.h"
66
#include "uart1.h"
65
#include "eeprom.h"
67
#include "eeprom.h"
66
#include "mymath.h"
68
#include "mymath.h"
Line 68... Line 70...
68
#include "spi_slave.h"
70
#include "spi_slave.h"
Line 69... Line 71...
69
 
71
 
70
u8 NCMAG_Present = 0;
72
u8 NCMAG_Present = 0;
Line -... Line 73...
-
 
73
u8 NCMAG_IsCalibrated = 0;
-
 
74
 
-
 
75
u8 *I2C_BufferPnt;
-
 
76
u8 *I2C_ErrorPnt;
-
 
77
I2C_TransmissionFunc_t                          I2C_TransmissionFunc;
-
 
78
I2C_LockBufferFunc_t                            I2C_LockBufferFunc;
-
 
79
I2C_WaitForEndOfTransmissionFunc_t      I2C_WaitForEndOfTransmissionFunc;
71
u8 NCMAG_IsCalibrated = 0;
80
 
72
 
81
 
73
// supported magnetic sensor types
82
// supported magnetic sensor types
74
#define TYPE_NONE                       0
83
#define TYPE_NONE                       0
75
#define TYPE_HMC5843            1
84
#define TYPE_HMC5843            1
Line 218... Line 227...
218
} __attribute__((packed)) MagConfig_t;
227
} __attribute__((packed)) MagConfig_t;
Line 219... Line 228...
219
 
228
 
Line -... Line 229...
-
 
229
volatile MagConfig_t MagConfig;
-
 
230
 
-
 
231
 
220
volatile MagConfig_t MagConfig;
232
 
221
 
233
 
222
 
234
 
223
// self test value
235
// self test value
224
#define LSM303DLH_TEST_XSCALE   495
236
#define LSM303DLH_TEST_XSCALE   495
Line 543... Line 555...
543
// ---------------------------------------------------------------------
555
// ---------------------------------------------------------------------
544
u8 NCMAG_SetMagConfig(void)
556
u8 NCMAG_SetMagConfig(void)
545
{
557
{
546
        u8 retval = 0;
558
        u8 retval = 0;
547
        // try to catch the i2c buffer within 100 ms timeout
559
        // try to catch the i2c buffer within 100 ms timeout
548
        if(I2C_LockBuffer(100))
560
        if(I2C_LockBufferFunc(100))
549
        {
561
        {
550
                u8 TxBytes = 0;
562
                u8 TxBytes = 0;
551
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;    
563
                I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;        
552
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
564
                memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
553
                TxBytes += sizeof(MagConfig);
565
                TxBytes += sizeof(MagConfig);
554
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
566
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
555
                {
567
                {
556
                        if(I2C_WaitForEndOfTransmission(100))
568
                        if(I2C_WaitForEndOfTransmissionFunc(100))
557
                        {
569
                        {
558
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
570
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
559
                        }
571
                        }
560
                }
572
                }
561
        }
573
        }
562
        return(retval);        
574
        return(retval);        
563
}
575
}
Line 565... Line 577...
565
// ----------------------------------------------------------------------------------------
577
// ----------------------------------------------------------------------------------------
566
u8 NCMAG_GetMagConfig(void)
578
u8 NCMAG_GetMagConfig(void)
567
{
579
{
568
        u8 retval = 0;
580
        u8 retval = 0;
569
        // try to catch the i2c buffer within 100 ms timeout
581
        // try to catch the i2c buffer within 100 ms timeout
570
        if(I2C_LockBuffer(100))
582
        if(I2C_LockBufferFunc(100))
571
        {
583
        {
572
                u8 TxBytes = 0;
584
                u8 TxBytes = 0;
573
                I2C_Buffer[TxBytes++] = REG_MAG_CRA;
585
                I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;
574
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
586
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
575
                {
587
                {
576
                        if(I2C_WaitForEndOfTransmission(100))
588
                        if(I2C_WaitForEndOfTransmissionFunc(100))
577
                        {
589
                        {
578
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
590
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
579
                        }
591
                        }
580
                }
592
                }
581
        }
593
        }
582
        return(retval);        
594
        return(retval);        
583
}
595
}
Line 585... Line 597...
585
// ----------------------------------------------------------------------------------------
597
// ----------------------------------------------------------------------------------------
586
u8 NCMAG_SetAccConfig(void)
598
u8 NCMAG_SetAccConfig(void)
587
{
599
{
588
        u8 retval = 0;
600
        u8 retval = 0;
589
        // try to catch the i2c buffer within 100 ms timeout
601
        // try to catch the i2c buffer within 100 ms timeout
590
        if(I2C_LockBuffer(100))
602
        if(I2C_LockBufferFunc(100))
591
        {
603
        {
592
                u8 TxBytes = 0;
604
                u8 TxBytes = 0;
593
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;      
605
                I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;    
594
                memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
606
                memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
595
                TxBytes += sizeof(AccConfig);
607
                TxBytes += sizeof(AccConfig);
596
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
608
                if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
597
                {
609
                {
598
                        if(I2C_WaitForEndOfTransmission(100))
610
                        if(I2C_WaitForEndOfTransmissionFunc(100))
599
                        {
611
                        {
600
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
612
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
601
                        }
613
                        }
602
                }
614
                }
603
        }
615
        }
604
        return(retval);        
616
        return(retval);        
605
}
617
}
Line 607... Line 619...
607
// ----------------------------------------------------------------------------------------
619
// ----------------------------------------------------------------------------------------
608
u8 NCMAG_GetAccConfig(void)
620
u8 NCMAG_GetAccConfig(void)
609
{
621
{
610
        u8 retval = 0;
622
        u8 retval = 0;
611
        // try to catch the i2c buffer within 100 ms timeout
623
        // try to catch the i2c buffer within 100 ms timeout
612
        if(I2C_LockBuffer(100))
624
        if(I2C_LockBufferFunc(100))
613
        {
625
        {
614
                u8 TxBytes = 0;
626
                u8 TxBytes = 0;
615
                I2C_Buffer[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
627
                I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
616
                if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
628
                if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
617
                {
629
                {
618
                        if(I2C_WaitForEndOfTransmission(100))
630
                        if(I2C_WaitForEndOfTransmissionFunc(100))
619
                        {
631
                        {
620
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
632
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
621
                        }
633
                        }
622
                }
634
                }
623
        }
635
        }
624
        return(retval);        
636
        return(retval);        
625
}
637
}
Line 627... Line 639...
627
// ----------------------------------------------------------------------------------------
639
// ----------------------------------------------------------------------------------------
628
u8 NCMAG_GetIdentification(void)
640
u8 NCMAG_GetIdentification(void)
629
{
641
{
630
        u8 retval = 0;
642
        u8 retval = 0;
631
        // try to catch the i2c buffer within 100 ms timeout
643
        // try to catch the i2c buffer within 100 ms timeout
632
        if(I2C_LockBuffer(100))
644
        if(I2C_LockBufferFunc(100))
633
        {
645
        {
634
                u16 TxBytes = 0;
646
                u16 TxBytes = 0;
635
                NCMAG_Identification.A = 0xFF;
647
                NCMAG_Identification.A = 0xFF;
636
                NCMAG_Identification.B = 0xFF;
648
                NCMAG_Identification.B = 0xFF;
637
                NCMAG_Identification.C = 0xFF;
649
                NCMAG_Identification.C = 0xFF;
638
                I2C_Buffer[TxBytes++] = REG_MAG_IDA;
650
                I2C_BufferPnt[TxBytes++] = REG_MAG_IDA;
639
                // initiate transmission
651
                // initiate transmission
640
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
652
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
641
                {
653
                {
642
                        if(I2C_WaitForEndOfTransmission(100))
654
                        if(I2C_WaitForEndOfTransmissionFunc(100))
643
                        {
655
                        {
644
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
656
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
645
                        }
657
                        }
646
                }
658
                }
647
        }
659
        }
648
        return(retval);
660
        return(retval);
649
}
661
}
Line 650... Line 662...
650
 
662
 
651
u8 NCMAG_GetIdentification_Sub(void)
663
u8 NCMAG_GetIdentification_Sub(void)
652
{
664
{
653
        u8 retval = 0;
665
        u8 retval = 0;
654
        // try to catch the i2c buffer within 100 ms timeout
666
        // try to catch the i2c buffer within 100 ms timeout
655
        if(I2C_LockBuffer(100))
667
        if(I2C_LockBufferFunc(100))
656
        {
668
        {
657
                u16 TxBytes = 0;
669
                u16 TxBytes = 0;
658
                NCMAG_Identification2.Sub = 0xFF;
670
                NCMAG_Identification2.Sub = 0xFF;
659
                I2C_Buffer[TxBytes++] = REG_MAG_IDF;
671
                I2C_BufferPnt[TxBytes++] = REG_MAG_IDF;
660
                // initiate transmission
672
                // initiate transmission
661
                if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
673
                if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
662
                {
674
                {
663
                        if(I2C_WaitForEndOfTransmission(100))
675
                        if(I2C_WaitForEndOfTransmissionFunc(100))
664
                        {
676
                        {
665
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
677
                                if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
666
                        }
678
                        }
667
                }
679
                }
668
        }
680
        }
669
        return(retval);
681
        return(retval);
Line 672... Line 684...
672
 
684
 
673
// ----------------------------------------------------------------------------------------
685
// ----------------------------------------------------------------------------------------
674
void NCMAG_GetMagVector(void)
686
void NCMAG_GetMagVector(void)
675
{
687
{
676
        // try to catch the I2C buffer within 0 ms
688
        // try to catch the I2C buffer within 0 ms
677
        if(I2C_LockBuffer(0))
689
        if(I2C_LockBufferFunc(0))
678
        {
690
        {
679
                u16 TxBytes = 0;
691
                u16 TxBytes = 0;
680
                // set register pointer
692
                // set register pointer
681
                I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB;
693
                I2C_BufferPnt[TxBytes++] = REG_MAG_DATAX_MSB;
682
                // initiate transmission
694
                // initiate transmission
683
                I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
695
                I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
684
        }
696
        }
Line 685... Line 697...
685
}
697
}
686
 
698
 
687
//----------------------------------------------------------------
699
//----------------------------------------------------------------
688
void NCMAG_GetAccVector(void)
700
void NCMAG_GetAccVector(void)
689
{
701
{
690
        // try to catch the I2C buffer within 0 ms
702
        // try to catch the I2C buffer within 0 ms
691
        if(I2C_LockBuffer(0))
703
        if(I2C_LockBufferFunc(0))
692
        {
704
        {
693
                u16 TxBytes = 0;
705
                u16 TxBytes = 0;
694
                // set register pointer
706
                // set register pointer
695
                I2C_Buffer[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
707
                I2C_BufferPnt[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
696
                // initiate transmission
708
                // initiate transmission
697
                I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
709
                I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
Line 698... Line 710...
698
        }
710
        }
699
}
711
}
Line 902... Line 914...
902
        return(retval);
914
        return(retval);
903
}
915
}
Line 904... Line 916...
904
 
916
 
-
 
917
 
-
 
918
//----------------------------------------------------------------
-
 
919
void NCMAG_SelectI2CBus(u8 busno)
-
 
920
{
-
 
921
  if (busno == 0)
-
 
922
  {
-
 
923
    I2C_WaitForEndOfTransmissionFunc = &I2C0_WaitForEndOfTransmission;
-
 
924
        I2C_LockBufferFunc = &I2C0_LockBuffer;
-
 
925
        I2C_TransmissionFunc = &I2C0_Transmission;
-
 
926
        I2C_BufferPnt = I2C0_Buffer;
-
 
927
        I2C_ErrorPnt = &I2C0_Error;
-
 
928
  }
-
 
929
   else
-
 
930
  {
-
 
931
    I2C_WaitForEndOfTransmissionFunc = &I2C_WaitForEndOfTransmission;
-
 
932
        I2C_LockBufferFunc = &I2C_LockBuffer;
-
 
933
        I2C_TransmissionFunc = &I2C_Transmission;
-
 
934
        I2C_BufferPnt = I2C_Buffer;
-
 
935
        I2C_ErrorPnt = &I2C_Error;
-
 
936
  }
-
 
937
}
905
 
938
 
906
//----------------------------------------------------------------
939
//----------------------------------------------------------------
907
u8 NCMAG_Init(void)
940
u8 NCMAG_Init(void)
908
{
941
{
909
        u8 msg[64];
942
        u8 msg[64];
Line -... Line 943...
-
 
943
        u8 retval = 0;
-
 
944
        u8 repeat;
910
        u8 retval = 0;
945
 
911
        u8 repeat;
946
        NCMAG_SelectI2CBus(0);
912
 
947
 
913
        NCMAG_Present = 0;
948
        NCMAG_Present = 0;
914
        NCMAG_SensorType = TYPE_HMC5843;        // assuming having an HMC5843
949
        NCMAG_SensorType = TYPE_HMC5843;        // assuming having an HMC5843
915
        // polling for LSM302DLH/DLM option by ACC address ack
950
        // polling for LSM302DLH/DLM option by ACC address ack
916
        repeat = 0;
951
        repeat = 0;
917
        do
952
        do
918
        {
953
        {
919
                retval = NCMAG_GetAccConfig();
954
                retval = NCMAG_GetAccConfig();
920
                if(retval) break; // break loop on success
955
                if(retval) break; // break loop on success
921
                UART1_PutString(".");
956
                UART1_PutString("*");
922
                repeat++;
957
                repeat++;
923
        }while(repeat < 3);
958
        }while(repeat < 3);
Line 945... Line 980...
945
        retval = 0;
980
        retval = 0;
946
        do
981
        do
947
        {
982
        {
948
                retval = NCMAG_GetIdentification();
983
                retval = NCMAG_GetIdentification();
949
                if(retval) break; // break loop on success
984
                if(retval) break; // break loop on success
950
                UART1_PutString(".");
985
                UART1_PutString("#");
951
                repeat++;
986
                repeat++;
952
        }while(repeat < 12);
987
        }while(repeat < 12);
Line 953... Line 988...
953
 
988
 
954
        // if we got an answer to id request
989
        // if we got an answer to id request