From a891650679f5f72915f1619ad283900cf686dd7c Mon Sep 17 00:00:00 2001 From: Logan G Date: Wed, 9 Aug 2023 23:36:21 -0600 Subject: [PATCH 1/8] Changed project name to make sense --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f5c156..52f8f27 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(bacstat) +project(hello-world) # set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_C_COMPILER riscv-none-embed-gcc) From 6ca77928e227e590c25295b05d8f4d907b5dfea6 Mon Sep 17 00:00:00 2001 From: Logan G Date: Wed, 9 Aug 2023 23:37:02 -0600 Subject: [PATCH 2/8] Fixed wrong variable type in printf --- User/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/User/main.c b/User/main.c index 53c22f4..a6c64b3 100644 --- a/User/main.c +++ b/User/main.c @@ -36,7 +36,7 @@ int main(void) NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); Delay_Init(); USART_Printf_Init(115200); - printf("SystemClk:%d\r\n", SystemCoreClock); + printf("SystemClk:%ld\r\n", SystemCoreClock); printf("This is printf example\r\n"); From 5aeffc62775d3ba4151d8f6ee511c0b1a3cab06e Mon Sep 17 00:00:00 2001 From: Logan G Date: Wed, 9 Aug 2023 23:51:45 -0600 Subject: [PATCH 3/8] Fixed this file for upstream GCC use --- User/ch32v20x_it.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/User/ch32v20x_it.c b/User/ch32v20x_it.c index c997d9e..092e9a0 100644 --- a/User/ch32v20x_it.c +++ b/User/ch32v20x_it.c @@ -9,8 +9,12 @@ *******************************************************************************/ #include "ch32v20x_it.h" -void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); -void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); +// Upstream GCC as of version 12 does not support these, use normal ones instead :( +// If you are using a compiler that supports these, uncomment these lines instead, and use WCH-Interrupt-fast for new handlers +//void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); +//void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast"))); +void NMI_Handler(void) __attribute__((interrupt)); +void HardFault_Handler(void) __attribute__((interrupt)); /********************************************************************* * @fn NMI_Handler From 95f0acc88cf643111e43bd7dba889ef7491bca7b Mon Sep 17 00:00:00 2001 From: Logan G Date: Wed, 9 Aug 2023 23:52:40 -0600 Subject: [PATCH 4/8] Updated CMakeLists to work with upstream GCC >=12 The original march option was wrong (RV32E). According to WCH, the instruction set is RV32IMAC. Updating the GCC version broke a ton of things since it better complied with stack alignment requirements that RV32E had, which RV32I was not compatible with. Additionally, newer versions of the RISC-V ISA spec split off the Zicsr and Zifencei instruction from RV32I. GCC versions 12 and newer began using this spec, leading to missing opcode errors. Lastly, "--specs=nosys.specs" and "--specs=nano.specs" allegedly don't go together, and nano is apparently preferred. Also "-mcmodel=medany" is needed apparently. See https://gcc.gnu.org/onlinedocs/gcc/RISC-V-Options.html#index-mcmodel_003dmedany --- CMakeLists.txt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 52f8f27..4fb7370 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,10 +2,10 @@ cmake_minimum_required(VERSION 3.10) project(hello-world) # set(CMAKE_VERBOSE_MAKEFILE ON) -set(CMAKE_C_COMPILER riscv-none-embed-gcc) -set(CMAKE_CXX_COMPILER riscv-none-embed-gcc) -set(CMAKE_OBJCOPY riscv-none-embed-objcopy) -set(CMAKE_SIZE riscv-none-embed-size) +set(CMAKE_C_COMPILER riscv-none-elf-gcc) +set(CMAKE_CXX_COMPILER riscv-none-elf-gcc) +set(CMAKE_OBJCOPY riscv-none-elf-objcopy) +set(CMAKE_SIZE riscv-none-elf-size) set_property(SOURCE Startup/startup_ch32v20x_D6.S PROPERTY LANGUAGE C) @@ -21,7 +21,7 @@ include_directories(Peripheral/inc) add_definitions(-finline-functions-called-once -g) -add_definitions(-Wall -march=rv32ec -mabi=ilp32e -msmall-data-limit=0 -msave-restore -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common) +add_definitions(-Wall -Wextra -march=rv32imac_zicsr_zifencei -mabi=ilp32 -msmall-data-limit=0 -msave-restore -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -mcmodel=medany --specs=nano.specs) file(GLOB SOURCES "Debug/*.c" "Core/*.c" "User/*.c" "Peripheral/src/*.c" "Startup/*.S") @@ -34,5 +34,6 @@ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.siz COMMAND ${CMAKE_SIZE} ARGS --format=berkeley ${CMAKE_PROJECT_NAME}.elf) - add_executable(${CMAKE_PROJECT_NAME}.elf ${SOURCES}) -target_link_options(${CMAKE_PROJECT_NAME}.elf PRIVATE -T ${CMAKE_SOURCE_DIR}/Ld/Link.ld -Os -march=rv32ec -mabi=ilp32e -nostartfiles -Xlinker -gc-sections -static --specs=nano.specs --specs=nosys.specs) +add_executable(${CMAKE_PROJECT_NAME}.elf ${SOURCES}) + +target_link_options(${CMAKE_PROJECT_NAME}.elf PRIVATE -T ${CMAKE_SOURCE_DIR}/Ld/Link.ld -Os -march=rv32imac_zicsr_zifencei -mabi=ilp32 -mstrict-align -nostartfiles -Xlinker -gc-sections -static --specs=nano.specs -mcmodel=medany) From d1d767cdde9e3faa1ccdd3323481238b9f938957 Mon Sep 17 00:00:00 2001 From: Logan G Date: Thu, 10 Aug 2023 00:04:41 -0600 Subject: [PATCH 5/8] Use default strict-align for target --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4fb7370..7868133 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,4 +36,4 @@ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.siz add_executable(${CMAKE_PROJECT_NAME}.elf ${SOURCES}) -target_link_options(${CMAKE_PROJECT_NAME}.elf PRIVATE -T ${CMAKE_SOURCE_DIR}/Ld/Link.ld -Os -march=rv32imac_zicsr_zifencei -mabi=ilp32 -mstrict-align -nostartfiles -Xlinker -gc-sections -static --specs=nano.specs -mcmodel=medany) +target_link_options(${CMAKE_PROJECT_NAME}.elf PRIVATE -T ${CMAKE_SOURCE_DIR}/Ld/Link.ld -Os -march=rv32imac_zicsr_zifencei -mabi=ilp32 -nostartfiles -Xlinker -gc-sections -static --specs=nano.specs -mcmodel=medany) From 0ca95f59f8b0a5737d18f20372fc183ca9ad8ced Mon Sep 17 00:00:00 2001 From: Logan G Date: Thu, 10 Aug 2023 00:07:56 -0600 Subject: [PATCH 6/8] Surpress unused variable warning, it's sort of wrong --- Debug/debug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Debug/debug.c b/Debug/debug.c index 89117bd..9acbb87 100644 --- a/Debug/debug.c +++ b/Debug/debug.c @@ -182,7 +182,7 @@ void USART_Printf_Init(uint32_t baudrate) * @return size: Data length */ __attribute__((used)) -int _write(int fd, char *buf, int size) +int _write(__attribute__((unused)) int fd, char *buf, int size) { int i; From 8c8b132fd6f702bbb06af2103514e2ab1e10bcfc Mon Sep 17 00:00:00 2001 From: Logan G Date: Thu, 10 Aug 2023 19:32:29 -0600 Subject: [PATCH 7/8] Actually works now --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7868133..04ae673 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ include_directories(Peripheral/inc) add_definitions(-finline-functions-called-once -g) -add_definitions(-Wall -Wextra -march=rv32imac_zicsr_zifencei -mabi=ilp32 -msmall-data-limit=0 -msave-restore -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -mcmodel=medany --specs=nano.specs) +add_definitions(-Wall -Wextra -march=rv32imac_zicsr_zifencei -mabi=ilp32 -msmall-data-limit=0 -msave-restore -Os -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -mcmodel=medany) file(GLOB SOURCES "Debug/*.c" "Core/*.c" "User/*.c" "Peripheral/src/*.c" "Startup/*.S") @@ -36,4 +36,4 @@ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.siz add_executable(${CMAKE_PROJECT_NAME}.elf ${SOURCES}) -target_link_options(${CMAKE_PROJECT_NAME}.elf PRIVATE -T ${CMAKE_SOURCE_DIR}/Ld/Link.ld -Os -march=rv32imac_zicsr_zifencei -mabi=ilp32 -nostartfiles -Xlinker -gc-sections -static --specs=nano.specs -mcmodel=medany) +target_link_options(${CMAKE_PROJECT_NAME}.elf PRIVATE -T ${CMAKE_SOURCE_DIR}/Ld/Link.ld -Os -march=rv32imac_zicsr_zifencei -mabi=ilp32 -nostartfiles -Xlinker -gc-sections -static --specs=nano.specs --specs=nosys.specs -mcmodel=medany) From e5dc9abf1cce9977e2cd7028d9c5d79bc1fefa43 Mon Sep 17 00:00:00 2001 From: Logan G Date: Thu, 10 Aug 2023 22:09:00 -0600 Subject: [PATCH 8/8] Fixed ADC struct values not being init'd correctly --- Peripheral/src/ch32v20x_adc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Peripheral/src/ch32v20x_adc.c b/Peripheral/src/ch32v20x_adc.c index 4d642cf..f782679 100644 --- a/Peripheral/src/ch32v20x_adc.c +++ b/Peripheral/src/ch32v20x_adc.c @@ -185,6 +185,8 @@ void ADC_StructInit(ADC_InitTypeDef *ADC_InitStruct) ADC_InitStruct->ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStruct->ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStruct->ADC_NbrOfChannel = 1; + ADC_InitStruct->ADC_OutputBuffer = DISABLE; + ADC_InitStruct->ADC_Pga = ADC_Pga_1; } /*********************************************************************