📄 fls_jrnl.c
字号:
break;
}
else /* The rear pointer is not in this sector */
{
rm_jrnl_addr = Get_Next_Logic_Sector(rm_jrnl_addr);
}
}
if(i != 3) /* Note: When the i == 3, indicate the FLASH is wrong, will not occur this case */
Rm_Jrnl_Loc(rm_jrnl_addr);
}
/*--------------------------------------*/
}
/*
Write the active log flag byte sequence
The active log legal order is:
55 AA 55 AA 55 AA 55 AA 55 AA 55 AA 55 AA 55 AA.(16 bytes)
*/
void Atv_Log_Write(void)
{
byte *jrnl_atv_addr = (byte *)JRNL_ATV_ADDR;
xtr_Fls_WriteStr(jrnl_atv_addr, log_legal_order, 16);
Delay(100); /* Delay 100ms */
}
/* Initialize the sale journal */
void Init_Sale_Jrnl(void)
{
byte *sale_jrnl_addr = (byte *)SALE_JRNL_START;
Fls_SectorErase(2-1);
Fls_SectorErase(3-1);
Fls_SectorErase(4-1);
Max_Sale_Jrnl_No = DFT_SALE_JRNL_CNT;
sale_jrnl = (TRANSBUFFDEF *)(&sale_jrnl_addr[LOG_START_POSI]);
sale_jrnl_first_ptr = sale_jrnl_last_ptr = (byte *)sale_jrnl;
sale_jrnl_first = sale_jrnl_last = 0;
sale_jrnl_len = 0;
}
/* Initialize the discount journal */
void Init_Dc_Jrnl(void)
{
byte *dc_jrnl_addr = (byte *)DC_JRNL_START;
Fls_SectorErase(5-1);
Fls_SectorErase(6-1);
Max_Dc_Jrnl_No = DFT_DC_JRNL_CNT;
dc_jrnl = (TRANSBUFFDEF *)(&dc_jrnl_addr[LOG_START_POSI]);
dc_jrnl_first_ptr = dc_jrnl_last_ptr = (byte *)dc_jrnl;
dc_jrnl_first = dc_jrnl_last = 0;
dc_jrnl_len = 0;
}
/* Initialize the R.M. journal */
void Init_Rm_Jrnl(void)
{
byte *rm_jrnl_addr = (byte *)RM_JRNL_START;
Fls_SectorErase(7-1);
Fls_SectorErase(8-1);
Max_Rm_Jrnl_No = DFT_RM_JRNL_CNT;
rm_jrnl = (TRANSBUFFDEF *)(&rm_jrnl_addr[LOG_START_POSI]);
rm_jrnl_first_ptr = rm_jrnl_last_ptr = (byte *)rm_jrnl;
rm_jrnl_first = rm_jrnl_last = 0;
rm_jrnl_len = 0;
}
/*
Initial the journal paramter.
flag:
0: The abnormal initial
1: The normal initial
*/
void Init_Jrnl_Para(byte flag)
{
if(flag) /* Normal initial */
{
Init_Sale_Jrnl();
Init_Dc_Jrnl();
Init_Rm_Jrnl();
}
else /* Abnormal initial */
{
Max_Sale_Jrnl_No = 0;
sale_jrnl = NULL;
sale_jrnl_first_ptr = sale_jrnl_last_ptr = NULL;
sale_jrnl_first = sale_jrnl_last = 0;
sale_jrnl_len = 0;
Max_Dc_Jrnl_No = 0;
dc_jrnl = NULL;
dc_jrnl_first_ptr = dc_jrnl_last_ptr = NULL;
dc_jrnl_first = dc_jrnl_last = 0;
dc_jrnl_len = 0;
Max_Rm_Jrnl_No = 0;
rm_jrnl = NULL;
rm_jrnl_first_ptr = rm_jrnl_last_ptr = NULL;
rm_jrnl_first = rm_jrnl_last = 0;
rm_jrnl_len = 0;
}
}
/* Get the journal head's content */
ELECJRNLHEADDEF *get_jrnl_head(ELECJRNLHEADDEF *elec_jrnl_head)
{
fls_head_jrnl.ID = (elec_jrnl_head->ID);
fls_head_jrnl.receipt_no = (elec_jrnl_head->receipt_no);
fls_head_jrnl.clerk_no = (elec_jrnl_head->clerk_no);
fls_head_jrnl.table_no = (elec_jrnl_head->table_no);
fls_head_jrnl.year = (elec_jrnl_head->year);
fls_head_jrnl.month = (elec_jrnl_head->month);
fls_head_jrnl.day = (elec_jrnl_head->day);
fls_head_jrnl.hour = (elec_jrnl_head->hour);
fls_head_jrnl.minute = (elec_jrnl_head->minute);
fls_head_jrnl.length = (elec_jrnl_head->length);
return (&fls_head_jrnl);
}
/* Erase the sector by the address */
void fls_erase_sector(byte *elec_jrnl_ptr)
{
if(elec_jrnl_ptr == (byte *)FLS_SCT02_ADDR)
Fls_SectorErase(2-1);
else if(elec_jrnl_ptr == (byte *)FLS_SCT03_ADDR)
Fls_SectorErase(3-1);
else if(elec_jrnl_ptr == (byte *)FLS_SCT04_ADDR)
Fls_SectorErase(4-1);
else if(elec_jrnl_ptr == (byte *)FLS_SCT05_ADDR)
Fls_SectorErase(5-1);
else if(elec_jrnl_ptr == (byte *)FLS_SCT06_ADDR)
Fls_SectorErase(6-1);
else if(elec_jrnl_ptr == (byte *)FLS_SCT07_ADDR)
Fls_SectorErase(7-1);
else if(elec_jrnl_ptr == (byte *)FLS_SCT08_ADDR)
Fls_SectorErase(8-1);
else
;// Do nothing
xtr_Fls_WriteByte(&elec_jrnl_ptr[JRNL_ERS_POSI], 0x55);
}
/* Copy the waste journal to the new area */
byte *elec_jrnl_cpy(byte *dst, byte *src, byte len)
{
dword tmp;
tmp = len;
tmp *= sizeof(TRANSBUFFDEF);
xtr_Fls_WriteStr(dst, src, tmp);
return (dst+tmp);
}
/* Store the sale item in flash memory */
void fls_store_sale_item(byte *tsbf, byte inc)
{
byte length;
byte *sct_start_addr;
byte *tmp_ptr;
static ELECJRNLHEADDEF tmp_jrnl_head; /* The temporary journal */
sct_start_addr = (byte *)(FLS_BASE_ADDR + ((dword)sale_jrnl_last_ptr & 0xF0000));
if(store_head_step == 0)
{
tmp_ptr = sale_jrnl_last_ptr + (dword)(inc-1)*sizeof(TRANSBUFFDEF);
if((dword)(tmp_ptr+sizeof(TRANSBUFFDEF)-sct_start_addr) >= EACH_SCT_SIZE) /* More than a sector size */
{
xtr_Fls_WriteByte(&sct_start_addr[WASTE_SP_POSI], 0x55); /* Has waste some unit in the rear cell */
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI], (dword)sale_jrnl_last_ptr);
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI+1], ((dword)sale_jrnl_last_ptr>>8));
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI+2], ((dword)sale_jrnl_last_ptr)>>16);
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI+3], ((dword)sale_jrnl_last_ptr)>>24);
sct_start_addr = Get_Next_Logic_Sector(sct_start_addr);
if(sct_start_addr[STORE_LOG_POSI] != 0xFF) /* Has ever store the journal */
fls_erase_sector(sct_start_addr);
xtr_Fls_WriteByte(&sct_start_addr[STORE_LOG_POSI], 0x55); /* Ever store the journal flag */
elec_jrnl_cpy(&sct_start_addr[LOG_START_POSI], sale_jrnl_last_ptr, inc-1); /* Copy the waste journal to the new area */
sale_jrnl_last_ptr = &sct_start_addr[LOG_START_POSI];
tmp_ptr = sale_jrnl_last_ptr + (dword)(inc-1)*sizeof(TRANSBUFFDEF);
}
xtr_Fls_WriteStr(tmp_ptr, tsbf, sizeof(TRANSBUFFDEF));
}
else if((store_head_step == 1) || (store_head_step == 2)) /* The head store operation */
{
if(sale_jrnl_len)
{
length = ((ELECJRNLHEADDEF *)sale_jrnl_last_ptr)->length;
sale_jrnl_last_ptr = Get_Next_Head(sale_jrnl_last_ptr, length);
if(((dword)sale_jrnl_last_ptr-(dword)sct_start_addr) >= EACH_SCT_SIZE)
{
sct_start_addr = Get_Next_Logic_Sector(sct_start_addr);
if(sct_start_addr[STORE_LOG_POSI] != 0xFF) /* Has ever store the journal */
fls_erase_sector(sct_start_addr);
xtr_Fls_WriteByte(&sct_start_addr[STORE_LOG_POSI], 0x55); /* Ever store the journal flag */
}
}
else
{
xtr_Fls_WriteByte(&sct_start_addr[STORE_LOG_POSI], 0x55); /* Ever store the journal flag */
}
memcpy(&tmp_jrnl_head, tsbf, sizeof(ELECJRNLHEADDEF));
if(store_head_step == 1)
{
xtr_Fls_WriteStr(sale_jrnl_last_ptr, (byte *)&tmp_jrnl_head, sizeof(ELECJRNLHEADDEF));
}
}
else // store_head_step == 3 /* Can store the length, and store the data in flash */
{
tmp_jrnl_head.length = inc;
xtr_Fls_WriteStr(sale_jrnl_last_ptr, (byte *)&tmp_jrnl_head, sizeof(ELECJRNLHEADDEF));
}
if(store_head_step != 3)
{
sale_jrnl_last ++;
sale_jrnl_len ++;
if(sale_jrnl_len > Max_Sale_Jrnl_No) /* The length is over */
{
length = ((ELECJRNLHEADDEF *)sale_jrnl)->length;
sale_jrnl_first += length;
sale_jrnl_len -= length;
sct_start_addr = (byte *)(FLS_BASE_ADDR + ((dword)sale_jrnl_first_ptr & 0xF0000));
sale_jrnl_first_ptr = Get_Next_Head(sale_jrnl_first_ptr, length);
sale_jrnl = (TRANSBUFFDEF *)sale_jrnl_first_ptr;
if(((dword)sale_jrnl_first_ptr-(dword)sct_start_addr) >= EACH_SCT_SIZE)
{
xtr_Fls_WriteByte(&sct_start_addr[LOG_ATV_POSI], 0x55); /* The journal is not active in this factor */
xtr_Fls_WriteByte(&sct_start_addr[REAR_PTR_POSI], 0x55); /* The rear pointer is not in this factor */
sct_start_addr = (byte *)(FLS_BASE_ADDR + ((dword)sale_jrnl_first_ptr & 0xF0000));
if(sct_start_addr[JRNL_ERS_POSI] == 0x55) /* Has ever erase the flash */
xtr_Fls_WriteByte(&sct_start_addr[REAR_PTR2_POSI], 0x55); /* The rear pointer is not in this factor */
}
}
}
}
/* Store the discount item in flash memory */
void fls_store_dc_item(byte *tsbf, byte inc)
{
byte length;
byte *sct_start_addr;
byte *tmp_ptr;
static ELECJRNLHEADDEF tmp_jrnl_head; /* The temporary journal */
sct_start_addr = (byte *)(FLS_BASE_ADDR + ((dword)dc_jrnl_last_ptr & 0xF0000));
if(store_head_step == 0)
{
tmp_ptr = dc_jrnl_last_ptr + (dword)(inc-1)*sizeof(TRANSBUFFDEF);
if((dword)(tmp_ptr+sizeof(TRANSBUFFDEF)-sct_start_addr) >= EACH_SCT_SIZE) /* More than a sector size */
{
xtr_Fls_WriteByte(&sct_start_addr[WASTE_SP_POSI], 0x55); /* Has waste some unit in the rear cell */
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI], (dword)dc_jrnl_last_ptr);
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI+1], ((dword)dc_jrnl_last_ptr>>8));
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI+2], ((dword)dc_jrnl_last_ptr)>>16);
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI+3], ((dword)dc_jrnl_last_ptr)>>24);
sct_start_addr = Get_Next_Logic_Sector(sct_start_addr);
if(sct_start_addr[STORE_LOG_POSI] != 0xFF) /* Has ever store the journal */
fls_erase_sector(sct_start_addr);
xtr_Fls_WriteByte(&sct_start_addr[STORE_LOG_POSI], 0x55); /* Ever store the journal flag */
elec_jrnl_cpy(&sct_start_addr[LOG_START_POSI], dc_jrnl_last_ptr, inc-1); /* Copy the waste journal to the new area */
dc_jrnl_last_ptr = &sct_start_addr[LOG_START_POSI];
tmp_ptr = dc_jrnl_last_ptr + (dword)(inc-1)*sizeof(TRANSBUFFDEF);
}
xtr_Fls_WriteStr(tmp_ptr, tsbf, sizeof(TRANSBUFFDEF));
}
else if((store_head_step == 1) || (store_head_step == 2)) /* The head store operation */
{
if(dc_jrnl_len)
{
length = ((ELECJRNLHEADDEF *)dc_jrnl_last_ptr)->length;
dc_jrnl_last_ptr = Get_Next_Head(dc_jrnl_last_ptr, length);
if(((dword)dc_jrnl_last_ptr-(dword)sct_start_addr) >= EACH_SCT_SIZE)
{
sct_start_addr = Get_Next_Logic_Sector(sct_start_addr);
if(sct_start_addr[STORE_LOG_POSI] != 0xFF) /* Has ever store the journal */
fls_erase_sector(sct_start_addr);
xtr_Fls_WriteByte(&sct_start_addr[STORE_LOG_POSI], 0x55); /* Ever store the journal flag */
}
}
else
{
xtr_Fls_WriteByte(&sct_start_addr[STORE_LOG_POSI], 0x55); /* Ever store the journal flag */
}
memcpy(&tmp_jrnl_head, tsbf, sizeof(ELECJRNLHEADDEF));
if(store_head_step == 1)
{
xtr_Fls_WriteStr(dc_jrnl_last_ptr, (byte *)&tmp_jrnl_head, sizeof(ELECJRNLHEADDEF));
}
}
else // store_head_step == 3 /* Can store the length, and store the data in flash */
{
tmp_jrnl_head.length = inc;
xtr_Fls_WriteStr(dc_jrnl_last_ptr, (byte *)&tmp_jrnl_head, sizeof(ELECJRNLHEADDEF));
}
if(store_head_step != 3)
{
dc_jrnl_last ++;
dc_jrnl_len ++;
if(dc_jrnl_len > Max_Dc_Jrnl_No) /* The length is over */
{
length = ((ELECJRNLHEADDEF *)dc_jrnl)->length;
dc_jrnl_first += length;
dc_jrnl_len -= length;
sct_start_addr = (byte *)(FLS_BASE_ADDR + ((dword)dc_jrnl_first_ptr & 0xF0000));
dc_jrnl_first_ptr = Get_Next_Head(dc_jrnl_first_ptr, length);
dc_jrnl = (TRANSBUFFDEF *)dc_jrnl_first_ptr;
if(((dword)dc_jrnl_first_ptr-(dword)sct_start_addr) >= EACH_SCT_SIZE)
{
xtr_Fls_WriteByte(&sct_start_addr[LOG_ATV_POSI], 0x55); /* The journal is not active in this factor */
xtr_Fls_WriteByte(&sct_start_addr[REAR_PTR_POSI], 0x55); /* The rear pointer is not in this factor */
sct_start_addr = (byte *)(FLS_BASE_ADDR + ((dword)dc_jrnl_first_ptr & 0xF0000));
if(sct_start_addr[JRNL_ERS_POSI] == 0x55) /* Has ever erase the flash */
xtr_Fls_WriteByte(&sct_start_addr[REAR_PTR2_POSI], 0x55); /* The rear pointer is not in this factor */
}
}
}
}
/* Store the R.M. item in flash memory */
void fls_store_rm_item(byte *tsbf, byte inc)
{
byte length;
byte *sct_start_addr;
byte *tmp_ptr;
static ELECJRNLHEADDEF tmp_jrnl_head; /* The temporary journal */
sct_start_addr = (byte *)(FLS_BASE_ADDR + ((dword)rm_jrnl_last_ptr & 0xF0000));
if(store_head_step == 0)
{
tmp_ptr = rm_jrnl_last_ptr + (dword)(inc-1)*sizeof(TRANSBUFFDEF);
if((dword)(tmp_ptr+sizeof(TRANSBUFFDEF)-sct_start_addr) >= EACH_SCT_SIZE) /* More than a sector size */
{
xtr_Fls_WriteByte(&sct_start_addr[WASTE_SP_POSI], 0x55); /* Has waste some unit in the rear cell */
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI], (dword)rm_jrnl_last_ptr);
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI+1], ((dword)rm_jrnl_last_ptr>>8));
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI+2], ((dword)rm_jrnl_last_ptr)>>16);
xtr_Fls_WriteByte(&sct_start_addr[WASTE_ADDR_POSI+3], ((dword)rm_jrnl_last_ptr)>>24);
sct_start_addr = Get_Next_Logic_Sector(sct_start_addr);
if(sct_start_addr[STORE_LOG_POSI] != 0xFF) /* Has ever store the journal */
fls_erase_sector(sct_start_addr);
xtr_Fls_WriteByte(&sct_start_addr[STORE_LOG_POSI], 0x55); /* Ever store the journal flag */
elec_jrnl_cpy(&sct_start_addr[LOG_START_POSI], rm_jrnl_last_ptr, inc-1); /* Copy the waste journal to the new area */
rm_jrnl_last_ptr = &sct_start_addr[LOG_START_POSI];
tmp_ptr = rm_jrnl_last_ptr + (dword)(inc-1)*sizeof(TRANSBUFFDEF);
}
xtr_Fls_WriteStr(tmp_ptr, tsbf, sizeof(TRANSBUFFDEF));
}
else if((store_head_step == 1) || (store_head_step == 2)) /* The head store operation */
{
if(rm_jrnl_len)
{
length = ((ELECJRNLHEADDEF *)rm_jrnl_last_ptr)->length;
rm_jrnl_last_ptr = Get_Next_Head(rm_jrnl_last_ptr, length);
if(((dword)rm_jrnl_last_ptr-(dword)sct_start_addr) >= EACH_SCT_SIZE)
{
sct_start_addr = Get_Next_Logic_Sector(sct_start_addr);
if(sct_start_addr[STORE_LOG_POSI] != 0xFF) /* Has ever store the journal */
fls_erase_sector(sct_start_addr);
xtr_Fls_WriteByte(&sct_start_addr[STORE_LOG_POSI], 0x55); /* Ever store the journal flag */
}
}
else
{
xtr_Fls_WriteByte(&sct_start_addr[STORE_LOG_POSI], 0x55); /* Ever store the journal flag */
}
memcpy(&tmp_jrnl_head, tsbf, sizeof(ELECJRNLHEADDEF));
if(store_head_step == 1)
{
xtr_Fls_WriteStr(rm_jrnl_last_ptr, (byte *)&tmp_jrnl_head, sizeof(ELECJRNLHEADDEF));
}
}
else // store_head_step == 3 /* Can store the length, and store the data in flash */
{
tmp_jrnl_head.length = inc;
xtr_Fls_WriteStr(rm_jrnl_last_ptr, (byte *)&tmp_jrnl_head, sizeof(ELECJRNLHEADDEF));
}
if(store_head_step != 3)
{
rm_jrnl_last ++;
rm_jrnl_len ++;
if(rm_jrnl_len > Max_Rm_Jrnl_No) /* The length is over */
{
length = ((ELECJRNLHEADDEF *)rm_jrnl)->length;
rm_jrnl_first += length;
rm_jrnl_len -= length;
sct_start_addr = (byte *)(FLS_BASE_ADDR + ((dword)rm_jrnl_first_ptr & 0xF0000));
rm_jrnl_first_ptr = Get_Next_Head(rm_jrnl_first_ptr, length);
rm_jrnl = (TRANSBUFFDEF *)rm_jrnl_first_ptr;
if(((dword)rm_jrnl_first_ptr-(dword)sct_start_addr) >= EACH_SCT_SIZE)
{
xtr_Fls_WriteByte(&sct_start_addr[LOG_ATV_POSI], 0x55); /* The journal is not active in this factor */
xtr_Fls_WriteByte(&sct_start_addr[REAR_PTR_POSI], 0x55); /* The rear pointer is not in this factor */
sct_start_addr = (byte *)(FLS_BASE_ADDR + ((dword)rm_jrnl_first_ptr & 0xF0000));
if(sct_start_addr[JRNL_ERS_POSI] == 0x55) /* Has ever erase the flash */
xtr_Fls_WriteByte(&sct_start_addr[REAR_PTR2_POSI], 0x55); /* The rear pointer is not in this factor */
}
}
}
}
#endif /* End ELEC_JRNL */
/* Write one byte to the flash */
byte xtr_Fls_WriteByte(byte *dst, byte value)
{
if(value == 0xFF) /* No need to write it */
return (OK);
if(*dst != 0xFF) /* The destination is not empty */
{
bellcnt = 0xfe;
return (NG);
}
return(Fls_WriteByte(dst, value));
}
/* Write one string to the flash */
byte xtr_Fls_WriteStr(byte *dst, byte *src, word len)
{
for (; len > 0; len--)
{
if (xtr_Fls_WriteByte(dst, *src) != OK)
{
return NG;
}
dst++;
src++;
}
return (OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -