📄 storage.c
字号:
/*************************************
;Ade7169 demo program
;*************************************
;AUTHOR: Su RuTong
;DATE: 03 09 2006
;*************************************/
#include "hal.h"
#include "storage.h"
#include "utilities.h"
#include "sys_init.h"
#include "public.h"
#include "Display.h"
#include "RTC.h"
#include "Measure.h"
#include "UART.h"
unsigned char idata I2C_LEN;
unsigned char idata *I2C_PTR;
unsigned char idata ee_timeout_cnt = 0;
unsigned short idata I2C_ADDR;
#ifndef __IAR_SYSTEMS_ICC__
// setup I2C
void InitI2C(void)
{
CFG &= ~SCPS; // select I2C port for control of shared I2C/SPI lines,P0.4 and P0.6 are used as SDATA and SCLK for I2C
PINMAP0 |= (BIT4|BIT6); // disable the weak pull up resistors on P0.4 and P0.6
I2CMOD = 0x60;
IEIP2 &= ~ESPI_I2C; // disable the I2C/SPI interrupt
}
void ee_write_byte(unsigned short addr,unsigned char idata *dat)
{
//enable I2C
I2CMOD |= BIT7;//I2CEN=1;
//initialize the control word=1010 00 B0 0=Write operation
I2CADR = 0xa0 | (unsigned char)((unsigned short)(addr & 0x100)>>7);
SPI2CTx = (unsigned char)(addr & 0x0ff); //sends the address
//send the byte to be written
SPI2CTx = *dat;
}
unsigned char ee_write_bytes(unsigned short addr,unsigned char idata *dat,unsigned char len)
{
unsigned char i, rc =ERROR;
ee_timeout_cnt = 200;
I2C_LEN = len;
I2C_PTR = dat;
I2C_ADDR = addr;
ee_write_byte(I2C_ADDR,I2C_PTR);
while ((SPI2CSTAT_bit.BUSY)&&(ee_timeout_cnt != 0))
{
if(SPI2CSTAT_bit.I2CNOACK)
{
//erase NoACK bit by writing over a 0
SPI2CSTAT &= (~BIT6);
//set bit7 to empty the Tx FIFO
SPI2CSTAT |= 0x80;
ee_write_byte(I2C_ADDR,I2C_PTR);
}
CLR_WDT();
ee_timeout_cnt --;
}
I2C_LEN --;
I2C_PTR --;
I2C_ADDR ++;
for(i=0;i<I2C_LEN;i++,I2C_LEN--,I2C_ADDR++)
{
if(I2C_LEN>1)
{
SPI2CSTAT &= (~BIT6);
ee_write_byte(I2C_ADDR,I2C_PTR);
}
else
{
SPI2CSTAT &= (~BIT6);
}
// todo timeout
}
return rc=OK;
}
void ee_read_byte(void)
{
unsigned char timeout_cnt = 200;
//read data from RxFIFO until it becomes empty
while ((SPI2CSTAT & 0x0c)&&(timeout_cnt !=0))
{
//read from Rx buffer
*I2C_PTR = SPI2CRx;
//if during this process, a FIFO location has been read and written in the same time,
//then bit1 of SPI2CSTAT has been set. If this happened, we need to restart the
//entire reading process, so we set bit1 of I2C_Status to signal this
//if (I2CSTAT & Bit1) { I2C_Status = I2C_Status | Bit1;}
I2C_PTR ++;
I2C_LEN --;
CLR_WDT();
timeout_cnt --;
}
}
unsigned char ee_read_bytes(unsigned short addr,unsigned char idata*dat,unsigned char len)
{
//unsigned char i;
I2C_LEN = len;
I2C_PTR = dat;
I2C_ADDR = addr;
//enable I2C
I2CMOD |= BIT7;//I2CEN =1;
//initialize the control word=1010 00 B0 0=Write operation
I2CADR = 0xa0 | (unsigned char)((unsigned short)(I2C_ADDR & 0x100)>>7);
SPI2CTx = (unsigned char)(I2C_ADDR & 0x0ff); //sends the address
ee_timeout_cnt = 200;
while((I2C_LEN)&&(ee_timeout_cnt != 0))
{
if (SPI2CSTAT_bit.I2CNOACK)
{
//clear NoACK bit by writing over a 0
SPI2CSTAT &= (~BIT6);
//set bit7 to empty the Tx FIFO
SPI2CSTAT |= BIT7;
//enable I2C
I2CMOD |= BIT7;//I2CEN=1;
//initialize the control word=1010 00 B0 0=Write operation
I2CADR = 0xa0 | (unsigned char)((unsigned short)(I2C_ADDR & 0x100)>>7);
SPI2CTx = (unsigned char)(I2C_ADDR & 0x0ff); //sends the address
ee_timeout_cnt = 200;
}
if (SPI2CSTAT_bit.I2CTxIRQ)
{
//enable I2C, put N-1 bytes to read into the register
I2CMOD = ((I2CMOD & 0xE0) | 0x80) | (I2C_LEN-1);
//initialize the control word=1010 00 B0 1=Read operation
I2CADR = 0xa1 | ((I2CADR & 0x100)>>7);
ee_timeout_cnt = 200;
}
if (SPI2CSTAT_bit.I2CRxIRQ)
{
ee_read_byte();
ee_timeout_cnt = 200;
}
// todo timeout
ee_timeout_cnt --;
CLR_WDT();
}
return rc=OK;
}
#else
unsigned char I2CTemp0;
unsigned short I2CTemp1;
unsigned char ErrIndication = 0;
// setup I2C
void InitI2C(void)
{
//CFG &= ~SCPS; // select I2C port for control of shared I2C/SPI lines,P0.4 and P0.6 are used as SDATA and SCLK for I2C
PINMAP0 |= (BIT4|BIT6); // disable the weak pull up resistors on P0.4 and P0.6
I2CMOD = 00;//0x60;
IEIP2 &= ~ESPI_I2C; // disable the I2C/SPI interrupt
}
void I2CSCLHighToLow(void)
{
I2C_SCL = 1;
_no_operation();
_no_operation();
_no_operation();
//delay(5);
I2C_SCL = 0;
}
void I2CStart(void)
{
I2C_SDA = 1;
I2C_SCL = 1;
_no_operation();
_no_operation();
_no_operation();
//delay(5);
I2C_SDA = 0;
I2C_SCL = 0;
}
void I2CStop(void)
{
I2C_SDA = 0;
I2C_SCL = 1;
_no_operation();
_no_operation();
//delay(5);
I2C_SDA = 1;
CLR_WDT();
}
void I2CAck(void)
{
I2C_SDA = 0;
I2CSCLHighToLow();
}
void I2CNoAck(void)
{
I2C_SDA = 1;
I2CSCLHighToLow();
}
unsigned char I2CReadByte(void)
{
unsigned char temp = 0, BitCounter = 8;
I2C_SDA = 1;//0;
do{
temp <<= 1;
I2C_SCL = 1;
_no_operation();
_no_operation();
//delay(5);
if(I2C_SDA) temp ++;
I2C_SCL = 0;
}while(-- BitCounter);
// I2C_PN(DIR) |= I2C_SDA;
return(temp);
}
void I2CSendByte(unsigned char dat)
{
unsigned char BitCounter = 8;
unsigned char wait = 0;
do{
if(dat & 0x80) I2C_SDA = 1;
else I2C_SDA = 0;
I2CSCLHighToLow();
dat <<= 1;
}while(-- BitCounter);
// 将WAITACK放入SENDBYTE又省空间又有效率
I2C_SDA = 0;
I2C_SCL = 1;
while(I2C_SDA) // 循环等待ACK信号255次循环
{
wait ++;
if(!wait)
{
ErrIndication = 1;
break;
}
};
I2C_SCL = 0;
// I2C_PN(DIR) |= I2C_SDA;
}
void I2CBegin()
{
for(;I2CTemp1>=0x100;I2CTemp1-=0x100)
{
I2CTemp0+=2;
}
I2CStart();
I2CSendByte(I2CTemp0);
I2CSendByte((unsigned char)I2CTemp1);
}
unsigned char ee_read_bytes(unsigned short addr,unsigned char idata *dat,unsigned char len)
{
unsigned char i,j=0, rc = ERROR;
// set_led_ind(LED2);
// I2C_PN(OUT) &= ~(I2C_VCC);
// delay(500);
ReadRestar:
ErrIndication=0;
I2CTemp0=0xA0;
I2CTemp1=(unsigned short)addr;
for(i=0;i<len;i++,I2CTemp1++)
{
I2CBegin();
I2CStart();
I2CSendByte(I2CTemp0+1);
dat[i]=I2CReadByte();
I2CNoAck();
I2CStop();
}
if(ErrIndication)
{
j++;
if(j<3)
{
goto ReadRestar;
}
//SET_BELL(4);
}
else
{
rc = OK;
//SET_BELL(2);
}
//I2C_READ_END:
// I2C_PN(OUT) |= I2C_VCC;
// clear_led_ind(LED2);
return rc;
}
unsigned char ee_write_bytes(unsigned short addr,unsigned char idata *dat,unsigned char len)
{
unsigned char i,j,rc = ERROR;
// set_led_ind(LED2);
// I2C_PN(OUT) &= ~(I2C_VCC);
// delay(500);
j=0;
WriteRestar:
I2CTemp1=addr;
ErrIndication=0;
I2CTemp0=0xA0;
// I2C_PN(OUT) &= ~(I2C_WP);
for(i=0;i<len;i++,I2CTemp1++)
{
I2CBegin();
I2CSendByte(dat[i]);
I2CStop();
delay(2000);
}
// I2C_PN(OUT) |= I2C_WP;
if(ErrIndication)
{
j++;
if(j<3) goto WriteRestar;
//SET_BELL(4);
}
else
{
//SET_BELL(2);
rc = OK;
}
//I2C_WRITE_END:
// I2C_PN(OUT) |= I2C_VCC;
// clear_led_ind(LED2);
return rc;
}
#endif
#if 0
void test(void)
{
//_mem_set(&Uart_buf[3],0xAA,20);
//ee_write_bytes(0,&Uart_buf[3],20);
//delay(0xFF00);
ee_read_bytes(20,&Uart_buf[3],20);
Uart_buf[0]=0x5A;
Uart_buf[1]=20;
Uart_buf[2]=10;
_uart_start_tx();
delay(0xFF00);
// _DisplayTime();
// UartTest();
// lcd_buf[11] = 0xff;
//_Lcd_WScreen(lcd_buf);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -