Rev 171 | Rev 189 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 171 | Rev 184 | ||
---|---|---|---|
1 | /*#######################################################################################*/ |
1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
3 | /*#######################################################################################*/ |
3 | /*#######################################################################################*/ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
7 | // + FOR NON COMMERCIAL USE ONLY |
8 | // + www.MikroKopter.com |
8 | // + www.MikroKopter.com |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
10 | // + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation), |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
11 | // + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist. |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
15 | // + Verkauf von Luftbildaufnahmen, usw. |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
18 | // + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
20 | // + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
21 | // + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de" |
22 | // + eindeutig als Ursprung verlinkt werden |
22 | // + eindeutig als Ursprung verlinkt werden |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
25 | // + Benutzung auf eigene Gefahr |
25 | // + Benutzung auf eigene Gefahr |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
29 | // + mit unserer Zustimmung zulässig |
29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
34 | // + this list of conditions and the following disclaimer. |
34 | // + this list of conditions and the following disclaimer. |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
35 | // + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived |
36 | // + from this software without specific prior written permission. |
36 | // + from this software without specific prior written permission. |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
38 | // + for non-commercial use (directly or indirectly) |
38 | // + for non-commercial use (directly or indirectly) |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
40 | // + with our written permission |
40 | // + with our written permission |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
42 | // + clearly linked as origin |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
44 | // |
44 | // |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
45 | // + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
46 | // + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
51 | // + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
52 | // + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
53 | // + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
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 <stdlib.h> |
57 | #include <stdlib.h> |
58 | #include <string.h> |
58 | #include <string.h> |
59 | #include "91x_lib.h" |
59 | #include "91x_lib.h" |
60 | #include "i2c.h" |
60 | #include "i2c.h" |
61 | #include "uart1.h" |
61 | #include "uart1.h" |
62 | #include "timer1.h" |
62 | #include "timer1.h" |
63 | #include "config.h" |
63 | #include "config.h" |
64 | #include "main.h" |
64 | #include "main.h" |
65 | #include "led.h" |
65 | #include "led.h" |
66 | #include "spi_slave.h" |
66 | #include "spi_slave.h" |
67 | 67 | ||
68 | 68 | ||
69 | volatile I2C_State_t I2C_State = I2C_OFF; |
69 | volatile I2C_State_t I2C_State = I2C_OFF; |
70 | volatile u8 I2C_StopPolling = 1; |
70 | volatile u8 I2C_StopPolling = 1; |
71 | 71 | ||
72 | // rxbuffer |
72 | // rxbuffer |
73 | volatile u8 I2C_RxBufferSize; |
73 | volatile u8 I2C_RxBufferSize; |
74 | volatile u8 *I2C_RxBuffer; |
74 | volatile u8 *I2C_RxBuffer; |
75 | volatile u8 Rx_Idx = 0; |
75 | volatile u8 Rx_Idx = 0; |
76 | // txbuffer |
76 | // txbuffer |
77 | volatile u8 I2C_TxBufferSize; |
77 | volatile u8 I2C_TxBufferSize; |
78 | volatile u8 *I2C_TxBuffer; |
78 | volatile u8 *I2C_TxBuffer; |
79 | volatile u8 Tx_Idx = 0; |
79 | volatile u8 Tx_Idx = 0; |
80 | 80 | ||
81 | volatile u8 I2C_Direction; |
81 | volatile u8 I2C_Direction; |
82 | volatile u8 I2C_Command; |
82 | volatile u8 I2C_Command; |
83 | 83 | ||
84 | volatile I2C_Heading_t I2C_Heading; |
84 | volatile I2C_Heading_t I2C_Heading; |
85 | volatile I2C_WriteAttitude_t I2C_WriteAttitude; |
85 | volatile I2C_WriteAttitude_t I2C_WriteAttitude; |
86 | volatile I2C_Mag_t I2C_Mag; |
86 | volatile I2C_Mag_t I2C_Mag; |
87 | volatile I2C_Version_t MK3MAG_Version; |
87 | volatile I2C_Version_t MK3MAG_Version; |
88 | volatile I2C_Cal_t I2C_WriteCal; |
88 | volatile I2C_Cal_t I2C_WriteCal; |
89 | volatile I2C_Cal_t I2C_ReadCal; |
89 | volatile I2C_Cal_t I2C_ReadCal; |
90 | 90 | ||
91 | #define I2C1_TIMEOUT 500 // 500 ms |
91 | #define I2C1_TIMEOUT 500 // 500 ms |
92 | volatile u32 I2C1_Timeout = 0; |
92 | volatile u32 I2C1_Timeout = 0; |
93 | volatile u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers |
93 | volatile u8 I2C_PrimRxBuffer[10]; // must be larger than any of the secondary rx buffers |
94 | 94 | ||
95 | //-------------------------------------------------------------- |
95 | //-------------------------------------------------------------- |
96 | void I2C1_Init(void) |
96 | void I2C1_Init(void) |
97 | { |
97 | { |
98 | I2C_InitTypeDef I2C_Struct; |
98 | I2C_InitTypeDef I2C_Struct; |
99 | GPIO_InitTypeDef GPIO_InitStructure; |
99 | GPIO_InitTypeDef GPIO_InitStructure; |
100 | 100 | ||
101 | I2C_State = I2C_OFF; |
101 | I2C_State = I2C_OFF; |
102 | 102 | ||
103 | UART1_PutString("\r\n I2C init..."); |
103 | UART1_PutString("\r\n I2C init..."); |
104 | // enable Port 2 peripherie |
104 | // enable Port 2 peripherie |
105 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
105 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
106 | // disable a reset state |
106 | // disable a reset state |
107 | SCU_APBPeriphReset(__GPIO2, DISABLE); |
107 | SCU_APBPeriphReset(__GPIO2, DISABLE); |
108 | 108 | ||
109 | // free a busy bus |
109 | // free a busy bus |
110 | 110 | ||
111 | // At switch on I2C devices can get in a state where they |
111 | // At switch on I2C devices can get in a state where they |
112 | // are still waiting for a command due to all the bus lines bouncing |
112 | // are still waiting for a command due to all the bus lines bouncing |
113 | // around at startup have started clocking data into the device(s). |
113 | // around at startup have started clocking data into the device(s). |
114 | // Enable the ports as open collector port outputs |
114 | // Enable the ports as open collector port outputs |
115 | // and clock out at least 9 SCL pulses, then generate a stop |
115 | // and clock out at least 9 SCL pulses, then generate a stop |
116 | // condition and then leave the clock line high. |
116 | // condition and then leave the clock line high. |
117 | 117 | ||
118 | // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation |
118 | // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation |
119 | GPIO_StructInit(&GPIO_InitStructure); |
119 | GPIO_StructInit(&GPIO_InitStructure); |
120 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
120 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
121 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
121 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
122 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
122 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
123 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
123 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
124 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
124 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
125 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
125 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
126 | 126 | ||
127 | u8 i; |
127 | u8 i; |
128 | u32 delay; |
128 | u32 delay; |
129 | // set SCL high and then SDA to low (start condition) |
129 | // set SCL high and then SDA to low (start condition) |
130 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
130 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
131 | delay = SetDelay(1); |
131 | delay = SetDelay(1); |
132 | while (!CheckDelay(delay)); |
132 | while (!CheckDelay(delay)); |
133 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET); |
133 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET); |
134 | // toggle SCL at least 10 times from high to low to high |
134 | // toggle SCL at least 10 times from high to low to high |
135 | for(i = 0; i < 10; i++) |
135 | for(i = 0; i < 10; i++) |
136 | { |
136 | { |
137 | delay = SetDelay(1); |
137 | delay = SetDelay(1); |
138 | while (!CheckDelay(delay)); |
138 | while (!CheckDelay(delay)); |
139 | 139 | ||
140 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET); |
140 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET); |
141 | delay = SetDelay(1); |
141 | delay = SetDelay(1); |
142 | while (!CheckDelay(delay)); |
142 | while (!CheckDelay(delay)); |
143 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
143 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
144 | } |
144 | } |
145 | delay = SetDelay(1); |
145 | delay = SetDelay(1); |
146 | while (!CheckDelay(delay)); |
146 | while (!CheckDelay(delay)); |
147 | // create stop condition setting SDA HIGH when SCL is HIGH |
147 | // create stop condition setting SDA HIGH when SCL is HIGH |
148 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET); |
148 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET); |
149 | 149 | ||
150 | 150 | ||
151 | // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT |
151 | // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT |
152 | GPIO_StructInit(&GPIO_InitStructure); |
152 | GPIO_StructInit(&GPIO_InitStructure); |
153 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
153 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
154 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
154 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
155 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
155 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
156 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
156 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
157 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT |
157 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT |
158 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
158 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
159 | 159 | ||
160 | // enable I2C peripherie |
160 | // enable I2C peripherie |
161 | SCU_APBPeriphClockConfig(__I2C1,ENABLE); |
161 | SCU_APBPeriphClockConfig(__I2C1,ENABLE); |
162 | // reset I2C peripherie |
162 | // reset I2C peripherie |
163 | SCU_APBPeriphReset(__I2C1,ENABLE); |
163 | SCU_APBPeriphReset(__I2C1,ENABLE); |
164 | SCU_APBPeriphReset(__I2C1,DISABLE); |
164 | SCU_APBPeriphReset(__I2C1,DISABLE); |
165 | 165 | ||
166 | I2C_DeInit(I2C1); |
166 | I2C_DeInit(I2C1); |
167 | I2C_StructInit(&I2C_Struct); |
167 | I2C_StructInit(&I2C_Struct); |
168 | I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; |
168 | I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; |
169 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
169 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
170 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
170 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
171 | I2C_Struct.I2C_OwnAddress = 0x00; |
171 | I2C_Struct.I2C_OwnAddress = 0x00; |
172 | I2C_Init(I2C1, &I2C_Struct); |
172 | I2C_Init(I2C1, &I2C_Struct); |
173 | 173 | ||
174 | I2C_TxBuffer = NULL; |
174 | I2C_TxBuffer = NULL; |
175 | Tx_Idx = 0; |
175 | Tx_Idx = 0; |
176 | I2C_TxBufferSize = 0; |
176 | I2C_TxBufferSize = 0; |
177 | 177 | ||
178 | I2C_RxBuffer = NULL; |
178 | I2C_RxBuffer = NULL; |
179 | Rx_Idx = 0; |
179 | Rx_Idx = 0; |
180 | I2C_RxBufferSize = 0; |
180 | I2C_RxBufferSize = 0; |
181 | 181 | ||
182 | I2C_Cmd(I2C1, ENABLE); |
182 | I2C_Cmd(I2C1, ENABLE); |
183 | I2C_ITConfig(I2C1, ENABLE); |
183 | I2C_ITConfig(I2C1, ENABLE); |
184 | 184 | ||
185 | VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
185 | VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
186 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
186 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
187 | 187 | ||
188 | I2C1_Timeout = SetDelay(2*I2C1_TIMEOUT); |
188 | I2C1_Timeout = SetDelay(2*I2C1_TIMEOUT); |
189 | I2C_Heading.Heading = -1; |
189 | I2C_Heading.Heading = -1; |
190 | I2C_GenerateSTOP(I2C1, ENABLE); |
190 | I2C_GenerateSTOP(I2C1, ENABLE); |
191 | I2C_State = I2C_IDLE; |
191 | I2C_State = I2C_IDLE; |
192 | 192 | ||
193 | I2C_StopPolling = 0; // start polling |
193 | I2C_StopPolling = 0; // start polling |
194 | 194 | ||
195 | UART1_PutString("ok"); |
195 | UART1_PutString("ok"); |
196 | } |
196 | } |
197 | 197 | ||
198 | 198 | ||
199 | //-------------------------------------------------------------- |
199 | //-------------------------------------------------------------- |
200 | void I2C1_Deinit(void) |
200 | void I2C1_Deinit(void) |
201 | { |
201 | { |
202 | GPIO_InitTypeDef GPIO_InitStructure; |
202 | GPIO_InitTypeDef GPIO_InitStructure; |
203 | I2C_StopPolling = 1;// stop polling |
203 | I2C_StopPolling = 1;// stop polling |
204 | UART1_PutString("\r\n I2C deinit..."); |
204 | UART1_PutString("\r\n I2C deinit..."); |
205 | I2C_GenerateStart(I2C1, DISABLE); |
205 | I2C_GenerateStart(I2C1, DISABLE); |
206 | I2C_GenerateSTOP(I2C1, ENABLE); |
206 | I2C_GenerateSTOP(I2C1, ENABLE); |
207 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
207 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
208 | I2C_State = I2C_OFF; |
208 | I2C_State = I2C_OFF; |
209 | I2C_ITConfig(I2C1, DISABLE); |
209 | I2C_ITConfig(I2C1, DISABLE); |
210 | I2C_Cmd(I2C1, DISABLE); |
210 | I2C_Cmd(I2C1, DISABLE); |
211 | I2C_DeInit(I2C1); |
211 | I2C_DeInit(I2C1); |
212 | SCU_APBPeriphClockConfig(__I2C1, DISABLE); |
212 | SCU_APBPeriphClockConfig(__I2C1, DISABLE); |
213 | 213 | ||
214 | // set ports to input |
214 | // set ports to input |
215 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
215 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
216 | GPIO_StructInit(&GPIO_InitStructure); |
216 | GPIO_StructInit(&GPIO_InitStructure); |
217 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
217 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
218 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
218 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
219 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
219 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
220 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
220 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
221 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
221 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
222 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
222 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
223 | 223 | ||
224 | I2C_TxBuffer = NULL; |
224 | I2C_TxBuffer = NULL; |
225 | Tx_Idx = 0; |
225 | Tx_Idx = 0; |
226 | I2C_TxBufferSize = 0; |
226 | I2C_TxBufferSize = 0; |
227 | 227 | ||
228 | I2C_RxBuffer = NULL; |
228 | I2C_RxBuffer = NULL; |
229 | Rx_Idx = 0; |
229 | Rx_Idx = 0; |
230 | I2C_RxBufferSize = 0; |
230 | I2C_RxBufferSize = 0; |
231 | 231 | ||
232 | I2C1_Timeout = 0; |
232 | I2C1_Timeout = 0; |
233 | I2C_Heading.Heading = -1; |
233 | I2C_Heading.Heading = -1; |
234 | 234 | ||
235 | UART1_PutString("ok"); |
235 | UART1_PutString("ok"); |
236 | } |
236 | } |
237 | 237 | ||
238 | 238 | ||
239 | //-------------------------------------------------------------- |
239 | //-------------------------------------------------------------- |
240 | void I2C1_IRQHandler(void) |
240 | void I2C1_IRQHandler(void) |
241 | { |
241 | { |
242 | u16 status; |
242 | u16 status; |
243 | static u8 crc; |
243 | static u8 crc; |
244 | // detemine I2C State |
244 | // detemine I2C State |
245 | status = I2C_GetLastEvent(I2C1); |
245 | status = I2C_GetLastEvent(I2C1); |
246 | 246 | ||
247 | if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured |
247 | if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured |
248 | { // Set and subsequently clear the STOP bit while BTF is set. |
248 | { // Set and subsequently clear the STOP bit while BTF is set. |
249 | while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
249 | while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
250 | { |
250 | { |
251 | I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
251 | I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
252 | I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
252 | I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
253 | } |
253 | } |
254 | I2C_State = I2C_IDLE; |
254 | I2C_State = I2C_IDLE; |
255 | LED_GRN_OFF; |
255 | LED_GRN_OFF; |
256 | } |
256 | } |
257 | else |
257 | else |
258 | { // depending on current i2c state |
258 | { // depending on current i2c state |
259 | switch (status) |
259 | switch (status) |
260 | { |
260 | { |
261 | // the start condition was initiated on the bus |
261 | // the start condition was initiated on the bus |
262 | case I2C_EVENT_MASTER_MODE_SELECT: |
262 | case I2C_EVENT_MASTER_MODE_SELECT: |
263 | LED_GRN_ON; |
263 | LED_GRN_ON; |
264 | // update current bus state variable |
264 | // update current bus state variable |
265 | switch(I2C_Direction) |
265 | switch(I2C_Direction) |
266 | { |
266 | { |
267 | case I2C_MODE_TRANSMITTER: |
267 | case I2C_MODE_TRANSMITTER: |
268 | I2C_State = I2C_TX_PROGRESS; |
268 | I2C_State = I2C_TX_PROGRESS; |
269 | break; |
269 | break; |
270 | 270 | ||
271 | case I2C_MODE_RECEIVER: |
271 | case I2C_MODE_RECEIVER: |
272 | if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0)) |
272 | if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0)) |
273 | { |
273 | { |
274 | I2C_GenerateSTOP (I2C1, ENABLE); |
274 | I2C_GenerateSTOP (I2C1, ENABLE); |
275 | I2C_State = I2C_IDLE; |
275 | I2C_State = I2C_IDLE; |
276 | return; |
276 | return; |
277 | } |
277 | } |
278 | else |
278 | else |
279 | { |
279 | { |
280 | I2C_State = I2C_RX_PROGRESS; |
280 | I2C_State = I2C_RX_PROGRESS; |
281 | } |
281 | } |
282 | break; |
282 | break; |
283 | 283 | ||
284 | default: // invalid direction |
284 | default: // invalid direction |
285 | I2C_GenerateSTOP (I2C1, ENABLE); |
285 | I2C_GenerateSTOP (I2C1, ENABLE); |
286 | I2C_State = I2C_IDLE; |
286 | I2C_State = I2C_IDLE; |
287 | LED_GRN_OFF; |
287 | LED_GRN_OFF; |
288 | return; |
288 | return; |
289 | } |
289 | } |
290 | // enable acknowledge |
290 | // enable acknowledge |
291 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
291 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
292 | // send address/direction byte on the bus |
292 | // send address/direction byte on the bus |
293 | I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction); |
293 | I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction); |
294 | break; |
294 | break; |
295 | 295 | ||
296 | // the address byte was send |
296 | // the address byte was send |
297 | case I2C_EVENT_MASTER_MODE_SELECTED: |
297 | case I2C_EVENT_MASTER_MODE_SELECTED: |
298 | // Clear EV6 by set again the PE bit |
298 | // Clear EV6 by set again the PE bit |
299 | I2C_Cmd(I2C1, ENABLE); |
299 | I2C_Cmd(I2C1, ENABLE); |
300 | // reset checksum |
300 | // reset checksum |
301 | crc = 0; |
301 | crc = 0; |
302 | switch(I2C_State) |
302 | switch(I2C_State) |
303 | { |
303 | { |
304 | case I2C_TX_PROGRESS: |
304 | case I2C_TX_PROGRESS: |
305 | // send command 1st data byte (allways the command id) |
305 | // send command 1st data byte (allways the command id) |
306 | I2C_SendData(I2C1, I2C_Command); |
306 | I2C_SendData(I2C1, I2C_Command); |
307 | crc += I2C_Command; |
307 | crc += I2C_Command; |
308 | Tx_Idx = 0; |
308 | Tx_Idx = 0; |
309 | // reset timeout |
309 | // reset timeout |
310 | I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset |
310 | I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset |
311 | break; |
311 | break; |
312 | 312 | ||
313 | case I2C_RX_PROGRESS: |
313 | case I2C_RX_PROGRESS: |
314 | Rx_Idx = 0; |
314 | Rx_Idx = 0; |
315 | break; |
315 | break; |
316 | 316 | ||
317 | default: // unknown I2C state |
317 | default: // unknown I2C state |
318 | // should never happen |
318 | // should never happen |
319 | I2C_GenerateSTOP (I2C1, ENABLE); |
319 | I2C_GenerateSTOP (I2C1, ENABLE); |
320 | I2C_State = I2C_IDLE; |
320 | I2C_State = I2C_IDLE; |
321 | break; |
321 | break; |
322 | } |
322 | } |
323 | break; |
323 | break; |
324 | 324 | ||
325 | // the master has transmitted a byte and slave has been acknowledged |
325 | // the master has transmitted a byte and slave has been acknowledged |
326 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
326 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
327 | 327 | ||
328 | // some bytes have to be transmitted |
328 | // some bytes have to be transmitted |
329 | if(Tx_Idx < I2C_TxBufferSize) |
329 | if(Tx_Idx < I2C_TxBufferSize) |
330 | { |
330 | { |
331 | if(I2C_TxBuffer != NULL) |
331 | if(I2C_TxBuffer != NULL) |
332 | { |
332 | { |
333 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
333 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
334 | crc += I2C_TxBuffer[Tx_Idx]; |
334 | crc += I2C_TxBuffer[Tx_Idx]; |
335 | } |
335 | } |
336 | else |
336 | else |
337 | { |
337 | { |
338 | I2C_SendData(I2C1, 0x00); |
338 | I2C_SendData(I2C1, 0x00); |
339 | } |
339 | } |
340 | } |
340 | } |
341 | else // the last tx buffer byte was send |
341 | else // the last tx buffer byte was send |
342 | { |
342 | { |
343 | // send crc byte at the end |
343 | // send crc byte at the end |
344 | crc = ~crc; // flip all bits in the checksum |
344 | crc = ~crc; // flip all bits in the checksum |
345 | I2C_SendData(I2C1, crc); |
345 | I2C_SendData(I2C1, crc); |
346 | // generate stop or repeated start condition |
346 | // generate stop or repeated start condition |
347 | if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected? |
347 | if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected? |
348 | { |
348 | { |
349 | I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
349 | I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
350 | I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
350 | I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
351 | } |
351 | } |
352 | else |
352 | else |
353 | { // stop communication |
353 | { // stop communication |
354 | I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
354 | I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
355 | I2C_State = I2C_IDLE; // ready for new actions |
355 | I2C_State = I2C_IDLE; // ready for new actions |
356 | LED_GRN_OFF; |
356 | LED_GRN_OFF; |
357 | DebugOut.Analog[15]++; |
357 | DebugOut.Analog[15]++; |
358 | } |
358 | } |
359 | } |
359 | } |
360 | Tx_Idx++; |
360 | Tx_Idx++; |
361 | break; |
361 | break; |
362 | 362 | ||
363 | // the master has received a byte from the slave |
363 | // the master has received a byte from the slave |
364 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
364 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
365 | // some bytes have to be received |
365 | // some bytes have to be received |
366 | if (Rx_Idx < I2C_RxBufferSize) |
366 | if (Rx_Idx < I2C_RxBufferSize) |
367 | { // copy received byte from the data register to the rx-buffer |
367 | { // copy received byte from the data register to the rx-buffer |
368 | I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
368 | I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
369 | // update checksum |
369 | // update checksum |
370 | crc += I2C_PrimRxBuffer[Rx_Idx]; |
370 | crc += I2C_PrimRxBuffer[Rx_Idx]; |
371 | } |
371 | } |
372 | // if the last byte (crc) was received |
372 | // if the last byte (crc) was received |
373 | else if ( Rx_Idx == I2C_RxBufferSize) |
373 | else if ( Rx_Idx == I2C_RxBufferSize) |
374 | { |
374 | { |
375 | // generate a STOP condition on the bus before reading data register |
375 | // generate a STOP condition on the bus before reading data register |
376 | I2C_GenerateSTOP(I2C1, ENABLE); |
376 | I2C_GenerateSTOP(I2C1, ENABLE); |
377 | // compare last byte with checksum |
377 | // compare last byte with checksum |
378 | crc = ~crc;// flip all bits in calulated checksum |
378 | crc = ~crc;// flip all bits in calulated checksum |
379 | if(crc == I2C_ReceiveData(I2C1)) |
379 | if(crc == I2C_ReceiveData(I2C1)) |
380 | { // copy primary rx buffer content to rx buffer if exist |
380 | { // copy primary rx buffer content to rx buffer if exist |
381 | if(I2C_RxBuffer != NULL) |
381 | if(I2C_RxBuffer != NULL) |
382 | { |
382 | { |
383 | memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize); |
383 | memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize); |
384 | } |
384 | } |
385 | I2C1_Timeout = SetDelay(500); |
385 | I2C1_Timeout = SetDelay(500); |
386 | DebugOut.Analog[15]++; |
386 | DebugOut.Analog[15]++; |
387 | } |
387 | } |
388 | else // checksum error detected |
388 | else // checksum error detected |
389 | { |
389 | { |
390 | DebugOut.Analog[14]++; |
390 | // DebugOut.Analog[14]++; |
391 | } |
391 | } |
392 | I2C_State = I2C_IDLE; |
392 | I2C_State = I2C_IDLE; |
393 | LED_GRN_OFF; |
393 | LED_GRN_OFF; |
394 | } |
394 | } |
395 | Rx_Idx++; |
395 | Rx_Idx++; |
396 | // if the 2nd last byte was received disable acknowledge for the last one |
396 | // if the 2nd last byte was received disable acknowledge for the last one |
397 | if ( Rx_Idx == I2C_RxBufferSize ) |
397 | if ( Rx_Idx == I2C_RxBufferSize ) |
398 | { |
398 | { |
399 | I2C_AcknowledgeConfig (I2C1, DISABLE); |
399 | I2C_AcknowledgeConfig (I2C1, DISABLE); |
400 | } |
400 | } |
401 | break; |
401 | break; |
402 | 402 | ||
403 | default: |
403 | default: |
404 | break; |
404 | break; |
405 | } |
405 | } |
406 | } |
406 | } |
407 | } |
407 | } |
408 | //---------------------------------------------------------------- |
408 | //---------------------------------------------------------------- |
409 | void I2C1_SendCommand(u8 command) |
409 | void I2C1_SendCommand(u8 command) |
410 | { |
410 | { |
411 | // If I2C transmission is in progress |
411 | // If I2C transmission is in progress |
412 | if(I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress |
412 | if(I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress |
413 | // disable I2C IRQ to avoid read/write access to the tx/rx buffer pointers during |
413 | // disable I2C IRQ to avoid read/write access to the tx/rx buffer pointers during |
414 | // update of that buffer pointers and length |
414 | // update of that buffer pointers and length |
415 | I2C_ITConfig(I2C1, DISABLE); |
415 | I2C_ITConfig(I2C1, DISABLE); |
416 | // update current command id |
416 | // update current command id |
417 | I2C_Command = command; |
417 | I2C_Command = command; |
418 | // set pointers to data area with respect to the command id |
418 | // set pointers to data area with respect to the command id |
419 | switch (command) |
419 | switch (command) |
420 | { |
420 | { |
421 | case I2C_CMD_VERSION: |
421 | case I2C_CMD_VERSION: |
422 | I2C_RxBuffer = (u8 *)&MK3MAG_Version; |
422 | I2C_RxBuffer = (u8 *)&MK3MAG_Version; |
423 | I2C_RxBufferSize = sizeof(MK3MAG_Version); |
423 | I2C_RxBufferSize = sizeof(MK3MAG_Version); |
424 | I2C_TxBuffer = NULL; |
424 | I2C_TxBuffer = NULL; |
425 | I2C_TxBufferSize = 0; |
425 | I2C_TxBufferSize = 0; |
426 | break; |
426 | break; |
427 | case I2C_CMD_WRITE_CAL: |
427 | case I2C_CMD_WRITE_CAL: |
428 | I2C_RxBuffer = (u8 *)&I2C_ReadCal; |
428 | I2C_RxBuffer = (u8 *)&I2C_ReadCal; |
429 | I2C_RxBufferSize = sizeof(I2C_ReadCal); |
429 | I2C_RxBufferSize = sizeof(I2C_ReadCal); |
430 | I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
430 | I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
431 | I2C_TxBufferSize = sizeof(I2C_WriteCal); |
431 | I2C_TxBufferSize = sizeof(I2C_WriteCal); |
432 | break; |
432 | break; |
433 | case I2C_CMD_READ_MAG: |
433 | case I2C_CMD_READ_MAG: |
434 | I2C_RxBuffer = (u8 *)&I2C_Mag; |
434 | I2C_RxBuffer = (u8 *)&I2C_Mag; |
435 | I2C_RxBufferSize = sizeof(I2C_Mag); |
435 | I2C_RxBufferSize = sizeof(I2C_Mag); |
436 | I2C_TxBuffer = NULL; |
436 | I2C_TxBuffer = NULL; |
437 | I2C_TxBufferSize = 0; |
437 | I2C_TxBufferSize = 0; |
438 | break; |
438 | break; |
439 | case I2C_CMD_READ_HEADING: |
439 | case I2C_CMD_READ_HEADING: |
440 | DebugOut.Analog[10] = I2C_Heading.Heading; |
440 | DebugOut.Analog[10] = I2C_Heading.Heading; |
441 | I2C_RxBuffer = (u8 *)&I2C_Heading; |
441 | I2C_RxBuffer = (u8 *)&I2C_Heading; |
442 | I2C_RxBufferSize = sizeof(I2C_Heading); |
442 | I2C_RxBufferSize = sizeof(I2C_Heading); |
443 | // updat atitude from spi rx buffer |
443 | // updat atitude from spi rx buffer |
444 | I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll; |
444 | I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll; |
445 | I2C_WriteAttitude.Nick = FromFlightCtrl.AngleNick; |
445 | I2C_WriteAttitude.Nick = FromFlightCtrl.AngleNick; |
446 | I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
446 | I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
447 | I2C_TxBufferSize = sizeof(I2C_WriteAttitude); |
447 | I2C_TxBufferSize = sizeof(I2C_WriteAttitude); |
448 | break; |
448 | break; |
449 | default: // unknown command id |
449 | default: // unknown command id |
450 | I2C_RxBuffer = NULL; |
450 | I2C_RxBuffer = NULL; |
451 | I2C_RxBufferSize = 0; |
451 | I2C_RxBufferSize = 0; |
452 | I2C_TxBuffer = NULL; |
452 | I2C_TxBuffer = NULL; |
453 | I2C_TxBufferSize = 0; |
453 | I2C_TxBufferSize = 0; |
454 | break; |
454 | break; |
455 | } |
455 | } |
456 | // set direction to master transmitter |
456 | // set direction to master transmitter |
457 | I2C_Direction = I2C_MODE_TRANSMITTER; |
457 | I2C_Direction = I2C_MODE_TRANSMITTER; |
458 | // test on busy flag and clear it |
458 | // test on busy flag and clear it |
459 | I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
459 | I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
460 | // enable I2C IRQ again |
460 | // enable I2C IRQ again |
461 | I2C_ITConfig(I2C1, ENABLE); |
461 | I2C_ITConfig(I2C1, ENABLE); |
462 | // initiale start condition on the bus |
462 | // initiale start condition on the bus |
463 | I2C_GenerateStart(I2C1, ENABLE); |
463 | I2C_GenerateStart(I2C1, ENABLE); |
464 | // to be continued in the I2C1_IRQHandler() above |
464 | // to be continued in the I2C1_IRQHandler() above |
465 | } |
465 | } |
466 | 466 | ||
467 | //---------------------------------------------------------------- |
467 | //---------------------------------------------------------------- |
468 | void I2C1_GetMK3MagVersion(void) |
468 | void I2C1_GetMK3MagVersion(void) |
469 | { |
469 | { |
470 | u8 msg[64]; |
470 | u8 msg[64]; |
471 | u8 repeat; |
471 | u8 repeat; |
472 | u32 timeout; |
472 | u32 timeout; |
473 | 473 | ||
474 | UART1_PutString("\r\n Getting Version from MK3MAG"); |
474 | UART1_PutString("\r\n Getting Version from MK3MAG"); |
475 | // stop polling of other commands |
475 | // stop polling of other commands |
476 | I2C_StopPolling = 1; |
476 | I2C_StopPolling = 1; |
477 | 477 | ||
478 | MK3MAG_Version.Major = 0xFF; |
478 | MK3MAG_Version.Major = 0xFF; |
479 | MK3MAG_Version.Minor = 0xFF; |
479 | MK3MAG_Version.Minor = 0xFF; |
480 | MK3MAG_Version.Patch = 0xFF; |
480 | MK3MAG_Version.Patch = 0xFF; |
481 | MK3MAG_Version.Compatible = 0xFF; |
481 | MK3MAG_Version.Compatible = 0xFF; |
482 | // polling of version info |
482 | // polling of version info |
483 | repeat = 0; |
483 | repeat = 0; |
484 | do |
484 | do |
485 | { |
485 | { |
486 | I2C1_SendCommand(I2C_CMD_VERSION); |
486 | I2C1_SendCommand(I2C_CMD_VERSION); |
487 | timeout = SetDelay(250); |
487 | timeout = SetDelay(250); |
488 | do |
488 | do |
489 | { |
489 | { |
490 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
490 | if (MK3MAG_Version.Major != 0xFF) break; // break loop on success |
491 | }while (!CheckDelay(timeout)); |
491 | }while (!CheckDelay(timeout)); |
492 | UART1_PutString("."); |
492 | UART1_PutString("."); |
493 | repeat++; |
493 | repeat++; |
494 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
494 | }while ((MK3MAG_Version.Major == 0xFF) && (repeat < 12)); // 12*250ms=3s |
495 | // if we got it |
495 | // if we got it |
496 | if (MK3MAG_Version.Major != 0xFF) |
496 | if (MK3MAG_Version.Major != 0xFF) |
497 | { |
497 | { |
498 | sprintf(msg, "\r\n MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
498 | sprintf(msg, "\r\n MK3MAG V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
499 | UART1_PutString(msg); |
499 | UART1_PutString(msg); |
500 | sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible); |
500 | sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible); |
501 | UART1_PutString(msg); |
501 | UART1_PutString(msg); |
502 | } |
502 | } |
503 | else UART1_PutString("\n\r No version information from MK3Mag."); |
503 | else UART1_PutString("\n\r No version information from MK3Mag."); |
504 | 504 | ||
505 | I2C_StopPolling = 0; // enable polling of heading command |
505 | I2C_StopPolling = 0; // enable polling of heading command |
506 | } |
506 | } |
507 | 507 | ||
508 | 508 | ||
509 | //---------------------------------------------------------------- |
509 | //---------------------------------------------------------------- |
510 | void I2C1_UpdateCompass(void) |
510 | void I2C1_UpdateCompass(void) |
511 | { |
511 | { |
512 | static u32 TimerCompassUpdate = 0; |
512 | static u32 TimerCompassUpdate = 0; |
513 | 513 | ||
514 | if( (I2C_State == I2C_OFF) || I2C_StopPolling ) return; |
514 | if( (I2C_State == I2C_OFF) || I2C_StopPolling ) return; |
515 | 515 | ||
516 | if(CheckDelay(TimerCompassUpdate)) |
516 | if(CheckDelay(TimerCompassUpdate)) |
517 | { |
517 | { |
518 | // check for incomming compass calibration request |
518 | // check for incomming compass calibration request |
519 | // update CalByte from spi input queue |
519 | // update CalByte from spi input queue |
520 | fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
520 | fifo_get(&CompassCalcStateFiFo, (u8 *)&(I2C_WriteCal.CalByte)); |
521 | // send new calstate |
521 | // send new calstate |
522 | if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte) |
522 | if(I2C_ReadCal.CalByte != I2C_WriteCal.CalByte) |
523 | { |
523 | { |
524 | I2C1_SendCommand(I2C_CMD_WRITE_CAL); |
524 | I2C1_SendCommand(I2C_CMD_WRITE_CAL); |
525 | } |
525 | } |
526 | else // request current heading |
526 | else // request current heading |
527 | { |
527 | { |
528 | I2C1_SendCommand(I2C_CMD_READ_HEADING); |
528 | I2C1_SendCommand(I2C_CMD_READ_HEADING); |
529 | } |
529 | } |
530 | TimerCompassUpdate = SetDelay(25); // every 25 ms |
530 | TimerCompassUpdate = SetDelay(25); // every 25 ms |
531 | } |
531 | } |
532 | } |
532 | } |
533 | 533 |