Skip to content

Commit

Permalink
Add support for compilation on Windows (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtuchkin committed Jan 15, 2017
1 parent 3c2581e commit 3cb6b1a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
22 changes: 19 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ as Mavlink ATT_POS_MOCAP message to UART port (see mavlink.cpp).
NOTE: Currently, base station positions and direction matrices are hardcoded in geometry.cpp (`lightsources`). You'll need to
adjust it for your setup. See [#2](//github.com/ashtuchkin/vive-diy-position-sensor/issues/2).

### Installation instructions
### Installation on macOS, Linux

Prerequisites:
* [gcc-arm-embedded](https://launchpad.net/gcc-arm-embedded) toolchain. Can be installed on Mac with `brew cask install gcc-arm-embedded`.
* [GNU ARM Embedded toolchain](https://launchpad.net/gcc-arm-embedded). Can be installed on Mac with `brew cask install gcc-arm-embedded`.
I'm developing with version 5_4-2016q3, but other versions should work too.
* CMake 3.5+ `brew install cmake`
* Command line uploader/monitor: [ty](https://github.com/Koromix/ty). See build instructions in the repo.
Expand All @@ -158,4 +158,20 @@ $ make vive-diy-position-sensor_Upload # Upload to Teensy
$ tyc monitor # Serial console to Teensy
```

## Discussions
### Installation on Windows

I haven't been able to make it work in Visual Studio, so providing command line build solution.

Prerequisites:
* [GNU ARM Embedded toolchain](https://developer.arm.com/open-source/gnu-toolchain/gnu-rm). Windows 32 bit installer is fine.
* [CMake 3.5+](https://cmake.org/download/). Make sure it's in your %PATH%.
* [Ninja build tool](https://ninja-build.org/). Copy binary in any location in your %PATH%.

Getting the code is the same as above. GitHub client for Windows will make it even easier.

Building firmware:
```
cd build
cmake -G Ninja ..
ninja # Build firmware. Will generate "vive-diy-position-sensor.hex" in current directory.
```
61 changes: 41 additions & 20 deletions teensy-arm.toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,38 @@
# * https://github.com/xya/teensy-cmake
# * http:https://playground.arduino.cc/Code/CmakeBuild

# Toolchain definition
set(TOOLCHAIN_PREFIX "/usr/local/bin/arm-none-eabi-")

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_CROSSCOMPILING 1)

set(CMAKE_C_COMPILER "${TOOLCHAIN_PREFIX}gcc" CACHE PATH "gcc" FORCE)
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PREFIX}g++" CACHE PATH "g++" FORCE)
set(CMAKE_AR "${TOOLCHAIN_PREFIX}ar" CACHE PATH "ar" FORCE)
set(CMAKE_LINKER "${TOOLCHAIN_PREFIX}ld" CACHE PATH "ld" FORCE)
set(CMAKE_NM "${TOOLCHAIN_PREFIX}nm" CACHE PATH "nm" FORCE)
set(CMAKE_OBJCOPY "${TOOLCHAIN_PREFIX}objcopy" CACHE PATH "objcopy" FORCE)
set(CMAKE_OBJDUMP "${TOOLCHAIN_PREFIX}objdump" CACHE PATH "objdump" FORCE)
set(CMAKE_STRIP "${TOOLCHAIN_PREFIX}strip" CACHE PATH "strip" FORCE)
set(CMAKE_PRINT_SIZE "${TOOLCHAIN_PREFIX}size" CACHE PATH "size" FORCE)
set(CMAKE_RANLIB "${TOOLCHAIN_PREFIX}ranlib" CACHE PATH "ranlib" FORCE)
set(TOOLCHAIN_TRIPLE "arm-none-eabi-" CACHE STRING "Triple prefix for arm crosscompiling tools")
if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
set(TOOLCHAIN_SUFFIX ".exe" CACHE STRING "Toolchain executable file extension")
else()
set(TOOLCHAIN_SUFFIX "" CACHE STRING "Toolchain executable file extension")
endif()

# Search default paths for the GNU ARM Embedded Toolchain (https://developer.arm.com/open-source/gnu-toolchain/gnu-rm)
# This will need to be changed if you have it in a different directory.
find_path(TOOLCHAIN_BIN_PATH "${TOOLCHAIN_TRIPLE}gcc${TOOLCHAIN_SUFFIX}"
PATHS "/usr/local/bin" # Linux, Mac
PATHS "C:/Program Files (x86)/GNU Tools ARM Embedded/*/bin" # Default installation directory on Windows
DOC "Toolchain binaries directory")

if(NOT TOOLCHAIN_BIN_PATH)
message(FATAL_ERROR "GNU ARM Embedded Toolchain not found. Please install it and provide its /bin directory as a TOOLCHAIN_BIN_PATH variable.")
endif()

set(CMAKE_C_COMPILER "${TOOLCHAIN_BIN_PATH}/${TOOLCHAIN_TRIPLE}gcc${TOOLCHAIN_SUFFIX}" CACHE PATH "gcc")
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_BIN_PATH}/${TOOLCHAIN_TRIPLE}g++${TOOLCHAIN_SUFFIX}" CACHE PATH "g++")
set(CMAKE_AR "${TOOLCHAIN_BIN_PATH}/${TOOLCHAIN_TRIPLE}ar${TOOLCHAIN_SUFFIX}" CACHE PATH "ar")
set(CMAKE_LINKER "${TOOLCHAIN_BIN_PATH}/${TOOLCHAIN_TRIPLE}ld${TOOLCHAIN_SUFFIX}" CACHE PATH "ld")
set(CMAKE_NM "${TOOLCHAIN_BIN_PATH}/${TOOLCHAIN_TRIPLE}nm${TOOLCHAIN_SUFFIX}" CACHE PATH "nm")
set(CMAKE_OBJCOPY "${TOOLCHAIN_BIN_PATH}/${TOOLCHAIN_TRIPLE}objcopy${TOOLCHAIN_SUFFIX}" CACHE PATH "objcopy")
set(CMAKE_OBJDUMP "${TOOLCHAIN_BIN_PATH}/${TOOLCHAIN_TRIPLE}objdump${TOOLCHAIN_SUFFIX}" CACHE PATH "objdump")
set(CMAKE_STRIP "${TOOLCHAIN_BIN_PATH}/${TOOLCHAIN_TRIPLE}strip${TOOLCHAIN_SUFFIX}" CACHE PATH "strip")
set(CMAKE_PRINT_SIZE "${TOOLCHAIN_BIN_PATH}/${TOOLCHAIN_TRIPLE}size${TOOLCHAIN_SUFFIX}" CACHE PATH "size")
set(CMAKE_RANLIB "${TOOLCHAIN_BIN_PATH}/${TOOLCHAIN_TRIPLE}ranlib${TOOLCHAIN_SUFFIX}" CACHE PATH "ranlib")


# Teensy 3.1 settings
Expand Down Expand Up @@ -66,10 +81,6 @@ add_definitions("-DF_CPU=${TEENSY_FREQUENCY}000000")
add_definitions("-DLAYOUT_US_ENGLISH")
add_definitions("-MMD")


# See https://github.com/Koromix/ty
set(TY_EXECUTABLE "/usr/local/bin/tyc" CACHE FILEPATH "Path to the 'tyc' executable that can upload programs to the Teensy")

# Define target for the Teensy 'core' library.
# CMake does a lot of compiler checks which include toolchain file, so be careful to define it only once and only
# for the actual source (not sample programs).
Expand All @@ -89,21 +100,31 @@ function(add_firmware_targets TARGET_NAME)
# Generate the hex firmware files that can be flashed to the MCU.
set(EEPROM_OPTS -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0)
set(HEX_OPTS -O ihex -R .eeprom)

if(NOT (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows"))
set(PROCESS_SIZE_CMD_OUTPUT | tail -1 | xargs bash -c [[ TEXT=$0\; DATA=$1\; BSS=$2\; TOTAL_FLASH=262144\; TOTAL_RAM=65536\; FLASH=$((TEXT+DATA))\; RAM=$((DATA+BSS))\; echo "FLASH: $FLASH ($((FLASH*100/TOTAL_FLASH))%), RAM: $RAM ($((RAM*100/TOTAL_RAM))%), Free RAM: $((TOTAL_RAM-RAM))" ]])
endif()

add_custom_target(${TARGET_NAME}_Firmware ALL
COMMENT "Firmware size:"
COMMAND ${CMAKE_PRINT_SIZE} $<TARGET_FILE_NAME:${TARGET_NAME}> | tail -1 | xargs bash -c "TEXT=$0; DATA=$1; BSS=$2; TOTAL_FLASH=262144; TOTAL_RAM=65536; FLASH=$((TEXT+DATA)); RAM=$((DATA+BSS)); echo \"FLASH: $FLASH ($((FLASH*100/TOTAL_FLASH))%); RAM: $RAM ($((RAM*100/TOTAL_RAM))%); Free RAM: $((TOTAL_RAM-RAM))\";"
COMMAND ${CMAKE_PRINT_SIZE} $<TARGET_FILE_NAME:${TARGET_NAME}> ${PROCESS_SIZE_CMD_OUTPUT}
COMMAND ${CMAKE_OBJCOPY} ${EEPROM_OPTS} $<TARGET_FILE:${TARGET_NAME}> ${TARGET_NAME}.eep
COMMAND ${CMAKE_OBJCOPY} ${HEX_OPTS} $<TARGET_FILE:${TARGET_NAME}> ${TARGET_NAME}.hex
VERBATIM)

add_custom_target(${TARGET_NAME}_Assembler
COMMAND ${CMAKE_OBJDUMP} -d $<TARGET_FILE_NAME:${TARGET_NAME}> > ${CMAKE_SOURCE_DIR}/build/source.S )

if(EXISTS "${TY_EXECUTABLE}")
# See https://github.com/Koromix/ty
find_file(TY_EXECUTABLE tyc
PATHS "/usr/local/bin"
DOC "Path to the 'tyc' executable that can upload programs to the Teensy")

if(TY_EXECUTABLE)
add_custom_target(${TARGET_NAME}_Upload
COMMAND "${TY_EXECUTABLE}" upload ${TARGET_NAME}.hex)
add_dependencies(${TARGET_NAME}_Upload ${TARGET_NAME}_Firmware)
endif(EXISTS "${TY_EXECUTABLE}")
endif()
endfunction(add_firmware_targets)


Expand Down

0 comments on commit 3cb6b1a

Please sign in to comment.