From a069d0ba2003a59b3173d3081d43c532a66aa389 Mon Sep 17 00:00:00 2001 From: imi415 Date: Tue, 29 Mar 2022 01:00:15 +0800 Subject: [PATCH] Multiple tidying and warnings fixed, added LICENSE --- BSP/Debug/debug.c | 18 ++++---- BSP/Debug/debug.h | 6 +-- BSP/Ld/Link.ld | 75 +++++++++++++++++-------------- BSP/Peripheral/inc/ch32v30x_eth.h | 30 ++++++------- BSP/Peripheral/src/ch32v30x_eth.c | 4 +- CMakeLists.txt | 2 + LICENSE | 8 ++++ LICENSE.wch | 8 ++++ README.md | 8 ++++ src/main.c | 2 + 10 files changed, 98 insertions(+), 63 deletions(-) create mode 100644 LICENSE create mode 100644 LICENSE.wch diff --git a/BSP/Debug/debug.c b/BSP/Debug/debug.c index 01f6e0b..9eb432c 100644 --- a/BSP/Debug/debug.c +++ b/BSP/Debug/debug.c @@ -88,7 +88,7 @@ void USART_Printf_Init(uint32_t baudrate) GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; -#if(DEBUG == DEBUG_UART1) +#if(DEBUG_UART == DEBUG_UART1) RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; @@ -96,7 +96,7 @@ void USART_Printf_Init(uint32_t baudrate) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); -#elif(DEBUG == DEBUG_UART2) +#elif(DEBUG_UART == DEBUG_UART2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); @@ -105,7 +105,7 @@ void USART_Printf_Init(uint32_t baudrate) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); -#elif(DEBUG == DEBUG_UART3) +#elif(DEBUG_UART == DEBUG_UART3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); @@ -123,15 +123,15 @@ void USART_Printf_Init(uint32_t baudrate) USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx; -#if(DEBUG == DEBUG_UART1) +#if(DEBUG_UART == DEBUG_UART1) USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); -#elif(DEBUG == DEBUG_UART2) +#elif(DEBUG_UART == DEBUG_UART2) USART_Init(USART2, &USART_InitStructure); USART_Cmd(USART2, ENABLE); -#elif(DEBUG == DEBUG_UART3) +#elif(DEBUG_UART == DEBUG_UART3) USART_Init(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE); @@ -154,13 +154,13 @@ __attribute__((used)) int _write(int fd, char *buf, int size) for(i = 0; i < size; i++) { -#if(DEBUG == DEBUG_UART1) +#if(DEBUG_UART == DEBUG_UART1) while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); USART_SendData(USART1, *buf++); -#elif(DEBUG == DEBUG_UART2) +#elif(DEBUG_UART == DEBUG_UART2) while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); USART_SendData(USART2, *buf++); -#elif(DEBUG == DEBUG_UART3) +#elif(DEBUG_UART == DEBUG_UART3) while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); USART_SendData(USART3, *buf++); #endif diff --git a/BSP/Debug/debug.h b/BSP/Debug/debug.h index 8d8aff9..983b003 100644 --- a/BSP/Debug/debug.h +++ b/BSP/Debug/debug.h @@ -11,7 +11,7 @@ #ifndef __DEBUG_H #define __DEBUG_H -#include "stdio.h" +#include #include "ch32v30x.h" /* UART Printf Definition */ @@ -19,8 +19,8 @@ #define DEBUG_UART2 2 #define DEBUG_UART3 3 -/* DEBUG UATR Definition */ -#define DEBUG DEBUG_UART1 +/* DEBUG UART Definition */ +#define DEBUG_UART DEBUG_UART1 //#define DEBUG DEBUG_UART2 //#define DEBUG DEBUG_UART3 diff --git a/BSP/Ld/Link.ld b/BSP/Ld/Link.ld index d3b0a51..09174c7 100644 --- a/BSP/Ld/Link.ld +++ b/BSP/Ld/Link.ld @@ -7,8 +7,8 @@ PROVIDE( _stack_size = __stack_size ); MEMORY { - FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 288K - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K + FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 288K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K } @@ -22,13 +22,13 @@ SECTIONS KEEP(*(SORT_NONE(.init))) . = ALIGN(4); _einit = .; - } >FLASH AT>FLASH + } >FLASH .vector : { *(.vector); . = ALIGN(64); - } >FLASH AT>FLASH + } >FLASH .text : { @@ -41,13 +41,13 @@ SECTIONS *(.glue_7t) *(.gnu.linkonce.t.*) . = ALIGN(4); - } >FLASH AT>FLASH + } >FLASH .fini : { KEEP(*(SORT_NONE(.fini))) . = ALIGN(4); - } >FLASH AT>FLASH + } >FLASH PROVIDE( _etext = . ); PROVIDE( _eitcm = . ); @@ -57,7 +57,7 @@ SECTIONS PROVIDE_HIDDEN (__preinit_array_start = .); KEEP (*(.preinit_array)) PROVIDE_HIDDEN (__preinit_array_end = .); - } >FLASH AT>FLASH + } >FLASH .init_array : { @@ -65,7 +65,7 @@ SECTIONS 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 = .); - } >FLASH AT>FLASH + } >FLASH .fini_array : { @@ -73,38 +73,40 @@ SECTIONS 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 = .); - } >FLASH AT>FLASH + } >FLASH .ctors : { - /* gcc uses crtbegin.o to find the start of - the constructors, so we make sure it is - first. Because this is a wildcard, it - doesn't matter if the user does not - actually link against crtbegin.o; the - linker won't look for a file to match a - wildcard. The wildcard also means that it - doesn't matter which directory crtbegin.o - is in. */ - KEEP (*crtbegin.o(.ctors)) - KEEP (*crtbegin?.o(.ctors)) - /* We don't want to include the .ctor section from - the crtend.o file until after the sorted ctors. - The .ctor section from the crtend file contains the - end of ctors marker and it must be last */ + /* gcc uses crtbegin.o to find the start of + the constructors, so we make sure it is + first. Because this is a wildcard, it + doesn't matter if the user does not + actually link against crtbegin.o; the + linker won't look for a file to match a + wildcard. The wildcard also means that it + doesn't matter which directory crtbegin.o + is in. + */ + KEEP (*crtbegin.o(.ctors)) + KEEP (*crtbegin?.o(.ctors)) + /* We don't want to include the .ctor section from + the crtend.o file until after the sorted ctors. + The .ctor section from the crtend file contains the + end of ctors marker and it must be last + */ KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors)) KEEP (*(SORT(.ctors.*))) KEEP (*(.ctors)) - } >FLASH AT>FLASH + } >FLASH .dtors : { - KEEP (*crtbegin.o(.dtors)) - KEEP (*crtbegin?.o(.dtors)) - KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) - KEEP (*(SORT(.dtors.*))) - KEEP (*(.dtors)) - } >FLASH AT>FLASH + KEEP (*crtbegin.o(.dtors)) + KEEP (*crtbegin?.o(.dtors)) + KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors)) + KEEP (*(SORT(.dtors.*))) + KEEP (*(.dtors)) + } >FLASH .dalign : { @@ -142,18 +144,23 @@ SECTIONS { . = ALIGN(4); PROVIDE( _sbss = .); - *(.sbss*) + *(.sbss*) *(.gnu.linkonce.sb.*) *(.bss*) - *(.gnu.linkonce.b.*) + *(.gnu.linkonce.b.*) *(COMMON*) . = ALIGN(4); PROVIDE( _ebss = .); - } >RAM AT>FLASH + } >RAM PROVIDE( _end = _ebss); PROVIDE( end = . ); + .heap : + { + + } + .stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size : { PROVIDE( _heap_end = . ); diff --git a/BSP/Peripheral/inc/ch32v30x_eth.h b/BSP/Peripheral/inc/ch32v30x_eth.h index 0076003..1a71e7c 100644 --- a/BSP/Peripheral/inc/ch32v30x_eth.h +++ b/BSP/Peripheral/inc/ch32v30x_eth.h @@ -947,11 +947,11 @@ Wake-UpFrame Filter Re7 : Filter3 CRC16 - Filter2 CRC16 */ #define ETH_MACA3HR_MACA3H ((unsigned int)0x0000FFFF) /* MAC address3 high */ #define ETH_MACA3LR_MACA3L ((unsigned int)0xFFFFFFFF) /* MAC address3 low */ -/******************************************************************************/ -/* -/* ETH MMC Register -/* -/******************************************************************************/ +/****************************************************************************** + * + * ETH MMC Register + * + ******************************************************************************/ #define ETH_MMCCR_MCFHP ((unsigned int)0x00000020) /* MMC counter Full-Half preset */ #define ETH_MMCCR_MCP ((unsigned int)0x00000010) /* MMC counter preset */ #define ETH_MMCCR_MCF ((unsigned int)0x00000008) /* MMC Counter Freeze */ @@ -988,11 +988,11 @@ Wake-UpFrame Filter Re7 : Filter3 CRC16 - Filter2 CRC16 */ #define ETH_MMCRGUFCR_RGUFC ((unsigned int)0xFFFFFFFF) /* Number of good unicast frames received. */ -/******************************************************************************/ -/* -/* ETH Precise Clock Protocol Register -/* -/******************************************************************************/ +/****************************************************************************** + * + * ETH Precise Clock Protocol Register + * + ******************************************************************************/ #define ETH_PTPTSCR_TSCNT ((unsigned int)0x00030000) /* Time stamp clock node type */ #define ETH_PTPTSSR_TSSMRME ((unsigned int)0x00008000) /* Time stamp snapshot for message relevant to master enable */ #define ETH_PTPTSSR_TSSEME ((unsigned int)0x00004000) /* Time stamp snapshot for event message enable */ @@ -1031,11 +1031,11 @@ Wake-UpFrame Filter Re7 : Filter3 CRC16 - Filter2 CRC16 */ #define ETH_PTPTSSR_TSTTR ((unsigned int)0x00000020) /* Time stamp target time reached */ #define ETH_PTPTSSR_TSSO ((unsigned int)0x00000010) /* Time stamp seconds overflow */ -/******************************************************************************/ -/* -/* ETH DMA Register -/* -/******************************************************************************/ +/****************************************************************************** + * + * ETH DMA Register + * + ******************************************************************************/ #define ETH_DMABMR_AAB ((unsigned int)0x02000000) /* Address-Aligned beats */ #define ETH_DMABMR_FPM ((unsigned int)0x01000000) /* 4xPBL mode */ #define ETH_DMABMR_USP ((unsigned int)0x00800000) /* Use separate PBL */ diff --git a/BSP/Peripheral/src/ch32v30x_eth.c b/BSP/Peripheral/src/ch32v30x_eth.c index e72ab16..46e128b 100644 --- a/BSP/Peripheral/src/ch32v30x_eth.c +++ b/BSP/Peripheral/src/ch32v30x_eth.c @@ -1285,7 +1285,7 @@ FlagStatus ETH_GetSoftwareResetStatus(void) { bitstatus = RESET; } - printf("ETH->DMABMR is:%08x\n", ETH->DMABMR); + printf("ETH->DMABMR is:%08lx\n", ETH->DMABMR); return bitstatus; } @@ -1432,7 +1432,7 @@ ITStatus ETH_GetDMAITStatus(uint32_t ETH_DMA_IT) /********************************************************************* * @fn ETH_DMAClearITPendingBit * - * @brief Clears the ETHERNET’s DMA IT pending bit. + * @brief Clears the ETHERNET�s DMA IT pending bit. * * @param ETH_DMA_IT - specifies the interrupt pending bit to clear. * ETH_DMA_IT_NIS - Normal interrupt summary diff --git a/CMakeLists.txt b/CMakeLists.txt index c5219db..3115173 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,5 @@ +# SPDX-License-Identifier: MIT + cmake_minimum_required(VERSION 3.10) project(ch32v307_template) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..64390d2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +Copyright 2021 imi415 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/LICENSE.wch b/LICENSE.wch new file mode 100644 index 0000000..66999cb --- /dev/null +++ b/LICENSE.wch @@ -0,0 +1,8 @@ +THIS SOFTWARE CONTAINS A LOT OF UNLICENSED CODE FROM WCH. +These code can be downloaded from www.wch.cn, without *ANY* licenses provided, +assuming they can be reproduced and re distributed. + +So, USE THEM AT YOUR OWN RISKS, modifications are made, see commit log or diff them manually. +Anything comes without a SPDX-License-Identifier header are subject to this LICENSE. + +Fly safe and godspeed. diff --git a/README.md b/README.md index a1d2fbb..16d5f58 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ + + # Template for WCH CH32V30x series MCU ## How to use @@ -25,3 +27,9 @@ The first file is used for CH32V303 devices, which lacks of the following periph * DVP ** Change the startup file to the correct version for the hardware in use. ** + +## Compilers +These MCUs uses something called "RISC-V4A" core without further documentation provided, AFAIK. + +Here is the list of features proprietary or incompatible with upstream toolchain: +* Fast IRQ handlers: uses a special attribute of `interrupt`, which value is `WCH-Interrupt-fast` \ No newline at end of file diff --git a/src/main.c b/src/main.c index 5f082c3..b78ca40 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,5 @@ +// SPDX-License-Identifier: MIT + int main(void) { for(;;) { //