/* sii.c
*
* 2013 Frank Jeschke <fjeschke@synapticon.de>
*/
#include "sii.h"
#include "crc8.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define SKIP_DC 0x0001
#define SKIP_TXPDO 0x0010
#define SKIP_RXPDO 0x0020
/* category functions */
static struct _sii_cat *cat_new(uint16_t type, uint16_t size);
static void cat_data_cleanup(struct _sii_cat *cat);
static int cat_add(SiiInfo *sii, struct _sii_cat *new);
static int cat_rm(SiiInfo *sii);
static struct _sii_cat * cat_next(SiiInfo *sii);
static void cat_rewind(SiiInfo *sii);
static void cat_print(struct _sii_cat *cats);
static void cat_print_strings(struct _sii_cat *cat);
static void cat_print_datatypes(struct _sii_cat *cat);
static void cat_print_general(struct _sii_cat *cat);
static void cat_print_fmmu(struct _sii_cat *cat);
static void cat_print_syncm(struct _sii_cat *cat);
static void cat_print_rxpdo(struct _sii_cat *cat);
static void cat_print_txpdo(struct _sii_cat *cat);
static void cat_print_pdo(struct _sii_cat *cat);
static void cat_print_dc(struct _sii_cat *cat);
static uint16_t sii_cat_write_strings(struct _sii_cat *cat, unsigned char *buf);
static uint16_t sii_cat_write_datatypes(struct _sii_cat *cat, unsigned char *buf);
static uint16_t sii_cat_write_general(struct _sii_cat *cat, unsigned char *buf);
static uint16_t sii_cat_write_fmmu(struct _sii_cat *cat, unsigned char *buf);
static uint16_t sii_cat_write_syncm(struct _sii_cat *cat, unsigned char *buf);
//static void sii_cat_write_rxpdo(struct _sii_cat *cat);
//static void sii_cat_write_txpdo(struct _sii_cat *cat);
static uint16_t sii_cat_write_pdo(struct _sii_cat *cat, unsigned char *buf);
static uint16_t sii_cat_write_dc(struct _sii_cat *cat, unsigned char *buf);
#if DEBUG == 1
static uint16_t sii_cat_write_cat(struct _sii_cat *cat, unsigned char *buf);
#endif
static size_t sii_cat_write(struct _sii *sii, uint16_t skip_mask);
static void sii_write(SiiInfo *sii, unsigned int add_pdo_mapping);
static int read_eeprom(FILE *f, unsigned char *buffer, size_t size)
{
size_t count = 0;
int input;
while ((input=fgetc(f)) != EOF) {
if (count >= size) {
fprintf(stderr, "Error, buffer too small\n");
return -1;
}
buffer[count++] = (unsigned char)(input&0xff);
}
return count;
}
static struct _sii_preamble * parse_preamble(const unsigned char *buffer, size_t size)
{
struct _sii_preamble *preamble = calloc(1, sizeof(struct _sii_preamble));
size_t count = 0;
uint8_t crc = 0xff; /* init value for crc */
/* pdi control */
preamble->pdi_ctrl = ((int)(buffer[1]&0xff)<<8) | buffer[0];
crc8byte(&crc, buffer[0]);
crc8byte(&crc, buffer[1]);
/* pdi config */
preamble->pdi_conf = ((int)(buffer[3]&0xff)<<8) | buffer[2];
crc8byte(&crc, buffer[2]);
crc8byte(&crc, buffer[3]);
count = 4;
/* sync impulse len (multiples of 10ns), stored as nano seconds */
preamble->sync_impulse = BYTES_TO_WORD(buffer[count], buffer[count+1]);
crc8byte(&crc, buffer[count]);
crc8byte(&crc, buffer[count+1]);
count+=2;
/* PDI config 2 */
preamble->pdi_conf2 = BYTES_TO_WORD(buffer[count], buffer[count+1]);
crc8byte(&crc, buffer[count]);
crc8byte(&crc, buffer[count+1]);
count+=2;
/* configured station alias */
preamble->alias = BYTES_TO_WORD(buffer[count], buffer[count+1]);
crc8byte(&crc, buffer[count]);
crc8byte(&crc, buffer[count+1]);
count+=2;
crc8byte(&crc, buffer[count]);
crc8byte(&crc, buffer[count+1]);
crc8byte(&crc, buffer[count+2]);
crc8byte(&crc, buffer[count+3]);
count+=4; /* next 4 bytes are reserved */
/* checksum test */
preamble->checksum = BYTES_TO_WORD(buffer[count], buffer[count+1]);
crc8byte(&crc, buffer[count]);
crc8byte(&crc, buffer[count+1]);
count += 2;
if (size != count)
printf("%s: Warning counter differs from size\n", __func__);
preamble->checksum_ok = 1;
if (crc != 0) {
preamble->checksum_ok = 0;
fprintf(stderr, "Error, checksum is not correct!\n");
}
return preamble;
}
static struct _sii_stdconfig *parse_stdconfig(const unsigned char *buffer, size_t size)
{
size_t count =0;
const unsigned char *b = buffer;
struct _sii_stdconfig *stdc = calloc(1, sizeof(struct _sii_stdconfig));
stdc->vendor_id = BYTES_TO_DWORD(*(b+0), *(b+1), *(b+2), *(b+3));
b+=4;
stdc->product_id = BYTES_TO_DWORD(*(b+0), *(b+1), *(b+2), *(b+3));
b+=4;
stdc->revision_id = BYTES_TO_DWORD(*(b+0), *(b+1), *(b+2), *(b+3));
b+=4;
stdc->serial = BYTES_TO_DWORD(*(b+0), *(b+1), *(b+2), *(b+3));
b+=4;
b+=8; /* another reserved 8 bytes */
stdc->bs_rec_mbox_offset = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->bs_rec_mbox_size = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->bs_snd_mbox_offset = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->bs_snd_mbox_size = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->std_rec_mbox_offset = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->std_rec_mbox_size = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->std_snd_mbox_offset = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->std_snd_mbox_size = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->mailbox_protocol.word = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
b+=66; /* more reserved bytes */
stdc->eeprom_size = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
stdc->version = BYTES_TO_WORD(*(b+0), *(b+1));
b+=2;
count = b-buffer;
if (size != count)
printf("%s: Warning counter differs from size\n", __func__);
return stdc;
}
static struct _string *string_new(const char *string, size_t size)
{
struct _string *new = calloc(1, sizeof(struct _string));
new->length = size;
new->data = malloc(size+1);
memmove(new->data, string, size);
new->data[size] = 0;
return new;
}
static void strings_entry_add(struct _sii_strings *str, struct _string *new)
{
if (str->head == NULL) { /* first entry */
str->head = new;
new->id = 1;
} else {
struct _string *s = str->head;
while (s->next)
s = s->next;
s->next = new;
new->prev = s;
new->id = s->id+1;
}
str->count += 1;
str->size += new->length;
}
static struct _sii_strings *parse_string_section(const unsigned char *buffer, size_t size)
{
const unsigned char *pos = buffer;
unsigned index = 0;
char str[1024];
size_t len = 0;
memset(str, '\0', 1024);
struct _sii_strings *strings = (struct _sii_strings *)calloc(1, sizeof(struct _sii_strings));
int stringcount = *pos++;
int counter = 0;
for (index=0; counter < stringcount; index++) {
len = *pos++;
memmove(str, pos, len);
pos += len;
strings_entry_add(strings, string_new(str, len));
counter++;
}
if ((size_t)(pos-buffer) > size)
printf("%s: Warning counter differs from size\n", __func__);
return strings;
}
static void parse_datatype_section(const unsigned char *buffer, size_t size)
{
printf("\n+++ parsing of datatype section not yet implemented (first byte: 0x%.2x, size: %zu)\n",
*buffer, size);
}
static char *physport_type(uint8_t b)
{
switch (b) {
case 0x00:
return "not used";
case 0x01:
return "MII";
case 0x02:
return "reserved";
case 0x03:
return "EBUS";
default:
return "unknown";
}
return NULL;
}
static struct _sii_general *parse_general_section(const unsigned char *buffer, size_t size)
{
const unsigned char *b = buffer;
struct _sii_general *siig = calloc(1, sizeof(struct _sii_general));
siig->groupindex = *b;
b++;
siig->imageindex = *b;
b++;
siig->orderindex = *b;
b++;
siig->nameindex = *b;
b++;
b++;
siig->coe_enable_sdo = (*b&0x01) == 0 ? 0 : 1;
siig->coe_enable_sdo_info = (*b&0x02) == 0 ? 0 : 1;
siig->coe_enable_pdo_assign = (*b&0x04) == 0 ? 0 : 1;
siig->coe_enable_pdo_conf = (*b&0x08) == 0 ? 0 : 1;
siig->coe_enable_upload_start = (*b&0x10) == 0 ? 0 : 1;
siig->coe_enable_sdo_complete = (*b&0x20) == 0 ? 0 : 1;
b++;
siig->foe_enabled = (*b & 0x01) == 0 ? 0 : 1;
b++;
siig->eoe_enabled = (*b & 0x01) == 0 ? 0 : 1;
b++;
b+=3; /* SoE Channels, DS402 Channels, Sysman Class - reserved */
siig->flag_safe_op = (*b & 0
没有合适的资源?快使用搜索试试~ 我知道了~
sii-tool-master.zip
共26个文件
c:5个
xsd:4个
h:4个
需积分: 0 0 下载量 172 浏览量
2024-01-25
23:28:55
上传
评论
收藏 70KB ZIP 举报
温馨提示
sii_tool,配合笔记使用,为防无法下载,在此做一个备份,此外,感谢synapticon公司为开源做出的贡献,详细资源可以访问https://github.com/synapticon/siitool。
资源推荐
资源详情
资源评论
收起资源包目录
siitool-master.zip (26个子文件)
siitool-master
esifile.c 912B
sii.h 8KB
esi.h 577B
doc
index.rst 3KB
crc8.h 286B
Makefile 2KB
README.rst 1002B
LICENSE 2KB
crc8.c 2KB
examples
Somanet_CtrlProto.xml 67KB
Somanet_CiA402-multi.xml 344KB
README 655B
Somanet_CiA402-single.xml 26KB
sii_example.bin 472B
EtherCATInfo.xsd 53KB
EtherCATBase.xsd 29KB
xmlvalidate.sh 219B
EtherCATDict.xsd 793B
EtherCATDiag.xsd 485B
Changelog 2KB
siitool.1 3KB
sii.c 56KB
esi.c 29KB
main.c 9KB
esifile.h 244B
scripts
getversion 338B
共 26 条
- 1
资源评论
Primary_student
- 粉丝: 1248
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功