Rev 454 | Rev 465 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 454 | Rev 464 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + www.MikroKopter.com |
5 | // + www.MikroKopter.com |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
7 | // + Software Nutzungsbedingungen (english version: see below) |
7 | // + Software Nutzungsbedingungen (english version: see below) |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
22 | // + des Mitverschuldens offen. |
22 | // + des Mitverschuldens offen. |
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
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. |
24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
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 |
26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
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 | // + 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. |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // + Software LICENSING TERMS |
32 | // + Software LICENSING TERMS |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
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 | // + 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 |
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
37 | // + The Software may only be used with the Licensor's products. |
37 | // + The Software may only be used with the Licensor's products. |
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
40 | // + agreement shall be the property of the Licensor. |
40 | // + agreement shall be the property of the Licensor. |
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
43 | // + The customer shall be responsible for taking reasonable precautions |
43 | // + The customer shall be responsible for taking reasonable precautions |
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
53 | // + #### END OF LICENSING TERMS #### |
53 | // + #### END OF LICENSING TERMS #### |
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
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 "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" |
66 | #include "spi_slave.h" |
67 | #include "spi_slave.h" |
67 | 68 | ||
68 | #define MK3MAG_SLAVE_ADDRESS 0x50 // i2C slave address |
69 | #define MK3MAG_SLAVE_ADDRESS 0x50 // i2C slave address |
69 | 70 | ||
70 | u8 MK3MAG_Present = 0; |
71 | u8 MK3MAG_Present = 0; |
71 | 72 | ||
72 | typedef struct |
73 | typedef struct |
73 | { |
74 | { |
74 | s16 Nick; |
75 | s16 Nick; |
75 | s16 Roll; |
76 | s16 Roll; |
76 | } __attribute__((packed)) MK3MAG_WriteAttitude_t; |
77 | } __attribute__((packed)) MK3MAG_WriteAttitude_t; |
77 | 78 | ||
78 | typedef struct |
79 | typedef struct |
79 | { |
80 | { |
80 | u8 Major; |
81 | u8 Major; |
81 | u8 Minor; |
82 | u8 Minor; |
82 | u8 Patch; |
83 | u8 Patch; |
83 | u8 Compatible; |
84 | u8 Compatible; |
84 | } __attribute__((packed)) MK3MAG_Version_t; |
85 | } __attribute__((packed)) MK3MAG_Version_t; |
85 | 86 | ||
86 | typedef struct |
87 | typedef struct |
87 | { |
88 | { |
88 | u8 CalByte; |
89 | u8 CalByte; |
89 | u8 Dummy1; |
90 | u8 Dummy1; |
90 | u8 Dummy2; |
91 | u8 Dummy2; |
91 | } __attribute__((packed)) MK3MAG_Cal_t; |
92 | } __attribute__((packed)) MK3MAG_Cal_t; |
92 | 93 | ||
93 | // Transfer buffers |
94 | // Transfer buffers |
94 | volatile MK3MAG_WriteAttitude_t MK3MAG_WriteAttitude; |
95 | volatile MK3MAG_WriteAttitude_t MK3MAG_WriteAttitude; |
95 | volatile MK3MAG_Version_t MK3MAG_Version; |
96 | volatile MK3MAG_Version_t MK3MAG_Version; |
96 | volatile MK3MAG_Cal_t MK3MAG_WriteCal; |
97 | volatile MK3MAG_Cal_t MK3MAG_WriteCal; |
97 | volatile MK3MAG_Cal_t MK3MAG_ReadCal; |
98 | volatile MK3MAG_Cal_t MK3MAG_ReadCal; |
98 | 99 | ||
99 | // ------------------------------------------------------------------------------- |
100 | // ------------------------------------------------------------------------------- |
100 | // the calculation of the MK3MAG packet checksum |
101 | // the calculation of the MK3MAG packet checksum |
101 | 102 | ||
102 | // calculate the crc of bytecount bytes in buffer |
103 | // calculate the crc of bytecount bytes in buffer |
103 | u8 MK3MAG_CalcCRC(u8* pBuffer, u8 bytecount) |
104 | u8 MK3MAG_CalcCRC(u8* pBuffer, u8 bytecount) |
104 | { |
105 | { |
105 | u8 i, crc = 0; |
106 | u8 i, crc = 0; |
106 | for(i=0; i < bytecount; i++) |
107 | for(i=0; i < bytecount; i++) |
107 | { |
108 | { |
108 | crc += pBuffer[i]; |
109 | crc += pBuffer[i]; |
109 | } |
110 | } |
110 | crc = ~crc; |
111 | crc = ~crc; |
111 | return crc; |
112 | return crc; |
112 | } |
113 | } |
113 | 114 | ||
114 | // assuming the last byte in buffer is the crc byte |
115 | // assuming the last byte in buffer is the crc byte |
115 | u8 MK3MAG_CheckCRC(u8* pBuffer, u8 BuffLen) |
116 | u8 MK3MAG_CheckCRC(u8* pBuffer, u8 BuffLen) |
116 | { |
117 | { |
117 | u8 crc = 0, retval = 0; |
118 | u8 crc = 0, retval = 0; |
118 | if(BuffLen == 0) return(retval); |
119 | if(BuffLen == 0) return(retval); |
119 | crc = MK3MAG_CalcCRC(pBuffer, BuffLen-1); |
120 | crc = MK3MAG_CalcCRC(pBuffer, BuffLen-1); |
120 | if(crc == pBuffer[BuffLen-1]) |
121 | if(crc == pBuffer[BuffLen-1]) |
121 | { |
122 | { |
122 | retval = 1; |
123 | retval = 1; |
123 | DebugOut.Analog[15]++; // count mk3mag ok |
124 | DebugOut.Analog[15]++; // count mk3mag ok |
124 | } |
125 | } |
125 | else |
126 | else |
126 | { |
127 | { |
127 | retval = 0; |
128 | retval = 0; |
128 | DebugOut.Analog[14]++; // count mk3mag crc error |
129 | DebugOut.Analog[14]++; // count mk3mag crc error |
129 | } |
130 | } |
130 | return(retval); |
131 | return(retval); |
131 | } |
132 | } |
132 | 133 | ||
133 | // ------------------------------------------------------------------------------- |
134 | // ------------------------------------------------------------------------------- |
134 | // the I2C rx data handler functions |
135 | // the I2C rx data handler functions |
135 | 136 | ||
136 | // rx data handler for version info request |
137 | // rx data handler for version info request |
137 | void MK3MAG_UpdateVersion(u8* pRxBuffer, u8 RxBufferSize) |
138 | void MK3MAG_UpdateVersion(u8* pRxBuffer, u8 RxBufferSize) |
138 | { // if crc is ok and number of byte are matching |
139 | { // if crc is ok and number of byte are matching |
139 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Version)+1)) ) |
140 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Version)+1)) ) |
140 | { |
141 | { |
141 | memcpy((u8 *)&MK3MAG_Version, pRxBuffer, sizeof(MK3MAG_Version)); |
142 | memcpy((u8 *)&MK3MAG_Version, pRxBuffer, sizeof(MK3MAG_Version)); |
142 | } |
143 | } |
143 | } |
144 | } |
144 | 145 | ||
145 | // rx data handler for calibration request |
146 | // rx data handler for calibration request |
146 | void MK3MAG_UpdateCalibration(u8* pRxBuffer, u8 RxBufferSize) |
147 | void MK3MAG_UpdateCalibration(u8* pRxBuffer, u8 RxBufferSize) |
147 | { // if crc is ok and number of byte are matching |
148 | { // if crc is ok and number of byte are matching |
148 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_ReadCal)+1)) ) |
149 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_ReadCal)+1)) ) |
149 | { |
150 | { |
150 | memcpy((u8 *)&MK3MAG_ReadCal, pRxBuffer, sizeof(MK3MAG_ReadCal)); |
151 | memcpy((u8 *)&MK3MAG_ReadCal, pRxBuffer, sizeof(MK3MAG_ReadCal)); |
151 | } |
152 | } |
152 | } |
153 | } |
153 | 154 | ||
154 | // rx data handler for magnetic vector request |
155 | // rx data handler for magnetic vector request |
155 | void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
156 | void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
156 | { // if crc is ok and number of byte are matching |
157 | { // if crc is ok and number of byte are matching |
157 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) ) |
158 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) ) |
158 | { |
159 | { |
159 | s16vec_t *pMagVector = (s16vec_t*)pRxBuffer; |
160 | s16vec_t *pMagVector = (s16vec_t*)pRxBuffer; |
160 | MagVector.X = pMagVector->Y; |
161 | MagVector.X = pMagVector->Y; |
161 | MagVector.Y = pMagVector->X; |
162 | MagVector.Y = pMagVector->X; |
162 | MagVector.Z = -pMagVector->Z; |
163 | MagVector.Z = -pMagVector->Z; |
163 | Compass_CalcHeading(); |
164 | Compass_CalcHeading(); |
164 | } |
165 | } |
165 | } |
166 | } |
166 | 167 | ||
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 |
- | 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 | { |
177 | u16 TxBytes = 0; |
179 | u16 TxBytes = 0; |
178 | u16 RxBytes = 0; |
180 | u16 RxBytes = 0; |
179 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
181 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
180 | 182 | ||
181 | // update current command id |
183 | // update current command id |
182 | I2C_Buffer[TxBytes++] = command; |
184 | I2C_Buffer[TxBytes++] = command; |
183 | 185 | ||
184 | // set pointers to data area with respect to the command id |
186 | // set pointers to data area with respect to the command id |
185 | switch (command) |
187 | switch (command) |
186 | { |
188 | { |
187 | case MK3MAG_CMD_VERSION: |
189 | case MK3MAG_CMD_VERSION: |
188 | RxBytes = sizeof(MK3MAG_Version)+1; |
190 | RxBytes = sizeof(MK3MAG_Version)+1; |
189 | pRxHandlerFunc = &MK3MAG_UpdateVersion; |
191 | pRxHandlerFunc = &MK3MAG_UpdateVersion; |
190 | break; |
192 | break; |
191 | case MK3MAG_CMD_WRITE_CAL: |
193 | case MK3MAG_CMD_WRITE_CAL: |
192 | RxBytes = sizeof(MK3MAG_ReadCal)+1; |
194 | RxBytes = sizeof(MK3MAG_ReadCal)+1; |
193 | pRxHandlerFunc = &MK3MAG_UpdateCalibration; |
195 | pRxHandlerFunc = &MK3MAG_UpdateCalibration; |
194 | memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal)); |
196 | memcpy((u8*)I2C_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal)); |
195 | TxBytes += sizeof(MK3MAG_WriteCal); |
197 | TxBytes += sizeof(MK3MAG_WriteCal); |
196 | break; |
198 | break; |
197 | case MK3MAG_CMD_READ_MAGVECT: |
199 | case MK3MAG_CMD_READ_MAGVECT: |
198 | RxBytes = sizeof(MagVector)+1; |
200 | RxBytes = sizeof(MagVector)+1; |
199 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
201 | pRxHandlerFunc = &MK3MAG_UpdateMagVector; |
200 | break; |
202 | break; |
201 | default: // unknown command id |
203 | default: // unknown command id |
202 | RxBytes = 0; |
204 | RxBytes = 0; |
203 | pRxHandlerFunc = NULL; |
205 | pRxHandlerFunc = NULL; |
204 | break; |
206 | break; |
205 | } |
207 | } |
206 | // update packet checksum |
208 | // update packet checksum |
207 | I2C_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C_Buffer, TxBytes); |
209 | I2C_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C_Buffer, TxBytes); |
208 | TxBytes++; |
210 | TxBytes++; |
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 | } |
- | 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); |
|
213 | 263 | } |
|
214 | 264 | ||
215 | //---------------------------------------------------------------- |
265 | //---------------------------------------------------------------- |
216 | u8 MK3MAG_Init(void) |
266 | u8 MK3MAG_Init(void) |
217 | { |
267 | { |
218 | u8 msg[64]; |
268 | u8 msg[64]; |
219 | u8 repeat; |
269 | u8 repeat; |
220 | u32 timeout; |
270 | u32 timeout; |
221 | 271 | ||
222 | MK3MAG_Present = 0; |
272 | MK3MAG_Present = 0; |
223 | 273 | ||
224 | MK3MAG_Version.Major = 0xFF; |
274 | MK3MAG_Version.Major = 0xFF; |
225 | MK3MAG_Version.Minor = 0xFF; |
275 | MK3MAG_Version.Minor = 0xFF; |
226 | MK3MAG_Version.Patch = 0xFF; |
276 | MK3MAG_Version.Patch = 0xFF; |
227 | MK3MAG_Version.Compatible = 0xFF; |
277 | MK3MAG_Version.Compatible = 0xFF; |
228 | 278 | ||
229 | Compass_Heading = -1; |
279 | Compass_Heading = -1; |
230 | 280 | ||
231 | // polling of version info |
281 | // polling of version info |
232 | repeat = 0; |
282 | repeat = 0; |
233 | do |
283 | do |
234 | { |
284 | { |
235 | MK3MAG_SendCommand(MK3MAG_CMD_VERSION); |
285 | MK3MAG_SendCommand(MK3MAG_CMD_VERSION); |
236 | if(Version_HW > 11) timeout = SetDelay(100); |
286 | if(Version_HW > 11) timeout = SetDelay(100); |
237 | else timeout = SetDelay(250); |
287 | else timeout = SetDelay(250); |
238 | 288 | ||
239 | do |
289 | do |
240 | { |
290 | { |
241 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
291 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
242 | }while (!CheckDelay(timeout)); |
292 | }while (!CheckDelay(timeout)); |
243 | UART1_PutString("."); |
293 | UART1_PutString("."); |
244 | repeat++; |
294 | repeat++; |
245 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
295 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
246 | // if we got it |
296 | // if we got it |
247 | if (MK3MAG_Version.Major != 0xFF) |
297 | if (MK3MAG_Version.Major != 0xFF) |
248 | { |
298 | { |
249 | sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
299 | sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
250 | UART1_PutString(msg); |
300 | UART1_PutString(msg); |
251 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
301 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
252 | { |
302 | { |
253 | UART1_PutString("\n\r MK3MAG not compatible!"); |
303 | UART1_PutString("\n\r MK3MAG not compatible!"); |
254 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
304 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
255 | LED_RED_ON; |
305 | LED_RED_ON; |
256 | } |
306 | } |
257 | else |
307 | else |
258 | { // version ok |
308 | { // version ok |
259 | MK3MAG_Present = 1; |
309 | MK3MAG_Present = 1; |
260 | } |
310 | } |
261 | } |
311 | } |
262 | return(MK3MAG_Present); |
312 | return(MK3MAG_Present); |
263 | } |
313 | } |
264 | 314 | ||
265 | //---------------------------------------------------------------- |
315 | //---------------------------------------------------------------- |
266 | void MK3MAG_Update(void) |
316 | void MK3MAG_Update(void) |
267 | { |
317 | { |
268 | static u32 TimerUpdate = 0; |
318 | static u32 TimerUpdate = 0; |
269 | static s16 x_max,y_max,z_max,x_min,y_min,z_min; |
319 | static s16 x_max,y_max,z_max,x_min,y_min,z_min; |
270 | static u8 last_state,speak = 0; |
320 | static u8 last_state,speak = 0; |
271 | u8 msg[64]; |
321 | u8 msg[64]; |
272 | u16 MinCaclibration = 500; |
322 | u16 MinCaclibration = 500; |
273 | 323 | ||
274 | if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return; |
324 | if( (I2C_State == I2C_STATE_OFF) || !MK3MAG_Present ) return; |
275 | 325 | ||
276 | if(CheckDelay(TimerUpdate)) |
326 | if(CheckDelay(TimerUpdate)) |
277 | { |
327 | { |
278 | // check for incomming compass calibration request |
328 | // check for incomming compass calibration request |
279 | Compass_UpdateCalState(); |
329 | Compass_UpdateCalState(); |
280 | MK3MAG_WriteCal.CalByte = Compass_CalState; |
330 | MK3MAG_WriteCal.CalByte = Compass_CalState; |
281 | 331 | ||
282 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
332 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
283 | { // send new calibration state |
333 | { // send new calibration state |
284 | MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL); |
334 | MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL); |
285 | } |
335 | } |
286 | else |
336 | else |
287 | { // calibration state matches |
337 | { // calibration state matches |
288 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); // initiate magvector transfer |
338 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); // initiate magvector transfer |
289 | 339 | ||
290 | switch(Compass_CalState) |
340 | switch(Compass_CalState) |
291 | { |
341 | { |
292 | case 1: |
342 | case 1: |
293 | if(last_state != Compass_CalState) |
343 | if(last_state != Compass_CalState) |
294 | { |
344 | { |
295 | UART1_PutString("\r\nMK3Mag calibration\n\r"); |
345 | UART1_PutString("\r\nMK3Mag calibration\n\r"); |
296 | if(EarthMagneticStrengthTheoretic) |
346 | if(EarthMagneticStrengthTheoretic) |
297 | { |
347 | { |
298 | MinCaclibration = (MinCaclibration * EarthMagneticStrengthTheoretic) / 50; |
348 | MinCaclibration = (MinCaclibration * EarthMagneticStrengthTheoretic) / 50; |
299 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
349 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
300 | UART1_PutString(msg); |
350 | UART1_PutString(msg); |
301 | } |
351 | } |
302 | else UART1_PutString("without GPS\n\r"); |
352 | else UART1_PutString("without GPS\n\r"); |
303 | } |
353 | } |
304 | x_max = -30000; y_max = -30000; z_max = -30000; |
354 | x_max = -30000; y_max = -30000; z_max = -30000; |
305 | x_min = 30000; y_min = 30000; z_min = 30000; |
355 | x_min = 30000; y_min = 30000; z_min = 30000; |
306 | speak = 1; |
356 | speak = 1; |
307 | break; |
357 | break; |
308 | case 2: |
358 | case 2: |
309 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
359 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
310 | 360 | ||
311 | if(MagVector.X > x_max) { x_max = MagVector.X; BeepTime = 60; } |
361 | if(MagVector.X > x_max) { x_max = MagVector.X; BeepTime = 60; } |
312 | else if(MagVector.X < x_min) { x_min = MagVector.X; BeepTime = 20; } |
362 | else if(MagVector.X < x_min) { x_min = MagVector.X; BeepTime = 20; } |
313 | if(MagVector.Y > y_max) { y_max = MagVector.Y; BeepTime = 60; } |
363 | if(MagVector.Y > y_max) { y_max = MagVector.Y; BeepTime = 60; } |
314 | else if(MagVector.Y < y_min) { y_min = MagVector.Y; BeepTime = 20; } |
364 | else if(MagVector.Y < y_min) { y_min = MagVector.Y; BeepTime = 20; } |
315 | 365 | ||
316 | break; |
366 | break; |
317 | case 3: |
367 | case 3: |
318 | speak = 1; |
368 | speak = 1; |
319 | break; |
369 | break; |
320 | case 4: |
370 | case 4: |
321 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
371 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
322 | if(MagVector.Z > z_max) { z_max = MagVector.Z; BeepTime = 80; } |
372 | if(MagVector.Z > z_max) { z_max = MagVector.Z; BeepTime = 80; } |
323 | else if(MagVector.Z < z_min) { z_min = MagVector.Z; BeepTime = 80; } |
373 | else if(MagVector.Z < z_min) { z_min = MagVector.Z; BeepTime = 80; } |
324 | break; |
374 | break; |
325 | case 5: |
375 | case 5: |
326 | if(last_state == Compass_CalState) break; |
376 | if(last_state == Compass_CalState) break; |
327 | if(((x_max - x_min) > MinCaclibration) && ((y_max - y_min) > MinCaclibration) && ((z_max - z_min) > MinCaclibration)) |
377 | if(((x_max - x_min) > MinCaclibration) && ((y_max - y_min) > MinCaclibration) && ((z_max - z_min) > MinCaclibration)) |
328 | { |
378 | { |
329 | BeepTime = 2500; |
379 | BeepTime = 2500; |
330 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
380 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
331 | SpeakHoTT = SPEAK_MIKROKOPTER; |
381 | SpeakHoTT = SPEAK_MIKROKOPTER; |
332 | } |
382 | } |
333 | else |
383 | else |
334 | { |
384 | { |
335 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
385 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
336 | if((x_max - x_min) < MinCaclibration) UART1_PutString("X! "); |
386 | if((x_max - x_min) < MinCaclibration) UART1_PutString("X! "); |
337 | if((y_max - y_min) < MinCaclibration) UART1_PutString("Y! "); |
387 | if((y_max - y_min) < MinCaclibration) UART1_PutString("Y! "); |
338 | if((z_max - z_min) < MinCaclibration) UART1_PutString("Z! "); |
388 | if((z_max - z_min) < MinCaclibration) UART1_PutString("Z! "); |
339 | UART1_PutString("\r\n"); |
389 | UART1_PutString("\r\n"); |
340 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
390 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
341 | } |
391 | } |
342 | UART1_PutString(msg); |
392 | UART1_PutString(msg); |
343 | sprintf(msg, "\r\nX: (%i - %i = %i)\r\n",x_max,x_min,x_max - x_min); |
393 | sprintf(msg, "\r\nX: (%i - %i = %i)\r\n",x_max,x_min,x_max - x_min); |
344 | UART1_PutString(msg); |
394 | UART1_PutString(msg); |
345 | sprintf(msg, "Y: (%i - %i = %i)\r\n",y_max,y_min,y_max - y_min); |
395 | sprintf(msg, "Y: (%i - %i = %i)\r\n",y_max,y_min,y_max - y_min); |
346 | UART1_PutString(msg); |
396 | UART1_PutString(msg); |
347 | sprintf(msg, "Z: (%i - %i = %i)\r\n",z_max,z_min,z_max - z_min); |
397 | sprintf(msg, "Z: (%i - %i = %i)\r\n",z_max,z_min,z_max - z_min); |
348 | UART1_PutString(msg); |
398 | UART1_PutString(msg); |
349 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCaclibration); |
399 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCaclibration); |
350 | UART1_PutString(msg); |
400 | UART1_PutString(msg); |
351 | break; |
401 | break; |
352 | default: |
402 | default: |
353 | break; |
403 | break; |
354 | } |
404 | } |
355 | last_state = Compass_CalState; |
405 | last_state = Compass_CalState; |
356 | } |
406 | } |
357 | TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
407 | TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
358 | } |
408 | } |
359 | } |
409 | } |
360 | 410 |