A FLASH Bootloader for PIC16 and PIC18 Devices--硬译(三)

Author:  Ross M. Fosler and
             Rodger Richey
             Microchip Technology Inc.
译者: 逐影Linux

Communication Protocol 通信协议

The bootloader employs a basic communication protocol that is robust, simple to use, and easy to
implement.

Bootloader使用了一个基本的通信协议,是健壮的,使用简单,并且容易实现。

(译者注:其他串行通信的文件传输协议主要有:Xmodem、Ymodem、Zmodem和KERMIT等,请自行

参考资料)

PACKET FORMAT (数据包格式)
All data that is transmitted to or from the device follows the basic packet format
所有发送的数据或从器件发出的数据遵循如下基本的数据包格式:
<STX><STX>[<DATA><DATA>...]<CHKSUM><ETX>
where each <...> represents a byte and [...] represents the data field.
其中每个<…>表示一个字节和[…]表示数据字段。
The start of a packet is indicated by two 'Start of TeXt' control characters (<STX>), 
and is terminated by a single 'End of TeXt' control character (<ETX>). 
一个数据包的开始用两个'正文开始'的控制字符(用<STX>表示),和终止用单一的'文本结束'的控制字符(用<ETX>表示)。
The last byte before the <ETX> is always a checksum, which is the two’s complement of 
the Least Significant Byte of the sum of all data bytes.
在<ETX>之前的一个字节是一个校验和<CHKSUM>, 这是所有数据字节的和的最低有效字节的二进制补码。
(译者:关于校验和计算参看这两篇文章:http://wenku.baidu.com/view/16ed1f6ea45177232f60a2c8.html |
      http://blog.chinaunix.net/uid-26758209-id-3146230.html,
      不过这边指定的是补码,而且是最低有效字节,不是反码(其实解码出来一样)
     ,不是字(因为这里<CHKSUM>用8位表示),有点出入)

The data field is limited to 255 data bytes. If more bytes are received, then the packet
is ignored until the next <STX> pair is received.
数据字段限制为255字节的数据。如果收到更多的字节,则这个包将会被忽略,直到下一次收到一对<STX>。
Note: Although the protocol supports 255 bytes of data, the specific device that contains the
bootloader firmware may have a sufficiently large data memory to support the largest packet
size. Refer to the data sheet for the particular device for more information.

注意: 尽管协议支持255字节的数据, 有些特殊的器件,包含bootloader固件可能足够有大的数据内存支持最大的

数据包的 大小。参考具体器件数据表的更多信息。

CONTROL CHARACTERS(控制字符)
There are three control characters that have special meaning. Two of them, <STX> and <ETX>, 
are introduced above. The last character not shown is the 'Data Link Escape', <DLE>. 
Table 1 provides a summary of the three control characters.
有三个控制具有特殊意义的字符。其中两个,<STX>和<ETX>,已经上面介绍了。最后一个字符没有显示是
"Data Link Escape",<DLE>(<数据通信换码>)。表1总结了三个控制字符。

The <DLE> is used to identify a value that could be interpreted in the data field as a 
control character. Within the data field, the bootloader will always accept the byte 
following a <DLE> as data, and will always send a <DLE> before any of the three control 
characters. For example, if a byte of value 0Fh is transmitted as part of the data field, 
rather than as the <STX> control character, the <DLE> character is inserted before
the <STX>. This is called "byte stuffing".

<DLE>是用来确定一个值,可以解释的数据字段作为一个控制字符。在数据字段,bootloader将总是接收跟随一个

<DLE>作为数据字节,并且总是在任意三个控制字符之前发送一个<DLE>。例如,如果一个值为0FH的字节传输作

为数据字段的一部分,而不是<STX>控制字符, 那么<DLE>字符前会插入<STX>之前.(译者注:这个<DLE>相当

于C语言中转义字符'\',不过应该表示的是实际的值,反转义)。这就是所谓的"byte stuffing"。

表1 控制字符(译者注:这些跟ASC||表有出入)
Note: Control characters are not considered data and are not included in the checksum.

注意: 控制字符是不会被考虑进数据,并且不包括进校验和(译者注:<STX><ETX><DLE>不包括进数据长度,

<CHKSUM>考虑进数据长度)。

COMMANDS(命令)
The data field for each packet contains one command and its associated data. 
The commands are detailed in Appendix A.
每个包的数据字段包含一个命令及其相关的数据。详细的命令见附录一。
COMMAND RESPONSE LATENCY(命令响应延迟)
Flow control is built into the protocol. Thus, for every received command (except RESET), there
is a response. If there is no response, then one (or more) of the following has happened:
流量控制在协议建立。因此,每收到命令(除了复位),有一个响应。如果没有响应,那么一个(或更多)发生了下面的事:
• the data was corrupted (bad checksum) 
• the packet was never received
• the data field was too long
• RESET was executed
• 数据发生错误 (校验和发生错误,如果发送端checksum字段设为0,那么接收端checksum字段应该为1) 
• 没有接收到包
• 数据字段过长
• 执行了复位操作

So how long do you wait before deciding a problem has occurred? The response latency 
(shown in Figure 6) is dependent on the amount of data sent, the command being executed, and
 the clock frequency.

所以你等待多久才能决定一个上述问题发生了?响应延迟(如图6)依赖于发送的数据量,命令是否被执行,还有时钟频率。

图6 发送到接收延时

For read commands, the latency is highly dependent on the clock frequency, and the size of 
the packet. For a small packet at high frequency, the response is almost immediate, typically 
on the order of a few microseconds. For large packets, the latency could be on the
order of (大约)hundreds of microseconds.

读命令,延迟是高度依赖于时钟频率,包的大小。对于一个小包在高频率,响应几乎立即,通常大约几微秒。对于一

大的数据包,延迟可能要在大约数百微秒。

In general, read commands require very little time compared to write commands. Write commands 
are mostly dependent on internally timed write cycles. For example, the typical write time required
for a single EEPROM location is 4 ms. If the maximum packet size (250 bytes of writable data) was
sent, the receive to transmit latency would be about 1 second.

通常,读命令相对于写命令要求更小的时间。写命令大多数依赖于内部固定的写周期。例如, 典型的写操

作所需的时间对于单个EEPROM位置是4ms,如果最大数据包大小(250字节的可写数据)被发送, 传输到

接收延迟约1秒。

SYNCHRONIZING 同步
The first <STX> in the protocol is the synchronization byte. It is used to match the 
device’s baud rate to the source’s baud rate. Thus, the device is synchronized to the 
source on every new packet.
第一个<STX>在协议中是同步的字节。它是用来匹配器件的波特率和发送源的波特率。因此,在发送每个

新的数据包时,该器件与发送源是同步的。
Note: If a 'Start of Text' condition is received during the reception of a packet, then no
synchronization occurs.
注:  如果一个'Start of Text'的条件是在接收一个包期间被收到,那么不是同步发生。
    (译者注:同步指双方的时钟要调整到同一个频率)

SELECTING FOSC AND BAUD RATE 选择FOSC和波特率
The recommended baud rate for this application is 9600 bps. This is the ideal rate for a 
device operating from 4 MHz, to the device’s maximum operating frequency (40 MHz in most cases).
Higher baud rates are possible, but degenerate conditions can occur.
推荐波特率对于这个应用程序为9600 bps。这是对于一个器件操作的理想的速率从4 MHz,到器件的最高工作频率
(40 MHz在大多数情况下)。更高的波特率是有可能,但退化条件下可以发生。

There are a few clock frequency/standard baud rate combinations that lead to a degenerate 
baud rate selection during synchronization; under such conditions, the device will never 
synchronize to the source. Clock frequencies that avoid such degenerate
conditions are given by the equation:
有几个时钟频率/标准波特率组合,导致退化的波特率选择在同步;在这种情况下,该器件将永远不会同步与发送源。
为了时钟频率避免这样的退化条件是给出了下面的方程:

FOSC = (1 ± E)(X + 1)(16)(B)
where(其中) E is the error (误差)(typically 2%), X is the value for the SPBRG register, and B is the baud 

rate. A table of calculated clock oscillator ranges for most of the common baud rates is provided in Appendix

 B for quick reference.
其中E是误差值(通常是2%), X是SPBRG寄存器的值,B是波特率。一个用来计算时钟振荡器范围的最常见的波特率表
在附录B中提供了快速参考。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值