Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lib] merge dev branch into main branch #149

Merged
merged 6 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions board/avaota-cam/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@ sunxi_serial_t uart_dbg = {
};

dram_para_t dram_para = {
.dram_clk = 792,
.dram_type = 3,
.dram_zq = 0x7b7bfb,
.dram_odt_en = 0x01,
.dram_para1 = 0x000010d2,
.dram_para2 = 0,
.dram_mr0 = 0x1c70,
.dram_mr1 = 0x42,
.dram_mr2 = 0x18,
.dram_mr3 = 0,
.dram_tpr0 = 0x004a2195,
.dram_tpr1 = 0x02423190,
.dram_tpr2 = 0x0008b061,
.dram_tpr3 = 0xb4787896,// unused
.dram_tpr4 = 0,
.dram_tpr5 = 0x48484848,
.dram_tpr6 = 0x00000048,
.dram_tpr7 = 0x1620121e,// unused
.dram_tpr8 = 0,
.dram_tpr9 = 0,// clock?
.dram_tpr10 = 0,
.dram_tpr11 = 0x00770000,
.dram_tpr12 = 0x00000002,
.dram_tpr13 = 0x34050100,
.dram_clk = 528,
.dram_type = 2,
.dram_zq = 0x7b7bf9,
.dram_odt_en = 0x00,
.dram_para1 = 0x000000d2,
.dram_para2 = 0x00400000,
.dram_mr0 = 0x00000E73,
.dram_mr1 = 0x02,
.dram_mr2 = 0x0,
.dram_mr3 = 0x0,
.dram_tpr0 = 0x00471992,
.dram_tpr1 = 0x0131A10C,
.dram_tpr2 = 0x00057041,
.dram_tpr3 = 0xB4787896,
.dram_tpr4 = 0x0,
.dram_tpr5 = 0x48484848,
.dram_tpr6 = 0x48,
.dram_tpr7 = 0x1621121e,
.dram_tpr8 = 0x0,
.dram_tpr9 = 0x0,
.dram_tpr10 = 0x00000000,
.dram_tpr11 = 0x00000000,
.dram_tpr12 = 0x00000000,
.dram_tpr13 = 0x34000100,
};

void show_chip() {
Expand Down
33 changes: 14 additions & 19 deletions board/avaota-cam/cdk/avaota_cam.cdkproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@
<MonitorProgress>
<DebugLaunch>40</DebugLaunch>
</MonitorProgress>
<DebugSessions>
<watchExpressions/>
<memoryExpressions>0x0;;;</memoryExpressions>
<statistics>;;32;;MHZ;;up</statistics>
<peripheralTabs/>
<WatchDisplayFormat>1</WatchDisplayFormat>
<LocalDisplayFormat>1</LocalDisplayFormat>
<debugLayout/>
<memoryTabColSizeExpressions>100:8;100:8;100:8;100:8;</memoryTabColSizeExpressions>
<QuickWatchDisplayFormat>1</QuickWatchDisplayFormat>
</DebugSessions>
<VirtualDirectory Name="board">
<File Name="../board.c">
<FileOption/>
Expand Down Expand Up @@ -426,7 +415,7 @@
<File Name="../../../src/drivers/sys-dma.c" ExcludeProjConfig="BuildSet">
<FileOption/>
</File>
<File Name="../../../src/drivers/sys-dram.c" ExcludeProjConfig="BuildSet">
<File Name="../../../src/drivers/sys-dram.c" ExcludeProjConfig="">
<FileOption/>
</File>
<File Name="../../../src/drivers/sys-gic.c" ExcludeProjConfig="BuildSet">
Expand Down Expand Up @@ -619,9 +608,9 @@
<Tools/>
</User>
<Compiler>
<Define>CONFIG_CHIP_SUN20IW5</Define>
<Define>CONFIG_CHIP_SUN20IW5;TRACE_MODE</Define>
<Undefine/>
<Optim>Optimize for debug (-Og)</Optim>
<Optim>Default</Optim>
<DebugLevel>Default (-g)</DebugLevel>
<IncludePath>$(ProjectPath);../../../include/;../../../include/arch/riscv/;../../../include/cli/;../../../include/drivers/chips/sun20iw5/;../../../include/drivers/mmc/;../../../include/drivers/pmu/reg/;../../../include/drivers/pmu/;../../../include/drivers/reg/;../../../include/drivers/usb/;../../../include/image/;../../../include/lib/elf/;../../../include/lib/fatfs/;../../../include/lib/fdt/;../../../include/drivers/;../../../include/drivers/chips/</IncludePath>
<OtherFlags>-nostdlib -g -ggdb -O0 -nostdinc -march=rv32imafcxthead -mabi=ilp32f -Wno-int-to-pointer-cast -Wno-int-to-pointer-cast -Wno-shift-count-overflow -Wno-builtin-declaration-mismatch -Wno-pointer-to-int-cast -Wno-implicit-function-declaration -Wno-discarded-qualifiers -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable</OtherFlags>
Expand Down Expand Up @@ -654,8 +643,6 @@
<AutoLDFile>no</AutoLDFile>
<LinkType>start-group</LinkType>
<IncludeAllLibs>no</IncludeAllLibs>
<LinkSpecsType>none</LinkSpecsType>
<LinkUseNewlibNano>no</LinkUseNewlibNano>
</Linker>
<Debug>
<LoadApplicationAtStartup>yes</LoadApplicationAtStartup>
Expand Down Expand Up @@ -711,15 +698,12 @@
</ConfigSIM>
<ConfigOpenOCD>
<OpenOCDExecutablePath/>
<OpenOCDLocally>yes</OpenOCDLocally>
<OpenOCDTelnetPortEnable>no</OpenOCDTelnetPortEnable>
<OpenOCDTelnetPort>4444</OpenOCDTelnetPort>
<OpenOCDTclPortEnable>no</OpenOCDTclPortEnable>
<OpenOCDTclPort>6666</OpenOCDTclPort>
<OpenOCDConfigOptions/>
<OpenOCDTimeout>5000</OpenOCDTimeout>
<OpenOCDRemoteIP>localhost</OpenOCDRemoteIP>
<OpenOCDRemotePort>3333</OpenOCDRemotePort>
<PluginID>openocd-sifive</PluginID>
<Version>latest</Version>
</ConfigOpenOCD>
Expand All @@ -742,4 +726,15 @@
</Flash>
</BuildConfig>
</BuildConfigs>
<DebugSessions>
<watchExpressions/>
<memoryExpressions>0x0;;;</memoryExpressions>
<statistics>;;32;;MHZ;;up</statistics>
<peripheralTabs/>
<WatchDisplayFormat>1</WatchDisplayFormat>
<LocalDisplayFormat>1</LocalDisplayFormat>
<debugLayout/>
<memoryTabColSizeExpressions>100:8;100:8;100:8;100:8;</memoryTabColSizeExpressions>
<QuickWatchDisplayFormat>1</QuickWatchDisplayFormat>
</DebugSessions>
</Project>
1 change: 1 addition & 0 deletions board/avaota-cam/cdk/avaota_cam.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Obj/avaota-cam_board.o Obj/avaota-cam_eabi_compat.o Obj/avaota-cam_head.o Obj/avaota-cam_start.o Obj/src_common.o Obj/src_ctype.o Obj/src_fdt_wrapper.o Obj/src_os.o Obj/src_smalloc.o Obj/src_sstdlib.o Obj/src_string.o Obj/src_uart.o Obj/hello_world_main.o Obj/cli_commands.o Obj/cli_history.o Obj/cli_lineedit.o Obj/cli_parse.o Obj/cli_shell.o Obj/drivers_sys-clk.o Obj/drivers_sys-gpio.o Obj/drivers_sys-uart.o Obj/drivers_sys-dram.o Obj/riscv32_e907_exception.o Obj/riscv32_e907_fprw.o Obj/riscv32_e907_memcmp.o Obj/riscv32_e907_memcpy.o Obj/riscv32_e907_memset.o Obj/riscv32_e907_timer.o Obj/sun20iw5_sys-clk.o Obj/sun20iw5_sys-dram.o Obj/sun20iw5_sys-rproc.o Obj/sun20iw5_sys-sid.o Obj/sun20iw5_sys-uart.o Obj/sun20iw5_sys-wdt.o Obj/image_bimage.o Obj/image_uimage.o Obj/image_zimage.o Obj/log_log.o Obj/log_xformat.o
25 changes: 25 additions & 0 deletions board/avaota-cam/hello_world/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,29 @@
#include <log.h>

#include <sys-clk.h>
#include <sys-dram.h>

#include <common.h>

#include <cli.h>
#include <cli_shell.h>
#include <cli_termesc.h>

extern sunxi_serial_t uart_dbg;
extern dram_para_t dram_para;

msh_declare_command(helloworld);

msh_define_help(helloworld, "display helloworld", "Usage: helloworld\n");
int cmd_helloworld(int argc, const char **argv) {
printk(LOG_LEVEL_MUTE, "Hello World!\n");
return 0;
}

const msh_command_entry commands[] = {
msh_define_command(helloworld),
msh_command_end,
};

int main(void) {
sunxi_serial_init(&uart_dbg);
Expand All @@ -25,6 +46,10 @@ int main(void) {

sunxi_clk_dump();

uint64_t dram_size = sunxi_dram_init(&dram_para);

syterkit_shell_attach(commands);

abort();

return 0;
Expand Down
73 changes: 73 additions & 0 deletions board/avaota-cam/payloads/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
cmake_minimum_required(VERSION 3.10)

set(RISCV_ROOT_PATH "~/sdk/Xuantie-900-gcc-linux-5.10.4-glibc-x86_64-V2.8.0")

set(RISCV_ROOT_PATH ${RISCV_ROOT_PATH} CACHE STRING "root path to riscv toolchain")

set(CMAKE_C_COMPILER "${RISCV_ROOT_PATH}/bin/riscv64-unknown-linux-gnu-gcc")
set(CMAKE_CXX_COMPILER "${RISCV_ROOT_PATH}/bin/riscv64-unknown-linux-gnu-g++")

set(CMAKE_SYSROOT "${RISCV_ROOT_PATH}/sysroot")

set(CMAKE_COMMON_FLAGS "-mcpu=c906fdv -mcmodel=medany -fno-stack-protector")
set(CMAKE_DISABLE_WARN_FLAGS "-Wint-to-pointer-cast -Wimplicit-function-declaration")

set(CMAKE_C_FLAGS "${CMAKE_DISABLE_WARN_FLAGS} ${CMAKE_C_FLAGS} ${CMAKE_COMMON_FLAGS}" CACHE STRING "c flags")
set(CMAKE_CXX_FLAGS "${CMAKE_DISABLE_WARN_FLAGS} ${CMAKE_CXX_FLAGS} ${CMAKE_COMMON_FLAGS}" CACHE STRING "c++ flags")

set(CMAKE_FIND_ROOT_PATH "${RISCV_ROOT_PATH}/riscv64-unknown-linux-gnu")

set(CMAKE_SYSROOT "${RISCV_ROOT_PATH}/sysroot")

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

# Set Version, Release need cmake
set(PROJECT_VERSION_MAJOR 0)
set(PROJECT_VERSION_MINOR 0)

# use git version as library version
find_package(Git QUIET)
if (Git_FOUND AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../.git")
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-list --count HEAD
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE _git_version
OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%h
OUTPUT_VARIABLE PROJECT_GIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
)
else ()
set(_git_version 0)
set(PROJECT_GIT_HASH "")
endif ()

project(T113-I_Miniloader C CXX ASM)

configure_file(
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)

set(CMAKE_AR "${RISCV_ROOT_PATH}/bin/riscv64-unknown-linux-gnu-ar")
set(CMAKE_OBJCOPY "${RISCV_ROOT_PATH}/bin/riscv64-unknown-linux-gnu-objcopy")

set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/link.ld)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -T ${LINKER_SCRIPT} -nostdlib")

include_directories(
include
include/sys
${PROJECT_BINARY_DIR}
)

add_subdirectory(lib)
add_subdirectory(src)
54 changes: 54 additions & 0 deletions board/avaota-cam/payloads/src/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Tiny C906 Firmware For T113-I

## How to build

```
mkdir build && cd build
cmake -DRISCV_ROOT_PATH=/path/to/riscv/toolchain ..
make

```

1. `mkdir build` creates a directory named "build".
2. `cd build` changes the current directory to "build".
3. `cmake -DRISCV_ROOT_PATH=/path/to/riscv/toolchain ..` configures the build system using CMake. The `-DRISCV_ROOT_PATH` option specifies the path to the RISC-V toolchain.
4. `make` compiles the project using the configured build system.

By running these commands, a build directory is created, the build process is configured with the specified RISC-V toolchain path, and the project is compiled using the `make` command.

## start.S

This code is written in assembly language for a RISC-V processor. Here's a breakdown of its functionality:

The `.global _start` directive indicates that `_start` is the entry point of the program.

At the `_start` label:
1. `li t1, 0x1 << 22` loads the immediate value `0x1` shifted left by `22` into register `t1`.
2. `csrs mxstatus, t1` sets the MXSTATUS register with the value in `t1`, enabling machine-level interrupts.
3. `li t1, 0x30013` loads the immediate value `0x30013` into register `t1`.
4. `csrs mcor, t1` sets the MCOR register with the value in `t1`, configuring the machine-level interrupt controller.
5. `j reset` jumps to the `reset` label.

At the `reset` label:
1. Memory is allocated on the stack by subtracting `32` from the stack pointer (`sp`).
2. Registers `s0`, `s1`, and `ra` are saved onto the stack using the `sd` instruction.
3. The value of `a0` is moved into `s0` register.
4. `li t0, 0x07090108` loads the immediate value `0x07090108` into register `t0`.
5. `sw zero, (t0)` stores the value of register `zero` (which holds the value `0`) at the memory address pointed to by `t0`.
6. `jal main` jumps and links to the `main` function.

After the `jal` instruction, the execution will continue at the `main` function. Once the `main` function returns:
1. The saved registers are restored from the stack.
2. The stack pointer is adjusted by adding `32` to it.
3. `ret` instruction is executed, which returns control to the calling function.

Overall, this code sets up machine-level interrupts, configures the interrupt controller, initializes the stack, and jumps to the `main` function before returning to the caller.

## main.c

This code defines a constant `OPENSBI_FW_TEXT_START` with the value `0x41fc0000`.

The function `jmp_opensbi` takes an argument `opensbi_base` of type `uint32_t` and performs an assembly instruction that jumps to the address stored in register `a0`. After the jump, it enters an infinite loop where it executes a `WFI` (Wait for Interrupt) instruction and then jumps back to `__LOOP` label.

The `main` function initializes UART0 and prints a series of messages to the console using `sys_uart_printf` function. It counts from 0 to 8 using a `for` loop and delays for 100,000 cycles between each count. Finally, it prints a message and calls `jmp_opensbi` function with the `OPENSBI_FW_TEXT_START` constant as the argument.

11 changes: 11 additions & 0 deletions board/avaota-cam/payloads/src/config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef _CONFIG_H_
#define _CONFIG_H_

// Project Info
#define PROJECT_NAME "@PROJECT_NAME@"
#define PROJECT_GIT_HASH 0x@PROJECT_GIT_HASH@

// System Configure
#define UART0_BASE_ADDR 0x02500000

#endif // _CONFIG_H_
67 changes: 67 additions & 0 deletions board/avaota-cam/payloads/src/include/byteorder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#ifndef __BYTEORDER_H__
#define __BYTEORDER_H__

#include <types.h>

static inline u16_t __swab16(u16_t x) { return ((x << 8) | (x >> 8)); }

static inline u32_t __swab32(u32_t x) {
return ((x << 24) | (x >> 24) | ((x & (u32_t)0x0000ff00UL) << 8) |
((x & (u32_t)0x00ff0000UL) >> 8));
}

static inline u64_t __swab64(u64_t x) {
return ((x << 56) | (x >> 56) | ((x & (u64_t)0x000000000000ff00ULL) << 40) |
((x & (u64_t)0x0000000000ff0000ULL) << 24) |
((x & (u64_t)0x00000000ff000000ULL) << 8) |
((x & (u64_t)0x000000ff00000000ULL) >> 8) |
((x & (u64_t)0x0000ff0000000000ULL) >> 24) |
((x & (u64_t)0x00ff000000000000ULL) >> 40));
}

/*
* swap bytes bizarrely.
* swahw32 - swap 16-bit half-words in a 32-bit word
*/
static inline u32_t __swahw32(u32_t x) {
return (((x & (u32_t)0x0000ffffUL) << 16) |
((x & (u32_t)0xffff0000UL) >> 16));
}

/*
* swap bytes bizarrely.
* swahb32 - swap 8-bit halves of each 16-bit half-word in a 32-bit word
*/
static inline u32_t __swahb32(u32_t x) {
return (((x & (u32_t)0x00ff00ffUL) << 8) | ((x & (u32_t)0xff00ff00UL) >> 8));
}

#if (BYTE_ORDER == BIG_ENDIAN)
#define cpu_to_le64(x) (__swab64((u64_t)(x)))
#define le64_to_cpu(x) (__swab64((u64_t)(x)))
#define cpu_to_le32(x) (__swab32((u32_t)(x)))
#define le32_to_cpu(x) (__swab32((u32_t)(x)))
#define cpu_to_le16(x) (__swab16((u16_t)(x)))
#define le16_to_cpu(x) (__swab16((u16_t)(x)))
#define cpu_to_be64(x) ((u64_t)(x))
#define be64_to_cpu(x) ((u64_t)(x))
#define cpu_to_be32(x) ((u32_t)(x))
#define be32_to_cpu(x) ((u32_t)(x))
#define cpu_to_be16(x) ((u16_t)(x))
#define be16_to_cpu(x) ((u16_t)(x))
#else
#define cpu_to_le64(x) ((u64_t)(x))
#define le64_to_cpu(x) ((u64_t)(x))
#define cpu_to_le32(x) ((u32_t)(x))
#define le32_to_cpu(x) ((u32_t)(x))
#define cpu_to_le16(x) ((u16_t)(x))
#define le16_to_cpu(x) ((u16_t)(x))
#define cpu_to_be64(x) (__swab64((u64_t)(x)))
#define be64_to_cpu(x) (__swab64((u64_t)(x)))
#define cpu_to_be32(x) (__swab32((u32_t)(x)))
#define be32_to_cpu(x) (__swab32((u32_t)(x)))
#define cpu_to_be16(x) (__swab16((u16_t)(x)))
#define be16_to_cpu(x) (__swab16((u16_t)(x)))
#endif

#endif /* __BYTEORDER_H__ */
Loading
Loading