Rev 338 | Rev 342 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 338 | Rev 339 | ||
---|---|---|---|
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 <math.h> |
57 | #include <math.h> |
58 | #include <stdio.h> |
58 | #include <stdio.h> |
59 | #include <string.h> |
59 | #include <string.h> |
60 | #include "91x_lib.h" |
60 | #include "91x_lib.h" |
61 | #include "ncmag.h" |
61 | #include "ncmag.h" |
62 | #include "i2c.h" |
62 | #include "i2c.h" |
63 | #include "timer1.h" |
63 | #include "timer1.h" |
64 | #include "led.h" |
64 | #include "led.h" |
65 | #include "uart1.h" |
65 | #include "uart1.h" |
66 | #include "eeprom.h" |
66 | #include "eeprom.h" |
67 | #include "mymath.h" |
67 | #include "mymath.h" |
68 | #include "main.h" |
68 | #include "main.h" |
69 | 69 | ||
70 | u8 NCMAG_Present = 0; |
70 | u8 NCMAG_Present = 0; |
71 | u8 NCMAG_IsCalibrated = 0; |
71 | u8 NCMAG_IsCalibrated = 0; |
72 | 72 | ||
73 | #define MAG_TYPE_NONE 0 |
73 | #define MAG_TYPE_NONE 0 |
74 | #define MAG_TYPE_HMC5843 1 |
74 | #define MAG_TYPE_HMC5843 1 |
75 | #define MAG_TYPE_LSM303DLH 2 |
75 | #define MAG_TYPE_LSM303DLH 2 |
76 | u8 NCMAG_MagType = MAG_TYPE_NONE; |
76 | u8 NCMAG_MagType = MAG_TYPE_NONE; |
77 | 77 | ||
78 | #define CALIBRATION_VERSION 1 |
78 | #define CALIBRATION_VERSION 1 |
79 | #define EEPROM_ADR_MAG_CALIBRATION 50 |
79 | #define EEPROM_ADR_MAG_CALIBRATION 50 |
80 | #define MAG_CALIBRATION_COMPATIBEL 0xA1 |
80 | #define MAG_CALIBRATION_COMPATIBEL 0xA2 |
81 | 81 | ||
82 | #define NCMAG_MIN_RAWVALUE -2047 |
82 | #define NCMAG_MIN_RAWVALUE -2047 |
83 | #define NCMAG_MAX_RAWVALUE 2047 |
83 | #define NCMAG_MAX_RAWVALUE 2047 |
84 | #define NCMAG_INVALID_DATA -4096 |
84 | #define NCMAG_INVALID_DATA -4096 |
85 | 85 | ||
86 | typedef struct |
86 | typedef struct |
87 | { |
87 | { |
88 | s16 Range; |
88 | s16 Range; |
89 | s16 Offset; |
89 | s16 Offset; |
90 | } __attribute__((packed)) Scaling_t; |
90 | } __attribute__((packed)) Scaling_t; |
91 | 91 | ||
92 | typedef struct |
92 | typedef struct |
93 | { |
93 | { |
94 | Scaling_t MagX; |
94 | Scaling_t MagX; |
95 | Scaling_t MagY; |
95 | Scaling_t MagY; |
96 | Scaling_t MagZ; |
96 | Scaling_t MagZ; |
97 | u8 Version; |
97 | u8 Version; |
98 | u8 crc; |
98 | u8 crc; |
99 | } __attribute__((packed)) Calibration_t; |
99 | } __attribute__((packed)) Calibration_t; |
100 | 100 | ||
101 | Calibration_t Calibration; // calibration data in RAM |
101 | Calibration_t Calibration; // calibration data in RAM |
- | 102 | volatile s16vec_t AccRawVector; |
|
- | 103 | volatile s16vec_t MagRawVector; |
|
102 | 104 | ||
103 | // i2c MAG interface |
105 | // i2c MAG interface |
104 | #define MAG_SLAVE_ADDRESS 0x3C // i2C slave address mag. sensor registers |
106 | #define MAG_SLAVE_ADDRESS 0x3C // i2C slave address mag. sensor registers |
105 | 107 | ||
106 | // register mapping |
108 | // register mapping |
107 | #define REG_MAG_CRA 0x00 |
109 | #define REG_MAG_CRA 0x00 |
108 | #define REG_MAG_CRB 0x01 |
110 | #define REG_MAG_CRB 0x01 |
109 | #define REG_MAG_MODE 0x02 |
111 | #define REG_MAG_MODE 0x02 |
110 | #define REG_MAG_DATAX_MSB 0x03 |
112 | #define REG_MAG_DATAX_MSB 0x03 |
111 | #define REG_MAG_DATAX_LSB 0x04 |
113 | #define REG_MAG_DATAX_LSB 0x04 |
112 | #define REG_MAG_DATAY_MSB 0x05 |
114 | #define REG_MAG_DATAY_MSB 0x05 |
113 | #define REG_MAG_DATAY_LSB 0x06 |
115 | #define REG_MAG_DATAY_LSB 0x06 |
114 | #define REG_MAG_DATAZ_MSB 0x07 |
116 | #define REG_MAG_DATAZ_MSB 0x07 |
115 | #define REG_MAG_DATAZ_LSB 0x08 |
117 | #define REG_MAG_DATAZ_LSB 0x08 |
116 | #define REG_MAG_STATUS 0x09 |
118 | #define REG_MAG_STATUS 0x09 |
117 | 119 | ||
118 | #define REG_MAG_IDA 0x0A |
120 | #define REG_MAG_IDA 0x0A |
119 | #define REG_MAG_IDB 0x0B |
121 | #define REG_MAG_IDB 0x0B |
120 | #define REG_MAG_IDC 0x0C |
122 | #define REG_MAG_IDC 0x0C |
121 | #define REG_MAG_IDF 0x0F |
123 | #define REG_MAG_IDF 0x0F |
122 | 124 | ||
123 | // bit mask for configuration mode |
125 | // bit mask for configuration mode |
124 | #define CRA_MODE_MASK 0x03 |
126 | #define CRA_MODE_MASK 0x03 |
125 | #define CRA_MODE_NORMAL 0x00 //default |
127 | #define CRA_MODE_NORMAL 0x00 //default |
126 | #define CRA_MODE_POSBIAS 0x01 |
128 | #define CRA_MODE_POSBIAS 0x01 |
127 | #define CRA_MODE_NEGBIAS 0x02 |
129 | #define CRA_MODE_NEGBIAS 0x02 |
128 | #define CRA_MODE_SELFTEST 0x03 |
130 | #define CRA_MODE_SELFTEST 0x03 |
129 | 131 | ||
130 | // bit mask for measurement mode |
132 | // bit mask for measurement mode |
131 | #define MODE_MASK 0xFF |
133 | #define MODE_MASK 0xFF |
132 | #define MODE_CONTINUOUS 0x00 |
134 | #define MODE_CONTINUOUS 0x00 |
133 | #define MODE_SINGLE 0x01 // default |
135 | #define MODE_SINGLE 0x01 // default |
134 | #define MODE_IDLE 0x02 |
136 | #define MODE_IDLE 0x02 |
135 | #define MODE_SLEEP 0x03 |
137 | #define MODE_SLEEP 0x03 |
136 | 138 | ||
137 | // bit mask for rate |
139 | // bit mask for rate |
138 | #define CRA_RATE_MASK 0x1C |
140 | #define CRA_RATE_MASK 0x1C |
139 | 141 | ||
140 | // bit mask for gain |
142 | // bit mask for gain |
141 | #define CRB_GAIN_MASK 0xE0 |
143 | #define CRB_GAIN_MASK 0xE0 |
142 | 144 | ||
143 | // ids |
145 | // ids |
144 | #define MAG_IDA 0x48 |
146 | #define MAG_IDA 0x48 |
145 | #define MAG_IDB 0x34 |
147 | #define MAG_IDB 0x34 |
146 | #define MAG_IDC 0x33 |
148 | #define MAG_IDC 0x33 |
147 | 149 | ||
148 | // the special HMC5843 interface |
150 | // the special HMC5843 interface |
149 | // bit mask for rate |
151 | // bit mask for rate |
150 | #define HMC5843_CRA_RATE_0_5HZ 0x00 |
152 | #define HMC5843_CRA_RATE_0_5HZ 0x00 |
151 | #define HMC5843_CRA_RATE_1HZ 0x04 |
153 | #define HMC5843_CRA_RATE_1HZ 0x04 |
152 | #define HMC5843_CRA_RATE_2HZ 0x08 |
154 | #define HMC5843_CRA_RATE_2HZ 0x08 |
153 | #define HMC5843_CRA_RATE_5HZ 0x0C |
155 | #define HMC5843_CRA_RATE_5HZ 0x0C |
154 | #define HMC5843_CRA_RATE_10HZ 0x10 //default |
156 | #define HMC5843_CRA_RATE_10HZ 0x10 //default |
155 | #define HMC5843_CRA_RATE_20HZ 0x14 |
157 | #define HMC5843_CRA_RATE_20HZ 0x14 |
156 | #define HMC5843_CRA_RATE_50HZ 0x18 |
158 | #define HMC5843_CRA_RATE_50HZ 0x18 |
157 | // bit mask for gain |
159 | // bit mask for gain |
158 | #define HMC5843_CRB_GAIN_07GA 0x00 |
160 | #define HMC5843_CRB_GAIN_07GA 0x00 |
159 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
161 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
160 | #define HMC5843_CRB_GAIN_15GA 0x40 |
162 | #define HMC5843_CRB_GAIN_15GA 0x40 // <--- we use this |
161 | #define HMC5843_CRB_GAIN_20GA 0x60 |
163 | #define HMC5843_CRB_GAIN_20GA 0x60 |
162 | #define HMC5843_CRB_GAIN_32GA 0x80 |
164 | #define HMC5843_CRB_GAIN_32GA 0x80 |
163 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
165 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
164 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
166 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
165 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
167 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
166 | // self test value |
168 | // self test value |
167 | #define HMC5843_TEST_XSCALE 715 |
169 | #define HMC5843_TEST_XSCALE 555 |
168 | #define HMC5843_TEST_YSCALE 715 |
170 | #define HMC5843_TEST_YSCALE 555 |
169 | #define HMC5843_TEST_ZSCALE 715 |
171 | #define HMC5843_TEST_ZSCALE 555 |
170 | - | ||
- | 172 | // clibration range |
|
- | 173 | #define HMC5843_CALIBRATION_RANGE 550 |
|
171 | 174 | ||
172 | // the special LSM302DLH interface |
175 | // the special LSM302DLH interface |
173 | // bit mask for rate |
176 | // bit mask for rate |
174 | #define LSM303DLH_CRA_RATE_0_75HZ 0x00 |
177 | #define LSM303DLH_CRA_RATE_0_75HZ 0x00 |
175 | #define LSM303DLH_CRA_RATE_1_5HZ 0x04 |
178 | #define LSM303DLH_CRA_RATE_1_5HZ 0x04 |
176 | #define LSM303DLH_CRA_RATE_3_0HZ 0x08 |
179 | #define LSM303DLH_CRA_RATE_3_0HZ 0x08 |
177 | #define LSM303DLH_CRA_RATE_7_5HZ 0x0C |
180 | #define LSM303DLH_CRA_RATE_7_5HZ 0x0C |
178 | #define LSM303DLH_CRA_RATE_15HZ 0x10 //default |
181 | #define LSM303DLH_CRA_RATE_15HZ 0x10 //default |
179 | #define LSM303DLH_CRA_RATE_30HZ 0x14 |
182 | #define LSM303DLH_CRA_RATE_30HZ 0x14 |
180 | #define LSM303DLH_CRA_RATE_75HZ 0x18 |
183 | #define LSM303DLH_CRA_RATE_75HZ 0x18 |
181 | 184 | ||
182 | // bit mask for gain |
185 | // bit mask for gain |
183 | #define LSM303DLH_CRB_GAIN_XXGA 0x00 |
186 | #define LSM303DLH_CRB_GAIN_XXGA 0x00 |
184 | #define LSM303DLH_CRB_GAIN_13GA 0x20 //default |
187 | #define LSM303DLH_CRB_GAIN_13GA 0x20 //default |
185 | #define LSM303DLH_CRB_GAIN_19GA 0x40 |
188 | #define LSM303DLH_CRB_GAIN_19GA 0x40 // <--- we use this |
186 | #define LSM303DLH_CRB_GAIN_25GA 0x60 |
189 | #define LSM303DLH_CRB_GAIN_25GA 0x60 |
187 | #define LSM303DLH_CRB_GAIN_40GA 0x80 |
190 | #define LSM303DLH_CRB_GAIN_40GA 0x80 |
188 | #define LSM303DLH_CRB_GAIN_47GA 0xA0 |
191 | #define LSM303DLH_CRB_GAIN_47GA 0xA0 |
189 | #define LSM303DLH_CRB_GAIN_56GA 0xC0 |
192 | #define LSM303DLH_CRB_GAIN_56GA 0xC0 |
190 | #define LSM303DLH_CRB_GAIN_81GA 0xE0 |
193 | #define LSM303DLH_CRB_GAIN_81GA 0xE0 |
191 | // self test value |
194 | // self test value |
192 | #define LSM303DLH_TEST_XSCALE 495 |
195 | #define LSM303DLH_TEST_XSCALE 495 |
193 | #define LSM303DLH_TEST_YSCALE 495 |
196 | #define LSM303DLH_TEST_YSCALE 495 |
194 | #define LSM303DLH_TEST_ZSCALE 470 |
197 | #define LSM303DLH_TEST_ZSCALE 470 |
- | 198 | // clibration range |
|
- | 199 | #define LSM303_CALIBRATION_RANGE 500 |
|
195 | 200 | ||
196 | // the i2c ACC interface |
201 | // the i2c ACC interface |
197 | #define ACC_SLAVE_ADDRESS 0x30 // i2c slave for acc. sensor registers |
202 | #define ACC_SLAVE_ADDRESS 0x30 // i2c slave for acc. sensor registers |
198 | // register mapping |
203 | // register mapping |
199 | #define REG_ACC_CTRL1 0x20 |
204 | #define REG_ACC_CTRL1 0x20 |
200 | #define REG_ACC_CTRL2 0x21 |
205 | #define REG_ACC_CTRL2 0x21 |
201 | #define REG_ACC_CTRL3 0x22 |
206 | #define REG_ACC_CTRL3 0x22 |
202 | #define REG_ACC_CTRL4 0x23 |
207 | #define REG_ACC_CTRL4 0x23 |
203 | #define REG_ACC_CTRL5 0x24 |
208 | #define REG_ACC_CTRL5 0x24 |
204 | #define REG_ACC_HP_FILTER_RESET 0x25 |
209 | #define REG_ACC_HP_FILTER_RESET 0x25 |
205 | #define REG_ACC_REFERENCE 0x26 |
210 | #define REG_ACC_REFERENCE 0x26 |
206 | #define REG_ACC_STATUS 0x27 |
211 | #define REG_ACC_STATUS 0x27 |
207 | #define REG_ACC_X_LSB 0x28 |
212 | #define REG_ACC_X_LSB 0x28 |
208 | #define REG_ACC_X_MSB 0x29 |
213 | #define REG_ACC_X_MSB 0x29 |
209 | #define REG_ACC_Y_LSB 0x2A |
214 | #define REG_ACC_Y_LSB 0x2A |
210 | #define REG_ACC_Y_MSB 0x2B |
215 | #define REG_ACC_Y_MSB 0x2B |
211 | #define REG_ACC_Z_LSB 0x2C |
216 | #define REG_ACC_Z_LSB 0x2C |
212 | #define REG_ACC_Z_MSB 0x2D |
217 | #define REG_ACC_Z_MSB 0x2D |
213 | 218 | ||
214 | 219 | ||
215 | 220 | ||
216 | typedef struct |
221 | typedef struct |
217 | { |
222 | { |
218 | u8 A; |
223 | u8 A; |
219 | u8 B; |
224 | u8 B; |
220 | u8 C; |
225 | u8 C; |
221 | } __attribute__((packed)) Identification_t; |
226 | } __attribute__((packed)) Identification_t; |
222 | volatile Identification_t NCMAG_Identification; |
227 | volatile Identification_t NCMAG_Identification; |
223 | 228 | ||
224 | typedef struct |
229 | typedef struct |
225 | { |
230 | { |
226 | u8 Sub; |
231 | u8 Sub; |
227 | } __attribute__((packed)) Identification2_t; |
232 | } __attribute__((packed)) Identification2_t; |
228 | volatile Identification2_t NCMAG_Identification2; |
233 | volatile Identification2_t NCMAG_Identification2; |
229 | 234 | ||
230 | typedef struct |
235 | typedef struct |
231 | { |
236 | { |
232 | u8 cra; |
237 | u8 cra; |
233 | u8 crb; |
238 | u8 crb; |
234 | u8 mode; |
239 | u8 mode; |
235 | } __attribute__((packed)) MagConfig_t; |
240 | } __attribute__((packed)) MagConfig_t; |
236 | 241 | ||
237 | volatile MagConfig_t MagConfig; |
242 | volatile MagConfig_t MagConfig; |
238 | 243 | ||
239 | typedef struct |
244 | typedef struct |
240 | { |
245 | { |
241 | u8 ctrl_1; |
246 | u8 ctrl_1; |
242 | u8 ctrl_2; |
247 | u8 ctrl_2; |
243 | u8 ctrl_3; |
248 | u8 ctrl_3; |
244 | u8 ctrl_4; |
249 | u8 ctrl_4; |
245 | u8 ctrl_5; |
250 | u8 ctrl_5; |
246 | } __attribute__((packed)) AccConfig_t; |
251 | } __attribute__((packed)) AccConfig_t; |
247 | 252 | ||
248 | volatile AccConfig_t AccConfig; |
253 | volatile AccConfig_t AccConfig; |
249 | - | ||
250 | volatile s16vec_t AccRawVector; |
- | |
251 | volatile s16vec_t MagRawVector; |
- | |
252 | - | ||
253 | 254 | ||
254 | u8 NCMag_CalibrationWrite(void) |
255 | u8 NCMag_CalibrationWrite(void) |
255 | { |
256 | { |
256 | u8 i, crc = MAG_CALIBRATION_COMPATIBEL; |
257 | u8 i, crc = MAG_CALIBRATION_COMPATIBEL; |
257 | EEPROM_Result_t eres; |
258 | EEPROM_Result_t eres; |
258 | u8 *pBuff = (u8*)&Calibration; |
259 | u8 *pBuff = (u8*)&Calibration; |
259 | 260 | ||
260 | Calibration.Version = CALIBRATION_VERSION; |
261 | Calibration.Version = CALIBRATION_VERSION; |
261 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
262 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
262 | { |
263 | { |
263 | crc += pBuff[i]; |
264 | crc += pBuff[i]; |
264 | } |
265 | } |
265 | Calibration.crc = ~crc; |
266 | Calibration.crc = ~crc; |
266 | eres = EEPROM_WriteBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)); |
267 | eres = EEPROM_WriteBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)); |
267 | if(EEPROM_SUCCESS == eres) i = 1; |
268 | if(EEPROM_SUCCESS == eres) i = 1; |
268 | else i = 0; |
269 | else i = 0; |
269 | return(i); |
270 | return(i); |
270 | } |
271 | } |
271 | 272 | ||
272 | u8 NCMag_CalibrationRead(void) |
273 | u8 NCMag_CalibrationRead(void) |
273 | { |
274 | { |
274 | u8 i, crc = MAG_CALIBRATION_COMPATIBEL; |
275 | u8 i, crc = MAG_CALIBRATION_COMPATIBEL; |
275 | u8 *pBuff = (u8*)&Calibration; |
276 | u8 *pBuff = (u8*)&Calibration; |
276 | 277 | ||
277 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration))) |
278 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration))) |
278 | { |
279 | { |
279 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
280 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
280 | { |
281 | { |
281 | crc += pBuff[i]; |
282 | crc += pBuff[i]; |
282 | } |
283 | } |
283 | crc = ~crc; |
284 | crc = ~crc; |
284 | if(Calibration.crc != crc) return(0); // crc mismatch |
285 | if(Calibration.crc != crc) return(0); // crc mismatch |
285 | if(Calibration.Version == CALIBRATION_VERSION) return(1); |
286 | if(Calibration.Version == CALIBRATION_VERSION) return(1); |
286 | } |
287 | } |
287 | return(0); |
288 | return(0); |
288 | } |
289 | } |
289 | 290 | ||
290 | 291 | ||
291 | void NCMAG_Calibrate(void) |
292 | void NCMAG_Calibrate(void) |
292 | { |
293 | { |
293 | u8 msg[64]; |
294 | u8 msg[64]; |
294 | static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
295 | static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
295 | static s16 X = 0, Y = 0, Z = 0; |
296 | static s16 X = 0, Y = 0, Z = 0; |
296 | static u8 OldCalState = 0; |
297 | static u8 OldCalState = 0; |
- | 298 | s16 MinCaclibration = 450; |
|
297 | 299 | ||
298 | X = (4*X + MagRawVector.X + 3)/5; |
300 | X = (4*X + MagRawVector.X + 3)/5; |
299 | Y = (4*Y + MagRawVector.Y + 3)/5; |
301 | Y = (4*Y + MagRawVector.Y + 3)/5; |
300 | Z = (4*Z + MagRawVector.Z + 3)/5; |
302 | Z = (4*Z + MagRawVector.Z + 3)/5; |
301 | 303 | ||
302 | switch(Compass_CalState) |
304 | switch(Compass_CalState) |
303 | { |
305 | { |
304 | case 1: |
306 | case 1: |
305 | // 1st step of calibration |
307 | // 1st step of calibration |
306 | // initialize ranges |
308 | // initialize ranges |
307 | // used to change the orientation of the NC in the horizontal plane |
309 | // used to change the orientation of the NC in the horizontal plane |
308 | Xmin = 10000; |
310 | Xmin = 10000; |
309 | Xmax = -10000; |
311 | Xmax = -10000; |
310 | Ymin = 10000; |
312 | Ymin = 10000; |
311 | Ymax = -10000; |
313 | Ymax = -10000; |
312 | Zmin = 10000; |
314 | Zmin = 10000; |
313 | Zmax = -10000; |
315 | Zmax = -10000; |
314 | break; |
316 | break; |
315 | 317 | ||
316 | case 2: // 2nd step of calibration |
318 | case 2: // 2nd step of calibration |
317 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
319 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
318 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
320 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
319 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
321 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
320 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
322 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
321 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
323 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
322 | break; |
324 | break; |
323 | 325 | ||
324 | case 3: // 3rd step of calibration |
326 | case 3: // 3rd step of calibration |
325 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
327 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
326 | break; |
328 | break; |
327 | 329 | ||
328 | case 4: |
330 | case 4: |
329 | // find Min and Max of the Z-Sensor |
331 | // find Min and Max of the Z-Sensor |
330 | if(Z < Zmin) { Zmin = Z; BeepTime = 80;} |
332 | if(Z < Zmin) { Zmin = Z; BeepTime = 80;} |
331 | else if(Z > Zmax) { Zmax = Z; BeepTime = 80;} |
333 | else if(Z > Zmax) { Zmax = Z; BeepTime = 80;} |
332 | break; |
334 | break; |
333 | 335 | ||
334 | case 5: |
336 | case 5: |
335 | // Save values |
337 | // Save values |
336 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
338 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
337 | { |
339 | { |
338 | // #define MIN_CALIBRATION 256 |
340 | // #define MIN_CALIBRATION 256 |
- | 341 | if(NCMAG_MagType == MAG_TYPE_HMC5843) |
|
- | 342 | { |
|
- | 343 | UART1_PutString("\r\nHMC5843 calibration\n\r"); |
|
- | 344 | MinCaclibration = HMC5843_CALIBRATION_RANGE; |
|
- | 345 | } |
|
- | 346 | if(NCMAG_MagType == MAG_TYPE_LSM303DLH) |
|
- | 347 | { |
|
- | 348 | UART1_PutString("\r\n\r\nLSM303 calibration\n\r"); |
|
339 | #define MIN_CALIBRATION 450 |
349 | MinCaclibration =LSM303_CALIBRATION_RANGE; |
- | 350 | } |
|
- | 351 | ||
340 | Calibration.MagX.Range = Xmax - Xmin; |
352 | Calibration.MagX.Range = Xmax - Xmin; |
341 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
353 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
342 | Calibration.MagY.Range = Ymax - Ymin; |
354 | Calibration.MagY.Range = Ymax - Ymin; |
343 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
355 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
344 | Calibration.MagZ.Range = Zmax - Zmin; |
356 | Calibration.MagZ.Range = Zmax - Zmin; |
345 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
357 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
346 | if((Calibration.MagX.Range > MIN_CALIBRATION) && (Calibration.MagY.Range > MIN_CALIBRATION) && (Calibration.MagZ.Range > MIN_CALIBRATION)) |
358 | if((Calibration.MagX.Range > MinCaclibration) && (Calibration.MagY.Range > MinCaclibration) && (Calibration.MagZ.Range > MinCaclibration)) |
347 | { |
359 | { |
348 | NCMAG_IsCalibrated = NCMag_CalibrationWrite(); |
360 | NCMAG_IsCalibrated = NCMag_CalibrationWrite(); |
349 | BeepTime = 2500; |
361 | BeepTime = 2500; |
350 | UART1_PutString("\r\n Calibration okay\n\r"); |
362 | UART1_PutString("\r\n Calibration okay\n\r"); |
351 | } |
363 | } |
352 | else |
364 | else |
353 | { |
365 | { |
354 | UART1_PutString("\r\n Calibration FAILED - Values too low"); |
366 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
355 | if(Calibration.MagX.Range < MIN_CALIBRATION) UART1_PutString("X! "); |
367 | if(Calibration.MagX.Range < MinCaclibration) UART1_PutString("X! "); |
356 | if(Calibration.MagY.Range < MIN_CALIBRATION) UART1_PutString("y! "); |
368 | if(Calibration.MagY.Range < MinCaclibration) UART1_PutString("y! "); |
357 | if(Calibration.MagZ.Range < MIN_CALIBRATION) UART1_PutString("Z! "); |
369 | if(Calibration.MagZ.Range < MinCaclibration) UART1_PutString("Z! "); |
358 | UART1_PutString("\r\n"); |
370 | UART1_PutString("\r\n"); |
- | 371 | sprintf(msg, "Minimum is: %i \r\n",MinCaclibration); |
|
- | 372 | UART1_PutString(msg); |
|
- | 373 | ||
359 | // restore old calibration data from eeprom |
374 | // restore old calibration data from eeprom |
360 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
375 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
361 | } |
376 | } |
362 | sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin); |
377 | sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin); |
363 | UART1_PutString(msg); |
378 | UART1_PutString(msg); |
364 | sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin); |
379 | sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin); |
365 | UART1_PutString(msg); |
380 | UART1_PutString(msg); |
366 | sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin); |
381 | sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin); |
367 | UART1_PutString(msg); |
382 | UART1_PutString(msg); |
368 | } |
383 | } |
369 | break; |
384 | break; |
370 | 385 | ||
371 | default: |
386 | default: |
372 | break; |
387 | break; |
373 | } |
388 | } |
374 | OldCalState = Compass_CalState; |
389 | OldCalState = Compass_CalState; |
375 | } |
390 | } |
376 | 391 | ||
377 | // ---------- call back handlers ----------------------------------------- |
392 | // ---------- call back handlers ----------------------------------------- |
378 | 393 | ||
379 | // rx data handler for id info request |
394 | // rx data handler for id info request |
380 | void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize) |
395 | void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize) |
381 | { // if number of bytes are matching |
396 | { // if number of bytes are matching |
382 | if(RxBufferSize == sizeof(NCMAG_Identification) ) |
397 | if(RxBufferSize == sizeof(NCMAG_Identification) ) |
383 | { |
398 | { |
384 | memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification)); |
399 | memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification)); |
385 | } |
400 | } |
386 | } |
401 | } |
387 | 402 | ||
388 | void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize) |
403 | void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize) |
389 | { // if number of bytes are matching |
404 | { // if number of bytes are matching |
390 | if(RxBufferSize == sizeof(NCMAG_Identification2)) |
405 | if(RxBufferSize == sizeof(NCMAG_Identification2)) |
391 | { |
406 | { |
392 | memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2)); |
407 | memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2)); |
393 | } |
408 | } |
394 | } |
409 | } |
395 | 410 | ||
396 | // rx data handler for magnetic sensor raw data |
411 | // rx data handler for magnetic sensor raw data |
397 | void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
412 | void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
398 | { // if number of bytes are matching |
413 | { // if number of bytes are matching |
399 | if(RxBufferSize == sizeof(MagRawVector) ) |
414 | if(RxBufferSize == sizeof(MagRawVector) ) |
400 | { // byte order from big to little endian |
415 | { // byte order from big to little endian |
401 | s16 raw; |
416 | s16 raw; |
402 | raw = pRxBuffer[0]<<8; |
417 | raw = pRxBuffer[0]<<8; |
403 | raw+= pRxBuffer[1]; |
418 | raw+= pRxBuffer[1]; |
404 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw; |
419 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw; |
405 | raw = pRxBuffer[2]<<8; |
420 | raw = pRxBuffer[2]<<8; |
406 | raw+= pRxBuffer[3]; |
421 | raw+= pRxBuffer[3]; |
407 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
422 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
408 | { |
423 | { |
409 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Z = raw; // here Z and Y are exchanged |
424 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Z = raw; // here Z and Y are exchanged |
410 | else MagRawVector.Y = raw; |
425 | else MagRawVector.Y = raw; |
411 | } |
426 | } |
412 | raw = pRxBuffer[4]<<8; |
427 | raw = pRxBuffer[4]<<8; |
413 | raw+= pRxBuffer[5]; |
428 | raw+= pRxBuffer[5]; |
414 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
429 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
415 | { |
430 | { |
416 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Y = raw; // here Z and Y are exchanged |
431 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Y = raw; // here Z and Y are exchanged |
417 | else MagRawVector.Z = raw; |
432 | else MagRawVector.Z = raw; |
418 | } |
433 | } |
419 | 434 | ||
420 | } |
435 | } |
421 | if(Compass_CalState || !NCMAG_IsCalibrated) |
436 | if(Compass_CalState || !NCMAG_IsCalibrated) |
422 | { // mark out data invalid |
437 | { // mark out data invalid |
423 | MagVector.X = MagRawVector.X; |
438 | MagVector.X = MagRawVector.X; |
424 | MagVector.Y = MagRawVector.Y; |
439 | MagVector.Y = MagRawVector.Y; |
425 | MagVector.Z = MagRawVector.Z; |
440 | MagVector.Z = MagRawVector.Z; |
426 | Compass_Heading = -1; |
441 | Compass_Heading = -1; |
427 | } |
442 | } |
428 | else |
443 | else |
429 | { |
444 | { |
430 | // update MagVector from MagRaw Vector by Scaling |
445 | // update MagVector from MagRaw Vector by Scaling |
431 | MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range); |
446 | MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range); |
432 | MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range); |
447 | MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range); |
433 | MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range); |
448 | MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range); |
434 | Compass_CalcHeading(); |
449 | Compass_CalcHeading(); |
435 | } |
450 | } |
436 | } |
451 | } |
437 | // rx data handler for acceleration raw data |
452 | // rx data handler for acceleration raw data |
438 | void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize) |
453 | void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize) |
439 | { // if number of byte are matching |
454 | { // if number of byte are matching |
440 | if(RxBufferSize == sizeof(AccRawVector) ) |
455 | if(RxBufferSize == sizeof(AccRawVector) ) |
441 | { |
456 | { |
442 | memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector)); |
457 | memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector)); |
443 | } |
458 | } |
444 | } |
459 | } |
445 | // rx data handler for reading magnetic sensor configuration |
460 | // rx data handler for reading magnetic sensor configuration |
446 | void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize) |
461 | void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize) |
447 | { // if number of byte are matching |
462 | { // if number of byte are matching |
448 | if(RxBufferSize == sizeof(MagConfig) ) |
463 | if(RxBufferSize == sizeof(MagConfig) ) |
449 | { |
464 | { |
450 | memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig)); |
465 | memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig)); |
451 | } |
466 | } |
452 | } |
467 | } |
453 | // rx data handler for reading acceleration sensor configuration |
468 | // rx data handler for reading acceleration sensor configuration |
454 | void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize) |
469 | void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize) |
455 | { // if number of byte are matching |
470 | { // if number of byte are matching |
456 | if(RxBufferSize == sizeof(AccConfig) ) |
471 | if(RxBufferSize == sizeof(AccConfig) ) |
457 | { |
472 | { |
458 | memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig)); |
473 | memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig)); |
459 | } |
474 | } |
460 | } |
475 | } |
461 | //---------------------------------------------------------------------- |
476 | //---------------------------------------------------------------------- |
462 | 477 | ||
463 | 478 | ||
464 | // --------------------------------------------------------------------- |
479 | // --------------------------------------------------------------------- |
465 | u8 NCMAG_SetMagConfig(void) |
480 | u8 NCMAG_SetMagConfig(void) |
466 | { |
481 | { |
467 | u8 retval = 0; |
482 | u8 retval = 0; |
468 | // try to catch the i2c buffer within 100 ms timeout |
483 | // try to catch the i2c buffer within 100 ms timeout |
469 | if(I2C_LockBuffer(100)) |
484 | if(I2C_LockBuffer(100)) |
470 | { |
485 | { |
471 | u8 TxBytes = 0; |
486 | u8 TxBytes = 0; |
472 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
487 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
473 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig)); |
488 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig)); |
474 | TxBytes += sizeof(MagConfig); |
489 | TxBytes += sizeof(MagConfig); |
475 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0)) |
490 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0)) |
476 | { |
491 | { |
477 | if(I2C_WaitForEndOfTransmission(100)) |
492 | if(I2C_WaitForEndOfTransmission(100)) |
478 | { |
493 | { |
479 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
494 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
480 | } |
495 | } |
481 | } |
496 | } |
482 | } |
497 | } |
483 | return(retval); |
498 | return(retval); |
484 | } |
499 | } |
485 | 500 | ||
486 | // ---------------------------------------------------------------------------------------- |
501 | // ---------------------------------------------------------------------------------------- |
487 | u8 NCMAG_GetMagConfig(void) |
502 | u8 NCMAG_GetMagConfig(void) |
488 | { |
503 | { |
489 | u8 retval = 0; |
504 | u8 retval = 0; |
490 | // try to catch the i2c buffer within 100 ms timeout |
505 | // try to catch the i2c buffer within 100 ms timeout |
491 | if(I2C_LockBuffer(100)) |
506 | if(I2C_LockBuffer(100)) |
492 | { |
507 | { |
493 | u8 TxBytes = 0; |
508 | u8 TxBytes = 0; |
494 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
509 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
495 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig))) |
510 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig))) |
496 | { |
511 | { |
497 | if(I2C_WaitForEndOfTransmission(100)) |
512 | if(I2C_WaitForEndOfTransmission(100)) |
498 | { |
513 | { |
499 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
514 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
500 | } |
515 | } |
501 | } |
516 | } |
502 | } |
517 | } |
503 | return(retval); |
518 | return(retval); |
504 | } |
519 | } |
505 | 520 | ||
506 | // ---------------------------------------------------------------------------------------- |
521 | // ---------------------------------------------------------------------------------------- |
507 | u8 NCMAG_SetAccConfig(void) |
522 | u8 NCMAG_SetAccConfig(void) |
508 | { |
523 | { |
509 | u8 retval = 0; |
524 | u8 retval = 0; |
510 | // try to catch the i2c buffer within 100 ms timeout |
525 | // try to catch the i2c buffer within 100 ms timeout |
511 | if(I2C_LockBuffer(100)) |
526 | if(I2C_LockBuffer(100)) |
512 | { |
527 | { |
513 | u8 TxBytes = 0; |
528 | u8 TxBytes = 0; |
514 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
529 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
515 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig)); |
530 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig)); |
516 | TxBytes += sizeof(AccConfig); |
531 | TxBytes += sizeof(AccConfig); |
517 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0)) |
532 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0)) |
518 | { |
533 | { |
519 | if(I2C_WaitForEndOfTransmission(100)) |
534 | if(I2C_WaitForEndOfTransmission(100)) |
520 | { |
535 | { |
521 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
536 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
522 | } |
537 | } |
523 | } |
538 | } |
524 | } |
539 | } |
525 | return(retval); |
540 | return(retval); |
526 | } |
541 | } |
527 | 542 | ||
528 | // ---------------------------------------------------------------------------------------- |
543 | // ---------------------------------------------------------------------------------------- |
529 | u8 NCMAG_GetAccConfig(void) |
544 | u8 NCMAG_GetAccConfig(void) |
530 | { |
545 | { |
531 | u8 retval = 0; |
546 | u8 retval = 0; |
532 | // try to catch the i2c buffer within 100 ms timeout |
547 | // try to catch the i2c buffer within 100 ms timeout |
533 | if(I2C_LockBuffer(100)) |
548 | if(I2C_LockBuffer(100)) |
534 | { |
549 | { |
535 | u8 TxBytes = 0; |
550 | u8 TxBytes = 0; |
536 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
551 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
537 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig))) |
552 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig))) |
538 | { |
553 | { |
539 | if(I2C_WaitForEndOfTransmission(100)) |
554 | if(I2C_WaitForEndOfTransmission(100)) |
540 | { |
555 | { |
541 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
556 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
542 | } |
557 | } |
543 | } |
558 | } |
544 | } |
559 | } |
545 | return(retval); |
560 | return(retval); |
546 | } |
561 | } |
547 | 562 | ||
548 | // ---------------------------------------------------------------------------------------- |
563 | // ---------------------------------------------------------------------------------------- |
549 | u8 NCMAG_GetIdentification(void) |
564 | u8 NCMAG_GetIdentification(void) |
550 | { |
565 | { |
551 | u8 retval = 0; |
566 | u8 retval = 0; |
552 | // try to catch the i2c buffer within 100 ms timeout |
567 | // try to catch the i2c buffer within 100 ms timeout |
553 | if(I2C_LockBuffer(100)) |
568 | if(I2C_LockBuffer(100)) |
554 | { |
569 | { |
555 | u16 TxBytes = 0; |
570 | u16 TxBytes = 0; |
556 | NCMAG_Identification.A = 0xFF; |
571 | NCMAG_Identification.A = 0xFF; |
557 | NCMAG_Identification.B = 0xFF; |
572 | NCMAG_Identification.B = 0xFF; |
558 | NCMAG_Identification.C = 0xFF; |
573 | NCMAG_Identification.C = 0xFF; |
559 | I2C_Buffer[TxBytes++] = REG_MAG_IDA; |
574 | I2C_Buffer[TxBytes++] = REG_MAG_IDA; |
560 | // initiate transmission |
575 | // initiate transmission |
561 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification))) |
576 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification))) |
562 | { |
577 | { |
563 | if(I2C_WaitForEndOfTransmission(100)) |
578 | if(I2C_WaitForEndOfTransmission(100)) |
564 | { |
579 | { |
565 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
580 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
566 | } |
581 | } |
567 | } |
582 | } |
568 | } |
583 | } |
569 | return(retval); |
584 | return(retval); |
570 | } |
585 | } |
571 | 586 | ||
572 | u8 NCMAG_GetIdentification_Sub(void) |
587 | u8 NCMAG_GetIdentification_Sub(void) |
573 | { |
588 | { |
574 | u8 retval = 0; |
589 | u8 retval = 0; |
575 | // try to catch the i2c buffer within 100 ms timeout |
590 | // try to catch the i2c buffer within 100 ms timeout |
576 | if(I2C_LockBuffer(100)) |
591 | if(I2C_LockBuffer(100)) |
577 | { |
592 | { |
578 | u16 TxBytes = 0; |
593 | u16 TxBytes = 0; |
579 | NCMAG_Identification2.Sub = 0xFF; |
594 | NCMAG_Identification2.Sub = 0xFF; |
580 | I2C_Buffer[TxBytes++] = REG_MAG_IDF; |
595 | I2C_Buffer[TxBytes++] = REG_MAG_IDF; |
581 | // initiate transmission |
596 | // initiate transmission |
582 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2))) |
597 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2))) |
583 | { |
598 | { |
584 | if(I2C_WaitForEndOfTransmission(100)) |
599 | if(I2C_WaitForEndOfTransmission(100)) |
585 | { |
600 | { |
586 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
601 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
587 | } |
602 | } |
588 | } |
603 | } |
589 | } |
604 | } |
590 | return(retval); |
605 | return(retval); |
591 | } |
606 | } |
592 | 607 | ||
593 | 608 | ||
594 | // ---------------------------------------------------------------------------------------- |
609 | // ---------------------------------------------------------------------------------------- |
595 | void NCMAG_GetMagVector(void) |
610 | void NCMAG_GetMagVector(void) |
596 | { |
611 | { |
597 | // try to catch the I2C buffer within 0 ms |
612 | // try to catch the I2C buffer within 0 ms |
598 | if(I2C_LockBuffer(0)) |
613 | if(I2C_LockBuffer(0)) |
599 | { |
614 | { |
600 | // s16 tmp; |
615 | // s16 tmp; |
601 | u16 TxBytes = 0; |
616 | u16 TxBytes = 0; |
602 | // set register pointer |
617 | // set register pointer |
603 | I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB; |
618 | I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB; |
604 | // initiate transmission |
619 | // initiate transmission |
605 | I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector)); |
620 | I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector)); |
606 | } |
621 | } |
607 | } |
622 | } |
608 | 623 | ||
609 | //---------------------------------------------------------------- |
624 | //---------------------------------------------------------------- |
610 | void NCMAG_GetAccVector(void) |
625 | void NCMAG_GetAccVector(void) |
611 | { |
626 | { |
612 | // try to catch the I2C buffer within 0 ms |
627 | // try to catch the I2C buffer within 0 ms |
613 | if(I2C_LockBuffer(0)) |
628 | if(I2C_LockBuffer(0)) |
614 | { |
629 | { |
615 | u16 TxBytes = 0; |
630 | u16 TxBytes = 0; |
616 | // set register pointer |
631 | // set register pointer |
617 | I2C_Buffer[TxBytes++] = REG_ACC_X_LSB; |
632 | I2C_Buffer[TxBytes++] = REG_ACC_X_LSB; |
618 | // initiate transmission |
633 | // initiate transmission |
619 | I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector)); |
634 | I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector)); |
620 | } |
635 | } |
621 | } |
636 | } |
622 | 637 | ||
623 | //---------------------------------------------------------------- |
638 | //---------------------------------------------------------------- |
624 | void InitNC_MagnetSensor(void) |
639 | void InitNC_MagnetSensor(void) |
625 | { |
640 | { |
626 | s16 xscale, yscale, zscale; |
641 | s16 xscale, yscale, zscale; |
627 | u8 crb_gain, cra_rate; |
642 | u8 crb_gain, cra_rate; |
628 | // u8 retval = 1; |
643 | // u8 retval = 1; |
629 | 644 | ||
630 | switch(NCMAG_MagType) |
645 | switch(NCMAG_MagType) |
631 | { |
646 | { |
632 | case MAG_TYPE_HMC5843: |
647 | case MAG_TYPE_HMC5843: |
633 | crb_gain = HMC5843_CRB_GAIN_10GA; |
648 | crb_gain = HMC5843_CRB_GAIN_15GA; |
634 | cra_rate = HMC5843_CRA_RATE_50HZ; |
649 | cra_rate = HMC5843_CRA_RATE_50HZ; |
635 | xscale = HMC5843_TEST_XSCALE; |
650 | xscale = HMC5843_TEST_XSCALE; |
636 | yscale = HMC5843_TEST_YSCALE; |
651 | yscale = HMC5843_TEST_YSCALE; |
637 | zscale = HMC5843_TEST_ZSCALE; |
652 | zscale = HMC5843_TEST_ZSCALE; |
638 | break; |
653 | break; |
639 | 654 | ||
640 | case MAG_TYPE_LSM303DLH: |
655 | case MAG_TYPE_LSM303DLH: |
641 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
656 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
642 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
657 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
643 | xscale = LSM303DLH_TEST_XSCALE; |
658 | xscale = LSM303DLH_TEST_XSCALE; |
644 | yscale = LSM303DLH_TEST_YSCALE; |
659 | yscale = LSM303DLH_TEST_YSCALE; |
645 | zscale = LSM303DLH_TEST_ZSCALE; |
660 | zscale = LSM303DLH_TEST_ZSCALE; |
646 | break; |
661 | break; |
647 | 662 | ||
648 | default: |
663 | default: |
649 | return; |
664 | return; |
650 | } |
665 | } |
651 | 666 | ||
652 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
667 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
653 | MagConfig.crb = crb_gain; |
668 | MagConfig.crb = crb_gain; |
654 | MagConfig.mode = MODE_CONTINUOUS; |
669 | MagConfig.mode = MODE_CONTINUOUS; |
655 | NCMAG_SetMagConfig(); |
670 | NCMAG_SetMagConfig(); |
656 | } |
671 | } |
657 | 672 | ||
658 | 673 | ||
659 | // -------------------------------------------------------- |
674 | // -------------------------------------------------------- |
660 | void NCMAG_Update(void) |
675 | void NCMAG_Update(void) |
661 | { |
676 | { |
662 | static u32 TimerUpdate = 0; |
677 | static u32 TimerUpdate = 0; |
663 | static u8 send_config = 0; |
678 | static u8 send_config = 0; |
664 | 679 | ||
665 | if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present ) |
680 | if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present ) |
666 | { |
681 | { |
667 | Compass_Heading = -1; |
682 | Compass_Heading = -1; |
668 | DebugOut.Analog[14]++; // count I2C error |
683 | DebugOut.Analog[14]++; // count I2C error |
669 | return; |
684 | return; |
670 | } |
685 | } |
671 | if(CheckDelay(TimerUpdate)) |
686 | if(CheckDelay(TimerUpdate)) |
672 | { |
687 | { |
673 | if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid |
688 | if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid |
674 | if(++send_config == 25) // 500ms |
689 | if(++send_config == 25) // 500ms |
675 | { |
690 | { |
676 | send_config = 0; |
691 | send_config = 0; |
677 | InitNC_MagnetSensor(); |
692 | InitNC_MagnetSensor(); |
678 | TimerUpdate = SetDelay(15); // back into the old time-slot |
693 | TimerUpdate = SetDelay(15); // back into the old time-slot |
679 | } |
694 | } |
680 | else |
695 | else |
681 | { |
696 | { |
682 | // check for new calibration state |
697 | // check for new calibration state |
683 | Compass_UpdateCalState(); |
698 | Compass_UpdateCalState(); |
684 | if(Compass_CalState) NCMAG_Calibrate(); |
699 | if(Compass_CalState) NCMAG_Calibrate(); |
685 | NCMAG_GetMagVector(); //Get new data; |
700 | NCMAG_GetMagVector(); //Get new data; |
686 | if(send_config == 24) TimerUpdate = SetDelay(5); // next event is the re-configuration |
701 | if(send_config == 24) TimerUpdate = SetDelay(5); // next event is the re-configuration |
687 | else TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
702 | else TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
688 | } |
703 | } |
689 | } |
704 | } |
690 | } |
705 | } |
691 | 706 | ||
692 | 707 | ||
693 | // -------------------------------------------------------- |
708 | // -------------------------------------------------------- |
694 | u8 NCMAG_SelfTest(void) |
709 | u8 NCMAG_SelfTest(void) |
695 | { |
710 | { |
696 | u8 msg[64]; |
711 | u8 msg[64]; |
697 | static u8 done = 0; |
712 | static u8 done = 0; |
698 | 713 | ||
699 | if(done) return(1); // just make it once |
714 | if(done) return(1); // just make it once |
700 | 715 | ||
701 | #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;} |
716 | #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;} |
702 | u32 time; |
717 | u32 time; |
703 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
718 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
704 | s16 xscale, yscale, zscale, scale_min, scale_max; |
719 | s16 xscale, yscale, zscale, scale_min, scale_max; |
705 | u8 crb_gain, cra_rate; |
720 | u8 crb_gain, cra_rate; |
706 | u8 i = 0, retval = 1; |
721 | u8 i = 0, retval = 1; |
707 | 722 | ||
708 | switch(NCMAG_MagType) |
723 | switch(NCMAG_MagType) |
709 | { |
724 | { |
710 | case MAG_TYPE_HMC5843: |
725 | case MAG_TYPE_HMC5843: |
711 | crb_gain = HMC5843_CRB_GAIN_10GA; |
726 | crb_gain = HMC5843_CRB_GAIN_15GA; |
712 | cra_rate = HMC5843_CRA_RATE_50HZ; |
727 | cra_rate = HMC5843_CRA_RATE_50HZ; |
713 | xscale = HMC5843_TEST_XSCALE; |
728 | xscale = HMC5843_TEST_XSCALE; |
714 | yscale = HMC5843_TEST_YSCALE; |
729 | yscale = HMC5843_TEST_YSCALE; |
715 | zscale = HMC5843_TEST_ZSCALE; |
730 | zscale = HMC5843_TEST_ZSCALE; |
716 | break; |
731 | break; |
717 | 732 | ||
718 | case MAG_TYPE_LSM303DLH: |
733 | case MAG_TYPE_LSM303DLH: |
719 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
734 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
720 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
735 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
721 | xscale = LSM303DLH_TEST_XSCALE; |
736 | xscale = LSM303DLH_TEST_XSCALE; |
722 | yscale = LSM303DLH_TEST_YSCALE; |
737 | yscale = LSM303DLH_TEST_YSCALE; |
723 | zscale = LSM303DLH_TEST_ZSCALE; |
738 | zscale = LSM303DLH_TEST_ZSCALE; |
724 | break; |
739 | break; |
725 | 740 | ||
726 | default: |
741 | default: |
727 | return(0); |
742 | return(0); |
728 | } |
743 | } |
729 | 744 | ||
730 | MagConfig.cra = cra_rate|CRA_MODE_POSBIAS; |
745 | MagConfig.cra = cra_rate|CRA_MODE_POSBIAS; |
731 | MagConfig.crb = crb_gain; |
746 | MagConfig.crb = crb_gain; |
732 | MagConfig.mode = MODE_CONTINUOUS; |
747 | MagConfig.mode = MODE_CONTINUOUS; |
733 | // activate positive bias field |
748 | // activate positive bias field |
734 | NCMAG_SetMagConfig(); |
749 | NCMAG_SetMagConfig(); |
735 | // wait for stable readings |
750 | // wait for stable readings |
736 | time = SetDelay(50); |
751 | time = SetDelay(50); |
737 | while(!CheckDelay(time)); |
752 | while(!CheckDelay(time)); |
738 | // averaging |
753 | // averaging |
739 | #define AVERAGE 20 |
754 | #define AVERAGE 20 |
740 | for(i = 0; i<AVERAGE; i++) |
755 | for(i = 0; i<AVERAGE; i++) |
741 | { |
756 | { |
742 | NCMAG_GetMagVector(); |
757 | NCMAG_GetMagVector(); |
743 | time = SetDelay(20); |
758 | time = SetDelay(20); |
744 | while(!CheckDelay(time)); |
759 | while(!CheckDelay(time)); |
745 | XMax += MagRawVector.X; |
760 | XMax += MagRawVector.X; |
746 | YMax += MagRawVector.Y; |
761 | YMax += MagRawVector.Y; |
747 | ZMax += MagRawVector.Z; |
762 | ZMax += MagRawVector.Z; |
748 | } |
763 | } |
749 | MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS; |
764 | MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS; |
750 | // activate positive bias field |
765 | // activate positive bias field |
751 | NCMAG_SetMagConfig(); |
766 | NCMAG_SetMagConfig(); |
752 | // wait for stable readings |
767 | // wait for stable readings |
753 | time = SetDelay(50); |
768 | time = SetDelay(50); |
754 | while(!CheckDelay(time)); |
769 | while(!CheckDelay(time)); |
755 | // averaging |
770 | // averaging |
756 | for(i = 0; i < AVERAGE; i++) |
771 | for(i = 0; i < AVERAGE; i++) |
757 | { |
772 | { |
758 | NCMAG_GetMagVector(); |
773 | NCMAG_GetMagVector(); |
759 | time = SetDelay(20); |
774 | time = SetDelay(20); |
760 | while(!CheckDelay(time)); |
775 | while(!CheckDelay(time)); |
761 | XMin += MagRawVector.X; |
776 | XMin += MagRawVector.X; |
762 | YMin += MagRawVector.Y; |
777 | YMin += MagRawVector.Y; |
763 | ZMin += MagRawVector.Z; |
778 | ZMin += MagRawVector.Z; |
764 | } |
779 | } |
765 | // setup final configuration |
780 | // setup final configuration |
766 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
781 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
767 | // activate positive bias field |
782 | // activate positive bias field |
768 | NCMAG_SetMagConfig(); |
783 | NCMAG_SetMagConfig(); |
769 | // check scale for all axes |
784 | // check scale for all axes |
770 | // prepare scale limits |
785 | // prepare scale limits |
771 | LIMITS(xscale, scale_min, scale_max); |
786 | LIMITS(xscale, scale_min, scale_max); |
772 | xscale = (XMax - XMin)/(2*AVERAGE); |
787 | xscale = (XMax - XMin)/(2*AVERAGE); |
773 | if((xscale > scale_max) || (xscale < scale_min)) |
788 | if((xscale > scale_max) || (xscale < scale_min)) |
774 | { |
789 | { |
775 | retval = 0; |
790 | retval = 0; |
776 | sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max); |
791 | sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max); |
777 | UART1_PutString(msg); |
792 | UART1_PutString(msg); |
778 | } |
793 | } |
779 | LIMITS(yscale, scale_min, scale_max); |
794 | LIMITS(yscale, scale_min, scale_max); |
780 | yscale = (YMax - YMin)/(2*AVERAGE); |
795 | yscale = (YMax - YMin)/(2*AVERAGE); |
781 | if((yscale > scale_max) || (yscale < scale_min)) |
796 | if((yscale > scale_max) || (yscale < scale_min)) |
782 | { |
797 | { |
783 | retval = 0; |
798 | retval = 0; |
784 | sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max); |
799 | sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max); |
785 | UART1_PutString(msg); |
800 | UART1_PutString(msg); |
786 | } |
801 | } |
787 | LIMITS(zscale, scale_min, scale_max); |
802 | LIMITS(zscale, scale_min, scale_max); |
788 | zscale = (ZMax - ZMin)/(2*AVERAGE); |
803 | zscale = (ZMax - ZMin)/(2*AVERAGE); |
789 | if((zscale > scale_max) || (zscale < scale_min)) |
804 | if((zscale > scale_max) || (zscale < scale_min)) |
790 | { |
805 | { |
791 | retval = 0; |
806 | retval = 0; |
792 | sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max); |
807 | sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max); |
793 | UART1_PutString(msg); |
808 | UART1_PutString(msg); |
794 | } |
809 | } |
795 | done = retval; |
810 | done = retval; |
796 | return(retval); |
811 | return(retval); |
797 | } |
812 | } |
798 | 813 | ||
799 | 814 | ||
800 | //---------------------------------------------------------------- |
815 | //---------------------------------------------------------------- |
801 | u8 NCMAG_Init(void) |
816 | u8 NCMAG_Init(void) |
802 | { |
817 | { |
803 | u8 msg[64]; |
818 | u8 msg[64]; |
804 | u8 retval = 0; |
819 | u8 retval = 0; |
805 | u8 repeat; |
820 | u8 repeat; |
806 | 821 | ||
807 | NCMAG_Present = 0; |
822 | NCMAG_Present = 0; |
808 | NCMAG_MagType = MAG_TYPE_HMC5843; // assuming having an HMC5843 |
823 | NCMAG_MagType = MAG_TYPE_HMC5843; // assuming having an HMC5843 |
809 | // polling for LSM302DLH option |
824 | // polling for LSM302DLH option |
810 | repeat = 0; |
825 | repeat = 0; |
811 | do |
826 | do |
812 | { |
827 | { |
813 | retval = NCMAG_GetAccConfig(); |
828 | retval = NCMAG_GetAccConfig(); |
814 | if(retval) break; // break loop on success |
829 | if(retval) break; // break loop on success |
815 | UART1_PutString("."); |
830 | UART1_PutString("."); |
816 | repeat++; |
831 | repeat++; |
817 | }while(repeat < 3); |
832 | }while(repeat < 3); |
818 | if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH |
833 | if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH |
819 | // polling of identification |
834 | // polling of identification |
820 | repeat = 0; |
835 | repeat = 0; |
821 | do |
836 | do |
822 | { |
837 | { |
823 | retval = NCMAG_GetIdentification_Sub(); |
838 | retval = NCMAG_GetIdentification_Sub(); |
824 | if(retval) break; // break loop on success |
839 | if(retval) break; // break loop on success |
825 | UART1_PutString("."); |
840 | UART1_PutString("."); |
826 | repeat++; |
841 | repeat++; |
827 | }while(repeat < 12); |
842 | }while(repeat < 12); |
828 | retval = 0; |
843 | retval = 0; |
829 | do |
844 | do |
830 | { |
845 | { |
831 | retval = NCMAG_GetIdentification(); |
846 | retval = NCMAG_GetIdentification(); |
832 | if(retval) break; // break loop on success |
847 | if(retval) break; // break loop on success |
833 | UART1_PutString("."); |
848 | UART1_PutString("."); |
834 | repeat++; |
849 | repeat++; |
835 | }while(repeat < 12); |
850 | }while(repeat < 12); |
836 | 851 | ||
837 | // if we got an answer to id request |
852 | // if we got an answer to id request |
838 | if(retval) |
853 | if(retval) |
839 | { |
854 | { |
840 | u8 n1[] = "\n\r HMC5843"; |
855 | u8 n1[] = "\n\r HMC5843"; |
841 | u8 n2[] = "\n\r LSM303DLH"; |
856 | u8 n2[] = "\n\r LSM303DLH"; |
842 | u8 n3[] = "\n\r LSM303DLM"; |
857 | u8 n3[] = "\n\r LSM303DLM"; |
843 | u8* pn; |
858 | u8* pn; |
844 | 859 | ||
845 | pn = n1; |
860 | pn = n1; |
846 | if(NCMAG_MagType == MAG_TYPE_LSM303DLH) |
861 | if(NCMAG_MagType == MAG_TYPE_LSM303DLH) |
847 | { |
862 | { |
848 | if(NCMAG_Identification2.Sub == 0x3c) pn = n3; |
863 | if(NCMAG_Identification2.Sub == 0x3c) pn = n3; |
849 | else pn = n2; |
864 | else pn = n2; |
850 | } |
865 | } |
851 | 866 | ||
852 | sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub); |
867 | sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub); |
853 | UART1_PutString(msg); |
868 | UART1_PutString(msg); |
854 | if ( (NCMAG_Identification.A == MAG_IDA) |
869 | if ( (NCMAG_Identification.A == MAG_IDA) |
855 | && (NCMAG_Identification.B == MAG_IDB) |
870 | && (NCMAG_Identification.B == MAG_IDB) |
856 | && (NCMAG_Identification.C == MAG_IDC)) |
871 | && (NCMAG_Identification.C == MAG_IDC)) |
857 | { |
872 | { |
858 | NCMAG_Present = 1; |
873 | NCMAG_Present = 1; |
859 | 874 | ||
860 | if(EEPROM_Init()) |
875 | if(EEPROM_Init()) |
861 | { |
876 | { |
862 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
877 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
863 | if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!"); |
878 | if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!"); |
864 | } |
879 | } |
865 | else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!"); |
880 | else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!"); |
866 | 881 | ||
867 | if(NCMAG_Identification2.Sub == 0x00) |
882 | if(NCMAG_Identification2.Sub == 0x00) |
868 | { |
883 | { |
869 | if(!NCMAG_SelfTest()) |
884 | if(!NCMAG_SelfTest()) |
870 | { |
885 | { |
871 | UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n"); |
886 | UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n"); |
872 | LED_RED_ON; |
887 | LED_RED_ON; |
873 | NCMAG_IsCalibrated = 0; |
888 | NCMAG_IsCalibrated = 0; |
874 | } else UART1_PutString("\r\n Selftest ok"); |
889 | } else UART1_PutString("\r\n Selftest ok"); |
875 | } |
890 | } |
876 | else InitNC_MagnetSensor(); |
891 | else InitNC_MagnetSensor(); |
877 | } |
892 | } |
878 | else |
893 | else |
879 | { |
894 | { |
880 | UART1_PutString("\n\r Not compatible!"); |
895 | UART1_PutString("\n\r Not compatible!"); |
881 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
896 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
882 | LED_RED_ON; |
897 | LED_RED_ON; |
883 | } |
898 | } |
884 | } |
899 | } |
885 | else // nothing found |
900 | else // nothing found |
886 | { |
901 | { |
887 | NCMAG_MagType = MAG_TYPE_NONE; |
902 | NCMAG_MagType = MAG_TYPE_NONE; |
888 | UART1_PutString("not found!"); |
903 | UART1_PutString("not found!"); |
889 | } |
904 | } |
890 | return(NCMAG_Present); |
905 | return(NCMAG_Present); |
891 | } |
906 | } |
892 | 907 | ||
893 | 908 |