📄 seed_rtciic.c
字号:
/*
* Copyright 2005 by SEED.
* All rights reserved. Property of SEED.
* Restricted rights to use, duplicate or disclose this code are
* granted through contract.
*
*/
/* August 2005 */
/********************************************************************/
/* seed_rtciic.c file */
/********************************************************************/
#include <csl_gpio.h>
#include "seed_rtciic.h"
#define I2CDELAY(iterations) { \
volatile Int j; \
for(j = 0; j < iterations; j ++); \
}
#define DELAY_TIME 1000
static const I2C_Config VPM642RTCIIC_WriteConfig = {
0, /* master mode, i2coar; */
0, /* no interrupt, i2cimr; */
(32-5), /* scl low time, i2cclkl; */
(32-5), /* scl high time,i2cclkh; */
1, /* configure later, i2ccnt;*/
0, /* configure later, i2csar;*/
0x46a0, /* master tx mode, */
/* i2c runs free, */
/* 8-bit data + NACK */
/* repeat mode */
(75-1), /* 4MHz clock, i2cpsc */
};
static const I2C_Config VPM642RTCIIC_ReadConfig = {
0, /* master mode, i2coar; */
0, /* no interrupt, i2cimr; */
(32-5), /* scl low time, i2cclkl; */
(32-5), /* scl high time,i2cclkh; */
1, /* configure later, i2ccnt;*/
0, /* configure later, i2csar;*/
0x44a0, /* master rx mode, */
/* i2c runs free, */
/* 8-bit data + ACK */
/* repeat mode */
(75-1), /* 4MHz clock, i2cpsc */
};
/*
* ======== _IIC_write ========
* This function performs write operation via I2C bus.
*/
void RTC_IIC_write(I2C_Handle hI2C,
Uint8 devAddress,
Uint32 subAddress,
Uint8 *data,
Uint16 numBytes
)
{
Int i;
// Int gie;
I2C_Config prevIICConfig;
/* make sure handle is valid */
if(hI2C == INV) {
return;
}
/*禁止所有的中断*/
// gie = IRQ_globalDisable();
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* save old settings */
I2C_getConfig(hI2C, &prevIICConfig);
/* set I2C mode register */
I2C_RSETH(hI2C, I2CMDR, VPM642RTCIIC_WriteConfig.i2cmdr);
/* set I2C imr register */
I2C_RSETH(hI2C, I2CIMR, VPM642RTCIIC_WriteConfig.i2cimr);
/* configure the I2C slave address register */
I2C_RSETH(hI2C, I2CSAR, devAddress);
/* set I2C count register */
I2C_RSETH(hI2C, I2CCNT, numBytes + 1);
/* write the sub address */
I2C_writeByte(hI2C, ((subAddress & 0xFF00)>>8));
/* Generate start condition */
I2C_start(hI2C);
while(!I2C_xrdy(hI2C));
/* write the sub address */
I2C_writeByte(hI2C, subAddress);
I2CDELAY(DELAY_TIME);
/* write the data */
for(i = 0; i < numBytes; i ++) {
while(!I2C_xrdy(hI2C));
I2C_writeByte(hI2C, *data ++);
I2CDELAY(DELAY_TIME);
}
/* Generate stop condition */
I2C_sendStop(hI2C);
I2CDELAY(DELAY_TIME);
/* Wait until bus is free */
while (I2C_bb(hI2C));
I2CDELAY(DELAY_TIME);
/* now restore the previous I2C settings */
I2C_config(hI2C, &prevIICConfig);
I2CDELAY(DELAY_TIME);
/*重新使能中断*/
// IRQ_globalRestore(gie);
}
void RTC_IIC_read(I2C_Handle hI2C,
Uint8 devAddress,
Uint32 subAddress,
Uint8 *data,
Uint16 numBytes
)
{
Int i;
// Int gie;
I2C_Config prevIICConfig;
/* make sure handle is valid */
if(hI2C == INV) {
return;
}
/*禁止所有的中断*/
// gie = IRQ_globalDisable();
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* save old settings */
I2C_getConfig(hI2C, &prevIICConfig);
/* set I2C mode register */
I2C_RSETH(hI2C, I2CMDR, VPM642RTCIIC_WriteConfig.i2cmdr);
/* set I2C imr register */
I2C_RSETH(hI2C, I2CIMR, VPM642RTCIIC_WriteConfig.i2cimr);
/* configure the I2C slave address register */
I2C_RSETH(hI2C, I2CSAR, devAddress);
/* set I2C count register */
I2C_RSETH(hI2C, I2CCNT, 1);
I2C_writeByte(hI2C, ((subAddress & 0xFF00)>>8));
/* Generate start condition */
I2C_start(hI2C);
I2CDELAY(DELAY_TIME);
/* write the data */
while(!I2C_xrdy(hI2C));
/* write the sub address */
I2C_writeByte(hI2C, subAddress);
I2CDELAY(DELAY_TIME);
/* waiting for sub-address to be transmitted */
while(!I2C_xrdy(hI2C));
/* now enter the master receiver mode */
I2C_RSETH(hI2C, I2CMDR, VPM642RTCIIC_ReadConfig.i2cmdr);
/* set I2C count register */
I2C_RSETH(hI2C, I2CCNT, numBytes);
/* clear the DRR register */
I2C_readByte(hI2C);
/* Generate start condition */
I2C_start(hI2C);
I2CDELAY(DELAY_TIME);
/* read the data */
for(i = 0; i < numBytes; i ++) {
while(!I2C_rrdy(hI2C));
*data++ = I2C_readByte(hI2C);
I2CDELAY(DELAY_TIME);
}
/* Generate stop condition */
I2C_sendStop(hI2C);
I2CDELAY(DELAY_TIME);
/* Wait until bus is free */
while (I2C_bb(hI2C));
I2CDELAY(DELAY_TIME);
/* now restore the previous I2C settings */
I2C_config(hI2C, &prevIICConfig);
I2CDELAY(DELAY_TIME);
/*重新使能中断*/
// IRQ_globalRestore(gie);
}
//write Enable
void RTC_write_enable(I2C_Handle hI2C)
{
Uint8 devAddr = 0x6f;
Uint8 writeenable = 0;
/*将IIC总线切换到IIC1*/
GPIO_RSET(GPGC,0x0);/*将GPIO0不做为GPINT使用*/
GPIO_RSET(GPDIR,0x1);/*将GPIO0做为输出*/
GPIO_RSET(GPVAL,0x1);/*GPIO0输出为高,选择IIC1总线1*/
I2CDELAY(DELAY_TIME);
/*将02写入到Status Register,使能WEL位*/
writeenable = 0x2;
RTC_IIC_write(hI2C,devAddr,RTC_X1226_SR,&writeenable,1);
}
//write the rtc register by IIC bus
void RTC_write(I2C_Handle hI2C, Uint32 subAddress, Uint8 *data)
{
Uint8 devAddr = 0x6f;
Uint8 writeenable = 0;
/*将IIC总线切换到IIC1*/
GPIO_RSET(GPGC,0x0);/*将GPIO0不做为GPINT使用*/
GPIO_RSET(GPDIR,0x1);/*将GPIO0做为输出*/
GPIO_RSET(GPVAL,0x1);/*GPIO0输出为高,选择IIC1总线1*/
I2CDELAY(DELAY_TIME);
/*将02写入到Status Register,使能WEL位*/
writeenable = 0x2;
RTC_IIC_write(hI2C,devAddr,RTC_X1226_SR,&writeenable,1);
/*将02写入到Status Register,使能WEL位*/
writeenable = 0x6;
RTC_IIC_write(hI2C,devAddr,RTC_X1226_SR,&writeenable,1);
RTC_IIC_write(hI2C,devAddr,subAddress,data,1);
writeenable = 0x0;
}
//read the rtc register by IIC bus
void RTC_read( I2C_Handle hI2C,
Uint32 subAddress,
Uint8 *data)
{
Uint8 devAddr = 0x6f;
Uint8 writeenable = 0;
/*将IIC总线切换到IIC1*/
GPIO_RSET(GPGC,0x0);/*将GPIO0不做为GPINT使用*/
GPIO_RSET(GPDIR,0x1);/*将GPIO0做为输出*/
GPIO_RSET(GPVAL,0x1);/*GPIO0输出为高,选择IIC1总线1*/
I2CDELAY(DELAY_TIME);
/*将02写入到Status Register,使能WEL位*/
writeenable = 0x2;
RTC_IIC_write(hI2C,devAddr,RTC_X1226_SR,&writeenable,1);
/*将02写入到Status Register,使能WEL位*/
writeenable = 0x6;
RTC_IIC_write(hI2C,devAddr,RTC_X1226_SR,&writeenable,1);
RTC_IIC_read(hI2C,devAddr,subAddress,data,1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -