0,0 → 1,66 |
#ifndef __I2C_H |
#define __I2C_H |
|
#include "91x_lib.h" |
|
// I2C states |
#define I2C_STATE_UNDEF 0 |
#define I2C_STATE_IDLE 1 |
#define I2C_STATE_BUFFBUSY 2 |
#define I2C_STATE_TX_PENDING 3 |
#define I2C_STATE_TX_PROGRESS 4 |
#define I2C_STATE_RX_PENDING 5 |
#define I2C_STATE_RX_PROGRESS 6 |
// I2C Errors |
#define I2C_ERROR_NONE 0 |
#define I2C_ERROR_UNKNOWN 1 |
#define I2C_ERROR_NOACK 2 |
|
// the pointer to the rxbuffer handler function |
// called by the IRQ routine after all bytes are recieved from slave |
typedef void (*I2C_pRxHandler_t) (u8* pRxBuffer, u8 RxBufferSize); |
|
|
typedef struct |
{ |
u8 State; // bus status |
u8 Error; // bus error code |
u32 Timeout; // # time of last transfer |
u8 *pData; // # data buffer |
u8 TxBufferSize; // # of bytes to send |
u8 RxBufferSize; // # of bytes to read |
u8 Direction; // bus direction |
u8 SlaveAddr; // slave address |
u16 VIC_Source; // irq source |
I2C_pRxHandler_t pRxHandler; // function pointer to call back handler after transfer |
} __attribute__((packed)) I2C_Bus_t; |
|
#define I2C_TIMEOUT 500 // 500 ms |
#define I2C_BUFFER_LEN 200 // define the size of the rx/tx buffer in bytes |
|
// Retourns pointer to data structure of the selected bus |
volatile I2C_Bus_t* I2CBus(I2C_TypeDef* I2Cx); |
|
// initialize the I2C bus |
void I2CBus_Init(I2C_TypeDef* I2Cx); |
// deinitialize the I2C bus |
void I2CBus_StateReset(I2C_TypeDef* I2Cx); |
|
void I2CBus_Deinit(I2C_TypeDef* I2Cx); |
// try to allocate the I2C_Buffer within the timeout limit |
// returns 1 on success |
u8 I2CBus_LockBuffer(I2C_TypeDef* I2Cx, u32 timeout); |
// Initiate i2c transmission |
// A transmission sends first TxBytes from pTxData to slave |
// and then RxBytes are read from slave to internal buffer |
// replacing the byte that have been sent. |
// Then the RxHandler function is called to handle the result. |
// This function returns imediatly after a start condition in the bus. |
// returns 1 if a transmission has been started, returns 0 otherwise |
u8 I2CBus_Transmission(I2C_TypeDef* I2Cx, u8 SlaveAddr, u8* pTxData, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes); |
// wait until transmission progess is finished or timeout |
// returns 1 if no timeout occurs |
u8 I2CBus_WaitForEndOfTransmission(I2C_TypeDef* I2Cx, u32 timeout); |
|
#endif // I2C_H |
|