44b0开发板有个EEPROM可以通过IIC总线来进行读写。
不过有一点我需要解释下:EEPROM的slave address是怎么确定的呢?查看EEPROM芯片24C08的手册,发现有个Table 3. Device Select Code如下(注意我们使用的芯片M24C08这一行)
查看了一下硬件电路图,我发现管脚E2是接地的。所以,我认为,只要保证Device code是1010,E2是0的地址就是其slave adress。所以这里用0xa0.
至于表中的A9和A8,我还不明白怎么回事。
(这个问题呢,正是我在写这篇博客的时候搞明白的,呵呵。记得大一的时候有个英语老师说过这么一句话:如果想深入的了解某个东西,最好的办法就是写本书。确实,把东西记下来能更好的帮助我们去思考,我们记录的过程也是很好的思考过程。我也一直在想,在这段44b0实验的过程中,我是不是也可以写成一本实验教程呢?)
接下来需要做的实验是:
1)IIS : 概念上还不知道怎么回事呢
2)SIO: 概念上还不知道怎么回事呢
3)DMA:老早上大学的时候就知道DMA了,但是始终没有一个感性认识,希望这次实验能帮我吧
4)USB:本科毕业设计就是做的USB的数据处理采集,但是当时心思都用在研究生复试了,知之甚少。希望这次彻底搞明白吧。
5)LCD:手头还没有LCD,看情况吧,看看要不要再买一块呢?或者直接买个2440开发板,在那里做LCD实验登更高级的东西,会更划算。我想2440肯定要买的,不然这个arm7的开发板只能跑ucLinux,WinCE和Linux之类的都不能跑,多可惜。
下面直接贴代码吧。
#include <string.h>
#include "IIC.h"
#define WRDATA (1)
#define POLLACK (2)
#define RDDATA (3)
#define SETRDADDR (4)
#define IICBUFSIZE 0x20
U8 _iicData[IICBUFSIZE];
volatile int _iicDataCount;
volatile int _iicStatus;
volatile int _iicMode;
int _iicPt;
void __irq IicInt(void);
void Test_Iic(void)
{
unsigned int i,j,save_F,save_PF;
static U8 data[256];
Uart_Printf("[IIC Test using AT24LCxx]/n");
save_F=rPCONF;
save_PF=rPUPF;
rPCONF |=0xa; //PF0:IICSCL, PF1:IICSDA
rPUPF |=0x3; //pull-up disable
pISR_IIC=(unsigned)IicInt;
rINTMSK=~(BIT_GLOBAL|BIT_IIC);
rIICCON=(1<<7)|(0<<6)|(1<<5)|(0xf);
//Enable interrupt, IICCLK=MCLK/16, Enable ACK
//40Mhz/16/(15+1) = 257Khz
rIICADD=0x10; // S3C44B0X slave address
rIICSTAT=0x10;
Uart_Printf("Write test data into AT24LCxx/n");
for(i=0;i<256;i++)
Wr24LCxx(0xa0,(U8)i,i);
for(i=0;i<256;i++)
data[i]=0;
Uart_Printf("Read test data from AT24LCxx/n");
for(i=0;i<256;i++)
Rd24LCxx(0xa0,(U8)i,&(data[i]));
for(i=0;i<16;i++)
{
for(j=0;j<16;j++)
Uart_Printf("%2x ",data[i*16+j]);
Uart_Printf("/n");
}
rPCONF=save_F;
rPUPF=save_PF;
}
void Wr24LCxx(U32 slvAddr,U32 addr,U8 data)
{
_iicMode=WRDATA;
_iicPt=0;
_iicData[0]=(U8)addr;
_iicData[1]=data;
_iicDataCount=2;
rIICDS=slvAddr;//0xa0
rIICSTAT=0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(_iicDataCount!=-1);
_iicMode=POLLACK;
while(1)
{
rIICDS=slvAddr;
_iicStatus=0x100;
rIICSTAT=0xf0; //MasTx,Start
rIICCON=0xaf; //resumes IIC operation.
while(_iicStatus==0x100);
if(!(_iicStatus&0x1))
break; // when ACK is received
}
rIICSTAT=0xd0; //stop MasTx condition
rIICCON=0xaf; //resumes IIC operation.
Delay(1); //wait until stop condtion is in effect.
//write is completed.
}
void Rd24LCxx(U32 slvAddr,U32 addr,U8 *data)
{
_iicMode=SETRDADDR;
_iicPt=0;
_iicData[0]=(U8)addr;
_iicDataCount=1;
rIICDS=slvAddr;
rIICSTAT=0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(_iicDataCount!=-1);
_iicMode=RDDATA;
_iicPt=0;
_iicDataCount=1;
rIICDS=slvAddr;
rIICSTAT=0xb0; //MasRx,Start
rIICCON=0xaf; //resumes IIC operation.
while(_iicDataCount!=-1);
*data=_iicData[1];
}
void __irq IicInt(void)
{
U32 iicSt,i;
rI_ISPC=BIT_IIC;
iicSt=rIICSTAT;
if(iicSt&0x8){} // when bus arbitration is failed.
if(iicSt&0x4){} // when a slave address is matched with IICADD
if(iicSt&0x2){} // when a slave address is 0000000b
if(iicSt&0x1){} // when ACK isn't received
switch(_iicMode)
{
case POLLACK:
_iicStatus=iicSt;
break;
case RDDATA:
if((_iicDataCount--)==0)
{
_iicData[_iicPt++]=rIICDS;
rIICSTAT=0x90; //stop MasRx condition
rIICCON=0xaf; //resumes IIC operation.
Delay(1); //wait until stop condtion is in effect.
//too long time...
//The pending bit will not be set after issuing stop condition.
break;
}
_iicData[_iicPt++]=rIICDS;
//The last data has to be read with no ack.
if((_iicDataCount)==0)
rIICCON=0x2f; //resumes IIC operation with NOACK.
else
rIICCON=0xaf; //resumes IIC operation with ACK
break;
case WRDATA:
if((_iicDataCount--)==0)
{
rIICSTAT=0xd0; //stop MasTx condition
rIICCON=0xaf; //resumes IIC operation.
Delay(1); //wait until stop condtion is in effect.
//The pending bit will not be set after issuing stop condition.
break;
}
rIICDS=_iicData[_iicPt++]; //_iicData[0] has dummy.
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
rIICCON=0xaf; //resumes IIC operation.
break;
case SETRDADDR:
//Uart_Printf("[S%d]",_iicDataCount);
if((_iicDataCount--)==0)
{
break; //IIC operation is stopped because of IICCON[4]
}
rIICDS=_iicData[_iicPt++];
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
rIICCON=0xaf; //resumes IIC operation.
break;
default:
break;
}
}
最后的运行结果是: