delin17的笔记 https://passport2.21ic.com/?467504 [收藏] [复制] [RSS]

日志

CAT9555的驱动

已有 2037 次阅读2011-11-22 03:24 |系统分类:嵌入式系统

/***********************************************************************//**
 * @file  CAT9555.c
 * @brief  The Source file of Driver of the CAT9555
 * @version  0.0  --- Init Version
 * @date  22. Sept. 2011
 * @author  Denglijun
 * Copyright (C) 2011 Launch Limited. All rights reserved.
 **************************************************************************/
#include "CAT9555.h"


 


/**********************************************************************
*: CAT9555_WriteReg(T_CAT9555* pst,uint8_t u8RegAddr,uint8_t* pu8Val,uint8_t u8Length)
*Para:     pst----- the point of The Owner.
*          u8RegAddr----- The Address of the start register
*          pu8Val  ------- The Point of Buffer is will be writen
*          u8Length ------ The Length of The Buffer
*Return:    0 ----- Write success
*           <0 ----- Write FAIL
*Deion: Write the Data to the CAT9555 with the Command is u8RegAddr
************************************************************************/


int32_t CAT9555_WriteReg(T_CAT9555* pst,uint8_t u8RegAddr,uint8_t* pu8Val,uint8_t u8Length)
{
    uint8_t u8Buffer[4];
    I2C_M_SETUP_Type stI2CMSetup;
    if(u8Length>4)
        return -C_CAT9555_PARA_ERROR;
    if(NULL == pst)
        return -C_CAT9555_PARA_ERROR;
    stI2CMSetup.sl_addr7bit=pst->u8Addr;
    stI2CMSetup.rx_length=0;
    stI2CMSetup.rx_data=NULL;
    stI2CMSetup.tx_data=u8Buffer;
    stI2CMSetup.tx_length=u8Length+1;
    stI2CMSetup.retransmissions_max=5;


    stI2CMSetup.retransmissions_count=0;
    stI2CMSetup.rx_count=0;
    stI2CMSetup.tx_count=0;


    u8Buffer[0]=u8RegAddr;
    memcpy(u8Buffer+1,pu8Val,u8Length);


    if(SUCCESS != I2C_MasterTransferData(C_USE_I2C_PORT, &stI2CMSetup, I2C_TRANSFER_POLLING))
        return -C_CAT9555_I2C_ERROR;
    return 0;
 }
/**********************************************************************
*: CAT9555_SetDirByBit(T_CAT9555* pst,uint8_t u8Bit,uint8_t u8Dir)
*Para:     pst----- the point of The Owner.
*          u8Bit----- The Bit in CAT9555,P0 is 0~7,P1 is 8~15
*          u8Dir  ------- 1 is Input,0 is Output
*Return:    0 ----- Set success
*           <0 ----- Set FAIL
*Deion: Set the direction of the Port in CAT9555
************************************************************************/
int32_t CAT9555_SetDirByBit(T_CAT9555* pst,T_CAT9555_PORT_BIT u8Bit,uint8_t u8Dir)
{
    uint8_t u8Buffer[2];


    if(NULL == pst)
        return -C_CAT9555_PARA_ERROR;
    //The Max Value is 15
    if(u8Bit>=sizeof(SIZE_OF_PORT))
        return -C_CAT9555_PARA_ERROR;
    if(u8Bit<8)
    {
        if(u8Dir)
            pst->u8CfgOfP0 |= 1<<u8Bit;
        else
            pst->u8CfgOfP0 &= ~(1<<u8Bit);
    }
    else
    {
        u8Bit-=8;
        if(u8Dir)
            pst->u8CfgOfP1 |= 1<<u8Bit;
        else
            pst->u8CfgOfP1 &= ~(1<<u8Bit);
    }
    u8Buffer[0]=pst->u8CfgOfP0;
    u8Buffer[1]=pst->u8CfgOfP1;
    return CAT9555_WriteReg(pst,C_WR_CFG_CMD,u8Buffer,2);
}
/**********************************************************************
*: CAT9555_SetOutputByBit(T_CAT9555* pst,uint8_t u8Bit,uint8_t u8Value)
*Para:     pst----- the point of The Owner.
*          u8Bit----- The Bit in CAT9555,P0 is 0~7,P1 is 8~15
*          u8Value  ------- The Value of the Port, you must set the Port is
*                           output before setting the output value.
*Return:    0 ----- Set success
*           <0 ----- Set FAIL
*Deion: Set the direction of the Port in CAT9555
************************************************************************/


int32_t CAT9555_SetOutputByBit(T_CAT9555* pst,T_CAT9555_PORT_BIT u8Bit,uint8_t u8Value)
{
    uint8_t u8Buffer[3];
    if(NULL== pst)
        return -C_CAT9555_PARA_ERROR;;
    if(u8Bit>=sizeof(SIZE_OF_PORT))
        return -C_CAT9555_PARA_ERROR;;
    if(u8Bit<8)
    {
        if(u8Value)
            pst->u8OutputP0|= 1<<u8Bit;
        else
            pst->u8OutputP0 &= ~(1<<u8Bit);
    }
    else
    {
        u8Bit-=8;
        if(u8Value)
            pst->u8OutputP1 |= 1<<u8Bit;
        else
            pst->u8OutputP1 &= ~(1<<u8Bit);
    }


    u8Buffer[0]=pst->u8OutputP0;
    u8Buffer[1]=pst->u8OutputP1;
    return CAT9555_WriteReg(pst,C_WR_OUTPUT_CMD,u8Buffer,2);


}
/**********************************************************************
*: CAT9555_GetInput(T_CAT9555* pst)
*Para:     pst----- the point of The Owner.
*Return:    >0 ----- The Input Value in the PIN of CAT9555
*           <0 ----- FAIL
*Deion: Set the direction of the Port in CAT9555
************************************************************************/


int32_t CAT9555_GetInput(T_CAT9555* pst)
{
    uint8_t u8Buffer[4];
    uint16_t u16Input;
    I2C_M_SETUP_Type stI2CMSetup;
    if(NULL == pst)
      return -C_CAT9555_PARA_ERROR;;
    stI2CMSetup.sl_addr7bit=pst->u8Addr;
    stI2CMSetup.rx_length=4;
    stI2CMSetup.rx_data=u8Buffer;
    stI2CMSetup.tx_data=NULL;
    stI2CMSetup.tx_length=0;
    stI2CMSetup.retransmissions_max=5;
    stI2CMSetup.retransmissions_count=0;
    stI2CMSetup.rx_count=0;
    stI2CMSetup.tx_count=0;
    if(SUCCESS != I2C_MasterTransferData(C_USE_I2C_PORT, &stI2CMSetup, I2C_TRANSFER_POLLING))
        return -C_CAT9555_I2C_ERROR;
    u16Input = u8Buffer[1];
    u16Input <<= 8;
    u16Input += u8Buffer[0];
    return u16Input;
}
/**********************************************************************
*: CAT9555_ReadReg(T_CAT9555* pst,uint8_t u8RegAddr)
*Para:     pst----- the point of The Owner.
*          u8RegAddr -------- The Reg address wiil be read.
*Return:    int32>0 ----- the value of the Reg
*           <0 ----- FAIL
*Deion: Set the direction of the Port in CAT9555
************************************************************************/
int32_t CAT9555_ReadReg(T_CAT9555* pst,uint8_t u8RegAddr)
{
    uint8_t u8Buffer[3];
    I2C_M_SETUP_Type stI2CMSetup;
    if(NULL == pst)
        return -C_CAT9555_PARA_ERROR;
    stI2CMSetup.sl_addr7bit=pst->u8Addr;
    stI2CMSetup.rx_length=1;
    stI2CMSetup.rx_data=u8Buffer+1;
    stI2CMSetup.tx_data=u8Buffer;
    stI2CMSetup.tx_length=1;
    stI2CMSetup.retransmissions_max=5;


    stI2CMSetup.retransmissions_count=0;
    stI2CMSetup.rx_count=0;
    stI2CMSetup.tx_count=0;


    u8Buffer[0]=u8RegAddr;
    u8Buffer[1]=0;
    //u8Buffer[2]=pst->u8OutputP1;
    if(SUCCESS != I2C_MasterTransferData(C_USE_I2C_PORT, &stI2CMSetup, I2C_TRANSFER_POLLING))
        return -C_CAT9555_I2C_ERROR;
    return u8Buffer[1];
 }


/**********************************************************************
*: Init_CAT9555(T_CAT9555* pst,uint8_t u8Addr,PT_CAT9555_IO pstCAT9555P0,PT_CAT9555_IO pstCAT9555P1)
*Para:     pst----- the point of The Owner.
           u8Addr --- The Addr of the Owner
           pstCAT9555P0 ------ the para will be set to P0 in CAT9555
           pstCAT9555P1 ------- The para will be set to P1 in CAT9555
*Return:    >0 ----- intial success
*           <0 ----- FAIL
*Deion: Set the direction of the Port in CAT9555
************************************************************************/
int32_t Init_CAT9555(T_CAT9555* pst,uint8_t u8Addr,PT_CAT9555_IO pstCAT9555P0,PT_CAT9555_IO pstCAT9555P1)
{
    uint8_t u8Buffer[3];
    uint16_t u16Input;
    int32_t Error;
    if(NULL == pst)
        return -C_CAT9555_PARA_ERROR;
    if(pstCAT9555P0 == NULL)
        return -C_CAT9555_PARA_ERROR;
    if(NULL == pstCAT9555P1)
        return -C_CAT9555_PARA_ERROR;
    pst->u8Addr= C_ADDR_CAT9555 | u8Addr;
    pst->u8CfgOfP0=pstCAT9555P0->u8Cfg;
    pst->u8CfgOfP1=pstCAT9555P1->u8Cfg;
    pst->u8OutputP0=pstCAT9555P0->u8Output;
    pst->u8OutputP1=pstCAT9555P1->u8Output;
    //I2C_Init(C_USE_I2C_PORT, C_CAT_I2C_CLOCKRATE);


    //u8Buffer[0]=C_WR_CFG_CMD;
    u8Buffer[0]=pst->u8CfgOfP0;
    u8Buffer[1]=pst->u8CfgOfP1;
    Error=pst->WriteReg(pst,C_WR_CFG_CMD,u8Buffer,2);
    if(Error<0)
        return Error;


    u8Buffer[0]=pst->u8OutputP0;
    u8Buffer[1]=pst->u8OutputP1;
    Error=pst->WriteReg(pst,C_WR_OUTPUT_CMD,u8Buffer,2);
    if( Error <0)
        return Error;


    u16Input=pst->GetInput(pst);
    pst->u8InputP0=u16Input&0xFF;
    pst->u8InputP1=u16Input>>8;
    return 0;
}


 


#ifndef __CAT9555_H__



#define __CAT9555_H__
#include "Lpc17xx_i2c.h"
#include "error.h"



#define C_USE_I2C_PORT          LPC_I2C1
#define C_CAT_I2C_CLOCKRATE     20000


#define C_ADDR_CAT9555      0x20
#define C_WR_OUTPUT_CMD     0x02
#define C_WR_CFG_CMD        0x06
#define C_WR_CFG0_CMD       0x06
#define C_WR_CFG1_CMD       0x07



#define Set2Input(u8Cfg,u8Bit)        u8Cfg|=(1<<(u8Bit)
#define Set2Output(u8Cfg,u8Bit)       u8Cfg&=(~(1<<(u8Bit)))


#define     C_CAT9555_PARA_ERROR            C_PARA_ERROR
#define     C_CAT9555_I2C_ERROR             C_I2C_ERROR


typedef enum
{
   P0_0=0,
   P0_1,
   P0_2,
   P0_3,
   P0_4,
   P0_5,
   P0_6,
   P0_7,
   P1_0,
   P1_1,
   P1_2,
   P1_3,
   P1_4,
   P1_5,
   P1_6,
   P1_7,
   SIZE_OF_PORT
}T_CAT9555_PORT_BIT;


 


typedef enum
{
    EM_P0=0,
    EM_P1,
}CAT9555_PORT;


 


typedef struct tagCAT9555_IO
{
    uint8_t u8Input;
    uint8_t u8Output;
    //1 is Input, 0 is Output
    uint8_t u8Cfg;
}T_CAT9555_IO,*PT_CAT9555_IO;



typedef struct tagCAT9555
{
    //struct tagCAT9555* this;
    //BOOL blHasInit;
    //The Offset of the CAT9555 the value is 0-7|0x20
    uint8_t u8Addr;
    uint8_t u8InputP0;
    uint8_t u8InputP1;
    uint8_t u8OutputP0;
    uint8_t u8OutputP1;
    uint8_t u8InverOfInputP0;
    uint8_t u8InverOfInputP1;
    uint8_t u8CfgOfP0;
    uint8_t u8CfgOfP1;
    /**********************************************************************
    *: Init(T_CAT9555* pst,uint8_t u8Addr,PT_CAT9555_IO pstCAT9555P0,PT_CAT9555_IO pstCAT9555P1)
    *Para:     pst----- the point of The Owner.
               u8Addr --- The Addr of the Owner
               pstCAT9555P0 ------ the para will be set to P0 in CAT9555
               pstCAT9555P1 ------- The para will be set to P1 in CAT9555
    *Return:    >0 ----- Init Success
    *           <0 ----- FAIL
    *Deion: Set the direction of the Port in CAT9555
    ************************************************************************/
    int32_t (*Init)(struct tagCAT9555* pst,uint8_t u8Addr,PT_CAT9555_IO pstCAT9555P0,PT_CAT9555_IO pstCAT9555P1);
    /**********************************************************************
    *: ReadReg(T_CAT9555* pst,uint8_t u8RegAddr)
    *Para:     pst----- the point of The Owner.
    *          u8RegAddr -------- The Reg address wiil be read.
    *Return:    int32>0 ----- the value of the Reg
    *           <0 ----- FAIL
    *Deion: Set the direction of the Port in CAT9555
    ************************************************************************/
    int32_t (*ReadReg)(struct tagCAT9555* pst,uint8_t u8RegAddr);
    /**********************************************************************
    *: WriteReg(T_CAT9555* pst,uint8_t u8RegAddr,uint8_t* pu8Val,uint8_t u8Length)
    *Para:     pst----- the point of The Owner.
    *          u8RegAddr----- The Address of the start register
    *          pu8Val  ------- The Point of Buffer is will be writen
    *          u8Length ------ The Length of The Buffer
    *Return:    0 ----- Write success
    *           <0 ----- Write FAIL
    *Deion: Write the Data to the CAT9555 with the Command is u8RegAddr
    ************************************************************************/
    int32_t (*WriteReg)(struct tagCAT9555* pst,uint8_t u8RegAddr,uint8_t* pu8Val,uint8_t u8Length);
    /**********************************************************************
    *: GetInput(T_CAT9555* pst)
    *Para:     pst----- the point of The Owner.
    *Return:    int32>0 ----- The Input Value in the PIN of CAT9555
    *           <0 ----- FAIL
    *Deion: Set the direction of the Port in CAT9555
    ************************************************************************/
    int32_t (*GetInput)(struct tagCAT9555* pst);
    /**********************************************************************
    *: SetOutputByBit(T_CAT9555* pst,uint8_t u8Bit,uint8_t u8Value)
    *Para:     pst----- the point of The Owner.
    *          u8Bit----- The Bit in CAT9555,P0 is 0~7,P1 is 8~15
    *          u8Value  ------- The Value of the Port, you must set the Port is
    *                           output before setting the output value.
    *Return:    0 ----- Set success
    *           <0 ----- Set FAIL
    *Deion: Set the direction of the Port in CAT9555
    ************************************************************************/
    int32_t (*SetOutputByBit)(struct tagCAT9555* pst,T_CAT9555_PORT_BIT u8Bit,uint8_t u8Value);
    /**********************************************************************
    *: SetDirByBit(T_CAT9555* pst,uint8_t u8Bit,uint8_t u8Dir)
    *Para:     pst----- the point of The Owner.
    *          u8Bit----- The Bit in CAT9555,P0 is 0~7,P1 is 8~15
    *          u8Dir  ------- 1 is Input,0 is Output
    *Return:    0 ----- Set success
    *           <0 ----- Set FAIL
    *Deion: Set the direction of the Port in CAT9555
    ************************************************************************/
    int32_t (*SetDirByBit)(struct tagCAT9555* pst,T_CAT9555_PORT_BIT u8Bit,uint8_t u8Dir);
}T_CAT9555,*PT_CAT9555_REG;


int32_t Init_CAT9555(T_CAT9555* pst,uint8_t u8Addr,PT_CAT9555_IO pstCAT9555P0,PT_CAT9555_IO pstCAT9555P1);
int32_t CAT9555_ReadReg(T_CAT9555* pst,uint8_t u8RegAddr);
int32_t CAT9555_GetInput(T_CAT9555* pst);
int32_t CAT9555_SetOutputByBit(T_CAT9555* pst,T_CAT9555_PORT_BIT u8Bit,uint8_t u8Value);
int32_t CAT9555_SetDirByBit(T_CAT9555* pst,T_CAT9555_PORT_BIT u8Bit,uint8_t u8Dir);
int32_t CAT9555_WriteReg(T_CAT9555* pst,uint8_t u8RegAddr,uint8_t* pu8Val,uint8_t u8Length);


#define NEW_CAT9555()  {0,0,0,0,0,0,0,0,0, \
    Init_CAT9555,CAT9555_ReadReg,CAT9555_WriteReg, \
    CAT9555_GetInput,CAT9555_SetOutputByBit, \
    CAT9555_SetDirByBit}



#endif


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)