Convert Kinetis Project to use newlib instead of EWL

You can convert the ewl hello world project created for Kinetis devices to use newlib by making several changes to the build settings and the startup code. The purpose of these changes is to help the build process search the right directories for the right library files, and provide the right instructions to the linker.

Perform these steps to convert Kinetis project to use newlibe instead of EWL.

  1. Start the IDE.
  2. In the CodeWarrior Projects view, select the project for which you want to modify the build properties.
  3. Select Project > Properties .

    The Properties window appears. The left side of this window has a properties list. This list shows the build properties that apply to the current project.

  4. Expand the C/C++ Build property.
  5. Select Settings .

    The Properties window shows the corresponding build properties.

  6. Select Librarian and clear the Enable automatic library configurations checkbox.
  7. Select ARM Ltd Windows GCC C Compiler > Directories .
  8. Select and remove the EWL C/C++/Runtime include directories paths.

    For example remove:

    "${MCUToolsBaseDir}/ARM_GCC_Support/ewl/EWL_C/include" and

    "${MCUToolsBaseDir}/ARM_GCC_Support/ewl/EWL_Runtime/ include"

    This ensures that the compiler does not search in these EWL directories.

    Figure 1. EWL C/C++/Runtime Include Directories Paths
    EWL C/C++/Runtime Include Directories Paths
  9. Select ARM Ltd Windows GCC C Linker > Libraries .
  10. Clear the EWL library search path (-L) .

    For example, remove:

    "${MCUToolsBaseDir}/ARM_GCC_Support/ewl/lib/armv6-m"

    The linker will not be searching for the ewl library.

    Figure 2. ARM Ltd Windows GCC C Linker Libraries
    ARM Ltd Windows GCC C Linker Libraries
  11. Select ARM Ltd Windows GCC C Linker > Miscellaneous .
  12. Under Linker flags (-Xlinker [option]) add:

    -lc -lm -lgcc -lrdimon

    Figure 3. ARM Ltd Windows GCC C Linker Miscellaneous Settings
    ARM Ltd Windows GCC C Linker Miscellaneous Settings
  13. Scroll to the bottom of the settings panel and in the Other flags text box add -specs=rdimon.specs.
    Figure 4. ARM Ltd Windows GCC C Linker Miscellaneous Settings - Other flags
    ARM Ltd Windows GCC C Linker Miscellaneous Settings - Other flags
  14. Remove ' __arm_start.c', '__arm_end.c' and 'runtime_configuration.h' from Project_Settings/Startup_Code.
    Figure 5. Remove __arm_start.c, __arm_end.c and runtime_configuration.h - Before
    Remove __arm_start.c, __arm_end.c and runtime_configuration.h - Before
    Figure 6. Remove __arm_start.c, __arm_end.c and runtime_configuration.h - After
    Remove __arm_start.c, __arm_end.c and runtime_configuration.h - After
  15. Click OK to apply the modified build settings for the project.

    The above changes to the build settings ensure that the build picks newlib instead of ewl. However to make the build complete, you need to provide a startup routine.

Newlib provides a startup routine called _start. However, you must write a wrapper startup function that will do the following tasks before calling newlib '_start'.

  1. Initialize the sp register.
  2. Call the __init_hardware function which is defined in kinetis_sysinit.c.
  3. Copy ROM section to RAM sections. This function call is not required if you are using RAM programming.
  4. Call the _start function.

The following code demonstrates how what the wrapper startup file looks like. This file includes a __rom_to_ram_copy function along with wrapper startup function __thumb_startup.

To write a startup file and add it to the project, perform these steps.

  1. Right click on the Sources folder of the project and select New/Source File .
  2. Provide a name to the source file. For example startup.S.

    Alternatively, you can also create a *. c file and port the following code to inline c code.

  3. Click Finish .
  4. Open the recently created .S file and copy the code in the following listing.
  5. The startup function __thumb_startup is included as an entry point in the existing linker command file. Therefore, the __thumb_startup function will be called first as execution starts.
    Listing: Inline C Code
     .syntax unified
        .arch armv6-m
    
     .text 
        .thumb
        .thumb_func
    
    .align
     2
    
    .globl
       __rom_to_ram_copy
    
     .type 
       __rom_to_ram_copy, %function
    
    __rom_to_ram_copy: 
    
    ldr r1,=___ROM_AT
    ldr r2,=_sdata
    ldr r3,=_edata
    
        subs    r3, r2
        ble    rom_to_ram_loop_end
    
    movs r4,0
    
    rom_to_ram_loop_begin: 
    
       ldr    r0, [r1,r4]
       str    r0, [r2,r4]
    
        adds   r4, 4
        cmp    r4, r3
        blt    rom_to_ram_loop_begin
    
    rom_to_ram_loop_end: 
    
    mov pc,lr
    
        .pool
    
     .size
     __rom_to_ram_copy, . - __rom_to_ram_copy
        .thumb_func
    
     .align
     2
    
     .globl 
       __thumb_startup
    
     .type
        __thumb_startup, %function
    
    __thumb_startup: 
        ldr    r0, =_estack
        mov    sp,r0
        ldr    r0, =__init_hardware
        blx    r0
        ldr    r0, =__rom_to_ram_copy
        blx    r0
        ldr    r0, =_start
        bx     r0
        .pool
    
     .size
     __thumb_startup, . - __thumb_startup
        .end