Subversion Repositories NaviCtrl

Rev

Rev 861 | 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
861 holgerb 47
void I2CBus_StateReset(I2C_TypeDef* I2Cx);
48
 
482 killagreg 49
void I2CBus_Deinit(I2C_TypeDef* I2Cx);
50
// try to allocate the I2C_Buffer within the timeout limit
51
// returns 1 on success
52
u8 I2CBus_LockBuffer(I2C_TypeDef* I2Cx, u32 timeout);
53
// Initiate i2c transmission
54
// A transmission sends first TxBytes from pTxData to slave
55
// and then RxBytes are read from slave to internal buffer
56
// replacing the byte that have been sent.
57
// Then the RxHandler function is called to handle the result.
58
// This function returns imediatly after a start condition in the bus.
59
// returns 1 if a transmission has been started, returns 0 otherwise
60
u8 I2CBus_Transmission(I2C_TypeDef* I2Cx, u8 SlaveAddr, u8* pTxData, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes);
61
// wait until transmission progess is finished or timeout
62
// returns 1 if no timeout occurs
63
u8 I2CBus_WaitForEndOfTransmission(I2C_TypeDef* I2Cx, u32 timeout);
64
 
65
#endif // I2C_H
66