Rev 252 | Rev 254 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 252 | Rev 253 | ||
---|---|---|---|
Line 54... | Line 54... | ||
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
54 | // + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
55 | // + POSSIBILITY OF SUCH DAMAGE. |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
57 | #include <string.h> |
57 | #include <string.h> |
58 | #include "91x_lib.h" |
58 | #include "91x_lib.h" |
59 | #include "hmc5843.h" |
59 | #include "ncmag.h" |
60 | #include "i2c.h" |
60 | #include "i2c.h" |
61 | #include "timer1.h" |
61 | #include "timer1.h" |
62 | #include "led.h" |
62 | #include "led.h" |
63 | #include "spi_slave.h" |
63 | #include "spi_slave.h" |
64 | #include "uart1.h" |
64 | #include "uart1.h" |
65 | #include "compass.h" |
- | |
Line 66... | Line 65... | ||
66 | 65 | ||
Line -... | Line 66... | ||
- | 66 | u8 NCMAG_Present = 0; |
|
- | 67 | ||
- | 68 | #define MAG_TYPE_NONE 0 |
|
67 | #define HMC5843_SLAVE_ADDRESS 0x3C // i2C slave address |
69 | #define MAG_TYPE_HMC5843 1 |
- | 70 | #define MAG_TYPE_LSM303DLH 2 |
|
- | 71 | u8 NCMAG_MagType = MAG_TYPE_NONE; |
|
- | 72 | ||
- | 73 | // i2c MAG interface |
|
- | 74 | #define MAG_SLAVE_ADDRESS 0x3C // i2C slave address mag. sensor registers |
|
- | 75 | ||
- | 76 | // register mapping |
|
- | 77 | #define REG_MAG_CRA 0x00 |
|
- | 78 | #define REG_MAG_CRB 0x01 |
|
- | 79 | #define REG_MAG_MODE 0x02 |
|
- | 80 | #define REG_MAG_DATAX_MSB 0x03 |
|
- | 81 | #define REG_MAG_DATAX_LSB 0x04 |
|
- | 82 | #define REG_MAG_DATAY_MSB 0x05 |
|
- | 83 | #define REG_MAG_DATAY_LSB 0x06 |
|
- | 84 | #define REG_MAG_DATAZ_MSB 0x07 |
|
- | 85 | #define REG_MAG_DATAZ_LSB 0x08 |
|
- | 86 | #define REG_MAG_STATUS 0x09 |
|
- | 87 | #define REG_MAG_IDA 0x0A |
|
- | 88 | #define REG_MAG_IDB 0x0B |
|
- | 89 | #define REG_MAG_IDC 0x0C |
|
- | 90 | ||
- | 91 | // bit mask for configuration mode |
|
- | 92 | #define CRA_MODE_MASK 0x03 |
|
- | 93 | #define CRA_MODE_NORMAL 0x00 //default |
|
- | 94 | #define CRA_MODE_POSBIAS 0x01 |
|
Line 68... | Line -... | ||
68 | - | ||
69 | u8 HMC5843_Present = 0; |
- | |
70 | - | ||
71 | #define REGISTER_CRA 0 |
95 | #define CRA_MODE_NEGBIAS 0x02 |
72 | #define REGISTER_CRB 1 |
96 | #define CRA_MODE_SELFTEST 0x03 |
73 | #define REGISTER_MODE 2 |
97 | |
74 | #define REGISTER_DATAX_MSB 3 |
98 | // bit mask for measurement mode |
75 | #define REGISTER_DATAX_LSB 4 |
99 | #define MODE_MASK 0xFF |
76 | #define REGISTER_DATAY_MSB 5 |
100 | #define MODE_CONTINUOUS 0x00 |
- | 101 | #define MODE_SINGLE 0x01 // default |
|
77 | #define REGISTER_DATAY_LSB 6 |
102 | #define MODE_IDLE 0x02 |
78 | #define REGISTER_DATAZ_MSB 7 |
103 | #define MODE_SLEEP 0x03 |
- | 104 | ||
79 | #define REGISTER_DATAZ_LSB 8 |
105 | // bit mask for rate |
80 | #define REGISTER_STATUS 9 |
106 | #define CRA_RATE_MASK 0x1C |
81 | #define REGISTER_IDA 10 |
107 | |
82 | #define REGISTER_IDB 11 |
108 | // bit mask for gain |
83 | #define REGISTER_IDC 12 |
109 | #define CRB_GAIN_MASK 0xE0 |
84 | 110 | ||
85 | 111 | // ids |
|
Line -... | Line 112... | ||
- | 112 | #define MAG_IDA 0x48 |
|
86 | #define HMC5843_IDA 0x48 |
113 | #define MAG_IDB 0x34 |
87 | #define HMC5843_IDB 0x34 |
- | |
88 | #define HMC5843_IDC 0x33 |
114 | #define MAG_IDC 0x33 |
89 | 115 | ||
90 | // bit mask for rate |
116 | // the special HMC5843 interface |
91 | #define HMC5843_CRA_RATE_MASK 0x1C |
117 | // bit mask for rate |
92 | #define HMC5843_CRA_RATE_0_5HZ 0x00 |
118 | #define HMC5843_CRA_RATE_0_5HZ 0x00 |
93 | #define HMC5843_CRA_RATE_1HZ 0x04 |
119 | #define HMC5843_CRA_RATE_1HZ 0x04 |
94 | #define HMC5843_CRA_RATE_2HZ 0x08 |
120 | #define HMC5843_CRA_RATE_2HZ 0x08 |
95 | #define HMC5843_CRA_RATE_5HZ 0x0C |
- | |
96 | #define HMC5843_CRA_RATE_10HZ 0x10 //default |
- | |
97 | #define HMC5843_CRA_RATE_20HZ 0x14 |
- | |
98 | #define HMC5843_CRA_RATE_50HZ 0x18 |
- | |
99 | - | ||
100 | // bit mask for mode |
- | |
101 | #define HMC5843_CRA_MODE_MASK 0x03 |
- | |
102 | #define HMC5843_CRA_MODE_NORMAL 0x00 //default |
- | |
103 | #define HMC5843_CRA_MODE_POSBIAS 0x01 |
121 | #define HMC5843_CRA_RATE_5HZ 0x0C |
104 | #define HMC5843_CRA_MODE_NEGBIAS 0x02 |
- | |
105 | #define HMC5843_CRA_MODE_SELFTEST 0x03 |
122 | #define HMC5843_CRA_RATE_10HZ 0x10 //default |
106 | 123 | #define HMC5843_CRA_RATE_20HZ 0x14 |
|
107 | // bit mask for gain |
124 | #define HMC5843_CRA_RATE_50HZ 0x18 |
108 | #define HMC5843_CRB_GAIN_MASK 0xE0 |
125 | // bit mask for gain |
109 | #define HMC5843_CRB_GAIN_07GA 0x00 |
126 | #define HMC5843_CRB_GAIN_07GA 0x00 |
110 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
127 | #define HMC5843_CRB_GAIN_10GA 0x20 //default |
111 | #define HMC5843_CRB_GAIN_15GA 0x40 |
128 | #define HMC5843_CRB_GAIN_15GA 0x40 |
112 | #define HMC5843_CRB_GAIN_20GA 0x60 |
129 | #define HMC5843_CRB_GAIN_20GA 0x60 |
- | 130 | #define HMC5843_CRB_GAIN_32GA 0x80 |
|
- | 131 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
|
- | 132 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
|
- | 133 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
|
- | 134 | // self test value |
|
- | 135 | #define HMC5843_TEST_XSCALE 715 |
|
- | 136 | #define HMC5843_TEST_YSCALE 715 |
|
- | 137 | #define HMC5843_TEST_ZSCALE 715 |
|
- | 138 | ||
- | 139 | ||
- | 140 | // the special LSM302DLH interface |
|
- | 141 | // bit mask for rate |
|
- | 142 | #define LSM303DLH_CRA_RATE_0_75HZ 0x00 |
|
- | 143 | #define LSM303DLH_CRA_RATE_1_5HZ 0x04 |
|
- | 144 | #define LSM303DLH_CRA_RATE_3_0HZ 0x08 |
|
- | 145 | #define LSM303DLH_CRA_RATE_7_5HZ 0x0C |
|
- | 146 | #define LSM303DLH_CRA_RATE_15HZ 0x10 //default |
|
- | 147 | #define LSM303DLH_CRA_RATE_30HZ 0x14 |
|
- | 148 | #define LSM303DLH_CRA_RATE_75HZ 0x18 |
|
- | 149 | // bit mask for gain |
|
- | 150 | #define LSM303DLH_CRB_GAIN_XXGA 0x00 |
|
- | 151 | #define LSM303DLH_CRB_GAIN_13GA 0x20 //default |
|
- | 152 | #define LSM303DLH_CRB_GAIN_19GA 0x40 |
|
- | 153 | #define LSM303DLH_CRB_GAIN_25GA 0x60 |
|
- | 154 | #define LSM303DLH_CRB_GAIN_40GA 0x80 |
|
- | 155 | #define LSM303DLH_CRB_GAIN_47GA 0xA0 |
|
- | 156 | #define LSM303DLH_CRB_GAIN_56GA 0xC0 |
|
- | 157 | #define LSM303DLH_CRB_GAIN_81GA 0xE0 |
|
- | 158 | // self test value |
|
- | 159 | #define LSM303DLH_TEST_XSCALE 655 |
|
- | 160 | #define LSM303DLH_TEST_YSCALE 655 |
|
- | 161 | #define LSM303DLH_TEST_ZSCALE 630 |
|
- | 162 | ||
- | 163 | // the i2c ACC interface |
|
- | 164 | #define ACC_SLAVE_ADDRESS 0x30 // i2c slave for acc. sensor registers |
|
- | 165 | // register mapping |
|
- | 166 | #define REG_ACC_CTRL1 0x20 |
|
- | 167 | #define REG_ACC_CTRL2 0x21 |
|
- | 168 | #define REG_ACC_CTRL3 0x22 |
|
- | 169 | #define REG_ACC_CTRL4 0x23 |
|
- | 170 | #define REG_ACC_CTRL5 0x24 |
|
- | 171 | #define REG_ACC_HP_FILTER_RESET 0x25 |
|
- | 172 | #define REG_ACC_REFERENCE 0x26 |
|
- | 173 | #define REG_ACC_STATUS 0x27 |
|
- | 174 | #define REG_ACC_X_LSB 0x28 |
|
- | 175 | #define REG_ACC_X_MSB 0x29 |
|
- | 176 | #define REG_ACC_Y_LSB 0x2A |
|
Line 113... | Line -... | ||
113 | #define HMC5843_CRB_GAIN_32GA 0x80 |
- | |
114 | #define HMC5843_CRB_GAIN_38GA 0xA0 |
- | |
115 | #define HMC5843_CRB_GAIN_45GA 0xC0 |
- | |
116 | #define HMC5843_CRB_GAIN_65GA 0xE0 |
- | |
117 | - | ||
118 | // bit mask for measurement mode |
- | |
Line 119... | Line 177... | ||
119 | #define HMC5843_MODE_MASK 0xFF |
177 | #define REG_ACC_Y_MSB 0x2B |
120 | #define HMC5843_MODE_CONTINUOUS 0x00 |
178 | #define REG_ACC_Z_LSB 0x2C |
121 | #define HMC5843_MODE_SINGLE 0x01 // default |
179 | #define REG_ACC_Z_MSB 0x2D |
122 | #define HMC5843_MODE_IDLE 0x02 |
180 | |
123 | #define HMC5843_MODE_SLEEP 0x03 |
181 | |
124 | 182 | ||
Line 125... | Line 183... | ||
125 | typedef struct |
183 | typedef struct |
Line -... | Line 184... | ||
- | 184 | { |
|
- | 185 | u8 A; |
|
- | 186 | u8 B; |
|
- | 187 | u8 C; |
|
- | 188 | } __attribute__((packed)) Identification_t; |
|
- | 189 | ||
- | 190 | volatile Identification_t NCMAG_Identification; |
|
- | 191 | ||
- | 192 | typedef struct |
|
- | 193 | { |
|
- | 194 | u8 cra; |
|
- | 195 | u8 crb; |
|
- | 196 | u8 mode; |
|
- | 197 | } __attribute__((packed)) MagConfig_t; |
|
- | 198 | ||
- | 199 | volatile MagConfig_t MagConfig; |
|
- | 200 | ||
- | 201 | typedef struct |
|
- | 202 | { |
|
- | 203 | u8 ctrl_1; |
|
- | 204 | u8 ctrl_2; |
|
Line 126... | Line 205... | ||
126 | { |
205 | u8 ctrl_3; |
Line 127... | Line 206... | ||
127 | u8 A; |
206 | u8 ctrl_4; |
128 | u8 B; |
207 | u8 ctrl_5; |
129 | u8 C; |
208 | } __attribute__((packed)) AccConfig_t; |
130 | } __attribute__((packed)) HMC5843_Identification_t; |
209 | |
131 | 210 | volatile AccConfig_t AccConfig; |
|
132 | volatile HMC5843_Identification_t HMC5843_Identification; |
211 | |
133 | 212 | volatile s16vec_t AccVector; |
|
134 | 213 | ||
Line 135... | Line 214... | ||
135 | 214 | ||
136 | // ---------- call back handlers ----------------------------------------- |
215 | // ---------- call back handlers ----------------------------------------- |
137 | 216 | ||
138 | // rx data handler for id info request |
217 | // rx data handler for id info request |
139 | void HMC5843_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize) |
218 | void NCMAG_UpdateIdentification(u8* pRxBuffer, u8 RxBufferSize) |
140 | { // if number of byte are matching |
219 | { // if number of byte are matching |
141 | if(RxBufferSize == sizeof(HMC5843_Identification) ) |
220 | if(RxBufferSize == sizeof(NCMAG_Identification) ) |
142 | { |
221 | { |
143 | memcpy((u8 *)&HMC5843_Identification, pRxBuffer, sizeof(HMC5843_Identification)); |
222 | memcpy((u8 *)&NCMAG_Identification, pRxBuffer, sizeof(NCMAG_Identification)); |
144 | } |
223 | } |
145 | } |
224 | } |
146 | 225 | ||
147 | // rx data handler for mag vector request |
226 | // rx data handler for mag vector request |
148 | void HMC5843_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
227 | void NCMAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
- | 228 | { // if number of byte are matching |
|
- | 229 | if(RxBufferSize == sizeof(MagVector) ) |
|
- | 230 | { // byte order from big to little endian |
|
- | 231 | MagVector.X = pRxBuffer[0]<<8; |
|
- | 232 | MagVector.X+= pRxBuffer[1]; |
|
- | 233 | MagVector.Y = pRxBuffer[2]<<8; |
|
- | 234 | MagVector.Y+= pRxBuffer[3]; |
|
- | 235 | MagVector.Z = pRxBuffer[4]<<8; |
|
- | 236 | MagVector.Z+= pRxBuffer[5]; |
|
- | 237 | } |
|
- | 238 | // tbd. calculate heading from mag vector and attitude |
|
- | 239 | CompassHeading = 50; // tbd. |
|
- | 240 | } |
|
- | 241 | ||
- | 242 | // rx data handler for acc vector request |
|
- | 243 | void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize) |
|
- | 244 | { // if number of byte are matching |
|
149 | { // if number of byte are matching |
245 | if(RxBufferSize == sizeof(AccVector) ) |
Line -... | Line 246... | ||
- | 246 | { |
|
- | 247 | memcpy((u8*)&AccVector, pRxBuffer,sizeof(AccVector)); |
|
- | 248 | } |
|
- | 249 | } |
|
- | 250 | ||
- | 251 | void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize) |
|
- | 252 | { // if number of byte are matching |
|
Line 150... | Line 253... | ||
150 | if(RxBufferSize == sizeof(MagVector) ) |
253 | if(RxBufferSize == sizeof(MagConfig) ) |
- | 254 | { |
|
- | 255 | memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig)); |
|
- | 256 | } |
|
- | 257 | } |
|
- | 258 | ||
- | 259 | void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize) |
|
- | 260 | { // if number of byte are matching |
|
- | 261 | if(RxBufferSize == sizeof(AccConfig) ) |
|
- | 262 | { |
|
- | 263 | memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig)); |
|
- | 264 | } |
|
- | 265 | } |
|
- | 266 | ||
- | 267 | // ---------------------------------------------------------------------------------------- |
|
- | 268 | u8 NCMAG_SetMagConfig(void) |
|
- | 269 | { |
|
- | 270 | u8 retval = 0; |
|
- | 271 | // try to catch the i2c buffer within 100 ms timeout |
|
- | 272 | if(I2C_LockBuffer(100)) |
|
- | 273 | { |
|
Line -... | Line 274... | ||
- | 274 | u8 TxBytes = 0; |
|
151 | { // byte order from big to little endian |
275 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
152 | MagVector.X = pRxBuffer[0]<<8; |
276 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig)); |
153 | MagVector.X+= pRxBuffer[1]; |
277 | TxBytes += sizeof(MagConfig); |
154 | MagVector.Y = pRxBuffer[2]<<8; |
278 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, 0, 0)) |
155 | MagVector.Y+= pRxBuffer[3]; |
279 | { |
156 | MagVector.Z = pRxBuffer[4]<<8; |
280 | if(I2C_WaitForEndOfTransmission(100)) |
157 | MagVector.Z+= pRxBuffer[5]; |
281 | { |
158 | } |
- | |
159 | // tbd. calculate heading from mag vector and attitude |
- | |
160 | CompassHeading = 50; // tbd. |
- | |
161 | } |
282 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
162 | - | ||
163 | 283 | } |
|
164 | // ---------------------------------------------------------------------------------------- |
284 | } |
165 | 285 | } |
|
166 | u8 HMC5843_GetIdentification(void) |
286 | return(retval); |
167 | { |
287 | } |
168 | u8 retval = 0; |
288 | |
169 | // try to catch the i2c buffer within 100 ms timeout |
289 | // ---------------------------------------------------------------------------------------- |
170 | if(I2C_LockBuffer(100)) |
290 | u8 NCMAG_GetMagConfig(void) |
171 | { |
291 | { |
- | 292 | u8 retval = 0; |
|
- | 293 | // try to catch the i2c buffer within 100 ms timeout |
|
- | 294 | if(I2C_LockBuffer(100)) |
|
- | 295 | { |
|
- | 296 | u8 TxBytes = 0; |
|
- | 297 | I2C_Buffer[TxBytes++] = REG_MAG_CRA; |
|
- | 298 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig))) |
|
- | 299 | { |
|
- | 300 | if(I2C_WaitForEndOfTransmission(100)) |
|
- | 301 | { |
|
- | 302 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
|
- | 303 | } |
|
- | 304 | } |
|
- | 305 | } |
|
- | 306 | return(retval); |
|
- | 307 | } |
|
- | 308 | ||
- | 309 | // ---------------------------------------------------------------------------------------- |
|
- | 310 | u8 NCMAG_SetAccConfig(void) |
|
- | 311 | { |
|
- | 312 | u8 retval = 0; |
|
- | 313 | // try to catch the i2c buffer within 100 ms timeout |
|
172 | u16 TxBytes = 0; |
314 | if(I2C_LockBuffer(100)) |
Line 173... | Line 315... | ||
173 | HMC5843_Identification.A = 0xFF; |
315 | { |
174 | HMC5843_Identification.B = 0xFF; |
316 | u8 TxBytes = 0; |
175 | HMC5843_Identification.C = 0xFF; |
317 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
176 | I2C_Buffer[TxBytes++] = REGISTER_IDA; |
318 | memcpy((u8*)(&I2C_Buffer[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig)); |
177 | // initiate transmission |
319 | TxBytes += sizeof(AccConfig); |
178 | if(I2C_Transmission(HMC5843_SLAVE_ADDRESS, TxBytes, &HMC5843_UpdateIdentification, sizeof(HMC5843_Identification))) |
320 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, 0, 0)) |
179 | { |
321 | { |
180 | if(I2C_WaitForEndOfTransmission(100)) |
322 | if(I2C_WaitForEndOfTransmission(100)) |
181 | { |
323 | { |
- | 324 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
|
- | 325 | } |
|
- | 326 | } |
|
- | 327 | } |
|
- | 328 | return(retval); |
|
- | 329 | } |
|
- | 330 | ||
- | 331 | // ---------------------------------------------------------------------------------------- |
|
- | 332 | u8 NCMAG_GetAccConfig(void) |
|
- | 333 | { |
|
- | 334 | u8 retval = 0; |
|
- | 335 | // try to catch the i2c buffer within 100 ms timeout |
|
- | 336 | if(I2C_LockBuffer(100)) |
|
- | 337 | { |
|
- | 338 | u8 TxBytes = 0; |
|
- | 339 | I2C_Buffer[TxBytes++] = REG_ACC_CTRL1; |
|
- | 340 | if(I2C_Transmission(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig))) |
|
- | 341 | { |
|
182 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
342 | if(I2C_WaitForEndOfTransmission(100)) |
- | 343 | { |
|
- | 344 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
|
183 | } |
345 | } |
184 | } |
346 | } |
185 | } |
347 | } |
186 | return(retval); |
348 | return(retval); |
187 | } |
349 | } |
188 | 350 | ||
189 | // ---------------------------------------------------------------------------------------- |
351 | // ---------------------------------------------------------------------------------------- |
190 | u8 HMC5843_SetConfiguration(u8 cra, u8 crb, u8 mode) |
352 | u8 NCMAG_GetIdentification(void) |
191 | { |
353 | { |
192 | u8 retval = 0; |
354 | u8 retval = 0; |
193 | // tra to catch the I2c buffer within 100 ms timeout |
355 | // try to catch the i2c buffer within 100 ms timeout |
194 | if(I2C_LockBuffer(100)) |
356 | if(I2C_LockBuffer(100)) |
- | 357 | { |
|
- | 358 | u16 TxBytes = 0; |
|
- | 359 | NCMAG_Identification.A = 0xFF; |
|
- | 360 | NCMAG_Identification.B = 0xFF; |
|
- | 361 | NCMAG_Identification.C = 0xFF; |
|
- | 362 | I2C_Buffer[TxBytes++] = REG_MAG_IDA; |
|
- | 363 | // initiate transmission |
|
- | 364 | if(I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification))) |
|
- | 365 | { |
|
- | 366 | if(I2C_WaitForEndOfTransmission(100)) |
|
- | 367 | { |
|
- | 368 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
|
- | 369 | } |
|
- | 370 | } |
|
195 | { |
371 | } |
Line 196... | Line 372... | ||
196 | u16 TxBytes = 0; |
372 | return(retval); |
197 | I2C_Buffer[TxBytes++] = REGISTER_CRA; |
373 | } |
198 | I2C_Buffer[TxBytes++] = cra; |
374 | |
199 | I2C_Buffer[TxBytes++] = crb; |
375 | // ---------------------------------------------------------------------------------------- |
200 | I2C_Buffer[TxBytes++] = mode; |
376 | void NCMAG_GetMagVector(void) |
201 | // initiate transmission |
377 | { |
202 | if(I2C_Transmission(HMC5843_SLAVE_ADDRESS, TxBytes, NULL, 0)) |
378 | // try to catch the I2C buffer within 0 ms |
203 | { |
379 | if(I2C_LockBuffer(0)) |
204 | if(I2C_WaitForEndOfTransmission(50)) |
380 | { |
205 | { |
381 | u16 TxBytes = 0; |
206 | if(I2C_Error == I2C_ERROR_NONE) retval = 1; |
382 | // set register pointer |
207 | } |
383 | I2C_Buffer[TxBytes++] = REG_MAG_DATAX_MSB; |
208 | } |
384 | // initiate transmission |
Line -... | Line 385... | ||
- | 385 | I2C_Transmission(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector)); |
|
209 | } |
386 | } |
210 | return(retval); |
387 | } |
211 | } |
388 | |
Line 212... | Line 389... | ||
212 | 389 | //---------------------------------------------------------------- |
|
213 | //---------------------------------------------------------------- |
390 | void NCMAG_GetAccVector(void) |
214 | void HMC5843_GetMagVector(void) |
391 | { |
215 | { |
392 | // try to catch the I2C buffer within 0 ms |
216 | // try to catch the I2C buffer within 0 ms |
393 | if(I2C_LockBuffer(0)) |
217 | if(I2C_LockBuffer(0)) |
394 | { |
218 | { |
395 | u16 TxBytes = 0; |
Line 240... | Line 417... | ||
240 | { |
417 | { |
241 | do the calibration here |
418 | do the calibration here |
242 | } |
419 | } |
243 | else // request current heading */ |
420 | else // request current heading */ |
244 | { |
421 | { |
245 | HMC5843_GetMagVector(); |
422 | NCMAG_GetMagVector(); |
246 | } |
423 | } |
247 | TimerCompassUpdate = SetDelay(20); // every 20 ms are 50 Hz |
424 | TimerCompassUpdate = SetDelay(20); // every 20 ms are 50 Hz |
248 | } |
425 | } |
249 | } |
426 | } |
Line 250... | Line 427... | ||
250 | 427 | ||
251 | u8 HMC5843_SelfTest(void) |
428 | u8 NCMAG_SelfTest(void) |
- | 429 | { |
|
252 | { |
430 | #define LIMITS(value, min, max) {min = (90 * value)/100; max = (110 * value)/100;} |
253 | u32 time; |
431 | u32 time; |
254 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
432 | s32 XMin = 0, XMax = 0, YMin = 0, YMax = 0, ZMin = 0, ZMax = 0; |
- | 433 | s16 xscale, yscale, zscale, scale_min, scale_max; |
|
255 | s16 scale, scale_min, scale_max; |
434 | u8 crb_gain, cra_rate; |
Line -... | Line 435... | ||
- | 435 | u8 i = 0, retval = 1; |
|
- | 436 | ||
- | 437 | switch(NCMAG_MagType) |
|
- | 438 | { |
|
- | 439 | case MAG_TYPE_HMC5843: |
|
- | 440 | crb_gain = HMC5843_CRB_GAIN_10GA; |
|
- | 441 | cra_rate = HMC5843_CRA_RATE_50HZ; |
|
- | 442 | xscale = HMC5843_TEST_XSCALE; |
|
- | 443 | yscale = HMC5843_TEST_YSCALE; |
|
- | 444 | zscale = HMC5843_TEST_ZSCALE; |
|
- | 445 | break; |
|
- | 446 | ||
- | 447 | case MAG_TYPE_LSM303DLH: |
|
- | 448 | crb_gain = LSM303DLH_CRB_GAIN_13GA; |
|
- | 449 | cra_rate = LSM303DLH_CRA_RATE_75HZ; |
|
- | 450 | xscale = LSM303DLH_TEST_XSCALE; |
|
- | 451 | yscale = LSM303DLH_TEST_YSCALE; |
|
- | 452 | zscale = LSM303DLH_TEST_ZSCALE; |
|
- | 453 | break; |
|
- | 454 | ||
- | 455 | default: |
|
- | 456 | return(0); |
|
- | 457 | } |
|
- | 458 | ||
- | 459 | MagConfig.cra = cra_rate|CRA_MODE_POSBIAS; |
|
256 | u8 i = 0; |
460 | MagConfig.crb = crb_gain; |
257 | 461 | MagConfig.mode = MODE_CONTINUOUS; |
|
258 | // activate positive bias field of 0.55 gauss |
462 | // activate positive bias field |
259 | HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_POSBIAS, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS); |
463 | NCMAG_SetMagConfig(); |
260 | // wait for stable readings |
464 | // wait for stable readings |
261 | time = SetDelay(50); |
465 | time = SetDelay(50); |
- | 466 | while(!CheckDelay(time)); |
|
262 | while(!CheckDelay(time)); |
467 | // averaging |
263 | // averaging |
468 | #define AVERAGE 20 |
264 | for(i = 0; i<20; i++) |
469 | for(i = 0; i<AVERAGE; i++) |
265 | { |
470 | { |
266 | HMC5843_GetMagVector(); |
471 | NCMAG_GetMagVector(); |
267 | time = SetDelay(20); |
472 | time = SetDelay(20); |
268 | while(!CheckDelay(time)); |
473 | while(!CheckDelay(time)); |
269 | XMax += MagVector.X; |
474 | XMax += MagVector.X; |
270 | YMax += MagVector.Y; |
475 | YMax += MagVector.Y; |
- | 476 | ZMax += MagVector.Z; |
|
271 | ZMax += MagVector.Z; |
477 | } |
272 | } |
478 | MagConfig.cra = cra_rate|CRA_MODE_NEGBIAS; |
273 | // activate negative bias field of 0.55 gauss |
479 | // activate positive bias field |
274 | HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NEGBIAS, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS); |
480 | NCMAG_SetMagConfig(); |
275 | // wait for stable readings |
481 | // wait for stable readings |
276 | time = SetDelay(50); |
482 | time = SetDelay(50); |
277 | while(!CheckDelay(time)); |
483 | while(!CheckDelay(time)); |
278 | // averaging |
484 | // averaging |
279 | for(i = 0; i < 20; i++) |
485 | for(i = 0; i < AVERAGE; i++) |
280 | { |
486 | { |
281 | HMC5843_GetMagVector(); |
487 | NCMAG_GetMagVector(); |
282 | time = SetDelay(20); |
488 | time = SetDelay(20); |
283 | while(!CheckDelay(time)); |
489 | while(!CheckDelay(time)); |
284 | XMin += MagVector.X; |
490 | XMin += MagVector.X; |
285 | YMin += MagVector.Y; |
491 | YMin += MagVector.Y; |
286 | ZMin += MagVector.Z; |
492 | ZMin += MagVector.Z; |
287 | } |
493 | } |
- | 494 | // setup final configuration |
|
- | 495 | MagConfig.cra = cra_rate|CRA_MODE_NORMAL; |
|
288 | // setup final configuration |
496 | // activate positive bias field |
289 | HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NORMAL, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS); |
- | |
290 | // prepare scale limits |
497 | NCMAG_SetMagConfig(); |
291 | scale = 715; // 1300 counts/Gauss * 0.55 Gauss = 715 counts |
- | |
292 | scale_min = (scale * 90)/100; |
498 | // prepare scale limits |
293 | scale_max = (scale * 110)/100; |
499 | LIMITS(xscale, scale_min, scale_max); |
294 | // check scale for all axes |
500 | // check scale for all axes |
- | 501 | xscale = (XMax - XMin)/(2*AVERAGE); |
|
295 | scale = (XMax - XMin)/40; |
502 | if((xscale > scale_max) || (xscale < scale_min)) retval = 0; |
296 | if((scale > scale_max) || (scale < scale_min)) return(0); |
503 | LIMITS(yscale, scale_min, scale_max); |
- | 504 | yscale = (YMax - YMin)/(2*AVERAGE); |
|
297 | scale = (YMax - YMin)/40; |
505 | if((yscale > scale_max) || (yscale < scale_min)) retval = 0; |
298 | if((scale > scale_max) || (scale < scale_min)) return(0); |
506 | LIMITS(zscale, scale_min, scale_max); |
299 | scale = (ZMax - ZMin)/40; |
507 | zscale = (ZMax - ZMin)/(2*AVERAGE); |
300 | if((scale > scale_max) || (scale < scale_min)) return(0); |
508 | if((zscale > scale_max) || (zscale < scale_min)) retval = 0; |
Line 301... | Line 509... | ||
301 | return(1); |
509 | return(retval); |
302 | } |
510 | } |
303 | 511 | ||
304 | 512 | ||
305 | //---------------------------------------------------------------- |
513 | //---------------------------------------------------------------- |
306 | u8 HMC5843_Init(void) |
514 | u8 NCMAG_Init(void) |
Line 307... | Line 515... | ||
307 | { |
515 | { |
- | 516 | u8 msg[64]; |
|
- | 517 | u8 retval = 0; |
|
- | 518 | u8 repeat; |
|
- | 519 | ||
308 | u8 msg[64]; |
520 | NCMAG_Present = 0; |
- | 521 | NCMAG_MagType = MAG_TYPE_HMC5843; // assuming having an HMC5843 |
|
- | 522 | // polling for LSM302DLH option |
|
- | 523 | repeat = 0; |
|
- | 524 | do |
|
- | 525 | { |
|
- | 526 | retval = NCMAG_GetAccConfig(); |
|
309 | u8 retval = 0; |
527 | if(retval) break; // break loop on success |
310 | u8 repeat; |
528 | UART1_PutString("."); |
311 | 529 | repeat++; |
|
312 | HMC5843_Present = 0; |
530 | }while(repeat < 3); |
313 | 531 | if(retval) NCMAG_MagType = MAG_TYPE_LSM303DLH; // must be a LSM303DLH |
|
314 | // polling of identification |
532 | // polling of identification |
315 | repeat = 0; |
533 | repeat = 0; |
316 | do |
534 | do |
317 | { |
535 | { |
318 | retval = HMC5843_GetIdentification(); |
536 | retval = NCMAG_GetIdentification(); |
319 | if(retval) break; // break loop on success |
537 | if(retval) break; // break loop on success |
320 | UART1_PutString("."); |
538 | UART1_PutString("."); |
- | 539 | repeat++; |
|
- | 540 | }while(repeat < 12); |
|
- | 541 | // if we got an answer to id request |
|
- | 542 | if(retval) |
|
- | 543 | { |
|
321 | repeat++; |
544 | u8 n1[] = "HMC5843"; |
322 | }while(repeat < 12); |
545 | u8 n2[] = "LSM303DLH"; |
323 | // if we got it |
- | |
324 | if(retval) |
546 | u8* pn; |
325 | { |
547 | if(NCMAG_MagType == MAG_TYPE_LSM303DLH) pn = n2; |
326 | sprintf(msg, " HMC5843 ID %d/%d/%d", HMC5843_Identification.A, HMC5843_Identification.B, HMC5843_Identification.C); |
548 | else pn = n1; |
327 | UART1_PutString(msg); |
549 | sprintf(msg, " %s ID%d/%d/%d", pn, NCMAG_Identification.A, NCMAG_Identification.B, NCMAG_Identification.C); |
328 | - | ||
329 | if ( (HMC5843_Identification.A == HMC5843_IDA) |
550 | UART1_PutString(msg); |
330 | && (HMC5843_Identification.B == HMC5843_IDB) |
551 | if ( (NCMAG_Identification.A == MAG_IDA) |
331 | && (HMC5843_Identification.C == HMC5843_IDC)) |
552 | && (NCMAG_Identification.B == MAG_IDB) |
332 | { |
553 | && (NCMAG_Identification.C == MAG_IDC)) |
333 | //HMC5843_SetConfiguration(HMC5843_CRA_RATE_50HZ|HMC5843_CRA_MODE_NORMAL, HMC5843_CRB_GAIN_10GA, HMC5843_MODE_CONTINUOUS); |
554 | { |
334 | if(!HMC5843_SelfTest()) |
555 | if(!NCMAG_SelfTest()) |
335 | { |
556 | { |
336 | UART1_PutString("\n\r HMC5843 selftest failed!"); |
557 | UART1_PutString(" Selftest failed!"); |
337 | LED_RED_ON; |
558 | LED_RED_ON; |
338 | } |
559 | } |
339 | else HMC5843_Present = 1; |
560 | else NCMAG_Present = 1; |
340 | } |
561 | } |
341 | else |
562 | else |
342 | { |
563 | { |
- | 564 | UART1_PutString("\n\r not compatible!"); |
|
- | 565 | LED_RED_ON; |
|
- | 566 | } |
|
- | 567 | } |
|
343 | UART1_PutString("\n\r HMC5843 not compatible!"); |
568 | else // nothing found |
344 | LED_RED_ON; |
569 | { |