Author: Ross M. Fosler and
Rodger Richey
Microchip Technology Inc.
译者: 逐影Linux
/**参考《嵌入式硬件设计》 固件与程序的关系:固件是永久性存储在计算机的存储器中,用来将其他硬件
子系统初始化到一个已知的状态,并对其进行配置以保证计算机的正常运行。而bootloader(引导程序)或者网络)中读取到内存中,以便处理器随后运行该操作系统。系统在上电或复位时通常都从地址
0x00000000处开始执行,而在这个地址处安排的通常就是系统的BootLoader程序。**/
With the bootloader firmware loaded, there are two distinct modes of operation: Boot Mode and User
Mode. The bootloader uses the last location of data memory to determine which mode to run in. A
value of FFh indicates Boot mode. Any other value indicates User mode. Thus, a new part with its data
memory not initialized will automatically enter Boot mode the first time.
加载带有Bootloader的固件时,有两种截然不同的操作模式: 启动模式(译者注:产品发布的时候,一般采用
这种模式,没有用户介入)和用户模式(译者注:也叫下载模式,通过中断向用户提供命令行接口,用户
通过串口下载程序映像)。bootloader使用数据存储器的最后一个位置来确定哪些模式中运行。值FFH表
明启动模式。其他任何值表示用户模式。因此,数据存储器中一些未初始化的新的部分将自动进入启动模式。
(译者注:这里只讲启动模式,不讲用户模式)
To leave Boot mode, the last location must be changed to some value other than FFh. Then, a device
RESET (hardware or software) is initiated. For PIC18F devices, the RESET command actually generates
a true RESET via the RESET instruction (same as MCLR). Other than tying a port pin to MCLR, a true
RESET is not possible in firmware on PIC16F87XA devices. Although the RESET command is supported,
it only causes the PIC16F device to jump to the RESET vector; the registers used to perform bootload
operations are not changed to their RESET states.
离开启动模式,最后一个位置除了FFH必须改变一些值。然后,一个器件复位后(硬件或软件)初始化。For the PIC18F devices, commands 1 through 3 support operations to FLASH program memory.
Read operations occur at the byte level. Write operations are performed on multiples of 8 bytes
(one block). Erase operations are performed on 64 bytes (one row).
对于PIC18F器件, 命令1到3支持FLASH程序存储器操作。读操作发生在字节级别。写操作在8个字节的When writing program memory on a PIC18F device, the memory should be erased. The default operation
is: bits can only be cleared when written to. An erase operation is the only action that can be used to set
bits in program memory. Thus, if the bootloader protection bits are not setup in the configuration bytes,
operations on memory from 000h to 1FFh could partially, or completely disable the bootloader firmware.User IDs (starting at address 200000h) are considered to be part of program memory and are written
and erased like normal FLASH program memory. The Device ID (addresses 3FFFFEh and 3FFFFFh)
is also considered program memory. While they can be accessed, however, they are read only and
cannot be altered.
用户ID(从地址200000 H开始)被认为是参与程序存储器--像正常的FLASH程序存储器进行写和擦除。这个The PIC16F87XA devices support reading and writing to program memory. Commands 1 and 2 support
operations to FLASH program memory. Read operations occur at the word level (2 bytes). Write
operations are performed on multiples of 4 words (8 bytes). Since write operations are erase-before-write,
the erase command is not supported. The bootloader area, from 000h to 0FFh, should be write protected
to prevent overwriting itself.
PIC16F87XA器件支持读和写程序存储器。命令1和2的支持FLASH程序器操作。读操作发生在字级别
(2字节)。写操作是4个字的倍数上执行(8个字节)。因为写操作是erase-before-write, 擦除命令不支持。
bootloader区,从000H到 0FFH,应该写保护来防止覆盖。
Neither the User ID nor the Device ID locations are accessible during normal operation on the PIC16
architecture; therefore, these areas can neither be read nor written.
无论是用户ID和设备ID在正常操作PIC16结构中会被访问,因此,这些区域既不能读也不能写。Data memory is read or written one byte at a time, through commands 4 and 5. Since it is not actually
mapped to the normal FLASH memory space, the address starts at 000h and continues to the end of
EEDATA memory. Note that the last location of the data memory is used as a boot flag. Writing
anything other than FFh to the last location indicates normal code execution.
数据存储器通过命令4和5读取或写入一个字节。因为它实际上是没有映射到正常的闪存空间, 地址从000H
到EEDATA存储器(译者注:应该可翻译成EEPROM数据存储区)的末尾。注意,数据存储器的最后位置
作为引导标志。正常代码执行可以写任何位置除了最后位置的FFH。
PIC18F devices allow access to the device configuration bits (addresses starting at 300000h) during
normal operation. In the bootloader, commands 6 and 7 provide this access. Data is read one byte at
a time and, unlike program memory, is written one byte at a time. Since configuration bits are
automatically erased before being written, there is no erase command for configuration memory.
PIC18F器件在正常操作允许访问器件配置位(地址从300000H开始)。 在bootloader中,命令6和7提供这个访问权。数据一次读取一个字节,一次写一个字节,这个不像操作程序存储器那样。因为在写之前
配置位自动擦除, 所以没有擦除命令配置内存。
Having access to configuration settings is very powerful; it is also potentially very dangerous. For example,
assume that the system is designed to run in HS mode, with a 20 MHz crystal. If the bootloader changes
the oscillator setting to LP mode, the system will cease to function — including the bootloader!
Basically, the system has been killed by improperly changing one bit.
获取配置设置权限非常有用; 但是它也可能非常危险。例如, 假设系统是设计用来运行在HS模式, 用一个
20 MHz晶体。如果引导装载程序改变了振荡器,设置为LP模式,系统将停止功能——bootloader!基本上,
系统已经被不正当的改变位操作而杀死。
It is also important to note some configuration bits are single direction bits in Normal mode; they
can only be changed to one state, and cannot be changed back. The code protection bits in Configuration
Registers 5L and 5H are a good example. If any type of code protection is enabled for a block, it cannot
be disabled without a device programmer. Essentially, the bootloader cannot reverse code protection.
同样重要的是要注意一些配置位在正常模式中是单方向位;他们只能改变成一种状态,不能改回。The configuration memory is not accessible during normal operation on the PIC16 architecture; therefore,
this area can neither be read nor written.
配置存储器在PIC16结构上进行不能正常的访问操作;因此,这个区既不能读也不能写。
(译者注:在系统上电的时候,系统首先会确定以哪种模式启动,PIC单片机上文已描述,而其他单片机如
stm32则是根据引脚的电平来判断,然后根据相应模式的起始地址处映射到0地址处,并从0地址处开始执行。)