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