Rev 242 | Rev 252 | 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 |
||
171 | ingob | 6 | // + Nur für den privaten Gebrauch / NON-COMMERCIAL USE ONLY |
1 | ingob | 7 | // + FOR NON COMMERCIAL USE ONLY |
8 | // + www.MikroKopter.com |
||
9 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
37 | killagreg | 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. |
||
1 | ingob | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
37 | killagreg | 17 | // + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht, |
1 | ingob | 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 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
171 | ingob | 28 | // + Die Portierung oder Nutzung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur |
1 | ingob | 29 | // + mit unserer Zustimmung zulässig |
30 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
31 | // + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen |
||
32 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
37 | killagreg | 33 | // + Redistributions of source code (with or without modifications) must retain the above copyright notice, |
1 | ingob | 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 | killagreg | 37 | // + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted |
1 | ingob | 38 | // + for non-commercial use (directly or indirectly) |
37 | killagreg | 39 | // + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted |
1 | ingob | 40 | // + with our written permission |
37 | killagreg | 41 | // + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be |
42 | // + clearly linked as origin |
||
171 | ingob | 43 | // + * porting the sources to other systems or using the software on other systems (except hardware from www.mikrokopter.de) is not allowed |
1 | ingob | 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 |
||
37 | killagreg | 55 | // + POSSIBILITY OF SUCH DAMAGE. |
1 | ingob | 56 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
41 | ingob | 57 | #include <string.h> |
58 | #include "91x_lib.h" |
||
59 | #include "i2c.h" |
||
60 | #include "uart1.h" |
||
119 | killagreg | 61 | #include "timer1.h" |
136 | killagreg | 62 | #include "config.h" |
41 | ingob | 63 | #include "led.h" |
1 | ingob | 64 | |
248 | killagreg | 65 | |
241 | killagreg | 66 | volatile u8 I2C_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs |
1 | ingob | 67 | |
248 | killagreg | 68 | // number of bytes to send |
69 | volatile u8 I2C_TxBufferSize; |
||
70 | // number of bytes to receive |
||
41 | ingob | 71 | volatile u8 I2C_RxBufferSize; |
248 | killagreg | 72 | // the transfer buffer |
73 | volatile u8 I2C_Buffer[I2C_BUFFER_LEN]; |
||
74 | // the transfer direction |
||
41 | ingob | 75 | volatile u8 I2C_Direction; |
248 | killagreg | 76 | // the slave address |
241 | killagreg | 77 | volatile u8 I2C_SlaveAddress = 0x00; |
248 | killagreg | 78 | // function pointer to process the received bytes |
241 | killagreg | 79 | I2C_pRxHandler_t I2C_pRxHandler = NULL; |
248 | killagreg | 80 | // goblal timeout |
41 | ingob | 81 | volatile u32 I2C1_Timeout = 0; |
1 | ingob | 82 | |
83 | //-------------------------------------------------------------- |
||
84 | void I2C1_Init(void) |
||
85 | { |
||
41 | ingob | 86 | I2C_InitTypeDef I2C_Struct; |
87 | GPIO_InitTypeDef GPIO_InitStructure; |
||
149 | killagreg | 88 | |
241 | killagreg | 89 | I2C_State = I2C_STATE_OFF; |
37 | killagreg | 90 | |
110 | killagreg | 91 | UART1_PutString("\r\n I2C init..."); |
41 | ingob | 92 | // enable Port 2 peripherie |
93 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
||
94 | // disable a reset state |
||
95 | SCU_APBPeriphReset(__GPIO2, DISABLE); |
||
1 | ingob | 96 | |
41 | ingob | 97 | // free a busy bus |
1 | ingob | 98 | |
41 | ingob | 99 | // At switch on I2C devices can get in a state where they |
100 | // are still waiting for a command due to all the bus lines bouncing |
||
101 | // around at startup have started clocking data into the device(s). |
||
102 | // Enable the ports as open collector port outputs |
||
103 | // and clock out at least 9 SCL pulses, then generate a stop |
||
104 | // condition and then leave the clock line high. |
||
37 | killagreg | 105 | |
41 | ingob | 106 | // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation |
107 | GPIO_StructInit(&GPIO_InitStructure); |
||
108 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
||
109 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
||
110 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
||
196 | killagreg | 111 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
41 | ingob | 112 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
113 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
37 | killagreg | 114 | |
41 | ingob | 115 | u8 i; |
116 | u32 delay; |
||
117 | // set SCL high and then SDA to low (start condition) |
||
118 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
||
119 | delay = SetDelay(1); |
||
120 | while (!CheckDelay(delay)); |
||
121 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET); |
||
122 | // toggle SCL at least 10 times from high to low to high |
||
123 | for(i = 0; i < 10; i++) |
||
124 | { |
||
125 | delay = SetDelay(1); |
||
126 | while (!CheckDelay(delay)); |
||
1 | ingob | 127 | |
41 | ingob | 128 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET); |
129 | delay = SetDelay(1); |
||
130 | while (!CheckDelay(delay)); |
||
131 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
||
132 | } |
||
133 | delay = SetDelay(1); |
||
134 | while (!CheckDelay(delay)); |
||
135 | // create stop condition setting SDA HIGH when SCL is HIGH |
||
136 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET); |
||
37 | killagreg | 137 | |
138 | |||
41 | ingob | 139 | // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT |
140 | GPIO_StructInit(&GPIO_InitStructure); |
||
141 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
||
142 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
||
143 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
||
196 | killagreg | 144 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable; |
41 | ingob | 145 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT |
146 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
147 | |||
148 | // enable I2C peripherie |
||
149 | SCU_APBPeriphClockConfig(__I2C1,ENABLE); |
||
150 | // reset I2C peripherie |
||
151 | SCU_APBPeriphReset(__I2C1,ENABLE); |
||
152 | SCU_APBPeriphReset(__I2C1,DISABLE); |
||
153 | |||
154 | I2C_DeInit(I2C1); |
||
155 | I2C_StructInit(&I2C_Struct); |
||
156 | I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; |
||
157 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
||
136 | killagreg | 158 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
41 | ingob | 159 | I2C_Struct.I2C_OwnAddress = 0x00; |
160 | I2C_Init(I2C1, &I2C_Struct); |
||
161 | |||
248 | killagreg | 162 | // empty rx and tx buffer counters |
41 | ingob | 163 | I2C_TxBufferSize = 0; |
164 | I2C_RxBufferSize = 0; |
||
165 | |||
166 | I2C_Cmd(I2C1, ENABLE); |
||
167 | I2C_ITConfig(I2C1, ENABLE); |
||
168 | |||
136 | killagreg | 169 | VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
41 | ingob | 170 | |
189 | killagreg | 171 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
41 | ingob | 172 | I2C_GenerateSTOP(I2C1, ENABLE); |
241 | killagreg | 173 | I2C_State = I2C_STATE_IDLE; |
41 | ingob | 174 | |
248 | killagreg | 175 | // start some dummy transmissions cycles |
176 | // to get the irq routine to work |
||
177 | for(i=0;i<10;i++) |
||
178 | { |
||
179 | I2C_State = I2C_STATE_BUFFBUSY; |
||
180 | I2C_Transmission(0,1,0,1); |
||
181 | if(I2C_WaitForEndOfTransmission(10)) break; |
||
182 | UART1_Putchar('.'); |
||
183 | } |
||
110 | killagreg | 184 | UART1_PutString("ok"); |
1 | ingob | 185 | } |
186 | |||
41 | ingob | 187 | |
1 | ingob | 188 | //-------------------------------------------------------------- |
41 | ingob | 189 | void I2C1_Deinit(void) |
1 | ingob | 190 | { |
79 | killagreg | 191 | GPIO_InitTypeDef GPIO_InitStructure; |
110 | killagreg | 192 | UART1_PutString("\r\n I2C deinit..."); |
41 | ingob | 193 | I2C_GenerateStart(I2C1, DISABLE); |
194 | I2C_GenerateSTOP(I2C1, ENABLE); |
||
195 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
||
241 | killagreg | 196 | I2C_State = I2C_STATE_OFF; |
41 | ingob | 197 | I2C_ITConfig(I2C1, DISABLE); |
198 | I2C_Cmd(I2C1, DISABLE); |
||
199 | I2C_DeInit(I2C1); |
||
200 | SCU_APBPeriphClockConfig(__I2C1, DISABLE); |
||
79 | killagreg | 201 | |
41 | ingob | 202 | // set ports to input |
203 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
||
204 | GPIO_StructInit(&GPIO_InitStructure); |
||
205 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
||
79 | killagreg | 206 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
207 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
||
196 | killagreg | 208 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
79 | killagreg | 209 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
210 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
1 | ingob | 211 | |
241 | killagreg | 212 | // empty rx and tx buffer |
41 | ingob | 213 | I2C_TxBufferSize = 0; |
214 | I2C_RxBufferSize = 0; |
||
37 | killagreg | 215 | |
189 | killagreg | 216 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
37 | killagreg | 217 | |
110 | killagreg | 218 | UART1_PutString("ok"); |
41 | ingob | 219 | } |
1 | ingob | 220 | |
41 | ingob | 221 | //-------------------------------------------------------------- |
222 | void I2C1_IRQHandler(void) |
||
223 | { |
||
241 | killagreg | 224 | static u8 Rx_Idx = 0, Tx_Idx = 0; |
41 | ingob | 225 | u16 status; |
189 | killagreg | 226 | |
195 | killagreg | 227 | //IENABLE; // do not enable IRQ nesting for I2C!!!! |
41 | ingob | 228 | // detemine I2C State |
229 | status = I2C_GetLastEvent(I2C1); |
||
230 | |||
231 | if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured |
||
79 | killagreg | 232 | { // Set and subsequently clear the STOP bit while BTF is set. |
41 | ingob | 233 | while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
234 | { |
||
189 | killagreg | 235 | I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
41 | ingob | 236 | I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
237 | } |
||
241 | killagreg | 238 | I2C_State = I2C_STATE_IDLE; |
189 | killagreg | 239 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
41 | ingob | 240 | LED_GRN_OFF; |
189 | killagreg | 241 | return; |
41 | ingob | 242 | } |
243 | else |
||
244 | { // depending on current i2c state |
||
241 | killagreg | 245 | switch(status) |
41 | ingob | 246 | { |
247 | // the start condition was initiated on the bus |
||
248 | case I2C_EVENT_MASTER_MODE_SELECT: |
||
249 | LED_GRN_ON; |
||
250 | // update current bus state variable |
||
241 | killagreg | 251 | // jump to rx state if there is nothing to send |
41 | ingob | 252 | switch(I2C_Direction) |
253 | { |
||
254 | case I2C_MODE_TRANSMITTER: |
||
241 | killagreg | 255 | I2C_State = I2C_STATE_TX_PROGRESS; |
41 | ingob | 256 | break; |
79 | killagreg | 257 | |
41 | ingob | 258 | case I2C_MODE_RECEIVER: |
248 | killagreg | 259 | if (I2C_RxBufferSize == 0) // nothing to send? |
41 | ingob | 260 | { |
261 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
189 | killagreg | 262 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
263 | LED_GRN_OFF; |
||
241 | killagreg | 264 | I2C_State = I2C_STATE_IDLE; |
41 | ingob | 265 | return; |
266 | } |
||
267 | else |
||
268 | { |
||
241 | killagreg | 269 | I2C_State = I2C_STATE_RX_PROGRESS; |
41 | ingob | 270 | } |
271 | break; |
||
79 | killagreg | 272 | |
41 | ingob | 273 | default: // invalid direction |
274 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
189 | killagreg | 275 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
276 | LED_GRN_OFF; |
||
241 | killagreg | 277 | I2C_State = I2C_STATE_IDLE; |
41 | ingob | 278 | return; |
279 | } |
||
280 | // enable acknowledge |
||
281 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
||
282 | // send address/direction byte on the bus |
||
241 | killagreg | 283 | I2C_Send7bitAddress(I2C1, I2C_SlaveAddress, I2C_Direction); |
41 | ingob | 284 | break; |
79 | killagreg | 285 | |
41 | ingob | 286 | // the address byte was send |
287 | case I2C_EVENT_MASTER_MODE_SELECTED: |
||
288 | // Clear EV6 by set again the PE bit |
||
79 | killagreg | 289 | I2C_Cmd(I2C1, ENABLE); |
41 | ingob | 290 | switch(I2C_State) |
291 | { |
||
241 | killagreg | 292 | case I2C_STATE_TX_PROGRESS: |
293 | // send 1st data byte |
||
41 | ingob | 294 | Tx_Idx = 0; |
248 | killagreg | 295 | I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]); |
241 | killagreg | 296 | Tx_Idx++; |
41 | ingob | 297 | // reset timeout |
189 | killagreg | 298 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset |
41 | ingob | 299 | break; |
79 | killagreg | 300 | |
241 | killagreg | 301 | case I2C_STATE_RX_PROGRESS: |
41 | ingob | 302 | Rx_Idx = 0; |
241 | killagreg | 303 | // disable acknoledge if only one byte has to be read |
304 | if(I2C_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C1, DISABLE); |
||
41 | ingob | 305 | break; |
79 | killagreg | 306 | |
41 | ingob | 307 | default: // unknown I2C state |
308 | // should never happen |
||
309 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
189 | killagreg | 310 | LED_GRN_OFF; |
311 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
||
241 | killagreg | 312 | I2C_State = I2C_STATE_IDLE; |
189 | killagreg | 313 | return; |
41 | ingob | 314 | break; |
315 | } |
||
316 | break; |
||
79 | killagreg | 317 | |
41 | ingob | 318 | // the master has transmitted a byte and slave has been acknowledged |
319 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
||
79 | killagreg | 320 | |
41 | ingob | 321 | // some bytes have to be transmitted |
322 | if(Tx_Idx < I2C_TxBufferSize) |
||
323 | { |
||
248 | killagreg | 324 | I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]); |
241 | killagreg | 325 | Tx_Idx++; |
41 | ingob | 326 | } |
189 | killagreg | 327 | else // last byte was send |
328 | { |
||
41 | ingob | 329 | // generate stop or repeated start condition |
241 | killagreg | 330 | if (I2C_RxBufferSize > 0) // is any answer byte expected? |
41 | ingob | 331 | { |
79 | killagreg | 332 | I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
333 | I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
||
41 | ingob | 334 | } |
335 | else |
||
336 | { // stop communication |
||
337 | I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
||
189 | killagreg | 338 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
41 | ingob | 339 | LED_GRN_OFF; |
241 | killagreg | 340 | I2C_State = I2C_STATE_IDLE; // ready for new actions |
41 | ingob | 341 | } |
342 | } |
||
343 | break; |
||
79 | killagreg | 344 | |
41 | ingob | 345 | // the master has received a byte from the slave |
79 | killagreg | 346 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
41 | ingob | 347 | // some bytes have to be received |
241 | killagreg | 348 | if ( Rx_Idx+1 < I2C_RxBufferSize) |
41 | ingob | 349 | { // copy received byte from the data register to the rx-buffer |
248 | killagreg | 350 | I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
41 | ingob | 351 | } |
241 | killagreg | 352 | else // if the last byte was received |
41 | ingob | 353 | { |
354 | // generate a STOP condition on the bus before reading data register |
||
355 | I2C_GenerateSTOP(I2C1, ENABLE); |
||
248 | killagreg | 356 | I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
241 | killagreg | 357 | // call the rx handler function to process recieved data |
248 | killagreg | 358 | if(I2C_pRxHandler != NULL) (*I2C_pRxHandler)((u8*)I2C_Buffer, I2C_RxBufferSize); |
241 | killagreg | 359 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); |
360 | DebugOut.Analog[15]++; |
||
189 | killagreg | 361 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
362 | LED_GRN_OFF; |
||
241 | killagreg | 363 | I2C_State = I2C_STATE_IDLE; |
189 | killagreg | 364 | return; |
41 | ingob | 365 | } |
366 | Rx_Idx++; |
||
367 | // if the 2nd last byte was received disable acknowledge for the last one |
||
241 | killagreg | 368 | if ( (Rx_Idx + 1) == I2C_RxBufferSize ) |
41 | ingob | 369 | { |
241 | killagreg | 370 | I2C_AcknowledgeConfig(I2C1, DISABLE); |
41 | ingob | 371 | } |
372 | break; |
||
79 | killagreg | 373 | |
241 | killagreg | 374 | default:// unknown event |
375 | // should never happen |
||
376 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
377 | LED_GRN_OFF; |
||
378 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
||
379 | I2C_State = I2C_STATE_IDLE; |
||
41 | ingob | 380 | break; |
381 | } |
||
382 | } |
||
195 | killagreg | 383 | //IDISABLE; // do not enable IRQ nesting for I2C!!!! |
1 | ingob | 384 | } |
248 | killagreg | 385 | |
241 | killagreg | 386 | // ---------------------------------------------------------------------------------------- |
248 | killagreg | 387 | // wait for end of transmission |
388 | u8 I2C_WaitForEndOfTransmission(u32 timeout) |
||
389 | { |
||
390 | u32 time = SetDelay(timeout); |
||
391 | while(I2C_State != I2C_STATE_IDLE) |
||
392 | { |
||
393 | if(CheckDelay(time)) return(0); |
||
394 | } |
||
395 | return(1); |
||
396 | } |
||
397 | |||
398 | // ---------------------------------------------------------------------------------------- |
||
399 | // try to get access to the transfer buffer within a timeout limit |
||
400 | // returs 1 on success and 0 on error/timeout |
||
401 | u8 I2C_LockBuffer(u32 timeout) |
||
402 | { |
||
403 | if(I2C_WaitForEndOfTransmission(timeout)) |
||
404 | { |
||
405 | I2C_State = I2C_STATE_BUFFBUSY; |
||
406 | return(1); |
||
407 | } |
||
408 | else return(0); |
||
409 | } |
||
410 | // ---------------------------------------------------------------------------------------- |
||
241 | killagreg | 411 | // initate an i2c transmission |
248 | killagreg | 412 | u8 I2C_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes) |
1 | ingob | 413 | { |
241 | killagreg | 414 | u8 retval = 0; |
248 | killagreg | 415 | if(I2C_State == I2C_STATE_BUFFBUSY) |
416 | { |
||
417 | if((RxBytes > I2C_BUFFER_LEN) || (TxBytes > I2C_BUFFER_LEN)) |
||
418 | { |
||
419 | I2C_State = I2C_STATE_IDLE; |
||
420 | return(retval); |
||
421 | } |
||
241 | killagreg | 422 | I2C_RxBufferSize = RxBytes; |
248 | killagreg | 423 | I2C_TxBufferSize = TxBytes; |
241 | killagreg | 424 | // set direction to master transmitter |
248 | killagreg | 425 | if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER; |
426 | else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER; |
||
241 | killagreg | 427 | else // nothing to send or receive |
189 | killagreg | 428 | { |
248 | killagreg | 429 | I2C_State = I2C_STATE_IDLE; |
241 | killagreg | 430 | I2C_TxBufferSize = 0; |
431 | I2C_RxBufferSize = 0; |
||
432 | return(retval); |
||
189 | killagreg | 433 | } |
241 | killagreg | 434 | // update slave address and rx data handler funbction pointer |
435 | I2C_SlaveAddress = SlaveAddr; |
||
436 | I2C_pRxHandler = pRxHandler; |
||
189 | killagreg | 437 | // test on busy flag and clear it |
438 | I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
||
242 | killagreg | 439 | // enable I2C IRQ |
440 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
||
189 | killagreg | 441 | // initiate start condition on the bus |
442 | I2C_GenerateStart(I2C1, ENABLE); |
||
241 | killagreg | 443 | retval = 1; |
444 | } |
||
445 | return(retval); |
||
41 | ingob | 446 | } |