250K-T8S
This commit is contained in:
351
24c02.c
Normal file
351
24c02.c
Normal file
@@ -0,0 +1,351 @@
|
||||
|
||||
#include "MAIN.H"
|
||||
|
||||
|
||||
#define _nop_() udelay(1);
|
||||
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();}
|
||||
#define bit bool
|
||||
|
||||
#define MASTER_TEST_LEN 100//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E0B7A2>100<30><30><EFBFBD>ֽڵ<D6BD><DAB5><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
////FM25CL64ָ<34><EFBFBD><EEB6A8>
|
||||
#define FM25CL64_WREN 0x06 //ʹ<><CAB9>
|
||||
#define FM25CL64_WRDI 0x04 //ʧ<><CAA7>
|
||||
#define FM25CL64_RDSR 0x05 //<2F><>״̬
|
||||
#define FM25CL64_WRSR 0x01 //д״̬
|
||||
#define FM25CL64_READ 0x03 //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#define FM25CL64_WRITE 0x02 //д<><D0B4><EFBFBD><EFBFBD>
|
||||
|
||||
#define EN_24C02 1
|
||||
|
||||
#define MAX_FM25CL64_LEN 8192//8k<38>ֽ<EFBFBD>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void iic_start(void)
|
||||
{
|
||||
SDAOut(1);
|
||||
_nop_();
|
||||
SCLOut(1);
|
||||
somenop;
|
||||
SDAOut(0);
|
||||
somenop;
|
||||
SCLOut(0);
|
||||
}
|
||||
|
||||
void iic_stop(void)
|
||||
{
|
||||
SDAOut(0);
|
||||
_nop_();
|
||||
SCLOut(1);
|
||||
somenop;
|
||||
SDAOut(1);
|
||||
}
|
||||
|
||||
void iic_ack(bit ackbit)
|
||||
{
|
||||
if(ackbit)
|
||||
SDAOut(0);
|
||||
else
|
||||
SDAOut(1);
|
||||
somenop;
|
||||
SCLOut(1);
|
||||
somenop;
|
||||
SCLOut(0);
|
||||
SDAOut(1);
|
||||
somenop;
|
||||
}
|
||||
|
||||
bit iic_waitack(void)
|
||||
{
|
||||
SDAOut(1);
|
||||
somenop;
|
||||
SCLOut(1);
|
||||
// sdrv_pinctrl_set_input_select(GPIO_Y3, PIN_IS_CMOS_SCHMITT);
|
||||
// sdrv_gpio_set_pin_direction(GPIO_Y3, GPIO_DIR_IN);
|
||||
somenop;
|
||||
if(sdrv_gpio_read_pin_input_level(GPIO_Y3))
|
||||
{
|
||||
SCLOut(0);
|
||||
iic_stop();
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
SCLOut(0);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void iic_sendbyte(unsigned char byt)
|
||||
{
|
||||
unsigned char i;
|
||||
for(i=0;i<8;i++)
|
||||
{
|
||||
if(byt&0x80)
|
||||
SDAOut(1);
|
||||
else
|
||||
SDAOut(0);
|
||||
somenop;
|
||||
SCLOut(1);
|
||||
byt <<= 1;
|
||||
somenop;
|
||||
SCLOut(0);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char iic_recbyte(void)
|
||||
{
|
||||
unsigned char da;
|
||||
unsigned char i;
|
||||
for(i=0;i<8;i++)
|
||||
{
|
||||
SCLOut(1);
|
||||
// sdrv_pinctrl_set_input_select(GPIO_Y3, PIN_IS_CMOS_SCHMITT);
|
||||
// sdrv_gpio_set_pin_direction(GPIO_Y3, GPIO_DIR_IN);
|
||||
somenop;
|
||||
da <<= 1;
|
||||
|
||||
// P1_IOCR01 = 0x0020; // load port control register 1
|
||||
if(sdrv_gpio_read_pin_input_level(GPIO_Y3))
|
||||
da |= 0x01;
|
||||
SCLOut(0);
|
||||
somenop;
|
||||
}
|
||||
return da;
|
||||
}
|
||||
|
||||
void wrbyte_24c02(unsigned int add,unsigned char dat)
|
||||
{
|
||||
#if EN_24C02
|
||||
unsigned char eeprom_page = 0;
|
||||
unsigned char eeprom_add = 0;
|
||||
unsigned char device_add = 0;
|
||||
//-------------------------------------------------
|
||||
eeprom_page = add/256;//ҳ
|
||||
eeprom_add = add%256;
|
||||
if(eeprom_page >= 3)//ĿǰE2һ<32><D2BB>4ҳ<34><D2B3>1k
|
||||
{
|
||||
eeprom_page = 3;
|
||||
}
|
||||
// Device Address 1100 0 p1 p0 R/W
|
||||
device_add = (0xa0 | (eeprom_page << 1));//
|
||||
|
||||
// ssdk_printf(SSDK_CRIT, "eeprom_page:0x%x\r\n", eeprom_page);
|
||||
// ssdk_printf(SSDK_CRIT, "eeprom_add:0x%x\r\n", eeprom_add);
|
||||
// ssdk_printf(SSDK_CRIT, "device_add:0x%x\r\n", device_add);
|
||||
|
||||
iic_start();
|
||||
iic_sendbyte(device_add);
|
||||
iic_waitack();
|
||||
iic_sendbyte(eeprom_add);
|
||||
iic_waitack();
|
||||
iic_sendbyte(dat);
|
||||
iic_waitack();
|
||||
iic_stop();
|
||||
#else
|
||||
if(add >= MAX_FM25CL64_LEN)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>
|
||||
{
|
||||
printf("E2 len error!\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_tx_buf_u8[0] = FM25CL64_WREN;//дʹ<D0B4><CAB9>
|
||||
if (sdrv_spi_sync_transmit(&g_master, m_tx_buf_u8, m_rx_buf_u8,1,1))
|
||||
{
|
||||
printf("E2 write Failed!\r\n");
|
||||
}
|
||||
|
||||
m_tx_buf_u8[0] = FM25CL64_WRITE;//д<><D0B4><EFBFBD><EFBFBD>
|
||||
m_tx_buf_u8[1] = (uint8_t)( add >> 8);// <20><>8λ<38><CEBB>ַ
|
||||
m_tx_buf_u8[2] = (uint8_t)( add );//
|
||||
m_tx_buf_u8[3] = dat;//
|
||||
|
||||
if (sdrv_spi_sync_transmit(&g_master, m_tx_buf_u8, m_rx_buf_u8,4,4))
|
||||
{
|
||||
printf("E2 write Failed!\r\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
unsigned char rdbyte_24c02(unsigned int add)
|
||||
{
|
||||
|
||||
#if EN_24C02
|
||||
unsigned char eeprom_page = 0;
|
||||
unsigned char eeprom_add = 0;
|
||||
unsigned char device_add = 0;
|
||||
unsigned char da;
|
||||
//-------------------------------------------------
|
||||
|
||||
eeprom_page = add/256;//ҳ
|
||||
eeprom_add = add%256;
|
||||
if(eeprom_page >= 3)//ĿǰE2һ<32><D2BB>4ҳ<34><D2B3>1k
|
||||
{
|
||||
eeprom_page = 3;
|
||||
}
|
||||
|
||||
// Device Address 1100 000 R/W
|
||||
iic_start();
|
||||
device_add = (0xa0 | (eeprom_page << 1));// αд<CEB1><D0B4><EFBFBD><EFBFBD>
|
||||
iic_sendbyte(device_add);
|
||||
iic_waitack();
|
||||
iic_sendbyte(eeprom_add);
|
||||
iic_waitack();
|
||||
iic_start();
|
||||
|
||||
device_add = (0xa1 | (eeprom_page << 1));//
|
||||
|
||||
ssdk_printf(SSDK_CRIT, "device_add:0x%x\r\n", device_add);
|
||||
|
||||
iic_sendbyte(device_add);
|
||||
iic_waitack();
|
||||
da = iic_recbyte();
|
||||
iic_ack(0);
|
||||
iic_stop();
|
||||
//CAN_sendAck(add, da);
|
||||
return da;
|
||||
#else
|
||||
if(add >= MAX_FM25CL64_LEN)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>
|
||||
{
|
||||
printf("E2 len error!\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_tx_buf_u8[0] = FM25CL64_READ;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
m_tx_buf_u8[1] = (uint8_t)( add >> 8);// <20><>8λ<38><CEBB>ַ
|
||||
m_tx_buf_u8[2] = (uint8_t)( add );//
|
||||
m_tx_buf_u8[3] = 0xFF;//
|
||||
|
||||
if (sdrv_spi_sync_transmit(&g_master, m_tx_buf_u8, m_rx_buf_u8,4,4))
|
||||
{
|
||||
printf("E2 write Failed!\r\n");
|
||||
}
|
||||
|
||||
return m_rx_buf_u8[3];
|
||||
}
|
||||
#endif
|
||||
return 0xFF;
|
||||
}
|
||||
|
||||
|
||||
#ifdef _EEROM_CRC_
|
||||
#define POLY 0x1021
|
||||
/**
|
||||
* Calculating CRC-16 in 'C'
|
||||
* @para addr, start of data
|
||||
* @para num, length of data
|
||||
* @para crc, incoming CRC
|
||||
*/
|
||||
unsigned int crc16(unsigned char *addr, int num, unsigned int crc)
|
||||
{
|
||||
int i;
|
||||
for (; num > 0; num--) /* Step through bytes in memory */
|
||||
{
|
||||
crc = crc ^ ((*addr) << 8); /* Fetch byte from memory, XOR into CRC top byte*/
|
||||
addr++;
|
||||
for (i = 0; i < 8; i++) /* Prepare to rotate 8 bits */
|
||||
{
|
||||
if (crc & 0x8000) /* b15 is set... */
|
||||
crc = (crc << 1) ^ POLY; /* rotate and XOR with polynomic */
|
||||
else /* b15 is clear... */
|
||||
crc <<= 1; /* just rotate */
|
||||
} /* Loop for 8 bits */
|
||||
crc &= 0xFFFF; /* Ensure CRC remains 16-bit value */
|
||||
} /* Loop until num=0 */
|
||||
return(crc); /* Return updated CRC */
|
||||
}
|
||||
|
||||
//д<><D0B4><EFBFBD><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD>Զ<EFBFBD>дһ<D0B4><D2BB><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ͳ<EFBFBD><CDB2><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>
|
||||
unsigned char wrEE_CRC_Bak(unsigned char add,unsigned char *eeData,unsigned char len,unsigned char addbak)
|
||||
{
|
||||
uword crc_Res;
|
||||
unsigned char index;
|
||||
unsigned char flagEEwrOK;
|
||||
|
||||
crc_Res = crc16(eeData, len-2, 0xffff); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>У<EFBFBD><D0A3>
|
||||
eeData[len-2] = (ubyte)(crc_Res );
|
||||
eeData[len-1] = (ubyte)(crc_Res >> 8);
|
||||
|
||||
for(index=0;index<len;index++) //д<><D0B4><EFBFBD><EFBFBD>
|
||||
{WDT_vServiceWDT();
|
||||
wrbyte_24c02( add + index, eeData[index]);
|
||||
}
|
||||
flagEEwrOK = 1;
|
||||
|
||||
for(index=0;index<len;index++) //У<><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{WDT_vServiceWDT();
|
||||
if(rdbyte_24c02( add + index) != eeData[index])
|
||||
{
|
||||
flagEEwrOK = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(1 == flagEEwrOK) //
|
||||
{
|
||||
for(index=0;index<len;index++) //д<><D0B4><EFBFBD>ݺ<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{WDT_vServiceWDT();
|
||||
wrbyte_24c02( addbak + index, eeData[index]);
|
||||
}
|
||||
flagEEwrOK = 2;
|
||||
for(index=0;index<len;index++)
|
||||
{WDT_vServiceWDT();
|
||||
if(rdbyte_24c02( addbak + index) != eeData[index])
|
||||
{ flagEEwrOK = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
flagEEwrOK = 0;
|
||||
//.........
|
||||
}
|
||||
return flagEEwrOK;
|
||||
}
|
||||
unsigned char rdEE_CRC_Bak(unsigned char add,unsigned char *eeData,unsigned char len,unsigned char addbak)
|
||||
{
|
||||
uword crc_Res;
|
||||
unsigned char index;
|
||||
unsigned char reEEok = 0;
|
||||
|
||||
for(index=0;index<len;index++) //<2F><><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{WDT_vServiceWDT();
|
||||
eeData[index] = rdbyte_24c02(add + index);
|
||||
}
|
||||
crc_Res = crc16(eeData, len-2, 0xffff); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>У<EFBFBD><D0A3>
|
||||
if( crc_Res == ((uword)eeData[len-1] << 8) + (uword)eeData[len-2]) //<2F>Աȶ<D4B1>ȡ<EFBFBD><C8A1>У<EFBFBD><D0A3>ֵ <20><>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>EEֵ<45><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ...
|
||||
{
|
||||
reEEok = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
for(index=0;index<len;index++) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{WDT_vServiceWDT();
|
||||
eeData[index] = rdbyte_24c02(addbak + index);
|
||||
}
|
||||
crc_Res = crc16(eeData, len-2, 0xffff); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>У<EFBFBD><D0A3>
|
||||
if( crc_Res == ((uword)eeData[len-1] << 8) + (uword)eeData[len-2]) //<2F>Աȶ<D4B1>ȡ<EFBFBD><C8A1>У<EFBFBD><D0A3>ֵ <20><>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>EEֵ<45><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ...
|
||||
{
|
||||
reEEok = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
reEEok = 0;
|
||||
}
|
||||
}
|
||||
return reEEok;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user