add freertos

This commit is contained in:
Ajit Ananthadevan 2025-08-23 09:11:31 +10:00
parent 4cdd9a24c4
commit 912f18d7fb
43 changed files with 30269 additions and 1 deletions

View file

@ -0,0 +1,168 @@
######################################
# target
######################################
TARGET = ch592_blinky
DEFINES = -DDEBUG=Debug_UART3
# ../../
include freertos.mk
######################################
# building variables
######################################
# debug build?
DEBUG = 1
# optimization for size
OPT = -Os
#######################################
# paths
#######################################
# Build path
BUILD_DIR = build
######################################
# source
######################################
# C sources
C_SOURCES += \
src/main.c \
../../../vendor/StdPeriphDriver/CH59x_clk.c \
../../../vendor/StdPeriphDriver/CH59x_gpio.c \
../../../vendor/StdPeriphDriver/CH59x_pwr.c \
../../../vendor/StdPeriphDriver/CH59x_sys.c \
../../../vendor/StdPeriphDriver/CH59x_uart3.c \
../../../vendor/RVMSIS/core_riscv.c \
# ASM sources
ASM_SOURCES += \
../../../vendor/FreeRTOS/Startup/startup_CH592.S
#######################################
# binaries
#######################################
#PREFIX = riscv-none-elf-
#PREFIX = riscv64-elf-
#debian
#PREFIX = riscv64-unknown-elf-
PREFIX = riscv-wch-elf-
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S
#######################################
# CFLAGS
#######################################
# cpu
CPU = -march=rv32imac_zicsr -mabi=ilp32 -msmall-data-limit=8
# For gcc version less than v12
# CPU = -march=rv32imac -mabi=ilp32 -msmall-data-limit=8
# fpu
FPU =
# float-abi
FLOAT-ABI =
# mcu
MCU = $(CPU) $(FPU) $(FLOAT-ABI)
# AS includes
AS_INCLUDES =
# C includes
C_INCLUDES += \
-I../../../vendor/StdPeriphDriver/inc \
-I../../../vendor/RVMSIS \
-I../../../vendor/Core \
-Isrc/inc
# compile gcc flags
ASFLAGS = $(MCU) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS = $(MCU) $(C_INCLUDES) $(OPT) $(DEFINES) -Wall -fdata-sections -ffunction-sections
ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif
# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"
#######################################
# LDFLAGS
#######################################
# link script
LDSCRIPT = ../../../vendor/FreeRTOS/Ld/Link.ld
# libraries
LIBS = -lc -lm -lnosys ../../../vendor/StdPeriphDriver/libISP592.a
LIBDIR =
LDFLAGS = $(MCU) -mno-save-restore -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -Wunused -Wuninitialized -T $(LDSCRIPT) -nostartfiles -Xlinker --gc-sections -Wl,-Map=$(BUILD_DIR)/$(TARGET).map --specs=nano.specs $(LIBS)
# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.S=.o)))
vpath %.S $(sort $(dir $(ASM_SOURCES)))
$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR)
$(AS) -c $(CFLAGS) $< -o $@
#$(LUAOBJECTS) $(OBJECTS)
$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
$(CC) $(OBJECTS) $(LDFLAGS) -o $@
$(SZ) $@
$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(HEX) $< $@
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
$(BIN) $< $@
$(BUILD_DIR):
mkdir $@
#######################################
# Program
#######################################
isp: $(BUILD_DIR)/$(TARGET).bin
wchisp flash $(BUILD_DIR)/$(TARGET).bin
flash: $(BUILD_DIR)/$(TARGET).bin
wlink flash $(BUILD_DIR)/$(TARGET).bin
#######################################
# clean up
#######################################
clean:
-rm -fR $(BUILD_DIR)
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)
# *** EOF ***

View file

@ -0,0 +1,19 @@
C_SOURCES = \
../../../vendor/FreeRTOS/FreeRTOS/croutine.c \
../../../vendor/FreeRTOS/FreeRTOS/event_groups.c \
../../../vendor/FreeRTOS/FreeRTOS/list.c \
../../../vendor/FreeRTOS/FreeRTOS/queue.c \
../../../vendor/FreeRTOS/FreeRTOS/stream_buffer.c \
../../../vendor/FreeRTOS/FreeRTOS/tasks.c \
../../../vendor/FreeRTOS/FreeRTOS/timers.c \
../../../vendor/FreeRTOS/FreeRTOS/portable/MemMang/heap_4.c \
../../../vendor/FreeRTOS/FreeRTOS/portable/GCC/RISC-V/port.c
ASM_SOURCES = \
../../../vendor/FreeRTOS/FreeRTOS/portable/GCC/RISC-V/portASM.S
C_INCLUDES = \
-I../../../vendor/FreeRTOS/FreeRTOS/include \
-I../../../vendor/FreeRTOS/FreeRTOS/portable/GCC/RISC-V

View file

@ -0,0 +1,153 @@
/*
FreeRTOS V202112.00
All rights reserved
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
This file is part of the FreeRTOS distribution.
FreeRTOS is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License (version 2) as published by the
Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
***************************************************************************
>>! NOTE: The modification to the GPL is included to allow you to !<<
>>! distribute a combined work that includes FreeRTOS without being !<<
>>! obliged to provide the source code for proprietary components !<<
>>! outside of the FreeRTOS kernel. !<<
***************************************************************************
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. Full license text is available on the following
link: http://www.freertos.org/a00114.html
***************************************************************************
* *
* FreeRTOS provides completely free yet professionally developed, *
* robust, strictly quality controlled, supported, and cross *
* platform software that is more than just the market leader, it *
* is the industry's de facto standard. *
* *
* Help yourself get started quickly while simultaneously helping *
* to support the FreeRTOS project by purchasing a FreeRTOS *
* tutorial book, reference manual, or both: *
* http://www.FreeRTOS.org/Documentation *
* *
***************************************************************************
http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
the FAQ page "My application does not run, what could be wrong?". Have you
defined configASSERT()?
http://www.FreeRTOS.org/support - In return for receiving this top quality
embedded software for free we request you assist our global community by
participating in the support forum.
http://www.FreeRTOS.org/training - Investing in training allows your team to
be as productive as possible as early as possible. Now you can receive
FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
Ltd, and the world's leading authority on the world's leading RTOS.
http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
including FreeRTOS+Trace - an indispensable productivity tool, a DOS
compatible FAT file system, and our tiny thread aware UDP/IP stack.
http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
licenses offer ticketed support, indemnification and commercial middleware.
http://www.SafeRTOS.com - High Integrity Systems also provide a safety
engineered and independently SIL3 certified version for use in safety and
mission critical applications that require provable dependability.
1 tab == 4 spaces!
*/
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
#include "CH59x_common.h"
/*-----------------------------------------------------------
* Application specific definitions.
*
* These definitions should be adjusted for your particular hardware and
* application requirements.
*
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
*
* See http://www.freertos.org/a00110.html.
*----------------------------------------------------------*/
/* See https://www.freertos.org/Using-FreeRTOS-on-RISC-V.html */
/* don't have MTIME */
#define configMTIME_BASE_ADDRESS ( 0 )
#define configMTIMECMP_BASE_ADDRESS ( 0 )
#define configUSE_PREEMPTION 1
#define configUSE_TIME_SLICING 0
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configCPU_CLOCK_HZ FREQ_SYS
#define configTICK_RATE_HZ ( ( TickType_t ) 500 )
#define configMAX_PRIORITIES ( 15 )
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 128 ) /* Can be as low as 60 but some of the demo tasks that use this constant require it to be higher. */
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 8 * 1024 ) )
#define configMAX_TASK_NAME_LEN ( 16 )
#define configUSE_TRACE_FACILITY 0
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 0
#define configUSE_MUTEXES 1
#define configQUEUE_REGISTRY_SIZE 8
#define configCHECK_FOR_STACK_OVERFLOW 0
#define configUSE_RECURSIVE_MUTEXES 1
#define configUSE_MALLOC_FAILED_HOOK 0
#define configUSE_APPLICATION_TASK_TAG 0
#define configUSE_COUNTING_SEMAPHORES 1
#define configGENERATE_RUN_TIME_STATS 0
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
/* Co-routine definitions. */
#define configUSE_CO_ROUTINES 0
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
/* Software timer definitions. */
#define configUSE_TIMERS 1
#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
#define configTIMER_QUEUE_LENGTH 4
#define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE )
/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet 1
#define INCLUDE_uxTaskPriorityGet 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_vTaskCleanUpResources 1
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelayUntil 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_eTaskGetState 1
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_xTaskAbortDelay 1
#define INCLUDE_xTaskGetHandle 1
#define INCLUDE_xSemaphoreGetMutexHolder 1
/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); printf("err at line %d of file \"%s\". \r\n ",__LINE__,__FILE__); while(1); }
/* Map to the platform printf function. */
#define configPRINT_STRING( pcString ) printf( pcString )
#endif /* FREERTOS_CONFIG_H */

View file

@ -0,0 +1,224 @@
/********************************** (C) COPYRIGHT *******************************
* File Name : main.c
* Author : WCH
* Version : V1.1
* Date : 2022/05/10
* Description : FreeRTOS例程使使
* __attribute__((interrupt("WCH-Interrupt-fast")))
* 使HIGHCODE修饰即可
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/******************************************************************************/
/* 头文件包含 */
#include "CH59x_common.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "stdarg.h"
/*********************************************************************
* GLOBAL TYPEDEFS
*/
#define TASK1_TASK_PRIO 5
#define TASK1_STK_SIZE 256 /* 因在任务中使用了APP_Printf所以栈空间必须要大于APP_Printf中的buf_str的大小+configMINIMAL_STACK_SIZE */
#define TASK2_TASK_PRIO 5
#define TASK2_STK_SIZE 256
#define TASK3_TASK_PRIO configMAX_PRIORITIES - 1
#define TASK3_STK_SIZE 256
/* Global Variable */
TaskHandle_t Task1Task_Handler;
TaskHandle_t Task2Task_Handler;
TaskHandle_t Task3Task_Handler;
SemaphoreHandle_t printMutex;
SemaphoreHandle_t xBinarySem;
/*********************************************************************
* @fn App_Printf
*
* @brief printf can be used within freertos.
*
* @param *fmt - printf params.
*
* @return none
*/
__HIGH_CODE
void App_Printf(const char *fmt, ...)
{
char buf_str[128]; /* 需要注意在这里的内存空间是否足够打印 */
va_list v_args;
va_start(v_args, fmt);
(void)vsnprintf((char *)&buf_str[0],
(size_t ) sizeof(buf_str),
(char const *) fmt,
v_args);
va_end(v_args);
/* 互斥量操作,不可在中断中使用 */
xSemaphoreTake(printMutex, portMAX_DELAY);
printf("%s", buf_str);
xSemaphoreGive(printMutex);
}
/*********************************************************************
* @fn task1_task
*
* @brief task1 program.
*
* @param *pvParameters - Parameters point of task1
*
* @return none
*/
__HIGH_CODE
void task1_task(void *pvParameters)
{
while (1)
{
App_Printf("task1 entry 1\n");
vTaskDelay(configTICK_RATE_HZ / 4);
App_Printf("task1 entry 2\n");
vTaskDelay(configTICK_RATE_HZ / 4);
}
}
/*********************************************************************
* @fn task2_task
*
* @brief task2 program.
*
* @param *pvParameters - Parameters point of task2
*
* @return none
*/
__HIGH_CODE
void task2_task(void *pvParameters)
{
while (1)
{
App_Printf("task2 entry 1\n");
vTaskDelay(configTICK_RATE_HZ / 2);
App_Printf("task2 entry 2\n");
vTaskDelay(configTICK_RATE_HZ / 2);
}
}
/*********************************************************************
* @fn task3_task
*
* @brief task3 program.
*
* @param *pvParameters - Parameters point of task3
*
* @return none
*/
__HIGH_CODE
void task3_task(void *pvParameters)
{
xBinarySem = xSemaphoreCreateBinary();
if(xBinarySem != NULL)
{
GPIOA_ModeCfg(GPIO_Pin_12, GPIO_ModeIN_PU);
GPIOA_ITModeCfg(GPIO_Pin_12, GPIO_ITMode_FallEdge);
PFIC_EnableIRQ(GPIO_A_IRQn);
while (1)
{
if(xSemaphoreTake(xBinarySem, portMAX_DELAY) == pdTRUE)
{
App_Printf("task3 sem get ok\n");
}
else
{
App_Printf("task3 sem get failed\n");
}
}
}
else
{
App_Printf("task3 sem init failed\n");
}
}
/*********************************************************************
* @fn main
*
* @brief main function.
*
* @param none
*
* @return none
*/
int main(void)
{
#ifdef DEBUG
GPIOA_SetBits(bTXD3);
GPIOA_ModeCfg(bTXD3, GPIO_ModeOut_PP_5mA);
UART3_DefInit();
#endif
PRINT("start.\n");
printMutex = xSemaphoreCreateMutex();
if(printMutex == NULL)
{
PRINT("printMutex error\n");
while(1);
}
/* create three task */
xTaskCreate((TaskFunction_t)task3_task,
(const char *)"task3",
(uint16_t)TASK3_STK_SIZE,
(void *)NULL,
(UBaseType_t)TASK3_TASK_PRIO,
(TaskHandle_t *)&Task3Task_Handler);
xTaskCreate((TaskFunction_t)task2_task,
(const char *)"task2",
(uint16_t)TASK2_STK_SIZE,
(void *)NULL,
(UBaseType_t)TASK2_TASK_PRIO,
(TaskHandle_t *)&Task2Task_Handler);
xTaskCreate((TaskFunction_t)task1_task,
(const char *)"task1",
(uint16_t)TASK1_STK_SIZE,
(void *)NULL,
(UBaseType_t)TASK1_TASK_PRIO,
(TaskHandle_t *)&Task1Task_Handler);
vTaskStartScheduler();
while (1)
{
PRINT("shouldn't run at here!!\n");
}
}
/*********************************************************************
* @fn GPIOA_IRQHandler
*
* @brief GPIOA_IRQHandler.
*
* @param none
*
* @return none
*/
__HIGH_CODE
void GPIOA_IRQHandler(void)
{
/* 本函数可以作为在本工程FreeRTOS中的中断函数写法示例 */
uint16_t flag;
portBASE_TYPE xHigherPriorityTaskWoken;
flag = GPIOA_ReadITFlagPort();
if((flag & GPIO_Pin_12) != 0)
{
xSemaphoreGiveFromISR(xBinarySem, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); /* 根据需要发起切换请求 */
}
GPIOA_ClearITFlagBit(flag); /* 清除中断标志 */
}
/******************************** endfile @ main ******************************/

View file

@ -2,7 +2,6 @@
#include "CH59x_common.h"
#include <string.h>
int main()
@ -22,3 +21,4 @@ int main()
}
}