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