Skip to content

Commit

Permalink
First openmv version with 8.2.0 toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
hukaisunny committed Dec 11, 2018
1 parent ef6ab6d commit cb85cef
Show file tree
Hide file tree
Showing 76 changed files with 3,511 additions and 558 deletions.
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ set(BUILDING_SDK "yes" CACHE INTERNAL "")

# basic config
if (NOT PROJ)
message(FATAL_ERROR "PROJ must be set. e.g. -DPROJ=hello_world")
get_filename_component(PROJ ${CMAKE_CURRENT_BINARY_DIR} DIRECTORY)
get_filename_component(PROJ ${PROJ} NAME)
string(REPLACE " " "_" PROJ ${PROJ})
message(STATUS "PROJ not set, use ${PROJ} as PROJ. Also, you can set it manually. e.g. -DPROJ=hello_world")
else()
message("PROJ = ${PROJ}")
endif ()
cmake_minimum_required(VERSION 3.0)
include(./cmake/common.cmake)
Expand All @@ -17,10 +22,8 @@ header_directories(${SDK_ROOT}/lib)
# build library first
add_subdirectory(lib)

# TODO
#add_subdirectory(third_party)

# compile project
#add_source_files(src/${PROJ}/*.c src/${PROJ}/*.s src/${PROJ}/*.S src/${PROJ}/*.cpp)
add_custom_target(build_metis ALL
COMMAND ${CMAKE_MAKE_PROGRAM}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/openmv/src/micropython/ports/k210-standalone
Expand Down
14 changes: 14 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* Copyright 2018 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Kendryte-standalone-opemv build steps:
1. download V8.2.0 toolchain from https://kendryte.com/downloads/
2. export PATH=/opt/maix-toolchain/bin/:$PATH (riscv toolchain path for micropython build)
3. mkdir build
4. cd build
5. cmake .. -DPROJ=openmv -DTOOLCHAIN=/opt/maix-toolchain/bin (riscv toolchain path for kendryte sdk build)
6. make

The default example is face detect, you can change src/openmv/src/micropython/ports/k210-standalone/buildin-py/boot.py to implement other openmv functions.
2 changes: 1 addition & 1 deletion cmake/common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ IF (CMAKE_BUILD_TYPE STREQUAL Debug)
ENDIF ()

# definitions in macros
add_definitions(-DCONFIG_LOG_LEVEL=LOG_VERBOSE -DCONFIG_LOG_ENABLE -DCONFIG_LOG_COLORS -DLOG_KERNEL -D__riscv64 -DFPGA_PLL)
add_definitions(-DCONFIG_LOG_LEVEL=LOG_VERBOSE -DCONFIG_LOG_ENABLE -DCONFIG_LOG_COLORS -DLOG_KERNEL -D__riscv64)

if (NOT SDK_ROOT)
get_filename_component(_SDK_ROOT ${CMAKE_CURRENT_LIST_DIR} DIRECTORY)
Expand Down
16 changes: 12 additions & 4 deletions cmake/compile-flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ add_compile_flags(BOTH
-fdata-sections
-fstrict-volatile-bitfields
-fno-zero-initialized-in-bss
-Os
-Os
-ffloat-store
-ggdb
)
)

#hukai
#add_compile_flags(C
# -fstack-protector
# -fstack-protector-all
# -fstack-usage
# )

add_compile_flags(C -std=gnu11)
add_compile_flags(C -std=gnu11 -Wno-pointer-to-int-cast)
add_compile_flags(CXX -std=gnu++17)

if (BUILDING_SDK)
Expand All @@ -42,9 +50,9 @@ if (BUILDING_SDK)
-Wno-error=return-type
-Wno-error=pointer-sign
-Wno-missing-braces
-Wno-pointer-to-int-cast
-Wno-strict-aliasing
-Wno-implicit-fallthrough
-Wno-missing-field-initializers
)

add_compile_flags(C -Wno-old-style-declaration)
Expand Down
11 changes: 10 additions & 1 deletion cmake/dump-config.cmake
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
message("")
message("Project: ${PROJECT_NAME}")
message(" LIST_FILE=${CMAKE_PARENT_LIST_FILE}")
message(" TOOLCHAIN=${TOOLCHAIN}")
message(" KENDRYTE IDE=${KENDRYTE_IDE}")
message(" KENDRYTE_IDE=${KENDRYTE_IDE}")
message(" BUILDING_SDK=${BUILDING_SDK}")
message("")
message(" CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}")
message(" CMAKE_C_COMPILER=${CMAKE_C_COMPILER}")
message(" CMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}")
message(" CMAKE_LINKER=${CMAKE_LINKER}")
message(" CMAKE_OBJCOPY=${CMAKE_OBJCOPY}")
message(" CMAKE_OBJDUMP=${CMAKE_OBJDUMP}")
message(" CMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}")
message("")
message(" CMAKE_C_FLAGS=${CMAKE_C_FLAGS}")
message(" CMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}")
message(" LDFLAGS=${LDFLAGS}")
message(" CMAKE_BINARY_DIR=${CMAKE_BINARY_DIR}")
message("Makefile created.")
message("")
message("")
21 changes: 12 additions & 9 deletions cmake/executable.cmake
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
if (NOT BUILDING_SDK)
add_library(kendryte STATIC IMPORTED)
set_property(TARGET kendryte PROPERTY IMPORTED_LOCATION ${SDK_ROOT}/libmaix.a)
set_property(TARGET kendryte PROPERTY IMPORTED_LOCATION ${SDK_ROOT}/libkendryte.a)
include_directories(${SDK_ROOT}/include/)
endif ()

removeDuplicateSubstring(${CMAKE_C_FLAGS} CMAKE_C_FLAGS)
removeDuplicateSubstring(${CMAKE_CXX_FLAGS} CMAKE_CXX_FLAGS)

message("SOURCE_FILES=${SOURCE_FILES}")
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
# add_dependencies(${PROJECT_NAME} kendryte) # TODO: third_party
# target_link_libraries(${PROJECT_NAME} kendryte) # TODO: third_party
# link_directories(${CMAKE_BINARY_DIR})

target_link_libraries(${PROJECT_NAME}
${SDK_ROOT}/src/openmv/src/micropython/ports/k210-standalone/micropython.a
)

set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C)

target_link_libraries(${PROJECT_NAME}
-Wl,--start-group
gcc m c kendryte
gcc m c
-Wl,--whole-archive
kendryte
-Wl,--no-whole-archive
-Wl,--end-group
)

IF(SUFFIX)
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SUFFIX ${SUFFIX})
ENDIF()

#message("CMAKE_OBJCOPY=${CMAKE_OBJCOPY}")

# Build target
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_OBJCOPY} --output-format=binary ${CMAKE_BINARY_DIR}/${PROJECT_NAME}${SUFFIX} ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.bin
DEPENDS ${PROJECT_NAME}
COMMENT "Generating .bin file ...")


add_custom_target(firmware DEPENDS ${PROJECT_NAME}.firmware.bin)

# show information
include(${CMAKE_CURRENT_LIST_DIR}/dump-config.cmake)
7 changes: 7 additions & 0 deletions cmake/macros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ global_set(CMAKE_EXE_LINKER_FLAGS "")
global_set(CMAKE_SHARED_LINKER_FLAGS "")
global_set(CMAKE_MODULE_LINKER_FLAGS "")

function(removeDuplicateSubstring stringIn stringOut)
separate_arguments(stringIn)
list(REMOVE_DUPLICATES stringIn)
string(REPLACE ";" " " stringIn "${stringIn}")
set(${stringOut} "${stringIn}" PARENT_SCOPE)
endfunction()

macro(add_compile_flags WHERE)
JOIN("${ARGN}" " " STRING_ARGS)
if (${WHERE} STREQUAL C)
Expand Down
44 changes: 33 additions & 11 deletions cmake/toolchain.cmake
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
if (NOT TOOLCHAIN)
message(FATAL_ERROR "TOOLCHAIN must be set, to absolute path of kendryte-toolchain dist/bin folder.")
endif ()

if (WIN32)
set(EXT ".exe")
else ()
set(EXT "")
endif ()

condition_set(CMAKE_C_COMPILER "${TOOLCHAIN}/riscv64-unknown-elf-gcc${EXT}" CACHE INTERNAL "")
condition_set(CMAKE_CXX_COMPILER "${TOOLCHAIN}/riscv64-unknown-elf-g++${EXT}" CACHE INTERNAL "")
condition_set(CMAKE_LINKER "${TOOLCHAIN}/riscv64-unknown-elf-ld${EXT}" CACHE INTERNAL "")
condition_set(CMAKE_AR "${TOOLCHAIN}/riscv64-unknown-elf-ar${EXT}" CACHE INTERNAL "")
condition_set(CMAKE_CXX_COMPILER_AR "${CMAKE_AR}${EXT}" CACHE INTERNAL "")
condition_set(CMAKE_C_COMPILER_AR "${CMAKE_AR}${EXT}" CACHE INTERNAL "")
condition_set(CMAKE_OBJCOPY "${TOOLCHAIN}/riscv64-unknown-elf-objcopy${EXT}" CACHE INTERNAL "")
message(STATUS "Check for RISCV toolchain ...")
if(NOT TOOLCHAIN)
find_path(_TOOLCHAIN riscv64-unknown-elf-gcc${EXT})
global_set(TOOLCHAIN "${_TOOLCHAIN}")
elseif(NOT "${TOOLCHAIN}" MATCHES "/$")
global_set(TOOLCHAIN "${TOOLCHAIN}")
endif()

if (NOT TOOLCHAIN)
message(FATAL_ERROR "TOOLCHAIN must be set, to absolute path of kendryte-toolchain dist/bin folder.")
endif ()

message(STATUS "Using ${TOOLCHAIN} RISCV toolchain")

global_set(CMAKE_C_COMPILER "${TOOLCHAIN}/riscv64-unknown-elf-gcc${EXT}")
global_set(CMAKE_CXX_COMPILER "${TOOLCHAIN}/riscv64-unknown-elf-g++${EXT}")
global_set(CMAKE_LINKER "${TOOLCHAIN}/riscv64-unknown-elf-ld${EXT}")
global_set(CMAKE_AR "${TOOLCHAIN}/riscv64-unknown-elf-ar${EXT}")
global_set(CMAKE_OBJCOPY "${TOOLCHAIN}/riscv64-unknown-elf-objcopy${EXT}")
global_set(CMAKE_SIZE "${TOOLCHAIN}/riscv64-unknown-elf-size${EXT}")
global_set(CMAKE_OBJDUMP "${TOOLCHAIN}/riscv64-unknown-elf-objdump${EXT}")

execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crt0.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRT0_OBJ)
execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtbegin.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTBEGIN_OBJ)
execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtend.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTEND_OBJ)
execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crti.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTI_OBJ)
execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtn.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTN_OBJ)

global_set(CMAKE_C_LINK_EXECUTABLE
"<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> \"${CRTI_OBJ}\" \"${CRTBEGIN_OBJ}\" <OBJECTS> \"${CRTEND_OBJ}\" \"${CRTN_OBJ}\" -o <TARGET> <LINK_LIBRARIES>")

global_set(CMAKE_CXX_LINK_EXECUTABLE
"<CMAKE_CXX_COMPILER> <FLAGS> <CMAKE_CXX_LINK_FLAGS> <LINK_FLAGS> \"${CRTI_OBJ}\" \"${CRTBEGIN_OBJ}\" <OBJECTS> \"${CRTEND_OBJ}\" \"${CRTN_OBJ}\" -o <TARGET> <LINK_LIBRARIES>")

get_filename_component(_BIN_DIR "${CMAKE_C_COMPILER}" DIRECTORY)
if (NOT "${TOOLCHAIN}" STREQUAL "${_BIN_DIR}")
Expand Down
6 changes: 4 additions & 2 deletions lds/kendryte.ld
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ MEMORY
* Memory with CPU cache.
*6M CPU SRAM
*/
ram (wxa!ri) : ORIGIN = 0x80000000, LENGTH = (6 * 1024 * 1024)
/*ram (wxa!ri) : ORIGIN = 0x80000000, LENGTH = (6 * 1024 * 1024)*/
ram (wxa!ri) : ORIGIN = 0x80000000, LENGTH = (6 * 1024 * 1024)
/*
* Memory without CPU cache
* 6M CPU SRAM
Expand All @@ -23,7 +24,7 @@ PROVIDE( _ram_start = ORIGIN(ram) );
PROVIDE( _ram_end = ORIGIN(ram) + LENGTH(ram) );
PROVIDE( _io_start = 0x40000000 );
PROVIDE( _io_end = _io_start + LENGTH(ram) );
PROVIDE( _stack_size = 1 << 17 );
PROVIDE( _stack_size = 1 << 16 );


/*
Expand Down Expand Up @@ -244,6 +245,7 @@ SECTIONS
PROVIDE( _tp1 = _tp0 + _stack_size );
PROVIDE( _sp0 = _tp0 + _stack_size );
PROVIDE( _sp1 = _tp1 + _stack_size );

/* Heap end is at the end of memory, the memory size can set in kconfig */
PROVIDE( _heap_end = _ram_end );
}
Expand Down
8 changes: 1 addition & 7 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@ FILE(GLOB_RECURSE ASSEMBLY_FILES
"${CMAKE_CURRENT_LIST_DIR}/*.S"
)

include_directories(${CMAKE_CURRENT_LIST_DIR}/drivers/include ${CMAKE_CURRENT_LIST_DIR}/bsp/include ${CMAKE_CURRENT_LIST_DIR}/firmware/include)
#
#HEADER_DIRECTORIES(LIB_HEADERS)
#
#INCLUDE_DIRECTORIES(${LIB_HEADERS})
include_directories(${CMAKE_CURRENT_LIST_DIR}/drivers/include ${CMAKE_CURRENT_LIST_DIR}/bsp/include)

SET_PROPERTY(SOURCE ${ASSEMBLY_FILES} PROPERTY LANGUAGE C)
SET_SOURCE_FILES_PROPERTIES(${ASSEMBLY_FILES} PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp -D __riscv64")

#MESSAGE("CMAKE_C_FLAGS: ${CMAKE_C_FLAGS}")

ADD_LIBRARY(kendryte
${LIB_SRC}
)
Expand Down
18 changes: 3 additions & 15 deletions lib/bsp/crt.S
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# define LFREG fld
# define SFREG fsd
# define REGBYTES 8
# define STKSHIFT 17
# define STKSHIFT 16


.section .text.start, "ax", @progbits
Expand Down Expand Up @@ -72,14 +72,6 @@ _start:
li x30,0
li x31,0

csrr t0, misa
bltz t0, 1f
li a0, 1234
j sys_exit
1:

andi t0, t0, 1 << ('f' - 'a')
beqz t0, 1f
li t0, MSTATUS_FS
csrs mstatus, t0

Expand Down Expand Up @@ -116,18 +108,14 @@ _start:
fmv.d.x f29,x0
fmv.d.x f30,x0
fmv.d.x f31,x0

1:

.option push
.option norelax
la gp, __global_pointer$
la gp, __global_pointer$
.option pop
la tp, _end + 63
and tp, tp, -64
csrr a0, mhartid
li a1, 2

1:bgeu a0, a1, 1b

sll a2, a0, STKSHIFT
add tp, tp, a2
Expand Down
7 changes: 5 additions & 2 deletions lib/bsp/entry_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ void _init_bsp(int core_id, int number_of_cores)
extern int main(int argc, char* argv[]);
extern void __libc_init_array(void);
extern void __libc_fini_array(void);
/* Initialize thread local data */
init_tls();

if (core_id == 0)
{
Expand Down Expand Up @@ -101,3 +99,8 @@ void _init_bsp(int core_id, int number_of_cores)
exit(ret);
}

int pthread_setcancelstate(int __state, int *__oldstate)
{
return 0;
}

13 changes: 6 additions & 7 deletions lib/bsp/include/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ typedef struct _corelock
int core;
} corelock_t;


static inline int spinlock_trylock(spinlock_t *lock)
{
int res = atomic_swap(&lock->lock, -1);
Expand All @@ -83,18 +82,15 @@ static inline int spinlock_trylock(spinlock_t *lock)

static inline void spinlock_lock(spinlock_t *lock)
{
do
{
while (atomic_read(&lock->lock))
;
} while (spinlock_trylock(lock));
while (spinlock_trylock(lock));
}

static inline void spinlock_unlock(spinlock_t *lock)
{
/* Use memory barrier to keep coherency */
mb();
atomic_set(&lock->lock, 0);
asm volatile ("nop");
}

static inline void semaphore_signal(semaphore_t *semaphore, int i)
Expand Down Expand Up @@ -143,7 +139,10 @@ static inline int corelock_trylock(corelock_t *lock)

asm volatile("csrr %0, mhartid;"
: "=r"(core));
spinlock_lock(&lock->lock);
if(spinlock_trylock(&lock->lock))
{
return -1;
}

if (lock->count == 0)
{
Expand Down
Loading

0 comments on commit cb85cef

Please sign in to comment.