Rev 242 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 242 | Rev 248 | ||
---|---|---|---|
Line 60... | Line 60... | ||
60 | #include "uart1.h" |
60 | #include "uart1.h" |
61 | #include "timer1.h" |
61 | #include "timer1.h" |
62 | #include "config.h" |
62 | #include "config.h" |
63 | #include "led.h" |
63 | #include "led.h" |
Line -... | Line 64... | ||
- | 64 | ||
64 | 65 | ||
Line 65... | Line -... | ||
65 | volatile u8 I2C_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs |
- | |
66 | 66 | volatile u8 I2C_State = I2C_STATE_OFF; // only one byte, because of sync by nesting irqs |
|
67 | // rxbuffer |
- | |
68 | volatile u8 I2C_RxBufferSize; |
- | |
69 | volatile u8 I2C_RxBuffer[I2C_RXBUFFER_LEN]; |
67 | |
- | 68 | // number of bytes to send |
|
- | 69 | volatile u8 I2C_TxBufferSize; |
|
- | 70 | // number of bytes to receive |
|
70 | // txbuffer |
71 | volatile u8 I2C_RxBufferSize; |
71 | volatile u8 I2C_TxBufferSize; |
- | |
- | 72 | // the transfer buffer |
|
72 | volatile u8 I2C_TxBuffer[I2C_TXBUFFER_LEN]; |
73 | volatile u8 I2C_Buffer[I2C_BUFFER_LEN]; |
- | 74 | // the transfer direction |
|
73 | 75 | volatile u8 I2C_Direction; |
|
- | 76 | // the slave address |
|
74 | volatile u8 I2C_Direction; |
77 | volatile u8 I2C_SlaveAddress = 0x00; |
75 | volatile u8 I2C_SlaveAddress = 0x00; |
- | |
- | 78 | // function pointer to process the received bytes |
|
76 | I2C_pRxHandler_t I2C_pRxHandler = NULL; |
79 | I2C_pRxHandler_t I2C_pRxHandler = NULL; |
Line 77... | Line 80... | ||
77 | 80 | // goblal timeout |
|
78 | volatile u32 I2C1_Timeout = 0; |
81 | volatile u32 I2C1_Timeout = 0; |
79 | 82 | ||
Line 154... | Line 157... | ||
154 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
157 | I2C_Struct.I2C_Ack = I2C_Ack_Enable; |
155 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
158 | I2C_Struct.I2C_CLKSpeed = I2C1_CLOCK; |
156 | I2C_Struct.I2C_OwnAddress = 0x00; |
159 | I2C_Struct.I2C_OwnAddress = 0x00; |
157 | I2C_Init(I2C1, &I2C_Struct); |
160 | I2C_Init(I2C1, &I2C_Struct); |
Line 158... | Line 161... | ||
158 | 161 | ||
159 | // empty rx and tx buffer |
162 | // empty rx and tx buffer counters |
160 | I2C_TxBufferSize = 0; |
163 | I2C_TxBufferSize = 0; |
Line 161... | Line 164... | ||
161 | I2C_RxBufferSize = 0; |
164 | I2C_RxBufferSize = 0; |
162 | 165 | ||
Line 167... | Line 170... | ||
167 | 170 | ||
168 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
171 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
169 | I2C_GenerateSTOP(I2C1, ENABLE); |
172 | I2C_GenerateSTOP(I2C1, ENABLE); |
Line -... | Line 173... | ||
- | 173 | I2C_State = I2C_STATE_IDLE; |
|
- | 174 | ||
- | 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; |
|
170 | I2C_State = I2C_STATE_IDLE; |
182 | UART1_Putchar('.'); |
171 | 183 | } |
|
Line 172... | Line 184... | ||
172 | UART1_PutString("ok"); |
184 | UART1_PutString("ok"); |
Line 204... | Line 216... | ||
204 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
216 | I2C1_Timeout = SetDelay(10*I2C1_TIMEOUT); |
Line 205... | Line 217... | ||
205 | 217 | ||
206 | UART1_PutString("ok"); |
218 | UART1_PutString("ok"); |
Line 207... | Line -... | ||
207 | } |
- | |
208 | 219 | } |
|
209 | 220 | ||
210 | //-------------------------------------------------------------- |
221 | //-------------------------------------------------------------- |
211 | void I2C1_IRQHandler(void) |
222 | void I2C1_IRQHandler(void) |
212 | { |
223 | { |
Line 213... | Line 224... | ||
213 | static u8 Rx_Idx = 0, Tx_Idx = 0; |
224 | static u8 Rx_Idx = 0, Tx_Idx = 0; |
214 | u16 status; |
- | |
215 | 225 | u16 status; |
|
216 | //IENABLE; // do not enable IRQ nesting for I2C!!!! |
226 | |
Line 217... | Line 227... | ||
217 | 227 | //IENABLE; // do not enable IRQ nesting for I2C!!!! |
|
218 | // detemine I2C State |
228 | // detemine I2C State |
Line 244... | Line 254... | ||
244 | case I2C_MODE_TRANSMITTER: |
254 | case I2C_MODE_TRANSMITTER: |
245 | I2C_State = I2C_STATE_TX_PROGRESS; |
255 | I2C_State = I2C_STATE_TX_PROGRESS; |
246 | break; |
256 | break; |
Line 247... | Line 257... | ||
247 | 257 | ||
248 | case I2C_MODE_RECEIVER: |
258 | case I2C_MODE_RECEIVER: |
249 | if (I2C_RxBufferSize == 0) // nothingto send? |
259 | if (I2C_RxBufferSize == 0) // nothing to send? |
250 | { |
260 | { |
251 | I2C_GenerateSTOP (I2C1, ENABLE); |
261 | I2C_GenerateSTOP (I2C1, ENABLE); |
252 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
262 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
253 | LED_GRN_OFF; |
263 | LED_GRN_OFF; |
Line 280... | Line 290... | ||
280 | switch(I2C_State) |
290 | switch(I2C_State) |
281 | { |
291 | { |
282 | case I2C_STATE_TX_PROGRESS: |
292 | case I2C_STATE_TX_PROGRESS: |
283 | // send 1st data byte |
293 | // send 1st data byte |
284 | Tx_Idx = 0; |
294 | Tx_Idx = 0; |
285 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
295 | I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]); |
286 | Tx_Idx++; |
296 | Tx_Idx++; |
287 | // reset timeout |
297 | // reset timeout |
288 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset |
298 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); // after inactivity the I2C1 bus will be reset |
289 | break; |
299 | break; |
Line 309... | Line 319... | ||
309 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
319 | case I2C_EVENT_MASTER_BYTE_TRANSMITTED: |
Line 310... | Line 320... | ||
310 | 320 | ||
311 | // some bytes have to be transmitted |
321 | // some bytes have to be transmitted |
312 | if(Tx_Idx < I2C_TxBufferSize) |
322 | if(Tx_Idx < I2C_TxBufferSize) |
313 | { |
323 | { |
314 | I2C_SendData(I2C1, I2C_TxBuffer[Tx_Idx]); |
324 | I2C_SendData(I2C1, I2C_Buffer[Tx_Idx]); |
315 | Tx_Idx++; |
325 | Tx_Idx++; |
316 | } |
326 | } |
317 | else // last byte was send |
327 | else // last byte was send |
318 | { |
328 | { |
Line 335... | Line 345... | ||
335 | // the master has received a byte from the slave |
345 | // the master has received a byte from the slave |
336 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
346 | case I2C_EVENT_MASTER_BYTE_RECEIVED: |
337 | // some bytes have to be received |
347 | // some bytes have to be received |
338 | if ( Rx_Idx+1 < I2C_RxBufferSize) |
348 | if ( Rx_Idx+1 < I2C_RxBufferSize) |
339 | { // copy received byte from the data register to the rx-buffer |
349 | { // copy received byte from the data register to the rx-buffer |
340 | I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
350 | I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
341 | } |
351 | } |
342 | else // if the last byte was received |
352 | else // if the last byte was received |
343 | { |
353 | { |
344 | // generate a STOP condition on the bus before reading data register |
354 | // generate a STOP condition on the bus before reading data register |
345 | I2C_GenerateSTOP(I2C1, ENABLE); |
355 | I2C_GenerateSTOP(I2C1, ENABLE); |
346 | I2C_RxBuffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
356 | I2C_Buffer[Rx_Idx] = I2C_ReceiveData(I2C1); |
347 | // call the rx handler function to process recieved data |
357 | // call the rx handler function to process recieved data |
348 | if(I2C_pRxHandler != NULL) (*I2C_pRxHandler)((u8*)I2C_RxBuffer, I2C_RxBufferSize); |
358 | if(I2C_pRxHandler != NULL) (*I2C_pRxHandler)((u8*)I2C_Buffer, I2C_RxBufferSize); |
349 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); |
359 | I2C1_Timeout = SetDelay(I2C1_TIMEOUT); |
350 | DebugOut.Analog[15]++; |
360 | DebugOut.Analog[15]++; |
351 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
361 | VIC_ITCmd(I2C1_ITLine, DISABLE); |
352 | LED_GRN_OFF; |
362 | LED_GRN_OFF; |
353 | I2C_State = I2C_STATE_IDLE; |
363 | I2C_State = I2C_STATE_IDLE; |
Line 370... | Line 380... | ||
370 | break; |
380 | break; |
371 | } |
381 | } |
372 | } |
382 | } |
373 | //IDISABLE; // do not enable IRQ nesting for I2C!!!! |
383 | //IDISABLE; // do not enable IRQ nesting for I2C!!!! |
374 | } |
384 | } |
- | 385 | ||
- | 386 | // ---------------------------------------------------------------------------------------- |
|
- | 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 | } |
|
375 | // ---------------------------------------------------------------------------------------- |
410 | // ---------------------------------------------------------------------------------------- |
376 | // initate an i2c transmission |
411 | // initate an i2c transmission |
377 | u8 I2C_Transmission(u8 SlaveAddr, I2C_pRxHandler_t pRxHandler, u8 RxBytes) |
412 | u8 I2C_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes) |
378 | { |
413 | { |
379 | u8 retval = 0; |
414 | u8 retval = 0; |
- | 415 | if(I2C_State == I2C_STATE_BUFFBUSY) |
|
380 | 416 | { |
|
- | 417 | if((RxBytes > I2C_BUFFER_LEN) || (TxBytes > I2C_BUFFER_LEN)) |
|
- | 418 | { |
|
381 | if(I2C_State == I2C_STATE_IDLE) |
419 | I2C_State = I2C_STATE_IDLE; |
- | 420 | return(retval); |
|
382 | { |
421 | } |
383 | I2C_RxBufferSize = RxBytes; |
422 | I2C_RxBufferSize = RxBytes; |
- | 423 | I2C_TxBufferSize = TxBytes; |
|
384 | // set direction to master transmitter |
424 | // set direction to master transmitter |
385 | if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_TXBUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER; |
425 | if( (I2C_TxBufferSize > 0) && (I2C_TxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_TRANSMITTER; |
386 | else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_RXBUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER; |
426 | else if (( I2C_RxBufferSize > 0 ) && (I2C_RxBufferSize < I2C_BUFFER_LEN) ) I2C_Direction = I2C_MODE_RECEIVER; |
387 | else // nothing to send or receive |
427 | else // nothing to send or receive |
388 | { |
428 | { |
- | 429 | I2C_State = I2C_STATE_IDLE; |
|
389 | I2C_TxBufferSize = 0; |
430 | I2C_TxBufferSize = 0; |
390 | I2C_RxBufferSize = 0; |
431 | I2C_RxBufferSize = 0; |
391 | return(retval); |
432 | return(retval); |
392 | } |
433 | } |
393 | // update slave address and rx data handler funbction pointer |
434 | // update slave address and rx data handler funbction pointer |