Rev 241 | Rev 248 | 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 | |
241 | killagreg | 65 | volatile u8 I2C_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs |
1 | ingob | 66 | |
41 | ingob | 67 | // rxbuffer |
68 | volatile u8 I2C_RxBufferSize; |
||
241 | killagreg | 69 | volatile u8 I2C_RxBuffer[I2C_RXBUFFER_LEN]; |
41 | ingob | 70 | // txbuffer |
71 | volatile u8 I2C_TxBufferSize; |
||
241 | killagreg | 72 | volatile u8 I2C_TxBuffer[I2C_TXBUFFER_LEN]; |
41 | ingob | 73 | |
74 | volatile u8 I2C_Direction; |
||
241 | killagreg | 75 | volatile u8 I2C_SlaveAddress = 0x00; |
76 | I2C_pRxHandler_t I2C_pRxHandler = NULL; |
||
41 | ingob | 77 | |
78 | volatile u32 I2C1_Timeout = 0; |
||
1 | ingob | 79 | |
80 | //-------------------------------------------------------------- |
||
81 | void I2C1_Init(void) |
||
82 | { |
||
41 | ingob | 83 | I2C_InitTypeDef I2C_Struct; |
84 | GPIO_InitTypeDef GPIO_InitStructure; |
||
149 | killagreg | 85 | |
241 | killagreg | 86 | I2C_State = I2C_STATE_OFF; |
37 | killagreg | 87 | |
110 | killagreg | 88 | UART1_PutString("\r\n I2C init..."); |
41 | ingob | 89 | // enable Port 2 peripherie |
90 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
||
91 | // disable a reset state |
||
92 | SCU_APBPeriphReset(__GPIO2, DISABLE); |
||
1 | ingob | 93 | |
41 | ingob | 94 | // free a busy bus |
1 | ingob | 95 | |
41 | ingob | 96 | // At switch on I2C devices can get in a state where they |
97 | // are still waiting for a command due to all the bus lines bouncing |
||
98 | // around at startup have started clocking data into the device(s). |
||
99 | // Enable the ports as open collector port outputs |
||
100 | // and clock out at least 9 SCL pulses, then generate a stop |
||
101 | // condition and then leave the clock line high. |
||
37 | killagreg | 102 | |
41 | ingob | 103 | // configure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT to normal port operation |
104 | GPIO_StructInit(&GPIO_InitStructure); |
||
105 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
||
106 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
||
107 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
||
196 | killagreg | 108 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
41 | ingob | 109 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt1; |
110 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
37 | killagreg | 111 | |
41 | ingob | 112 | u8 i; |
113 | u32 delay; |
||
114 | // set SCL high and then SDA to low (start condition) |
||
115 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
||
116 | delay = SetDelay(1); |
||
117 | while (!CheckDelay(delay)); |
||
118 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_RESET); |
||
119 | // toggle SCL at least 10 times from high to low to high |
||
120 | for(i = 0; i < 10; i++) |
||
121 | { |
||
122 | delay = SetDelay(1); |
||
123 | while (!CheckDelay(delay)); |
||
1 | ingob | 124 | |
41 | ingob | 125 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_RESET); |
126 | delay = SetDelay(1); |
||
127 | while (!CheckDelay(delay)); |
||
128 | GPIO_WriteBit(GPIO2, GPIO_Pin_2, Bit_SET); |
||
129 | } |
||
130 | delay = SetDelay(1); |
||
131 | while (!CheckDelay(delay)); |
||
132 | // create stop condition setting SDA HIGH when SCL is HIGH |
||
133 | GPIO_WriteBit(GPIO2, GPIO_Pin_3, Bit_SET); |
||
37 | killagreg | 134 | |
135 | |||
41 | ingob | 136 | // reconfigure P2.2->I2C1_CLKOUT and P2.3->I2C1_DOUT |
137 | GPIO_StructInit(&GPIO_InitStructure); |
||
138 | GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput; |
||
139 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
||
140 | GPIO_InitStructure.GPIO_Type = GPIO_Type_OpenCollector; |
||
196 | killagreg | 141 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Enable; |
41 | ingob | 142 | GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2; //I2C1_CLKOUT, I2C1_DOUT |
143 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
144 | |||
145 | // enable I2C peripherie |
||
146 | SCU_APBPeriphClockConfig(__I2C1,ENABLE); |
||
147 | // reset I2C peripherie |
||
148 | SCU_APBPeriphReset(__I2C1,ENABLE); |
||
149 | SCU_APBPeriphReset(__I2C1,DISABLE); |
||
150 | |||
151 | I2C_DeInit(I2C1); |
||
152 | I2C_StructInit(&I2C_Struct); |
||
153 | I2C_Struct.I2C_GeneralCall = I2C_GeneralCall_Disable; |
||
154 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
||
136 | killagreg | 155 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
41 | ingob | 156 | I2C_Struct.I2C_OwnAddress = 0x00; |
157 | I2C_Init(I2C1, &I2C_Struct); |
||
158 | |||
241 | killagreg | 159 | // empty rx and tx buffer |
41 | ingob | 160 | I2C_TxBufferSize = 0; |
161 | I2C_RxBufferSize = 0; |
||
162 | |||
163 | I2C_Cmd(I2C1, ENABLE); |
||
164 | I2C_ITConfig(I2C1, ENABLE); |
||
165 | |||
136 | killagreg | 166 | VIC_Config(I2C1_ITLine, VIC_IRQ , PRIORITY_I2C1); |
41 | ingob | 167 | |
189 | killagreg | 168 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
41 | ingob | 169 | I2C_GenerateSTOP(I2C1, ENABLE); |
241 | killagreg | 170 | I2C_State = I2C_STATE_IDLE; |
41 | ingob | 171 | |
110 | killagreg | 172 | UART1_PutString("ok"); |
1 | ingob | 173 | } |
174 | |||
41 | ingob | 175 | |
1 | ingob | 176 | //-------------------------------------------------------------- |
41 | ingob | 177 | void I2C1_Deinit(void) |
1 | ingob | 178 | { |
79 | killagreg | 179 | GPIO_InitTypeDef GPIO_InitStructure; |
110 | killagreg | 180 | UART1_PutString("\r\n I2C deinit..."); |
41 | ingob | 181 | I2C_GenerateStart(I2C1, DISABLE); |
182 | I2C_GenerateSTOP(I2C1, ENABLE); |
||
183 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
||
241 | killagreg | 184 | I2C_State = I2C_STATE_OFF; |
41 | ingob | 185 | I2C_ITConfig(I2C1, DISABLE); |
186 | I2C_Cmd(I2C1, DISABLE); |
||
187 | I2C_DeInit(I2C1); |
||
188 | SCU_APBPeriphClockConfig(__I2C1, DISABLE); |
||
79 | killagreg | 189 | |
41 | ingob | 190 | // set ports to input |
191 | SCU_APBPeriphClockConfig(__GPIO2, ENABLE); |
||
192 | GPIO_StructInit(&GPIO_InitStructure); |
||
193 | GPIO_InitStructure.GPIO_Direction = GPIO_PinInput; |
||
79 | killagreg | 194 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; |
195 | GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull; |
||
196 | killagreg | 196 | GPIO_InitStructure.GPIO_IPInputConnected = GPIO_IPInputConnected_Disable; |
79 | killagreg | 197 | GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1; |
198 | GPIO_Init(GPIO2, &GPIO_InitStructure); |
||
1 | ingob | 199 | |
241 | killagreg | 200 | // empty rx and tx buffer |
41 | ingob | 201 | I2C_TxBufferSize = 0; |
202 | I2C_RxBufferSize = 0; |
||
37 | killagreg | 203 | |
189 | killagreg | 204 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
37 | killagreg | 205 | |
110 | killagreg | 206 | UART1_PutString("ok"); |
41 | ingob | 207 | } |
1 | ingob | 208 | |
41 | ingob | 209 | |
210 | //-------------------------------------------------------------- |
||
211 | void I2C1_IRQHandler(void) |
||
212 | { |
||
241 | killagreg | 213 | static u8 Rx_Idx = 0, Tx_Idx = 0; |
41 | ingob | 214 | u16 status; |
189 | killagreg | 215 | |
195 | killagreg | 216 | //IENABLE; // do not enable IRQ nesting for I2C!!!! |
217 | |||
41 | ingob | 218 | // detemine I2C State |
219 | status = I2C_GetLastEvent(I2C1); |
||
220 | |||
221 | if(status & (I2C_FLAG_AF|I2C_FLAG_BERR)) // if an acknowledge failure or bus error occured |
||
79 | killagreg | 222 | { // Set and subsequently clear the STOP bit while BTF is set. |
41 | ingob | 223 | while(I2C_GetFlagStatus (I2C1, I2C_FLAG_BTF) != RESET) |
224 | { |
||
189 | killagreg | 225 | I2C_GenerateSTOP (I2C1, ENABLE); // free the bus |
41 | ingob | 226 | I2C_GenerateSTOP (I2C1, DISABLE); // free the bus |
227 | } |
||
241 | killagreg | 228 | I2C_State = I2C_STATE_IDLE; |
189 | killagreg | 229 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
41 | ingob | 230 | LED_GRN_OFF; |
189 | killagreg | 231 | return; |
41 | ingob | 232 | } |
233 | else |
||
234 | { // depending on current i2c state |
||
241 | killagreg | 235 | switch(status) |
41 | ingob | 236 | { |
237 | // the start condition was initiated on the bus |
||
238 | case I2C_EVENT_MASTER_MODE_SELECT: |
||
239 | LED_GRN_ON; |
||
240 | // update current bus state variable |
||
241 | killagreg | 241 | // jump to rx state if there is nothing to send |
41 | ingob | 242 | switch(I2C_Direction) |
243 | { |
||
244 | case I2C_MODE_TRANSMITTER: |
||
241 | killagreg | 245 | I2C_State = I2C_STATE_TX_PROGRESS; |
41 | ingob | 246 | break; |
79 | killagreg | 247 | |
41 | ingob | 248 | case I2C_MODE_RECEIVER: |
241 | killagreg | 249 | if (I2C_RxBufferSize == 0) // nothingto send? |
41 | ingob | 250 | { |
251 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
189 | killagreg | 252 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
253 | LED_GRN_OFF; |
||
241 | killagreg | 254 | I2C_State = I2C_STATE_IDLE; |
41 | ingob | 255 | return; |
256 | } |
||
257 | else |
||
258 | { |
||
241 | killagreg | 259 | I2C_State = I2C_STATE_RX_PROGRESS; |
41 | ingob | 260 | } |
261 | break; |
||
79 | killagreg | 262 | |
41 | ingob | 263 | default: // invalid direction |
264 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
189 | killagreg | 265 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
266 | LED_GRN_OFF; |
||
241 | killagreg | 267 | I2C_State = I2C_STATE_IDLE; |
41 | ingob | 268 | return; |
269 | } |
||
270 | // enable acknowledge |
||
271 | I2C_AcknowledgeConfig (I2C1, ENABLE); |
||
272 | // send address/direction byte on the bus |
||
241 | killagreg | 273 | I2C_Send7bitAddress(I2C1, I2C_SlaveAddress, I2C_Direction); |
41 | ingob | 274 | break; |
79 | killagreg | 275 | |
41 | ingob | 276 | // the address byte was send |
277 | case I2C_EVENT_MASTER_MODE_SELECTED: |
||
278 | // Clear EV6 by set again the PE bit |
||
79 | killagreg | 279 | I2C_Cmd(I2C1, ENABLE); |
41 | ingob | 280 | switch(I2C_State) |
281 | { |
||
241 | killagreg | 282 | case I2C_STATE_TX_PROGRESS: |
283 | // send 1st data byte |
||
41 | ingob | 284 | Tx_Idx = 0; |
241 | killagreg | 285 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
286 | Tx_Idx++; |
||
41 | ingob | 287 | // reset timeout |
189 | killagreg | 288 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset |
41 | ingob | 289 | break; |
79 | killagreg | 290 | |
241 | killagreg | 291 | case I2C_STATE_RX_PROGRESS: |
41 | ingob | 292 | Rx_Idx = 0; |
241 | killagreg | 293 | // disable acknoledge if only one byte has to be read |
294 | if(I2C_RxBufferSize == 1) I2C_AcknowledgeConfig (I2C1, DISABLE); |
||
41 | ingob | 295 | break; |
79 | killagreg | 296 | |
41 | ingob | 297 | default: // unknown I2C state |
298 | // should never happen |
||
299 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
189 | killagreg | 300 | LED_GRN_OFF; |
301 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
||
241 | killagreg | 302 | I2C_State = I2C_STATE_IDLE; |
189 | killagreg | 303 | return; |
41 | ingob | 304 | break; |
305 | } |
||
306 | break; |
||
79 | killagreg | 307 | |
41 | ingob | 308 | // the master has transmitted a byte and slave has been acknowledged |
309 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
||
79 | killagreg | 310 | |
41 | ingob | 311 | // some bytes have to be transmitted |
312 | if(Tx_Idx < I2C_TxBufferSize) |
||
313 | { |
||
241 | killagreg | 314 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
315 | Tx_Idx++; |
||
41 | ingob | 316 | } |
189 | killagreg | 317 | else // last byte was send |
318 | { |
||
41 | ingob | 319 | // generate stop or repeated start condition |
241 | killagreg | 320 | if (I2C_RxBufferSize > 0) // is any answer byte expected? |
41 | ingob | 321 | { |
79 | killagreg | 322 | I2C_Direction = I2C_MODE_RECEIVER; // switch to master receiver after repeated start condition |
323 | I2C_GenerateStart(I2C1, ENABLE); // initiate repeated start condition on the bus |
||
41 | ingob | 324 | } |
325 | else |
||
326 | { // stop communication |
||
327 | I2C_GenerateSTOP(I2C1, ENABLE); // generate stop condition to free the bus |
||
189 | killagreg | 328 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
41 | ingob | 329 | LED_GRN_OFF; |
241 | killagreg | 330 | I2C_State = I2C_STATE_IDLE; // ready for new actions |
41 | ingob | 331 | } |
332 | } |
||
333 | break; |
||
79 | killagreg | 334 | |
41 | ingob | 335 | // the master has received a byte from the slave |
79 | killagreg | 336 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
41 | ingob | 337 | // some bytes have to be received |
241 | killagreg | 338 | if ( Rx_Idx+1 < I2C_RxBufferSize) |
41 | ingob | 339 | { // copy received byte from the data register to the rx-buffer |
241 | killagreg | 340 | I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
41 | ingob | 341 | } |
241 | killagreg | 342 | else // if the last byte was received |
41 | ingob | 343 | { |
344 | // generate a STOP condition on the bus before reading data register |
||
345 | I2C_GenerateSTOP(I2C1, ENABLE); |
||
241 | killagreg | 346 | I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
347 | // call the rx handler function to process recieved data |
||
348 | if(I2C_pRxHandler != NULL) (*I2C_pRxHandler)((u8*)I2C_RxBuffer, I2C_RxBufferSize); |
||
349 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); |
||
350 | DebugOut.Analog[15]++; |
||
189 | killagreg | 351 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
352 | LED_GRN_OFF; |
||
241 | killagreg | 353 | I2C_State = I2C_STATE_IDLE; |
189 | killagreg | 354 | return; |
41 | ingob | 355 | } |
356 | Rx_Idx++; |
||
357 | // if the 2nd last byte was received disable acknowledge for the last one |
||
241 | killagreg | 358 | if ( (Rx_Idx + 1) == I2C_RxBufferSize ) |
41 | ingob | 359 | { |
241 | killagreg | 360 | I2C_AcknowledgeConfig(I2C1, DISABLE); |
41 | ingob | 361 | } |
362 | break; |
||
79 | killagreg | 363 | |
241 | killagreg | 364 | default:// unknown event |
365 | // should never happen |
||
366 | I2C_GenerateSTOP (I2C1, ENABLE); |
||
367 | LED_GRN_OFF; |
||
368 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
||
369 | I2C_State = I2C_STATE_IDLE; |
||
41 | ingob | 370 | break; |
371 | } |
||
372 | } |
||
195 | killagreg | 373 | //IDISABLE; // do not enable IRQ nesting for I2C!!!! |
1 | ingob | 374 | } |
241 | killagreg | 375 | // ---------------------------------------------------------------------------------------- |
376 | // initate an i2c transmission |
||
377 | u8 I2C_Transmission(u8 SlaveAddr, I2C_pRxHandler_t pRxHandler, u8 RxBytes) |
||
1 | ingob | 378 | { |
241 | killagreg | 379 | u8 retval = 0; |
380 | |||
381 | if(I2C_State == I2C_STATE_IDLE) |
||
242 | killagreg | 382 | { |
241 | killagreg | 383 | I2C_RxBufferSize = RxBytes; |
384 | // set direction to master transmitter |
||
385 | if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_TXBUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER; |
||
386 | else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_RXBUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER; |
||
387 | else // nothing to send or receive |
||
189 | killagreg | 388 | { |
241 | killagreg | 389 | I2C_TxBufferSize = 0; |
390 | I2C_RxBufferSize = 0; |
||
391 | return(retval); |
||
189 | killagreg | 392 | } |
241 | killagreg | 393 | // update slave address and rx data handler funbction pointer |
394 | I2C_SlaveAddress = SlaveAddr; |
||
395 | I2C_pRxHandler = pRxHandler; |
||
189 | killagreg | 396 | // test on busy flag and clear it |
397 | I2C_CheckEvent( I2C1, I2C_FLAG_BUSY ); |
||
242 | killagreg | 398 | // enable I2C IRQ |
399 | VIC_ITCmd(I2C1_ITLine, ENABLE); |
||
189 | killagreg | 400 | // initiate start condition on the bus |
401 | I2C_GenerateStart(I2C1, ENABLE); |
||
241 | killagreg | 402 | retval = 1; |
403 | } |
||
404 | return(retval); |
||
41 | ingob | 405 | } |