Update eeprom driver

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1795 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
yungchi@cs.nctu.edu.tw 2011-11-20 18:44:10 +00:00
parent 3efb7d5cda
commit 1dd92a391d
3 changed files with 69 additions and 30 deletions

View File

@ -20,10 +20,9 @@
#define EE24LC024H #define EE24LC024H
/* /*
Note: If eeprom size lager then 256 byte, you must define EE_ADDR_SIZE == I2C_MEM_2Bytes Note: If eeprom size lager then EE_MEM_SIZE byte, you must define EE_ADDR_SIZE == I2C_MEM_2Bytes
*/ */
#ifdef EE24LC024H #ifdef EE24LC024H
#define EE_PageSize 8
#define EE_ADDR_SIZE I2C_MEM_1Byte #define EE_ADDR_SIZE I2C_MEM_1Byte
#define EE_MEM_SIZE 256 #define EE_MEM_SIZE 256
#define EE_PageSize 16 #define EE_PageSize 16
@ -31,37 +30,41 @@
static struct rt_device ee_dev; static struct rt_device ee_dev;
uint32_t EE_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead) uint32_t EE_ReadBuffer(void* pBuffer, rt_off_t ReadAddr, rt_size_t NumByteToRead)
{ {
return I2C_IORW(I2C1, pBuffer, NumByteToRead, ReadAddr, EE_Address | 0x01, I2C_MEM_1Byte ); return I2C_IORW(I2C1, (uint8_t*)pBuffer, (uint16_t)NumByteToRead, (uint16_t)ReadAddr, EE_Address | 0x01, I2C_MEM_1Byte );
} }
uint32_t EE_WritePage(uint8_t* pBuffer, uint16_t WriteAddr) uint32_t EE_WritePage(void* pBuffer, uint16_t WriteAddr)
{ {
I2C_IORW(I2C1, pBuffer, EE_PageSize , WriteAddr, EE_Address , EE_ADDR_SIZE ); I2C_IORW(I2C1, (uint8_t*)pBuffer, EE_PageSize , WriteAddr, EE_Address , EE_ADDR_SIZE );
/*if( I2C_AcknowledgePolling(I2C1 , EE_Address) == Error ) /*if( I2C_AcknowledgePolling(I2C1 , EE_Address) == Error )
rt_kprintf("EE ACK failed\n");*/ rt_kprintf("EE ACK failed\n");*/
rt_thread_delay(50); rt_thread_delay(50);
return 0;
} }
uint32_t EE_WriteByte(uint8_t* pBuffer, uint16_t WriteAddr) uint32_t EE_WriteByte(void* pBuffer, uint16_t WriteAddr)
{ {
I2C_IORW(I2C1, pBuffer, 1 , WriteAddr, EE_Address, EE_ADDR_SIZE ); I2C_IORW(I2C1, (uint8_t*)pBuffer, 1 , WriteAddr, EE_Address, EE_ADDR_SIZE );
/*if( I2C_AcknowledgePolling(I2C1 , EE_Address) == Error ) /*if( I2C_AcknowledgePolling(I2C1 , EE_Address) == Error )
rt_kprintf("EE ACK failed\n");*/ rt_kprintf("EE ACK failed\n");*/
rt_thread_delay(50); rt_thread_delay(50);
return 0;
} }
Status EE_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite) Status EE_WriteBuffer(const void* pBuffer, rt_off_t WriteAddr, rt_size_t NumByteToWrite)
{ {
uint8_t NumOfPage = 0, NumOfSingle = 0; uint8_t NumOfPage = 0, NumOfSingle = 0;
uint16_t Addr = 0,count = 0; uint16_t Addr = 0,count = 0;
uint8_t* ptr = pBuffer; uint8_t* ptr = (uint8_t*)pBuffer;
Addr = WriteAddr; Addr = (uint16_t)(WriteAddr&0xFFFF);
count = NumByteToWrite; count = (uint16_t)(NumByteToWrite&0xFFFF);
if( (WriteAddr + NumByteToWrite ) > EE_MEM_SIZE ) if( (WriteAddr + NumByteToWrite ) > EE_MEM_SIZE )
return Error; return Error;
@ -96,7 +99,7 @@ static rt_size_t ee24LCxx_read( rt_device_t dev, rt_off_t pos, void *buf, rt_siz
return -1; return -1;
} }
static rt_size_t ee24LCxx_write( rt_device_t dev, rt_off_t pos, void *buf, rt_size_t size ) static rt_size_t ee24LCxx_write( rt_device_t dev, rt_off_t pos, const void *buf, rt_size_t size )
{ {
if( EE_WriteBuffer(buf, pos, size) == Success ) if( EE_WriteBuffer(buf, pos, size) == Success )
return size; return size;
@ -121,8 +124,15 @@ static rt_err_t ee24LCxx_control (rt_device_t dev, rt_uint8_t cmd, void *args)
void ee24LCxx_hw_init() void ee24LCxx_hw_init()
{ {
uint32_t delay, i;
I2C1_INIT(); I2C1_INIT();
for( i =0; i < 4; i++ )
{
delay = 0xFFFFF;
while(delay--);
}
ee_dev.init = ee24LCxx_init; ee_dev.init = ee24LCxx_init;
ee_dev.open = ee24LCxx_open; ee_dev.open = ee24LCxx_open;
ee_dev.close = ee24LCxx_close; ee_dev.close = ee24LCxx_close;
@ -135,33 +145,51 @@ void ee24LCxx_hw_init()
rt_device_register(&ee_dev, "eeprom", RT_DEVICE_FLAG_RDWR); rt_device_register(&ee_dev, "eeprom", RT_DEVICE_FLAG_RDWR);
} }
void ee_test() void dump_ee()
{ {
char buf[256], read[256];
int i,ret;
rt_device_t dev; rt_device_t dev;
dev = rt_device_find("eeprom"); char buf[EE_MEM_SIZE];
int i, j;
for(i = 0; i < 256; i++ ) dev = rt_device_find("eeprom");
rt_device_read(dev, 0, buf, EE_MEM_SIZE );
for( i = 0; i < 16; i++ )
{ {
buf[i] = i; for( j = 0; j < 16; j++ )
{
rt_kprintf("0x%02X ", buf[ i*16+ j]);
}
rt_kprintf("\n");
}
}
void ee_reset()
{
char buf[EE_MEM_SIZE], read[EE_MEM_SIZE];
int i;
rt_device_t dev = rt_device_find("eeprom");
for(i = 0; i < EE_MEM_SIZE; i++ )
{
buf[i] = 0xFF;
read[i] = 0; read[i] = 0;
} }
if( rt_device_write(dev, 0, buf, 256 ) == 256 ) if( rt_device_write(dev, 0, buf, EE_MEM_SIZE ) == EE_MEM_SIZE )
rt_kprintf("Write Success\n"); rt_kprintf("Write Success\n");
rt_device_read(dev, 0, read, 256 ); rt_device_read(dev, 0, read, EE_MEM_SIZE );
for(i = 0; i < 256; i++ ) for(i = 0; i < EE_MEM_SIZE; i++ )
{ {
if( buf[i] != read[i] ) if( buf[i] != read[i] )
rt_kprintf("EE Failed %X != %X at %d\n", buf[i], read[i], i); rt_kprintf("EE Failed %X != %X at %d\n", buf[i], read[i], i);
} }
rt_kprintf("Finsh\n");
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
FINSH_FUNCTION_EXPORT(ee_test, test system); FINSH_FUNCTION_EXPORT(ee_reset, test system);
FINSH_FUNCTION_EXPORT(dump_ee, test system);
#endif #endif

View File

@ -383,6 +383,7 @@ Status I2C_Free_Bus(I2C_TypeDef* I2Cx, u32 timeout )
I2Cx: I2C1 or I2C2 (Now it only support I2C1) I2Cx: I2C1 or I2C2 (Now it only support I2C1)
pBuffer: Buffer point pBuffer: Buffer point
NumByteToRW: Number of bytes read/write NumByteToRW: Number of bytes read/write
memAddr: 1-2 bytes memory address
SlaveAddress: device address SlaveAddress: device address
MemType: 1 = memory address size 1 bytes, 2 = memory address size 2 bytes MemType: 1 = memory address size 1 bytes, 2 = memory address size 2 bytes
*/ */

View File

@ -102,18 +102,28 @@
#define I2C2_DMA_CHANNEL_TX DMA1_Stream2 #define I2C2_DMA_CHANNEL_TX DMA1_Stream2
#define I2C2_DMA_CHANNEL_RX DMA1_Stream7 #define I2C2_DMA_CHANNEL_RX DMA1_Stream7
#define I2C2_DMA_TX_IRQn DMA1_Stream2_IRQn
#define I2C2_DMA_RX_IRQn DMA1_Stream7_IRQn
#define I2C1_DR_Address 0x40005410 #define I2C1_DR_Address 0x40005410
#define I2C2_DR_Address 0x40005810 #define I2C2_DR_Address 0x40005810
#define I2C1_SDA_PIN GPIO_Pin_9 #define I2C1_SDA_PIN GPIO_Pin_7
#define I2C1_SCL_PIN GPIO_Pin_8 #define I2C1_SCL_PIN GPIO_Pin_6
#define I2C1_SDA_SOURCE GPIO_PinSource9 #define I2C1_SDA_SOURCE GPIO_PinSource7
#define I2C1_SCL_SOURCE GPIO_PinSource8 #define I2C1_SCL_SOURCE GPIO_PinSource6
#define I2C1_GPIO_PORT GPIOB #define I2C1_GPIO_PORT GPIOB
#define I2C1_GPIO_CLK RCC_AHB1Periph_GPIOB #define I2C1_GPIO_CLK RCC_AHB1Periph_GPIOB
#define I2C1_CLK RCC_APB1Periph_I2C1 #define I2C1_CLK RCC_APB1Periph_I2C1
#define I2C2_SDA_PIN GPIO_Pin_11
#define I2C2_SCL_PIN GPIO_Pin_10
#define I2C2_SDA_SOURCE GPIO_PinSource11
#define I2C2_SCL_SOURCE GPIO_PinSource10
#define I2C2_GPIO_PORT GPIOB
#define I2C2_GPIO_CLK RCC_AHB1Periph_GPIOB
#define I2C2_CLK RCC_APB1Periph_I2C1
#define I2C_MEM_1Byte 1 #define I2C_MEM_1Byte 1
#define I2C_MEM_2Bytes 2 #define I2C_MEM_2Bytes 2