Subversion Repositories Code-Repo

Rev

Rev 155 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

#ifndef I2C_H
#define I2C_H

#define MAXI2CBUF 64

// I2C Operating Speed
#define I2C_400KHZ              0x0
#define I2C_100KHZ              0x1

// Operating State
#define I2C_IDLE                0x1
#define I2C_STARTED             0x2
#define I2C_RCV_DATA            0x3
#define I2C_SEND_DATA           0x4
#define I2C_SEND_ADDR           0x5
#define I2C_SEND_ADDR_2         0x6
#define I2C_CHECK_ACK_SEND      0x7
#define I2C_CHECK_ACK_RECV      0x8
#define I2C_CHECK_ACK_RESTART   0x9
#define I2C_REQ_DATA            0xA
#define I2C_SEND_STOP           0xB
#define I2C_SEND_START          0xC

// Operating Mode
#define I2C_MODE_SLAVE          0x10
#define I2C_MODE_MASTER         0x11

// Master Status
#define I2C_MASTER_SEND         0x20
#define I2C_MASTER_RECV         0x21
#define I2C_MASTER_RESTART      0x22
#define I2C_MASTER_IDLE         0x23

// Return Status
#define I2C_SEND_OK             0x30
#define I2C_SEND_FAIL           0x31
#define I2C_RECV_OK             0x32
#define I2C_RECV_FAIL           0x33
#define I2C_DATA_AVAL           0x34
#define I2C_ERR_NOADDR          0x35
#define I2C_ERR_OVERRUN         0x36
#define I2C_ERR_NODATA          0x37
#define I2C_ERR_BUFFER_OVERRUN  0x38

typedef struct {
    char buffer_in[MAXI2CBUF];
    char buffer_in_len;
    char buffer_in_len_tmp;
    char buffer_in_read_ind;
    char buffer_in_write_ind;
    
    char buffer_out[MAXI2CBUF];
    char buffer_out_len;
    char buffer_out_ind;

    char operating_mode;
    char operating_state;
    char return_status;

    char master_dest_addr;
    char master_status;
    
    char slave_in_last_byte;
    char slave_sending_data;
} I2C_DATA;

void I2C_Init(I2C_DATA *data);
void I2C_Interrupt_Handler(void);
void I2C_Interrupt_Slave(void);
void I2C_Interrupt_Master(void);
void I2C_Configure_Slave(char);
void I2C_Configure_Master(char speed);
void I2C_Master_Send(char address, char length, char *msg);
void I2C_Master_Recv(char address, char length);
void I2C_Master_Restart(char address, char msg, char length);
char I2C_Get_Status(void);
char I2C_Buffer_Len(void);
char I2C_Read_Buffer(char *buffer);
char I2C_Process_Send(char);

#endif