Subversion Repositories NaviCtrl

Rev

Rev 248 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 248 Rev 252
1
#ifndef __I2C_H
1
#ifndef __I2C_H
2
#define __I2C_H
2
#define __I2C_H
3
 
3
 
4
extern volatile u32 I2C1_Timeout;
4
extern volatile u32 I2C1_Timeout;
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
#define I2C_STATE_OFF                   7
14
#define I2C_STATE_OFF                   7
-
 
15
 
-
 
16
#define I2C_ERROR_NONE                  0
-
 
17
#define I2C_ERROR_UNKNOWN               1
-
 
18
#define I2C_ERROR_NOACK                 2
15
 
19
 
16
// current I2C state
20
// current I2C state
-
 
21
extern volatile u8 I2C_State;
-
 
22
// the last I2C error
17
extern volatile u8 I2C_State;
23
extern volatile u8 I2C_Error;
18
 
24
 
19
#define I2C1_TIMEOUT 500 // 500 ms
25
#define I2C1_TIMEOUT 500 // 500 ms
20
 
26
 
21
// define the size of the rx/tx buffer
27
// define the size of the rx/tx buffer
22
#define I2C_BUFFER_LEN 150
28
#define I2C_BUFFER_LEN 100
23
// transfer buffer should be filled after a successful
29
// transfer buffer should be filled after a successful
24
// I2C_LockBuffer(...) and before a start of transmission
30
// I2C_LockBuffer(...) and before a start of transmission
25
// initiated by  I2C_Transmission(...).
31
// initiated by  I2C_Transmission(...).
26
extern volatile u8 I2C_Buffer[];
32
extern volatile u8 I2C_Buffer[];
27
 
33
 
28
void I2C1_Init(void);
34
void I2C1_Init(void);
29
 
35
 
30
void I2C1_Deinit(void);
36
void I2C1_Deinit(void);
31
 
37
 
32
// the pointer to the rxbuffer handler function
38
// the pointer to the rxbuffer handler function
33
// called by the IRQ routine after all bytes are recieved from slave
39
// called by the IRQ routine after all bytes are recieved from slave
34
typedef void (*I2C_pRxHandler_t) (u8* pRxBuffer, u8 RxBufferSize);
40
typedef void (*I2C_pRxHandler_t) (u8* pRxBuffer, u8 RxBufferSize);
35
// Initiate i2c transmission
41
// Initiate i2c transmission
36
// A transmission sends first TxBytes from I2C_Buffer to slave
42
// A transmission sends first TxBytes from I2C_Buffer to slave
37
// and then RxBytes are read from slave to I2C_Buffer
43
// and then RxBytes are read from slave to I2C_Buffer
38
// replacing the byte that have been sent.
44
// replacing the byte that have been sent.
39
// Then the RxHandler function is called to handle the result.
45
// Then the RxHandler function is called to handle the result.
40
// This function returns imediatly after a start condition.
46
// This function returns imediatly after a start condition.
41
// returns 1 if a transmission has been started, otherwise 0
47
// returns 1 if a transmission has been started, otherwise 0
42
u8 I2C_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes);
48
u8 I2C_Transmission(u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes);
43
// try to allocate the I2C_Buffer within the timeout limit
49
// try to allocate the I2C_Buffer within the timeout limit
44
// returns 1 on success
50
// returns 1 on success
45
u8 I2C_LockBuffer(u32 timeout);
51
u8 I2C_LockBuffer(u32 timeout);
46
// wait until transmission progess is finished or timeout
52
// wait until transmission progess is finished or timeout
47
// returns 1 if no timeout occurs
53
// returns 1 if no timeout occurs
48
u8 I2C_WaitForEndOfTransmission(u32 timeout);
54
u8 I2C_WaitForEndOfTransmission(u32 timeout);
49
 
55
 
50
#endif // I2C_H
56
#endif // I2C_H
51
 
57
 
52
 
58