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

📄 m24c16.c

📁 sonata2standardsourcedriver的EEProm读写方案
💻 C
字号:
//=============================================================================// File Name: //// Copyright 2003 Cirrus Logic Corporation//// Description://  The API for the MicroChip 24C16B////////============================================================================#include "apitypes.h"//#define DEBUG_MSG#define  DEBUG_CPU	0#include "eromdbg.h"#include "dvhw32.h"#include "eeprom.h"#include "apii2c.h"#include "basetype.h"		// basic types#include "osapi.h"#include "assert.h"//-----------------------------------------------------------------------------// External definitions//-----------------------------------------------------------------------------extern int API_SendMultiI2C(Byte addr, Byte subadr, Byte nbytes, Byte *data);  //-----------------------------------------------------------------------------// Local definitions//-----------------------------------------------------------------------------#define AT_DEVICE_ADDR      0x50#define EEPROM_PAGE_SIZE    16          // 16 bytes for each page-write#define EEPROM_BLOCK_SIZE   256#define EEPROM_MEM_SIZE     2048#define DEBUG_OUT(fmt, args...)         { ERomPrintFunc (DEBUG_CPU, fmt, ## args); }int EEPROM_GetSize(Uint32 *size){	*size = EEPROM_MEM_SIZE;	return SUCCESS;}//-----------------------------------------------------------------------------// Function:int EEPROM_Write8(//// Description://  This operation performs an 8 bit (byte) write to the AT24C02 eeprom.//// Parmeters:    Uint32 addr, // address to be written to    Byte data    // data byte to be written)////  Returns://          SUCCESS = 0 or FAIL////----------------------------------------------------------------------------{       return SUCCESS;} // end EEPROM_Write8//-----------------------------------------------------------------------------// Function:int EEPROM_Write32(//// Description://  This operation performs a 32 bit write to the AT24C02 eeprom. This used to be//  a page write for the at24c01, but for the at24c02 a page write is 8 bytes, not//  4.  This function is deprecated, and included for backwards compatibility with//  external functions that may call it.  The preferred method for writing is to call//  At24c02_BufWrite().////// Parmeters:    Uint32 addr,    // address to be written to    Uint32 data     // data double word (32 bits) to be written)////  Returns://          SUCCESS = 0 or FAIL////----------------------------------------------------------------------------{        return SUCCESS;} // end EEPROM_Write32//-----------------------------------------------------------------------------// Function:int EEPROM_BufWrite(//// Description://  This operation writes the buffer to the AT24C02 at the given address.//// Parmeters:*/    Uint32 addr,    // address to be written to    void * bufPtr,  // pointer to the source buffer pointer    Uint32 length   // length in bytes of data to be written)////  Returns://          SUCCESS = 0 or FAIL////----------------------------------------------------------------------------{    int status = SUCCESS;    return status;} // end EEPROM_BufWriteint EEPROM_Write(//// Description://  This operation writes the buffer to the 24C16 at the given address.//// Parmeters:*/    Uint32 addr,    // address to be written to    void* buf,  // pointer to the source buffer pointer    Uint32 length   // length in bytes of data to be written)////  Returns://          SUCCESS = 0 or FAIL////----------------------------------------------------------------------------{    int status = SUCCESS;    Uint32 i;    Uint32 block=0;    Uint32 devaddr;    Uint32 subaddr=0;    Byte data;    Byte *bufPtr = (Byte *) buf;#ifdef DEBUG_MSG    Uint32 j;    DEBUG_OUT("EEPROM_Write: addr - %x,", addr);    j = (length > 16) ? 16 : length;    for ( i = 0; i < j; i++ )   DEBUG_OUT(" %02x", (unsigned char) *((char*) buf + i));    DEBUG_OUT("\n");#endif        for ( i = 0; i < length; i++ )    {        data = *(bufPtr+i);        block = (addr + i) / EEPROM_BLOCK_SIZE;        devaddr = (AT_DEVICE_ADDR|block)<<1;        subaddr = (addr + i) % EEPROM_BLOCK_SIZE;        if (API_SendI2C(devaddr, subaddr, data) == FAIL)        {            DEBUG_OUT("EEPROM_Write error at subaddr = %d with %d\n",subaddr+i,data);            OS_TaskDelay(1);            if (API_SendI2C(devaddr, subaddr+i, data) == FAIL)            {                DEBUG_OUT("EEPROM_Write error again at subaddr = %d with %d\n",subaddr+i,data);                break;            }        }        OS_TaskDelay(1);//about 16ms    }     return status;} int EEPROM_WritePage(//// Description://  Use API_SendMultiI2C() to write 16 bytes page to the 24C16 start from the given address.// Each single API_SendMultiI2C() writes up to the 16 bytes boundary.//// Parmeters:*/    Uint32 addr,    // address to be written to    void* buf,  // pointer to the source buffer pointer    Uint32 length   // length in bytes of data to be written)////  Returns://          SUCCESS = 0 or FAIL////----------------------------------------------------------------------------{    int status = SUCCESS;    Uint32 i;    Uint32 block;    Uint32 devaddr;    Uint32 subaddr;    Uint32 bytesToWrite;        // up to the 16 bytes boundary which will automatically fall in the 256 bytes block    Byte *bufPtr = (Byte *) buf;#ifdef DEBUG_MSG    Uint32 j;    DEBUG_OUT("EEPROM_WritePage: addr/size - %x/%d,", addr, length);    for ( j = 0; j < 16; j++ )   DEBUG_OUT(" %02x", (unsigned char) *((char*) buf + j));    DEBUG_OUT("\n");#endif    for ( i = 0; i < length;)    {        bytesToWrite = EEPROM_PAGE_SIZE - ((addr + i) % EEPROM_PAGE_SIZE);        if (bytesToWrite > (length - i))   bytesToWrite = (length - i);                block = (addr + i) / EEPROM_BLOCK_SIZE;      // 256 bytes for each block        devaddr = (AT_DEVICE_ADDR|block)<<1;        subaddr = (addr + i) % EEPROM_BLOCK_SIZE;    #ifdef DEBUG_MSG        DEBUG_OUT("API_SendMultiI2C: addr/size - %x/%x/%d,", devaddr, subaddr, bytesToWrite);        for ( j = 0; j < bytesToWrite; j++ )   DEBUG_OUT(" %02x", (unsigned char) *((char*) buf + i + j));        DEBUG_OUT("\n");#endif                if (API_SendMultiI2C(devaddr, subaddr, bytesToWrite, bufPtr+i) == FAIL)        {            DEBUG_OUT("EEPROM_WritePage error at addr <%x,%x>\n",devaddr,subaddr);            OS_TaskDelay(1);            if (API_SendMultiI2C(devaddr, subaddr, bytesToWrite, bufPtr+i) == FAIL)            {                DEBUG_OUT("EEPROM_WritePage error at addr <%x,%x>\n",devaddr,subaddr);                break;            }        }        i += bytesToWrite;        OS_TaskDelay(1);//about 16ms    }         return status;} //-----------------------------------------------------------------------------// Function:int EEPROM_Read(//// Description://  This operation reads data from the 24C16 at the given address//// Parmeters:*/    Uint32 addr,    // address to be read from    void * buf,  // pointer to the target buffer    Uint32 length   // size in bytes of the target buffer)////  Returns://      n/a////----------------------------------------------------------------------------{	return EEPROM_ReadPage(addr,buf,length);} //-----------------------------------------------------------------------------// Function:int EEPROM_ReadPage(//// Description://  This operation reads data from the 24C16 at the given address//// Parmeters:*/    Uint32 addr,    // address to be read from    void * buf,  // pointer to the target buffer    Uint32 length   // size in bytes of the target buffer)////  Returns://      n/a////----------------------------------------------------------------------------{    int status = SUCCESS;	Uint32 bytesToRead;	Byte *bufPtr = (void *)buf;    Uint32 i;    Uint32 block;    Uint32 devaddr;    Uint32 subaddr;    API_SetI2CMaster ();    if ( !length )    {        status = FAIL;        ErrPrint("Failure reading -tried to read 0\n");    }     else    {		for ( i = 0; i < length;)		{			// We could actually read the entire memory contents at once, but			// limit reads to the block size so we don't hold the bus too long			bytesToRead = EEPROM_BLOCK_SIZE - ((addr + i) % EEPROM_BLOCK_SIZE);			if (bytesToRead > (length - i))				bytesToRead = (length - i);        			block = (addr + i) / EEPROM_BLOCK_SIZE;      // 256 bytes for each block			devaddr = (AT_DEVICE_ADDR|block)<<1;			subaddr = (addr + i) % EEPROM_BLOCK_SIZE;    #ifdef DEBUG_MSG			DEBUG_OUT("API_GetMultiI2C(%x %x %d %x)\n",				devaddr, subaddr, bytesToRead, bufPtr+i);#endif			if (API_GetMultiI2C(devaddr, subaddr, bytesToRead, bufPtr+i) == FAIL)			{				DEBUG_OUT("EEPROM_ReadPage error at addr <%x,%x>\n",devaddr,subaddr);				status = FAIL;				break;			}			i += bytesToRead;		}     }#ifdef DEBUG_MSG    DEBUG_OUT("EEPROM_ReadPage: addr - %x, %d \n", addr, length);	{		Uint32 j = (length + 15) / 16;		for ( i = 0; i < j; i++ )   		{			DEBUG_OUT("   %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x \n",				bufPtr[i*16 + 0],bufPtr[i*16 + 1],				bufPtr[i*16 + 2],bufPtr[i*16 + 3],				bufPtr[i*16 + 4],bufPtr[i*16 + 5],				bufPtr[i*16 + 6],bufPtr[i*16 + 7],				bufPtr[i*16 + 8],bufPtr[i*16 + 9],				bufPtr[i*16 +10],bufPtr[i*16 +11],				bufPtr[i*16 +12],bufPtr[i*16 +13],				bufPtr[i*16 +14],bufPtr[i*16 +15]);		}	}#endif    return status;} #ifdef DEBUG_MSG#define START_ADDR          0x500#define EEPROM_TEST_SIZE    0x200 // (EEPROM_MEM_SIZE - START_ADDR)void EEPROM_Test(void){    unsigned char    buf[EEPROM_TEST_SIZE], readBuf[EEPROM_TEST_SIZE];    int     i, j, testSize, startAddr, errCount;    OS_TaskDelay(600);    DEBUG_OUT("EEPROM_Test Bufs: %x, %x\n", buf, readBuf);    EEPROM_ReadPage(0, (void*)readBuf, 16);    EEPROM_Read(0, (void*)readBuf, 16);    EEPROM_ReadPage(256, (void*)readBuf, 16);    EEPROM_Read(256, (void*)readBuf, 16);    EEPROM_ReadPage(432, (void*)readBuf, 16);    EEPROM_Read(432, (void*)readBuf, 16);/*    // reset the content    for (i = 0; i < EEPROM_TEST_SIZE; i++)           buf[i] = 0;    EEPROM_Write(START_ADDR, (void*)buf, EEPROM_TEST_SIZE);*/    for (j = 0; j < 256; j ++)    {        startAddr = START_ADDR + (EEPROM_TEST_SIZE / EEPROM_PAGE_SIZE) * (j%EEPROM_PAGE_SIZE) + (j%EEPROM_PAGE_SIZE);        testSize = (EEPROM_TEST_SIZE/EEPROM_PAGE_SIZE) * (16 - (j%EEPROM_PAGE_SIZE)) - (j%EEPROM_PAGE_SIZE);        //startAddr = START_ADDR; testSize = EEPROM_TEST_SIZE;        for (i = 0; i < testSize; i++)            buf[i] = (i+j)%EEPROM_BLOCK_SIZE;        for (i = 0; i < testSize; i++)            readBuf[i] = 0;        EEPROM_WritePage(startAddr, (void*)buf, testSize);        OS_TaskDelay(2);        EEPROM_Read(startAddr, (void*)readBuf, testSize);        errCount = 0;        for (i = 0; i < testSize; i++)           {            if (readBuf[i] != buf[i])            {                DEBUG_OUT("EEPROM addr: %d, exp: %02x, act: %02x\n", i, buf[i], readBuf[i]);                errCount++;                                if (errCount > 3)                {                    soft_assert(0); // break;                }            }        }    }    DEBUG_OUT("Done\n");}#endif

⌨️ 快捷键说明

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