📄 para.c.bak
字号:
/************************************
* para.c *
* save parameters *
* design by Liulixun *
* create at 04-13-2007 *
* update: 04-15-2007 *
************************************/
/* **************************************
Memory organization
------------------------
| SEC0 | 55 f0 ... |
------------------------
| SEC1 | 55 f0 ... |
------------------------
0x55: head
0xf0: using
0x00: erased
0xff: empty
0xfc: copying
If a sector is now in use, the 1st 2 byte is 0x55f0, else it should be 0x5500.
When sector 0 is full, we should copy sector 0 to sector 1, then then write
0x5500 to the begining 2 bytes of sector 0.
*****************************************/
#include "includes.h"
/*
// parameters to save:
#define PARA_ERASHED 0x00 // parameter erased
#define PARA_HEAD 0x55 // head of parameter
#define H_USING 0xf0
#define H_ERASED 0x00
#define H_COPYING 0xfc
#define H_EMPTY 0xff
#define PARA_EMPTY 0xff // unused
#define PARA_SWITCHS 0xfe // switch parameters
#define F_BORFC 0x01 // beep on rf card
#define F_BOTC 0x02 // beep on touch card
#define F_CINV 0x04 // invert package
#define F_NOTICE 0x08 // notice when card status changed
#define F_SNOA 0x10 // stop notice on ack
#define F_ASC 0x20 // auto search card
#define F_LED 0x40 // enable/disable led operation
#define PARA_LORC 0xfd // led on rf card
#define PARA_LOTC 0xfc // led on both card
#define PARA_LOBC 0xfb // led on touch card
#define PARA_LONC 0xfa // led on none card
#define PARA_RFCCT 0xf9 // rf card config time, 1 for 10 ms
#define PARA_RFRXGAIN 0xf8 //////////////////////////////////////////////////////////////////////////////////////////////////////
#define SECTOR_SIZE 0x80
*/
static code unsigned int ParaStart = 0x1c00;
static code unsigned int ParaSize = SECTOR_SIZE * 2;
// copy parameters to next sector when one is full
// dir: 1: sec 0 --> sect 1; 0: sec 1 --> sec 0
static void ParaCopy(bit dir);
// write a parameter
// para: parameter type, defined upon
// value: parameter value
// return: positive value for new value, -1 for error
int ParaWrite(unsigned char para, unsigned char value)
{
unsigned int i;
unsigned char oldvalue;
unsigned char oldpara;
unsigned int StartAddr;
bit fRedo = 0;
// find which sector is now in use.
if(FlashRdByte(ParaStart+1) == H_USING)
StartAddr = ParaStart + 2;
else if(FlashRdByte(ParaStart + SECTOR_SIZE + 1) == H_USING)
StartAddr = ParaStart + SECTOR_SIZE + 2;
else
return -1;
Redo:
// search and write
for(i=StartAddr; i<StartAddr+SECTOR_SIZE-2; i+=2)
{
oldpara = FlashRdByte(i);
if(oldpara == PARA_EMPTY) // if empty, write para
{
FlashWrByte(i, para);
oldpara = FlashRdByte(i);
}
if(oldpara == para) // same as input para
{
oldvalue = FlashRdByte(i+1);
if(oldvalue == value) // same value, return success
{
return oldvalue;
}
else if((oldvalue | value) == oldvalue) // can be over write
{
FlashWrByte(i+1, value);
return FlashRdByte(i+1);
}
else // should be erase 1st
{ // then find a new place
FlashWrByte(i, PARA_ERASHED);
}
}
}
// check redo flag
if(fRedo)
return -1;
// copy parameters to new sector and erase old one
// and update start address
if(StartAddr == ParaStart+2)
{
ParaCopy(1);
StartAddr = ParaStart + SECTOR_SIZE + 2;
}
else
{
ParaCopy(0);
StartAddr = ParaStart + 2;
}
// update redo flag
fRedo = 1;
// again
goto Redo;
}
// read a pafameter value
// para: parameter to be read
// return: positive value for reading, -1 for error
int ParaRead(unsigned char para)
{
unsigned int i;
unsigned int StartAddr;
unsigned char oldpara;
// find which sector is in use now
if(FlashRdByte(ParaStart+1) == H_USING)
StartAddr = ParaStart + 2;
else if(FlashRdByte(ParaStart+SECTOR_SIZE+1) == H_USING)
StartAddr = ParaStart + SECTOR_SIZE + 2;
else
return -1;
// search parameter
for(i=StartAddr; i<StartAddr+SECTOR_SIZE-2; i+=2)
{
oldpara = FlashRdByte(i);
if(oldpara == para)
return FlashRdByte(i+1);
else if(oldpara == PARA_EMPTY)
return -1;
}
return -1;
}
// parameter sector initialize
// return: <2 for exist, >=2 for new formated
unsigned char ParaMemInit(void)
{
unsigned int s0head, s0flag;
unsigned int s1head, s1flag;
unsigned char ch = 0;
ComWrite("Mem initializing...\r\n", 21);
s0head = FlashRdByte(ParaStart);
s0flag = FlashRdByte(ParaStart+1);
s1head = FlashRdByte(ParaStart+SECTOR_SIZE);
s1flag = FlashRdByte(ParaStart+SECTOR_SIZE+1);
checkhead:
// check sector 0
if(s0head != PARA_HEAD)
{
FlashErSector(ParaStart);
FlashWrByte(ParaStart, PARA_HEAD);
s0flag = FlashRdByte(ParaStart+1);
ch ++;
ComWrite("S0 empty.\r\n", 11);
}
// check sector 1
if(s1head != PARA_HEAD)
{
FlashErSector(ParaStart+SECTOR_SIZE);
FlashWrByte(ParaStart+SECTOR_SIZE, PARA_HEAD);
s1flag = FlashRdByte(ParaStart+SECTOR_SIZE+1);
ch ++;
ComWrite("S1 empty.\r\n", 11);
}
// deside which sector is to be used
if((s0flag == H_EMPTY) && (s1flag == H_EMPTY))
{
FlashWrByte(ParaStart+1, H_USING);
ch += 2;
ComWrite("Both empty, use S0.\r\n", 21);
}
else if((s0flag == H_ERASED) && (s1flag == H_COPYING))
{
FlashWrByte(ParaStart+SECTOR_SIZE+1, H_USING);
ComWrite("Use S1.\r\n", 9);
}
else if((s1flag == H_ERASED) && (s0flag == H_COPYING))
{
FlashWrByte(ParaStart+1, H_USING);
ComWrite("Use S0.\r\n", 9);
}
else if((s0flag != H_USING) && (s1flag != H_USING))
{
s0head = PARA_EMPTY;
s1head = PARA_EMPTY;
ComWrite("Error, check again and format.\r\n", 32);
goto checkhead;
}
return ch;
}
// set the initialzation values
unsigned char ParaInit(void)
{
if(ParaMemInit() >= 2)
{
ComWrite("New mem or mem error, use default values. Writing...\r\n", 54);
ParaWrite(PARA_SWITCHS, F_BORFC | F_BOTC | F_NOTICE | F_ASC | F_SNOA | F_LED);
ParaWrite(PARA_LORC, 12);
ParaWrite(PARA_LOTC, 25);
ParaWrite(PARA_LOBC, 0xff);
ParaWrite(PARA_LONC, 0);
ParaWrite(PARA_RFCCT, 20);
ParaWrite(PARA_RFRXGAIN, 2);
ParaWrite(PARA_LIMIT, 0xff);
ComWrite("Ok.\r\n", 5);
}
return 0;
}
/******************************************** internal functions ***************************************/
// copy parameters to next sector when one is full
// dir: 1: sec 0 --> sect 1; 0: sec 1 --> sec 0
static void ParaCopy(bit dir)
{
unsigned int i0, i01, i1, i11;
unsigned int StartAddr;
unsigned char oldpara;
// 1, check dir
if(dir)
{
StartAddr = ParaStart + 2;
i0 = ParaStart + 2;
i1 = ParaStart + SECTOR_SIZE + 2;
}
else
{
StartAddr = ParaStart + SECTOR_SIZE + 2;
i1 = ParaStart + 2;
i0 = ParaStart + SECTOR_SIZE + 2;
}
i11 = i1;
i01 = i0;
// 2, if new sector not empty, erase
if(FlashRdByte(i1-1) != H_EMPTY)
FlashErSector(i1-2);
// 3, write head 0
if(FlashRdByte(i1-2) != PARA_HEAD)
FlashWrByte(i1-2, PARA_HEAD);
// 4, write head 1 to show copying status
FlashWrByte(i1-1, H_COPYING);
// 5, copy
for( ; i0<StartAddr+SECTOR_SIZE-2; i0+=2)
{
oldpara = FlashRdByte(i0);
ComWrite(&oldpara, 1);
if(oldpara == PARA_EMPTY)
{
break;
}
if(oldpara == PARA_ERASHED)
{
continue;
}
else
{
FlashWrByte(i1, oldpara);
oldpara = FlashRdByte(i0+1);
i1 ++;
FlashWrByte(i1, oldpara);
i1 ++;
}
}
// 6, write head 1 of old sector to show erashed
FlashWrByte(i01-1, H_ERASED);
// 7, write head 1 of new sector to show using it now
FlashWrByte(i11-1, H_USING);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -