锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

Motorola S19(S-record)格式解析

时间:2022-09-15 09:30:00 s503热继电器

S-record格式文件是Freescale CodeWarrior编译器生成的后缀叫做.S19的程序文件是直接烧写的MCU的ASCII英文全称代码问题Motorola format for EEPROM programming。

1.格式

S-record 每行最大78字节,156个字符由5部分组成

Image srec_motorola_1.png

Stype 1byte 描述记录的类型((S0,S1,S2,S3,S5,S6,S7,S8,S9)。
Record Length 1byte 此行后续剩余数据 byte数,16进制
Address

2byte

3byte

4byte

记录数据地址信息,地址长度由载入地址byte数决定
Data 0-64byte 代表内存载入数据或者描述信息16进制数
CheckSum 1byte Record length 、address、data 16进制数据相加求和,只保留最低值byte 0xNN,CheckSum =0xFF - 0xNN

stype 描述
s0

Address未使用,零位置(0x数据场中的信息分为以下四个子域:

name(名称):编码单元名称的20个字符

ver(版本):用于编码版本号的两个字符

rev(修改版):用于编码修改版本号的两个字符

description(描述):0-36个字符用于编码文本注释

这次旅行表示程序的开始,不需要燃烧memory。

s1 Address 用2 byte 表示
s2 Address 用3 byte 表示
s3 Address 用4 byte 表示
s5 Address 用 2byte 表示,包括以前的 s1,s2,s3 没有记录的计数Data部分
s7 Address 用4byte表示包含了开始执行地址,没有Data 部分表示程序结束,无需烧写memory
s8 Address 用3byte表示包含了开始执行地址,没有Data 部分表示程序结束,无需烧写memory
s9 Address 用2byte表示包含了开始执行地址,没有Data 部分表示程序结束,无需烧写memory
s6 Address 用 3byte 表示,包括以前的 s1,s2,s3 没有记录的计数Data部分

2.实例

3.格式转换工具

http://srecord.sourceforge.net/

4.bin 转s19功能实现

#include  #include  #include  #include   #include "common.h"  #define HEADER1 "\nBIN2SREC " SREC_VER " - Convert binary to Motorola S-Record file.\n"  char *infilename; char *outfilename; FILE *infile, *outfile;  uint32_t addr_offset = 0; uint32_t begin_addr; uint32_t end_addr; uint32_t addr_bytes = 2; bool do_headers = true; bool verbose = true; uint32_t line_length = 32; char *head_char; bool have_header=false; uint8_t headhex[255];   /***************************************************************************/  void syntax(void) {  fprintf(stderr, HEADER1);  fprintf(stderr, HEADER2);  fprintf(stderr, "Syntax: BIN2SREC  INFILE > OUTFILE\n\n");  fprintf(stderr, "-help            Show this help.\n");  fprintf(stderr, "-b        Address to begin at in binary file (hex), default = 0.\n");  fprintf(stderr, "-e          Address to end at in binary file (hex), default = end of file.\n");  fprintf(stderr, "-o       Generated address offset (hex), default = begin address.\n");  fprintf(stderr, "-a     Number of bytes used for address (2-4),\n");  fprintf(stderr, "                  default = minimum needed for maximum address.\n");  fprintf(stderr, "-l   Number of bytes per line (8-32), default = 32.\n");  fprintf(stderr, "-s               Suppress header and footer records,if have -s ,not add header records.default  add header records\n");  fprintf(stderr, "-n               Add S0 note ,default none\n");  fprintf(stderr, "-q               Quiet mode - no output except S-Record.\n"); }  /**************************************************************************/

void process(void)
{
	int i;
	uint32_t max_addr, address;
	uint32_t byte_count, this_line;
	uint8_t checksum;
	uint32_t c;
	int record_count = 0;
	uint32_t head_count = 0;
	uint32_t head_checksum = 0;

	unsigned char buf[32];

	max_addr = addr_offset + (end_addr - begin_addr);

	fseek(infile, begin_addr, SEEK_SET);

	if ((max_addr > 0xffffl) && (addr_bytes < 3))
		addr_bytes = 3;

	if ((max_addr > 0xffffffl) && (addr_bytes < 4))
		addr_bytes = 4;

	if((outfile = fopen(outfilename, "wb")) != NULL)
	{

	}
	else
	{
		fprintf(stderr, "Can't create output file %s.\n", outfilename);
		//return(1);
		return;
	}
	
	if (verbose)
	{
		fprintf(stderr, HEADER1);
		fprintf(stderr, HEADER2);
		fprintf(stderr, "Input binary file: %s\n", infilename);
		fprintf(stderr, "Begin address   = %Xh\n", begin_addr);
		fprintf(stderr, "End address     = %Xh\n", end_addr);
		fprintf(stderr, "Address offset  = %Xh\n", addr_offset);
		fprintf(stderr, "Maximum address = %Xh\n", max_addr);
		fprintf(stderr, "Address bytes   = %u\n", addr_bytes);
	}


	if(have_header)
	{
		
		head_count = strlen(head_char);
		fprintf(outfile,"S0%02X0000",head_count+3);
		head_checksum = head_count+3;
		for(i=0;i= 0; i--)
		{
			c = (address >> (i << 3)) & 0xff;
			fprintf(outfile,"%02X", c);
			checksum += c;
		}

		if(fread(buf, 1, this_line, infile)) {}

		for (i = 0; i < this_line; i++)
		{
			fprintf(outfile,"%02X", buf[i]);
			checksum += buf[i];
		}

		fprintf(outfile,"%02X\n", 255 - checksum);

		record_count++;

		/* check before adding to allow for finishing at 0xffffffff */
		if ((address - 1 + line_length) >= max_addr)
			break;

		address += line_length;
	}

	if (do_headers)
	{
		
		#if 0 
		
		if (record_count > 0xffff)
		{
			checksum = 4 + (record_count & 0xff) + ((record_count >> 8) & 0xff) + ((record_count >> 16) & 0xff);
			fprintf(outfile,"S604%06X%02X\n", record_count, 255 - checksum);
		}
		else
		{
			checksum = 3 + (record_count & 0xff) + ((record_count >> 8) & 0xff);
			fprintf(outfile,"S503%04X%02X\n", record_count, 255 - checksum);
		}
		#endif 

		byte_count = (addr_bytes + 1);
		fprintf(outfile,"S%u%02X", 11 - addr_bytes, byte_count);

		checksum = byte_count;

		for (i = addr_bytes - 1; i >= 0; i--)
		{
			c = (addr_offset >> (i << 3)) & 0xff;
			fprintf(outfile,"%02X", c);
			checksum += c;
		}
		fprintf(outfile,"%02X\n", 255 - checksum);
	}

	if (verbose)
		fprintf(stderr, "Processing complete \n");
}

/***************************************************************************/

int main(int argc, char *argv[])
{
	int i;
	uint32_t size;
	bool offset_specified = false;
	bool end_specified = false;

	for (i = 1; i < argc; i++)
	{
		if (!strcmp(argv[i], "-o"))
		{
			addr_offset = str_to_uint32(argv[++i]);
			offset_specified = true;
			continue;
		}

		else if (!strcmp(argv[i], "-b"))
		{
			begin_addr = str_to_uint32(argv[++i]);
			continue;
		}

		else if (!strcmp(argv[i], "-e"))
		{
			end_addr = str_to_uint32(argv[++i]);
			end_addr -= 1;
			end_specified = true;
			continue;
		}

		else if (!strcmp(argv[i], "-a"))
		{
			sscanf(argv[++i], "%u", &addr_bytes);
			addr_bytes = max(2, addr_bytes);
			addr_bytes = min(4, addr_bytes);
			continue;
		}

		else if (!strcmp(argv[i], "-l"))
		{
			sscanf(argv[++i], "%u", &line_length);
			line_length = max(8, line_length);
			line_length = min(32, line_length);
			continue;
		}

		else if (!strcmp(argv[i], "-s"))
		{
			do_headers = false;
			continue;
		}
		
		else if(!strcmp(argv[i],"-n"))
		{
			have_header  = true;
			head_char = argv[++i];
			if(head_char == NULL)
			{
				
				fprintf(stderr, "\n** No  head note specified\n");
				return(1);
			}
		}
		

		else if (!strcmp(argv[i], "-q"))
		{
			verbose = false;
			continue;
		}

		else if (!strncmp(argv[i], "-h", 2))		 /* -h or -help */
		{
			syntax();
			return(0);
		}

		else
		{
			infilename = argv[i];
			outfilename = argv[++i];
		}
	}

	if (infilename == NULL)
	{
		syntax();
		fprintf(stderr, "\n** No input infilename specified\n");
		return(1);
	}
	if (outfilename == NULL)
	{
		//syntax();
		fprintf(stderr, "\n** No output infilename specified\n");
		return(1);
	}

	if ((infile = fopen(infilename, "rb")) != NULL)
	{
		size = file_size(infile) - 1;

		if (end_specified)
			end_addr = min(size, end_addr);
		else
			end_addr = size;

		if (begin_addr > size)
		{
			fprintf(stderr, "Begin address %Xh is greater than file size %Xh\n", begin_addr, size);
			return(3);
		}

		if (end_addr < begin_addr)
		{
			fprintf(stderr, "End address %Xh is less than begin address %Xh\n", end_addr, begin_addr);
			return(3);
		}

		if (!offset_specified)
			addr_offset = begin_addr;

		process();
		fclose(infile);
		return(0);
	}
	else
	{
		fprintf(stderr, "Input file %s not found\n", infilename);
		return(2);
	}
}

5.s19转bin 实现


#include 
#include 
#include 
#include 

#include "common.h"

#define HEADER1 "\nSREC2BIN " SREC_VER " - Convert Motorola S-Record to binary file.\n"

#define LINE_LEN 1024

char *infilename;
char *outfilename;
FILE *infile, *outfile;

uint32_t max_addr = 0;
uint32_t min_addr = 0;

uint8_t filler = 0xff;
bool verbose = true;

/***************************************************************************/

void syntax(void)
{
	fprintf(stderr, HEADER1);
	fprintf(stderr, HEADER2);
	fprintf(stderr, "Syntax: SREC2BIN  INFILE OUTFILE\n\n");
	fprintf(stderr, "-help            Show this help.\n");
	fprintf(stderr, "-o       Start address offset (hex), default = 0.\n");
	fprintf(stderr, "-a     Minimum binary file size (hex), default = 0.\n");
	fprintf(stderr, "-f     Filler byte (hex), default = FF.\n");
	fprintf(stderr, "-q               Quiet mode\n");
}

/***************************************************************************/

void parse(int scan, uint32_t *max, uint32_t *min)
{
	int i, j;
	char line[LINE_LEN] = "";
	uint32_t address;
	uint32_t rec_type, addr_bytes, byte_count;
	char c;
	uint8_t buf[256];

	do
	{
		if(fgets(line, LINE_LEN, infile)) {}

		if (line[0] == 'S')								/* an S-record */
		{
			rec_type = line[1] - '0';

			if ((rec_type >= 1) && (rec_type <= 3))		/* data record */
			{
				address = 0;
				addr_bytes = rec_type + 1;

				for (i = 4; i < (addr_bytes * 2) + 4; i++)
				{
					c = line[i];
					address <<= 4;
					address += char_to_uint8(c);
				}

				byte_count = (char_to_uint8(line[2]) << 4) + char_to_uint8(line[3]);
				byte_count -= (addr_bytes + 1);

				if (scan)
				{
					if (*min > address)
						*min = address;

					if (*max < (address + (byte_count - 1)))
						*max = address + (byte_count - 1);
				}
				else
				{
					address -= min_addr;

					if (verbose)
						fprintf(stderr, "Writing %3u bytes at %08X\r", byte_count, address);

					j = 0;
					for (i = (addr_bytes * 2) + 4; i < (addr_bytes * 2) + (byte_count * 2) + 4; i += 2)
					{
						buf[j] = (char_to_uint8(line[i]) << 4) + char_to_uint8(line[i+1]);
						j++;
					}
					fseek(outfile, address, SEEK_SET);
					fwrite(buf, 1, byte_count, outfile);
				}
			}
		}
	}
	while(!feof(infile));

	rewind(infile);
}

/***************************************************************************/
#define BLOCK_SIZE 512

int process(void)
{
	uint32_t i;
	uint32_t blocks, remain;
	uint32_t pmax = 0;
	uint32_t pmin = 0xffffffff;

	uint8_t buf[BLOCK_SIZE];

	if (verbose)
	{
		fprintf(stderr, HEADER1);
		fprintf(stderr, HEADER2);
		fprintf(stderr, "Input Motorola S-Record file: %s\n", infilename);
		fprintf(stderr, "Output binary file: %s\n", outfilename);
	}

	parse(true, &pmax, &pmin);

	min_addr = min(min_addr, pmin);
	max_addr = max(pmax, min_addr + max_addr);

	blocks = (max_addr - min_addr + 1) / BLOCK_SIZE;
	remain = (max_addr - min_addr + 1) % BLOCK_SIZE;

	if (verbose)
	{
		fprintf(stderr, "Minimum address  = %Xh\n", min_addr);
		fprintf(stderr, "Maximum address  = %Xh\n", max_addr);
		i = max_addr - min_addr + 1;
		fprintf(stderr, "Binary file size = %u (%Xh) bytes.\n", i, i);
	}

	if ((outfile = fopen(outfilename, "wb")) != NULL)
	{
		for (i = 0; i < BLOCK_SIZE; i++)
			buf[i] = filler;
		for (i = 0; i < blocks; i++)
			fwrite(buf, 1, BLOCK_SIZE, outfile);
		fwrite(buf, 1, remain, outfile);

		parse(false, &pmax, &pmin);
		fclose(outfile);
	}
	else
	{
		fprintf(stderr, "Can't create output file %s.\n", outfilename);
		return(1);
	}

	if (verbose)
		fprintf(stderr, "Processing complete          \n");

	return(0);
}

/***************************************************************************/

int main(int argc, char *argv[])
{
	int i;
	int result = 0;

	for (i = 1; i < argc; i++)
	{
		if (!strcmp(argv[i], "-q"))
			verbose = false;

		else if (!strcmp(argv[i], "-a"))
			max_addr = str_to_uint32(argv[++i]) - 1;

		else if (!strcmp(argv[i], "-o"))
			min_addr = str_to_uint32(argv[++i]);

		else if (!strcmp(argv[i], "-f"))
			filler = str_to_uint32(argv[++i]) & 0xff;

		else if (!strncmp(argv[i], "-h", 2))			/* -h or -help */
		{
			syntax();
			return(0);
		}
		else
		{
			infilename = argv[i];
			outfilename = argv[++i];
		}
	}

	if (infilename == NULL)
	{
		syntax();
		fprintf(stderr, "\n** No input filename specified\n");
		return(1);
	}

	if (outfilename == NULL)
	{
		syntax();
		fprintf(stderr, "\n** No output filename specified\n");
		return(1);
	}

	if ((infile = fopen(infilename, "rb")) != NULL)
	{
		result = process();
		fclose(infile);
		return(result);
	}
	else
	{
		printf("Input file %s not found\n", infilename);
		return(2);
	}
}

参考

1.http://srecord.sourceforge.net/man/man5/srec_motorola.html

2.http://blog.chinaunix.net/uid-22915173-id-249854.html

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章