📄 test.cpp
字号:
return 0;
thePack.txLen1 = 2;
thePack.txLen2 = 1;
thePack.rxLen = byteNum;
thePack.rxData = dataAddr;
thePack.repeatTimes = CAT24C021_READ_REPEAT;
// The bit8 and bit9 of baseaddr must put in slave address
// a sector has 256 bytes, so the following operation will
// save the bit8 and bit9 in sect_addr
sect_addr = baseAddr >> 8;
sect_addr = sect_addr << 1;
thePack.txData[0] = CAT24C021_SLAVE_ADDR | sect_addr | OP_WRITE;
thePack.txData[1] = (baseAddr & 0xff);
thePack.txData[2] = CAT24C021_SLAVE_ADDR | sect_addr | OP_READ;
/*
if (OPMODE_INTERRUPT == sMBusInfo.mode)
{
if (1 == MBusPackQuePut(&gCAT24C021ReadPackQue, &thePack))
{
EnterInterrupt( );
// Important changing: LLH.2001.07.04
// A Sleep added here to wait for the data trasnferring been done,
// so, after returning from this function, you can use data be read
// immedidately.
//NU_Sleep( 50);
k = 0;
while( k < 50000 )
k++;
return 1;
}
return 0;
}
else
*/
return MBusTransferByQuery(&thePack);
}
/**
* CAT24C021: write several bytes( less than or equal to 16 bytes).
*
* @param baseAddr the first address
* @param byteNum the number of bytes to be written
* @param data point to what to write
*
* @return 1 if the operation succeed.
* 0 if the operation failure because packetQue is full.
*/
CHAR CAT24C021WritePage(UINT16 baseAddr, UINT16 byteNum, UCHAR *data)
{
MBusPackType thePack;
UCHAR sect_addr;
CHAR i;
// one page has less than 16 bytes
if ( 1 > byteNum || CAT24C021_PAGE_SIZE < byteNum)
return 0;
thePack.txLen1 = 2 + byteNum;
thePack.txLen2 = 0;
thePack.rxLen = 0;
thePack.repeatTimes = CAT24C021_WRITE_REPEAT;
// The bit8 and bit9 of baseaddr must put in slave address
// a sector has 256 bytes, so the following operation will
// save the bit8 and bit9 in sect_addr
sect_addr = baseAddr >> 8;
sect_addr = sect_addr << 1;
thePack.txData[0] = CAT24C021_SLAVE_ADDR | sect_addr | OP_WRITE;
thePack.txData[1] = (baseAddr & 0xff);
for (i = 0; i < byteNum; i ++)
thePack.txData[2+i] = *data ++;
/*
if (OPMODE_INTERRUPT == sMBusInfo.mode)
{
if (1 == MBusPackQuePut(&gCAT24C021WritePackQue, &thePack))
{
EnterInterrupt( );
return 1;
}
return 0;
}
else
*/
return MBusTransferByQuery(&thePack);
}
/**
* CAT24C021: write several bytes( no limitation to the number).
*
* @param baseAddr the first address
* @param byteNum the number of bytes to be written
* @param data point to what to write
*
* @return 1 if the operation succeed.
* 0 if the operation failure because packetQue is full
* or byteNum is improper.
*/
CHAR CAT24C021WriteSeqByte(UINT16 baseAddr, UINT16 byteNum, UCHAR *data)
{
UCHAR i;
if (1 > byteNum || 2048 < (baseAddr + byteNum) )
return 0;
// write the first bytes of one page.
i = CAT24C021_PAGE_SIZE - ( baseAddr % CAT24C021_PAGE_SIZE );
if (i > byteNum)
i = byteNum;
if (0 == CAT24C021WritePage(baseAddr, i, data) )
return 0;
baseAddr += i;
data += i;
byteNum -= i;
// transfer data page by page
while (CAT24C021_PAGE_SIZE <= byteNum)
{
if (0 == CAT24C021WritePage(baseAddr, CAT24C021_PAGE_SIZE, data) )
return 0;
baseAddr += CAT24C021_PAGE_SIZE;
data += CAT24C021_PAGE_SIZE;
byteNum -= CAT24C021_PAGE_SIZE;
}
// transfer the remained data.
if (0 < byteNum)
return CAT24C021WritePage(baseAddr, byteNum, data);
else
return 1;
}
/**
* X1203 initialize: check clock, set time system, set periodic interrupt
* cycle.
*
* @return 0 X1203 not exist.
* 1 operation succeed and clock is OK
* 2 operation succeed but clock has been stopped, clock need set
*
* @note very important: this function work only in query mode.
*/
static CHAR X1203Initialize( void )
{
// Default time: 2000.1.1, 00:00:00
UNCHAR initTime[6] = {0, 0, 0x80, 1, 1, 0};
UNCHAR clockNeedSet = 0;
UNCHAR data;
// clear error counter
sMBusInfo.transferFail = 0;
// read control register 2
if (0 == X1203ReadSeqByte( X1203_CONTROL_REG, 1, &data))
return 0;
// X1203 not acknowledge, it may not exist.
if (1 <= sMBusInfo.transferFail)
return 0;
// Determine whether clock need re-set or not
if (X1203_RTCF == (X1203_RTCF&data))
clockNeedSet = 1;
// X1203: enable WEL
data = X1203_WEL;
X1203WritePage( X1203_CONTROL_REG, 1, &data);
// X1203: enable RWEL and WEL
data = X1203_WEL | X1203_RWEL;
X1203WritePage( X1203_CONTROL_REG, 1, &data);
if (clockNeedSet)
{
X1203WritePage( 0x30, 6, initTime );
return 2;
}
else
return 1;
}
/**
* RS5C372A: write several bytes(less than 16 bytes).
*
* @param baseAddr the first address
* @param byteNum the number of bytes to be written
* @param data point to what to write
*
* @return 1 if the operation succeed.
* 0 if the operation failure because packetQue is full.
*/
static CHAR RS5C372AWriteSeqByte(UNCHAR baseAddr, UNCHAR byteNum, UNCHAR* data)
{
MBusPackType thePack;
CHAR i;
if ( 1 > byteNum || 16 < byteNum)
return 0;
thePack.txLen1 = 2 + byteNum;
thePack.txLen2 = 0;
thePack.rxLen = 0;
thePack.txData[0] = RS5C372A_SLAVE_ADDR | OP_WRITE;
thePack.txData[1] = (baseAddr << 4);
thePack.repeatTimes = RS5C372A_WRITE_REPEAT;
for (i = 0; i < byteNum; i ++)
thePack.txData[2+i] = *data ++;
/*
if (OPMODE_INTERRUPT == sMBusInfo.mode)
{
if (1 == MBusPackQuePut(&gRS5C372AWritePackQue, &thePack))
{
EnterInterrupt( );
return 1;
}
return 0;
}
else
*/
return MBusTransferByQuery(&thePack);
}
/**
* RS5C372A: read several bytes(less than 16 bytes).
*
* @param baseAddr the first address
* @param byteNum the number of bytes to be written
* @param data point to what to write
*
* @return 1 if the operation succeed.
* 0 if the operation failure because packetQue is full.
*/
static CHAR RS5C372AReadSeqByte(UNCHAR baseAddr, UNCHAR byteNum, UNCHAR* data)
{
MBusPackType thePack;
CHAR i;
if ( 1 > byteNum || 16 < byteNum)
return 0;
thePack.txLen1 = 2;
thePack.txLen2 = 1;
thePack.rxLen = byteNum;
thePack.rxData = data;
thePack.repeatTimes = RS5C372A_READ_REPEAT;
thePack.txData[0] = RS5C372A_SLAVE_ADDR | OP_WRITE;
thePack.txData[1] = (baseAddr << 4);
thePack.txData[2] = RS5C372A_SLAVE_ADDR | OP_READ;
/*
if (OPMODE_INTERRUPT == sMBusInfo.mode)
{
if (1 == MBusPackQuePut(&gRS5C372AReadPackQue, &thePack))
{
EnterInterrupt( );
return 1;
}
return 0;
}
else
*/
return MBusTransferByQuery(&thePack);
}
/**
* X1203: write several bytes(less than 8 bytes).
*
* @param baseAddr the first address
* @param byteNum the number of bytes to be written
* @param data point to what to write
*
* @return 1 if the operation succeed.
* 0 if the operation failure because packetQue is full.
*/
static CHAR X1203WritePage(UNCHAR baseAddr, UNCHAR byteNum, UNCHAR* data)
{
MBusPackType thePack;
CHAR i;
if ( 1 > byteNum || 8 < byteNum)
return 0;
thePack.txLen1 = 3 + byteNum;
thePack.txLen2 = 0;
thePack.rxLen = 0;
thePack.txData[0] = X1203_SLAVE_ADDR | OP_WRITE;
thePack.txData[1] = X1203_CCR_ADDR1;
thePack.txData[2] = baseAddr;
thePack.repeatTimes = X1203_WRITE_REPEAT;
for (i = 0; i < byteNum; i ++)
thePack.txData[3+i] = *data ++;
/*
if (OPMODE_INTERRUPT == sMBusInfo.mode)
{
if (1 == MBusPackQuePut(&gX1203WritePackQue, &thePack))
{
EnterInterrupt( );
return 1;
}
return 0;
}
else
*/
return MBusTransferByQuery(&thePack);
}
/**
* X1203: read several bytes.
*
* @param baseAddr the first byte address
* @param byteNum the number of bytes to be read
* @param dataAddr where to store result
*
* @return 1 if the operation succeed.
* 0 if the operation failure because packetQue is full.
*/
static CHAR X1203ReadSeqByte(UNCHAR baseAddr, UNCHAR byteNum, UNCHAR *dataAddr)
{
MBusPackType thePack;
thePack.txLen1 = 3;
thePack.txLen2 = 1;
thePack.rxLen = byteNum;
thePack.rxData = dataAddr;
thePack.repeatTimes = X1203_READ_REPEAT;
thePack.txData[0] = X1203_SLAVE_ADDR | OP_WRITE;
thePack.txData[1] = X1203_CCR_ADDR1;
thePack.txData[2] = baseAddr;
thePack.txData[3] = X1203_SLAVE_ADDR | OP_READ;
/*
if (OPMODE_INTERRUPT == sMBusInfo.mode)
{
if (1 == MBusPackQuePut(&gX1203ReadPackQue, &thePack))
{
EnterInterrupt( );
return 1;
}
return 0;
}
else
*/
return MBusTransferByQuery(&thePack);
}
/**
* RS5C372A initialize: check clock, set time system, set periodic interrupt
* cycle.
*
* @return 0 RS5C372A not exist.
* 1 operation succeed and clock is OK
* 2 operation succeed but clock has been stopped, clock need set
*
* @note very important: this function work only in query mode.
*/
static CHAR RS5C372AInitialize( void )
{
UNCHAR clockNeedSet = 0;
UNCHAR data;
// if MBUS working not in query mode, return
if (OPMODE_QUERY != sMBusInfo.mode)
return 0;
sMBusInfo.transferFail = 0;
// read control register 2
if (0 == RS5C372AReadSeqByte( RS5C372A_CONTROL_REG2, 1, &data))
return 0;
if (1 <= sMBusInfo.transferFail )
return 0;
// Determine whether clock need re-set or not
data = 0;
if (RS5C372A_XSTP == (RS5C372A_XSTP&data))
{
clockNeedSet = 1;
// reset control register 2
RS5C372AWriteSeqByte( RS5C372A_CONTROL_REG2, 1, &data);
}
// Set control register 1
data = 0
// | RS5C372A_TIMER_SECOND
// | RS5C372A_TIMER_MINUTE // be careful: don't select exclusive items
;
RS5C372AWriteSeqByte( RS5C372A_CONTROL_REG1, 1, &data);
// Set control register 2
data = 0
| RS5C372A_24HOUR_SYSTEM
// | RS5C372A_12HOUR_SYSTEM // be careful: don't select exclusive items
// | RS5C372A_ENABLE_TIMER
;
RS5C372AWriteSeqByte( RS5C372A_CONTROL_REG2, 1, &data);
if (clockNeedSet)
return 2;
else
return 1;
}
/**
* Real-time clock: read time.
*
* @param theTime where to store time read from RTC.
*
* @return 1 if the operation succeed.
* 0 if the operation failure because packetQue is full.
*
* @note when work in interrupt mode, the time return may be the time
* read last time. call this function for one time when initializing.
*
* time items(all value are HEX data)
* ITEM VALUE RANGE
* second 0 - 59
* minute 0 - 59
* hour 0 - 23
* day 1 - 31
* month 1 - 12
* year 2000 - 2099
*/
CHAR RTCReadTime(Stime *theTime)
{
UCHAR time[7];
CHAR result;
INT16 delta = 0, i;
Stime localTime;
// transfer data
switch (sMBusInfo.rtcChip)
{
case RTC_X1203:
result = X1203ReadSeqByte(0x30, 7, sTime);
// Copy data to a local memory.
for (i = 0; i < 7; i ++)
time[i] = sTime[i];
// 24HOUR system
if (X1203_24HOUR_SYSTEM == (time[2]&X1203_24HOUR_SYSTEM))
{
time[2] &= ~X1203_24HOUR_SYSTEM;
localTime.hour = ( time[2] >> 4 ) * 10 + ( time[2] & 0xf );
}
else
// 12HOUR system
{
// Convert to 24HOUR system
if (X1203_HOUR_PM == (time[2]&X1203_HOUR_PM))
{
time[2] &= 0x1f;
localTime.hour = ( time[2] >> 4 ) * 10 + ( time[2] & 0xf ) + 12;
}
else
localTime.hour = ( time[2] >> 4 ) * 10 + ( time[2] & 0xf );
}
break;
case RTC_RS5C372A:
result = RS5C372AReadSeqByte(0, 7, sTime);
// Copy data to a local memory.
for (i = 0; i < 7; i ++)
time[i] = sTime[i];
localTime.hour = ( time[2] >> 4 ) * 10 + ( time[2] & 0xf );
delta = 1;
break;
case RTC_DS1337:
result = DS1337ReadSeqByte(DS1337_SET_ADDR, 7, sTime);
// Copy data to a local memory.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -