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