Rev 1 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + Copyright (c) 2008 Ingo Busker, Holger Buss |
||
6 | // + Nur für den privaten Gebrauch |
||
7 | // + FOR NON COMMERCIAL USE ONLY |
||
8 | // + www.MikroKopter.com |
||
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
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. |
||
12 | // + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt |
||
13 | // + bzgl. der Nutzungsbedingungen aufzunehmen. |
||
14 | // + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen, |
||
15 | // + Verkauf von Luftbildaufnahmen, usw. |
||
16 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
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 |
||
19 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
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" |
||
22 | // + eindeutig als Ursprung verlinkt werden |
||
23 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
24 | // + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion |
||
25 | // + Benutzung auf eigene Gefahr |
||
26 | // + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden |
||
27 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
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 |
||
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
||
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 |
||
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 |
||
38 | // + for non-commercial use (directly or indirectly) |
||
39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
||
40 | // + with our written permission |
||
41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
||
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 |
||
44 | // |
||
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 |
||
47 | // + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
48 | // + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
||
49 | // + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
||
50 | // + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
||
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 |
||
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 |
||
55 | // + POSSIBILITY OF SUCH DAMAGE. |
||
56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
57 | #include "main.h" |
||
58 | |||
59 | u8 I2C_RxBufferSize, I2C_TxBufferSize; |
||
60 | u8 *I2C_TxBuffer, *I2C_RxBuffer; |
||
61 | u8 Tx_Idx=0, Rx_Idx=0, I2C_Direction; |
||
62 | u8 I2C_Command; |
||
63 | |||
64 | struct str_I2C_Heading I2C_Heading; |
||
65 | struct str_I2C_WriteNickRoll I2C_WriteNickRoll; |
||
66 | struct str_I2C_Mag I2C_Mag; |
||
67 | struct str_I2C_EEPROM I2C_ReadEEPROM, I2C_WriteEEPROM; |
||
68 | struct str_I2C_Version I2C_Version; |
||
69 | struct str_I2C_WriteCal I2C_WriteCal; |
||
70 | u8 CompassUpdateActiv = 0; |
||
71 | u8 CompassCalState = 0; |
||
72 | volatile u8 I2C_ReadRequest = 0; |
||
73 | |||
74 | //-------------------------------------------------------------- |
||
75 | void I2C1_Init(void) |
||
76 | { |
||
77 | I2C_InitTypeDef I2C_Struct; |
||
78 | GPIO_InitTypeDef GPIO_Struct; |
||
79 | |||
80 | SerialPutString("I2C init..."); |
||
81 | SCU_APBPeriphClockConfig(__I2C1,ENABLE); |
||
82 | I2C_DeInit(I2C1); |
||
83 | |||
84 | GPIO_Struct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
||
85 | GPIO_Struct.GPIO_Type = GPIO_Type_OpenCollector; |
||
86 | GPIO_Struct.GPIO_IPConnected = GPIO_IPConnected_Enable; |
||
87 | GPIO_Struct.GPIO_Alternate=GPIO_OutputAlt2; |
||
88 | GPIO_Init(GPIO2, &GPIO_Struct); |
||
89 | |||
90 | I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; |
||
91 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
||
92 | I2C_Struct.I2C_CLKSpeed = 20000; |
||
93 | I2C_Struct.I2C_OwnAddress = I2C_SLAVE_ADDRESS; |
||
94 | I2C_Init(I2C1, &I2C_Struct); |
||
95 | |||
96 | I2C_Cmd(I2C1, ENABLE); |
||
97 | I2C_ITConfig(I2C1, ENABLE); |
||
98 | |||
99 | VIC_Config(I2C1_ITLine,VIC_IRQ , 8); |
||
100 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
||
101 | |||
102 | I2C_Heading.Heading = 0; |
||
103 | SerialPutString("ok\n\r"); |
||
104 | |||
105 | |||
106 | } |
||
107 | |||
108 | //-------------------------------------------------------------- |
||
109 | void I2C1_IRQHandler(void) |
||
110 | { |
||
111 | |||
112 | switch (I2C_GetLastEvent(I2C1)) |
||
113 | { |
||
114 | case I2C_EVENT_MASTER_MODE_SELECT: // EV5 |
||
115 | Tx_Idx = 0; |
||
116 | Rx_Idx = 0; |
||
117 | I2C_Send7bitAddress(I2C1, I2C_SLAVE_ADDRESS, I2C_Direction); |
||
118 | // DebugOut.Analog[16]++; |
||
119 | break; |
||
120 | |||
121 | case I2C_EVENT_MASTER_MODE_SELECTED: // EV6 |
||
122 | // Clear EV6 by set again the PE bit |
||
123 | I2C1->CR |= 0x20; |
||
124 | if (I2C_Direction == I2C_MODE_TRANSMITTER) |
||
125 | { |
||
126 | I2C_SendData(I2C1, I2C_Command);//EV8 just after EV6 |
||
127 | } |
||
128 | break; |
||
129 | |||
130 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: // EV8 |
||
131 | // DebugOut.Analog[17]++; |
||
132 | if ( Tx_Idx >= I2C_TxBufferSize ) |
||
133 | { |
||
134 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
135 | Tx_Idx = 0; |
||
136 | if (I2C_RxBufferSize > 0) // wird Antwort erwartet ? |
||
137 | { |
||
138 | I2C_ReadRequest = 1; |
||
139 | TimerI2CReadDelay = SetDelay(10); |
||
140 | } |
||
141 | } |
||
142 | else |
||
143 | { |
||
144 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
||
145 | Tx_Idx++; |
||
146 | } |
||
147 | break; |
||
148 | |||
149 | case I2C_EVENT_MASTER_BYTE_RECEIVED: // EV7 |
||
150 | DebugOut.Analog[16]++; |
||
151 | //if (Rx_Idx == 1) DebugOut.Analog[19] = I2C_RxBuffer[0]; |
||
152 | if (Rx_Idx < I2C_RxBufferSize) |
||
153 | { I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
||
154 | |||
155 | } else I2C_ReceiveData(I2C1); |
||
156 | |||
157 | Rx_Idx++; |
||
158 | if ( Rx_Idx == I2C_RxBufferSize-2 ) |
||
159 | { |
||
160 | I2C_AcknowledgeConfig (I2C1, DISABLE); |
||
161 | } |
||
162 | if ( Rx_Idx == I2C_RxBufferSize -1 ) |
||
163 | { |
||
164 | I2C_GenerateSTOP(I2C1, ENABLE); |
||
165 | if (I2C_Command == I2C_CMD_READ_HEADING) CompassUpdateActiv = 0; |
||
166 | } |
||
167 | break; |
||
168 | |||
169 | default: |
||
170 | break; |
||
171 | } |
||
172 | } |
||
173 | //---------------------------------------------------------------- |
||
174 | void SendI2C_Command(u8 command) |
||
175 | { |
||
176 | I2C_Command = command; |
||
177 | //I2C_GenerateSTOP(I2C1, ENABLE); |
||
178 | |||
179 | switch (command) |
||
180 | { |
||
181 | case I2C_CMD_VERSION: |
||
182 | I2C_RxBuffer = (u8 *)&I2C_Version; |
||
183 | I2C_RxBufferSize = sizeof(I2C_Version); |
||
184 | I2C_TxBufferSize = 0; |
||
185 | break; |
||
186 | case I2C_CMD_WRITE_EEPROM: |
||
187 | I2C_RxBufferSize = 0; |
||
188 | I2C_TxBuffer = (u8 *)&I2C_WriteEEPROM; |
||
189 | I2C_TxBufferSize = sizeof(I2C_WriteEEPROM); |
||
190 | break; |
||
191 | case I2C_CMD_WRITE_CAL: |
||
192 | I2C_RxBufferSize = 0; |
||
193 | I2C_WriteCal.CalByte = CompassCalState; |
||
194 | I2C_TxBuffer = (u8 *)&I2C_WriteCal; |
||
195 | I2C_TxBufferSize = sizeof(I2C_WriteCal); |
||
196 | break; |
||
197 | case I2C_CMD_READ_EEPROM: |
||
198 | I2C_RxBuffer = (u8 *)&I2C_ReadEEPROM.Inhalt; |
||
199 | I2C_RxBufferSize = 2; |
||
200 | I2C_TxBuffer = (u8 *)&I2C_ReadEEPROM; |
||
201 | I2C_TxBufferSize = 1; |
||
202 | break; |
||
203 | case I2C_CMD_READ_MAG: |
||
204 | I2C_RxBuffer = (u8 *)&I2C_Mag; |
||
205 | I2C_RxBufferSize = sizeof(I2C_Mag); |
||
206 | I2C_TxBufferSize = 0; |
||
207 | break; |
||
208 | case I2C_CMD_READ_HEADING: |
||
209 | CompassUpdateActiv = 1; |
||
210 | I2C_RxBuffer = (u8 *)&I2C_Heading; |
||
211 | I2C_RxBufferSize = sizeof(I2C_Heading); |
||
212 | I2C_TxBuffer = (u8 *)&I2C_WriteNickRoll; |
||
213 | I2C_TxBufferSize = sizeof(I2C_WriteNickRoll); |
||
214 | break; |
||
215 | |||
216 | } |
||
217 | if (I2C_RxBufferSize > 0) |
||
218 | { |
||
219 | I2C_RxBufferSize++; |
||
220 | if (I2C_RxBufferSize < 3) I2C_RxBufferSize = 3; |
||
221 | } |
||
222 | |||
223 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
||
224 | I2C_Direction = I2C_MODE_TRANSMITTER; |
||
225 | |||
226 | I2C_GenerateStart(I2C1, ENABLE); |
||
227 | |||
228 | } |