📄 seeddm642_rtc.c
字号:
/********************************************************************/
/* Copyright 2004 by SEED Incorporated. */
/* All rights reserved. Property of SEED Incorporated. */
/* Restricted rights to use, duplicate or disclose this code are */
/* granted through contract. */
/* */
/********************************************************************/
/*
* ======== seeddm642_rtc.c ========
* I2C RTC module implementation for SEED-DM642
*/
#include <std.h>
#include <csl.h>
#include <csl_gpio.h>
#include <csl_i2c.h>
#include "seeddm642_rtc.h"
I2C_Config rtcReadCfg = {
0x0000007f, /* I2COAR - Not used if master */
0x00000000, /* I2CIER - Disable interrupts, use polling */
0x0000001b, /* I2CCLKL - Low period for 100KHz operation */
0x0000001b, /* I2CCLKH - High period for 100KHz operation */
0x00000002, /* I2CCNT - Data words per transmission */
0x0000006f, /* I2CSAR - Slave address */
0x000044a0, /* I2CMDR - Mode */
0x00000019 /* I2CPSC - Prescale 300MHz to 12MHz */
};
I2C_Config rtcWriteCfg = {
0x0000007f, /* I2COAR - Not used if master */
0x00000000, /* I2CIER - Disable interrupts, use polling */
0x0000001b, /* I2CCLKL - Low period for 100KHz operation */
0x0000001b, /* I2CCLKH - High period for 100KHz operation */
0x00000002, /* I2CCNT - Data words per transmission */
0x0000006f, /* I2CSAR - Slave address */
0x000046a0, /* I2CMDR - Mode */
0x00000019 /* I2CPSC - Prescale 300MHz to 12MHz */
};
/********************************************************************/
/*rtc model register write */
/********************************************************************/
void SEEDDM642_RTC_regwrite( I2C_Handle hI2C,
Uint32 src,
Uint32 dst,
Uint32 length)
{
Uint8 *psrc;
Uint32 i;
I2C_Config prevI2CCfg;
/*将IIC总线切换到IIC1*/
GPIO_RSET(GPGC,0x0);/*将GPIO0不做为GPINT使用*/
GPIO_RSET(GPDIR,0x1);/*将GPIO0做为输出*/
GPIO_RSET(GPVAL,0x1);/*GPIO0输出为高,选择IIC1总线1*/
/* Establish source pointer */
psrc = (Uint8 *)src;
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* Clear bus busy */
I2C_FSETH(hI2C, I2CSTR, BB, 1);
/* Save old settings */
I2C_getConfig(hI2C, &prevI2CCfg);
SEEDDM642_waitusec(10);
/* Configure for transmit */
I2C_config(hI2C, &rtcWriteCfg);
SEEDDM642_waitusec(1);
/* Submit the high address byte */
I2C_writeByte(hI2C, (dst & 0xff00) >> 8);
/* Generate start condition */
I2C_start(hI2C);
while(!I2C_xrdy(hI2C));
/* Submit the low address byte */
I2C_writeByte(hI2C, dst & 0xff);
while(!I2C_xrdy(hI2C));
/* Transmit the data */
for (i = 0; i < length; i++)
{
I2C_writeByte(hI2C, *psrc++);
while(!I2C_xrdy(hI2C));
}
/* Generate stop condition */
I2C_sendStop(hI2C);
/* Need to wait at least 10ms */
SEEDDM642_waitusec(10000);
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* Reconfigure I2C with old settings */
I2C_config(hI2C, &prevI2CCfg);
}
/*
* ======== SEEDDM642_rtc_read ========
* Read data from an I2C EEPROM
*/
void SEEDDM642_RTC_read(I2C_Handle hI2C,
Uint32 src,
Uint32 dst,
Uint32 length)
{
Uint8 *pdst;
Uint32 i;
I2C_Config prevI2CCfg;
/*将IIC总线切换到IIC1*/
GPIO_RSET(GPGC,0x0);/*将GPIO0不做为GPINT使用*/
GPIO_RSET(GPDIR,0x1);/*将GPIO0做为输出*/
GPIO_RSET(GPVAL,0x1);/*GPIO0输出为高,选择IIC1总线1*/
/*delay for a while*/
SEEDDM642_waitusec(1);
/* Establish destination pointer */
pdst = (Uint8 *)dst;
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* Save old settings */
I2C_getConfig(hI2C, &prevI2CCfg);
/* Set address using dummy write */
SEEDDM642_RTC_regwrite(hI2C,0, src, 0);
/* Configure for receive */
I2C_config(hI2C, &rtcReadCfg);
SEEDDM642_waitusec(1);
/* Generate start condition */
I2C_start(hI2C);
/* Receive the data */
for (i = 0; i < length; i++)
{
while(!I2C_rrdy(hI2C));
*pdst++ = I2C_readByte(hI2C);
}
/* Generate stop condition */
I2C_sendStop(hI2C);
/* Need to wait at least 10ms */
SEEDDM642_waitusec(10000);
/* Wait until bus is free */
while (I2C_bb(hI2C));
/* Reconfigure I2C with old settings */
I2C_config(hI2C, &prevI2CCfg);
}
/********************************************************************/
/*rtc model write */
/********************************************************************/
void SEEDDM642_RTC_write( I2C_Handle hI2C,
Uint32 src,
Uint32 dst,
Uint32 length)
{
Uint32 writeenable = 0;
/*将02写入到Status Register,使能WEL位*/
writeenable = 0x2;
SEEDDM642_RTC_regwrite( hI2C,
(Uint32)(&writeenable),
RTC_X1226_SR,
1);
/*将02写入到Status Register,使能WEL位*/
writeenable = 0x6;
SEEDDM642_RTC_regwrite( hI2C,
(Uint32)(&writeenable),
RTC_X1226_SR,
1);
/*写入相应的寄存器*/
SEEDDM642_RTC_regwrite( hI2C,
src,
dst,
length);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -