Initial commit

This commit is contained in:
Embedded_Projects 2024-03-08 16:05:09 +00:00
commit 11173b0dba
10 changed files with 369 additions and 0 deletions

12
.clang-format Normal file
View File

@ -0,0 +1,12 @@
BasedOnStyle: Google
IndentWidth: 4
AlignConsecutiveMacros: Consecutive
AlignConsecutiveDeclarations: Consecutive
AlignConsecutiveAssignments: Consecutive
AllowShortFunctionsOnASingleLine: None
BreakBeforeBraces: Custom
BraceWrapping:
AfterEnum: false
AfterStruct: false
SplitEmptyFunction: false
ColumnLimit: 120

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
/cmake-build-*
/build
/board/*.bak
/.vscode

83
CMakeLists.txt Normal file
View File

@ -0,0 +1,83 @@
cmake_minimum_required(VERSION 3.10)
project(t113i_c906_template)
enable_language(CXX)
enable_language(ASM)
# Different linker scripts
set(TARGET_LDSCRIPT_FLASH "${CMAKE_SOURCE_DIR}/GCC/T113i_RAM.ld")
set(TARGET_SOURCES
"startup/resource_table.c"
"startup/start_c906.S"
"startup/system_c906.c"
"src/main.c"
)
set(TARGET_C_DEFINES
)
set(TARGET_C_INCLUDES
"include"
)
# Shared libraries linked with application
set(TARGET_LIBS
"c"
"m"
"nosys"
)
# Shared library and linker script search paths
set(TARGET_LIB_DIRECTORIES
)
# Conditional flags
# DEBUG
set(CMAKE_C_FLAGS_DEBUG "-DDEBUG -O0 -g")
set(CMAKE_CXX_FLAGS_DEBUG "-DDEBUG -O0 -g")
set(CMAKE_ASM_FLAGS_DEBUG "-DDEBUG -O0 -g")
# RELEASE
set(CMAKE_C_FLAGS_RELEASE "-DNDEBUG -O2")
set(CMAKE_CXX_FLAGS_RELEASE "-DNDEBUG -O2")
set(CMAKE_ASM_FLAGS_RELEASE "-DNDEBUG -O2")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "")
# Final compiler flags
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-common -fno-builtin -ffreestanding -fdata-sections -ffunction-sections")
set(CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS} -x assembler-with-cpp")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
# Shared sources, includes and definitions
add_compile_definitions(${TARGET_C_DEFINES})
include_directories(${TARGET_C_INCLUDES})
link_directories(${TARGET_LIB_DIRECTORIES})
link_libraries(${TARGET_LIBS})
# Main targets are added here
# Create ELF
add_executable("${CMAKE_PROJECT_NAME}_FLASH.elf" ${TARGET_SOURCES})
target_compile_definitions("${CMAKE_PROJECT_NAME}_FLASH.elf"
PRIVATE ${TARGET_C_DEFINES_XIP}
)
target_link_options("${CMAKE_PROJECT_NAME}_FLASH.elf"
PRIVATE "-T${TARGET_LDSCRIPT_FLASH}"
PRIVATE "-Wl,--Map=${CMAKE_PROJECT_NAME}_FLASH.map"
)
set_property(TARGET "${CMAKE_PROJECT_NAME}_FLASH.elf" APPEND
PROPERTY ADDITIONAL_CLEAN_FILES "${CMAKE_PROJECT_NAME}_FLASH.map"
)
add_custom_command(OUTPUT "${CMAKE_PROJECT_NAME}_FLASH.hex"
COMMAND ${CMAKE_OBJCOPY} "-O" "ihex" "${CMAKE_PROJECT_NAME}_FLASH.elf" "${CMAKE_PROJECT_NAME}_FLASH.hex"
DEPENDS "${CMAKE_PROJECT_NAME}_FLASH.elf"
)
add_custom_target("${CMAKE_PROJECT_NAME}_FLASH_HEX" DEPENDS "${CMAKE_PROJECT_NAME}_FLASH.hex")
if(DEFINED TARGET_TOOLCHAIN_SIZE)
add_custom_command(TARGET "${CMAKE_PROJECT_NAME}_FLASH.elf" POST_BUILD
COMMAND ${TARGET_TOOLCHAIN_SIZE} "${CMAKE_PROJECT_NAME}_FLASH.elf"
)
endif()

149
GCC/T113i_RAM.ld Normal file
View File

@ -0,0 +1,149 @@
ENTRY( _start )
/* Configure heap and stack space reserved for application.
* Too low may cause standard library failed,
* too high may cause SDRAM overflow.
* Collision may occur with heap when stack gets too deep.
*/
__stack_size = 2048;
__heap_size = 2048;
MEMORY
{
SRAM (xrw) : ORIGIN = 0x00020000, LENGTH = 8k
SDRAM (xrw) : ORIGIN = 0x4f800000, LENGTH = 8M
}
SECTIONS
{
.resource_table :
{
. = ALIGN(4);
KEEP(*(SORT_NONE(.resource_table)))
. = ALIGN(4);
} >SDRAM
.init :
{
. = ALIGN(4);
KEEP(*(SORT_NONE(.init)))
. = ALIGN(4);
} >SDRAM
.vectors :
{
. = ALIGN(4);
KEEP(*(SORT_NONE(.vectors)))
. = ALIGN(4);
} >SDRAM
.text :
{
. = ALIGN(4);
_stext = .;
*(.text)
*(.text.*)
*(.gnu.linkonce.t.*)
*(.rodata)
*(.rodata*)
*(.gnu.linkonce.r.*)
. = ALIGN(4);
_etext = .;
} >SDRAM
.preinit_array :
{
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP (*(.preinit_array))
PROVIDE_HIDDEN (__preinit_array_end = .);
} >SDRAM
.init_array :
{
PROVIDE_HIDDEN (__init_array_start = .);
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
PROVIDE_HIDDEN (__init_array_end = .);
} >SDRAM
.fini_array :
{
PROVIDE_HIDDEN (__fini_array_start = .);
KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
PROVIDE_HIDDEN (__fini_array_end = .);
} >SDRAM
.ctors :
{
KEEP (*crtbegin.o(.ctors))
KEEP (*crtbegin?.o(.ctors))
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
KEEP (*(SORT(.ctors.*)))
KEEP (*(.ctors))
} >SDRAM
.dtors :
{
KEEP (*crtbegin.o(.dtors))
KEEP (*crtbegin?.o(.dtors))
KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
KEEP (*(SORT(.dtors.*)))
KEEP (*(.dtors))
} >SDRAM
.data :
{
. = ALIGN(4);
_sdata = .;
*(.data .data.*)
*(.gnu.linkonce.d.*)
*(.sdata .sdata.*)
*(.srodata .srodata.*)
*(.gnu.linkonce.s.*)
. = ALIGN(4);
_edata = .;
} >SDRAM
. = ALIGN(16);
PROVIDE( __global_pointer$ = . );
.bss :
{
. = ALIGN(4);
_sbss = .;
*(.sbss)
*(.sbss.*)
*(.gnu.linkonce.sb.*)
*(.bss)
*(.bss.*)
*(.gnu.linkonce.b.*)
*(COMMON)
. = ALIGN(4);
_ebss = .;
} >SDRAM
.heap_stack :
{
. = ALIGN(8);
_end = .;
PROVIDE(end = . );
. = . + __heap_size;
. = ALIGN(8);
PROVIDE(_heap_end = .);
. = . + __stack_size;
. = ALIGN(8);
} >SDRAM
/* Place initial SP to the end of SDRAM */
__stack_top = ORIGIN(SDRAM) + LENGTH(SDRAM);
PROVIDE(_eusrstack = __stack_top);
}

0
LICENSE Normal file
View File

17
riscv64-elf.cmake Normal file
View File

@ -0,0 +1,17 @@
# Poor old Windows...
if(WIN32)
set(CMAKE_SYSTEM_NAME "Generic")
endif()
set(CMAKE_C_COMPILER riscv64-elf-gcc)
set(CMAKE_CXX_COMPILER riscv64-elf-g++)
# Optionally set size binary name, for elf section size reporting.
set(TARGET_TOOLCHAIN_SIZE riscv64-elf-size)
set(CMAKE_C_FLAGS_INIT "-march=rv64imafdc_zicsr -mabi=lp64d")
set(CMAKE_CXX_FLAGS_INIT "-march=rv64imafdc_zicsr -mabi=lp64d")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-specs=nano.specs -specs=nosys.specs -nostartfiles -Wl,--print-memory-usage -Wl,--no-warn-rwx-segments")
# Make CMake happy about those compilers
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")

9
src/main.c Normal file
View File

@ -0,0 +1,9 @@
#include <stdint.h>
static volatile uint64_t s_count = 0U;
int main(void) {
for(;;) {
s_count++;
}
}

55
startup/resource_table.c Normal file
View File

@ -0,0 +1,55 @@
#include <stddef.h>
#include <stdint.h>
#define __PACKED __attribute__((packed))
typedef enum {
RSC_CARVEOUT = 0,
RSC_DEVMEM = 1,
RSC_TRACE = 2,
RSC_VDEV = 3,
RSC_LAST = 4,
RSC_VENDOR_START = 128,
RSC_VENDOR_END = 512,
} rsc_type_t;
typedef struct __PACKED {
uint32_t ver;
uint32_t num;
uint32_t reserved[2];
} rsc_head_t;
typedef struct __PACKED {
rsc_type_t type;
uint32_t da;
uint32_t pa;
uint32_t len;
uint32_t flags;
uint32_t reserved;
uint8_t name[32];
} rsc_fw_carveout_t;
typedef struct __PACKED {
rsc_head_t head;
uint32_t offsets[1];
rsc_fw_carveout_t carveout_sdram;
} rsc_table_t;
__attribute__((section(".resource_table"))) const rsc_table_t rsc_tbl = {
.head =
{
.ver = 1,
.num = 1,
.reserved = {0UL, 0UL},
},
.offsets = {offsetof(rsc_table_t, carveout_sdram)},
.carveout_sdram =
{
.type = RSC_CARVEOUT,
.da = 0x4F800000UL,
.pa = 0x4F800000UL,
.len = 0x00800000UL,
.flags = 0x00000000UL,
.name = "SDRAM",
},
};

36
startup/start_c906.S Normal file
View File

@ -0,0 +1,36 @@
/* Custom CSR definitions */
#define CSR_MCOR 0x7C2
#define CSR_MSMPR 0x7f3
/* Reset vector */
.section .init,"ax",@progbits
.global _start
_start:
/* Initialize GP */
.option push
.option norelax
la gp, __global_pointer$
.option pop
/* Initialize SP */
la sp, __stack_top
/* Data section has been copied by framework. */
clear_bss:
/* Clear bss section */
la a0, _sbss
la a1, _ebss
bgeu a0, a1, post_crt0
loop_clear_bss:
sd zero, (a0)
addi a0, a0, 8
bltu a0, a1, loop_clear_bss
post_crt0:
jal SystemInit
jal main

3
startup/system_c906.c Normal file
View File

@ -0,0 +1,3 @@
void SystemInit(void) {
/* -- Nothing to do here for now. -- */
}