|
本帖最后由 天天爱玛丽 于 2024-3-20 16:47 编辑
用于在电脑程序中调试带EEPROM的嵌入式程序,实现的虚拟芯片,方便脱离硬件验证程序,
有需要的也可以随便用,估计玩的不会太多,建议linux上调程序用,windows的只读属性有点烦人.
虽然代码中有我自己的数据格式,但是改成需要的格式,对于码农没难度.
xprintf是硬件平台的prinf函数,用prinf就行了,
属于自己备忘,若您感兴趣,欢迎交流.
//24CXX, 1 page = 64 byte, 需16字节对齐
typedef struct
{
char company[CF_STRING_SZ];
uint16_t count; //记录条数
uint16_t max; //最大条数
uint16_t lcdsw; //是否启用LCDGUI
uint16_t magic;
uint16_t reserved[4];
}cfhead_t;
cfhead_t cfhead;
#define POWER_MAX 100
#define LASER_MAX 100
typedef struct
{
uint16_t magic;
uint16_t squeue;
uint8_t power, workmode, wavetype, reserved[5];
uint16_t zhongpin;
uint16_t tiaofu;
uint16_t interval_keep;
uint16_t interval_pause;
uint16_t surge_up, surge_keep, surge_down, surge_pause;
uint16_t time;
uint16_t eindex;
char name[CF_STRING_SZ];
char treat_time_str[CF_STRING_SZ];
char treat_freq_str[CF_STRING_SZ];
char treat_effect[CF_STRING_SZ];
}cfitem_t;
//quick sort
static void cflist_index_quick_sort(cfitem_t s[], int l, int h)
{
if (l < h)
{
//Swap(s[l], s[(l + r) / 2]);
int i = l, j = h;
cfitem_t x = s[l];
while (i < j)
{
while (i < j && s[j].squeue >= x.squeue)
j--;
if (i < j)
s[i++] = s[j];
while (i < j && s.squeue < x.squeue)
i++;
if (i < j)
s[j--] = s;
}
s = x;
cflist_index_quick_sort(s, l, i - 1);
cflist_index_quick_sort(s, i + 1, h);
}
}
void ee_default_when_non_exist(void)
{
int fd = open(eeprom, O_RDONLY);
if (fd < 3)
{
//windwos, after creation, manually remove read-only flag.
fd = open(eeprom, O_RDWR | O_CREAT | O_BINARY, 0777);
if (fd >= 3)
{
char buf[1024];
memset(buf, 0xFF, 1024);
int count = GX_COUNT(chufang_preset);
for (int i = 0; i < count; i++)
{
write(fd, buf, CF_ITEM_SZ);
}
close(fd);
}
} else {
close(fd);
}
}
uint16_t ee_read_magic(int fd, uint16_t index)
{
uint16_t magic = 0;
off_t offset = CFLIST_ADDR + CF_MAGIC_OFS + index * CF_ITEM_SZ;
lseek(fd, offset, SEEK_SET);
read(fd, &magic, 2);
return magic;
}
uint16_t ee_read_squeue(int fd, uint16_t index)
{
uint16_t squeue = 0;
off_t offset = CFLIST_ADDR + CF_SQUEUE_OFS + index * CF_ITEM_SZ;
lseek(fd, offset, SEEK_SET);
read(fd, &squeue, 2);
return squeue;
}
void ee_write_squeue(int fd, uint16_t index, uint16_t squ)
{
off_t offset = CFLIST_ADDR + CF_SQUEUE_OFS + index * CF_ITEM_SZ;
lseek(fd, offset, SEEK_SET);
write(fd, &squ, 2);
}
uint16_t ee_get_freeed_index(void)
{
uint16_t index = 0xFFFF;
int fd = open(eeprom, O_RDONLY);
if (fd < 3) return index;
/* get a free index in cflist */
for (int i = 0; i<CHUFANG_MAX; i++)
{
if (ee_read_magic(fd, i) != MEDI_MAGIC) {
index = i;
xprintf("freeed index %d\n", index);
break;
}
}
close(fd);
return index;
}
/*
* cfhead_t header, 64B
* cfitem_t cflist[512]; 64B per item
* others
*/
void ee_read_cflist(void)
{
cfhead_t header;
cfitem_t item;
int fd = 0;
int cnt = 0;
#if 1
ee_default_when_non_exist();
#endif
fd = open(eeprom, O_RDWR|O_BINARY);
if (fd < 3) {
xprintf("open eeprom failed\n");
return;
}
lseek(fd, 0, SEEK_SET);
read(fd, &header, CF_HEAD_SZ);//read header
#if DEBUG_EEPROM==1
xprintf("eeprom read header at %04X:\n", 0x0000);
for (int i = 0; i<CF_ITEM_SZ; i++) {
xprintf("%02X ", *(((uint8_t*)&header) + i));
if (i>0 && i % 16 == 15) xprintf("\n");
}
xprintf("\n");
#endif
if (header.magic == MEDI_MAGIC)
{
memset(&cfhead, 0, CF_HEAD_SZ);
cfhead = header;
xprintf("read chufang header. headsz:%d\n", CF_HEAD_SZ);
xprintf("read chufang list. itemsz:%d\n", CF_ITEM_SZ);
for (int i = 0; i<CHUFANG_MAX; i++)
{
if (ee_read_magic(fd, i) == MEDI_MAGIC)
{
ee_read_cfitem(i, &item);
item.eindex = i;
cflist[cnt++] = item;
#if DEBUG_EEPROM==1 //DEBUG
xprintf("readin item(%d).power = %d\n", i, item.power);
xprintf(" item(%d).mode = %d\n", i, item.mode);
xprintf(" item(%d).zhongpin = %d\n", i, item.zhongpin);
xprintf(" item(%d).tiaofu = %d\n", i, item.tiaofu);
xprintf(" item(%d).interval_keep = %d\n", i, item.interval_keep);
xprintf(" item(%d).interval_pause = %d\n", i, item.interval_pause);
xprintf(" item(%d).surge_up = %d\n", i, item.surge_up);
xprintf(" item(%d).surge_keep = %d\n", i, item.surge_keep);
xprintf(" item(%d).surge_down = %d\n", i, item.surge_down);
xprintf(" item(%d).surge_pause = %d\n", i, item.surge_pause);
xprintf(" item(%d).time = %d\n", i, item.time);
xprintf(" item(%d).eindex = %d\n", i, item.eindex);
xprintf(" item(%d).squeue = %d\n", i, item.squeue);
xprintf("\n");
#endif
#if DEBUG_EEPROM==1
xprintf("eeprom %d sequeue=%d\n", i, item.squeue);
#endif
}
}
cfhead.count = cnt; /* use to query chufang list */
//quick sort by index
xprintf("cflist quick sort by squeue\n");
cflist_index_quick_sort(cflist, 0, cnt - 1);
close(fd);
}
else
{ //new eeprom, setup a default chufang list
cnt = GX_COUNT(chufang_preset);
xprintf("new eeprom. headsz:%d, cnt:%d\n", CF_HEAD_SZ, cnt);
memset(&header, 0, CF_HEAD_SZ);
sprintf(header.company, "%s", "1234567890ABCDEFG");
header.count = cnt;
header.max = CHUFANG_MAX;
header.magic = MEDI_MAGIC;
lseek(fd, 0, SEEK_SET);
write(fd, &header, CF_HEAD_SZ);
#if DEBUG_EEPROM==1
xprintf("preset chufang header.\n");
for (int i = 0; i<headsz; i++) {
xprintf("%02X ", *(((uint8_t*)&header) + i));
if (i>0 && i % 16 == 15) xprintf("\n");
}
xprintf("\n");
#endif
//use preset chufang list
xprintf("preset chufang list. itemsz:%d\n", CF_ITEM_SZ);
for (int i = 0; i<cnt; i++)
{
item = chufang_preset;
item.eindex = i;
item.squeue = i;
item.magic = MEDI_MAGIC;
#if DEBUG_EEPROM==1 //DEBUG
xprintf("preset item(%d).power = %d\n", i, item.power);
xprintf(" item(%d).mode = %d\n", i, item.mode);
xprintf(" item(%d).zhongpin = %d\n", i, item.zhongpin);
xprintf(" item(%d).tiaofu = %d\n", i, item.tiaofu);
xprintf(" item(%d).interval_keep = %d\n", i, item.interval_keep);
xprintf(" item(%d).interval_pause = %d\n", i, item.interval_pause);
xprintf(" item(%d).surge_up = %d\n", i, item.surge_up);
xprintf(" item(%d).surge_keep = %d\n", i, item.surge_keep);
xprintf(" item(%d).surge_down = %d\n", i, item.surge_down);
xprintf(" item(%d).surge_pause = %d\n", i, item.surge_pause);
xprintf(" item(%d).time = %d\n", i, item.time);
xprintf("\n");
#endif
write(fd, &item, CF_ITEM_SZ);
cflist = item;
}
close(fd);
system("sync");
}
}
void ee_mark_invalid(uint16_t index)
{
int fd = open(eeprom, O_RDWR | O_BINARY);
if (fd < 3) {
xprintf("open eeprom failed\n");
return;
}
xprintf("eeprom mark %d as invalid\n", index);
//off_t offset = CFLIST_ADDR + index * CF_ITEM_SZ;
off_t offset = CFLIST_ADDR + CF_MAGIC_OFS + index * CF_ITEM_SZ;
lseek(fd, offset, SEEK_SET);
cfitem_t item;
memset(&item, 0xFF, CF_ITEM_SZ);
write(fd, &item, CF_ITEM_SZ);
close(fd);
}
void ee_read_cfitem(uint16_t index, cfitem_t *item)
{
int fd = open(eeprom, O_RDONLY);
if (fd < 3)
return;
off_t offset = CFLIST_ADDR + index * CF_ITEM_SZ;
lseek(fd, offset, SEEK_SET);
read(fd, item, CF_ITEM_SZ);
if (item->power > POWER_MAX)
item->power = POWER_MAX;
close(fd);
}
void ee_write_cfitem(uint16_t index, cfitem_t *item)
{
int fd = open(eeprom, O_RDWR | O_BINARY);
if (fd < 3) {
xprintf("open eeprom failed\n");
return;
}
off_t offset = CFLIST_ADDR + index * CF_ITEM_SZ;
lseek(fd, offset, SEEK_SET);
write(fd, item, CF_ITEM_SZ);
#if 0
xprintf("write at %d:\n", index);
for (int i = 0; i<CF_ITEM_SZ; i++) {
xprintf("%02X ", *(((uint8_t*)item) + i));
if (i>0 && i % 16 == 15) xprintf("\n");
}
xprintf("\n");
#endif
system("sync");
close(fd);
}
uint16_t cflist_get_squeue(uint16_t index)
{
listbox_t *lbox = chufang_lbox_get();
for (uint16_t i = 0; i < lbox->item_count; i++)
{
if (lbox->items.data == index) {
return i;
}
}
return 0xFFFF;
}
void ee_squeue_index_sync(void)
{
int fd = open(eeprom, O_RDWR | O_BINARY);
if (fd < 3) {
xprintf("open eeprom failed\n");
return;
}
xprintf("ee_squeue_index_sync\n");//DEBUG
for (uint16_t i = 0; i<CHUFANG_MAX; i++)
{
if (ee_read_magic(fd, i) == MEDI_MAGIC)
{
uint16_t squeue = cflist_get_squeue(i);
ee_write_squeue(fd, i, squeue);
}
}
close(fd);
}
|
评分
-
1
查看全部评分
-
|