Rev 522 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
482 | killagreg | 1 | #ifndef __I2C_H |
2 | #define __I2C_H |
||
3 | |||
4 | #include "91x_lib.h" |
||
5 | |||
6 | // I2C states |
||
7 | #define I2C_STATE_UNDEF 0 |
||
8 | #define I2C_STATE_IDLE 1 |
||
9 | #define I2C_STATE_BUFFBUSY 2 |
||
10 | #define I2C_STATE_TX_PENDING 3 |
||
11 | #define I2C_STATE_TX_PROGRESS 4 |
||
12 | #define I2C_STATE_RX_PENDING 5 |
||
13 | #define I2C_STATE_RX_PROGRESS 6 |
||
14 | // I2C Errors |
||
15 | #define I2C_ERROR_NONE 0 |
||
16 | #define I2C_ERROR_UNKNOWN 1 |
||
17 | #define I2C_ERROR_NOACK 2 |
||
18 | |||
19 | // the pointer to the rxbuffer handler function |
||
20 | // called by the IRQ routine after all bytes are recieved from slave |
||
21 | typedef void (*I2C_pRxHandler_t) (u8* pRxBuffer, u8 RxBufferSize); |
||
22 | |||
23 | |||
24 | typedef struct |
||
25 | { |
||
26 | u8 State; // bus status |
||
27 | u8 Error; // bus error code |
||
28 | u32 Timeout; // # time of last transfer |
||
29 | u8 *pData; // # data buffer |
||
30 | u8 TxBufferSize; // # of bytes to send |
||
31 | u8 RxBufferSize; // # of bytes to read |
||
32 | u8 Direction; // bus direction |
||
33 | u8 SlaveAddr; // slave address |
||
34 | u16 VIC_Source; // irq source |
||
35 | I2C_pRxHandler_t pRxHandler; // function pointer to call back handler after transfer |
||
36 | } __attribute__((packed)) I2C_Bus_t; |
||
37 | |||
38 | #define I2C_TIMEOUT 500 // 500 ms |
||
522 | holgerb | 39 | #define I2C_BUFFER_LEN 200 // define the size of the rx/tx buffer in bytes |
482 | killagreg | 40 | |
41 | // Retourns pointer to data structure of the selected bus |
||
42 | volatile I2C_Bus_t* I2CBus(I2C_TypeDef* I2Cx); |
||
43 | |||
44 | // initialize the I2C bus |
||
45 | void I2CBus_Init(I2C_TypeDef* I2Cx); |
||
46 | // deinitialize the I2C bus |
||
47 | void I2CBus_Deinit(I2C_TypeDef* I2Cx); |
||
48 | // try to allocate the I2C_Buffer within the timeout limit |
||
49 | // returns 1 on success |
||
50 | u8 I2CBus_LockBuffer(I2C_TypeDef* I2Cx, u32 timeout); |
||
51 | // Initiate i2c transmission |
||
52 | // A transmission sends first TxBytes from pTxData to slave |
||
53 | // and then RxBytes are read from slave to internal buffer |
||
54 | // replacing the byte that have been sent. |
||
55 | // Then the RxHandler function is called to handle the result. |
||
56 | // This function returns imediatly after a start condition in the bus. |
||
57 | // returns 1 if a transmission has been started, returns 0 otherwise |
||
58 | u8 I2CBus_Transmission(I2C_TypeDef* I2Cx, u8 SlaveAddr, u8* pTxData, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes); |
||
59 | // wait until transmission progess is finished or timeout |
||
60 | // returns 1 if no timeout occurs |
||
61 | u8 I2CBus_WaitForEndOfTransmission(I2C_TypeDef* I2Cx, u32 timeout); |
||
62 | |||
63 | #endif // I2C_H |
||
64 |