Application Note AN2216/D Rev. 2, 4/2003 MC9S12DP256 Software Development Using Metrowerk's Codewarrior by Stuart Robb Applications Engineer Motorola, East Kilbride Introduction Metrowerk's Codewarrior tool suite provides a set of tools and utilities for MC9S12DP256 software development.
AN2216/D In addition to the hardware paging mechanism, the instruction set has been enhanced with two instructions that allow inter-page function (subroutine) calls. The CALL instruction is similar to the JSR instruction, however, in addition to placing the paged window return address on the stack, it also places the current value of the PPAGE register on the stack before writing the new 8-bit value supplied by the CALL instruction to the PPAGE register.
AN2216/D CodeWarrior Compiler While all 256K of Flash memory can be accessed through the 16K PPAGE window, two of the 16K byte pages can also be accessed at fixed address locations as shown in Figure 1. The fixed page at $4000 – $7FFF is the same block of memory that can be accessed through the page window when the PPAGE register contains $3E. The fixed page from $C000 – $FFFF is the same block of memory that can be accessed through the page window when the PPAGE register contains $3F.
AN2216/D const int *far ptr; declares a pointer to a constant in paged memory. This pointer may be used to access variables in paged memory. The MC9S12DP256 does not have hardware support for far pointers, so if the code which uses this pointer is also in paged memory, an intermediate runtime routine call will be inserted by the compiler. Compiler Memory Models The Metrowerks Codewarrior compiler for the MC68HC12 supports three different memory models.
AN2216/D CodeWarrior Compiler segment until the next #pragma CODE_SEG statement is encountered. Thus assignment of the default segment is a convenient means of terminating a specific segment: #pragma CODE_SEG DEFAULT A function prototype must be declared in the same code segment as the function definition itself. Thus, if the function prototypes are declared in a separate header file, the #pragma CODE_SEG statement must be duplicated, as shown in Figure 2 and Figure 3.
AN2216/D Data Segment #pragma DATA_SEG [SHORT] #pragma DATA_SEG specifies a segment for global data variables. This includes both variable and constant data by default. Constant data may be assigned to a specific segment by means of the #pragma CONST_SEG described below. The only relevant attribute for DATA_SEG for the MC9S12DP256 is SHORT. This specifies that the direct addressing mode is used to access the variable.
AN2216/D CodeWarrior Compiler constant data to the segment ROM_VAR. All constant variables following the #pragma CONST_SEG statement are included in the segment until the next #pragma CONST_SEG statement. Thus assignment of the default segment is a convenient means of terminating a specific segment: #pragma CONST_SEG DEFAULT Any declarations of external data variables must be within the same data segment as the data variable definition itself.
AN2216/D Compiling the BANKED Memory Model The BANKED memory model is specified to the compiler with the option -Mb on the compiler command line. If constant data variables located in paged memory will be used, the options -CpPpage=RUNTIME -CpPpage=0x30 must also be specified on the command line. If the Codewarrior IDE is being used, Command line arguments are specified in the 'Compiler for HC12' dialogue in 'Target Settings Panels'.
AN2216/D CodeWarrior Compiler shown in Figure 7 is changed to that shown in Figure 8. This change has been implemented in version 2.0 of the Codewarrior product. /* Compile with option -DDG128 to activate this code */ #ifdef DG128 /* HC12 DG128 derivative has page register at 0xff and only P page */ #define PPAGE_ADDR (0xFF+REGISTER_BASE) #ifndef __PPAGE__ /* may be set already by option -CPPPAGE */ #define __PPAGE__ #endif #elif defined (A4) /* all setting default to A4 already */ #endif Figure 7.
AN2216/D CodeWarrior Linker Linking Compiled Code Modules Before presenting an example linker command file for the MC9S12DP256, a short discussion on memory addresses used by the linker is necessary. The Codewarrior development tools view the MC9S12DP256’s program memory expansion space as a logical address space. For non-paged memory, the logical address corresponds to the physical address. For paged memory, the logical address is composed of the PPAGE value followed by the page window address.
AN2216/D CodeWarrior Linker $C000 to $FFFF corresponds to the same physical memory as $3F8000 to $3FBFFF. For the reasons described above, code is linked to $C000 to $FFFF in preference to $3F8000 to $3FBFFF. Linker Command File The allocation of the defined code and data segments to memory addresses is controlled by the linker command file. This file may be recognised by the file extension '.prm'. Within this file, the SECTIONS command block is used to define physical regions of memory.
AN2216/D PLACEMENT _PRESTART, STARTUP, ROM_VAR, STRINGS, NON_BANKED, INTERRUPT_ROUTINES, COPY INTO ROM_C000, ROM_4000; DEFAULT_ROM INTO PAGE_30, PAGE_31, PAGE_32, PAGE_33, PAGE_34, PAGE_35, PAGE_36, PAGE_37, PAGE_38, PAGE_39, PAGE_3A, PAGE_3B, PAGE_3C, PAGE_3D; DEFAULT_RAM INTO RAM; END Figure 11. Example of Linker Placement Command Block The PLACEMENT command block is used to allocate code and data segments to the memory segments.
AN2216/D CodeWarrior Linker typedef unsigned char tUINT8; typedef struct { volatile tUINT8 volatile tUINT8 tUINT8 tUINT8 porta; portb; ddra; ddrb; /* /* /* /* /* port A data register */ port B data register */ port A data direction register */ port B data direction register */ continue… */ }tREGISTER; #pragma DATA_SEG S12_REG extern tREGISTER Registers; #pragma DATA_SEG DEFAULT Figure 12.
AN2216/D MC9S12DP256, many programming tools, including the Codewarrior debugger, automatically program the security byte with a value of $FE after successfully erasing the Flash. This prevents the device from accidentally being placed in a secure state if a programming operation were to fail. Having this block of data included in the object file with a value of $FE for the security byte will ensure that a verify operation will be performed properly.
AN2216/D CodeWarrior Linker lower two bits in the security byte in which the part remains unsecured. Figure 15 shows an example linker commant for the placement of the flash security and protection data. SECTIONS /* flash, RAM, EEPROM etc */ FLASH_PROT_AREA = READ_ONLY 0xFF00 TO 0xFF0F; END PLACEMENT /* Placement of code */ FLASH_PROT INTO FLASH_PROT_AREA; /* Placement of Flash Protection Registers */ END Figure 15.
AN2216/D NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, CAN0_TransmitISR, CAN0_ReceiveISR, NULL, CAN0_WakeupISR, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, _Startup /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* /* $FF9C:9D $FF9E
AN2216/D CodeWarrior Linker The VECTORS segment must be allocated to the correct memory address in the linker command file, as shown in Figure 17. In addition, the ENTRIES command must be used to ensure that the table is included. As there are no direct references to the vector table in the code, it would otherwise be 'optimised' and removed.
AN2216/D Linker Optimizations for the BANKED Memory Model In the BANKED memory model all user defined functions are of type 'far' by default. All functions are therefore called using the CALL instruction and terminated with the RTC instruction. There is clearly a price to be paid for this simplistic approach, as some functions which are called only by other functions within the same page of memory could be called with a JSR/RTS instruction sequence.
AN2216/D CodeWarrior Linker PAGE_38 PAGE_39 PAGE_3A PAGE_3B PAGE_3C PAGE_3D = = = = = = READ_ONLY READ_ONLY READ_ONLY READ_ONLY READ_ONLY READ_ONLY IBCC_FAR IBCC_FAR IBCC_FAR IBCC_FAR IBCC_FAR IBCC_FAR 0x388000 0x398000 0x3A8000 0x3B8000 0x3C8000 0x3D8000 TO TO TO TO TO TO 0x38BFFF; 0x39BFFF; 0x3ABFFF; 0x3BBFFF; 0x3CBFFF; 0x3DBFFF; END PLACEMENT _PRESTART, STARTUP, ROM_VAR, STRINGS, NON_BANKED, COPY INTO ROM_C000, ROM_4000; DISTRIBUTE DISTRIBUTE_INTO PAGE_30, PAGE_31, PAGE_32, PAGE_33, PAGE_34, PA
AN2216/D NOTE: Whenever any new functions are added to the source, a new linker generated header file must be generated, i.e. both passes must be executed. NOTE: When code is added to a function, it may no longer fit into its allocated section. If this happens, both build passes must be executed to generate a new include file.
AN2216/D Appendix A: Flash Memory Protection Address Description $FF00 - $FF07 Security Backdoor Comparison Key $FF08 - $FF09 Reserved $FF0A Protection Byte For Flash Block 3 $FF0B Protection Byte For Flash Block 2 $FF0C Protection Byte For Flash Block 1 $FF0D Protection Byte For Flash Block 0 $FF0E Reserved $FF0F Security Byte Figure 20.
AN2216/D Flash Security While no security feature can be 100% guaranteed to prevent access to an MCU’s internal resources, the MC9S12DP256’s security mechanism makes it extremely difficult to access the Flash or EEPROM contents. Once the security mechanism has been enabled, access to the Flash and EEPROM either through the BDM or the expanded bus is inhibited.
AN2216/D Appendix A: Flash Memory Protection SEC[1:0] Security State 0:0 Secured 0:1 Secured 1:0 Unsecured 1:1 Secured Figure 22. Security Bits Even if the memory security and protection features are not being utilized during development, a file containing data for this 16 byte area should be created, compiled and inserted into the linker file for compatibility with some Flash programming tools.
HOW TO REACH US: USA/EUROPE/LOCATIONS NOT LISTED: Motorola Literature Distribution; P.O. Box 5405, Denver, Colorado 80217 1-303-675-2140 or 1-800-441-2447 JAPAN: Motorola Japan Ltd.; SPS, Technical Information Center, 3-20-1, Minami-Azabu Minato-ku, Tokyo 106-8573 Japan 81-3-3440-3569 ASIA/PACIFIC: Motorola Semiconductors H.K. Ltd.; Silicon Harbour Centre, 2 Dai King Street, Tai Po Industrial Estate, Tai Po, N.T.