Rev 465 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
464 | ingob | 1 | /*#######################################################################################*/ |
2 | /* !!! THIS IS NOT FREE SOFTWARE !!! */ |
||
3 | /*#######################################################################################*/ |
||
4 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
5 | // + www.MikroKopter.com |
||
6 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
7 | // + Software Nutzungsbedingungen (english version: see below) |
||
8 | // + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt - |
||
9 | // + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den |
||
10 | // + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool |
||
11 | // + - nachfolgend Software genannt - nur für private Zwecke zu nutzen. |
||
12 | // + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig. |
||
13 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
14 | // + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im |
||
15 | // + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu. |
||
16 | // + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie |
||
17 | // + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden. |
||
18 | // + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren |
||
19 | // + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
20 | // + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren |
||
21 | // + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand |
||
22 | // + des Mitverschuldens offen. |
||
23 | // + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet. |
||
24 | // + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt. |
||
25 | // + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern. |
||
26 | // + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang |
||
27 | // + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt. |
||
28 | // + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software. |
||
29 | // + #### ENDE DER NUTZUNGSBEDINGUNGEN ####' |
||
30 | // + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar. |
||
31 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
32 | // + Software LICENSING TERMS |
||
33 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
34 | // + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor - |
||
35 | // + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware |
||
36 | // + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*. |
||
37 | // + The Software may only be used with the Licensor's products. |
||
38 | // + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this |
||
39 | // + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this |
||
40 | // + agreement shall be the property of the Licensor. |
||
41 | // + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other |
||
42 | // + features that can be used to identify the program may not be altered or defaced by the customer. |
||
43 | // + The customer shall be responsible for taking reasonable precautions |
||
44 | // + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the |
||
45 | // + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and |
||
46 | // + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product |
||
47 | // + liability. However, the Licensor shall be entitled to the defense of contributory negligence. |
||
48 | // + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test |
||
49 | // + the software for his purpose before any operational usage. The customer will backup his data before using the software. |
||
50 | // + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data |
||
51 | // + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations. |
||
52 | // + *) The territory aspect only refers to the place where the Software is used, not its programmed range. |
||
53 | // + #### END OF LICENSING TERMS #### |
||
54 | // + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de. |
||
55 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
56 | #include <string.h> |
||
57 | #include "91x_lib.h" |
||
58 | #include "i2c0.h" |
||
59 | #include "i2c.h" |
||
60 | #include "uart1.h" |
||
61 | #include "timer1.h" |
||
62 | #include "config.h" |
||
63 | #include "led.h" |
||
64 | |||
65 | |||
66 | volatile u8 I2C0_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs |
||
67 | volatile u8 I2C0_Error = I2C_ERROR_NOACK; // only one byte! |
||
68 | |||
69 | // number of bytes to send |
||
70 | volatile u8 I2C0_TxBufferSize; |
||
71 | // number of bytes to receive |
||
72 | volatile u8 I2C0_RxBufferSize; |
||
73 | // the transfer buffer |
||
74 | volatile u8 I2C0_Buffer[I2C0_Buffer_LEN]; |
||
75 | // the transfer direction |
||
76 | volatile u8 I2C0_Direction; |
||
77 | // the slave address |
||
78 | volatile u8 I2C0_SlaveAddress = 0x00; |
||
79 | // function pointer to process the received bytes |
||
80 | I2C_pRxHandler_t I2C0_pRxHandler = NULL; |
||
81 | // goblal timeout |
||
82 | volatile u32 I2C0_Timeout = 0; |
||
83 | |||
84 | //-------------------------------------------------------------- |
||
85 | void I2C0_Init(void) |
||
86 | { |
||
87 | I2C_InitTypeDef I2C_Struct; |
||
88 | GPIO_InitTypeDef GPIO_InitStructure; |
||
89 | |||
90 | I2C0_State = I2C_STATE_OFF; |
||
91 | |||
92 | UART1_PutString("\r\n I2C0 init..."); |
||
93 | // enable Port 2 peripherie |
||
94 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
||
95 | // disable a reset state |
||
96 | SCU_APBPeriphReset(__GPIO2, DISABLE); |
||
97 | |||
98 | // free a busy bus |
||
99 | |||
100 | // At switch on I2C devices can get in a state where they |
||
101 | // are still waiting for a command due to all the bus lines bouncing |
||
102 | // around at startup have started clocking data into the device(s). |
||
103 | // Enable the ports as open collector port outputs |
||
104 | // and clock out at least 9 SCL pulses, then generate a stop |
||
105 | // condition and then leave the clock line high. |
||
106 | |||
107 | // configure P2.2->I2C0_CLKOUT and P2.3->I2C0_DOUT to normal port operation |
||
108 | GPIO_StructInit(&GPIO_InitStructure); |
||
109 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
||
110 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1; |
||
111 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
||
112 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
||
113 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
||
114 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
115 | |||
116 | u8 i; |
||
117 | u32 delay; |
||
118 | // set SCL high and then SDA to low (start condition) |
||
119 | GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_SET); |
||
120 | delay = SetDelay(1); |
||
121 | while (!CheckDelay(delay)); |
||
122 | GPIO_WriteBit(GPIO2, GPIO_Pin_1, Bit_RESET); |
||
123 | // toggle SCL at least 10 times from high to low to high |
||
124 | for(i = 0; i < 10; i++) |
||
125 | { |
||
126 | delay = SetDelay(1); |
||
127 | while (!CheckDelay(delay)); |
||
128 | |||
129 | GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_RESET); |
||
130 | delay = SetDelay(1); |
||
131 | while (!CheckDelay(delay)); |
||
132 | GPIO_WriteBit(GPIO2, GPIO_Pin_0, Bit_SET); |
||
133 | } |
||
134 | delay = SetDelay(1); |
||
135 | while (!CheckDelay(delay)); |
||
136 | // create stop condition setting SDA HIGH when SCL is HIGH |
||
137 | GPIO_WriteBit(GPIO2, GPIO_Pin_1, Bit_SET); |
||
138 | |||
139 | |||
140 | // reconfigure P2.2->I2C0_CLKOUT and P2.3->I2C0_DOUT |
||
141 | GPIO_StructInit(&GPIO_InitStructure); |
||
142 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
||
143 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; |
||
144 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
||
145 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable; |
||
146 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C0_CLKOUT, I2C0_DOUT |
||
147 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
148 | |||
149 | // enable I2C peripherie |
||
150 | SCU_APBPeriphClockConfig(__I2C0,ENABLE); |
||
151 | // reset I2C peripherie |
||
152 | SCU_APBPeriphReset(__I2C0,ENABLE); |
||
153 | SCU_APBPeriphReset(__I2C0,DISABLE); |
||
154 | |||
155 | I2C_DeInit(I2C0); |
||
156 | I2C_StructInit(&I2C_Struct); |
||
157 | I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; |
||
158 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
||
159 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
||
160 | I2C_Struct.I2C_OwnAddress = 0x00; |
||
161 | I2C_Init(I2C0, &I2C_Struct); |
||
162 | |||
163 | // empty rx and tx buffer counters |
||
164 | I2C0_TxBufferSize = 0; |
||
165 | I2C0_RxBufferSize = 0; |
||
166 | |||
167 | I2C_Cmd(I2C0, ENABLE); |
||
168 | I2C_ITConfig(I2C0, ENABLE); |
||
169 | |||
170 | VIC_Config(I2C0_ITLine, VIC_IRQ , PRIORITY_I2C0); |
||
171 | |||
172 | I2C0_Timeout = SetDelay(10*I2C0_Timeout); |
||
173 | I2C_GenerateSTOP(I2C0, ENABLE); |
||
174 | I2C0_State = I2C_STATE_IDLE; |
||
175 | |||
176 | // start some dummy transmissions cycles |
||
177 | // to get the irq routine to work |
||
178 | for(i=0;i<10;i++) |
||
179 | { |
||
180 | I2C0_State = I2C_STATE_BUFFBUSY; |
||
181 | I2C0_Transmission(0,1,0,1); |
||
182 | if(I2C0_WaitForEndOfTransmission(10)) break; |
||
183 | UART1_Putchar('.'); |
||
184 | } |
||
185 | UART1_PutString("ok"); |
||
186 | } |
||
187 | |||
188 | |||
189 | //-------------------------------------------------------------- |
||
190 | void I2C0_Deinit(void) |
||
191 | { |
||
192 | GPIO_InitTypeDef GPIO_InitStructure; |
||
193 | UART1_PutString("\r\n I2C deinit..."); |
||
194 | I2C_GenerateStart(I2C0, DISABLE); |
||
195 | I2C_GenerateSTOP(I2C0, ENABLE); |
||
196 | VIC_ITCmd(I2C0_ITLine, DISABLE); |
||
197 | I2C0_State = I2C_STATE_OFF; |
||
198 | I2C_ITConfig(I2C0, DISABLE); |
||
199 | I2C_Cmd(I2C0, DISABLE); |
||
200 | I2C_DeInit(I2C0); |
||
201 | SCU_APBPeriphClockConfig(__I2C0, DISABLE); |
||
202 | |||
203 | // set ports to input |
||
204 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
||
205 | GPIO_StructInit(&GPIO_InitStructure); |
||
206 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
||
207 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; |
||
208 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
||
209 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
||
210 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
||
211 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
212 | |||
213 | // empty rx and tx buffer |
||
214 | I2C0_TxBufferSize = 0; |
||
215 | I2C0_RxBufferSize = 0; |
||
216 | |||
217 | I2C0_Timeout = SetDelay(10*I2C0_Timeout); |
||
218 | |||
219 | UART1_PutString("ok"); |
||
220 | } |
||
221 | |||
222 | //-------------------------------------------------------------- |
||
223 | void I2C0_IRQHandler(void) |
||
224 | { |
||
225 | static u8 Rx_Idx = 0, Tx_Idx = 0; |
||
226 | u16 status; |
||
227 | |||
228 | //IENABLE; // do not enable IRQ nesting for I2C!!!! |
||
229 | // detemine I2C State |
||
230 | status = I2C_GetLastEvent(I2C0); |
||
231 | |||
232 | if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured |
||
233 | { // Set and subsequently clear the STOP bit while BTF is set. |
||
234 | while(I2C_GetFlagStatus (I2C0, I2C_FLAG_BTF) != RESET) |
||
235 | { |
||
236 | I2C_GenerateSTOP (I2C0, ENABLE); // free the bus |
||
237 | I2C_GenerateSTOP (I2C0, DISABLE); // free the bus |
||
238 | } |
||
239 | I2C0_State = I2C_STATE_IDLE; |
||
240 | I2C0_Error = I2C_ERROR_NOACK; |
||
241 | VIC_ITCmd(I2C0_ITLine, DISABLE); |
||
242 | return; |
||
243 | } |
||
244 | else |
||
245 | { // depending on current i2c state |
||
246 | switch(status) |
||
247 | { |
||
248 | // the start condition was initiated on the bus |
||
249 | case I2C_EVENT_MASTER_MODE_SELECT: |
||
250 | // update current bus state variable |
||
251 | // jump to rx state if there is nothing to send |
||
252 | switch(I2C0_Direction) |
||
253 | { |
||
254 | case I2C_MODE_TRANSMITTER: |
||
255 | I2C0_State = I2C_STATE_TX_PROGRESS; |
||
256 | break; |
||
257 | |||
258 | case I2C_MODE_RECEIVER: |
||
259 | if (I2C0_RxBufferSize == 0) // nothing to send? |
||
260 | { |
||
261 | I2C_GenerateSTOP (I2C0, ENABLE); |
||
262 | VIC_ITCmd(I2C0_ITLine, DISABLE); |
||
263 | I2C0_State = I2C_STATE_IDLE; |
||
264 | I2C0_Error = I2C_ERROR_NONE; |
||
265 | return; |
||
266 | } |
||
267 | else |
||
268 | { |
||
269 | I2C0_State = I2C_STATE_RX_PROGRESS; |
||
270 | } |
||
271 | break; |
||
272 | |||
273 | default: // invalid direction |
||
274 | I2C_GenerateSTOP (I2C0, ENABLE); |
||
275 | VIC_ITCmd(I2C0_ITLine, DISABLE); |
||
276 | I2C0_State = I2C_STATE_IDLE; |
||
277 | I2C0_Error = I2C_ERROR_UNKNOWN; |
||
278 | return; |
||
279 | } |
||
280 | // enable acknowledge |
||
281 | I2C_AcknowledgeConfig (I2C0, ENABLE); |
||
282 | // send address/direction byte on the bus |
||
283 | I2C_Send7bitAddress(I2C0, I2C0_SlaveAddress, I2C0_Direction); |
||
284 | break; |
||
285 | |||
286 | // the address byte was send |
||
287 | case I2C_EVENT_MASTER_MODE_SELECTED: |
||
288 | // Clear EV6 by set again the PE bit |
||
289 | I2C_Cmd(I2C0, ENABLE); |
||
290 | switch(I2C0_State) |
||
291 | { |
||
292 | case I2C_STATE_TX_PROGRESS: |
||
293 | // send 1st data byte |
||
294 | Tx_Idx = 0; |
||
295 | I2C_SendData(I2C0, I2C0_Buffer[Tx_Idx]); |
||
296 | Tx_Idx++; |
||
297 | // reset timeout |
||
298 | I2C0_Timeout = SetDelay(I2C0_Timeout); // after inactivity the I2C0 bus will be reset |
||
299 | break; |
||
300 | |||
301 | case I2C_STATE_RX_PROGRESS: |
||
302 | Rx_Idx = 0; |
||
303 | // disable acknoledge if only one byte has to be read |
||
304 | if(I2C0_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C0, DISABLE); |
||
305 | break; |
||
306 | |||
307 | default: // unknown I2C state |
||
308 | // should never happen |
||
309 | I2C_GenerateSTOP (I2C0, ENABLE); |
||
310 | VIC_ITCmd(I2C0_ITLine, DISABLE); |
||
311 | I2C0_State = I2C_STATE_IDLE; |
||
312 | I2C0_Error = I2C_ERROR_UNKNOWN; |
||
313 | return; |
||
314 | break; |
||
315 | } |
||
316 | break; |
||
317 | |||
318 | // the master has transmitted a byte and slave has been acknowledged |
||
319 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
||
320 | |||
321 | // some bytes have to be transmitted |
||
322 | if(Tx_Idx < I2C0_TxBufferSize) |
||
323 | { |
||
324 | I2C_SendData(I2C0, I2C0_Buffer[Tx_Idx]); |
||
325 | Tx_Idx++; |
||
326 | } |
||
327 | else // last byte was send |
||
328 | { |
||
329 | // generate stop or repeated start condition |
||
330 | if (I2C0_RxBufferSize > 0) // is any answer byte expected? |
||
331 | { |
||
332 | I2C0_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
||
333 | I2C_GenerateStart(I2C0, ENABLE); // initiate repeated start condition on the bus |
||
334 | } |
||
335 | else |
||
336 | { // stop communication |
||
337 | I2C_GenerateSTOP(I2C0, ENABLE); // generate stop condition to free the bus |
||
338 | VIC_ITCmd(I2C0_ITLine, DISABLE); |
||
339 | I2C0_State = I2C_STATE_IDLE; // ready for new actions |
||
340 | I2C0_Error = I2C_ERROR_NONE; |
||
341 | } |
||
342 | } |
||
343 | break; |
||
344 | |||
345 | // the master has received a byte from the slave |
||
346 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
||
347 | // some bytes have to be received |
||
348 | if ( Rx_Idx+1 < I2C0_RxBufferSize) |
||
349 | { // copy received byte from the data register to the rx-buffer |
||
350 | I2C0_Buffer[Rx_Idx] = I2C_ReceiveData(I2C0); |
||
351 | } |
||
352 | else // if the last byte was received |
||
353 | { |
||
354 | // generate a STOP condition on the bus before reading data register |
||
355 | I2C_GenerateSTOP(I2C0, ENABLE); |
||
356 | I2C0_Buffer[Rx_Idx] = I2C_ReceiveData(I2C0); |
||
357 | // call the rx handler function to process recieved data |
||
358 | if(I2C0_pRxHandler != NULL) (*I2C0_pRxHandler)((u8*)I2C0_Buffer, I2C0_RxBufferSize); |
||
359 | I2C0_Timeout = SetDelay(I2C0_Timeout); |
||
360 | DebugOut.Analog[15]++; |
||
361 | VIC_ITCmd(I2C0_ITLine, DISABLE); |
||
362 | I2C0_State = I2C_STATE_IDLE; |
||
363 | I2C0_Error = I2C_ERROR_NONE; |
||
364 | return; |
||
365 | } |
||
366 | Rx_Idx++; |
||
367 | // if the 2nd last byte was received disable acknowledge for the last one |
||
368 | if ( (Rx_Idx + 1) == I2C0_RxBufferSize ) |
||
369 | { |
||
370 | I2C_AcknowledgeConfig(I2C0, DISABLE); |
||
371 | } |
||
372 | break; |
||
373 | |||
374 | default:// unknown event |
||
375 | // should never happen |
||
376 | I2C_GenerateSTOP (I2C0, ENABLE); |
||
377 | VIC_ITCmd(I2C0_ITLine, DISABLE); |
||
378 | I2C0_State = I2C_STATE_IDLE; |
||
379 | I2C0_Error = I2C_ERROR_UNKNOWN; |
||
380 | break; |
||
381 | } |
||
382 | } |
||
383 | //IDISABLE; // do not enable IRQ nesting for I2C!!!! |
||
384 | VIC1->VAR = 0xFF; // write any value to VIC0 Vector address register |
||
385 | } |
||
386 | |||
387 | // ---------------------------------------------------------------------------------------- |
||
388 | // wait for end of transmission |
||
389 | u8 I2C0_WaitForEndOfTransmission(u32 timeout) |
||
390 | { |
||
391 | u32 time = SetDelay(timeout); |
||
392 | while(I2C0_State != I2C_STATE_IDLE) |
||
393 | { |
||
394 | if(CheckDelay(time)) return(0); |
||
395 | } |
||
396 | return(1); |
||
397 | } |
||
398 | |||
399 | // ---------------------------------------------------------------------------------------- |
||
400 | // try to get access to the transfer buffer within a timeout limit |
||
401 | // returs 1 on success and 0 on error/timeout |
||
402 | u8 I2C0_LockBuffer(u32 timeout) |
||
403 | { |
||
404 | if(I2C0_WaitForEndOfTransmission(timeout)) |
||
405 | { |
||
406 | I2C0_State = I2C_STATE_BUFFBUSY; |
||
407 | I2C0_Error = I2C_ERROR_UNKNOWN; |
||
408 | return(1); |
||
409 | } |
||
410 | else return(0); |
||
411 | } |
||
412 | // ---------------------------------------------------------------------------------------- |
||
413 | // initate an i2c transmission |
||
414 | u8 I2C0_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes) |
||
415 | { |
||
416 | u8 retval = 0; |
||
417 | if(I2C0_State == I2C_STATE_BUFFBUSY) |
||
418 | { |
||
419 | if((RxBytes > I2C0_Buffer_LEN) || (TxBytes > I2C0_Buffer_LEN)) |
||
420 | { |
||
421 | I2C0_State = I2C_STATE_IDLE; |
||
422 | return(retval); |
||
423 | } |
||
424 | I2C0_RxBufferSize = RxBytes; |
||
425 | I2C0_TxBufferSize = TxBytes; |
||
426 | // set direction to master transmitter |
||
427 | if( (I2C0_TxBufferSize > 0) && (I2C0_TxBufferSize < I2C0_Buffer_LEN) ) I2C0_Direction = I2C_MODE_TRANSMITTER; |
||
428 | else if (( I2C0_RxBufferSize > 0 ) && (I2C0_RxBufferSize < I2C0_Buffer_LEN) ) I2C0_Direction = I2C_MODE_RECEIVER; |
||
429 | else // nothing to send or receive |
||
430 | { |
||
431 | I2C0_State = I2C_STATE_IDLE; |
||
432 | I2C0_Error = I2C_ERROR_NONE; |
||
433 | I2C0_TxBufferSize = 0; |
||
434 | I2C0_RxBufferSize = 0; |
||
435 | return(retval); |
||
436 | } |
||
437 | // update slave address and rx data handler funbction pointer |
||
438 | I2C0_SlaveAddress = SlaveAddr; |
||
439 | I2C0_pRxHandler = pRxHandler; |
||
440 | // test on busy flag and clear it |
||
441 | I2C_CheckEvent( I2C0, I2C_FLAG_BUSY ); |
||
442 | // enable I2C IRQ |
||
443 | VIC_ITCmd(I2C0_ITLine, ENABLE); |
||
444 | // initiate start condition on the bus |
||
445 | I2C_GenerateStart(I2C0, ENABLE); |
||
446 | retval = 1; |
||
447 | } |
||
448 | return(retval); |
||
449 | } |