iarsystems / cmake-tutorial

Build and test embedded software using the IAR C/C++ Compiler alongside CMake
https://iar.com
MIT License
76 stars 14 forks source link

How to properly convert and flash .bin files created using IAR and CMake for the 8051 #33

Closed achieve-dream1221 closed 9 months ago

achieve-dream1221 commented 9 months ago

I'm currently working on a project that involves the compilation of code for 8051 using IAR with CMake . The process successfully generates a .bin file, but my main challenge has shifted to figuring out how to properly flash this binary file onto a CC2530 chip, or how to convert it to a .hex file. Despite several attempts to handle this issue by using srec_cat to convert the binary file into a hex file for flashing, the device does not operate as expected once the flashing process is complete. Below is my CMake script that I've been using for the build process:

# Add a executable target named "hello-world"
add_executable(hello-world
        # Target sources
        main.c)

# Set the properties for the "hello-world" target
set_target_properties(hello-world PROPERTIES
        XCL "${TOOLKIT_DIR}/config/devices/_generic/lnk51ew_8051.xcl"
        # C-SPY-related properties
        DDF "${TOOLKIT_DIR}/config/devices/_generic/io8051.ddf")

# Set the compiler flags for the "hello-world" target
target_compile_options(hello-world PRIVATE
        $<$<COMPILE_LANGUAGE:C>:--core=plain --dptr=16,1 --data_model=large --code_model=near
        --calling_convention=xdata_reentrant --place_constants=data --nr_virtual_regs 8
        --dlib --dlib_config ${TOOLKIT_DIR}/lib/dlib/dl8051Normal.h>)

# Set the linker flags for the target
target_link_options(hello-world PRIVATE
        -rt
        # Set the linker script
        -f $<TARGET_PROPERTY:XCL>
        ${TOOLKIT_DIR}/lib/dlib/dl-pli-nlxd-1e16x01n.r51
        # Create a map file from the target's UBROF
        -l $<TARGET_PROPERTY:NAME>.map
        # The `SHELL:` prefix prevents option de-duplication
        "SHELL:-D_NR_OF_BANKS=0"
        "SHELL:-D_CODEBANK_END=0"
        "SHELL:-D_CODEBANK_START=0"
        "SHELL:-D_NR_OF_VIRTUAL_REGISTERS=8"
        "SHELL:-D?PBANK=0xA0"
        "SHELL:-D_IDATA_STACK_SIZE=0x40"
        "SHELL:-D?ESP=0"
        "SHELL:-D?ESP_MASK=0"
        "SHELL:-D_EXTENDED_STACK_START=0"
        "SHELL:-D_EXTENDED_STACK_SIZE=0"
        "SHELL:-D_PDATA_STACK_SIZE=0x80"
        "SHELL:-D_XDATA_STACK_SIZE=0xEFF"
        "SHELL:-D_XDATA_HEAP_SIZE=0xFF"
        "SHELL:-D_FAR_HEAP_SIZE=0xFFF"
        "SHELL:-D_HUGE_HEAP_SIZE=0xFFF"
        "SHELL:-D_FAR22_HEAP_SIZE=0xFFF")

# Link "hello-world" against the "myMath" library
#target_link_libraries(hello-world LINK_PUBLIC myMath)

# https://srecord.sourceforge.net/download.html, 这是将bin文件转为hex文件的工具

set(CMAKE_BUILD_TYPE Debug) # 设置CMAKE_BUILD_TYPE为Debug模式
get_target_property(TARGET_NAME hello-world NAME) # 获取hello-world的名称
# 添加自定义命令,将bin文件转为hex文件
add_custom_command(TARGET hello-world POST_BUILD
        COMMAND srec_cat ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/${TARGET_NAME}.bin -binary -o ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}/${TARGET_NAME}.hex -intel
)
felipe-iar commented 9 months ago

Hi and thanks for your question. I am unable to assist with any suggestion around srec_cat. Though, for XLINK-based compilers, such as for 8051, I'd suggest you consider using, for example, the -Ointel-extended option instead:

target_link_options(<exec-target> PRIVATE   
  # ...
  $<$<CONFIG:Release>:-Ointel-extended>
  $<$<CONFIG:Debug>:-option-that-only-makes-sense-for-debugging>
  -etc
  # ...
)

If you need technical support please reach out to support@iar.com.