历史上的今天
返回首页

历史上的今天

今天是:2025年07月30日(星期三)

正在发生

2021年07月30日 | Motorola S-records(S19)解析模块

2021-07-30 来源:eefocus

飞思卡尔系列单片机程序编译后生成的是S-records格式的文件(.s19)。在实现远程升级的时候,不可避免的需要传输S-record记录(或其转码后的数据),然后根据里头的数据来更新程序。这就需要对ASCII码表示的S-record数据进行解析。


于是就写了这个模块,或说工具包可能更准确点。


顺便一提。我已基于官方的版本基本实现了自定协议的bootloader,改成了软方式进入bootloader而不是官方的使用引脚的方式。代码不准备放出来,但后面会写篇文章介绍下自定的协议,原理,以及实现时遇到的坑等。


下面上代码:


/*

*********************************************************************************************************

*

*

*                              Motorola S-records Support package

*                                        S-records 支持包

*

* File    : SRecord.h

* By      : Lin Shijun(http://blog.csdn.net/lin_strong)

* Date    : 2018/03/03

* Version : V1.0

* Note    : 1. This package provide the functions for converting between the S-record and the 

*                corresponding string.

*              这个包提供了S-record与字符串间的转换函数

*           2. The package supposed the letter in the string in upper case. user should make

*                sure for that.

*              工具包默认字符串中的字母都是大写的,用户需要保证这件事。

*           3. the first thing to do with this package is to check the TYPE DEFINE.

*              用这个包的第一步是检测下定义的类型对不对 

*

* the follow description of Motorola S-records Format is from 

*    http://www.amelek.gda.pl/avr/uisp/srecord.htm

* chinese version: http://blog.csdn.net/lin_strong/article/details/78521950

*

* NAME

*   srec - S-record file and record format

* DESCRIPTION

*

*   An S-record file consists of a sequence of specially formatted ASCII character strings. An S-record 

* will be less than or equal to 78 bytes in length.

*

*   The order of S-records within a file is of no significance and no particular order may be assumed.

*

*   The general format of an S-record follows:

*

*   +-------------------//------------------//-----------------------+

*   | type | count | address  |            data           | checksum |

*   +-------------------//------------------//-----------------------+

*

*   type -- A char[2] field. These characters describe the type of record (S0, S1, S2, S3, S5, S7, S8, or S9).

*

*   count -- A char[2] field. These characters when paired and interpreted as a hexadecimal value, display the 

* count of remaining character pairs in the record.

*

*   address -- A char[4,6, or 8] field. These characters grouped and interpreted as a hexadecimal value, display 

* the address at which the data field is to be loaded into memory. The length of the field depends on the number

* of bytes necessary to hold the address. A 2-byte address uses 4 characters, a 3-byte address uses 6 characters,

* and a 4-byte address uses 8 characters.

*

*   data -- A char [0-64] field. These characters when paired and interpreted as hexadecimal values represent 

* the memory loadable data or descriptive information.

*

*   checksum -- A char[2] field. These characters when paired and interpreted as a hexadecimal value display the

* least significant byte of the ones complement of the sum of the byte values represented by the pairs of 

* characters making up the count, the address, and the data fields.

*

*   Each record is terminated with a line feed. If any additional or different record terminator(s) or delay 

* characters are needed during transmission to the target system it is the responsibility of the transmitting 

* program to provide them.

*

*   S0 Record. The type of record is 'S0' (0x5330). The address field is unused and will be filled with zeros 

* (0x0000). The header information within the data field is divided into the following subfields.

*   

*     mname is char[20] and is the module name.

*     ver is char[2] and is the version number.

*     rev is char[2] and is the revision number.

*     description is char[0-36] and is a text comment.

*

*     Each of the subfields is composed of ASCII bytes whose associated characters, when paired, represent one 

* byte hexadecimal values in the case of the version and revision numbers, or represent the hexadecimal values 

* of the ASCII characters comprising the module name and description.

*

*   S1 Record. The type of record field is 'S1' (0x5331). The address field is intrepreted as a 2-byte address.

* The data field is composed of memory loadable data.

*

*   S2 Record. The type of record field is 'S2' (0x5332). The address field is intrepreted as a 3-byte address. 

* The data field is composed of memory loadable data.

*

*   S3 Record. The type of record field is 'S3' (0x5333). The address field is intrepreted as a 4-byte address. 

* The data field is composed of memory loadable data.

*

*   S5 Record. The type of record field is 'S5' (0x5335). The address field is intrepreted as a 2-byte value and 

* contains the count of S1, S2, and S3 records previously transmitted. There is no data field.

*

*   S7 Record. The type of record field is 'S7' (0x5337). The address field contains the starting execution 

* address and is intrepreted as 4-byte address. There is no data field.

*

*   S8 Record. The type of record field is 'S8' (0x5338). The address field contains the starting execution 

* address and is intrepreted as 3-byte address. There is no data field.

*

*   S9 Record. The type of record field is 'S9' (0x5339). The address field contains the starting execution 

* address and is intrepreted as 2-byte address. There is no data field.

*

* EXAMPLE

*

*   Shown below is a typical S-record format file.

*

*     S00600004844521B

*     S1130000285F245F2212226A000424290008237C2A

*     S11300100002000800082629001853812341001813

*     S113002041E900084E42234300182342000824A952

*     S107003000144ED492

*     S5030004F8

*     S9030000FC

*

*   The file consists of one S0 record, four S1 records, one S5 record and an S9 record.


*   The S0 record is comprised as follows:


*     S0 S-record type S0, indicating it is a header record.

*     06 Hexadecimal 06 (decimal 6), indicating that six character pairs (or ASCII bytes) follow.

*     00 00 Four character 2-byte address field, zeroes in this example.

*     48 44 52 ASCII H, D, and R - "HDR".

*     1B The checksum.

*

*   The first S1 record is comprised as follows:

*

*     S1 S-record type S1, indicating it is a data record to be loaded at a 2-byte address.

*     13 Hexadecimal 13 (decimal 19), indicating that nineteen character pairs, representing a 2 byte address,

*  16 bytes of binary data, and a 1 byte checksum, follow.

*     00 00 Four character 2-byte address field; hexidecimal address 0x0000, where the data which follows is 

*  to be loaded.

*     28 5F 24 5F 22 12 22 6A 00 04 24 29 00 08 23 7C Sixteen character pairs representing the actual binary

*  data.

*     2A The checksum.

*

*   The second and third S1 records each contain 0x13 (19) character pairs and are ended with checksums of 

* 13 and 52, respectively. The fourth S1 record contains 07 character pairs and has a checksum of 92.

*

*   The S5 record is comprised as follows:

*

*     S5 S-record type S5, indicating it is a count record indicating the number of S1 records

*     03 Hexadecimal 03 (decimal 3), indicating that three character pairs follow.

*     00 04 Hexadecimal 0004 (decimal 4), indicating that there are four data records previous to this record.

*     F8 The checksum.

*

*   The S9 record is comprised as follows:

*

*     S9 S-record type S9, indicating it is a termination record.

*     03 Hexadecimal 03 (decimal 3), indicating that three character pairs follow.

*     00 00 The address field, hexadecimal 0 (decimal 0) indicating the starting execution address.

*     FC The checksum.

*

*********************************************************************************************************

*/


#ifndef SRECORD_H 

#define SRECORD_H


/*

*********************************************************************************************************

*                                      INCLUDE

*********************************************************************************************************

*/


#include


/*

*********************************************************************************************************

*                                      TYPE DEFINE

*********************************************************************************************************

*/


typedef unsigned char INT8U;

typedef unsigned long INT32U;


/*

*********************************************************************************************************

*                                      CONSTANT

*********************************************************************************************************

推荐阅读

史海拾趣

展恒电子(Broadic)公司的发展小趣事

2007年,Broadic在美国加州成立,以其前瞻性的视野和深厚的技术积累,迅速在电子行业中崭露头角。同年,公司开始着手研发创新的芯片技术,奠定了其未来发展的坚实基础。2008年,深圳市展恒电子有限公司应运而生,作为Broadic在国内的销售公司,它的成立标志着Broadic正式进入中国市场,开始了其在国内的快速发展之旅。

复旦微电子(FM)公司的发展小趣事

展恒电子始终坚持以品质为核心,严格把控产品质量。在与上海芯北电子科技有限公司的合作中,双方共同保证产品品质的一致性、兼容性和安全性。在合作期间,展恒电子成功向客户供应了数百万片芯片,并得到了客户的高度认可和信任。这种对品质的坚持和追求,使得展恒电子在电子行业中赢得了良好的声誉,也为公司的长期发展奠定了坚实的基础。

这五个故事从不同角度展示了展恒电子(Broadic)在电子行业中的发展历程和成就,体现了其在技术创新、市场拓展、品质保证等方面的实力和努力。通过不断努力和创新,展恒电子已经在电子行业中取得了显著的成果,并将继续为行业的发展贡献自己的力量。

芯茂微电子公司的发展小趣事

芯茂微电子始终坚持品质至上和客户至上的原则,致力于为客户提供高品质、高性能的集成电路产品和服务。公司建立了完善的质量管理体系和客户服务体系,从产品研发、生产、销售到售后服务,每一个环节都严格把控,确保产品质量和客户满意度。正是凭借这种对品质和客户的执着追求,芯茂微电子赢得了客户的广泛信赖和赞誉。

Densitron公司的发展小趣事

为了扩大市场份额,Densitron公司制定了一系列市场拓展策略。公司首先分析了市场需求和竞争态势,确定了目标市场。然后,通过加强品牌宣传、优化销售渠道、提高客户服务质量等手段,不断提升品牌知名度和客户满意度。此外,公司还积极开展国际合作,与全球知名电子企业建立了紧密的合作关系,共同开拓市场。这些策略的实施,使得Densitron公司的市场份额逐年上升,成为行业内的领军企业。

CIRCUITCO公司的发展小趣事

CIRCUITCO公司自创立之初,就致力于电子电路技术的研发。在某一时期,公司投入大量资源研发新型高性能电路板。经过数年的努力,团队终于成功开发出一款具有更高集成度、更低能耗的电路板。这一突破不仅提升了公司的技术实力,也使其在市场上获得了显著的竞争优势。随着这款新产品的推出,CIRCUITCO公司的业绩迅速攀升,成为行业内的佼佼者。

Etron公司的发展小趣事

1991年,台湾的电子行业正蓬勃发展,钰创科技(Etron)在这一时期应运而生,创始人凭借对市场敏锐的洞察力,决定专注于利基型缓冲记忆体产品与系统晶片的设计与生产。初创时期,公司面临着技术、资金和市场等多重挑战。然而,钰创科技凭借其团队的技术实力和创新精神,成功开发出了一系列具有竞争力的产品,逐渐在市场上崭露头角。

问答坊 | AI 解惑

有需要电感、变压器样品的工程师请进来,可能会有您用到的产品。

本帖最后由 jameswangsynnex 于 2015-3-3 19:57 编辑 我工厂位于深圳观澜,是一家专业电感、贴片变压器生产商.在品质、交期、单价各方面都有一定的优势.有需要这方面样品的工程师敬请联络索样,我会尽快免费提供给大家,谢谢! 美登一电子(深圳)有 ...…

查看全部问答>

智能电子钟程序

学习单片机十天,编写的智能电子钟程序。…

查看全部问答>

50Mhz分频的问题

怎么将50M的时钟分频成153600hz? 用普通的方法好像不能解决,因为除不尽。 我设想用一个pll,倍频后,再分 高手指教…

查看全部问答>

有操作系统的嵌入式设备和没有操作系统的嵌入式设备之间的区别

有操作系统的嵌入式设备和没有操作系统的嵌入式设备之间的区别,以及有操作系统的嵌入式设备的优点是什么??…

查看全部问答>

初学者问几个问题,关于wince与arm,望高人指教

首先,我用的是微芯力科的ws-430评估板,cpu为at91rm9200,要求用wince开发。 评估板附带的资料只有linux以及ucos系统的,没有wince的资料,打电话到微芯力科公司,被告知该板不支持wince,因为wince要求某尺寸的真彩屏幕,而我的板子上是一个128 ...…

查看全部问答>

如何验证下位机的串行发送程序

我现在没有芯片,只是在keilc中写的串行通信程序,我想验证一下它能不能实现发送字节,只有一台的情况下该如何做?其中发送字节程序是? void sendbyte(unsigned char word) { SBUF=word; while(TI==0);   TI=0; } …

查看全部问答>

到底怎么才算嵌入式编程?

会C语言就算是会嵌入式编程了吗?…

查看全部问答>

原以为写个usb驱动很简单

DOS下的usb storage驱动一个比一个不好用,我一急就决定自己写一个,结果搞了20天了,还一头浆糊,怎么办。想来想去还是原始社会好啊…

查看全部问答>

AD快捷键大全

挺全的,ad6的快捷键大全,很实用的…

查看全部问答>

如何使用MSP430外部计数信号TACLK

急求高手指点,我的做法是引脚TACLK连接外部频率信号,对外部信号直接计数,不知道为什么总是得不到计数值。 MSP430F2274 P1SEL |= BIT0; P1DIR &= ~BIT0; TACTL = TASSEL_0 + ID_0 + MC_2;…

查看全部问答>