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

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

/**参考《嵌入式硬件设计》 固件与程序的关系:固件是永久性存储在计算机的存储器中,用来将其他硬件

子系统初始化到一个已知的状态,并对其进行配置以保证计算机的正常运行。而bootloader(引导程序)
保存在固件中。引导程序是由处理器运行的一个特殊程序,它把操作系统从磁盘(或者非易失性存储器,

或者网络)中读取到内存中,以便处理器随后运行该操作系统。系统在上电或复位时通常都从地址

0x00000000处开始执行,而在这个地址处安排的通常就是系统的BootLoader程序。**/


BOOTING A DEVICE 启动设备
Entering and Leaving Boot Mode 进入和离开启动模式

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必须改变一些值。然后,一个器件复位后(硬件或软件)初始化。
对于PIC18F器件,复位命令实际上会通过复位指令(像MCLR)产生一个真正的复位。除了
把一个MCLR指令写入到端口引脚,在PIC16F87XA器件中一个真正的复位是不可能在在固件中(译者注:
就是说开机启动时,不允许发生复位)。虽然支持复位命令,但它只导致PIC16F器件跳转到复位向量;
寄存器将会改变成复位状态。

ading/Writing/Erasing Program Memory 读/写/擦除程序存储器
PIC18F 

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个字节的
倍数上执行(一块)。擦除操作在64个字节上执行(一行)。


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.
当在PIC18F器件上程序存储器进行写时,内存应该被删除。默认的操作是:当写入时,位只能被清除。
一个擦除操作是唯一的动作可用于在程序存储器中设置。因此,如果bootloader保护位不在配置字节中设置, 
内存操作从000H到1FFH可以部分或全部禁用bootloader固件。(译者注:配置保护位很重要)


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程序存储器进行写和擦除。这个
器件ID(地址3FFFFEH和3 3FFFFFH)也会被考虑进程序存储器。虽然他们可以访问的,然而,他们都是只读,
不能被改变。


PIC16F

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结构中会被访问,因此,这些区域既不能读也不能写。

Reading/Writing Data Memory 读/写数据存储器

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。


Configuration Bits 配置位
PIC18F

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.

同样重要的是要注意一些配置位在正常模式中是单方向位;他们只能改变成一种状态,不能改回。
代码保护位在配置寄存器5L和5H是一个很好的例子。如果任何类型的代码保护对一个块是使能的,
在没有器件编程器的情况下它不能被禁用。从本质上讲,bootloader无法撤消代码保护。


PIC16F

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地址处开始执行。)

你可能感兴趣的:(A FLASH Bootloader for PIC16 and PIC18 Devices--硬译(四))