Rev 454 | Rev 465 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 454 | Rev 464 | ||
---|---|---|---|
Line 56... | Line 56... | ||
56 | #include <stdio.h> |
56 | #include <stdio.h> |
57 | #include <string.h> |
57 | #include <string.h> |
58 | #include "91x_lib.h" |
58 | #include "91x_lib.h" |
59 | #include "mk3mag.h" |
59 | #include "mk3mag.h" |
60 | #include "i2c.h" |
60 | #include "i2c.h" |
- | 61 | #include "i2c0.h" |
|
61 | #include "timer1.h" |
62 | #include "timer1.h" |
62 | #include "led.h" |
63 | #include "led.h" |
63 | #include "main.h" |
64 | #include "main.h" |
64 | #include "uart1.h" |
65 | #include "uart1.h" |
65 | #include "compass.h" |
66 | #include "compass.h" |
Line 167... | Line 168... | ||
167 | //---------------------------------------------------------------- |
168 | //---------------------------------------------------------------- |
168 | #define MK3MAG_CMD_VERSION 0x01 |
169 | #define MK3MAG_CMD_VERSION 0x01 |
169 | #define MK3MAG_CMD_READ_MAGVECT 0x02 |
170 | #define MK3MAG_CMD_READ_MAGVECT 0x02 |
170 | #define MK3MAG_CMD_WRITE_CAL 0x04 |
171 | #define MK3MAG_CMD_WRITE_CAL 0x04 |
Line -... | Line 172... | ||
- | 172 | ||
171 | 173 | // use I2C1 for communication |
|
172 | void MK3MAG_SendCommand(u8 command) |
174 | void MK3MAG_SendCommandI2C1(u8 command) |
173 | { |
175 | { |
174 | // try to catch the I2C buffer |
176 | // try to catch the I2C buffer |
175 | if(I2C_LockBuffer(0)) |
177 | if(I2C_LockBuffer(0)) |
176 | { |
178 | { |
Line 209... | Line 211... | ||
209 | // initiate I2C transmission |
211 | // initiate I2C transmission |
210 | I2C_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes); |
212 | I2C_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes); |
211 | } // EOF I2C_State == I2C_IDLE |
213 | } // EOF I2C_State == I2C_IDLE |
212 | } |
214 | } |
Line -... | Line 215... | ||
- | 215 | ||
- | 216 | // use I2C0 for communication |
|
- | 217 | void MK3MAG_SendCommandI2C0(u8 command) |
|
- | 218 | { |
|
- | 219 | // try to catch the I2C buffer |
|
- | 220 | if(I2C0_LockBuffer(0)) |
|
- | 221 | { |
|
- | 222 | u16 TxBytes = 0; |
|
- | 223 | u16 RxBytes = 0; |
|
- | 224 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
|
- | 225 | ||
- | 226 | // update current command id |
|
- | 227 | I2C0_Buffer[TxBytes++] = command; |
|
- | 228 | ||
- | 229 | // set pointers to data area with respect to the command id |
|
- | 230 | switch (command) |
|
- | 231 | { |
|
- | 232 | case MK3MAG_CMD_VERSION: |
|
- | 233 | RxBytes = sizeof(MK3MAG_Version)+1; |
|
- | 234 | pRxHandlerFunc = &MK3MAG_UpdateVersion; |
|
- | 235 | break; |
|
- | 236 | case MK3MAG_CMD_WRITE_CAL: |
|
- | 237 | RxBytes = sizeof(MK3MAG_ReadCal)+1; |
|
- | 238 | pRxHandlerFunc = &MK3MAG_UpdateCalibration; |
|
- | 239 | memcpy((u8*)I2C0_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal)); |
|
- | 240 | TxBytes += sizeof(MK3MAG_WriteCal); |
|
- | 241 | break; |
|
- | 242 | case MK3MAG_CMD_READ_MAGVECT: |
|
- | 243 | RxBytes = sizeof(MagVector)+1; |
|
- | 244 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
|
- | 245 | break; |
|
- | 246 | default: // unknown command id |
|
- | 247 | RxBytes = 0; |
|
- | 248 | pRxHandlerFunc = NULL; |
|
- | 249 | break; |
|
- | 250 | } |
|
- | 251 | // update packet checksum |
|
- | 252 | I2C0_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C0_Buffer, TxBytes); |
|
- | 253 | TxBytes++; |
|
- | 254 | // initiate I2C transmission |
|
- | 255 | I2C0_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes); |
|
- | 256 | } // EOF I2C_State == I2C_IDLE |
|
- | 257 | } |
|
- | 258 | ||
- | 259 | void MK3MAG_SendCommand(u8 command) |
|
- | 260 | { |
|
- | 261 | // MK3MAG_SendCommandI2C1(command); |
|
- | 262 | MK3MAG_SendCommandI2C0(command); |
|
Line 213... | Line 263... | ||
213 | 263 | } |
|
214 | 264 | ||
215 | //---------------------------------------------------------------- |
265 | //---------------------------------------------------------------- |
216 | u8 MK3MAG_Init(void) |
266 | u8 MK3MAG_Init(void) |