1、【增加】RT-Thread 系统 Demo。

Signed-off-by: armink <armink.ztl@gmail.com>
This commit is contained in:
armink
2017-04-15 15:14:49 +08:00
parent cee09e4a57
commit b536a063d6
247 changed files with 137359 additions and 2 deletions
@@ -0,0 +1,13 @@
/*********************************************************************************************************
// NCLUDE FILES
*********************************************************************************************************/
#ifndef APP_TASK_H
#define APP_TASK_H
#include <rthw.h>
#include <rtthread.h>
#include <stm32f4xx_conf.h>
void rtthread_startup(void);
#endif
@@ -0,0 +1,41 @@
#ifndef __DELAY_CONF_H__
#define __DELAY_CONF_H__
#include"rtconfig.h"
#if RT_TICK_PER_SECOND == 1
#define DELAY_1S (RT_TICK_PER_SECOND)
#define DELAY_S(X) (X*DELAY_1S)
#elif RT_TICK_PER_SECOND == 10
#define DELAY_100MS(X) (X)
#define DELAY_S(X) (X*10)
#elif RT_TICK_PER_SECOND == 100
#define DELAY_10MS(X) (X)
#define DELAY_100MS(X) (X*10)
#define DELAY_S(X) (X*100)
#elif (RT_TICK_PER_SECOND == 1000)
#define DELAY_1MS (RT_TICK_PER_SECOND/1000)
#define DELAY_MS(X) (X*DELAY_1MS)
#define DELAY_S(X) (X*1000*DELAY_1MS)
#elif (RT_TICK_PER_SECOND == 10000)
#define DELAY_100US(X) (X*RT_TICK_PER_SECOND/10000)
#define DELAY_1MS (RT_TICK_PER_SECOND/1000)
#define DELAY_MS(X) (X*DELAY_1MS)
#define DELAY_S(X) (X*1000*DELAY_1MS)
#endif
#define DELAY_SYS_INIT_LED_ON DELAY_MS(90)
#define DELAY_SYS_INIT_LED_OFF DELAY_MS(10)
#define DELAY_SYS_RUN_LED_ON DELAY_MS(960)
#define DELAY_SYS_RUN_LED_OFF DELAY_MS(40)
#define DELAY_SYS_FAULT_LED_ON DELAY_MS(40)
#define DELAY_SYS_FAULT_LED_OFF DELAY_MS(960)
#endif
@@ -0,0 +1,87 @@
/* RT-Thread config file */
#ifndef __RTTHREAD_CFG_H__
#define __RTTHREAD_CFG_H__
/* RT_NAME_MAX*/
#define RT_NAME_MAX 24
/* RT_ALIGN_SIZE*/
#define RT_ALIGN_SIZE 8
/* PRIORITY_MAX */
#define RT_THREAD_PRIORITY_MAX 32
/* Tick per Second */
#define RT_TICK_PER_SECOND 1000 //1ms
/* SECTION: RT_DEBUG */
/* Thread Debug */
#define RT_DEBUG
#define RT_THREAD_DEBUG
#define RT_USING_OVERFLOW_CHECK
/* Using Hook */
#define RT_USING_HOOK
/* Using Software Timer */
#define RT_USING_TIMER_SOFT
#define RT_TIMER_THREAD_PRIO 4
#define RT_TIMER_THREAD_STACK_SIZE 512
#define RT_TIMER_TICK_PER_SECOND 1000
/* SECTION: IPC */
/* Using Semaphore*/
#define RT_USING_SEMAPHORE
/* Using Mutex */
#define RT_USING_MUTEX
/* Using Event */
#define RT_USING_EVENT
/* Using MailBox */
//#define RT_USING_MAILBOX
/* Using Message Queue */
//#define RT_USING_MESSAGEQUEUE
/* SECTION: Memory Management */
/* Using Memory Pool Management*/
//#define RT_USING_MEMPOOL
/* Using Dynamic Heap Management */
#define RT_USING_HEAP
/* Using Small MM */
#define RT_USING_SMALL_MEM
/* SECTION: Device System */
/* Using Device System */
#define RT_USING_CONSOLE
// <string name="RT_CONSOLE_DEVICE_NAME" description="The device name for console" default="uart1" />
#define RT_CONSOLE_DEVICE_NAME "uart1"
/* SECTION: Console options */
/* the buffer size of console*/
#define RT_CONSOLEBUF_SIZE 512
/* SECTION: finsh, a C-Express shell */
#define RT_USING_FINSH
/* Using symbol table */
#define FINSH_USING_SYMTAB
#define FINSH_USING_DESCRIPTION
/* Using msh and finsh */
#define FINSH_USING_MSH
/* Using msh only */
#define FINSH_USING_MSH_ONLY
/* SECTION: Device System */
/* Using Device System */
#define RT_USING_DEVICE
// <bool name="RT_USING_DEVICE_IPC" description="Using device communication" default="true" />
#define RT_USING_DEVICE_IPC
// <bool name="RT_USING_SERIAL" description="Using Serial" default="true" />
#define RT_USING_SERIAL
#endif
@@ -0,0 +1,134 @@
/**
******************************************************************************
* @file Project/STM32F4xx_StdPeriph_Templates/stm32f4xx_conf.h
* @author MCD Application Team
* @version V1.5.0
* @date 06-March-2015
* @brief Library configuration file.
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F4xx_CONF_H
#define __STM32F4xx_CONF_H
/* Includes ------------------------------------------------------------------*/
/* Uncomment the line below to enable peripheral header file inclusion */
#include "stm32f4xx_adc.h"
//#include "stm32f4xx_crc.h"
//#include "stm32f4xx_dbgmcu.h"
//#include "stm32f4xx_dma.h"
#include "stm32f4xx_exti.h"
#include "stm32f4xx_flash.h"
#include "stm32f4xx_gpio.h"
//#include "stm32f4xx_i2c.h"
#include "stm32f4xx_iwdg.h"
//#include "stm32f4xx_pwr.h"
#include "stm32f4xx_rcc.h"
//#include "stm32f4xx_rtc.h"
//#include "stm32f4xx_sdio.h"
//#include "stm32f4xx_spi.h"
//#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_usart.h"
//#include "stm32f4xx_wwdg.h"
#include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */
#if defined (STM32F429_439xx) || defined(STM32F446xx)
#include "stm32f4xx_cryp.h"
#include "stm32f4xx_hash.h"
#include "stm32f4xx_rng.h"
#include "stm32f4xx_can.h"
#include "stm32f4xx_dac.h"
#include "stm32f4xx_dcmi.h"
#include "stm32f4xx_dma2d.h"
#include "stm32f4xx_fmc.h"
#include "stm32f4xx_ltdc.h"
#include "stm32f4xx_sai.h"
#endif /* STM32F429_439xx || STM32F446xx */
#if defined (STM32F427_437xx)
#include "stm32f4xx_cryp.h"
#include "stm32f4xx_hash.h"
#include "stm32f4xx_rng.h"
#include "stm32f4xx_can.h"
#include "stm32f4xx_dac.h"
#include "stm32f4xx_dcmi.h"
#include "stm32f4xx_dma2d.h"
#include "stm32f4xx_fmc.h"
#include "stm32f4xx_sai.h"
#endif /* STM32F427_437xx */
#if defined (STM32F40_41xxx)
//#include "stm32f4xx_cryp.h"
//#include "stm32f4xx_hash.h"
//#include "stm32f4xx_rng.h"
#include "stm32f4xx_can.h"
//#include "stm32f4xx_dac.h"
//#include "stm32f4xx_dcmi.h"
//#include "stm32f4xx_fsmc.h"
#endif /* STM32F40_41xxx */
#if defined (STM32F411xE)
#include "stm32f4xx_flash_ramfunc.h"
#endif /* STM32F411xE */
#if defined (STM32F446xx)
#include "stm32f4xx_qspi.h"
#include "stm32f4xx_fmpi2c.h"
#include "stm32f4xx_spdifrx.h"
#include "stm32f4xx_cec.h"
#endif /* STM32F446xx */
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* If an external clock source is used, then the value of the following define
should be set to the value of the external clock source, else, if no external
clock is used, keep this define commented */
/*#define I2S_EXTERNAL_CLOCK_VAL 12288000 */ /* Value of the external clock in Hz */
/* Uncomment the line below to expanse the "assert_param" macro in the
Standard Peripheral Library drivers code */
/* #define USE_FULL_ASSERT 1 */
/* Exported macro ------------------------------------------------------------*/
#ifdef USE_FULL_ASSERT
/**
* @brief The assert_param macro is used for function's parameters check.
* @param expr: If expr is false, it calls assert_failed function
* which reports the name of the source file and the source
* line number of the call that failed.
* If expr is true, it returns no value.
* @retval None
*/
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
void assert_failed(uint8_t* file, uint32_t line);
#else
#define assert_param(expr) ((void)0)
#endif /* USE_FULL_ASSERT */
#endif /* __STM32F4xx_CONF_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
@@ -0,0 +1,58 @@
/**
******************************************************************************
* @file Project/STM32F4xx_StdPeriph_Templates/stm32f4xx_it.h
* @author MCD Application Team
* @version V1.5.0
* @date 06-March-2015
* @brief This file contains the headers of the interrupt handlers.
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F4xx_IT_H
#define __STM32F4xx_IT_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx.h"
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void NMI_Handler(void);
void MemManage_Handler(void);
void BusFault_Handler(void);
void UsageFault_Handler(void);
void SVC_Handler(void);
void DebugMon_Handler(void);
void SysTick_Handler(void);
#ifdef __cplusplus
}
#endif
#endif /* __STM32F4xx_IT_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
+13
View File
@@ -0,0 +1,13 @@
#include "app_task.h"
int main(void){
/* disable interrupt first */
rt_hw_interrupt_disable();
/* startup RT-Thread RTOS */
rtthread_startup();
return 0;
}
@@ -0,0 +1,57 @@
#include <ucos_ii.h>
#include <cpu_core.h>
#include <bsp.h>
#include <cm_backtrace.h>
#include <stdio.h>
#define HARDWARE_VERSION "V1.0.0"
#define SOFTWARE_VERSION "V0.1.0"
static OS_STK AppTaskStartStk[APP_TASK_START_STK_SIZE];
static void AppTaskStart(void *p_arg);
extern void fault_test_by_unalign(void);
extern void fault_test_by_div0(void);
int main(void)
{
INT8U err;
BSP_IntDisAll();
BSP_Init();
CPU_Init();
OSInit();
/* CmBacktrace initialize */
cm_backtrace_init("CmBacktrace", HARDWARE_VERSION, SOFTWARE_VERSION);
OSTaskCreateExt((void (*)(void *)) AppTaskStart,
(void *) 0,
(OS_STK *)&AppTaskStartStk[APP_TASK_START_STK_SIZE - 1],
(INT8U ) APP_TASK_START_PRIO,
(INT16U ) APP_TASK_START_PRIO,
(OS_STK *)&AppTaskStartStk[0],
(INT32U ) APP_TASK_START_STK_SIZE,
(void *) 0,
(INT16U )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));
OSTaskNameSet(APP_TASK_START_PRIO, "app_start", &err);
printf("System initialize finish. Starting..\r\n");
OSStart();
return (1);
}
static void AppTaskStart (void *p_arg)
{
// fault_test_by_unalign();
fault_test_by_div0();
while (DEF_TRUE) {
LED_RUN_ON;
OSTimeDlyHMSM(0, 0, 0, 500);
LED_RUN_OFF;
OSTimeDlyHMSM(0, 0, 0, 500);
}
}
@@ -0,0 +1,184 @@
#include "app_task.h"
#include <stdlib.h>
#include <stdio.h>
#include <bsp.h>
#include <finsh.h>
#include <shell.h>
#include <delay_conf.h>
#include <cpuusage.h>
#include <utils.h>
#include <cm_backtrace.h>
#define thread_sys_monitor_prio 30
#define HARDWARE_VERSION "V1.0.0"
#define SOFTWARE_VERSION "V0.1.0"
extern void fault_test_by_unalign(void);
extern void fault_test_by_div0(void);
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t thread_sys_monitor_stack[512];
struct rt_thread thread_sys_monitor;
uint8_t cpu_usage_major, cpu_usage_minor;
/**
* System monitor thread.
*
* @param parameter parameter
*/
void thread_entry_sys_monitor(void* parameter)
{
extern void cpu_usage_get(rt_uint8_t *major, rt_uint8_t *minor);
/* CmBacktrace test */
// fault_test_by_unalign();
fault_test_by_div0();
while (1)
{
if(get_system_status() == SYSTEM_STATUS_RUN){
cpu_usage_get(&cpu_usage_major, &cpu_usage_minor);
LED_RUN_ON;
rt_thread_delay(DELAY_SYS_RUN_LED_ON);
LED_RUN_OFF;
rt_thread_delay(DELAY_SYS_RUN_LED_OFF);
} else if (get_system_status() == SYSTEM_STATUS_INIT){
LED_RUN_ON;
rt_thread_delay(DELAY_SYS_INIT_LED_ON);
LED_RUN_OFF;
rt_thread_delay(DELAY_SYS_INIT_LED_OFF);
} else if (get_system_status() == SYSTEM_STATUS_FAULT){
LED_RUN_ON;
rt_thread_delay(DELAY_SYS_FAULT_LED_ON);
LED_RUN_OFF;
rt_thread_delay(DELAY_SYS_FAULT_LED_OFF);
}
IWDG_Feed();
}
}
static rt_err_t exception_hook(void *context) {
extern long list_thread(void);
uint8_t _continue = 1;
rt_enter_critical();
#ifdef RT_USING_FINSH
list_thread();
#endif
cm_backtrace_fault(*((uint32_t *)(__get_SP() + sizeof(uint32_t) * 8)), __get_SP() + sizeof(uint32_t) * 9);
while (_continue == 1);
return RT_EOK;
}
/**
* System initialization thread.
*
* @param parameter parameter
*/
void sys_init_thread(void* parameter){
set_system_status(SYSTEM_STATUS_INIT);
/* CmBacktrace initialize */
cm_backtrace_init("CmBacktrace", HARDWARE_VERSION, SOFTWARE_VERSION);
/* set exception hook */
rt_hw_exception_install(exception_hook);
/* initialize OK and switch to running status */
set_system_status(SYSTEM_STATUS_RUN);
rt_thread_delete(rt_thread_self());
}
int rt_application_init(void)
{
rt_thread_t init_thread = NULL;
rt_thread_init(&thread_sys_monitor,
"sys_monitor",
thread_entry_sys_monitor,
RT_NULL,
thread_sys_monitor_stack,
sizeof(thread_sys_monitor_stack),
thread_sys_monitor_prio, 5);
rt_thread_startup(&thread_sys_monitor);
init_thread = rt_thread_create("sys init", sys_init_thread,
NULL, 512, 10, 10);
if (init_thread != NULL) {
rt_thread_startup(init_thread);
}
return 0;
}
/**
* This function will startup RT-Thread RTOS.
*/
void rtthread_startup(void)
{
extern void cpu_usage_init(void);
/* init board */
rt_hw_board_init();
/* show version */
rt_show_version();
/* init tick */
rt_system_tick_init();
/* init kernel object */
rt_system_object_init();
/* init timer system */
rt_system_timer_init();
#ifdef RT_USING_HEAP
/* init memory system */
rt_system_heap_init((void*)STM32_SRAM_BEGIN, (void*)STM32_SRAM_END);
#endif
/* init scheduler system */
rt_system_scheduler_init();
/* initialize timer */
rt_system_timer_init();
/* init timer thread */
rt_system_timer_thread_init();
/* Add CPU usage to system */
cpu_usage_init();
#ifdef RT_USING_COMPONENTS_INIT
/* initialization RT-Thread Components */
rt_components_init();
#endif
#ifdef RT_USING_FINSH
/* init finsh */
finsh_system_init();
finsh_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
#ifdef RT_USING_COMPONENTS_INIT
/* initialization RT-Thread Components */
rt_components_init();
#endif
/* init application */
rt_application_init();
/* init idle thread */
rt_thread_idle_init();
/* start scheduler */
rt_system_scheduler_start();
/* never reach here */
return;
}
@@ -0,0 +1,40 @@
/*
* fault_test.c
*
* Created on: 2016/12/25
* Author: Armink
*/
#include <stdio.h>
void fault_test_by_unalign(void) {
volatile int * SCB_CCR = (volatile int *) 0xE000ED14; // SCB->CCR
volatile int * p;
volatile int value;
*SCB_CCR |= (1 << 3); /* bit3: UNALIGN_TRP. */
p = (int *) 0x00;
value = *p;
printf("addr:0x%02X value:0x%08X\r\n", (int) p, value);
p = (int *) 0x04;
value = *p;
printf("addr:0x%02X value:0x%08X\r\n", (int) p, value);
p = (int *) 0x03;
value = *p;
printf("addr:0x%02X value:0x%08X\r\n", (int) p, value);
}
void fault_test_by_div0(void) {
volatile int * SCB_CCR = (volatile int *) 0xE000ED14; // SCB->CCR
int x, y, z;
*SCB_CCR |= (1 << 4); /* bit4: DIV_0_TRP. */
x = 10;
y = 0;
z = x / y;
printf("z:%d\n", z);
}
@@ -0,0 +1,147 @@
/**
******************************************************************************
* @file Project/STM32F4xx_StdPeriph_Templates/stm32f4xx_it.c
* @author MCD Application Team
* @version V1.5.0
* @date 06-March-2015
* @brief Main Interrupt Service Routines.
* This file provides template for all exceptions handler and
* peripherals interrupt service routine.
******************************************************************************
* @attention
*
* <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_it.h"
#include "bsp.h"
/** @addtogroup Template_Project
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************/
/* Cortex-M4 Processor Exceptions Handlers */
/******************************************************************************/
/**
* @brief This function handles NMI exception.
* @param None
* @retval None
*/
void NMI_Handler(void)
{
}
/**
* @brief This function handles Memory Manage exception.
* @param None
* @retval None
*/
void MemManage_Handler(void)
{
/* Go to infinite loop when Memory Manage exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Bus Fault exception.
* @param None
* @retval None
*/
void BusFault_Handler(void)
{
/* Go to infinite loop when Bus Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles Usage Fault exception.
* @param None
* @retval None
*/
void UsageFault_Handler(void)
{
/* Go to infinite loop when Usage Fault exception occurs */
while (1)
{
}
}
/**
* @brief This function handles SVCall exception.
* @param None
* @retval None
*/
void SVC_Handler(void)
{
}
/**
* @brief This function handles Debug Monitor exception.
* @param None
* @retval None
*/
void DebugMon_Handler(void)
{
}
/**
* @brief This function handles SysTick Handler.
* @param None
* @retval None
*/
void SysTick_Handler(void)
{
rt_hw_timer_handler();
}
/******************************************************************************/
/* STM32F4xx Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_stm32f4xx.s). */
/******************************************************************************/
/**
* @brief This function handles PPP interrupt request.
* @param None
* @retval None
*/
/*void PPP_IRQHandler(void)
{
}*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
@@ -0,0 +1,21 @@
/*
* user_finsh_cmd.c
*
* Created on: 2013Äê12ÔÂ7ÈÕ
* Author: Armink
*/
#include <rthw.h>
#include <rtthread.h>
#include <stm32f4xx_conf.h>
#include "finsh.h"
void reboot(uint8_t argc, char **argv) {
NVIC_SystemReset();
}
MSH_CMD_EXPORT(reboot, Reboot System);
void get_cpuusage(void) {
extern uint8_t cpu_usage_major, cpu_usage_minor;
rt_kprintf("The CPU usage is %d.%d% now.\n", cpu_usage_major, cpu_usage_minor);
}
MSH_CMD_EXPORT(get_cpuusage, Get control board cpu usage);