Rev 137 | Rev 146 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 137 | Rev 141 | ||
---|---|---|---|
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 |
6 | // + Nur für den privaten Gebrauch |
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 der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
28 | // + Die PORTIERUNG 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 this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed |
43 | // + * PORTING this software (or part of it) to systems (other than 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 | 70 | ||
71 | // rxbuffer |
71 | // rxbuffer |
72 | volatile u8 I2C_RxBufferSize; |
72 | volatile u8 I2C_RxBufferSize; |
73 | volatile u8 *I2C_RxBuffer; |
73 | volatile u8 *I2C_RxBuffer; |
74 | volatile u8 Rx_Idx = 0; |
74 | volatile u8 Rx_Idx = 0; |
75 | // txbuffer |
75 | // txbuffer |
76 | volatile u8 I2C_TxBufferSize; |
76 | volatile u8 I2C_TxBufferSize; |
77 | volatile u8 *I2C_TxBuffer; |
77 | volatile u8 *I2C_TxBuffer; |
78 | volatile u8 Tx_Idx = 0; |
78 | volatile u8 Tx_Idx = 0; |
79 | 79 | ||
80 | volatile u8 I2C_Direction; |
80 | volatile u8 I2C_Direction; |
81 | volatile u8 I2C_Command; |
81 | volatile u8 I2C_Command; |
82 | 82 | ||
83 | volatile I2C_Heading_t I2C_Heading; |
83 | volatile I2C_Heading_t I2C_Heading; |
84 | volatile I2C_WriteAttitude_t I2C_WriteAttitude; |
84 | volatile I2C_WriteAttitude_t I2C_WriteAttitude; |
85 | volatile I2C_Mag_t I2C_Mag; |
85 | volatile I2C_Mag_t I2C_Mag; |
86 | volatile I2C_Version_t MK3MAG_Version; |
86 | volatile I2C_Version_t MK3MAG_Version; |
87 | volatile I2C_Cal_t I2C_WriteCal; |
87 | volatile I2C_Cal_t I2C_WriteCal; |
88 | volatile I2C_Cal_t I2C_ReadCal; |
88 | volatile I2C_Cal_t I2C_ReadCal; |
89 | 89 | ||
90 | volatile u8 I2C_ReadRequest = 0; |
90 | volatile u8 I2C_ReadRequest = 0; |
91 | volatile u32 I2C1_Timeout = 0; |
91 | volatile u32 I2C1_Timeout = 0; |
92 | volatile u32 CheckI2COkay = 0; |
92 | volatile u32 CheckI2COkay = 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 | //-------------------------------------------------------------- |
96 | //-------------------------------------------------------------- |
97 | void I2C1_Init(void) |
97 | void I2C1_Init(void) |
98 | { |
98 | { |
99 | I2C_InitTypeDef I2C_Struct; |
99 | I2C_InitTypeDef I2C_Struct; |
100 | GPIO_InitTypeDef GPIO_InitStructure; |
100 | GPIO_InitTypeDef GPIO_InitStructure; |
101 | 101 | ||
102 | UART1_PutString("\r\n I2C init..."); |
102 | UART1_PutString("\r\n I2C init..."); |
103 | // enable Port 2 peripherie |
103 | // enable Port 2 peripherie |
104 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
104 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
105 | // disable a reset state |
105 | // disable a reset state |
106 | SCU_APBPeriphReset(__GPIO2, DISABLE); |
106 | SCU_APBPeriphReset(__GPIO2, DISABLE); |
107 | 107 | ||
108 | // free a busy bus |
108 | // free a busy bus |
109 | 109 | ||
110 | // At switch on I2C devices can get in a state where they |
110 | // At switch on I2C devices can get in a state where they |
111 | // are still waiting for a command due to all the bus lines bouncing |
111 | // are still waiting for a command due to all the bus lines bouncing |
112 | // around at startup have started clocking data into the device(s). |
112 | // around at startup have started clocking data into the device(s). |
113 | // Enable the ports as open collector port outputs |
113 | // Enable the ports as open collector port outputs |
114 | // and clock out at least 9 SCL pulses, then generate a stop |
114 | // and clock out at least 9 SCL pulses, then generate a stop |
115 | // condition and then leave the clock line high. |
115 | // condition and then leave the clock line high. |
116 | 116 | ||
117 | // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation |
117 | // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation |
118 | GPIO_StructInit(&GPIO_InitStructure); |
118 | GPIO_StructInit(&GPIO_InitStructure); |
119 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
119 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
120 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
120 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
121 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
121 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
122 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
122 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
123 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
123 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
124 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
124 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
125 | 125 | ||
126 | u8 i; |
126 | u8 i; |
127 | u32 delay; |
127 | u32 delay; |
128 | // set SCL high and then SDA to low (start condition) |
128 | // set SCL high and then SDA to low (start condition) |
129 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
129 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
130 | delay = SetDelay(1); |
130 | delay = SetDelay(1); |
131 | while (!CheckDelay(delay)); |
131 | while (!CheckDelay(delay)); |
132 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET); |
132 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET); |
133 | // toggle SCL at least 10 times from high to low to high |
133 | // toggle SCL at least 10 times from high to low to high |
134 | for(i = 0; i < 10; i++) |
134 | for(i = 0; i < 10; i++) |
135 | { |
135 | { |
136 | delay = SetDelay(1); |
136 | delay = SetDelay(1); |
137 | while (!CheckDelay(delay)); |
137 | while (!CheckDelay(delay)); |
138 | 138 | ||
139 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET); |
139 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET); |
140 | delay = SetDelay(1); |
140 | delay = SetDelay(1); |
141 | while (!CheckDelay(delay)); |
141 | while (!CheckDelay(delay)); |
142 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
142 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
143 | } |
143 | } |
144 | delay = SetDelay(1); |
144 | delay = SetDelay(1); |
145 | while (!CheckDelay(delay)); |
145 | while (!CheckDelay(delay)); |
146 | // create stop condition setting SDA HIGH when SCL is HIGH |
146 | // create stop condition setting SDA HIGH when SCL is HIGH |
147 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET); |
147 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET); |
148 | 148 | ||
149 | 149 | ||
150 | // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT |
150 | // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT |
151 | GPIO_StructInit(&GPIO_InitStructure); |
151 | GPIO_StructInit(&GPIO_InitStructure); |
152 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
152 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
153 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
153 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
154 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
154 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
155 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
155 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable; |
156 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT |
156 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT |
157 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
157 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
158 | 158 | ||
159 | // enable I2C peripherie |
159 | // enable I2C peripherie |
160 | SCU_APBPeriphClockConfig(__I2C1,ENABLE); |
160 | SCU_APBPeriphClockConfig(__I2C1,ENABLE); |
161 | // reset I2C peripherie |
161 | // reset I2C peripherie |
162 | SCU_APBPeriphReset(__I2C1,ENABLE); |
162 | SCU_APBPeriphReset(__I2C1,ENABLE); |
163 | SCU_APBPeriphReset(__I2C1,DISABLE); |
163 | SCU_APBPeriphReset(__I2C1,DISABLE); |
164 | 164 | ||
165 | I2C_DeInit(I2C1); |
165 | I2C_DeInit(I2C1); |
166 | I2C_StructInit(&I2C_Struct); |
166 | I2C_StructInit(&I2C_Struct); |
167 | I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; |
167 | I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; |
168 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
168 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
169 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
169 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
170 | I2C_Struct.I2C_OwnAddress = 0x00; |
170 | I2C_Struct.I2C_OwnAddress = 0x00; |
171 | I2C_Init(I2C1, &I2C_Struct); |
171 | I2C_Init(I2C1, &I2C_Struct); |
172 | 172 | ||
173 | I2C_TxBuffer = NULL; |
173 | I2C_TxBuffer = NULL; |
174 | Tx_Idx = 0; |
174 | Tx_Idx = 0; |
175 | I2C_TxBufferSize = 0; |
175 | I2C_TxBufferSize = 0; |
176 | 176 | ||
177 | I2C_RxBuffer = NULL; |
177 | I2C_RxBuffer = NULL; |
178 | Rx_Idx = 0; |
178 | Rx_Idx = 0; |
179 | I2C_RxBufferSize = 0; |
179 | I2C_RxBufferSize = 0; |
180 | 180 | ||
181 | I2C_Cmd(I2C1, ENABLE); |
181 | I2C_Cmd(I2C1, ENABLE); |
182 | I2C_ITConfig(I2C1, ENABLE); |
182 | I2C_ITConfig(I2C1, ENABLE); |
183 | 183 | ||
184 | VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
184 | VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
185 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
185 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
186 | 186 | ||
187 | I2C1_Timeout = SetDelay(1000); |
187 | I2C1_Timeout = SetDelay(1000); |
188 | I2C_Heading.Heading = -1; |
188 | I2C_Heading.Heading = -1; |
189 | I2C_GenerateSTOP(I2C1, ENABLE); |
189 | I2C_GenerateSTOP(I2C1, ENABLE); |
190 | I2C_State = I2C_IDLE; |
190 | I2C_State = I2C_IDLE; |
191 | 191 | ||
192 | UART1_PutString("ok"); |
192 | UART1_PutString("ok"); |
193 | } |
193 | } |
194 | 194 | ||
195 | 195 | ||
196 | //-------------------------------------------------------------- |
196 | //-------------------------------------------------------------- |
197 | void I2C1_Deinit(void) |
197 | void I2C1_Deinit(void) |
198 | { |
198 | { |
199 | GPIO_InitTypeDef GPIO_InitStructure; |
199 | GPIO_InitTypeDef GPIO_InitStructure; |
200 | 200 | ||
201 | UART1_PutString("\r\n I2C deinit..."); |
201 | UART1_PutString("\r\n I2C deinit..."); |
202 | I2C_GenerateStart(I2C1, DISABLE); |
202 | I2C_GenerateStart(I2C1, DISABLE); |
203 | I2C_GenerateSTOP(I2C1, ENABLE); |
203 | I2C_GenerateSTOP(I2C1, ENABLE); |
204 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
204 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
205 | I2C_ITConfig(I2C1, DISABLE); |
205 | I2C_ITConfig(I2C1, DISABLE); |
206 | I2C_Cmd(I2C1, DISABLE); |
206 | I2C_Cmd(I2C1, DISABLE); |
207 | I2C_DeInit(I2C1); |
207 | I2C_DeInit(I2C1); |
208 | SCU_APBPeriphClockConfig(__I2C1, DISABLE); |
208 | SCU_APBPeriphClockConfig(__I2C1, DISABLE); |
209 | 209 | ||
210 | // set ports to input |
210 | // set ports to input |
211 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
211 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
212 | GPIO_StructInit(&GPIO_InitStructure); |
212 | GPIO_StructInit(&GPIO_InitStructure); |
213 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
213 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
214 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
214 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
215 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
215 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
216 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
216 | GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Disable; |
217 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
217 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
218 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
218 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
219 | 219 | ||
220 | I2C_TxBuffer = NULL; |
220 | I2C_TxBuffer = NULL; |
221 | Tx_Idx = 0; |
221 | Tx_Idx = 0; |
222 | I2C_TxBufferSize = 0; |
222 | I2C_TxBufferSize = 0; |
223 | 223 | ||
224 | I2C_RxBuffer = NULL; |
224 | I2C_RxBuffer = NULL; |
225 | Rx_Idx = 0; |
225 | Rx_Idx = 0; |
226 | I2C_RxBufferSize = 0; |
226 | I2C_RxBufferSize = 0; |
227 | 227 | ||
228 | I2C_State = I2C_OFF; |
228 | I2C_State = I2C_OFF; |
229 | 229 | ||
230 | I2C1_Timeout = 0; |
230 | I2C1_Timeout = 0; |
231 | I2C_Heading.Heading = -1; |
231 | I2C_Heading.Heading = -1; |
232 | 232 | ||
233 | UART1_PutString("ok"); |
233 | UART1_PutString("ok"); |
234 | } |
234 | } |
235 | 235 | ||
236 | 236 | ||
237 | //-------------------------------------------------------------- |
237 | //-------------------------------------------------------------- |
238 | void I2C1_IRQHandler(void) |
238 | void I2C1_IRQHandler(void) |
239 | { |
239 | { |
240 | u16 status; |
240 | u16 status; |
241 | static u8 crc; |
241 | static u8 crc; |
242 | // detemine I2C State |
242 | // detemine I2C State |
243 | status = I2C_GetLastEvent(I2C1); |
243 | status = I2C_GetLastEvent(I2C1); |
244 | 244 | ||
245 | if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured |
245 | if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured |
246 | { // Set and subsequently clear the STOP bit while BTF is set. |
246 | { // Set and subsequently clear the STOP bit while BTF is set. |
247 | while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
247 | while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
248 | { |
248 | { |
249 | I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
249 | I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
250 | I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
250 | I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
251 | } |
251 | } |
252 | I2C_State = I2C_IDLE; |
252 | I2C_State = I2C_IDLE; |
253 | LED_GRN_OFF; |
253 | LED_GRN_OFF; |
254 | } |
254 | } |
255 | else |
255 | else |
256 | { // depending on current i2c state |
256 | { // depending on current i2c state |
257 | switch (status) |
257 | switch (status) |
258 | { |
258 | { |
259 | // the start condition was initiated on the bus |
259 | // the start condition was initiated on the bus |
260 | case I2C_EVENT_MASTER_MODE_SELECT: |
260 | case I2C_EVENT_MASTER_MODE_SELECT: |
261 | LED_GRN_ON; |
261 | LED_GRN_ON; |
262 | // update current bus state variable |
262 | // update current bus state variable |
263 | switch(I2C_Direction) |
263 | switch(I2C_Direction) |
264 | { |
264 | { |
265 | case I2C_MODE_TRANSMITTER: |
265 | case I2C_MODE_TRANSMITTER: |
266 | I2C_State = I2C_TX_PROGRESS; |
266 | I2C_State = I2C_TX_PROGRESS; |
267 | break; |
267 | break; |
268 | 268 | ||
269 | case I2C_MODE_RECEIVER: |
269 | case I2C_MODE_RECEIVER: |
270 | if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0)) |
270 | if ((I2C_RxBuffer == NULL) || (I2C_RxBufferSize == 0)) |
271 | { |
271 | { |
272 | I2C_GenerateSTOP (I2C1, ENABLE); |
272 | I2C_GenerateSTOP (I2C1, ENABLE); |
273 | I2C_State = I2C_IDLE; |
273 | I2C_State = I2C_IDLE; |
274 | return; |
274 | return; |
275 | } |
275 | } |
276 | else |
276 | else |
277 | { |
277 | { |
278 | I2C_State = I2C_RX_PROGRESS; |
278 | I2C_State = I2C_RX_PROGRESS; |
279 | } |
279 | } |
280 | break; |
280 | break; |
281 | 281 | ||
282 | default: // invalid direction |
282 | default: // invalid direction |
283 | I2C_GenerateSTOP (I2C1, ENABLE); |
283 | I2C_GenerateSTOP (I2C1, ENABLE); |
284 | I2C_State = I2C_IDLE; |
284 | I2C_State = I2C_IDLE; |
285 | LED_GRN_OFF; |
285 | LED_GRN_OFF; |
286 | return; |
286 | return; |
287 | } |
287 | } |
288 | // enable acknowledge |
288 | // enable acknowledge |
289 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
289 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
290 | // send address/direction byte on the bus |
290 | // send address/direction byte on the bus |
291 | I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction); |
291 | I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction); |
292 | break; |
292 | break; |
293 | 293 | ||
294 | // the address byte was send |
294 | // the address byte was send |
295 | case I2C_EVENT_MASTER_MODE_SELECTED: |
295 | case I2C_EVENT_MASTER_MODE_SELECTED: |
296 | // Clear EV6 by set again the PE bit |
296 | // Clear EV6 by set again the PE bit |
297 | I2C_Cmd(I2C1, ENABLE); |
297 | I2C_Cmd(I2C1, ENABLE); |
298 | // reset checksum |
298 | // reset checksum |
299 | crc = 0; |
299 | crc = 0; |
300 | switch(I2C_State) |
300 | switch(I2C_State) |
301 | { |
301 | { |
302 | case I2C_TX_PROGRESS: |
302 | case I2C_TX_PROGRESS: |
303 | // send command 1st data byte (allways the command id) |
303 | // send command 1st data byte (allways the command id) |
304 | I2C_SendData(I2C1, I2C_Command); |
304 | I2C_SendData(I2C1, I2C_Command); |
305 | crc += I2C_Command; |
305 | crc += I2C_Command; |
306 | Tx_Idx = 0; |
306 | Tx_Idx = 0; |
307 | // reset timeout |
307 | // reset timeout |
308 | I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset |
308 | I2C1_Timeout = SetDelay(500); // after 500 ms of inactivity the I2C1 bus will be reset |
309 | break; |
309 | break; |
310 | 310 | ||
311 | case I2C_RX_PROGRESS: |
311 | case I2C_RX_PROGRESS: |
312 | Rx_Idx = 0; |
312 | Rx_Idx = 0; |
313 | break; |
313 | break; |
314 | 314 | ||
315 | default: // unknown I2C state |
315 | default: // unknown I2C state |
316 | // should never happen |
316 | // should never happen |
317 | I2C_GenerateSTOP (I2C1, ENABLE); |
317 | I2C_GenerateSTOP (I2C1, ENABLE); |
318 | I2C_State = I2C_IDLE; |
318 | I2C_State = I2C_IDLE; |
319 | break; |
319 | break; |
320 | } |
320 | } |
321 | break; |
321 | break; |
322 | 322 | ||
323 | // the master has transmitted a byte and slave has been acknowledged |
323 | // the master has transmitted a byte and slave has been acknowledged |
324 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
324 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
325 | 325 | ||
326 | // some bytes have to be transmitted |
326 | // some bytes have to be transmitted |
327 | if(Tx_Idx < I2C_TxBufferSize) |
327 | if(Tx_Idx < I2C_TxBufferSize) |
328 | { |
328 | { |
329 | if(I2C_TxBuffer != NULL) |
329 | if(I2C_TxBuffer != NULL) |
330 | { |
330 | { |
331 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
331 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
332 | crc += I2C_TxBuffer[Tx_Idx]; |
332 | crc += I2C_TxBuffer[Tx_Idx]; |
333 | } |
333 | } |
334 | else |
334 | else |
335 | { |
335 | { |
336 | I2C_SendData(I2C1, 0x00); |
336 | I2C_SendData(I2C1, 0x00); |
337 | } |
337 | } |
338 | } |
338 | } |
339 | else // the last tx buffer byte was send |
339 | else // the last tx buffer byte was send |
340 | { |
340 | { |
341 | // send crc byte at the end |
341 | // send crc byte at the end |
342 | crc = ~crc; // flip all bits in the checksum |
342 | crc = ~crc; // flip all bits in the checksum |
343 | I2C_SendData(I2C1, crc); |
343 | I2C_SendData(I2C1, crc); |
344 | // generate stop or repeated start condition |
344 | // generate stop or repeated start condition |
345 | if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected? |
345 | if ((I2C_RxBuffer != NULL) && (I2C_RxBufferSize > 0)) // is any answer byte expected? |
346 | { |
346 | { |
347 | I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
347 | I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
348 | I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
348 | I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
349 | } |
349 | } |
350 | else |
350 | else |
351 | { // stop communication |
351 | { // stop communication |
352 | I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
352 | I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
353 | I2C_State = I2C_IDLE; // ready for new actions |
353 | I2C_State = I2C_IDLE; // ready for new actions |
354 | LED_GRN_OFF; |
354 | LED_GRN_OFF; |
355 | DebugOut.Analog[15]++; |
355 | DebugOut.Analog[15]++; |
356 | } |
356 | } |
357 | } |
357 | } |
358 | Tx_Idx++; |
358 | Tx_Idx++; |
359 | break; |
359 | break; |
360 | 360 | ||
361 | // the master has received a byte from the slave |
361 | // the master has received a byte from the slave |
362 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
362 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
363 | // some bytes have to be received |
363 | // some bytes have to be received |
364 | if (Rx_Idx < I2C_RxBufferSize) |
364 | if (Rx_Idx < I2C_RxBufferSize) |
365 | { // copy received byte from the data register to the rx-buffer |
365 | { // copy received byte from the data register to the rx-buffer |
366 | I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
366 | I2C_PrimRxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
367 | // update checksum |
367 | // update checksum |
368 | crc += I2C_PrimRxBuffer[Rx_Idx]; |
368 | crc += I2C_PrimRxBuffer[Rx_Idx]; |
369 | } |
369 | } |
370 | // if the last byte (crc) was received |
370 | // if the last byte (crc) was received |
371 | else if ( Rx_Idx == I2C_RxBufferSize) |
371 | else if ( Rx_Idx == I2C_RxBufferSize) |
372 | { |
372 | { |
373 | // generate a STOP condition on the bus before reading data register |
373 | // generate a STOP condition on the bus before reading data register |
374 | I2C_GenerateSTOP(I2C1, ENABLE); |
374 | I2C_GenerateSTOP(I2C1, ENABLE); |
375 | // compare last byte with checksum |
375 | // compare last byte with checksum |
376 | crc = ~crc;// flip all bits in calulated checksum |
376 | crc = ~crc;// flip all bits in calulated checksum |
377 | if(crc == I2C_ReceiveData(I2C1)) |
377 | if(crc == I2C_ReceiveData(I2C1)) |
378 | { // copy primary rx buffer content to rx buffer if exist |
378 | { // copy primary rx buffer content to rx buffer if exist |
379 | if(I2C_RxBuffer != NULL) |
379 | if(I2C_RxBuffer != NULL) |
380 | { |
380 | { |
381 | memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize); |
381 | memcpy((u8 *)I2C_RxBuffer, (u8 *)I2C_PrimRxBuffer, I2C_RxBufferSize); |
382 | } |
382 | } |
- | 383 | I2C1_Timeout = SetDelay(500); |
|
383 | DebugOut.Analog[15]++; |
384 | DebugOut.Analog[15]++; |
384 | CheckI2COkay++; |
385 | CheckI2COkay++; |
385 | } |
386 | } |
386 | else // checksum error detected |
387 | else // checksum error detected |
387 | { |
388 | { |
388 | DebugOut.Analog[14]++; |
389 | DebugOut.Analog[14]++; |
389 | } |
390 | } |
390 | I2C_State = I2C_IDLE; |
391 | I2C_State = I2C_IDLE; |
391 | LED_GRN_OFF; |
392 | LED_GRN_OFF; |
392 | } |
393 | } |
393 | Rx_Idx++; |
394 | Rx_Idx++; |
394 | // if the 2nd last byte was received disable acknowledge for the last one |
395 | // if the 2nd last byte was received disable acknowledge for the last one |
395 | if ( Rx_Idx == I2C_RxBufferSize ) |
396 | if ( Rx_Idx == I2C_RxBufferSize ) |
396 | { |
397 | { |
397 | I2C_AcknowledgeConfig (I2C1, DISABLE); |
398 | I2C_AcknowledgeConfig (I2C1, DISABLE); |
398 | } |
399 | } |
399 | break; |
400 | break; |
400 | 401 | ||
401 | default: |
402 | default: |
402 | break; |
403 | break; |
403 | } |
404 | } |
404 | } |
405 | } |
405 | } |
406 | } |
406 | //---------------------------------------------------------------- |
407 | //---------------------------------------------------------------- |
407 | void I2C1_SendCommand(u8 command) |
408 | void I2C1_SendCommand(u8 command) |
408 | { |
409 | { |
409 | // If I2C transmission is in progress |
410 | // If I2C transmission is in progress |
410 | while (I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress |
411 | while (I2C_State != I2C_IDLE) return; // return imediatly if a transfer is still in progress |
411 | // disable I2C IRQ to avoid read/write access to the tx/rx buffer pointers during |
412 | // disable I2C IRQ to avoid read/write access to the tx/rx buffer pointers during |
412 | // update of that buffer pointers and length |
413 | // update of that buffer pointers and length |
413 | I2C_ITConfig(I2C1, DISABLE); |
414 | I2C_ITConfig(I2C1, DISABLE); |
414 | // update current command id |
415 | // update current command id |
415 | I2C_Command = command; |
416 | I2C_Command = command; |
416 | // set pointers to data area with respect to the command id |
417 | // set pointers to data area with respect to the command id |
417 | switch (command) |
418 | switch (command) |
418 | { |
419 | { |
419 | case I2C_CMD_VERSION: |
420 | case I2C_CMD_VERSION: |
420 | I2C_RxBuffer = (u8 *)&MK3MAG_Version; |
421 | I2C_RxBuffer = (u8 *)&MK3MAG_Version; |
421 | I2C_RxBufferSize = sizeof(MK3MAG_Version); |
422 | I2C_RxBufferSize = sizeof(MK3MAG_Version); |
422 | I2C_TxBuffer = NULL; |
423 | I2C_TxBuffer = NULL; |
423 | I2C_TxBufferSize = 0; |
424 | I2C_TxBufferSize = 0; |
424 | break; |
425 | break; |
425 | case I2C_CMD_WRITE_CAL: |
426 | case I2C_CMD_WRITE_CAL: |
426 | I2C_RxBuffer = (u8 *)&I2C_ReadCal; |
427 | I2C_RxBuffer = (u8 *)&I2C_ReadCal; |
427 | I2C_RxBufferSize = sizeof(I2C_ReadCal); |
428 | I2C_RxBufferSize = sizeof(I2C_ReadCal); |
428 | I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
429 | I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
429 | I2C_TxBufferSize = sizeof(I2C_WriteCal); |
430 | I2C_TxBufferSize = sizeof(I2C_WriteCal); |
430 | break; |
431 | break; |
431 | case I2C_CMD_READ_MAG: |
432 | case I2C_CMD_READ_MAG: |
432 | I2C_RxBuffer = (u8 *)&I2C_Mag; |
433 | I2C_RxBuffer = (u8 *)&I2C_Mag; |
433 | I2C_RxBufferSize = sizeof(I2C_Mag); |
434 | I2C_RxBufferSize = sizeof(I2C_Mag); |
434 | I2C_TxBuffer = NULL; |
435 | I2C_TxBuffer = NULL; |
435 | I2C_TxBufferSize = 0; |
436 | I2C_TxBufferSize = 0; |
436 | break; |
437 | break; |
437 | case I2C_CMD_READ_HEADING: |
438 | case I2C_CMD_READ_HEADING: |
438 | DebugOut.Analog[10] = I2C_Heading.Heading; |
439 | DebugOut.Analog[10] = I2C_Heading.Heading; |
439 | I2C_RxBuffer = (u8 *)&I2C_Heading; |
440 | I2C_RxBuffer = (u8 *)&I2C_Heading; |
440 | I2C_RxBufferSize = sizeof(I2C_Heading); |
441 | I2C_RxBufferSize = sizeof(I2C_Heading); |
441 | // updat atitude from spi rx buffer |
442 | // updat atitude from spi rx buffer |
442 | I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll; |
443 | I2C_WriteAttitude.Roll = FromFlightCtrl.AngleRoll; |
443 | I2C_WriteAttitude.Nick = FromFlightCtrl.AngleNick; |
444 | I2C_WriteAttitude.Nick = FromFlightCtrl.AngleNick; |
444 | I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
445 | I2C_TxBuffer = (u8 *)&I2C_WriteAttitude; |
445 | I2C_TxBufferSize = sizeof(I2C_WriteAttitude); |
446 | I2C_TxBufferSize = sizeof(I2C_WriteAttitude); |
446 | break; |
447 | break; |
447 | default: // unknown command id |
448 | default: // unknown command id |
448 | I2C_RxBuffer = NULL; |
449 | I2C_RxBuffer = NULL; |
449 | I2C_RxBufferSize = 0; |
450 | I2C_RxBufferSize = 0; |
450 | I2C_TxBuffer = NULL; |
451 | I2C_TxBuffer = NULL; |
451 | I2C_TxBufferSize = 0; |
452 | I2C_TxBufferSize = 0; |
452 | break; |
453 | break; |
453 | } |
454 | } |
454 | // enable I2C IRQ again |
455 | // enable I2C IRQ again |
455 | I2C_ITConfig(I2C1, ENABLE); |
456 | I2C_ITConfig(I2C1, ENABLE); |
456 | // set direction to master transmitter |
457 | // set direction to master transmitter |
457 | I2C_Direction = I2C_MODE_TRANSMITTER; |
458 | I2C_Direction = I2C_MODE_TRANSMITTER; |
458 | // test on busy flag and clear it |
459 | // test on busy flag and clear it |
459 | I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
460 | I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
460 | // initiale start condition on the bus |
461 | // initiale start condition on the bus |
461 | I2C_GenerateStart(I2C1, ENABLE); |
462 | I2C_GenerateStart(I2C1, ENABLE); |
462 | // to be continued in the I2C1_IRQHandler() above |
463 | // to be continued in the I2C1_IRQHandler() above |
463 | } |
464 | } |
464 | 465 | ||
465 | //---------------------------------------------------------------- |
466 | //---------------------------------------------------------------- |
466 | void I2C1_GetMK3MagVersion(void) |
467 | void I2C1_GetMK3MagVersion(void) |
467 | { |
468 | { |
468 | u8 msg[64]; |
469 | u8 msg[64]; |
469 | u8 repeat = 10; |
470 | u8 repeat = 10; |
470 | 471 | ||
471 | MK3MAG_Version.Major = 0xFF; |
472 | MK3MAG_Version.Major = 0xFF; |
472 | MK3MAG_Version.Minor = 0xFF; |
473 | MK3MAG_Version.Minor = 0xFF; |
473 | MK3MAG_Version.Patch = 0xFF; |
474 | MK3MAG_Version.Patch = 0xFF; |
474 | MK3MAG_Version.Compatible = 0xFF; |
475 | MK3MAG_Version.Compatible = 0xFF; |
475 | 476 | ||
476 | while ((MK3MAG_Version.Major == 0xFF) && (repeat != 0)) |
477 | while ((MK3MAG_Version.Major == 0xFF) && (repeat != 0)) |
477 | { |
478 | { |
478 | I2C1_SendCommand(I2C_CMD_VERSION); |
479 | I2C1_SendCommand(I2C_CMD_VERSION); |
479 | I2C1_Timeout = SetDelay(500); |
480 | I2C1_Timeout = SetDelay(500); |
480 | while (!CheckDelay(I2C1_Timeout)) if (MK3MAG_Version.Major != 0xFF) break; |
481 | while (!CheckDelay(I2C1_Timeout)) if (MK3MAG_Version.Major != 0xFF) break; |
481 | repeat--; |
482 | repeat--; |
482 | } |
483 | } |
483 | 484 | ||
484 | if (MK3MAG_Version.Major != 0xFF) |
485 | if (MK3MAG_Version.Major != 0xFF) |
485 | { |
486 | { |
486 | sprintf(msg, "\n\r MK3Mag V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
487 | sprintf(msg, "\n\r MK3Mag V%d.%d%c", MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a' + MK3MAG_Version.Patch); |
487 | UART1_PutString(msg); |
488 | UART1_PutString(msg); |
488 | sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible); |
489 | sprintf(msg, " Compatible: %d", MK3MAG_Version.Compatible); |
489 | UART1_PutString(msg); |
490 | UART1_PutString(msg); |
490 | } |
491 | } |
491 | else UART1_PutString("\n\rNo version information from MK3Mag."); |
492 | else UART1_PutString("\n\rNo version information from MK3Mag."); |
492 | 493 | ||
493 | } |
494 | } |
494 | 495 |