Rev 342 | Rev 394 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 342 | Rev 360 | ||
---|---|---|---|
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 |
- | |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
- | |
7 | // + FOR NON COMMERCIAL USE ONLY |
- | |
8 | // + www.MikroKopter.com |
5 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | 7 | // + Software Nutzungsbedingungen (english version: see below) |
|
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
- | |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
22 | // + eindeutig als Ursprung verlinkt werden |
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
25 | // + Benutzung auf eigene Gefahr |
22 | // + des Mitverschuldens offen. |
- | 23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
|
- | 24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
|
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
- | 26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
|
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
29 | // + mit unserer Zustimmung zulässig |
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
- | 30 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
|
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
32 | // + Software LICENSING TERMS |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
34 | // + this list of conditions and the following disclaimer. |
35 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
36 | // + from this software without specific prior written permission. |
37 | // + The Software may only be used with the Licensor's products. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
38 | // + for non-commercial use (directly or indirectly) |
- | |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
40 | // + with our written permission |
40 | // + agreement shall be the property of the Licensor. |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
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 |
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
44 | // |
- | |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
43 | // + The customer shall be responsible for taking reasonable precautions |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
53 | // + #### END OF LICENSING TERMS #### |
- | 54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
|
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <math.h> |
56 | #include <math.h> |
58 | #include <stdio.h> |
57 | #include <stdio.h> |
59 | #include <string.h> |
58 | #include <string.h> |
60 | #include "91x_lib.h" |
59 | #include "91x_lib.h" |
61 | #include "ncmag.h" |
60 | #include "ncmag.h" |
62 | #include "i2c.h" |
61 | #include "i2c.h" |
63 | #include "timer1.h" |
62 | #include "timer1.h" |
64 | #include "led.h" |
63 | #include "led.h" |
65 | #include "uart1.h" |
64 | #include "uart1.h" |
66 | #include "eeprom.h" |
65 | #include "eeprom.h" |
67 | #include "mymath.h" |
66 | #include "mymath.h" |
68 | #include "main.h" |
67 | #include "main.h" |
69 | 68 | ||
70 | u8 NCMAG_Present = 0; |
69 | u8 NCMAG_Present = 0; |
71 | u8 NCMAG_IsCalibrated = 0; |
70 | u8 NCMAG_IsCalibrated = 0; |
72 | 71 | ||
73 | #define MAG_TYPE_NONE 0 |
72 | #define MAG_TYPE_NONE 0 |
74 | #define MAG_TYPE_HMC5843 1 |
73 | #define MAG_TYPE_HMC5843 1 |
75 | #define MAG_TYPE_LSM303DLH 2 |
74 | #define MAG_TYPE_LSM303DLH 2 |
76 | u8 NCMAG_MagType = MAG_TYPE_NONE; |
75 | u8 NCMAG_MagType = MAG_TYPE_NONE; |
77 | 76 | ||
78 | #define CALIBRATION_VERSION 1 |
77 | #define CALIBRATION_VERSION 1 |
79 | #define EEPROM_ADR_MAG_CALIBRATION 50 |
78 | #define EEPROM_ADR_MAG_CALIBRATION 50 |
80 | #define MAG_CALIBRATION_COMPATIBEL 0xA2 |
79 | #define MAG_CALIBRATION_COMPATIBEL 0xA2 |
81 | 80 | ||
82 | #define NCMAG_MIN_RAWVALUE -2047 |
81 | #define NCMAG_MIN_RAWVALUE -2047 |
83 | #define NCMAG_MAX_RAWVALUE 2047 |
82 | #define NCMAG_MAX_RAWVALUE 2047 |
84 | #define NCMAG_INVALID_DATA -4096 |
83 | #define NCMAG_INVALID_DATA -4096 |
85 | 84 | ||
86 | typedef struct |
85 | typedef struct |
87 | { |
86 | { |
88 | s16 Range; |
87 | s16 Range; |
89 | s16 Offset; |
88 | s16 Offset; |
90 | } __attribute__((packed)) Scaling_t; |
89 | } __attribute__((packed)) Scaling_t; |
91 | 90 | ||
92 | typedef struct |
91 | typedef struct |
93 | { |
92 | { |
94 | Scaling_t MagX; |
93 | Scaling_t MagX; |
95 | Scaling_t MagY; |
94 | Scaling_t MagY; |
96 | Scaling_t MagZ; |
95 | Scaling_t MagZ; |
97 | u8 Version; |
96 | u8 Version; |
98 | u8 crc; |
97 | u8 crc; |
99 | } __attribute__((packed)) Calibration_t; |
98 | } __attribute__((packed)) Calibration_t; |
100 | 99 | ||
101 | Calibration_t Calibration; // calibration data in RAM |
100 | Calibration_t Calibration; // calibration data in RAM |
102 | volatile s16vec_t AccRawVector; |
101 | volatile s16vec_t AccRawVector; |
103 | volatile s16vec_t MagRawVector; |
102 | volatile s16vec_t MagRawVector; |
104 | 103 | ||
105 | // i2c MAG interface |
104 | // i2c MAG interface |
106 | #define MAG_SLAVE_ADDRESS 0x3C // i2C slave address mag. sensor registers |
105 | #define MAG_SLAVE_ADDRESS 0x3C // i2C slave address mag. sensor registers |
107 | 106 | ||
108 | // register mapping |
107 | // register mapping |
109 | #define REG_MAG_CRA 0x00 |
108 | #define REG_MAG_CRA 0x00 |
110 | #define REG_MAG_CRB 0x01 |
109 | #define REG_MAG_CRB 0x01 |
111 | #define REG_MAG_MODE 0x02 |
110 | #define REG_MAG_MODE 0x02 |
112 | #define REG_MAG_DATAX_MSB 0x03 |
111 | #define REG_MAG_DATAX_MSB 0x03 |
113 | #define REG_MAG_DATAX_LSB 0x04 |
112 | #define REG_MAG_DATAX_LSB 0x04 |
114 | #define REG_MAG_DATAY_MSB 0x05 |
113 | #define REG_MAG_DATAY_MSB 0x05 |
115 | #define REG_MAG_DATAY_LSB 0x06 |
114 | #define REG_MAG_DATAY_LSB 0x06 |
116 | #define REG_MAG_DATAZ_MSB 0x07 |
115 | #define REG_MAG_DATAZ_MSB 0x07 |
117 | #define REG_MAG_DATAZ_LSB 0x08 |
116 | #define REG_MAG_DATAZ_LSB 0x08 |
118 | #define REG_MAG_STATUS 0x09 |
117 | #define REG_MAG_STATUS 0x09 |
119 | 118 | ||
120 | #define REG_MAG_IDA 0x0A |
119 | #define REG_MAG_IDA 0x0A |
121 | #define REG_MAG_IDB 0x0B |
120 | #define REG_MAG_IDB 0x0B |
122 | #define REG_MAG_IDC 0x0C |
121 | #define REG_MAG_IDC 0x0C |
123 | #define REG_MAG_IDF 0x0F |
122 | #define REG_MAG_IDF 0x0F |
124 | 123 | ||
125 | // bit mask for configuration mode |
124 | // bit mask for configuration mode |
126 | #define CRA_MODE_MASK 0x03 |
125 | #define CRA_MODE_MASK 0x03 |
127 | #define CRA_MODE_NORMAL 0x00 //default |
126 | #define CRA_MODE_NORMAL 0x00 //default |
128 | #define CRA_MODE_POSBIAS 0x01 |
127 | #define CRA_MODE_POSBIAS 0x01 |
129 | #define CRA_MODE_NEGBIAS 0x02 |
128 | #define CRA_MODE_NEGBIAS 0x02 |
130 | #define CRA_MODE_SELFTEST 0x03 |
129 | #define CRA_MODE_SELFTEST 0x03 |
131 | 130 | ||
132 | // bit mask for measurement mode |
131 | // bit mask for measurement mode |
133 | #define MODE_MASK 0xFF |
132 | #define MODE_MASK 0xFF |
134 | #define MODE_CONTINUOUS 0x00 |
133 | #define MODE_CONTINUOUS 0x00 |
135 | #define MODE_SINGLE 0x01 // default |
134 | #define MODE_SINGLE 0x01 // default |
136 | #define MODE_IDLE 0x02 |
135 | #define MODE_IDLE 0x02 |
137 | #define MODE_SLEEP 0x03 |
136 | #define MODE_SLEEP 0x03 |
138 | 137 | ||
139 | // bit mask for rate |
138 | // bit mask for rate |
140 | #define CRA_RATE_MASK 0x1C |
139 | #define CRA_RATE_MASK 0x1C |
141 | 140 | ||
142 | // bit mask for gain |
141 | // bit mask for gain |
143 | #define CRB_GAIN_MASK 0xE0 |
142 | #define CRB_GAIN_MASK 0xE0 |
144 | 143 | ||
145 | // ids |
144 | // ids |
146 | #define MAG_IDA 0x48 |
145 | #define MAG_IDA 0x48 |
147 | #define MAG_IDB 0x34 |
146 | #define MAG_IDB 0x34 |
148 | #define MAG_IDC 0x33 |
147 | #define MAG_IDC 0x33 |
149 | 148 | ||
150 | // the special HMC5843 interface |
149 | // the special HMC5843 interface |
151 | // bit mask for rate |
150 | // bit mask for rate |
152 | #define HMC5843_CRA_RATE_0_5HZ 0x00 |
151 | #define HMC5843_CRA_RATE_0_5HZ 0x00 |
153 | #define HMC5843_CRA_RATE_1HZ 0x04 |
152 | #define HMC5843_CRA_RATE_1HZ 0x04 |
154 | #define HMC5843_CRA_RATE_2HZ 0x08 |
153 | #define HMC5843_CRA_RATE_2HZ 0x08 |
155 | #define HMC5843_CRA_RATE_5HZ 0x0C |
154 | #define HMC5843_CRA_RATE_5HZ 0x0C |
156 | #define HMC5843_CRA_RATE_10HZ 0x10 //default |
155 | #define HMC5843_CRA_RATE_10HZ 0x10 //default |
157 | #define HMC5843_CRA_RATE_20HZ 0x14 |
156 | #define HMC5843_CRA_RATE_20HZ 0x14 |
158 | #define HMC5843_CRA_RATE_50HZ 0x18 |
157 | #define HMC5843_CRA_RATE_50HZ 0x18 |
159 | // bit mask for gain |
158 | // bit mask for gain |
160 | #define HMC5843_CRB_GAIN_07GA 0x00 |
159 | #define HMC5843_CRB_GAIN_07GA 0x00 |
161 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
160 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
162 | #define HMC5843_CRB_GAIN_15GA 0x40 // <--- we use this |
161 | #define HMC5843_CRB_GAIN_15GA 0x40 // <--- we use this |
163 | #define HMC5843_CRB_GAIN_20GA 0x60 |
162 | #define HMC5843_CRB_GAIN_20GA 0x60 |
164 | #define HMC5843_CRB_GAIN_32GA 0x80 |
163 | #define HMC5843_CRB_GAIN_32GA 0x80 |
165 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
164 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
166 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
165 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
167 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
166 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
168 | // self test value |
167 | // self test value |
169 | #define HMC5843_TEST_XSCALE 555 |
168 | #define HMC5843_TEST_XSCALE 555 |
170 | #define HMC5843_TEST_YSCALE 555 |
169 | #define HMC5843_TEST_YSCALE 555 |
171 | #define HMC5843_TEST_ZSCALE 555 |
170 | #define HMC5843_TEST_ZSCALE 555 |
172 | // clibration range |
171 | // clibration range |
173 | #define HMC5843_CALIBRATION_RANGE 600 |
172 | #define HMC5843_CALIBRATION_RANGE 600 |
174 | 173 | ||
175 | // the special LSM302DLH interface |
174 | // the special LSM302DLH interface |
176 | // bit mask for rate |
175 | // bit mask for rate |
177 | #define LSM303DLH_CRA_RATE_0_75HZ 0x00 |
176 | #define LSM303DLH_CRA_RATE_0_75HZ 0x00 |
178 | #define LSM303DLH_CRA_RATE_1_5HZ 0x04 |
177 | #define LSM303DLH_CRA_RATE_1_5HZ 0x04 |
179 | #define LSM303DLH_CRA_RATE_3_0HZ 0x08 |
178 | #define LSM303DLH_CRA_RATE_3_0HZ 0x08 |
180 | #define LSM303DLH_CRA_RATE_7_5HZ 0x0C |
179 | #define LSM303DLH_CRA_RATE_7_5HZ 0x0C |
181 | #define LSM303DLH_CRA_RATE_15HZ 0x10 //default |
180 | #define LSM303DLH_CRA_RATE_15HZ 0x10 //default |
182 | #define LSM303DLH_CRA_RATE_30HZ 0x14 |
181 | #define LSM303DLH_CRA_RATE_30HZ 0x14 |
183 | #define LSM303DLH_CRA_RATE_75HZ 0x18 |
182 | #define LSM303DLH_CRA_RATE_75HZ 0x18 |
184 | 183 | ||
185 | // bit mask for gain |
184 | // bit mask for gain |
186 | #define LSM303DLH_CRB_GAIN_XXGA 0x00 |
185 | #define LSM303DLH_CRB_GAIN_XXGA 0x00 |
187 | #define LSM303DLH_CRB_GAIN_13GA 0x20 //default |
186 | #define LSM303DLH_CRB_GAIN_13GA 0x20 //default |
188 | #define LSM303DLH_CRB_GAIN_19GA 0x40 // <--- we use this |
187 | #define LSM303DLH_CRB_GAIN_19GA 0x40 // <--- we use this |
189 | #define LSM303DLH_CRB_GAIN_25GA 0x60 |
188 | #define LSM303DLH_CRB_GAIN_25GA 0x60 |
190 | #define LSM303DLH_CRB_GAIN_40GA 0x80 |
189 | #define LSM303DLH_CRB_GAIN_40GA 0x80 |
191 | #define LSM303DLH_CRB_GAIN_47GA 0xA0 |
190 | #define LSM303DLH_CRB_GAIN_47GA 0xA0 |
192 | #define LSM303DLH_CRB_GAIN_56GA 0xC0 |
191 | #define LSM303DLH_CRB_GAIN_56GA 0xC0 |
193 | #define LSM303DLH_CRB_GAIN_81GA 0xE0 |
192 | #define LSM303DLH_CRB_GAIN_81GA 0xE0 |
194 | // self test value |
193 | // self test value |
195 | #define LSM303DLH_TEST_XSCALE 495 |
194 | #define LSM303DLH_TEST_XSCALE 495 |
196 | #define LSM303DLH_TEST_YSCALE 495 |
195 | #define LSM303DLH_TEST_YSCALE 495 |
197 | #define LSM303DLH_TEST_ZSCALE 470 |
196 | #define LSM303DLH_TEST_ZSCALE 470 |
198 | // clibration range |
197 | // clibration range |
199 | #define LSM303_CALIBRATION_RANGE 550 |
198 | #define LSM303_CALIBRATION_RANGE 550 |
200 | 199 | ||
201 | // the i2c ACC interface |
200 | // the i2c ACC interface |
202 | #define ACC_SLAVE_ADDRESS 0x30 // i2c slave for acc. sensor registers |
201 | #define ACC_SLAVE_ADDRESS 0x30 // i2c slave for acc. sensor registers |
203 | // register mapping |
202 | // register mapping |
204 | #define REG_ACC_CTRL1 0x20 |
203 | #define REG_ACC_CTRL1 0x20 |
205 | #define REG_ACC_CTRL2 0x21 |
204 | #define REG_ACC_CTRL2 0x21 |
206 | #define REG_ACC_CTRL3 0x22 |
205 | #define REG_ACC_CTRL3 0x22 |
207 | #define REG_ACC_CTRL4 0x23 |
206 | #define REG_ACC_CTRL4 0x23 |
208 | #define REG_ACC_CTRL5 0x24 |
207 | #define REG_ACC_CTRL5 0x24 |
209 | #define REG_ACC_HP_FILTER_RESET 0x25 |
208 | #define REG_ACC_HP_FILTER_RESET 0x25 |
210 | #define REG_ACC_REFERENCE 0x26 |
209 | #define REG_ACC_REFERENCE 0x26 |
211 | #define REG_ACC_STATUS 0x27 |
210 | #define REG_ACC_STATUS 0x27 |
212 | #define REG_ACC_X_LSB 0x28 |
211 | #define REG_ACC_X_LSB 0x28 |
213 | #define REG_ACC_X_MSB 0x29 |
212 | #define REG_ACC_X_MSB 0x29 |
214 | #define REG_ACC_Y_LSB 0x2A |
213 | #define REG_ACC_Y_LSB 0x2A |
215 | #define REG_ACC_Y_MSB 0x2B |
214 | #define REG_ACC_Y_MSB 0x2B |
216 | #define REG_ACC_Z_LSB 0x2C |
215 | #define REG_ACC_Z_LSB 0x2C |
217 | #define REG_ACC_Z_MSB 0x2D |
216 | #define REG_ACC_Z_MSB 0x2D |
218 | 217 | ||
219 | 218 | ||
220 | 219 | ||
221 | typedef struct |
220 | typedef struct |
222 | { |
221 | { |
223 | u8 A; |
222 | u8 A; |
224 | u8 B; |
223 | u8 B; |
225 | u8 C; |
224 | u8 C; |
226 | } __attribute__((packed)) Identification_t; |
225 | } __attribute__((packed)) Identification_t; |
227 | volatile Identification_t NCMAG_Identification; |
226 | volatile Identification_t NCMAG_Identification; |
228 | 227 | ||
229 | typedef struct |
228 | typedef struct |
230 | { |
229 | { |
231 | u8 Sub; |
230 | u8 Sub; |
232 | } __attribute__((packed)) Identification2_t; |
231 | } __attribute__((packed)) Identification2_t; |
233 | volatile Identification2_t NCMAG_Identification2; |
232 | volatile Identification2_t NCMAG_Identification2; |
234 | 233 | ||
235 | typedef struct |
234 | typedef struct |
236 | { |
235 | { |
237 | u8 cra; |
236 | u8 cra; |
238 | u8 crb; |
237 | u8 crb; |
239 | u8 mode; |
238 | u8 mode; |
240 | } __attribute__((packed)) MagConfig_t; |
239 | } __attribute__((packed)) MagConfig_t; |
241 | 240 | ||
242 | volatile MagConfig_t MagConfig; |
241 | volatile MagConfig_t MagConfig; |
243 | 242 | ||
244 | typedef struct |
243 | typedef struct |
245 | { |
244 | { |
246 | u8 ctrl_1; |
245 | u8 ctrl_1; |
247 | u8 ctrl_2; |
246 | u8 ctrl_2; |
248 | u8 ctrl_3; |
247 | u8 ctrl_3; |
249 | u8 ctrl_4; |
248 | u8 ctrl_4; |
250 | u8 ctrl_5; |
249 | u8 ctrl_5; |
251 | } __attribute__((packed)) AccConfig_t; |
250 | } __attribute__((packed)) AccConfig_t; |
252 | 251 | ||
253 | volatile AccConfig_t AccConfig; |
252 | volatile AccConfig_t AccConfig; |
254 | 253 | ||
255 | u8 NCMag_CalibrationWrite(void) |
254 | u8 NCMag_CalibrationWrite(void) |
256 | { |
255 | { |
257 | u8 i, crc = MAG_CALIBRATION_COMPATIBEL; |
256 | u8 i, crc = MAG_CALIBRATION_COMPATIBEL; |
258 | EEPROM_Result_t eres; |
257 | EEPROM_Result_t eres; |
259 | u8 *pBuff = (u8*)&Calibration; |
258 | u8 *pBuff = (u8*)&Calibration; |
260 | 259 | ||
261 | Calibration.Version = CALIBRATION_VERSION; |
260 | Calibration.Version = CALIBRATION_VERSION; |
262 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
261 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
263 | { |
262 | { |
264 | crc += pBuff[i]; |
263 | crc += pBuff[i]; |
265 | } |
264 | } |
266 | Calibration.crc = ~crc; |
265 | Calibration.crc = ~crc; |
267 | eres = EEPROM_WriteBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)); |
266 | eres = EEPROM_WriteBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration)); |
268 | if(EEPROM_SUCCESS == eres) i = 1; |
267 | if(EEPROM_SUCCESS == eres) i = 1; |
269 | else i = 0; |
268 | else i = 0; |
270 | return(i); |
269 | return(i); |
271 | } |
270 | } |
272 | 271 | ||
273 | u8 NCMag_CalibrationRead(void) |
272 | u8 NCMag_CalibrationRead(void) |
274 | { |
273 | { |
275 | u8 i, crc = MAG_CALIBRATION_COMPATIBEL; |
274 | u8 i, crc = MAG_CALIBRATION_COMPATIBEL; |
276 | u8 *pBuff = (u8*)&Calibration; |
275 | u8 *pBuff = (u8*)&Calibration; |
277 | 276 | ||
278 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration))) |
277 | if(EEPROM_SUCCESS == EEPROM_ReadBlock(EEPROM_ADR_MAG_CALIBRATION, pBuff, sizeof(Calibration))) |
279 | { |
278 | { |
280 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
279 | for(i = 0; i<(sizeof(Calibration)-1); i++) |
281 | { |
280 | { |
282 | crc += pBuff[i]; |
281 | crc += pBuff[i]; |
283 | } |
282 | } |
284 | crc = ~crc; |
283 | crc = ~crc; |
285 | if(Calibration.crc != crc) return(0); // crc mismatch |
284 | if(Calibration.crc != crc) return(0); // crc mismatch |
286 | if(Calibration.Version == CALIBRATION_VERSION) return(1); |
285 | if(Calibration.Version == CALIBRATION_VERSION) return(1); |
287 | } |
286 | } |
288 | return(0); |
287 | return(0); |
289 | } |
288 | } |
290 | 289 | ||
291 | 290 | ||
292 | void NCMAG_Calibrate(void) |
291 | void NCMAG_Calibrate(void) |
293 | { |
292 | { |
294 | u8 msg[64]; |
293 | u8 msg[64]; |
295 | static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
294 | static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0; |
296 | static s16 X = 0, Y = 0, Z = 0; |
295 | static s16 X = 0, Y = 0, Z = 0; |
297 | static u8 OldCalState = 0; |
296 | static u8 OldCalState = 0; |
298 | s16 MinCaclibration = 450; |
297 | s16 MinCaclibration = 450; |
299 | 298 | ||
300 | X = (4*X + MagRawVector.X + 3)/5; |
299 | X = (4*X + MagRawVector.X + 3)/5; |
301 | Y = (4*Y + MagRawVector.Y + 3)/5; |
300 | Y = (4*Y + MagRawVector.Y + 3)/5; |
302 | Z = (4*Z + MagRawVector.Z + 3)/5; |
301 | Z = (4*Z + MagRawVector.Z + 3)/5; |
303 | 302 | ||
304 | switch(Compass_CalState) |
303 | switch(Compass_CalState) |
305 | { |
304 | { |
306 | case 1: |
305 | case 1: |
307 | // 1st step of calibration |
306 | // 1st step of calibration |
308 | // initialize ranges |
307 | // initialize ranges |
309 | // used to change the orientation of the NC in the horizontal plane |
308 | // used to change the orientation of the NC in the horizontal plane |
310 | Xmin = 10000; |
309 | Xmin = 10000; |
311 | Xmax = -10000; |
310 | Xmax = -10000; |
312 | Ymin = 10000; |
311 | Ymin = 10000; |
313 | Ymax = -10000; |
312 | Ymax = -10000; |
314 | Zmin = 10000; |
313 | Zmin = 10000; |
315 | Zmax = -10000; |
314 | Zmax = -10000; |
316 | break; |
315 | break; |
317 | 316 | ||
318 | case 2: // 2nd step of calibration |
317 | case 2: // 2nd step of calibration |
319 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
318 | // find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane |
320 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
319 | if(X < Xmin) { Xmin = X; BeepTime = 20;} |
321 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
320 | else if(X > Xmax) { Xmax = X; BeepTime = 20;} |
322 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
321 | if(Y < Ymin) { Ymin = Y; BeepTime = 60;} |
323 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
322 | else if(Y > Ymax) { Ymax = Y; BeepTime = 60;} |
324 | break; |
323 | break; |
325 | 324 | ||
326 | case 3: // 3rd step of calibration |
325 | case 3: // 3rd step of calibration |
327 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
326 | // used to change the orientation of the MK3MAG vertical to the horizontal plane |
328 | break; |
327 | break; |
329 | 328 | ||
330 | case 4: |
329 | case 4: |
331 | // find Min and Max of the Z-Sensor |
330 | // find Min and Max of the Z-Sensor |
332 | if(Z < Zmin) { Zmin = Z; BeepTime = 80;} |
331 | if(Z < Zmin) { Zmin = Z; BeepTime = 80;} |
333 | else if(Z > Zmax) { Zmax = Z; BeepTime = 80;} |
332 | else if(Z > Zmax) { Zmax = Z; BeepTime = 80;} |
334 | break; |
333 | break; |
335 | 334 | ||
336 | case 5: |
335 | case 5: |
337 | // Save values |
336 | // Save values |
338 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
337 | if(Compass_CalState != OldCalState) // avoid continously writing of eeprom! |
339 | { |
338 | { |
340 | // #define MIN_CALIBRATION 256 |
339 | // #define MIN_CALIBRATION 256 |
341 | if(NCMAG_MagType == MAG_TYPE_HMC5843) |
340 | if(NCMAG_MagType == MAG_TYPE_HMC5843) |
342 | { |
341 | { |
343 | UART1_PutString("\r\nHMC5843 calibration\n\r"); |
342 | UART1_PutString("\r\nHMC5843 calibration\n\r"); |
344 | MinCaclibration = HMC5843_CALIBRATION_RANGE; |
343 | MinCaclibration = HMC5843_CALIBRATION_RANGE; |
345 | } |
344 | } |
346 | if(NCMAG_MagType == MAG_TYPE_LSM303DLH) |
345 | if(NCMAG_MagType == MAG_TYPE_LSM303DLH) |
347 | { |
346 | { |
348 | UART1_PutString("\r\n\r\nLSM303 calibration\n\r"); |
347 | UART1_PutString("\r\n\r\nLSM303 calibration\n\r"); |
349 | MinCaclibration =LSM303_CALIBRATION_RANGE; |
348 | MinCaclibration =LSM303_CALIBRATION_RANGE; |
350 | } |
349 | } |
351 | if(EarthMagneticStrengthTheoretic) |
350 | if(EarthMagneticStrengthTheoretic) |
352 | { |
351 | { |
353 | MinCaclibration = (MinCaclibration * EarthMagneticStrengthTheoretic) / 50; |
352 | MinCaclibration = (MinCaclibration * EarthMagneticStrengthTheoretic) / 50; |
354 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
353 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
355 | UART1_PutString(msg); |
354 | UART1_PutString(msg); |
356 | } |
355 | } |
357 | else UART1_PutString("without GPS\n\r"); |
356 | else UART1_PutString("without GPS\n\r"); |
358 | 357 | ||
359 | Calibration.MagX.Range = Xmax - Xmin; |
358 | Calibration.MagX.Range = Xmax - Xmin; |
360 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
359 | Calibration.MagX.Offset = (Xmin + Xmax) / 2; |
361 | Calibration.MagY.Range = Ymax - Ymin; |
360 | Calibration.MagY.Range = Ymax - Ymin; |
362 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
361 | Calibration.MagY.Offset = (Ymin + Ymax) / 2; |
363 | Calibration.MagZ.Range = Zmax - Zmin; |
362 | Calibration.MagZ.Range = Zmax - Zmin; |
364 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
363 | Calibration.MagZ.Offset = (Zmin + Zmax) / 2; |
365 | if((Calibration.MagX.Range > MinCaclibration) && (Calibration.MagY.Range > MinCaclibration) && (Calibration.MagZ.Range > MinCaclibration)) |
364 | if((Calibration.MagX.Range > MinCaclibration) && (Calibration.MagY.Range > MinCaclibration) && (Calibration.MagZ.Range > MinCaclibration)) |
366 | { |
365 | { |
367 | NCMAG_IsCalibrated = NCMag_CalibrationWrite(); |
366 | NCMAG_IsCalibrated = NCMag_CalibrationWrite(); |
368 | BeepTime = 2500; |
367 | BeepTime = 2500; |
369 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
368 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
370 | } |
369 | } |
371 | else |
370 | else |
372 | { |
371 | { |
373 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
372 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
374 | if(Calibration.MagX.Range < MinCaclibration) UART1_PutString("X! "); |
373 | if(Calibration.MagX.Range < MinCaclibration) UART1_PutString("X! "); |
375 | if(Calibration.MagY.Range < MinCaclibration) UART1_PutString("Y! "); |
374 | if(Calibration.MagY.Range < MinCaclibration) UART1_PutString("Y! "); |
376 | if(Calibration.MagZ.Range < MinCaclibration) UART1_PutString("Z! "); |
375 | if(Calibration.MagZ.Range < MinCaclibration) UART1_PutString("Z! "); |
377 | UART1_PutString("\r\n"); |
376 | UART1_PutString("\r\n"); |
378 | 377 | ||
379 | // restore old calibration data from eeprom |
378 | // restore old calibration data from eeprom |
380 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
379 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
381 | } |
380 | } |
382 | sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin); |
381 | sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin); |
383 | UART1_PutString(msg); |
382 | UART1_PutString(msg); |
384 | sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin); |
383 | sprintf(msg, "Y: (%i - %i = %i)\r\n",Ymax,Ymin,Ymax - Ymin); |
385 | UART1_PutString(msg); |
384 | UART1_PutString(msg); |
386 | sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin); |
385 | sprintf(msg, "Z: (%i - %i = %i)\r\n",Zmax,Zmin,Zmax - Zmin); |
387 | UART1_PutString(msg); |
386 | UART1_PutString(msg); |
388 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCaclibration); |
387 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCaclibration); |
389 | UART1_PutString(msg); |
388 | UART1_PutString(msg); |
390 | } |
389 | } |
391 | break; |
390 | break; |
392 | 391 | ||
393 | default: |
392 | default: |
394 | break; |
393 | break; |
395 | } |
394 | } |
396 | OldCalState = Compass_CalState; |
395 | OldCalState = Compass_CalState; |
397 | } |
396 | } |
398 | 397 | ||
399 | // ---------- call back handlers ----------------------------------------- |
398 | // ---------- call back handlers ----------------------------------------- |
400 | 399 | ||
401 | // rx data handler for id info request |
400 | // rx data handler for id info request |
402 | void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize) |
401 | void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize) |
403 | { // if number of bytes are matching |
402 | { // if number of bytes are matching |
404 | if(RxBufferSize == sizeof(NCMAG_Identification) ) |
403 | if(RxBufferSize == sizeof(NCMAG_Identification) ) |
405 | { |
404 | { |
406 | memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification)); |
405 | memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification)); |
407 | } |
406 | } |
408 | } |
407 | } |
409 | 408 | ||
410 | void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize) |
409 | void NCMAG_UpdateIdentification_Sub(u8* pRxBuffer, u8 RxBufferSize) |
411 | { // if number of bytes are matching |
410 | { // if number of bytes are matching |
412 | if(RxBufferSize == sizeof(NCMAG_Identification2)) |
411 | if(RxBufferSize == sizeof(NCMAG_Identification2)) |
413 | { |
412 | { |
414 | memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2)); |
413 | memcpy((u8 *)&NCMAG_Identification2, pRxBuffer, sizeof(NCMAG_Identification2)); |
415 | } |
414 | } |
416 | } |
415 | } |
417 | 416 | ||
418 | // rx data handler for magnetic sensor raw data |
417 | // rx data handler for magnetic sensor raw data |
419 | void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
418 | void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
420 | { // if number of bytes are matching |
419 | { // if number of bytes are matching |
421 | if(RxBufferSize == sizeof(MagRawVector) ) |
420 | if(RxBufferSize == sizeof(MagRawVector) ) |
422 | { // byte order from big to little endian |
421 | { // byte order from big to little endian |
423 | s16 raw; |
422 | s16 raw; |
424 | raw = pRxBuffer[0]<<8; |
423 | raw = pRxBuffer[0]<<8; |
425 | raw+= pRxBuffer[1]; |
424 | raw+= pRxBuffer[1]; |
426 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw; |
425 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) MagRawVector.X = raw; |
427 | raw = pRxBuffer[2]<<8; |
426 | raw = pRxBuffer[2]<<8; |
428 | raw+= pRxBuffer[3]; |
427 | raw+= pRxBuffer[3]; |
429 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
428 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
430 | { |
429 | { |
431 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Z = raw; // here Z and Y are exchanged |
430 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Z = raw; // here Z and Y are exchanged |
432 | else MagRawVector.Y = raw; |
431 | else MagRawVector.Y = raw; |
433 | } |
432 | } |
434 | raw = pRxBuffer[4]<<8; |
433 | raw = pRxBuffer[4]<<8; |
435 | raw+= pRxBuffer[5]; |
434 | raw+= pRxBuffer[5]; |
436 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
435 | if(raw >= NCMAG_MIN_RAWVALUE && raw <= NCMAG_MAX_RAWVALUE) |
437 | { |
436 | { |
438 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Y = raw; // here Z and Y are exchanged |
437 | if(NCMAG_Identification2.Sub == 0x3c) MagRawVector.Y = raw; // here Z and Y are exchanged |
439 | else MagRawVector.Z = raw; |
438 | else MagRawVector.Z = raw; |
440 | } |
439 | } |
441 | //MagRawVector.X += 2 * ((s32) FC.Poti[7] - 128); |
440 | //MagRawVector.X += 2 * ((s32) FC.Poti[7] - 128); |
442 | } |
441 | } |
443 | if(Compass_CalState || !NCMAG_IsCalibrated) |
442 | if(Compass_CalState || !NCMAG_IsCalibrated) |
444 | { // mark out data invalid |
443 | { // mark out data invalid |
445 | MagVector.X = MagRawVector.X; |
444 | MagVector.X = MagRawVector.X; |
446 | MagVector.Y = MagRawVector.Y; |
445 | MagVector.Y = MagRawVector.Y; |
447 | MagVector.Z = MagRawVector.Z; |
446 | MagVector.Z = MagRawVector.Z; |
448 | Compass_Heading = -1; |
447 | Compass_Heading = -1; |
449 | } |
448 | } |
450 | else |
449 | else |
451 | { |
450 | { |
452 | // update MagVector from MagRaw Vector by Scaling |
451 | // update MagVector from MagRaw Vector by Scaling |
453 | MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range); |
452 | MagVector.X = (s16)((1024L*(s32)(MagRawVector.X - Calibration.MagX.Offset))/Calibration.MagX.Range); |
454 | MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range); |
453 | MagVector.Y = (s16)((1024L*(s32)(MagRawVector.Y - Calibration.MagY.Offset))/Calibration.MagY.Range); |
455 | MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range); |
454 | MagVector.Z = (s16)((1024L*(s32)(MagRawVector.Z - Calibration.MagZ.Offset))/Calibration.MagZ.Range); |
456 | Compass_CalcHeading(); |
455 | Compass_CalcHeading(); |
457 | } |
456 | } |
458 | } |
457 | } |
459 | // rx data handler for acceleration raw data |
458 | // rx data handler for acceleration raw data |
460 | void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize) |
459 | void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize) |
461 | { // if number of byte are matching |
460 | { // if number of byte are matching |
462 | if(RxBufferSize == sizeof(AccRawVector) ) |
461 | if(RxBufferSize == sizeof(AccRawVector) ) |
463 | { |
462 | { |
464 | memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector)); |
463 | memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector)); |
465 | } |
464 | } |
466 | } |
465 | } |
467 | // rx data handler for reading magnetic sensor configuration |
466 | // rx data handler for reading magnetic sensor configuration |
468 | void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize) |
467 | void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize) |
469 | { // if number of byte are matching |
468 | { // if number of byte are matching |
470 | if(RxBufferSize == sizeof(MagConfig) ) |
469 | if(RxBufferSize == sizeof(MagConfig) ) |
471 | { |
470 | { |
472 | memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig)); |
471 | memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig)); |
473 | } |
472 | } |
474 | } |
473 | } |
475 | // rx data handler for reading acceleration sensor configuration |
474 | // rx data handler for reading acceleration sensor configuration |
476 | void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize) |
475 | void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize) |
477 | { // if number of byte are matching |
476 | { // if number of byte are matching |
478 | if(RxBufferSize == sizeof(AccConfig) ) |
477 | if(RxBufferSize == sizeof(AccConfig) ) |
479 | { |
478 | { |
480 | memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig)); |
479 | memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig)); |
481 | } |
480 | } |
482 | } |
481 | } |
483 | //---------------------------------------------------------------------- |
482 | //---------------------------------------------------------------------- |
484 | 483 | ||
485 | 484 | ||
486 | // --------------------------------------------------------------------- |
485 | // --------------------------------------------------------------------- |
487 | u8 NCMAG_SetMagConfig(void) |
486 | u8 NCMAG_SetMagConfig(void) |
488 | { |
487 | { |
489 | u8 retval = 0; |
488 | u8 retval = 0; |
490 | // try to catch the i2c buffer within 100 ms timeout |
489 | // try to catch the i2c buffer within 100 ms timeout |
491 | if(I2C_LockBuffer(100)) |
490 | if(I2C_LockBuffer(100)) |
492 | { |
491 | { |
493 | u8 TxBytes = 0; |
492 | u8 TxBytes = 0; |
494 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
493 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
495 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig)); |
494 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig)); |
496 | TxBytes += sizeof(MagConfig); |
495 | TxBytes += sizeof(MagConfig); |
497 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0)) |
496 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0)) |
498 | { |
497 | { |
499 | if(I2C_WaitForEndOfTransmission(100)) |
498 | if(I2C_WaitForEndOfTransmission(100)) |
500 | { |
499 | { |
501 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
500 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
502 | } |
501 | } |
503 | } |
502 | } |
504 | } |
503 | } |
505 | return(retval); |
504 | return(retval); |
506 | } |
505 | } |
507 | 506 | ||
508 | // ---------------------------------------------------------------------------------------- |
507 | // ---------------------------------------------------------------------------------------- |
509 | u8 NCMAG_GetMagConfig(void) |
508 | u8 NCMAG_GetMagConfig(void) |
510 | { |
509 | { |
511 | u8 retval = 0; |
510 | u8 retval = 0; |
512 | // try to catch the i2c buffer within 100 ms timeout |
511 | // try to catch the i2c buffer within 100 ms timeout |
513 | if(I2C_LockBuffer(100)) |
512 | if(I2C_LockBuffer(100)) |
514 | { |
513 | { |
515 | u8 TxBytes = 0; |
514 | u8 TxBytes = 0; |
516 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
515 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
517 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig))) |
516 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig))) |
518 | { |
517 | { |
519 | if(I2C_WaitForEndOfTransmission(100)) |
518 | if(I2C_WaitForEndOfTransmission(100)) |
520 | { |
519 | { |
521 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
520 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
522 | } |
521 | } |
523 | } |
522 | } |
524 | } |
523 | } |
525 | return(retval); |
524 | return(retval); |
526 | } |
525 | } |
527 | 526 | ||
528 | // ---------------------------------------------------------------------------------------- |
527 | // ---------------------------------------------------------------------------------------- |
529 | u8 NCMAG_SetAccConfig(void) |
528 | u8 NCMAG_SetAccConfig(void) |
530 | { |
529 | { |
531 | u8 retval = 0; |
530 | u8 retval = 0; |
532 | // try to catch the i2c buffer within 100 ms timeout |
531 | // try to catch the i2c buffer within 100 ms timeout |
533 | if(I2C_LockBuffer(100)) |
532 | if(I2C_LockBuffer(100)) |
534 | { |
533 | { |
535 | u8 TxBytes = 0; |
534 | u8 TxBytes = 0; |
536 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
535 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
537 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig)); |
536 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig)); |
538 | TxBytes += sizeof(AccConfig); |
537 | TxBytes += sizeof(AccConfig); |
539 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0)) |
538 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0)) |
540 | { |
539 | { |
541 | if(I2C_WaitForEndOfTransmission(100)) |
540 | if(I2C_WaitForEndOfTransmission(100)) |
542 | { |
541 | { |
543 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
542 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
544 | } |
543 | } |
545 | } |
544 | } |
546 | } |
545 | } |
547 | return(retval); |
546 | return(retval); |
548 | } |
547 | } |
549 | 548 | ||
550 | // ---------------------------------------------------------------------------------------- |
549 | // ---------------------------------------------------------------------------------------- |
551 | u8 NCMAG_GetAccConfig(void) |
550 | u8 NCMAG_GetAccConfig(void) |
552 | { |
551 | { |
553 | u8 retval = 0; |
552 | u8 retval = 0; |
554 | // try to catch the i2c buffer within 100 ms timeout |
553 | // try to catch the i2c buffer within 100 ms timeout |
555 | if(I2C_LockBuffer(100)) |
554 | if(I2C_LockBuffer(100)) |
556 | { |
555 | { |
557 | u8 TxBytes = 0; |
556 | u8 TxBytes = 0; |
558 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
557 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
559 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig))) |
558 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig))) |
560 | { |
559 | { |
561 | if(I2C_WaitForEndOfTransmission(100)) |
560 | if(I2C_WaitForEndOfTransmission(100)) |
562 | { |
561 | { |
563 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
562 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
564 | } |
563 | } |
565 | } |
564 | } |
566 | } |
565 | } |
567 | return(retval); |
566 | return(retval); |
568 | } |
567 | } |
569 | 568 | ||
570 | // ---------------------------------------------------------------------------------------- |
569 | // ---------------------------------------------------------------------------------------- |
571 | u8 NCMAG_GetIdentification(void) |
570 | u8 NCMAG_GetIdentification(void) |
572 | { |
571 | { |
573 | u8 retval = 0; |
572 | u8 retval = 0; |
574 | // try to catch the i2c buffer within 100 ms timeout |
573 | // try to catch the i2c buffer within 100 ms timeout |
575 | if(I2C_LockBuffer(100)) |
574 | if(I2C_LockBuffer(100)) |
576 | { |
575 | { |
577 | u16 TxBytes = 0; |
576 | u16 TxBytes = 0; |
578 | NCMAG_Identification.A = 0xFF; |
577 | NCMAG_Identification.A = 0xFF; |
579 | NCMAG_Identification.B = 0xFF; |
578 | NCMAG_Identification.B = 0xFF; |
580 | NCMAG_Identification.C = 0xFF; |
579 | NCMAG_Identification.C = 0xFF; |
581 | I2C_Buffer[TxBytes++] = REG_MAG_IDA; |
580 | I2C_Buffer[TxBytes++] = REG_MAG_IDA; |
582 | // initiate transmission |
581 | // initiate transmission |
583 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification))) |
582 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification))) |
584 | { |
583 | { |
585 | if(I2C_WaitForEndOfTransmission(100)) |
584 | if(I2C_WaitForEndOfTransmission(100)) |
586 | { |
585 | { |
587 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
586 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
588 | } |
587 | } |
589 | } |
588 | } |
590 | } |
589 | } |
591 | return(retval); |
590 | return(retval); |
592 | } |
591 | } |
593 | 592 | ||
594 | u8 NCMAG_GetIdentification_Sub(void) |
593 | u8 NCMAG_GetIdentification_Sub(void) |
595 | { |
594 | { |
596 | u8 retval = 0; |
595 | u8 retval = 0; |
597 | // try to catch the i2c buffer within 100 ms timeout |
596 | // try to catch the i2c buffer within 100 ms timeout |
598 | if(I2C_LockBuffer(100)) |
597 | if(I2C_LockBuffer(100)) |
599 | { |
598 | { |
600 | u16 TxBytes = 0; |
599 | u16 TxBytes = 0; |
601 | NCMAG_Identification2.Sub = 0xFF; |
600 | NCMAG_Identification2.Sub = 0xFF; |
602 | I2C_Buffer[TxBytes++] = REG_MAG_IDF; |
601 | I2C_Buffer[TxBytes++] = REG_MAG_IDF; |
603 | // initiate transmission |
602 | // initiate transmission |
604 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2))) |
603 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2))) |
605 | { |
604 | { |
606 | if(I2C_WaitForEndOfTransmission(100)) |
605 | if(I2C_WaitForEndOfTransmission(100)) |
607 | { |
606 | { |
608 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
607 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
609 | } |
608 | } |
610 | } |
609 | } |
611 | } |
610 | } |
612 | return(retval); |
611 | return(retval); |
613 | } |
612 | } |
614 | 613 | ||
615 | 614 | ||
616 | // ---------------------------------------------------------------------------------------- |
615 | // ---------------------------------------------------------------------------------------- |
617 | void NCMAG_GetMagVector(void) |
616 | void NCMAG_GetMagVector(void) |
618 | { |
617 | { |
619 | // try to catch the I2C buffer within 0 ms |
618 | // try to catch the I2C buffer within 0 ms |
620 | if(I2C_LockBuffer(0)) |
619 | if(I2C_LockBuffer(0)) |
621 | { |
620 | { |
622 | // s16 tmp; |
621 | // s16 tmp; |
623 | u16 TxBytes = 0; |
622 | u16 TxBytes = 0; |
624 | // set register pointer |
623 | // set register pointer |
625 | I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB; |
624 | I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB; |
626 | // initiate transmission |
625 | // initiate transmission |
627 | I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector)); |
626 | I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector)); |
628 | } |
627 | } |
629 | } |
628 | } |
630 | 629 | ||
631 | //---------------------------------------------------------------- |
630 | //---------------------------------------------------------------- |
632 | void NCMAG_GetAccVector(void) |
631 | void NCMAG_GetAccVector(void) |
633 | { |
632 | { |
634 | // try to catch the I2C buffer within 0 ms |
633 | // try to catch the I2C buffer within 0 ms |
635 | if(I2C_LockBuffer(0)) |
634 | if(I2C_LockBuffer(0)) |
636 | { |
635 | { |
637 | u16 TxBytes = 0; |
636 | u16 TxBytes = 0; |
638 | // set register pointer |
637 | // set register pointer |
639 | I2C_Buffer[TxBytes++] = REG_ACC_X_LSB; |
638 | I2C_Buffer[TxBytes++] = REG_ACC_X_LSB; |
640 | // initiate transmission |
639 | // initiate transmission |
641 | I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector)); |
640 | I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector)); |
642 | } |
641 | } |
643 | } |
642 | } |
644 | 643 | ||
645 | //---------------------------------------------------------------- |
644 | //---------------------------------------------------------------- |
646 | void InitNC_MagnetSensor(void) |
645 | void InitNC_MagnetSensor(void) |
647 | { |
646 | { |
648 | s16 xscale, yscale, zscale; |
647 | s16 xscale, yscale, zscale; |
649 | u8 crb_gain, cra_rate; |
648 | u8 crb_gain, cra_rate; |
650 | // u8 retval = 1; |
649 | // u8 retval = 1; |
651 | 650 | ||
652 | switch(NCMAG_MagType) |
651 | switch(NCMAG_MagType) |
653 | { |
652 | { |
654 | case MAG_TYPE_HMC5843: |
653 | case MAG_TYPE_HMC5843: |
655 | crb_gain = HMC5843_CRB_GAIN_15GA; |
654 | crb_gain = HMC5843_CRB_GAIN_15GA; |
656 | cra_rate = HMC5843_CRA_RATE_50HZ; |
655 | cra_rate = HMC5843_CRA_RATE_50HZ; |
657 | xscale = HMC5843_TEST_XSCALE; |
656 | xscale = HMC5843_TEST_XSCALE; |
658 | yscale = HMC5843_TEST_YSCALE; |
657 | yscale = HMC5843_TEST_YSCALE; |
659 | zscale = HMC5843_TEST_ZSCALE; |
658 | zscale = HMC5843_TEST_ZSCALE; |
660 | break; |
659 | break; |
661 | 660 | ||
662 | case MAG_TYPE_LSM303DLH: |
661 | case MAG_TYPE_LSM303DLH: |
663 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
662 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
664 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
663 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
665 | xscale = LSM303DLH_TEST_XSCALE; |
664 | xscale = LSM303DLH_TEST_XSCALE; |
666 | yscale = LSM303DLH_TEST_YSCALE; |
665 | yscale = LSM303DLH_TEST_YSCALE; |
667 | zscale = LSM303DLH_TEST_ZSCALE; |
666 | zscale = LSM303DLH_TEST_ZSCALE; |
668 | break; |
667 | break; |
669 | 668 | ||
670 | default: |
669 | default: |
671 | return; |
670 | return; |
672 | } |
671 | } |
673 | 672 | ||
674 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
673 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
675 | MagConfig.crb = crb_gain; |
674 | MagConfig.crb = crb_gain; |
676 | MagConfig.mode = MODE_CONTINUOUS; |
675 | MagConfig.mode = MODE_CONTINUOUS; |
677 | NCMAG_SetMagConfig(); |
676 | NCMAG_SetMagConfig(); |
678 | } |
677 | } |
679 | 678 | ||
680 | 679 | ||
681 | // -------------------------------------------------------- |
680 | // -------------------------------------------------------- |
682 | void NCMAG_Update(void) |
681 | void NCMAG_Update(void) |
683 | { |
682 | { |
684 | static u32 TimerUpdate = 0; |
683 | static u32 TimerUpdate = 0; |
685 | static u8 send_config = 0; |
684 | static u8 send_config = 0; |
686 | 685 | ||
687 | if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present ) |
686 | if( (I2C_State == I2C_STATE_OFF) || !NCMAG_Present ) |
688 | { |
687 | { |
689 | Compass_Heading = -1; |
688 | Compass_Heading = -1; |
690 | DebugOut.Analog[14]++; // count I2C error |
689 | DebugOut.Analog[14]++; // count I2C error |
691 | return; |
690 | return; |
692 | } |
691 | } |
693 | if(CheckDelay(TimerUpdate)) |
692 | if(CheckDelay(TimerUpdate)) |
694 | { |
693 | { |
695 | if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid |
694 | if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid |
696 | if(++send_config == 25) // 500ms |
695 | if(++send_config == 25) // 500ms |
697 | { |
696 | { |
698 | send_config = 0; |
697 | send_config = 0; |
699 | InitNC_MagnetSensor(); |
698 | InitNC_MagnetSensor(); |
700 | TimerUpdate = SetDelay(15); // back into the old time-slot |
699 | TimerUpdate = SetDelay(15); // back into the old time-slot |
701 | } |
700 | } |
702 | else |
701 | else |
703 | { |
702 | { |
704 | // check for new calibration state |
703 | // check for new calibration state |
705 | Compass_UpdateCalState(); |
704 | Compass_UpdateCalState(); |
706 | if(Compass_CalState) NCMAG_Calibrate(); |
705 | if(Compass_CalState) NCMAG_Calibrate(); |
707 | NCMAG_GetMagVector(); //Get new data; |
706 | NCMAG_GetMagVector(); //Get new data; |
708 | if(send_config == 24) TimerUpdate = SetDelay(5); // next event is the re-configuration |
707 | if(send_config == 24) TimerUpdate = SetDelay(5); // next event is the re-configuration |
709 | else TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
708 | else TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
710 | } |
709 | } |
711 | } |
710 | } |
712 | } |
711 | } |
713 | 712 | ||
714 | 713 | ||
715 | // -------------------------------------------------------- |
714 | // -------------------------------------------------------- |
716 | u8 NCMAG_SelfTest(void) |
715 | u8 NCMAG_SelfTest(void) |
717 | { |
716 | { |
718 | u8 msg[64]; |
717 | u8 msg[64]; |
719 | static u8 done = 0; |
718 | static u8 done = 0; |
720 | 719 | ||
721 | if(done) return(1); // just make it once |
720 | if(done) return(1); // just make it once |
722 | 721 | ||
723 | #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;} |
722 | #define LIMITS(value, min, max) {min = (80 * value)/100; max = (120 * value)/100;} |
724 | u32 time; |
723 | u32 time; |
725 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
724 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
726 | s16 xscale, yscale, zscale, scale_min, scale_max; |
725 | s16 xscale, yscale, zscale, scale_min, scale_max; |
727 | u8 crb_gain, cra_rate; |
726 | u8 crb_gain, cra_rate; |
728 | u8 i = 0, retval = 1; |
727 | u8 i = 0, retval = 1; |
729 | 728 | ||
730 | switch(NCMAG_MagType) |
729 | switch(NCMAG_MagType) |
731 | { |
730 | { |
732 | case MAG_TYPE_HMC5843: |
731 | case MAG_TYPE_HMC5843: |
733 | crb_gain = HMC5843_CRB_GAIN_15GA; |
732 | crb_gain = HMC5843_CRB_GAIN_15GA; |
734 | cra_rate = HMC5843_CRA_RATE_50HZ; |
733 | cra_rate = HMC5843_CRA_RATE_50HZ; |
735 | xscale = HMC5843_TEST_XSCALE; |
734 | xscale = HMC5843_TEST_XSCALE; |
736 | yscale = HMC5843_TEST_YSCALE; |
735 | yscale = HMC5843_TEST_YSCALE; |
737 | zscale = HMC5843_TEST_ZSCALE; |
736 | zscale = HMC5843_TEST_ZSCALE; |
738 | break; |
737 | break; |
739 | 738 | ||
740 | case MAG_TYPE_LSM303DLH: |
739 | case MAG_TYPE_LSM303DLH: |
741 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
740 | crb_gain = LSM303DLH_CRB_GAIN_19GA; |
742 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
741 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
743 | xscale = LSM303DLH_TEST_XSCALE; |
742 | xscale = LSM303DLH_TEST_XSCALE; |
744 | yscale = LSM303DLH_TEST_YSCALE; |
743 | yscale = LSM303DLH_TEST_YSCALE; |
745 | zscale = LSM303DLH_TEST_ZSCALE; |
744 | zscale = LSM303DLH_TEST_ZSCALE; |
746 | break; |
745 | break; |
747 | 746 | ||
748 | default: |
747 | default: |
749 | return(0); |
748 | return(0); |
750 | } |
749 | } |
751 | 750 | ||
752 | MagConfig.cra = cra_rate|CRA_MODE_POSBIAS; |
751 | MagConfig.cra = cra_rate|CRA_MODE_POSBIAS; |
753 | MagConfig.crb = crb_gain; |
752 | MagConfig.crb = crb_gain; |
754 | MagConfig.mode = MODE_CONTINUOUS; |
753 | MagConfig.mode = MODE_CONTINUOUS; |
755 | // activate positive bias field |
754 | // activate positive bias field |
756 | NCMAG_SetMagConfig(); |
755 | NCMAG_SetMagConfig(); |
757 | // wait for stable readings |
756 | // wait for stable readings |
758 | time = SetDelay(50); |
757 | time = SetDelay(50); |
759 | while(!CheckDelay(time)); |
758 | while(!CheckDelay(time)); |
760 | // averaging |
759 | // averaging |
761 | #define AVERAGE 20 |
760 | #define AVERAGE 20 |
762 | for(i = 0; i<AVERAGE; i++) |
761 | for(i = 0; i<AVERAGE; i++) |
763 | { |
762 | { |
764 | NCMAG_GetMagVector(); |
763 | NCMAG_GetMagVector(); |
765 | time = SetDelay(20); |
764 | time = SetDelay(20); |
766 | while(!CheckDelay(time)); |
765 | while(!CheckDelay(time)); |
767 | XMax += MagRawVector.X; |
766 | XMax += MagRawVector.X; |
768 | YMax += MagRawVector.Y; |
767 | YMax += MagRawVector.Y; |
769 | ZMax += MagRawVector.Z; |
768 | ZMax += MagRawVector.Z; |
770 | } |
769 | } |
771 | MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS; |
770 | MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS; |
772 | // activate positive bias field |
771 | // activate positive bias field |
773 | NCMAG_SetMagConfig(); |
772 | NCMAG_SetMagConfig(); |
774 | // wait for stable readings |
773 | // wait for stable readings |
775 | time = SetDelay(50); |
774 | time = SetDelay(50); |
776 | while(!CheckDelay(time)); |
775 | while(!CheckDelay(time)); |
777 | // averaging |
776 | // averaging |
778 | for(i = 0; i < AVERAGE; i++) |
777 | for(i = 0; i < AVERAGE; i++) |
779 | { |
778 | { |
780 | NCMAG_GetMagVector(); |
779 | NCMAG_GetMagVector(); |
781 | time = SetDelay(20); |
780 | time = SetDelay(20); |
782 | while(!CheckDelay(time)); |
781 | while(!CheckDelay(time)); |
783 | XMin += MagRawVector.X; |
782 | XMin += MagRawVector.X; |
784 | YMin += MagRawVector.Y; |
783 | YMin += MagRawVector.Y; |
785 | ZMin += MagRawVector.Z; |
784 | ZMin += MagRawVector.Z; |
786 | } |
785 | } |
787 | // setup final configuration |
786 | // setup final configuration |
788 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
787 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
789 | // activate positive bias field |
788 | // activate positive bias field |
790 | NCMAG_SetMagConfig(); |
789 | NCMAG_SetMagConfig(); |
791 | // check scale for all axes |
790 | // check scale for all axes |
792 | // prepare scale limits |
791 | // prepare scale limits |
793 | LIMITS(xscale, scale_min, scale_max); |
792 | LIMITS(xscale, scale_min, scale_max); |
794 | xscale = (XMax - XMin)/(2*AVERAGE); |
793 | xscale = (XMax - XMin)/(2*AVERAGE); |
795 | if((xscale > scale_max) || (xscale < scale_min)) |
794 | if((xscale > scale_max) || (xscale < scale_min)) |
796 | { |
795 | { |
797 | retval = 0; |
796 | retval = 0; |
798 | sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max); |
797 | sprintf(msg, "\r\n Value X: %d not %d-%d !", xscale, scale_min,scale_max); |
799 | UART1_PutString(msg); |
798 | UART1_PutString(msg); |
800 | } |
799 | } |
801 | LIMITS(yscale, scale_min, scale_max); |
800 | LIMITS(yscale, scale_min, scale_max); |
802 | yscale = (YMax - YMin)/(2*AVERAGE); |
801 | yscale = (YMax - YMin)/(2*AVERAGE); |
803 | if((yscale > scale_max) || (yscale < scale_min)) |
802 | if((yscale > scale_max) || (yscale < scale_min)) |
804 | { |
803 | { |
805 | retval = 0; |
804 | retval = 0; |
806 | sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max); |
805 | sprintf(msg, "\r\n Value Y: %d not %d-%d !", yscale, scale_min,scale_max); |
807 | UART1_PutString(msg); |
806 | UART1_PutString(msg); |
808 | } |
807 | } |
809 | LIMITS(zscale, scale_min, scale_max); |
808 | LIMITS(zscale, scale_min, scale_max); |
810 | zscale = (ZMax - ZMin)/(2*AVERAGE); |
809 | zscale = (ZMax - ZMin)/(2*AVERAGE); |
811 | if((zscale > scale_max) || (zscale < scale_min)) |
810 | if((zscale > scale_max) || (zscale < scale_min)) |
812 | { |
811 | { |
813 | retval = 0; |
812 | retval = 0; |
814 | sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max); |
813 | sprintf(msg, "\r\n Value Z: %d not %d-%d !", zscale, scale_min,scale_max); |
815 | UART1_PutString(msg); |
814 | UART1_PutString(msg); |
816 | } |
815 | } |
817 | done = retval; |
816 | done = retval; |
818 | return(retval); |
817 | return(retval); |
819 | } |
818 | } |
820 | 819 | ||
821 | 820 | ||
822 | //---------------------------------------------------------------- |
821 | //---------------------------------------------------------------- |
823 | u8 NCMAG_Init(void) |
822 | u8 NCMAG_Init(void) |
824 | { |
823 | { |
825 | u8 msg[64]; |
824 | u8 msg[64]; |
826 | u8 retval = 0; |
825 | u8 retval = 0; |
827 | u8 repeat; |
826 | u8 repeat; |
828 | 827 | ||
829 | NCMAG_Present = 0; |
828 | NCMAG_Present = 0; |
830 | NCMAG_MagType = MAG_TYPE_HMC5843; // assuming having an HMC5843 |
829 | NCMAG_MagType = MAG_TYPE_HMC5843; // assuming having an HMC5843 |
831 | // polling for LSM302DLH option |
830 | // polling for LSM302DLH option |
832 | repeat = 0; |
831 | repeat = 0; |
833 | do |
832 | do |
834 | { |
833 | { |
835 | retval = NCMAG_GetAccConfig(); |
834 | retval = NCMAG_GetAccConfig(); |
836 | if(retval) break; // break loop on success |
835 | if(retval) break; // break loop on success |
837 | UART1_PutString("."); |
836 | UART1_PutString("."); |
838 | repeat++; |
837 | repeat++; |
839 | }while(repeat < 3); |
838 | }while(repeat < 3); |
840 | if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH |
839 | if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH |
841 | // polling of identification |
840 | // polling of identification |
842 | repeat = 0; |
841 | repeat = 0; |
843 | do |
842 | do |
844 | { |
843 | { |
845 | retval = NCMAG_GetIdentification_Sub(); |
844 | retval = NCMAG_GetIdentification_Sub(); |
846 | if(retval) break; // break loop on success |
845 | if(retval) break; // break loop on success |
847 | UART1_PutString("."); |
846 | UART1_PutString("."); |
848 | repeat++; |
847 | repeat++; |
849 | }while(repeat < 12); |
848 | }while(repeat < 12); |
850 | retval = 0; |
849 | retval = 0; |
851 | do |
850 | do |
852 | { |
851 | { |
853 | retval = NCMAG_GetIdentification(); |
852 | retval = NCMAG_GetIdentification(); |
854 | if(retval) break; // break loop on success |
853 | if(retval) break; // break loop on success |
855 | UART1_PutString("."); |
854 | UART1_PutString("."); |
856 | repeat++; |
855 | repeat++; |
857 | }while(repeat < 12); |
856 | }while(repeat < 12); |
858 | 857 | ||
859 | // if we got an answer to id request |
858 | // if we got an answer to id request |
860 | if(retval) |
859 | if(retval) |
861 | { |
860 | { |
862 | u8 n1[] = "\n\r HMC5843"; |
861 | u8 n1[] = "\n\r HMC5843"; |
863 | u8 n2[] = "\n\r LSM303DLH"; |
862 | u8 n2[] = "\n\r LSM303DLH"; |
864 | u8 n3[] = "\n\r LSM303DLM"; |
863 | u8 n3[] = "\n\r LSM303DLM"; |
865 | u8* pn; |
864 | u8* pn; |
866 | 865 | ||
867 | pn = n1; |
866 | pn = n1; |
868 | if(NCMAG_MagType == MAG_TYPE_LSM303DLH) |
867 | if(NCMAG_MagType == MAG_TYPE_LSM303DLH) |
869 | { |
868 | { |
870 | if(NCMAG_Identification2.Sub == 0x3c) pn = n3; |
869 | if(NCMAG_Identification2.Sub == 0x3c) pn = n3; |
871 | else pn = n2; |
870 | else pn = n2; |
872 | } |
871 | } |
873 | 872 | ||
874 | sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub); |
873 | sprintf(msg, " %s ID 0x%02x/%02x/%02x-%02x", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C,NCMAG_Identification2.Sub); |
875 | UART1_PutString(msg); |
874 | UART1_PutString(msg); |
876 | if ( (NCMAG_Identification.A == MAG_IDA) |
875 | if ( (NCMAG_Identification.A == MAG_IDA) |
877 | && (NCMAG_Identification.B == MAG_IDB) |
876 | && (NCMAG_Identification.B == MAG_IDB) |
878 | && (NCMAG_Identification.C == MAG_IDC)) |
877 | && (NCMAG_Identification.C == MAG_IDC)) |
879 | { |
878 | { |
880 | NCMAG_Present = 1; |
879 | NCMAG_Present = 1; |
881 | 880 | ||
882 | if(EEPROM_Init()) |
881 | if(EEPROM_Init()) |
883 | { |
882 | { |
884 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
883 | NCMAG_IsCalibrated = NCMag_CalibrationRead(); |
885 | if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!"); |
884 | if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!"); |
886 | } |
885 | } |
887 | else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!"); |
886 | else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!"); |
888 | 887 | ||
889 | if(NCMAG_Identification2.Sub == 0x00) |
888 | if(NCMAG_Identification2.Sub == 0x00) |
890 | { |
889 | { |
891 | if(!NCMAG_SelfTest()) |
890 | if(!NCMAG_SelfTest()) |
892 | { |
891 | { |
893 | UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n"); |
892 | UART1_PutString("\r\n Selftest failed!!!!!!!!!!!!!!!!!!!!\r\n"); |
894 | LED_RED_ON; |
893 | LED_RED_ON; |
895 | NCMAG_IsCalibrated = 0; |
894 | NCMAG_IsCalibrated = 0; |
896 | } else UART1_PutString("\r\n Selftest ok"); |
895 | } else UART1_PutString("\r\n Selftest ok"); |
897 | } |
896 | } |
898 | else InitNC_MagnetSensor(); |
897 | else InitNC_MagnetSensor(); |
899 | } |
898 | } |
900 | else |
899 | else |
901 | { |
900 | { |
902 | UART1_PutString("\n\r Not compatible!"); |
901 | UART1_PutString("\n\r Not compatible!"); |
903 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
902 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
904 | LED_RED_ON; |
903 | LED_RED_ON; |
905 | } |
904 | } |
906 | } |
905 | } |
907 | else // nothing found |
906 | else // nothing found |
908 | { |
907 | { |
909 | NCMAG_MagType = MAG_TYPE_NONE; |
908 | NCMAG_MagType = MAG_TYPE_NONE; |
910 | UART1_PutString("not found!"); |
909 | UART1_PutString("not found!"); |
911 | } |
910 | } |
912 | return(NCMAG_Present); |
911 | return(NCMAG_Present); |
913 | } |
912 | } |
914 | 913 | ||
915 | 914 |