MSP430 single-chip microcomputer program upgrade instance

Source: Internet
Author: User
Tags lockstate

How to upgrade the MSP430 Program

Key Laboratory of optoelectronics, Ocean University of China

Abstract: This paper introduces how to upgrade the program of the MSP430 Series single-chip microcomputer, and describes in detail how to implement custom firmware upgrade and remote program upgrade. Various strategies and technologies required for firmware upgrade are provided.

Keywords: MSP430 In-System Program JTAG BSL

About MSP430

TI's MSP430 Series microcontroller is a low-power hybrid signal controller. It uses a 16-bit simplified instruction set and integrates 16 General registers and constant generators, the code execution efficiency is greatly improved; 27 simple kernel commands facilitate learning; and flexible clock source selection can maximize the battery life. Different models of MSP430 Series single-chip microcomputer are integrated with a wide range of Peripheral modules for different application fields, including: wdt, Timer A (timer-A), and timer B (timer-B), comparator, serial port 0, 1 (usart0, 1), hardware multiplier, LCD driver, 8-way 12/16-bit ADC, port, basic timer (Basic timer ).

Thanks to its outstanding low power design and rich on-chip peripheral modules, it is very suitable for embedded intelligent systems that require low power consumption and high performance.

MSP430 program upgrade method

There are three programming methods for the MSP430 Series microcontroller: using the JTAG interface, using the BSL firmware and using the User-Defined firmware upgrade. The custom firmware update method is flexible and widely used. This article will focus on it.

1. Use the JTAG interface

The JTAG interface is integrated with the single-chip microcomputer of the MSP430 Series to implement the test access port State Machine (TAP controller) that complies with ieeestd1149.1 ). It uses a four-line serial interface (test is used for chips with fewer pins ). Data or commands are migrated from TDI (test data input); serial data is removed from TDO (test data output); TCK (test clock) is used as the clock signal input; TMS (test mode selection) signal Control the status of the TAP controller. This interface can be used to move commands and data to control the address line and data line of the target chip, so as to read and write the flash and simulate debugging of the target chip [1]. In addition, TI has now introduced a new debugging interface-SPY-BI-WIRE, which uses two-wire, one of which is a data cable (two-way), one for the clock line.

The advantage of this interface is that you do not need to design additional circuits and programs, and you can download the program using the simulator. The disadvantage is that once the JTAG fuse is burned out to ensure code security, the interface is permanently destroyed and cannot be used.

2. Use the BSL Interface

BSL is the abbreviation of bootstrap loader, and the Chinese name is the program loader. It is essentially a communication program in the chip (occupying 0c00h-1000h address space), which can be used to erase and read/write flash. Because it is solidified in the chip, you do not have to worry about its changes or loss.

This interface uses five lines: Gnd, TX (p1.1/p1.0), rx (P2.2/p1.1), RST, tck (test), in rst and TCK (test) the BSL program can be started with a specific level timing signal to achieve communication with the target chip. The character format of the communication is 8 data bits, One Stop bits and one even check bits. The initial baud rate is 9600bps (BSL 1.6 can change the baud rate to 38400bps ). The BSL Protocol requires that an 80 h character be accepted for synchronous clock, and then a response character 90 H is sent. Then, it takes 8 characters and jumps to the corresponding processing routine according to the command. The C language of the BSL program is described as follows:

Void main ()

{

Byte B, barray [8];

Lockstate = lock;

Initports ();

While (1)

{

B = receivesyncbyte ();

Synctimer (B );

Sendack ();

For (I = 0; I <8; I ++) barray [I] = receivebyte ();

Switch (barray [I])

{

0x12: // write flash

For (I = 0; I <barray [7]; I ++)

{

B = receivebyte ();

If (lockstate = unlock) writebyte (B );

}

Break;

0x10: // receive Password

Receive32byte ();

If (passwordcorrect) lockstate = unlock;

Break;

0x18 .. 0xn:

If (passwordcorrect) dotask ();

}

If (noerror) sendnak ();

Else

If (txdata) senddata ();

Else

Sendack ();

}

The implementation details may vary depending on the version. If you want to use it for program upgrade, refer to [2] [3]. The BSL program is used to upgrade the firmware, saving code space. You do not need to upgrade the firmware, and many ready-made BSL upgrade tools are available. The disadvantage is that you need to reserve BSL interfaces, field Wiring is required.

3. Use custom firmware update

The flash memory module of MSP430 Series single-chip microcomputer is a physical storage unit that can be operated independently. All modules are arranged in the same linear address space. The memory is divided into multiple 512-byte segments (the information segment size is 128/64 bytes ). Each segment can be erased separately, and the flash can be erased by the program under normal working voltage. Therefore, it is particularly suitable for online program upgrade (In-System Program ).

A custom firmware update contains a piece of code used to upgrade the application. You can use the existing communication interface to upgrade the remote code. The implementation principle is to place two pieces of code in the target chip: Application and upgrade. The address segments of the two do not overlap, so that you can use the Upgrade Program to erase the application and write new code.

3.1 boot program

After resetting, go to the boot program and decide to enter the Upgrade Program or application. The Bootstrap program directly enters the upgrade program when the application does not exist or is incorrect, so as to ensure that the upgrade fails and can be upgraded again.

The Bootstrap program is described as follows:

Void main ()

{

While (1)

{

If (resetvectorvalid () Application ();

Updata ();

}

}

The resetvectorvalid () function is used to check whether an application exists or is valid. The implementation can be to check whether the entry address of enterapplication is legal. A simple implementation is

# Define resetvectorvalid () (resetvector! = FFFF)

Here, resetvector is the entry address of the application, which is usually placed in a fixed address. After upgrading the program, modify the entry address. Application () is an application. It will not return if it is executed normally. It will only return after receiving the upgrade command. You can use the return statement in application () to enter the Upgrade Program.

Updata () is the Upgrade Program. Check commands must be added at the entrance to confirm that the Upgrade Program is normal. After entering the Upgrade Program, the communication end should first send the erasure command, erase the original code, and then send the Upgrade Code to update flash. If external extended memory or user programs are small, you can receive the entire program segment first. If the verification is correct, then the writing will be more reliable.

Here, a policy is to first erase the block containing the resetvector, and finally write the value of the resetvector, so as to avoid entering the incomplete application.

3.2 write an application

There is no major change in application writing. You need to add a custom upgrade command in the communication protocol to enter the Upgrade Program. In addition, you need to change the link file (*. xcl) and specify the application address range. The following uses the application address range of 2500-f7dc as an example (the default setting is commented out)

// Code

//-Z (CODE) cstart = 2500-ffdf

//-Z (CODE) code = 2500-ffdf

-Z (CODE) cstart = 2500-f7df

-Z (CODE) code = 2500-f7df

// Constant data

//-Z (const) data16_c, data16_id, difunct, checksum = 2500-ffdf

-Z (const) data16_c, data16_id, difunct, checksum = 2500-f7df

// Interrupt Vectors

//-Z (const) intvec = FFE0-FFFF

-Z (const) intvec = F7E0-F7FF

After the modification, add the file to the project. The compiled code can be used as the Upgrade Code.

3.3 Preparation of the Upgrade Program

Create a new project and locate the Upgrade Code in a region that does not overlap with the application, such as the F800-FFFF, without modification

-Z (const) intvec = FFE0-FFFF

In the Upgrade Program, all interrupts except reset interruptions are mapped to the application. One way is to embed the Assembly, use the Assembly locating command org, or write 15 interrupt shooting functions, as shown below:

// Remap the interrupt vector address

# Pragma vector = 0x0

_ Interrupt void intvec_0 (void)

{

ASM ("BR & 0f7e0h"); // assume that f7e0 stores the address of Interrupt 15.

}

In addition, you can dynamically determine the interrupt entry address by placing the interrupt vector address in the agreed Ram, as shown below:

_ No_init void (* intvec1 [16]) () @ 0x200; // defines an array pointing to the function pointer to map new interrupt Vectors

// Remap the interrupt vector address

# Pragma vector = 0x0 //

_ Interrupt void intvec_0 (void)

{

ASM ("Push R15 ");

ASM ("mov #0x200, R15 ");

ASM ("Call @ R15 ");

ASM ("Pop R15 ");

}

Then map the interrupt vectors in the application, as shown in figure

Intvec1 [timera0_vector/2] = timer_a_0;

The timer_a_0 () function is executed when timera0 is interrupted. This feature allows you to dynamically determine the entry to interrupt functions at runtime, such as virtual functions in advanced languages ).

After writing the two function blocks, you can perform engineering tests.

3.4 The application and upgrade are completed at the same time

You may also want two functions to be completed in one project. In addition to modifying the link file, pay attention to the following points:

(1) locate all functions of the Upgrade Program to the upgrade program space, that is, add the positioning command before the Function

# Pragma location = "updatecode" // updatecode indicates the name of the segment where the Upgrade Program is located.

(2) modify the function return call routine. When the function returns, the default register routines will be called, and these routines may not be in the address space of the upgrade program. One solution is to use the LST file (assembly code) generated by the compiling environment to modify the pop-up register routine called when the function returns one by one, so that the code is independent of the two. The disadvantage of this is that every time you change the C language code, you have to re-Modify the assembly code, which is cumbersome. Another method is to consider that the upgrade program accepts and sends data, and generally does not need to be interrupted. In this way, you can add the _ monitor compilation command before the upgrade function to indicate that the function is an atomic operation. This type of function is first pushed to the entrance of the SR and interrupt is prohibited. When the return is performed, reti is used to return the result. In this case, the compiler does not call the routine to pop up the saved registers, but one by one pops up the registers based on the stack situation.

(3) change the switch statement. When a switch statement is used, the compiler will also generate a default routine call, which is difficult to block. Therefore, only the switch can be modified into multiple judgment statements.

Conclusion

This article gives a detailed introduction to the upgrade solution of the MSP430 Series Single-Chip Microcomputer. Readers can easily upgrade remote programs by following certain steps, which is of great significance in practical application. In addition, the upgrade method in this article is not limited to the MSP430 Series, it can also be applied to similar single-chip series.

References

1 programming a flash-based MSP430 using the JTAG interface literature No. slaa149

2 features of the MSP430 bootstrap loader literature No. slaa089a

3 "Application of bootstrap loader in MSP430 with Flash hardware and software Proposal" Literature No. slaa096b

4. MSP430 family mixed-signal microcontroller application reports literature No. slaa024

5 MSP430 iar c/C ++ compiler Reference Guide

6 Liu Hongzhi Xie Lili. Discussion on flash single-chip microcomputer self-programming technology; application of single-chip microcomputer and embedded system 2004.3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.