Rev 465 | Rev 482 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 465 | Rev 470 | ||
---|---|---|---|
Line 55... | Line 55... | ||
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
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 "i2c1.h" |
61 | #include "i2c0.h" |
61 | #include "i2c0.h" |
62 | #include "timer1.h" |
62 | #include "timer1.h" |
63 | #include "led.h" |
63 | #include "led.h" |
64 | #include "main.h" |
64 | #include "main.h" |
65 | #include "uart1.h" |
65 | #include "uart1.h" |
Line 172... | Line 172... | ||
172 | 172 | ||
173 | // use I2C1 for communication |
173 | // use I2C1 for communication |
174 | void MK3MAG_SendCommand(u8 command) |
174 | void MK3MAG_SendCommand(u8 command) |
175 | { |
175 | { |
176 | // try to catch the I2C buffer |
176 | // try to catch the I2C buffer |
177 | if(I2C_LockBuffer(0)) |
177 | if(I2C1_LockBuffer(0)) |
178 | { |
178 | { |
179 | u16 TxBytes = 0; |
179 | u16 TxBytes = 0; |
180 | u16 RxBytes = 0; |
180 | u16 RxBytes = 0; |
Line 181... | Line 181... | ||
181 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
181 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
182 | 182 | ||
Line 183... | Line 183... | ||
183 | // update current command id |
183 | // update current command id |
184 | I2C_Buffer[TxBytes++] = command; |
184 | I2C1_Buffer[TxBytes++] = command; |
185 | 185 | ||
186 | // set pointers to data area with respect to the command id |
186 | // set pointers to data area with respect to the command id |
Line 191... | Line 191... | ||
191 | pRxHandlerFunc = &MK3MAG_UpdateVersion; |
191 | pRxHandlerFunc = &MK3MAG_UpdateVersion; |
192 | break; |
192 | break; |
193 | case MK3MAG_CMD_WRITE_CAL: |
193 | case MK3MAG_CMD_WRITE_CAL: |
194 | RxBytes = sizeof(MK3MAG_ReadCal)+1; |
194 | RxBytes = sizeof(MK3MAG_ReadCal)+1; |
195 | pRxHandlerFunc = &MK3MAG_UpdateCalibration; |
195 | pRxHandlerFunc = &MK3MAG_UpdateCalibration; |
196 | memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal)); |
196 | memcpy((u8*)I2C1_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal)); |
197 | TxBytes += sizeof(MK3MAG_WriteCal); |
197 | TxBytes += sizeof(MK3MAG_WriteCal); |
198 | break; |
198 | break; |
199 | case MK3MAG_CMD_READ_MAGVECT: |
199 | case MK3MAG_CMD_READ_MAGVECT: |
200 | RxBytes = sizeof(MagVector)+1; |
200 | RxBytes = sizeof(MagVector)+1; |
201 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
201 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
Line 204... | Line 204... | ||
204 | RxBytes = 0; |
204 | RxBytes = 0; |
205 | pRxHandlerFunc = NULL; |
205 | pRxHandlerFunc = NULL; |
206 | break; |
206 | break; |
207 | } |
207 | } |
208 | // update packet checksum |
208 | // update packet checksum |
209 | I2C_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C_Buffer, TxBytes); |
209 | I2C1_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C1_Buffer, TxBytes); |
210 | TxBytes++; |
210 | TxBytes++; |
211 | // initiate I2C transmission |
211 | // initiate I2C transmission |
212 | I2C_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes); |
212 | I2C1_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes); |
213 | } // EOF I2C_State == I2C_IDLE |
213 | } // EOF I2C_State == I2C_IDLE |
214 | } |
214 | } |
Line 215... | Line 215... | ||
215 | 215 | ||
Line 271... | Line 271... | ||
271 | static s16 x_max,y_max,z_max,x_min,y_min,z_min; |
271 | static s16 x_max,y_max,z_max,x_min,y_min,z_min; |
272 | static u8 last_state,speak = 0; |
272 | static u8 last_state,speak = 0; |
273 | u8 msg[64]; |
273 | u8 msg[64]; |
274 | u16 MinCaclibration = 500; |
274 | u16 MinCaclibration = 500; |
Line 275... | Line 275... | ||
275 | 275 | ||
Line 276... | Line 276... | ||
276 | if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return; |
276 | if( (I2C1_State == I2C_STATE_OFF) || !MK3MAG_Present ) return; |
277 | 277 | ||
278 | if(CheckDelay(TimerUpdate)) |
278 | if(CheckDelay(TimerUpdate)) |
279 | { |
279 | { |