eprom.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:8k
- #include "string.h"
- #include "i2c.h"
- #include "osp.h"
- #include "eprom.h"
- #include "ste2p.h"
- #define EPROM_SIZE (0x2000)
- #define EPROM_I2C_ADDR (0xa0)
- #define EPROM_TIME_WRITE (10)
- #define EPROM_TIME_READ (1)
- #define PAGE_WRITE_SIZE (32)
- #define SEQ_READ_SIZE (1)
- static UINT32 gE2pHandle;
- static UINT32 epromSem;
- static void E2P_Protect(void);
- static void E2P_UnProtect(void);
- INT32 KB_E2PInit(void)
- {
-
- KB_I2COpen(EPROM_I2C_ADDR, &gE2pHandle);
- KB_OSPSemInit("EEPS", 1, J_OSP_WAIT_FIFO, &epromSem);
-
- return RETOK;
- }
- INT32 KB_E2PRead(UINT32 offset, UINT8 *data, UINT32 byteCount, UINT32 *byteRead)
- {
- INT32 rc;
- UINT32 index;
- UINT8 addr[2];
-
- if( (NULL == data) || (byteCount == 0)
- || (offset + byteCount > EPROM_SIZE) )
- return RETFAIL3;
- E2P_Protect();
- index = 0;
- rc = RETOK;
-
- while(byteCount >= SEQ_READ_SIZE)
- {
- addr[0] = (UINT8)((offset >> 8) & 0xFF);
- addr[1] = (UINT8)(offset & 0xFF);
- if((rc = KB_I2CWriteWithoutStop(gE2pHandle, 2, addr)) == RETOK)
- {
- if((rc = KB_I2CReadWithStop(gE2pHandle, SEQ_READ_SIZE, &data[index])) == RETOK)
- {
- byteCount -= SEQ_READ_SIZE;
- index += SEQ_READ_SIZE;
- offset += SEQ_READ_SIZE;
- }
- else
- {
- break;
- }
- }
- else
- {
- break;
- }
- }
- if((byteCount > 0) && (byteCount < SEQ_READ_SIZE) && (rc == RETOK))
- {
- addr[0] = (UINT8)((offset >> 8) & 0xFF);
- addr[1] = (UINT8)(offset & 0xFF);
- if((rc = KB_I2CWriteWithoutStop(gE2pHandle, 2, addr)) == RETOK)
- {
- if((rc = KB_I2CReadWithStop(gE2pHandle, byteCount, &data[index])) == RETOK)
- {
- index += byteCount;
- offset += byteCount;
- byteCount = 0;
- }
- }
- }
- if(byteRead != NULL) *byteRead = index;
-
- E2P_UnProtect();
-
- return rc;
- }
- /* The data word address lower 5 bits are internally incremented
- ** following the receipt of each data word. The higher
- ** data word address bits are not incremented, retaining the
- ** memory page row location. When the word address, internally
- ** generated, reaches the page boundary, the following
- ** byte is placed at the beginning of the same page. If more
- ** than 32 data words are transmitted to the EEPROM, the
- ** data word address will “roll over” and previous data will be
- ** overwritten.
- */
- INT32 KB_E2PWrite(UINT32 offset, UINT8 *data, UINT32 byteCount, UINT32 *byteWrite)
- {
- INT32 rc;
- UINT32 index, tmp;
- UINT8 addr[2 + PAGE_WRITE_SIZE]; /* 2 Bytes addr, others are data */
- if( (NULL == data) || (byteCount == 0)
- || (offset + byteCount > EPROM_SIZE) )
- return RETFAIL3;
- E2P_Protect();
- index = 0;
- rc = RETOK;
- /* Align offset to 32 for page write. */
- tmp = offset % PAGE_WRITE_SIZE;
- if(tmp)
- {
- tmp = PAGE_WRITE_SIZE - tmp;
- if(byteCount <= tmp)
- {
- tmp = byteCount;
- }
-
- addr[0] = (UINT8)((offset >> 8) & 0xFF);
- addr[1] = (UINT8)(offset & 0xFF);
- memcpy(&addr[2], &data[index], (size_t)tmp); /* copy data to addr buf */
-
- if((rc = KB_I2CWriteWithStop(gE2pHandle, 2+tmp, addr)) == RETOK)
- {
- index += tmp;
- offset += tmp;
- byteCount -= tmp;
- }
- KB_OSPTaskDelay(EPROM_TIME_WRITE);
- }
- if(rc == RETOK)
- {
- while(byteCount >= PAGE_WRITE_SIZE)
- {
- addr[0] = (UINT8)((offset >> 8) & 0xFF);
- addr[1] = (UINT8)(offset & 0xFF);
- memcpy(&addr[2], &data[index], PAGE_WRITE_SIZE); /* copy data to addr buf */
-
- if((rc = KB_I2CWriteWithStop(gE2pHandle, 2+PAGE_WRITE_SIZE, addr)) == RETOK)
- {
- byteCount -= PAGE_WRITE_SIZE;
- index += PAGE_WRITE_SIZE;
- offset += PAGE_WRITE_SIZE;
- KB_OSPTaskDelay(EPROM_TIME_WRITE);
- }
- else
- {
- KB_OSPTaskDelay(EPROM_TIME_WRITE);
- break;
- }
- }
- }
-
- if((byteCount > 0) && (rc == RETOK))
- {
- addr[0] = (UINT8)((offset >> 8) & 0xFF);
- addr[1] = (UINT8)(offset & 0xFF);
- memcpy(&addr[2], &data[index], (size_t)byteCount); /* copy data to addr buf */
-
- if((rc = KB_I2CWriteWithStop(gE2pHandle, 2+byteCount, addr)) == RETOK)
- {
- index += byteCount;
- offset += byteCount;
- byteCount = 0;
- }
- KB_OSPTaskDelay(EPROM_TIME_WRITE);
- }
- if(byteWrite != NULL) *byteWrite = index;
-
- E2P_UnProtect();
-
- return rc;
- }
- INT32 KB_E2PGetSize(void)
- {
- return EPROM_SIZE;
- }
- static void E2P_Protect(void)
- {
- KB_OSPSemGet(epromSem, KB_Wait, 0);
- }
- static void E2P_UnProtect(void)
- {
- KB_OSPSemSet(epromSem);
- }
- #if 1
- extern ST_Partition_t *SystemPartition;
- void E2P_Test(void)
- {
- U8 index;
- U32 byteWrited,byteReaded;
- U8 bufRead[255];
- U8 bufWrite[255];
- ST_ErrorCode_t rel;
- //------------------------------------------------------
- ST_ErrorCode_t error, stored;
- STE2P_InitParams_t para;
- STE2P_OpenParams_t openPara;
- STE2P_Handle_t e2pHandle;
- para.BaseAddress = NULL; /* not memory-mapped */
- strcpy( para.I2CDeviceName, "I2C_BACK" ); /* must match I2C Name */
- para.MemoryWidth = 1; /* this is not used by driver */
- para.SizeInBytes = 4*1024-4;
- para.PagingBytes = 32; /* max. number of writes in page */
- para.DeviceCode = 0xA0; /* Manufacturer's code */
- para.MaxSimOpen = 1; /* up to 2 simultaneous opens */
- para.DriverPartition = SystemPartition;//TEST_PARTITION_1;
- error = STE2P_Init("STACKTEST", ¶ );
- if (error != ST_NO_ERROR)
- {
- printf("nSTE2P_Init error.");
- }
- openPara.Offset = 0x00; /* select small seg. base */
- openPara.Length = 4*1024-4; /* select small seg. size */
- error = STE2P_Open("STACKTEST", &openPara, &e2pHandle);
- //------------------------------------------------------
- for(index=0;index<255;index++)
- {
- bufRead[index]=0xAA;
- bufWrite[index]=0xBB;
- }
- //rel=KB_E2PWrite(0,bufWrite,TEST_DATA_LEN,&byteWrited);
- rel=STE2P_Write(e2pHandle,0,(U8*)bufWrite,255,&byteWrited);
- printf("nSTE2P_Write %d 0x%x.",bufWrite[22],bufWrite[22]);
- // for(index=0;index<(255*100);index++);
- printf("nbefore STE2P_Read bufRead[22] = %d 0x%xn",bufRead[22],bufRead[22]);
- rel=STE2P_Read(e2pHandle,0,(U8*)bufRead,255,&byteReaded);
- printf("after STE2P_Read bufRead[22] = %d 0x%xn",bufRead[22],bufRead[22]);
- //rel=KB_E2PRead(0,bufRead,TEST_DATA_LEN,&byteReaded);
- }
- #else
- #define TEST_DATA_LEN 255
- U8 bufRead[TEST_DATA_LEN],bufWrite[TEST_DATA_LEN];
- extern ST_Partition_t *SystemPartition;
- #define SML_SEG_BASE 0x00000701 /* base for small seg. */
#define SML_SEG_SIZE 0x00000200 /* size of small seg. */
- void E2P_Test(void)
- {
- UINT8 index;
- U32 byteWrited,byteReaded;
- //INT32 rel;
- ST_ErrorCode_t rel;
- //------------------------------------------------------
- ST_ErrorCode_t error, stored;
- STE2P_InitParams_t para;
- STE2P_OpenParams_t openPara;
- STE2P_Handle_t e2pHandle;
-
para.BaseAddress = NULL; /* not memory-mapped */
- strcpy( para.I2CDeviceName, "I2C_BACK" ); /* must match I2C Name */
- para.MemoryWidth = 1; /* this is not used by driver */
- para.SizeInBytes = 4*1024-4;
- para.PagingBytes = 32; /* max. number of writes in page */
- para.DeviceCode = 0xA0; /* Manufacturer's code */
- para.MaxSimOpen = 1; /* up to 2 simultaneous opens */
- para.DriverPartition = SystemPartition;//TEST_PARTITION_1;
- error = STE2P_Init("STACKTEST", ¶ );
- if (error != ST_NO_ERROR)
- {
- printf("nSTE2P_Init error.");
- }
- openPara.Offset = 0x00; /* select small seg. base */
- openPara.Length = 4*1024-4; /* select small seg. size */
- error = STE2P_Open("STACKTEST", &openPara, &e2pHandle);
- //------------------------------------------------------
- for(index=0;index<TEST_DATA_LEN;index++)
- {
- bufRead[index]=0xAA;
- bufWrite[index]=0xBB;
- }
- //rel=KB_E2PWrite(0,bufWrite,TEST_DATA_LEN,&byteWrited);
- rel=STE2P_Write(e2pHandle,0,(U8*)bufWrite,TEST_DATA_LEN,&byteWrited);
- for(index=0;index<(TEST_DATA_LEN*100);index++);
- rel=STE2P_Read(e2pHandle,0,(U8*)bufRead,TEST_DATA_LEN,&byteReaded);
- //rel=KB_E2PRead(0,bufRead,TEST_DATA_LEN,&byteReaded);
- }
- #endif