⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eeprom_24cxx.c

📁 IT projecotr reference design.
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************/
/*             TEXAS INSTRUMENTS PROPRIETARY INFORMATION                    */
/*                                                                          */
/*  (c) Copyright, Texas Instruments Incorporated, 2006.                    */
/*      All Rights Reserved.                                                */
/*                                                                          */
/*  Property of Texas Instruments Incorporated. Restricted Rights -         */
/*  Use, duplication, or disclosure is subject to restrictions set          */
/*  forth in TI's program license agreement and associated documentation.   */
/****************************************************************************/

/****************************************************************************/
/* eeeprom_24cXX.c.                                                         */
/*                                                                          */
/* 24cXX-family EEPROM driver.                                              */
/****************************************************************************/

#include <string.h>

#include "common.h"
#include "ddp2230_rtos_include.h"
#include "tmr.h"
#include "i2c.h"

#include "taskParm.h"
#include "eeprom_24cXX.h"

  

/****************************************************************************/
/* Local data.                                                              */
/****************************************************************************/

#define PMAX 32     /* max pageSize defined in following memParms structure */

static const struct
{
    uint16 memSize;                                /* size of memory, bytes */
    uint08 pageSize;                          /* size of memory page, bytes */
    uint08 addrMask;                                   /* address byte mask */
    uint08 addrSize;                       /* number of bytes in subaddress */
    
} memParms[] =
{
    {  128,  8, 0x00, 1 },                                         /* 24C01 */
    {  256,  8, 0x00, 1 },                                         /* 24C02 */
    {  512, 16, 0x02, 1 },                                         /* 24C04 */
    { 1024, 16, 0x06, 1 },                                         /* 24C08 */
    { 2048, 16, 0x0e, 1 },                                         /* 24C16 */
    { 4096, 32, 0x00, 2 },                                         /* 24C32 */
    { 8192, 32, 0x00, 2 }                                          /* 24C64 */
};

static uint32  semID;                              /* i2c port semaphore ID */
static int32   tktimeout;                     /* access timeout, RTOS ticks */
static I2CPORT portNo;                      /* i2c port connected to EEPROM */
static uint16  memSize;                            /* size of memory, bytes */
static uint08  pageSize;                      /* size of memory page, bytes */
static uint08  addrMask;                               /* address byte mask */
static uint08  addrSize;                   /* number of bytes in subaddress */
static uint08  baseAddr;                           /* base i2c address << 1 */

static void (*pMakeAddr)( uint16 offset, uint08 *devaddr, uint16 *subaddr );

static BOOL    isOpen = FALSE;                       /* driver is open flag */


/****************************************************************************/
/* Local functions.                                                         */
/****************************************************************************/

static EEPROM_CC _ccTestRtn( int08 APIcc, EEPROM_CC cc );
static int08     _waitComplete( void );
  
                        /****************************************************/
                        /* Make i2c address byte and subbaddress byte for   */
                        /* devices with an 8-bit subaddress.                */
                        /****************************************************/

static void makeAddr08( uint16 offset, uint08 *devaddr, uint16 *subaddr )
{
   *devaddr = baseAddr | ( addrMask & ( offset >> 7 ));
   *subaddr = offset & 0xff;
}

                        /****************************************************/
                        /* Make i2c address byte and subbaddress bytes for  */
                        /* devices with a 16-bit subaddress.                */
                        /****************************************************/

static void makeAddr16( uint16 offset, uint08 *devaddr, uint16 *subaddr )
{
   *devaddr = baseAddr;                 /* no offset bits in device address */
   *subaddr = ( offset >> 8 ) | ( offset << 8 );              /* swap bytes */
}

  
  
/****************************************************************************/
/* Open the driver.                                                         */
/****************************************************************************/

EEPROM_CC EEPROM_Open( const EEPROM_INITSTRUCT *pInitStruct )
{
    int px = pInitStruct -> eeType;                      /* part type index */
    
    if( EE_24MAX <= px )                            /* if unknown part type */
        return EEPROM_DEVICE;                                    /* unknown */
    else
        isOpen = TRUE;                      /* driver is open and available */
    
    portNo    = pInitStruct -> portNo;                  /* ASIC port number */
    baseAddr  = pInitStruct -> baseAddr;               /* base address << 1 */
    tktimeout = TMR_ConvertMSToTicks( 100 * pInitStruct -> ttimeout );

    memSize   = memParms[px].memSize;                 /* memory size, bytes */
    pageSize  = memParms[px].pageSize;                  /* page size, bytes */
    addrMask  = memParms[px].addrMask;                      /* address mask */
    addrSize  = memParms[px].addrSize;            /* subaddress size, bytes */
    
    if( 1 == addrSize )
       pMakeAddr = makeAddr08;                      /* make 8-bit addresses */
    else
       pMakeAddr = makeAddr16;                     /* make 16-bit addresses */
       
    I2C_GetSemaphoreID( portNo, &semID );              /* port semaphore ID */

    return EEPROM_PASS;
}
  
  
  
/****************************************************************************/
/* Get data.                                                                */
/****************************************************************************/

EEPROM_CC EEPROM_GetData( BOOL mirror, uint16 offset, size_t numBytes, void *data )
{
    int08   cc;                                      /* API completion code */
    uint08  lower[PMAX];                     /* lower buffer, page size max */
    uint08  upper[PMAX];                     /* upper buffer, page size max */
    uint08  devAddr;                  /* device address including page bits */
    uint16  subAddr;                                  /* device sub-address */
    uint08  readCount;                           /* number of bytes to read */
    uint32  count;                                        /* transfer count */
    void   *dest;                                  /* read data destination */
    
    if( !isOpen )
        return EEPROM_CLOSED;
        
    if( mirror && (( offset + numBytes ) > ( memSize >> 1 )))
        return EEPROM_ADDR;
    else if(( offset + numBytes ) > memSize )
        return EEPROM_ADDR;

    if( RTA_SUCCESS != RTA_SemReserve( semID, tktimeout ))
        return EEPROM_SEM;

                        /****************************************************/
                        /* Read leading non-page aligned bytes.             */
                        /****************************************************/

    if( 0 != ( readCount = ( offset % pageSize )))
    {
        readCount = pageSize - readCount;         /* non-page aligned bytes */
        
        if( readCount > numBytes )       /* if fewer than remaining in page */
            readCount = numBytes;
            
        dest      = ( NULL == data ) ? lower : data;      /* no return data */
       
        (*pMakeAddr)( offset, &devAddr, &subAddr );       /* make addresses */
        
        if( PASS != ( cc = I2C_MasterWriteRestartRead( portNo, devAddr, addrSize,
                        (uint08*)&subAddr, 0, readCount, dest, tktimeout,
                         &count, &count )))
        {
            return _ccTestRtn( cc, EEPROM_API );
        }
                        /****************************************************/
                        /* Read and compare mirror.                         */
                        /****************************************************/

        if( mirror )
        {
            (*pMakeAddr)( offset + ( memSize >> 1 ), &devAddr, &subAddr );
            
            if( PASS != ( cc = I2C_MasterWriteRestartRead( portNo, devAddr, addrSize,
                            (uint08*)&subAddr, 0, readCount, upper, tktimeout,
                             &count, &count )))
            {
                return _ccTestRtn( cc, EEPROM_API );
            }

            if( memcmp( dest, upper, readCount ))          /* if miscompare */
            {
                return _ccTestRtn( PASS, EEPROM_FAIL );
            }
        }
        
        numBytes -= readCount;                 /* bump remaining byte count */
        offset   += readCount;                       /* bump EEPROM address */
        data      = (char*)data + readCount;    /* bump data buffer address */
    }
                        /****************************************************/
                        /* Read remaining page-aligned bytes.               */
                        /****************************************************/

    while( numBytes )
    {
        readCount = ( numBytes > pageSize ) ? pageSize : numBytes; /* count */
        dest = ( NULL == data ) ? lower : data;           /* no return data */
       
        (*pMakeAddr)( offset, &devAddr, &subAddr );       /* make addresses */
        
        if( PASS != ( cc = I2C_MasterWriteRestartRead( portNo, devAddr, addrSize,
                        (uint08*)&subAddr, 0, readCount, dest, tktimeout, 
                         &count, &count )))
        {
            return _ccTestRtn( cc, EEPROM_API );
        }
        
        if( mirror )
        {
            (*pMakeAddr)( offset + ( memSize >> 1 ), &devAddr, &subAddr );
            

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -