Skip to content

Commit

Permalink
WP test app over USB CDC (#65)
Browse files Browse the repository at this point in the history
* WP test app over USB CDC

- add test app for Wire Protocol over USB CDC
- tested in STM32F4 DISCOVERY4 board
- addresses #8

* Code corrections per reviewer request

Signed-off-by: Stanislav Šimíček <[email protected]> on behalf of José Simões <[email protected]>
  • Loading branch information
josesimoes authored and cw2 committed Jan 18, 2017
1 parent 9ce0847 commit 05bd72c
Show file tree
Hide file tree
Showing 13 changed files with 2,627 additions and 0 deletions.
73 changes: 73 additions & 0 deletions targets/os/chibios/nanoBooter-cdc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
cmake_minimum_required(VERSION 3.0)
ENABLE_LANGUAGE(ASM)

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
)

find_package(CHIBIOS REQUIRED)
include_directories(${CHIBIOS_INCLUDE_DIRS})


# pass the RTOS option to the compiler flags
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CHIBIOS_COMPILE_OPTIONS} ")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CHIBIOS_COMPILE_OPTIONS} ")

set(PROJECT_SOURCES
# test app
main.c
usbcfg.c

# required files to add Wire Protocol
WireProtocol_Receiver.c
WireProtocol_Commands.c
)

add_executable(
# executables for project, project sources
${CMAKE_PROJECT_NAME} ${PROJECT_SOURCES}

# sources for ChibiOS
${CHIBIOS_SOURCES}
)

CHIBIOS_SET_LINKER_OPTIONS(${CMAKE_PROJECT_NAME})

CHIBIOS_ADD_HEX_BIN_DUMP_TARGETS(${CMAKE_PROJECT_NAME})

CHIBIOS_PRINT_SIZE_OF_TARGETS(${CMAKE_PROJECT_NAME})

# generate map file
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.map,--library-path=${PROJECT_BINARY_DIR}/ChibiOS_Source/common/ports/ARMCMx/compilers/GCC,--defsym=__main_stack_size__=0x400,--defsym=__process_stack_size__=0x400")

set(HEX_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.hex)
set(S19_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.s19)
set(BIN_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.bin)
set(DUMP_FILE ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}.lst)

# generate HEX, BIN and LST files as needed
if(CMAKE_BUILD_TYPE EQUAL "Release")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
# copy target image to other formats
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Osrec $<TARGET_FILE:${PROJECT_NAME}> ${S19_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}> ${BIN_FILE}

# copy target file to build folder (this is only usefull for debugging in VS Code because of path in launch.json)
COMMAND ${CMAKE_OBJCOPY} $<TARGET_FILE:${PROJECT_NAME}> ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}

COMMENT "Generate HEX and BIN files fro deployment")
else()
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
# copy target image to other formats
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Osrec $<TARGET_FILE:${PROJECT_NAME}> ${S19_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}> ${BIN_FILE}

# copy target file to build folder (this is only usefull for debugging in VS Code because of path in launch.json)
COMMAND ${CMAKE_OBJCOPY} $<TARGET_FILE:${PROJECT_NAME}> ${PROJECT_SOURCE_DIR}/build/${PROJECT_NAME}

# dump target image as source code listing
COMMAND ${CMAKE_OBJDUMP} -d -EL -S $<TARGET_FILE:${PROJECT_NAME}> > ${DUMP_FILE}
COMMENT "Generate HEX and BIN files for deployment, LST file for debug")
endif()
16 changes: 16 additions & 0 deletions targets/os/chibios/nanoBooter-cdc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
For the ChibiOS test app to build succesfully the following changes are required:

In _halconf.g_ (when compared with a default file)
- HAL_USE_SERIAL to TRUE
- HAL_USE_SERIAL_USB to TRUE
- HAL_USE_USB to TRUE
- SERIAL_DEFAULT_BITRATE to 115200

In _mcuconf.h_ (when compared with a default file)
- STM32_SERIAL_USE_USART2 to TRUE
- STM32_USB_USE_OTG1 to TRUE

When making the build the first time it will fail with an error about a duplicate definition of __dso_handle.
Edit the file _../various/cpp_wrappers/syscalls_cpp.hpp_ located in the ChibiOS source folder and comment the line where it's being defined there.

NOTE: this configuration was sucessfully tested in a ST_STM32F4_DISCOVERY board using the Serial over USB connection on USB port 1 that creates a virtual COM port.
74 changes: 74 additions & 0 deletions targets/os/chibios/nanoBooter-cdc/WireProtocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// Copyright (c) 2017 The nano Framework project contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//

#ifndef _WIREPROTOCOL_H_
#define _WIREPROTOCOL_H_

#include <ch.h>

#define MARKER_DEBUGGER_V1 "MSdbgV1" // Used to identify the debugger at boot time.
#define MARKER_PACKET_V1 "MSpktV1" // Used to identify the start of a packet.

struct WP_Packet;
struct WP_Message;

// enum with Wire Protocol flags
// backwards compatible with .NETMF
typedef enum WP_Flags
{
WP_Flags_c_NonCritical = 0x0001, // This doesn't need an acknowledge.
WP_Flags_c_Reply = 0x0002, // This is the result of a command.
WP_Flags_c_BadHeader = 0x0004,
WP_Flags_c_BadPayload = 0x0008,
WP_Flags_c_Spare0010 = 0x0010,
WP_Flags_c_Spare0020 = 0x0020,
WP_Flags_c_Spare0040 = 0x0040,
WP_Flags_c_Spare0080 = 0x0080,
WP_Flags_c_Spare0100 = 0x0100,
WP_Flags_c_Spare0200 = 0x0200,
WP_Flags_c_Spare0400 = 0x0400,
WP_Flags_c_Spare0800 = 0x0800,
WP_Flags_c_Spare1000 = 0x1000,
WP_Flags_c_NoCaching = 0x2000,
WP_Flags_c_NACK = 0x4000,
WP_Flags_c_ACK = 0x8000
}WP_Flags;

// structure for Wire Protocol packet
// backwards compatible with .NETMF
typedef struct WP_Packet
{
uint8_t m_signature[8];
uint32_t m_crcHeader;
uint32_t m_crcData;

uint32_t m_cmd;
uint16_t m_seq;
uint16_t m_seqReply;
uint32_t m_flags;
uint32_t m_size;
}WP_Packet;

// structure for Wire Protocol message
// backwards compatible with .NETMF equivalent in names and types to help code reuse
typedef struct WP_Message
{
WP_Packet m_header;
uint8_t* m_payload;

uint8_t* m_pos;
uint16_t m_size;
ReceiveState m_rxState;
}WP_Message;


// This structure is never used, its purpose is to generate a compiler error in case the size of any structure changes.
struct WP_CompileCheck
{
char buf1[ sizeof(WP_Packet) == 8 * 4 ? 1 : -1 ];
};

#endif // _WIREPROTOCOL_H_
57 changes: 57 additions & 0 deletions targets/os/chibios/nanoBooter-cdc/WireProtocol_Commands.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Copyright (c) 2017 The nano Framework project contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//


#include "WireProtocol_Commands.h"

// this one is implemented at WireProtocol_Receiver.c
extern void ReplyToCommand(WP_Message* message, bool fSuccess, bool fCritical, void* ptr, int size);


bool Monitor_Ping(WP_Message* message)
{
if((message->m_header.m_flags & WP_Flags_c_Reply) == 0)
{
Monitor_Ping_Reply cmdReply;
// TODO
// set flag with NanoBooter or NanoCLR according to the thread we are running
cmdReply.m_source = Monitor_Ping_c_Ping_Source_NanoBooter;

ReplyToCommand(message, true, false, &cmdReply, sizeof(cmdReply));
}

return true;
}

bool Monitor_OemInfo(WP_Message* message)
{
if((message->m_header.m_flags & WP_Flags_c_Reply ) == 0)
{
Monitor_OemInfo_Reply cmdReply;

bool fOK = NanoBooter_GetReleaseInfo(&cmdReply.m_releaseInfo) == true;

ReplyToCommand(message, fOK, false, &cmdReply, sizeof(cmdReply) );
}

return true;
}

//////////////////////////////////////////////////////////////////////
// helper functions

bool NanoBooter_GetReleaseInfo(ReleaseInfo* releaseInfo)
{
releaseInfo->version.usMajor = 0;
releaseInfo->version.usMinor = 0;
releaseInfo->version.usBuild = 12345;
releaseInfo->version.usRevision = 1;

// TODO replace this with string from (possibly...) main config file
memcpy(&releaseInfo->infoString, ">>>> nanoFramework RULES <<<<", sizeof(releaseInfo->infoString));

return true;
}
101 changes: 101 additions & 0 deletions targets/os/chibios/nanoBooter-cdc/WireProtocol_Commands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// Copyright (c) 2017 The nano Framework project contributors
// Portions Copyright (c) Microsoft Corporation. All rights reserved.
// See LICENSE file in the project root for full license information.
//

#ifndef _WIREPROTOCOL_COMMANDS_H_
#define _WIREPROTOCOL_COMMANDS_H_

#include <string.h>
#include "WireProtocol.h"

//////////////////////////////////////////
// enums

// enum with CLR and debugger commands
// backwards compatible with .NETMF
typedef enum CLR_DBG_Commands
{
CLR_DBG_Commands_c_Monitor_Ping = 0x00000000, // The payload is empty, this command is used to let the other side know we are here...
CLR_DBG_Commands_c_Monitor_Message = 0x00000001, // The payload is composed of the string characters, no zero at the end.
CLR_DBG_Commands_c_Monitor_ReadMemory = 0x00000002,
CLR_DBG_Commands_c_Monitor_WriteMemory = 0x00000003,
CLR_DBG_Commands_c_Monitor_CheckMemory = 0x00000004,
CLR_DBG_Commands_c_Monitor_EraseMemory = 0x00000005,
CLR_DBG_Commands_c_Monitor_Execute = 0x00000006,
CLR_DBG_Commands_c_Monitor_Reboot = 0x00000007,
CLR_DBG_Commands_c_Monitor_MemoryMap = 0x00000008,
CLR_DBG_Commands_c_Monitor_ProgramExit = 0x00000009, // The payload is empty, this command is used to tell the PC of a program termination
CLR_DBG_Commands_c_Monitor_CheckSignature = 0x0000000A,
CLR_DBG_Commands_c_Monitor_DeploymentMap = 0x0000000B,
CLR_DBG_Commands_c_Monitor_FlashSectorMap = 0x0000000C,
CLR_DBG_Commands_c_Monitor_SignatureKeyUpdate = 0x0000000D,
CLR_DBG_Commands_c_Monitor_OemInfo = 0x0000000E,
}CLR_DBG_Commands;

// enum with flags for Monitor ping source and debugger flags
// backwards compatible with .NETMF in debugger flags only
// adds NEW flags for nanoBooter and nanoCLR
typedef enum Monitor_Ping_Source_Flags
{
Monitor_Ping_c_Ping_Source_NanoCLR = 0x00010000,
Monitor_Ping_c_Ping_Source_NanoBooter = 0x00010001,

Monitor_Ping_c_Ping_DbgFlag_Stop = 0x00000001,
Monitor_Ping_c_Ping_DbgFlag_AppExit = 0x00000004,
}Monitor_Ping_Source_Flags;

//////////////////////////////////////////
// typedefs

// structure for Monitor Ping Reply
// backwards compatible with .NETMF
typedef struct Monitor_Ping_Reply
{
uint32_t m_source;
uint32_t m_dbg_flags;

}Monitor_Ping_Reply;

// structure to hold 'standard' version information
// equivalent with .NETMF MFVersion
typedef struct VersionInfo
{
uint16_t usMajor;
uint16_t usMinor;
uint16_t usBuild;
uint16_t usRevision;

}VersionInfo;

// structure to hold nano Framework release information
// equivalent with .NETMF MfReleaseInfo
typedef struct ReleaseInfo
{

VersionInfo version;

uint8_t infoString[64-sizeof(VersionInfo)];

}ReleaseInfo;

// structure with reply for OEM information command
typedef struct Monitor_OemInfo_Reply
{

ReleaseInfo m_releaseInfo;

}Monitor_OemInfo_Reply;

//////////////////////////////////////////
// function declarations (commands)

bool Monitor_Ping(WP_Message* message);
bool Monitor_OemInfo(WP_Message* message);

//////////////////////////////////////////
// helper functions
bool NanoBooter_GetReleaseInfo(ReleaseInfo* releaseInfo);

#endif //_WIREPROTOCOL_COMMANDS_H_
Loading

0 comments on commit 05bd72c

Please sign in to comment.