CY8CKit-059, PSOC5LP, VSCode, KitProg3, C++

KitProg3 now works with PSOC5LP, if you put lipstick on a debugger stub. I finally deciphered some of the issues regarding using KitProg3 with PSOC5LP. You have to promote a KitProg2 and use him for the job.

TLDR; Cut off the KitProg stub from the 059 kit. Get a KitProg2 (red board) kit (i.e. cy8ckit-149) Update the KitProg2 to KitProg3. Cut it off the kit. Use the fwloader to upgrade it to a kitprog3. Use the same program to configure it into CMSIS-DAP HID mode. Connect the 059 to the KitProg3 debugger SWD pins.

I use 0.1″ posts soldered onto the stub and the end of the 059 board, and jumper wire kit to connect the two boards (the stub debugger and the 059 board). The 12[6] and 12[7] pins on the stub (the TTL RS232 i/o pins, see the bottom of the stub’s board) are brought to the i/o pins on the 059 board (12[7] and 12[6] ). Then you have a single device that debugs and provides UART connectivity.

Learn the terminal program on mac or linux. It works about the same in Linux, Mac, Solaris, HPUX, and other unixes I have used. ie. “screen /dev/tty.usbmodem144413 15200” On my Mac I use screen in a terminal window. Replace the 144413 with the ID you get when you use “ls /dev/tty.*”

Also, a ctrl-a, ctrl-\ exits the screen program. After exiting screen, you usually have to use the “reset” command (type reset, hit enter) on the terminal window to put it back to proper operation. On the Mac, “shutdown -r now” is the reboot command that reboots your computer.

Details, SmeTails

KitProg3 allows you to debug with the PSOC5LP while on the Mac (or linux) and have a UART side channel that allows you to have a debug terminal. (TerminalDebug.c is a permanent part of my code now.)

Unfortunately, “true” KitProg3 does not work on the KitProg that ships with the Cy8CKit-059, mainly because it is 3.3 volts and the 059 kit is 5 volts. However, the KitProg2 is a 5 volt debugger stub. It can also be modified to be a KitProg3 using the fwloader that Cypress/IF provides.

After making the changes, test the kitprog3 debugger in windows with PSOC Creator. Once that works, you can move to the Macintosh/Linux side of things. I assume that you have read my previous posts and have the Macintosh VSCode PSOC5LP environment working. If not, go through that mind numbing exercise and come back to this post.

In the VSCode PSOC5 environment, there is an “invisible” .vscode directory that contains launch.json. Modify it so it looks similar to the following. I have lots of “note to self” type of entries:

{
    "version": "0.2.1",
    "configurations": [
        {
            "name": "KitProg1 Debug (CMSIS-DAP)",
            "type": "cortex-debug",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "executable": "${workspaceRoot}/build/${workspaceFolderBasename}.elf",         
            "servertype": "openocd",
            "serverpath": "${env:ONETHINX_TOOLS_LOC}/openocd/bin/openocd",
            "searchDir": [ "${env:ONETHINX_TOOLS_LOC}/openocd/scripts" ],
            "openOCDLaunchCommands": [
                "tcl_port disabled",
                "telnet_port disabled",
                "adapter speed 1500", // was 500 (is in khz)
            ],
            "configFiles": [
               "interface/cmsis-dap.cfg",//<-- Kitprog 2.2.1 must be in "breathing" mode.
               //"../cmsis-dap.cfg", <-- this also works
               "target/psoc5lp.cfg"
            ],
            "preLaunchCommands": [
                "directory PSoC_Creator.cydsn/Generated_Source/PSoC5",          // Add location of source files built with PSoC Creator
                "set mem inaccessible-by-default off",
                "set output-radix 16",                                        // Comment this out if you want decimal output instead of hexadecimal
            ],
            "postLaunchCommands": [
                "monitor reg",
                "monitor reset init"
            ],
            "postRestartCommands": [
                "tbreak main",
                "monitor reg",
                "monitor reset init",
            ],
            "runToEntryPoint": "main",
            //"runToMain": true,     <--deprecated       // if true, program will halt at main. Not used for a restart     
            "preLaunchTask": "${command:otx.preLaunch}",                        // Set this to run a task from tasks.json before starting a debug session
            "showDevDebugOutput": "none",                                        // Shows output of GDB, helpful when something is not working right
        },
        {
            "name": "KitProg1 Attach (CMSIS-DAP)",
            "type": "cortex-debug",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "executable": "${workspaceRoot}/build/${workspaceFolderBasename}.elf",         
            "servertype": "openocd",
            "serverpath": "${env:ONETHINX_TOOLS_LOC}/openocd/bin/openocd",
            "searchDir": [ "${env:ONETHINX_TOOLS_LOC}/openocd/scripts" ],
            "openOCDLaunchCommands": [
                "tcl_port disabled",
                "telnet_port disabled",
                "adapter speed 1500", // was 500 (is in khz)
            ],
            "configFiles": [
               "interface/cmsis-dap.cfg",//<-- Kitprog 2.2.1 must be in "breathing" mode.
               "target/psoc5lp.cfg"
            ],
            "preLaunchCommands": [
                "directory PSoC_Creator.cydsn/Generated_Source/PSoC5",          // Add location of source files built with PSoC Creator
                "set mem inaccessible-by-default off",
                "set output-radix 16",                                        // Comment this out if you want decimal output instead of hexadecimal
            ],
            "postLaunchCommands": [
                "monitor reg",
                "tbreak",               // force immediate break.  comment out if you want it to continue running after attach
            ],
            "postRestartCommands": [
               // "tbreak main",        // used to prove did not cause psoc reset.
                "monitor reg",
            ],
            "preLaunchTask": "${command:otx.preLaunch}",                        // Set this to run a task from tasks.json before starting a debug session
            "showDevDebugOutput": "none",                                        // Shows output of GDB, helpful when something is not working right
        },
        {
            "name": "KP3 Debug (CMSIS-DAP)",
            "type": "cortex-debug",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "executable": "${workspaceRoot}/build/${workspaceFolderBasename}.elf",    
            "servertype": "openocd",
            "serverpath": "${env:ONETHINX_TOOLS_LOC}/openocd/bin/openocd",
            "searchDir": [ "${env:ONETHINX_TOOLS_LOC}/openocd/scripts" ],
            "openOCDLaunchCommands": [
                "tcl_port disabled",
                "telnet_port disabled",
                "adapter speed 1500",
                //"set ENABLE_ACQUIRE 0",
                //"cmsis_dap_serial 141610F301237400",                          // Select debugger if you have multiple connected
               // "kitprog3 power_config on 3300"           // if you must use this, set to 5000 for PSOC5LP cy8ckit-059
            ],
            "configFiles": [
                "interface/kitprog3.cfg",
                "target/psoc5lp.cfg"
            ],
            "preLaunchCommands": [
                "directory PSoC_Creator.cydsn/Generated_Source/PSoC5",          // Add location of source files built with PSoC Creator
                "set mem inaccessible-by-default off",
                "-enable-pretty-printing",
                "set output-radix 16",                                        // Comment this out if you want decimal output instead of hexadecimal
            ],
            "postLaunchCommands": [
                "monitor reg",
                "monitor reset init"
            ],
            "postRestartCommands": [
                "tbreak main",
                "monitor reg",
                "monitor reset init",
            ],
            "runToEntryPoint": "main",                                                  // if true, program will halt at main. Not used for a restart     
            "preLaunchTask": "${command:otx.preLaunch}",                        // Set this to run a task from tasks.json before starting a debug session
        },
        {
            "name": "KP3 Attach (CMSIS-DAP)",
            "type": "cortex-debug",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "executable": "${workspaceRoot}/build/${workspaceFolderBasename}.elf",    
            "servertype": "openocd",
            "serverpath": "${env:ONETHINX_TOOLS_LOC}/openocd/bin/openocd",
            "searchDir": [ "${env:ONETHINX_TOOLS_LOC}/openocd/scripts" ],
            "openOCDLaunchCommands": [
                "tcl_port disabled",
                "telnet_port disabled",
                "adapter speed 1500",
                "set ENABLE_ACQUIRE 1",
            ],
            "configFiles": [
                "interface/kitprog3.cfg",
                "target/psoc5lp.cfg"
            ],
            "preLaunchCommands": [
                "directory PSoC_Creator.cydsn/Generated_Source/PSoC5",          // Add location of source files built with PSoC Creator
                "set mem inaccessible-by-default off",
                "-enable-pretty-printing",
                "set output-radix 16",                                        // Comment this out if you want decimal output instead of hexadecimal
            ],
            "postLaunchCommands": [
                "monitor reg",
                "tbreak",                       // force immediate halt. comment out if you wish it to continue running.
            ],
            "postRestartCommands": [
                "monitor reg",
            ],
            "preLaunchTask": "${command:otx.preLaunch}",                        // Set this to run a task from tasks.json before starting a debug session
        },
        {
            "name": "Program (KitProg3)",
            "type": "cortex-debug",
            "request": "launch",
            "cwd": "${workspaceRoot}",
            "executable": "${workspaceRoot}/build/${workspaceFolderBasename}.elf",
            "servertype": "openocd",
            "serverpath": "${env:ONETHINX_TOOLS_LOC}/openocd/bin/openocd",
            "searchDir": [ "${env:ONETHINX_TOOLS_LOC}/openocd/scripts" ],
            "openOCDPreConfigLaunchCommands": [
                "set PROGRAMMER kitprog3",
                //"set ENABLE_ACQUIRE 0",
                //"set ENABLE_CM0 0"
            ],
            "configFiles": [
                "interface/kitprog3.cfg",
                "target/psoc5lp.cfg"
            ],
            "openOCDLaunchCommands": [
                "tcl_port disabled",
                "telnet_port disabled",
                //"cmsis_dap_serial 141610F301237400",      // Select debugger if you have multiple connected
                //"cmsis_dap_serial 1B1D0F8B02237400",
                //"kitprog3 power_config on 3300"
            ],
            "overrideLaunchCommands": [
                "monitor program ./build/${workspaceFolderBasename}.hex",
                //"monitor reset_config srst_only",
                "monitor reset run",
                //"monitor psoc6.dap dpreg 0x04 0x00",
                //"-gdb-exit"
            ],
        }
    ]
}

The KitProg1 is the kitprog on the cy8ckit-059, version 2.21. The KitProg3 is the KitProg2 that was upgraded to KitProg3 with firmware.

The Attach works with KitProg 1 and 3. It lets you connect to a running program and debug it. If I Remember Correctly, this one will halt the program where it is. Very handy for an overnight run.

The Program only feature has not been tested well, but it should work.

If you wish to use C++ with PSOC5LP see my previous post, and the information provided by embeddedNinja, linked to in the previous posts. The CMakeLists.txt that works with both C and C++ files (where the c++ files are of the type .cpp), and provides access to the floating point math library for things like sine, cosine, tan, etc is as follows:

cmake_minimum_required(VERSION 3.7.2)
#set(CMAKE_MAKE_PROGRAM "/usr/bin/make")
set(DEBUG_ON true)
######################################################################
#
#	Toolchain Setup
#
######################################################################
# Put the following line (without the # in front) in main.c for VS_CODE cross compile in mac
# static volatile uint8_t dummy __attribute__ ((section(".cyeeprom"),unused));        // Fix for linker error.cyeeprom data will not fit in EEPROM 
#
macro(FIND_DIRECTORIES return_list dir_in)
    FILE(GLOB_RECURSE new_list ${dir_in})
    SET(dir_list "")
    FOREACH(file_path ${new_list})
        GET_FILENAME_COMPONENT(dir_path ${file_path} PATH)
        SET(dir_list ${dir_list} ${dir_path})
    ENDFOREACH()
    LIST(REMOVE_DUPLICATES dir_list)
    SET(${return_list} ${dir_list})
endmacro()

if(${DEBUG_ON})
    set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "ON")
    message("INFO: path=$ENV{PATH}")
endif()

set(TOOLS_DIR $ENV{ONETHINX_PACK_LOC}/tools_2.0)
set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/source)
set(CONFIG_DIR ${CMAKE_CURRENT_SOURCE_DIR}/config)
set(CREATOR_DIR ${CMAKE_CURRENT_SOURCE_DIR}/PSoC_Creator.cydsn)
set(CREATOR_SOURCE_DIR ${CREATOR_DIR}/Generated_Source/PSoC5)

if (${CMAKE_HOST_SYSTEM_NAME} MATCHES "Windows")
    find_program (BASH_TOOL ${TOOLS_DIR}/bash/bash.exe DOC "The bash executable")
else()
    find_program (BASH_TOOL bash DOC "The bash executable")
endif()

find_program(CYPRESS_ELF_TOOL CyMcuElfTool "${TOOLS_DIR}/cymcuelftool-1.0/bin" DOC "Cypress ELF tool executable")
find_program(READELF_TOOL arm-none-eabi-readelf "${COMPILER_DIR}" DOC "ReadELF tool executable")

#Get the top directory name. This will be used as project_name in later section
string(FIND ${CMAKE_CURRENT_SOURCE_DIR} / POSITION REVERSE )
MATH(EXPR POSITION "${POSITION}+1")
string(SUBSTRING ${CMAKE_CURRENT_SOURCE_DIR} ${POSITION} -1 TOP_DIR_NAME)
set(ENV{TOP_DIR_NAME} ${TOP_DIR_NAME})

if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/local.cmake)
    include (${CMAKE_CURRENT_SOURCE_DIR}/local.cmake)
endif()

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR ARM)

# Without that flag CMake is not able to pass test compilation check
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

######################################################################
#
#	Project
#
######################################################################

message("INFO: Project \"${TOP_DIR_NAME}\"")
project(${TOP_DIR_NAME})
enable_language(ASM)

#Check if the correct compiler is installed
execute_process(COMMAND ${CMAKE_C_COMPILER} -print-multi-lib OUTPUT_VARIABLE GCC_VERSION)
string(FIND "${GCC_VERSION}" "armv7-m" ARM_V7M_DETECTED)
if (${ARM_V7M_DETECTED} EQUAL "-1")
    message( FATAL_ERROR "Wrong compiler selected, CMake will exit." )
endif()

#Include common settings
include(${CONFIG_DIR}/common.cmake)

######################################################################
#
#	Project compiler & linker options
#
######################################################################
# adding -x c++ here failed the assembler
set(PROJECT_FLAGS_ASM
    "-mthumb"
    "-mcpu=cortex-m3"
    "-DCY_CORE_ID=0"
    "-Wall"
    "-Wtype-limits"
    "-Wunused"
    "-Wunused-parameter"
    "-ffunction-sections"
    "-ffat-lto-objects"
    "-Og"
)
set(PROJECT_FLAGS_ARCH
    "-mthumb"
    "-mcpu=cortex-m3"
    "-DCY_CORE_ID=0"
    "-Wall"
    "-Wtype-limits"
    "-Wunused"
    "-Wunused-parameter"
    "-ffunction-sections"
    "-ffat-lto-objects"
    "-Og"
)
#-g -D DEBUG -D CY_CORE_ID=0 -Wall -ffunction-sections -ffat-lto-objects -Og -c
#"-L/Applications/VSCode_OnethinxPack_macOS/gcc-arm-none-eabi-9-2020-q2-update/arm-none-eabi/lib/"
#add the "m" library for math
set(PROJECT_LINKER_FLAGS
    "-Wl,-Map=${PROJECT_NAME}.map"
    "-L${CREATOR_SOURCE_DIR}"
    "-l m"
    "-Tcm3gcc.ld"
    "-mcpu=cortex-m3"
    "-mthumb"
    "-specs=nano.specs"
    "-Wl,--gc-sections"
    "-g"
    "-ffunction-sections"
    "-Og"
    "-ffat-lto-objects"
)

######################################################################
#
#	Sources
#
######################################################################

# include these paths
FIND_DIRECTORIES(SOURCE_INCLUDE_DIRS ${SOURCE_DIR}/*.h)
include_directories (
    ${SOURCE_INCLUDE_DIRS}
    ${CREATOR_SOURCE_DIR}
)

# Find Source files (containing *.c)
#FILE(GLOB SOURCE_SRCS ${SOURCE_DIR}/*.c)
# wsm the following is to also allow c++
FILE(GLOB_RECURSE SOURCE_SRCS ${SOURCE_DIR}/*.c ${SOURCE_DIR}/*.cpp)
#list(APPEND CMAKE_CXX_SOURCE_FILE_EXTENSIONS C_EXTENSIONS )
#FILE(GLOB_RECURSE SOURCE_SRCS ${SOURCE_DIR}/*.c)


set(PROJECT_SRCS
    ${SOURCE_SRCS}
    ${CREATOR_SOURCE_DIR}/CyBootAsmGnu.s
    ${CREATOR_SOURCE_DIR}/cyfitter_cfg.c
    #${CREATOR_SOURCE_DIR}/cymetadata.c
    ${CREATOR_SOURCE_DIR}/Cm3Start.c
)

if(${DEBUG_ON})
    message("INFO: Project sources        = ${PROJECT_SRCS}")
    message("INFO: Project source folder  = ${PROJECT_SOURCE_DIR}")
    message("INFO: Source include folders = ${SOURCE_INCLUDE_DIRS}")
    message("INFO: Source folders         = ${SOURCE_SRCS}")
    message("INFO: Design source folders  = ${DESIGN_SRCS}")
endif()

# use the line below to make all "c" files compile as C++
#SET_SOURCE_FILES_PROPERTIES( ${SOURCE_SRCS} PROPERTIES LANGUAGE CXX )


######################################################################
#
#	TARGET
#
######################################################################
#TODO: Evaluate ASM FLAGS
set (CMAKE_ASM_FLAGS "${C_FLAGS} ${C_FLAGS_WARNINGS} ${FLAGS_OPTIMIZATION} ${PROJECT_FLAGS_ASM}")
#set (CMAKE_C_FLAGS "${C_FLAGS} ${C_FLAGS_WARNINGS} ${FLAGS_OPTIMIZATION} ${PROJECT_FLAGS_ARCH}")
set (CMAKE_C_FLAGS "${PROJECT_FLAGS_ARCH}")
# wsm added separate cxx flags
set (CMAKE_CXX_FLAGS "${PROJECT_FLAGS_ARCH}")
#set (CMAKE_CXX_FLAGS "${CXX_FLAGS} ${CXX_FLAGS_WARNINGS} ${FLAGS_OPTIMIZATION} ${PROJECT_FLAGS_ARCH}")
set (CMAKE_EXE_LINKER_FLAGS "${PROJECT_LINKER_FLAGS}")

# CMake lists are separated by semicolons, remove these
string(REGEX REPLACE ";" " " CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS}")
string(REGEX REPLACE ";" " " CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
string(REGEX REPLACE ";" " " CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REGEX REPLACE ";" " " CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")

#set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS}")
#set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS}")
#set (CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS}")

#set (CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS}" CACHE STRING "" FORCE)
#set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS}" CACHE STRING "" FORCE)
#set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS}" CACHE STRING "" FORCE)
#set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "" FORCE)
#set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}" CACHE STRING "" FORCE)

if(${DEBUG_ON})
    message("INFO: ASM Options    = ${CMAKE_ASM_FLAGS}")
    message("INFO: C Options      = ${CMAKE_C_FLAGS}")
    message("INFO: C++ Options    = ${CMAKE_CXX_FLAGS}")
    message("INFO: Linker Options = ${CMAKE_EXE_LINKER_FLAGS}")
endif()

add_executable(${PROJECT_NAME}.elf ${PROJECT_SRCS})

######################################################################
#
#	LIBRARIES  Adding the CyComponent libraries
#
######################################################################
add_library(cycomponentlib STATIC IMPORTED)
add_library(designlib STATIC IMPORTED)

SET_TARGET_PROPERTIES(cycomponentlib PROPERTIES IMPORTED_LOCATION "${CREATOR_DIR}/CyComponentLibrary.a")
TARGET_LINK_LIBRARIES(${PROJECT_NAME}.elf cycomponentlib)

SET_TARGET_PROPERTIES(designlib PROPERTIES IMPORTED_LOCATION "${CREATOR_DIR}/CortexM3/ARM_GCC_541/Debug/PSoC_Creator.a")
TARGET_LINK_LIBRARIES(${PROJECT_NAME}.elf designlib)

######################################################################
#
#	Build actions
#
######################################################################

add_custom_target(
    ${PROJECT_NAME} ALL
    #POST BUILD
    COMMAND ${CONFIG_DIR}/cyelftool -C "${PROJECT_NAME}.elf" --flash_row_size 256 --flash_size 262144 --flash_offset 0x00000000
    COMMAND "${READELF_TOOL}" -Sl ${PROJECT_NAME}.elf > ${PROJECT_NAME}.readelf
    COMMAND "${BASH_TOOL}" ${CONFIG_DIR}/memcalc.bash ${PROJECT_NAME}.readelf "262144" "65536" "0" "536838144" #Flash 0x40000   SRAM 0x10000   Flash start 0x00000000   SRAM start 0x1FFF8000
    DEPENDS "${PROJECT_NAME}.elf"
)

I brought in some Arduino code for talking to an MPU6050 (BSD/MIT license), used a couple of global classes, and was talking to two gyros in a few hours after mucking with the PSOC5LP i2c library. I hate I2c. The protocol makes no sense. However, I made it work. I had to modify the Cypress code to avoid a couple of lockups when errors happen. Ugh.

So, the project now is using Free-RTOS (see an earlier post). It is communicating with a debug terminal over KitProg3 simultaneously with debugging the code. It also takes commands over USB if necessary, and sends data to a remote device.

The whole thing took about 14 days to make work, once the KitProg3 setup started working. That speed was in large part due to the fact the whole environment was borrowed from another project. Free-RTOS allowed me to modularize my code rather successfully. No re-write of previously working code.

VSCode native to the mac allowed the debug-modify-debug cycle to take tens of seconds rather than tens of minutes.

Enjoy!

2 Comments

Add a Comment

Your email address will not be published. Required fields are marked *