Flash o CAN M16C Application Note REU05B0067 0109
Flash o CAN M16C Application Note REU05B0067 0109
Flash o CAN M16C Application Note REU05B0067 0109
Table of Contents
1. Introduction to Flash over CAN ............................................................................................................................ 2
8.4 Tags................................................................................................................................................................. 9
Application note
Figure 1. Flash over CAN may be used to reflash an application after a device is installed by giving the device
a unique programming ID.
FoCAN consists of a High-performance Embedded Workshop (HEW) workspace containing two
projects, CANloader and UserApp. CANloader and UserApp are independent of each other in that they are not
sensitive to any code or data remaps. Each project has a header so that the other one can read key
data; entry access points into the other program space, device and version IDs, data consistency checksum
values, and the security unlock code for flashing.
CANloader, the flashing project, is built to be located in the first flash block of the MCU. The user application is
free to occupy the other flash block(s). PC_Download sends programming commands and data frames thru
the sniffer, over CAN, to the MCU device. CANloader erases the user block(s) and reflashes the new user
application. After completion, the device reboots, checks for data consistency and enters the user application.
The application may be reflashed any time a user connects the PC to the bus and invokes the device’s proper
unlock code and reprogramming CAN ID. When this happens the application exits, reads the CANloader
header for entry address into CANloader, tells the PC it’s ready and UserApp is reflashed again.
firmware includes reflash exit code to transfer execution over to CANloader whenever reflashing is invoked.
This feature allows the device to enter reflash mode during normal execution of the application. UserApp is
Application note
confirm with
CTRL_MSG_ID data = 0x55
Yes
RESET
Wait for for reflashing of user blocks
STOP
Application note
If CAN_Download sends the correct control frame CAN-ID, the UserApp application reads from
canload_head.c in the CANloader section for an entry address to enter CANloader. CANloader then confirms
to the sniffer that it is ready and the application is reflashed.
Warning: No other interrupts should occur during reflash, as the MCU goes into and out of MCU E/W mode.
4. Reflash description
When the ‘Program’ button in the Windows PC application CAN_Download is pressed, commands will be sent
to the Systec sniffer to start downloading the user application code to the MCU flash. The first flash command
frame that is sent is the unlock code that the user enters right after the ‘Program’ button was pressed on the
PC. If the unlock code matches the value in the device’s CANloader header, programming is accepted, the flag
flashing_in_progress is set and the target device firmware enters into ‘Flash over CAN’ mode. CANloader then
confirms to PC_Download to proceed with the flash update. However, only CAN frames with CAN ID value
CTRL_MSG_ID are accepted and processed for reprogramming.
The CANloader firmware will idle when not processing CAN interrupts.
UserApp firmware is reflashed into the user flash memory blocks using EW1 mode. After the user blocks are
reflashed the device reboots.
After reboot, CANloader checks that the UserApp space was successfully flashed by verifying UserApps
memory using a checksum. The user mode interrupt vector in the UserApp scope is used if the device has
been successfully flashed. If CANloader does not calculate a matching checksum to what is stored in the App-
header, the device stays in CANloader and will wait until a new attempt is made to flash UserApp. See
Procedure as well as Tips & hints below for more information on successful programming.
To add a checksum to the application header you can use the default checksum algorithm or your own. This is
to be done when all application development activities are finished. See ‘Checksum protection against failed
application reflash’.
Download procedure
a. Program the RSK board with the CANloader project binary using HEW or FDT. See your RSK’s
Quick Start Guide for details on which MCU device to use, and how to connect and program the
device.
b. After programming CANloader, unplug your debugger and press reset or power cycle the device.
The bottom red LED should light up indicating the device is in CAN bootload mode waiting to be
programmed.
d. Add your application under user_main(). To debug your application see ‘Debugging your application’
below.
f. If you are not going to run CAN_Download from its folder within the source tree, copy the following
DLL-files from the “Flash over CAN/PC_Download” subfolder ‘DLL’ to the windows\system32 folder
Application note
MFC42D.DLL
MFC042D.DLL
MSVCRTD.DLL
USBCAN32.DLL
g. Start CAN_Download.exe
The PC application CAN_Download.exe can be found in the “PC_Download” folder in the “Flash over
CAN” Hew workspace directory.
h. When CANloader is running on target RSK board, the CAN_Download.exe will send CAN control
messages with CAN-id CTRL_MSG_ID (0x201 in the example code) and subsequent program data
frames with the id DATA_MSG_ID (0x200).
i. Connect a Systec USB CANmodul sniffer to your PC and to CAN ch 0. For the 6NK that is the CAN
connectors closest to the E8 connector.
j. Inside CAN_Download, Press ‘CAN Setup’ and set to 500 kbaud. CAN Message ID should default to
200 in Standard.
k. Press ‘File Open’ and select the new application binary created from compiling UserApp.
l. The Unlock code is 0x11 0x11 0x11 0x11 0x11 0x11 0x11 0x11 but can be changed, see
variable unlockcode[8] located in the source code file canload_head.c.
m. Press Download. It takes a moment to erase the user blocks, then the LED will flash each time a
control frame arrives and the flash process continues.
n. When the flash process is complete and CANloader is sent the ‘file complete code’ from
PC_Download, the device is fully programmed.
o. After rebooting, the UserApp header is checked for a correct checksum or code before entering. This
header code would preferably contain a valid checksum generated via the process mentioned below in
‘Checksum protection against failed application reflash’.
Application note
Define message box for control message responses. Do not set to 0 as this is the receive box.
#define CANBOX_TX_CNTRL_RSP 15
Optional size, must fit in RAM. If small, speed should presumably decrease. Check how much RAM your
device has before changing as it will be permanently allocated. An alternative is to use the stack for this by :
#define CANLOAD_FLASHING_BUFFER_SIZE 0x0200
Do not change. PC CAN_Downloader specific value.
#define MAX_CNTL_BATCH_LEN 0x20
CANloader UserApp
Figures 2a & 2b. Memory maps of projects CANloader (to the left) and UserApp for an M16C/6NK. The users
application code simply goes into ‘app_program’. For project CANloader, note the location of section
‘canload_head_FE’, and the fact that all following sections are in flash Block 0. For project UserApp, note the
location of section ‘app_program’ and the subsequent other sections for this project with app_header_FE at
the end of Block 1 right up against Block 0. This was done to keep the headers together and the code size
down.
Application note
FoCAN has a data checksum byte in the control message frames (CTRL_MSG_ID frames), but this is
currently not used on the embedded side. The reason is because there already is a CRC field in all CAN
data frames. One can therefore say that it is already implemented in the CAN standard.
To prevent a failed application reflash should this occur due to some error when reflashing, the application
checksum protection shall be used. When finished developing UserApp’s application source code, a
checksum for the UserApp firmware can be calculated as follows:
1. Put a break in the application checksum calculation routine and restart, the application code will enter
the checksum calculation routine in the sample code when SW3 is pressed.
2. Make a note of the calculated checksum and write it to the application source code header for variable
app_check. Also make the calc-checksum routine of CANloader return a calculated checksum
instead of the default constant test value 0x55AA.
Here is how we jump from CANloader to UserApp using an address specified inside the UserApp header: The
actual jump consists of two assembly lines of code. The first one clears register A0:
asm("mov.w #0,A0");
The next line does an indirect jump. It jumps to the address contained in [an offset+]A0, that is, to the
address contained in BF00+0.
asm("jmpi.a 0BF00h[A0]");
Notice the "contained in" above. The program will start executing from the address that is stored in BF00+0.
(This is not the address to jump to, but the location of the address in App-header). The next question is what
value is actually stored in BF00 (in the App-header)? This is determined at link time. The location of the
application start address is determined by the following lines in sect30_canapp.inc.
Notice the comments after ';'
;---------------------------------------------------------------
; Application ROM data and program section
;---------------------------------------------------------------
APP_HEAD_ADR .equ 0BF00h
.section program,CODE ;Tell the linker that we are now defining a new flash memory
;section named 'program' and it will contain actual program code.
.org APP_ADR ;This is the starting address where the code should reside.
.glb _app_mem_start ;This is just telling the linker to first of all place a label
_app_mem_start: ;'_app_mem_start' at the beginning of the section.
The next piece of the puzzle is filling the App-header at link time with the values we need. Go to app_head.c:
Application note
When debugging with an E8: If your application uses all available memory blocks (it cannot use Block 0 where
CANloader is) you must in the dialog box for E8 place the target debug kernel code in block 0 above canloader
at a higher address. See the mapfile for free space in block 0. By default most FoCAN UserApp sample code
uses all blocks except Block 0. Here is an example using the M16C/29.
Figure: You can place the target debug kernel code in block 0 above canloader at a higher address to leave all
of UserApp free for your code. See the mapfile for free space in block 0.
The programming ID code is shown in the confirmation window when you flash the device!
The default unlock code for project CANloader connecting with E8 (not for reflashing over CAN) is
00000000000000h, or 0xFFFFFFFFFFFFFFh.
Application note
If the default value was not used, and you cannot remember the unlock code, read the CANloader.mot file that
you used to program the device with, at the ID code addresses. These addresses are found in your MCU HW
manual. It will specify at what addresses the ID code values lie.
Example; In a certain project using the R8C/23 one could read the following ID code values by reading the mot
file at the addresses in the second column:
ID byte Address Value read
ID1 FFDF 00
ID2 FFE3 00
ID3 FFE8 00
ID4 FFEF 00
ID5 FFF3 00
ID6 FFF7 00
ID7 FFFB FF
In this case, use the ID-code 000000000000FF to unlock the device.
Tip: Use the power supply from E8 if there are problems unlocking.
You can debug your application with the E8 debugger standalone without CANloader. That way CANloader
does not have to be downloaded when debugging. In this case uncomment the line
DEBUG_APP .equ 1
in sect30_canapp.inc. The reset vector now causes the MCU to start at the Application memory range
bypassing the FoCAN code. Also uncomment
#define DEBUG_APP 1
in flash_o_can.h if you want to use or add debug code.
8.4 Tags
It is possible to search for where modifications have been made to original device .a30 and .inc startup files.
To see the places in the startup code that have been modified to run the FoCAN concept, search for the
character ‘$’ in all the source files.
a. CAN MCUs
Devices which can use this concept are primarily M32C/8x, M16C/6Nx, M16C/1N, M16C/29,
R8C/23. FoCAN software already exists for some of these devices. See item b.
b. See the CAN download page at http://america.renesas.com/CAN. Click on ‘Downloads’.
Application note
c. E-mail
techsupport.rta@renesas.com
Application note
• These materials are intended as a reference to assist our customers in the selection of the Renesas
Technology Corporation product best suited to the customer’s application; they do not convey any license
under any intellectual property rights, or any other rights, belonging to Renesas Technology Corporation or
a third party.
• Renesas Technology Corporation assumes no responsibility for any damage, or infringement of any third-
party’s rights, originating in the use of any product data, diagrams, charts, programs, algorithms, or circuit
application examples contained in these materials.
• All information contained in these materials, including product data, diagrams, charts, programs and
algorithms represents information on products at the time of publication of these materials, and are subject
to change by Renesas Technology Corporation without notice due to product improvements or other
reasons. It is therefore recommended that customers contact Renesas Technology Corporation or an
authorized Renesas Technology Corporation product distributor for the latest product information before
purchasing a product listed herein.
• The information described here may contain technical inaccuracies or typographical errors.
• Renesas Technology Corporation assumes no responsibility for any damage, liability, or other loss rising
from these inaccuracies or errors.
• Please also pay attention to information published by Renesas Technology Corporation by various means,
including the Renesas Technology Corporation Semiconductor home page (http://www.renesas.com).
• When using any or all of the information contained in these materials, including product data, diagrams,
charts, programs, and algorithms, please be sure to evaluate all information as a total system before
making a final decision on the applicability of the information and products. Renesas Technology
Corporation assumes no responsibility for any damage, liability or other loss resulting from the information
contained herein.
• Renesas Technology Corporation semiconductors are not designed or manufactured for use in a device or
system that is used under circumstances in which human life is potentially at stake. Please contact
Renesas Technology Corporation or an authorized Renesas Technology Corporation product distributor
when considering the use of a product contained herein for any specific purposes, such as apparatus or
systems for transportation, vehicular, medical, aerospace, nuclear, or undersea repeater use.
• The prior written approval of Renesas Technology Corporation is necessary to reprint or reproduce in whole
or in part these materials.
• If these products or technologies are subject to the Japanese export control restrictions, they must be
exported under a license from the Japanese government and cannot be imported into a country other than
the approved destination.
• Any diversion or re-export contrary to the export control laws and regulations of Japan and/or the country of
destination is prohibited.
• Please contact Renesas Technology Corporation for further details on these materials or the products
contained therein.