Share this

C8051F040 firmware upgrade via CAN bus

2026-04-06 04:33:50 · · #1

Abstract: This paper focuses on the C8051F040 microcontroller [1] , a new type of SOC (System on a Chip) on-chip mixed signal microcontroller. It elaborates on the Flash storage structure, erasure and write process, CAN bus communication application layer protocol, and software flow, and proposes a firmware upgrade scheme with high stability and high reliability through the CAN communication port.

Abstract: This paper introduces a system that can upgrade the firmware of the C8051F040, a series of the SOC SCM, and elaborates firmware memories structure, process of Flash clear and write, application layer protocol by CAN-Bus, software process, then put forward a high stability and strong blue print for firmware upgrading byCAN-Bus.

Keywords: C8051F040; FLASH Page;monitor routine;CAN-bus communication protocol

0 Introduction

With the development of electronic technology, microcontrollers are widely used in industrial automatic control and instrumentation. In practical applications, system upgrades and updates are often required. In many cases, microcontrollers used in vehicles, construction machinery, and other products are not easily disassembled, making online firmware updates a crucial issue.

Silicon Labs' C8051F040 51-series microcontroller integrates a CAN controller that fully supports CAN 2.0A and CAN 2.0B. This paper uses the C8051F040 microcontroller to design IAP (In Application Program) application software. Through the CAN communication port, it receives program update code from a PC via CAN bus communication, achieving stable and reliable firmware upgrades.

1 C 8051F 040 Microcontroller Firmware Storage Structure

The C8051F040 is a fully integrated mixed-signal system-on-a-chip (SoC) with 64 digital I/O pins. It integrates a CAN 2.0B controller with 32 message objects, each with its own identifier. It features a high-speed, pipelined, 8051-compatible CIP-51 core (up to 25 MIPS), 64KB of built-in Flash program memory, 4352 (4K+256) of on-chip RAM, two 12-bit DACs, two voltage comparators, and one voltage reference.

IAP (Instant Access Programming) refers to the method of erasing and reprogramming Flash blocks, Flash registers, and Boot vectors by acquiring new code during the execution of the main program (user application). The C8051F040 MCU can implement IAP because it can perform erase and write operations on the Flash memory through user code running within the MCU. The C8051F series microcontrollers can implement IAP through any of their digital peripheral interfaces, including UART, SMBUS, SPI, and CAN. The C8051F040 microcontroller's internal 64KB Flash memory can be divided into two parts: a bootloader program area for program loading and a user application memory area.

2. Firmware upgrade implementation method

The system requires two projects: a bootloader, which writes the bootloader code to the FLASH memory via JTAG; and an application program, which writes the application code to the application code area via the bootloader. When the application needs an upgrade, the bootloader program is executed. The bootloader writes the new application code received from the CAN communication port into the application code area. In this paper, the bootloader area occupies the FLASH space from 0x0000 to 0x1000, while the application program area uses the remaining FLASH space. The functional block diagrams of the bootloader and application program are shown in Figure 1 and Figure 2, respectively.

When updating FLASH program code, it is necessary to erase it first and then write it.

(1) Flash erase

The Flash program area of ​​the C8051F040 microcontroller [2] is organized by page for erasure and writing, with 512 bytes per page. When deleting the user program area, first disable all interrupts and enable the program deletion bit PSEE (PSCTL.1), and enable PSWE (PSCTL=0x03, i.e., PSWE=1, PSEE=1, page erase mode), and use a pointer in C51 language to point to the Flash program area. For example: char xdata *data addr=0x0000;

Each time a page is erased from the program area, the address pointer is incremented by 512; this process is repeated to erase the program area. Partial program erasure is performed.

while(addr<0x0F000) // 0xF000 is 60KB

{ *addr = 0x00; // Empty write, initiate erase

for(i=0;i<10;i++)

addr = addr + 512; // This erases the 60KB old Flash application.

(2) Flash writing

Flash memory can be programmed using the MOVX instruction in software. Like a regular operand, the MOVX instruction provides the address and data bytes to be programmed. Before writing to the flash memory using the MOVX instruction, the program store write enable bit PSWE (PSCTL.0) must be set to logic '1' to allow flash write operations. This will cause the MOVX instruction to perform a write operation to the flash memory instead of writing to the XRAM. The PSWE bit remains set until it is cleared by software. To avoid accidental writes to the flash memory, interrupts are disabled while PSWE is logic '1'.

Writing to FLASH memory can clear data bits, but it cannot set data bits to '1'. Only the erase operation can set data bits in FLASH to '1'. Therefore, before writing a new value, the address of the byte to be programmed must be erased first. FLASH memory is organized in 512-byte sectors, and the erase operation operates on a complete FLASH page (setting all bytes in the page to 0xFF). FLASH operations are: erase first, then write; erase by page, write by byte.

(3) The application file needs to include program statements that redirect the application code to the new address:

This ensures that the application will be executed from the application area after the program update is complete.

3. CAN Communication Protocol

CAN bus (Controller Area Network) is a multi-master local area network, namely controller area network, first launched by BOSCH in the mid-1980s for the application of modern automobiles. The CAN bus itself only defines the physical layer and data link layer in the ISO/OSI model. In actual application, users need to define the application layer protocol according to their own needs to define the use of 11/29-bit identifier and 8-byte data in CAN message in order to ensure efficient data transmission. C8051F040 has a controller area network (CAN) controller and uses the CAN protocol [3] for serial communication. Silicon Labs CAN controller conforms to Bosch specifications 2.0A (basic CAN) and 2.0B (full-function CAN). The CAN controller integrated on the Silicon Labs MCU chip is only a protocol controller and cannot provide physical layer driver (i.e., no transceiver). Therefore, an external CAN bus transceiver is required when using it.

The CAN application layer protocol [4] mainly specifies the allocation scheme of identifiers, the process data exchange method, and the communication implementation method. For the experimental stage, for small systems with a small number of nodes and small data types, the CAN communication network is selected and the application layer protocol is defined. The protocol needs to define the format of the information frame, and the CAN multi-master transmission mode can be used. The definition of the application layer protocol [5] mainly defines the following aspects: the allocation of identifiers, the message frame format, and the communication implementation method. There are two different frame formats for CAN communication message transmission: standard frame and extended frame. The difference is that the length of the identifier field is different. The frame with 11-bit identifier is called the standard frame, while the frame with 29-bit identifier is called the extended frame. This paper adopts the standard frame format with 11-bit identifier for the small control system completed under laboratory conditions. The message transmission is represented and controlled by four different frame types: data frame, remote frame, error frame, and overload frame.

(1) Identifier allocation

In the CAN protocol, identifiers determine message priority, and the communication method is determined by the setting of the acceptance filter. Identifiers can also be defined to represent node addresses, custom data types, etc. Application layer protocols must first allocate identifiers according to the characteristics of the system. Points to note during allocation include:

① Different messages in the system have different identifiers.

② Standard frame format, identifier length is 11 bits. The transmission order is ID10 to ID0, where bit timing and frame structure determine that the high 7 bits of the identifier (ID10 to ID4) must not all be "recessive".

The data types in the system include: analog data, digital data, and equipment parameters.

This paper designs a small system. In the 11-bit identifier (ID10~ID0) allocation, ID10 and ID9 represent data types. Since the number of system nodes is less than 16, and the node address code requires 4 bits, ID8~ID5 are assigned to each node as node address encoding, and ID4~ID0 are assigned to the classification numbers of various messages.

(2) Message frame format

The application layer message frame format is basically the data frame or remote frame format, but the identifier has been further specified. When updating the FLASH program code, it must be erased first and then written.

(1) Flash erase

The Flash program area of ​​the C8051F040 microcontroller [2] is organized by page for erasure and writing, with 512 bytes per page. When deleting the user program area, first disable all interrupts and enable the program deletion bit PSEE (PSCTL.1), and enable PSWE (PSCTL=0x03, i.e., PSWE=1, PSEE=1, page erase mode), and use a pointer in C51 language to point to the Flash program area. For example: char xdata *data addr=0x0000;

Each time a page is erased from the program area, the address pointer is incremented by 512; this process is repeated to erase the program area. Partial program erasure is performed.

Flash memory can be programmed using the MOVX instruction in software. Like a regular operand, the MOVX instruction provides the address and data bytes to be programmed. Before writing to the flash memory using the MOVX instruction, the program store write enable bit PSWE (PSCTL.0) must be set to logic '1' to allow flash write operations. This will cause the MOVX instruction to perform a write operation to the flash memory instead of writing to the XRAM. The PSWE bit remains set until it is cleared by software. To avoid accidental writes to the flash memory, interrupts are disabled while PSWE is logic '1'.

Writing to FLASH memory can clear data bits, but it cannot set data bits to '1'. Only the erase operation can set data bits in FLASH to '1'. Therefore, before writing a new value, the address of the byte to be programmed must be erased first. FLASH memory is organized in 512-byte sectors, and the erase operation operates on a complete FLASH page (setting all bytes in the page to 0xFF). FLASH operations are: erase first, then write; erase by page, write by byte.

(3) The application file needs to include program statements that redirect the application code to the new address:

#pragma INTVECTOR (0X1000)

#pragma INTERVAL(3)

This ensures that the application will be executed from the application area after the program update is complete.

Application layer message frame formats primarily use data frame and remote frame formats, but further standardize the identifiers. The application layer frame format is divided into three segments, as shown in Table-1 below:

Based on the function and type of the data, it is organized according to this application layer frame format and prepared for transmission. When a node receives data, it decomposes the data according to the corresponding application layer frame format, correctly understands the information function, and uses the data. For a received message, the Remote Request bit (RTR) is first checked. When RTR=0, it is a data frame, and the meaning of the message is then determined and processed. When RTR=1, it is a remote frame, and the message is organized and sent according to the data type, message classification, and other information provided in the message.

During system debugging and maintenance, operators may need to modify or access parameters of a node, and this information is crucial for system communication. To ensure communication accuracy, this paper adopts a point-to-point communication method, where the host computer acts as the master node, initiating communication and performing read/write operations on slave node messages. In point-to-point communication, the master node communicates with slave nodes by sending data frames and remote frames. Each transmission consists of 8 bytes of data, which generally meets the requirements of all data types. For data exceeding 8 bytes, additional protocol information is required to indicate the length of the data block and the order of the bytes. The first byte of the first frame segment can be defined as the frame number and sequence number, for example: the high 4 bits represent the frame number, and the low 4 bits represent the frame sequence number. If the first byte is insufficient, the first two bytes can be used to define the frame number and sequence number. To ensure data accuracy, very long data blocks should be avoided as much as possible.

summary

This paper describes an online upgrade system that implements online software upgrades for the C8051F040 microcontroller via the CAN bus. During the experiment, to ensure the accuracy and effectiveness of communication, message verification of the communication data is required, and error handling procedures must be defined. This online upgrade system is applicable to the C8051F series microcontrollers and also provides valuable reference for the online programming design of other microcontrollers.

References

[1] Pan Zhuojin. C8051F040/1/2/3/4/5/6/7 Mixed-signal ISP Flash Microcontroller

[2] SILICON LABORATORIES. C8051F040 Datasheet[S],2003

[3] Bosch Corporation. CAN Specification V2.0 [S], 2005

[4] Wu Kuanming. CAN Bus Principles and Application System Design [M], Beijing University of Aeronautics and Astronautics Press, 1996.

[5] Yang Chunjie et al. CAN Bus Technology [M], Beijing University of Aeronautics and Astronautics Press, 2010

Read next

CATDOLL 138CM Jing TPE (Customer Photos)

Height: 138cm Weight: 26kg Shoulder Width: 30cm Bust/Waist/Hip: 65/61/76cm Oral Depth: 3-5cm Vaginal Depth: 3-15cm Anal...

Articles 2026-02-22