Subversion Repositories NaviCtrl

Rev

Rev 251 | Rev 253 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 251 Rev 252
1
/*#######################################################################################*/
1
/*#######################################################################################*/
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
2
/* !!! THIS IS NOT FREE SOFTWARE !!!                                                     */
3
/*#######################################################################################*/
3
/*#######################################################################################*/
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5
// + Copyright (c) 2010 Ingo Busker, Holger Buss
5
// + Copyright (c) 2010 Ingo Busker, Holger Buss
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
6
// + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
7
// + FOR NON COMMERCIAL USE ONLY
8
// + www.MikroKopter.com
8
// + www.MikroKopter.com
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
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.
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
12
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
13
// + bzgl. der Nutzungsbedingungen aufzunehmen.
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
14
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
15
// + Verkauf von Luftbildaufnahmen, usw.
15
// + Verkauf von Luftbildaufnahmen, usw.
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
17
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
18
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
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"
21
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
22
// + eindeutig als Ursprung verlinkt werden
22
// + eindeutig als Ursprung verlinkt werden
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
24
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
25
// + Benutzung auf eigene Gefahr
25
// + Benutzung auf eigene Gefahr
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
26
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
28
// + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
29
// + mit unserer Zustimmung zulässig
29
// + mit unserer Zustimmung zulässig
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
31
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
33
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
34
// + this list of conditions and the following disclaimer.
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
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.
36
// +     from this software without specific prior written permission.
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
37
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
38
// +     for non-commercial use (directly or indirectly)
38
// +     for non-commercial use (directly or indirectly)
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
39
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
40
// +     with our written permission
40
// +     with our written permission
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
41
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
42
// +     clearly linked as origin
42
// +     clearly linked as origin
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
43
// +   * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed
44
//
44
//
45
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
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
46
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
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
52
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53
// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
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
54
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55
// +  POSSIBILITY OF SUCH DAMAGE.
55
// +  POSSIBILITY OF SUCH DAMAGE.
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <string.h>
57
#include <string.h>
58
#include "91x_lib.h"
58
#include "91x_lib.h"
59
#include "hmc5843.h"
59
#include "hmc5843.h"
60
#include "i2c.h"
60
#include "i2c.h"
61
#include "timer1.h"
61
#include "timer1.h"
62
#include "led.h"
62
#include "led.h"
63
#include "spi_slave.h"
63
#include "spi_slave.h"
64
#include "uart1.h"
64
#include "uart1.h"
65
#include "compass.h"
65
#include "compass.h"
66
 
66
 
67
#define HMC5843_SLAVE_ADDRESS           0x3C  // i2C slave address
67
#define HMC5843_SLAVE_ADDRESS           0x3C  // i2C slave address
68
 
68
 
69
u8 HMC5843_Present = 0;
69
u8 HMC5843_Present = 0;
70
 
70
 
71
#define REGISTER_CRA            0
71
#define REGISTER_CRA            0
72
#define REGISTER_CRB            1
72
#define REGISTER_CRB            1
73
#define REGISTER_MODE           2
73
#define REGISTER_MODE           2
74
#define REGISTER_DATAX_MSB      3
74
#define REGISTER_DATAX_MSB      3
75
#define REGISTER_DATAX_LSB      4
75
#define REGISTER_DATAX_LSB      4
76
#define REGISTER_DATAY_MSB      5
76
#define REGISTER_DATAY_MSB      5
77
#define REGISTER_DATAY_LSB      6
77
#define REGISTER_DATAY_LSB      6
78
#define REGISTER_DATAZ_MSB      7
78
#define REGISTER_DATAZ_MSB      7
79
#define REGISTER_DATAZ_LSB      8
79
#define REGISTER_DATAZ_LSB      8
80
#define REGISTER_STATUS         9
80
#define REGISTER_STATUS         9
81
#define REGISTER_IDA            10
81
#define REGISTER_IDA            10
82
#define REGISTER_IDB            11
82
#define REGISTER_IDB            11
83
#define REGISTER_IDC            12
83
#define REGISTER_IDC            12
84
 
84
 
85
 
85
 
86
#define HMC5843_IDA 0x48
86
#define HMC5843_IDA 0x48
87
#define HMC5843_IDB 0x34
87
#define HMC5843_IDB 0x34
88
#define HMC5843_IDC     0x33
88
#define HMC5843_IDC     0x33
89
 
89
 
90
// bit mask for rate
90
// bit mask for rate
91
#define HMC5843_CRA_RATE_MASK           0x1C
91
#define HMC5843_CRA_RATE_MASK           0x1C
92
#define HMC5843_CRA_RATE_0_5HZ          0x00
92
#define HMC5843_CRA_RATE_0_5HZ          0x00
93
#define HMC5843_CRA_RATE_1HZ            0x04
93
#define HMC5843_CRA_RATE_1HZ            0x04
94
#define HMC5843_CRA_RATE_2HZ            0x08
94
#define HMC5843_CRA_RATE_2HZ            0x08
95
#define HMC5843_CRA_RATE_5HZ            0x0C
95
#define HMC5843_CRA_RATE_5HZ            0x0C
96
#define HMC5843_CRA_RATE_10HZ           0x10    //default
96
#define HMC5843_CRA_RATE_10HZ           0x10    //default
97
#define HMC5843_CRA_RATE_20HZ           0x14
97
#define HMC5843_CRA_RATE_20HZ           0x14
98
#define HMC5843_CRA_RATE_50HZ           0x18
98
#define HMC5843_CRA_RATE_50HZ           0x18
99
 
99
 
100
// bit mask for mode
100
// bit mask for mode
101
#define HMC5843_CRA_MODE_MASK           0x03
101
#define HMC5843_CRA_MODE_MASK           0x03
102
#define HMC5843_CRA_MODE_NORMAL         0x00    //default
102
#define HMC5843_CRA_MODE_NORMAL         0x00    //default
103
#define HMC5843_CRA_MODE_POSBIAS        0x01
103
#define HMC5843_CRA_MODE_POSBIAS        0x01
104
#define HMC5843_CRA_MODE_NEGBIAS        0x02
104
#define HMC5843_CRA_MODE_NEGBIAS        0x02
105
#define HMC5843_CRA_MODE_SELFTEST       0x03    
105
#define HMC5843_CRA_MODE_SELFTEST       0x03    
106
 
106
 
107
// bit mask for gain
107
// bit mask for gain
108
#define HMC5843_CRB_GAIN_MASK           0xE0
108
#define HMC5843_CRB_GAIN_MASK           0xE0
109
#define HMC5843_CRB_GAIN_07GA           0x00
109
#define HMC5843_CRB_GAIN_07GA           0x00
110
#define HMC5843_CRB_GAIN_10GA           0x20    //default
110
#define HMC5843_CRB_GAIN_10GA           0x20    //default
111
#define HMC5843_CRB_GAIN_15GA           0x40
111
#define HMC5843_CRB_GAIN_15GA           0x40
112
#define HMC5843_CRB_GAIN_20GA           0x60
112
#define HMC5843_CRB_GAIN_20GA           0x60
113
#define HMC5843_CRB_GAIN_32GA           0x80
113
#define HMC5843_CRB_GAIN_32GA           0x80
114
#define HMC5843_CRB_GAIN_38GA           0xA0
114
#define HMC5843_CRB_GAIN_38GA           0xA0
115
#define HMC5843_CRB_GAIN_45GA           0xC0
115
#define HMC5843_CRB_GAIN_45GA           0xC0
116
#define HMC5843_CRB_GAIN_65GA           0xE0
116
#define HMC5843_CRB_GAIN_65GA           0xE0
117
 
117
 
118
// bit mask for measurement mode
118
// bit mask for measurement mode
119
#define HMC5843_MODE_MASK                       0xFF
119
#define HMC5843_MODE_MASK                       0xFF
120
#define HMC5843_MODE_CONTINUOUS         0x00
120
#define HMC5843_MODE_CONTINUOUS         0x00
121
#define HMC5843_MODE_SINGLE                     0x01    // default
121
#define HMC5843_MODE_SINGLE                     0x01    // default
122
#define HMC5843_MODE_IDLE                       0x02
122
#define HMC5843_MODE_IDLE                       0x02
123
#define HMC5843_MODE_SLEEP                      0x03    
123
#define HMC5843_MODE_SLEEP                      0x03    
124
 
124
 
125
typedef struct
125
typedef struct
126
{
126
{
127
  u8 A;
127
  u8 A;
128
  u8 B;
128
  u8 B;
129
  u8 C;
129
  u8 C;
130
} __attribute__((packed)) HMC5843_Identification_t;
130
} __attribute__((packed)) HMC5843_Identification_t;
131
 
131
 
132
volatile HMC5843_Identification_t HMC5843_Identification;
132
volatile HMC5843_Identification_t HMC5843_Identification;
133
 
133
 
134
 
134
 
135
 
135
 
136
// ---------- call back handlers -----------------------------------------
136
// ---------- call back handlers -----------------------------------------
137
 
137
 
138
// rx data handler for id info request
138
// rx data handler for id info request
139
void HMC5843_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize)
139
void HMC5843_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize)
140
{       // if number of byte are matching
140
{       // if number of byte are matching
141
        if(RxBufferSize == sizeof(HMC5843_Identification) )
141
        if(RxBufferSize == sizeof(HMC5843_Identification) )
142
        {
142
        {
143
                memcpy((u8 *)&HMC5843_Identification, pRxBuffer, sizeof(HMC5843_Identification));
143
                memcpy((u8 *)&HMC5843_Identification, pRxBuffer, sizeof(HMC5843_Identification));
144
        }                
144
        }                
145
}
145
}
146
 
146
 
147
// rx data handler for mag vector request
147
// rx data handler for mag vector request
148
void HMC5843_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
148
void HMC5843_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize)
149
{       // if number of byte are matching
149
{       // if number of byte are matching
150
        if(RxBufferSize == sizeof(MagVector) )
150
        if(RxBufferSize == sizeof(MagVector) )
151
        {       // byte order from big to little endian
151
        {       // byte order from big to little endian
152
                MagVector.X = pRxBuffer[0]<<8;
152
                MagVector.X = pRxBuffer[0]<<8;
153
                MagVector.X+= pRxBuffer[1];
153
                MagVector.X+= pRxBuffer[1];
154
                MagVector.Y = pRxBuffer[2]<<8;
154
                MagVector.Y = pRxBuffer[2]<<8;
155
                MagVector.Y+= pRxBuffer[3];
155
                MagVector.Y+= pRxBuffer[3];
156
                MagVector.Z = pRxBuffer[4]<<8;
156
                MagVector.Z = pRxBuffer[4]<<8;
157
                MagVector.Z+= pRxBuffer[5];
157
                MagVector.Z+= pRxBuffer[5];
158
        }
158
        }
159
        // tbd. calculate heading from mag vector and attitude
159
        // tbd. calculate heading from mag vector and attitude
160
        CompassHeading = 50; // tbd.              
160
        CompassHeading = 50; // tbd.              
161
}
161
}
162
 
162
 
163
 
163
 
164
// ----------------------------------------------------------------------------------------
164
// ----------------------------------------------------------------------------------------
165
 
165
 
166
void HMC5843_GetIdentification(void)
166
u8 HMC5843_GetIdentification(void)
-
 
167
{
-
 
168
        u8 retval = 0; 
167
{
169
        // try to catch the i2c buffer within 100 ms timeout
168
        if(I2C_LockBuffer(100))
170
        if(I2C_LockBuffer(100))
169
        {
171
        {
170
                u16 TxBytes = 0;
172
                u16 TxBytes = 0;
171
                HMC5843_Identification.A = 0xFF;
173
                HMC5843_Identification.A = 0xFF;
172
                HMC5843_Identification.B = 0xFF;
174
                HMC5843_Identification.B = 0xFF;
173
                HMC5843_Identification.C = 0xFF;
175
                HMC5843_Identification.C = 0xFF;
174
                I2C_Buffer[TxBytes++] = REGISTER_IDA;
176
                I2C_Buffer[TxBytes++] = REGISTER_IDA;
175
                // initiate transmission
177
                // initiate transmission
176
                if(I2C_Transmission(HMC5843_SLAVE_ADDRESS, TxBytes, &HMC5843_UpdateIdentification, sizeof(HMC5843_Identification)))
178
                if(I2C_Transmission(HMC5843_SLAVE_ADDRESS, TxBytes, &HMC5843_UpdateIdentification, sizeof(HMC5843_Identification)))
177
                {
179
                {
178
                        I2C_WaitForEndOfTransmission(100);
180
                        if(I2C_WaitForEndOfTransmission(100))
-
 
181
                        {
-
 
182
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
-
 
183
                        }
179
                }
184
                }
180
        }
185
        }
-
 
186
        return(retval);
181
}
187
}
182
 
188
 
183
// ----------------------------------------------------------------------------------------
189
// ----------------------------------------------------------------------------------------
184
u8 HMC5843_SetConfiguration(u8 cra, u8 crb, u8 mode)
190
u8 HMC5843_SetConfiguration(u8 cra, u8 crb, u8 mode)
185
{
191
{
186
u8 retval = 0;
192
        u8 retval = 0;
-
 
193
        // tra to catch the I2c buffer within 100 ms timeout
187
        if(I2C_LockBuffer(100))
194
        if(I2C_LockBuffer(100))
188
        {
195
        {
189
                u16 TxBytes = 0;       
196
                u16 TxBytes = 0;       
190
                I2C_Buffer[TxBytes++] = REGISTER_CRA;
197
                I2C_Buffer[TxBytes++] = REGISTER_CRA;
191
                I2C_Buffer[TxBytes++] = cra;
198
                I2C_Buffer[TxBytes++] = cra;
192
                I2C_Buffer[TxBytes++] = crb;
199
                I2C_Buffer[TxBytes++] = crb;
193
                I2C_Buffer[TxBytes++] = mode;
200
                I2C_Buffer[TxBytes++] = mode;
194
                // initiate transmission
201
                // initiate transmission
195
                if(I2C_Transmission(HMC5843_SLAVE_ADDRESS, TxBytes, NULL, 0))
202
                if(I2C_Transmission(HMC5843_SLAVE_ADDRESS, TxBytes, NULL, 0))
196
                {
203
                {
197
                        retval = I2C_WaitForEndOfTransmission(50);
204
                        if(I2C_WaitForEndOfTransmission(50))
-
 
205
                        {
-
 
206
                                if(I2C_Error == I2C_ERROR_NONE) retval = 1;
-
 
207
                        }
198
                }
208
                }
199
        }
209
        }
200
        return(retval);
210
        return(retval);
201
}
211
}
202
 
212
 
203
//----------------------------------------------------------------
213
//----------------------------------------------------------------
204
void HMC5843_GetMagVector(void)
214
void HMC5843_GetMagVector(void)
205
{
215
{
-
 
216
        // try to catch the I2C buffer within 0 ms
206
        if(I2C_LockBuffer(100))
217
        if(I2C_LockBuffer(0))
207
        {
218
        {
208
                u16 TxBytes = 0;
219
                u16 TxBytes = 0;
209
                // set register pointer
220
                // set register pointer
210
                I2C_Buffer[TxBytes++] = REGISTER_DATAX_MSB;
221
                I2C_Buffer[TxBytes++] = REGISTER_DATAX_MSB;
211
                // initiate transmission
222
                // initiate transmission
212
                I2C_Transmission(HMC5843_SLAVE_ADDRESS, TxBytes, &HMC5843_UpdateMagVector, sizeof(MagVector));
223
                I2C_Transmission(HMC5843_SLAVE_ADDRESS, TxBytes, &HMC5843_UpdateMagVector, sizeof(MagVector));
213
        }
224
        }
214
}
225
}
215
 
226
 
216
void HMC5843_UpdateCompass(void)
227
void HMC5843_UpdateCompass(void)
217
{
228
{
218
        static u32 TimerCompassUpdate = 0;
229
        static u32 TimerCompassUpdate = 0;
219
 
230
 
220
        if( (I2C_State == I2C_STATE_OFF) || !HMC5843_Present ) return;
231
        if( (I2C_State == I2C_STATE_OFF) || !HMC5843_Present ) return;
221
       
232
       
222
        if(CheckDelay(TimerCompassUpdate))
233
        if(CheckDelay(TimerCompassUpdate))
223
        {
234
        {
224
                // check for incomming compass calibration request
235
                // check for incomming compass calibration request
225
                // update CalByte from spi input queue
236
                // update CalByte from spi input queue
226
                /*fifo_get(&CompassCalcStateFiFo, (u8 *)&(MK3MAG_WriteCal.CalByte));
237
                /*fifo_get(&CompassCalcStateFiFo, (u8 *)&(MK3MAG_WriteCal.CalByte));
227
                // send new calstate
238
                // send new calstate
228
                if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte)
239
                if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte)
229
                {
240
                {
230
                        do the calibration here
241
                        do the calibration here
231
                }
242
                }
232
                else // request current heading */
243
                else // request current heading */
233
                {
244
                {
234
                        HMC5843_GetMagVector();
245
                        HMC5843_GetMagVector();
235
                }              
246
                }              
236
                TimerCompassUpdate = SetDelay(20);    // every 20 ms are 50 Hz
247
                TimerCompassUpdate = SetDelay(20);    // every 20 ms are 50 Hz
237
        }
248
        }
238
}
249
}
239
 
250
 
240
u8 HMC5843_SelfTest(void)
251
u8 HMC5843_SelfTest(void)
241
{
252
{
242
        u32 time;
253
        u32 time;
243
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
254
        s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0;
244
        s16 scale, scale_min, scale_max;
255
        s16 scale, scale_min, scale_max;
245
        u8 i = 0;
256
        u8 i = 0;
246
 
257
 
247
        // activate positive bias field  of 0.55 gauss
258
        // activate positive bias field  of 0.55 gauss
248
        HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_POSBIAS, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
259
        HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_POSBIAS, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
249
        // wait for stable readings
260
        // wait for stable readings
250
        time = SetDelay(50);
261
        time = SetDelay(50);
251
        while(!CheckDelay(time));
262
        while(!CheckDelay(time));
252
        // averaging
263
        // averaging
253
        for(i = 0; i<20; i++)
264
        for(i = 0; i<20; i++)
254
        {
265
        {
255
                HMC5843_GetMagVector();
266
                HMC5843_GetMagVector();
256
                time = SetDelay(20);
267
                time = SetDelay(20);
257
        while(!CheckDelay(time));
268
        while(!CheckDelay(time));
258
                XMax += MagVector.X;
269
                XMax += MagVector.X;
259
                YMax += MagVector.Y;
270
                YMax += MagVector.Y;
260
                ZMax += MagVector.Z;
271
                ZMax += MagVector.Z;
261
        }
272
        }
262
        // activate negative bias field of 0.55 gauss
273
        // activate negative bias field of 0.55 gauss
263
        HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NEGBIAS, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
274
        HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NEGBIAS, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
264
    // wait for stable readings
275
    // wait for stable readings
265
        time = SetDelay(50);
276
        time = SetDelay(50);
266
        while(!CheckDelay(time));
277
        while(!CheckDelay(time));
267
        // averaging
278
        // averaging
268
        for(i = 0; i < 20; i++)
279
        for(i = 0; i < 20; i++)
269
        {
280
        {
270
                HMC5843_GetMagVector();
281
                HMC5843_GetMagVector();
271
                time = SetDelay(20);
282
                time = SetDelay(20);
272
        while(!CheckDelay(time));
283
        while(!CheckDelay(time));
273
                XMin += MagVector.X;
284
                XMin += MagVector.X;
274
                YMin += MagVector.Y;
285
                YMin += MagVector.Y;
275
                ZMin += MagVector.Z;     
286
                ZMin += MagVector.Z;     
276
        }
287
        }
277
        // setup final configuration
288
        // setup final configuration
278
        HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NORMAL, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
289
        HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NORMAL, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
279
        // prepare scale limits
290
        // prepare scale limits
280
        scale = 715;  // 1300 counts/Gauss * 0.55 Gauss = 715 counts
291
        scale = 715;  // 1300 counts/Gauss * 0.55 Gauss = 715 counts
281
        scale_min = (scale *  90)/100;
292
        scale_min = (scale *  90)/100;
282
        scale_max = (scale * 110)/100;
293
        scale_max = (scale * 110)/100;
283
        // check scale for all axes
294
        // check scale for all axes
284
        scale = (XMax - XMin)/40;
295
        scale = (XMax - XMin)/40;
285
        if((scale > scale_max) || (scale < scale_min)) return(0);
296
        if((scale > scale_max) || (scale < scale_min)) return(0);
286
        scale = (YMax - YMin)/40;
297
        scale = (YMax - YMin)/40;
287
        if((scale > scale_max) || (scale < scale_min)) return(0);
298
        if((scale > scale_max) || (scale < scale_min)) return(0);
288
        scale = (ZMax - ZMin)/40;
299
        scale = (ZMax - ZMin)/40;
289
        if((scale > scale_max) || (scale < scale_min)) return(0);
300
        if((scale > scale_max) || (scale < scale_min)) return(0);
290
        return(1);
301
        return(1);
291
}
302
}
292
 
303
 
293
 
304
 
294
//----------------------------------------------------------------
305
//----------------------------------------------------------------
295
u8 HMC5843_Init(void)
306
u8 HMC5843_Init(void)
296
{
307
{
297
        u8 msg[64];
308
        u8 msg[64];
-
 
309
        u8 retval = 0;
298
        u8 repeat;
310
        u8 repeat;
299
 
311
 
300
        HMC5843_Present = 0;
312
        HMC5843_Present = 0;
301
 
-
 
302
        HMC5843_Identification.A = 0xFF;
-
 
303
        HMC5843_Identification.B = 0xFF;
-
 
304
        HMC5843_Identification.C = 0xFF;
-
 
305
 
313
 
306
        // polling of identification
314
        // polling of identification
307
        repeat = 0;
315
        repeat = 0;
308
        do
316
        do
309
        {
317
        {
310
                HMC5843_GetIdentification();
318
                retval = HMC5843_GetIdentification();
311
                if (HMC5843_Identification.A != 0xFF) break; // break loop on success
319
                if(retval) break; // break loop on success
312
                UART1_PutString(".");
320
                UART1_PutString(".");
313
                repeat++;
321
                repeat++;
314
        }while ((HMC5843_Identification.A == 0xFF) && (repeat < 12));
322
        }while(repeat < 12);
315
        // if we got it
323
        // if we got it
316
        if(HMC5843_Identification.A != 0xFF)
324
        if(retval)
317
        {
325
        {
318
                sprintf(msg, " HMC5843 ID %d/%d/%d", HMC5843_Identification.A, HMC5843_Identification.B, HMC5843_Identification.C);
326
                sprintf(msg, " HMC5843 ID %d/%d/%d", HMC5843_Identification.A, HMC5843_Identification.B, HMC5843_Identification.C);
319
                UART1_PutString(msg);
327
                UART1_PutString(msg);
320
 
328
 
321
                if (    (HMC5843_Identification.A == HMC5843_IDA)
329
                if (    (HMC5843_Identification.A == HMC5843_IDA)
322
                     && (HMC5843_Identification.B == HMC5843_IDB)
330
                     && (HMC5843_Identification.B == HMC5843_IDB)
323
                         && (HMC5843_Identification.C == HMC5843_IDC))
331
                         && (HMC5843_Identification.C == HMC5843_IDC))
324
                {
332
                {
325
                        //HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NORMAL, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
333
                        //HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NORMAL, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS);
326
                        if(!HMC5843_SelfTest())
334
                        if(!HMC5843_SelfTest())
327
                        {
335
                        {
328
                                UART1_PutString("\n\r HMC5843 selftest failed!");
336
                                UART1_PutString("\n\r HMC5843 selftest failed!");
329
                                LED_RED_ON;
337
                                LED_RED_ON;
330
                        }
338
                        }
331
                        else HMC5843_Present = 1;
339
                        else HMC5843_Present = 1;
332
                }
340
                }
333
                else
341
                else
334
                {
342
                {
335
                        UART1_PutString("\n\r HMC5843 not compatible!");
343
                        UART1_PutString("\n\r HMC5843 not compatible!");
336
                        LED_RED_ON;
344
                        LED_RED_ON;
337
                }
345
                }
338
        }
346
        }
-
 
347
        // else no answer
339
        return(HMC5843_Present);
348
        return(HMC5843_Present);
340
}
349
}
341
 
350
 
342
 
351