Rev 493 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 493 | Rev 530 | ||
---|---|---|---|
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 "timer1.h" |
61 | #include "timer1.h" |
62 | #include "led.h" |
62 | #include "led.h" |
63 | #include "main.h" |
63 | #include "main.h" |
64 | #include "uart1.h" |
64 | #include "uart1.h" |
65 | #include "compass.h" |
65 | #include "compass.h" |
66 | #include "spi_slave.h" |
66 | #include "spi_slave.h" |
67 | 67 | ||
68 | #define MK3MAG_SLAVE_ADDRESS 0x50 // i2C slave address |
68 | #define MK3MAG_SLAVE_ADDRESS 0x50 // i2C slave address |
69 | 69 | ||
70 | u8 MK3MAG_Present = 0; |
70 | u8 MK3MAG_Present = 0; |
71 | 71 | ||
72 | typedef struct |
72 | typedef struct |
73 | { |
73 | { |
74 | s16 Nick; |
74 | s16 Nick; |
75 | s16 Roll; |
75 | s16 Roll; |
76 | } __attribute__((packed)) MK3MAG_WriteAttitude_t; |
76 | } __attribute__((packed)) MK3MAG_WriteAttitude_t; |
77 | 77 | ||
78 | typedef struct |
78 | typedef struct |
79 | { |
79 | { |
80 | u8 Major; |
80 | u8 Major; |
81 | u8 Minor; |
81 | u8 Minor; |
82 | u8 Patch; |
82 | u8 Patch; |
83 | u8 Compatible; |
83 | u8 Compatible; |
84 | } __attribute__((packed)) MK3MAG_Version_t; |
84 | } __attribute__((packed)) MK3MAG_Version_t; |
85 | 85 | ||
86 | typedef struct |
86 | typedef struct |
87 | { |
87 | { |
88 | u8 CalByte; |
88 | u8 CalByte; |
89 | u8 Dummy1; |
89 | u8 Dummy1; |
90 | u8 Dummy2; |
90 | u8 Dummy2; |
91 | } __attribute__((packed)) MK3MAG_Cal_t; |
91 | } __attribute__((packed)) MK3MAG_Cal_t; |
92 | 92 | ||
93 | // Transfer buffers |
93 | // Transfer buffers |
94 | volatile MK3MAG_WriteAttitude_t MK3MAG_WriteAttitude; |
94 | volatile MK3MAG_WriteAttitude_t MK3MAG_WriteAttitude; |
95 | volatile MK3MAG_Version_t MK3MAG_Version; |
95 | volatile MK3MAG_Version_t MK3MAG_Version; |
96 | volatile MK3MAG_Cal_t MK3MAG_WriteCal; |
96 | volatile MK3MAG_Cal_t MK3MAG_WriteCal; |
97 | volatile MK3MAG_Cal_t MK3MAG_ReadCal; |
97 | volatile MK3MAG_Cal_t MK3MAG_ReadCal; |
98 | 98 | ||
99 | // ------------------------------------------------------------------------------- |
99 | // ------------------------------------------------------------------------------- |
100 | // the calculation of the MK3MAG packet checksum |
100 | // the calculation of the MK3MAG packet checksum |
101 | 101 | ||
102 | // calculate the crc of bytecount bytes in buffer |
102 | // calculate the crc of bytecount bytes in buffer |
103 | u8 MK3MAG_CalcCRC(u8* pBuffer, u8 bytecount) |
103 | u8 MK3MAG_CalcCRC(u8* pBuffer, u8 bytecount) |
104 | { |
104 | { |
105 | u8 i, crc = 0; |
105 | u8 i, crc = 0; |
106 | for(i=0; i < bytecount; i++) |
106 | for(i=0; i < bytecount; i++) |
107 | { |
107 | { |
108 | crc += pBuffer[i]; |
108 | crc += pBuffer[i]; |
109 | } |
109 | } |
110 | crc = ~crc; |
110 | crc = ~crc; |
111 | return crc; |
111 | return crc; |
112 | } |
112 | } |
113 | 113 | ||
114 | // assuming the last byte in buffer is the crc byte |
114 | // assuming the last byte in buffer is the crc byte |
115 | u8 MK3MAG_CheckCRC(u8* pBuffer, u8 BuffLen) |
115 | u8 MK3MAG_CheckCRC(u8* pBuffer, u8 BuffLen) |
116 | { |
116 | { |
117 | u8 crc = 0, retval = 0; |
117 | u8 crc = 0, retval = 0; |
118 | if(BuffLen == 0) return(retval); |
118 | if(BuffLen == 0) return(retval); |
119 | crc = MK3MAG_CalcCRC(pBuffer, BuffLen-1); |
119 | crc = MK3MAG_CalcCRC(pBuffer, BuffLen-1); |
120 | if(crc == pBuffer[BuffLen-1]) |
120 | if(crc == pBuffer[BuffLen-1]) |
121 | { |
121 | { |
122 | retval = 1; |
122 | retval = 1; |
123 | DebugOut.Analog[15]++; // count mk3mag ok |
123 | DebugOut.Analog[15]++; // count mk3mag ok |
124 | } |
124 | } |
125 | else |
125 | else |
126 | { |
126 | { |
127 | retval = 0; |
127 | retval = 0; |
128 | DebugOut.Analog[14]++; // count mk3mag crc error |
128 | DebugOut.Analog[14]++; // count mk3mag crc error |
129 | } |
129 | } |
130 | return(retval); |
130 | return(retval); |
131 | } |
131 | } |
132 | 132 | ||
133 | // ------------------------------------------------------------------------------- |
133 | // ------------------------------------------------------------------------------- |
134 | // the I2C rx data handler functions |
134 | // the I2C rx data handler functions |
135 | 135 | ||
136 | // rx data handler for version info request |
136 | // rx data handler for version info request |
137 | void MK3MAG_UpdateVersion(u8* pRxBuffer, u8 RxBufferSize) |
137 | void MK3MAG_UpdateVersion(u8* pRxBuffer, u8 RxBufferSize) |
138 | { // if crc is ok and number of byte are matching |
138 | { // if crc is ok and number of byte are matching |
139 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Version)+1)) ) |
139 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_Version)+1)) ) |
140 | { |
140 | { |
141 | memcpy((u8 *)&MK3MAG_Version, pRxBuffer, sizeof(MK3MAG_Version)); |
141 | memcpy((u8 *)&MK3MAG_Version, pRxBuffer, sizeof(MK3MAG_Version)); |
142 | } |
142 | } |
143 | } |
143 | } |
144 | 144 | ||
145 | // rx data handler for calibration request |
145 | // rx data handler for calibration request |
146 | void MK3MAG_UpdateCalibration(u8* pRxBuffer, u8 RxBufferSize) |
146 | void MK3MAG_UpdateCalibration(u8* pRxBuffer, u8 RxBufferSize) |
147 | { // if crc is ok and number of byte are matching |
147 | { // if crc is ok and number of byte are matching |
148 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_ReadCal)+1)) ) |
148 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MK3MAG_ReadCal)+1)) ) |
149 | { |
149 | { |
150 | memcpy((u8 *)&MK3MAG_ReadCal, pRxBuffer, sizeof(MK3MAG_ReadCal)); |
150 | memcpy((u8 *)&MK3MAG_ReadCal, pRxBuffer, sizeof(MK3MAG_ReadCal)); |
151 | } |
151 | } |
152 | } |
152 | } |
153 | 153 | ||
154 | // rx data handler for magnetic vector request |
154 | // rx data handler for magnetic vector request |
155 | void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
155 | void MK3MAG_UpdateMagVector(u8* pRxBuffer, u8 RxBufferSize) |
156 | { // if crc is ok and number of byte are matching |
156 | { // if crc is ok and number of byte are matching |
157 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) ) |
157 | if(MK3MAG_CheckCRC(pRxBuffer, RxBufferSize) && (RxBufferSize == (sizeof(MagVector)+1)) ) |
158 | { |
158 | { |
159 | s16vec_t *pMagVector = (s16vec_t*)pRxBuffer; |
159 | s16vec_t *pMagVector = (s16vec_t*)pRxBuffer; |
160 | MagVector.X = pMagVector->Y; |
160 | MagVector.X = pMagVector->Y; |
161 | MagVector.Y = pMagVector->X; |
161 | MagVector.Y = pMagVector->X; |
162 | MagVector.Z = -pMagVector->Z; |
162 | MagVector.Z = -pMagVector->Z; |
163 | Compass_CalcHeading(); |
163 | Compass_CalcHeading(); |
164 | } |
164 | } |
165 | } |
165 | } |
166 | 166 | ||
167 | //---------------------------------------------------------------- |
167 | //---------------------------------------------------------------- |
168 | #define MK3MAG_CMD_VERSION 0x01 |
168 | #define MK3MAG_CMD_VERSION 0x01 |
169 | #define MK3MAG_CMD_READ_MAGVECT 0x02 |
169 | #define MK3MAG_CMD_READ_MAGVECT 0x02 |
170 | #define MK3MAG_CMD_WRITE_CAL 0x04 |
170 | #define MK3MAG_CMD_WRITE_CAL 0x04 |
171 | 171 | ||
172 | // use I2C1 for communication |
172 | // use I2C1 for communication |
173 | void MK3MAG_SendCommand(u8 command) |
173 | void MK3MAG_SendCommand(u8 command) |
174 | { |
174 | { |
175 | // try to catch the I2C buffer |
175 | // try to catch the I2C buffer |
176 | if(I2CBus_LockBuffer(I2C1, 0)) |
176 | if(I2CBus_LockBuffer(I2C1, 0)) |
177 | { |
177 | { |
178 | u8 TxData[100]; |
178 | u8 TxData[100]; |
179 | u16 TxBytes = 0; |
179 | u16 TxBytes = 0; |
180 | u16 RxBytes = 0; |
180 | u16 RxBytes = 0; |
181 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
181 | I2C_pRxHandler_t pRxHandlerFunc = NULL; |
182 | 182 | ||
183 | // update current command id |
183 | // update current command id |
184 | TxData[TxBytes++] = command; |
184 | TxData[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 |
187 | switch (command) |
187 | switch (command) |
188 | { |
188 | { |
189 | case MK3MAG_CMD_VERSION: |
189 | case MK3MAG_CMD_VERSION: |
190 | RxBytes = sizeof(MK3MAG_Version)+1; |
190 | RxBytes = sizeof(MK3MAG_Version)+1; |
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(TxData+TxBytes, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal)); |
196 | memcpy(TxData+TxBytes, (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; |
202 | break; |
202 | break; |
203 | default: // unknown command id |
203 | default: // unknown command id |
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 | TxData[TxBytes] = MK3MAG_CalcCRC(TxData, TxBytes); |
209 | TxData[TxBytes] = MK3MAG_CalcCRC(TxData, TxBytes); |
210 | TxBytes++; |
210 | TxBytes++; |
211 | // initiate I2C transmission |
211 | // initiate I2C transmission |
212 | I2CBus_Transmission(I2C1, MK3MAG_SLAVE_ADDRESS, TxData, TxBytes, pRxHandlerFunc, RxBytes); |
212 | I2CBus_Transmission(I2C1, MK3MAG_SLAVE_ADDRESS, TxData, TxBytes, pRxHandlerFunc, RxBytes); |
213 | } // EOF I2C_State == I2C_IDLE |
213 | } // EOF I2C_State == I2C_IDLE |
214 | } |
214 | } |
215 | 215 | ||
216 | 216 | ||
217 | //---------------------------------------------------------------- |
217 | //---------------------------------------------------------------- |
218 | u8 MK3MAG_Init(void) |
218 | u8 MK3MAG_Init(void) |
219 | { |
219 | { |
220 | if(MK3MAG_Present) // do only short init ! , full init was called before |
220 | if(MK3MAG_Present) // do only short init ! , full init was called before |
221 | { |
221 | { |
222 | // try reconnect by reseting the I2C bus |
222 | // try reconnect by reseting the I2C bus |
223 | I2CBus_Deinit(I2C1); |
223 | I2CBus_Deinit(I2C1); |
224 | I2CBus_Init(I2C1); |
224 | I2CBus_Init(I2C1); |
225 | } |
225 | } |
226 | else // full init |
226 | else // full init |
227 | { |
227 | { |
228 | u8 msg[64]; |
228 | u8 msg[64]; |
229 | u8 repeat; |
229 | u8 repeat; |
230 | u32 timeout; |
230 | u32 timeout; |
231 | 231 | ||
232 | Compass_I2CPort = I2C1; |
232 | Compass_I2CPort = I2C1; |
233 | MK3MAG_Present = 0; |
233 | MK3MAG_Present = 0; |
234 | 234 | ||
235 | MK3MAG_Version.Major = 0xFF; |
235 | MK3MAG_Version.Major = 0xFF; |
236 | MK3MAG_Version.Minor = 0xFF; |
236 | MK3MAG_Version.Minor = 0xFF; |
237 | MK3MAG_Version.Patch = 0xFF; |
237 | MK3MAG_Version.Patch = 0xFF; |
238 | MK3MAG_Version.Compatible = 0xFF; |
238 | MK3MAG_Version.Compatible = 0xFF; |
239 | 239 | ||
240 | // polling of version info |
240 | // polling of version info |
241 | repeat = 0; |
241 | repeat = 0; |
242 | do |
242 | do |
243 | { |
243 | { |
244 | MK3MAG_SendCommand(MK3MAG_CMD_VERSION); |
244 | MK3MAG_SendCommand(MK3MAG_CMD_VERSION); |
245 | if((Version_HW & 0x7F) > 11) timeout = SetDelay(100); |
245 | if(UART_VersionInfo.HWMajor > 11) timeout = SetDelay(100); |
246 | else timeout = SetDelay(250); |
246 | else timeout = SetDelay(250); |
247 | 247 | ||
248 | do |
248 | do |
249 | { |
249 | { |
250 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
250 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
251 | }while (!CheckDelay(timeout)); |
251 | }while (!CheckDelay(timeout)); |
252 | UART1_PutString("."); |
252 | UART1_PutString("."); |
253 | repeat++; |
253 | repeat++; |
254 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
254 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
255 | // if we got it |
255 | // if we got it |
256 | if (MK3MAG_Version.Major != 0xFF) |
256 | if (MK3MAG_Version.Major != 0xFF) |
257 | { |
257 | { |
258 | sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
258 | sprintf(msg, " MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
259 | UART1_PutString(msg); |
259 | UART1_PutString(msg); |
260 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
260 | if(MK3MAG_Version.Compatible != MK3MAG_I2C_COMPATIBLE) |
261 | { |
261 | { |
262 | UART1_PutString("\n\r MK3MAG not compatible!"); |
262 | UART1_PutString("\n\r MK3MAG not compatible!"); |
263 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
263 | UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_INCOMPATIBLE; |
264 | LED_RED_ON; |
264 | LED_RED_ON; |
265 | } |
265 | } |
266 | else |
266 | else |
267 | { // version ok |
267 | { // version ok |
268 | MK3MAG_Present = 1; |
268 | MK3MAG_Present = 1; |
269 | } |
269 | } |
270 | } |
270 | } |
271 | } |
271 | } |
272 | return(MK3MAG_Present); |
272 | return(MK3MAG_Present); |
273 | } |
273 | } |
274 | 274 | ||
275 | //---------------------------------------------------------------- |
275 | //---------------------------------------------------------------- |
276 | void MK3MAG_Update(void) |
276 | void MK3MAG_Update(void) |
277 | { |
277 | { |
278 | static u32 TimerUpdate = 0; |
278 | static u32 TimerUpdate = 0; |
279 | static s16 x_max,y_max,z_max,x_min,y_min,z_min; |
279 | static s16 x_max,y_max,z_max,x_min,y_min,z_min; |
280 | static u8 last_state,speak = 0; |
280 | static u8 last_state,speak = 0; |
281 | u8 msg[64]; |
281 | u8 msg[64]; |
282 | u16 MinCaclibration = 500; |
282 | u16 MinCaclibration = 500; |
283 | 283 | ||
284 | if( (I2CBus(I2C1)->State == I2C_STATE_UNDEF) || !MK3MAG_Present ) return; |
284 | if( (I2CBus(I2C1)->State == I2C_STATE_UNDEF) || !MK3MAG_Present ) return; |
285 | 285 | ||
286 | if(CheckDelay(TimerUpdate)) |
286 | if(CheckDelay(TimerUpdate)) |
287 | { |
287 | { |
288 | // check for incomming compass calibration request |
288 | // check for incomming compass calibration request |
289 | Compass_UpdateCalState(); |
289 | Compass_UpdateCalState(); |
290 | MK3MAG_WriteCal.CalByte = Compass_CalState; |
290 | MK3MAG_WriteCal.CalByte = Compass_CalState; |
291 | 291 | ||
292 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
292 | if(MK3MAG_ReadCal.CalByte != MK3MAG_WriteCal.CalByte) |
293 | { // send new calibration state |
293 | { // send new calibration state |
294 | MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL); |
294 | MK3MAG_SendCommand(MK3MAG_CMD_WRITE_CAL); |
295 | } |
295 | } |
296 | else |
296 | else |
297 | { // calibration state matches |
297 | { // calibration state matches |
298 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); // initiate magvector transfer |
298 | MK3MAG_SendCommand(MK3MAG_CMD_READ_MAGVECT); // initiate magvector transfer |
299 | 299 | ||
300 | switch(Compass_CalState) |
300 | switch(Compass_CalState) |
301 | { |
301 | { |
302 | case 1: |
302 | case 1: |
303 | if(last_state != Compass_CalState) |
303 | if(last_state != Compass_CalState) |
304 | { |
304 | { |
305 | UART1_PutString("\r\nMK3Mag calibration\n\r"); |
305 | UART1_PutString("\r\nMK3Mag calibration\n\r"); |
306 | if(EarthMagneticStrengthTheoretic) |
306 | if(EarthMagneticStrengthTheoretic) |
307 | { |
307 | { |
308 | MinCaclibration = (MinCaclibration * EarthMagneticStrengthTheoretic) / 50; |
308 | MinCaclibration = (MinCaclibration * EarthMagneticStrengthTheoretic) / 50; |
309 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
309 | sprintf(msg, "Earth field on your location should be: %iuT\r\n",EarthMagneticStrengthTheoretic); |
310 | UART1_PutString(msg); |
310 | UART1_PutString(msg); |
311 | } |
311 | } |
312 | else UART1_PutString("without GPS\n\r"); |
312 | else UART1_PutString("without GPS\n\r"); |
313 | } |
313 | } |
314 | x_max = -30000; y_max = -30000; z_max = -30000; |
314 | x_max = -30000; y_max = -30000; z_max = -30000; |
315 | x_min = 30000; y_min = 30000; z_min = 30000; |
315 | x_min = 30000; y_min = 30000; z_min = 30000; |
316 | speak = 1; |
316 | speak = 1; |
317 | break; |
317 | break; |
318 | case 2: |
318 | case 2: |
319 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
319 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
320 | 320 | ||
321 | if(MagVector.X > x_max) { x_max = MagVector.X; BeepTime = 60; } |
321 | if(MagVector.X > x_max) { x_max = MagVector.X; BeepTime = 60; } |
322 | else if(MagVector.X < x_min) { x_min = MagVector.X; BeepTime = 20; } |
322 | else if(MagVector.X < x_min) { x_min = MagVector.X; BeepTime = 20; } |
323 | if(MagVector.Y > y_max) { y_max = MagVector.Y; BeepTime = 60; } |
323 | if(MagVector.Y > y_max) { y_max = MagVector.Y; BeepTime = 60; } |
324 | else if(MagVector.Y < y_min) { y_min = MagVector.Y; BeepTime = 20; } |
324 | else if(MagVector.Y < y_min) { y_min = MagVector.Y; BeepTime = 20; } |
325 | 325 | ||
326 | break; |
326 | break; |
327 | case 3: |
327 | case 3: |
328 | speak = 1; |
328 | speak = 1; |
329 | break; |
329 | break; |
330 | case 4: |
330 | case 4: |
331 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
331 | if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0; |
332 | if(MagVector.Z > z_max) { z_max = MagVector.Z; BeepTime = 80; } |
332 | if(MagVector.Z > z_max) { z_max = MagVector.Z; BeepTime = 80; } |
333 | else if(MagVector.Z < z_min) { z_min = MagVector.Z; BeepTime = 80; } |
333 | else if(MagVector.Z < z_min) { z_min = MagVector.Z; BeepTime = 80; } |
334 | break; |
334 | break; |
335 | case 5: |
335 | case 5: |
336 | if(last_state == Compass_CalState) break; |
336 | if(last_state == Compass_CalState) break; |
337 | if(((x_max - x_min) > MinCaclibration) && ((y_max - y_min) > MinCaclibration) && ((z_max - z_min) > MinCaclibration)) |
337 | if(((x_max - x_min) > MinCaclibration) && ((y_max - y_min) > MinCaclibration) && ((z_max - z_min) > MinCaclibration)) |
338 | { |
338 | { |
339 | BeepTime = 2500; |
339 | BeepTime = 2500; |
340 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
340 | UART1_PutString("\r\n-> Calibration okay <-\n\r"); |
341 | SpeakHoTT = SPEAK_MIKROKOPTER; |
341 | SpeakHoTT = SPEAK_MIKROKOPTER; |
342 | } |
342 | } |
343 | else |
343 | else |
344 | { |
344 | { |
345 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
345 | UART1_PutString("\r\nCalibration FAILED - Values too low: "); |
346 | if((x_max - x_min) < MinCaclibration) UART1_PutString("X! "); |
346 | if((x_max - x_min) < MinCaclibration) UART1_PutString("X! "); |
347 | if((y_max - y_min) < MinCaclibration) UART1_PutString("Y! "); |
347 | if((y_max - y_min) < MinCaclibration) UART1_PutString("Y! "); |
348 | if((z_max - z_min) < MinCaclibration) UART1_PutString("Z! "); |
348 | if((z_max - z_min) < MinCaclibration) UART1_PutString("Z! "); |
349 | UART1_PutString("\r\n"); |
349 | UART1_PutString("\r\n"); |
350 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
350 | SpeakHoTT = SPEAK_ERR_CALIBARTION; |
351 | } |
351 | } |
352 | UART1_PutString(msg); |
352 | UART1_PutString(msg); |
353 | sprintf(msg, "\r\nX: (%i - %i = %i)\r\n",x_max,x_min,x_max - x_min); |
353 | sprintf(msg, "\r\nX: (%i - %i = %i)\r\n",x_max,x_min,x_max - x_min); |
354 | UART1_PutString(msg); |
354 | UART1_PutString(msg); |
355 | sprintf(msg, "Y: (%i - %i = %i)\r\n",y_max,y_min,y_max - y_min); |
355 | sprintf(msg, "Y: (%i - %i = %i)\r\n",y_max,y_min,y_max - y_min); |
356 | UART1_PutString(msg); |
356 | UART1_PutString(msg); |
357 | sprintf(msg, "Z: (%i - %i = %i)\r\n",z_max,z_min,z_max - z_min); |
357 | sprintf(msg, "Z: (%i - %i = %i)\r\n",z_max,z_min,z_max - z_min); |
358 | UART1_PutString(msg); |
358 | UART1_PutString(msg); |
359 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCaclibration); |
359 | sprintf(msg, "(Minimum ampilitude is: %i)\r\n",MinCaclibration); |
360 | UART1_PutString(msg); |
360 | UART1_PutString(msg); |
361 | break; |
361 | break; |
362 | default: |
362 | default: |
363 | break; |
363 | break; |
364 | } |
364 | } |
365 | last_state = Compass_CalState; |
365 | last_state = Compass_CalState; |
366 | } |
366 | } |
367 | TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
367 | TimerUpdate = SetDelay(20); // every 20 ms are 50 Hz |
368 | } |
368 | } |
369 | } |
369 | } |
370 | 370 |