在/* USER CODE BEGIN Includes */的下一行添加
#include "bsp.h"
#include "RTE_Components.h"
#ifdef RTE_Compiler_EventRecorder
#include "EventRecorder.h"
#endif
#include "cmsis_os2.h"
注意:#include "bsp.h"是我自己的头文件
在/* USER CODE BEGIN 0 */下一行添加
uint32_t HAL_GetTick (void) {
static uint32_t ticks = 0U;
uint32_t i;
if (osKernelGetState () == osKernelRunning) {
return ((uint32_t)osKernelGetTickCount ());
}
/* If Kernel is not running wait approximately 1 ms then increment and return auxiliary tick counter value */
for (i = (SystemCoreClock >> 14U); i > 0U; i--) {
__NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); }
return ++ticks; }
extern void app_main (void* arg);
注意:extern void app_main (void* arg);是我自己的函数
在 /* USER CODE BEGIN 2 */下一行添加
bsp_Init();
#ifdef RTE_Compiler_EventRecorder
EventRecorderInitialize(EventRecordAll, 1);
#endif
SystemCoreClockUpdate();
osKernelInitialize(); // Initialize CMSIS-RTOS
osThreadNew(app_main, NULL, NULL); // Create application main thread
osKernelStart();
注意:bsp_Init();是我自己定义的外设驱动初始化函数。在bsp.c里。
到此,结束。
下面给我自己app_main.c,app_main.c,app_main.h,bsp.c及 bsp.h内容,仅供参考:
main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* © Copyright (c) 2021 STMicroelectronics.
* All rights reserved.
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "can.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "fmc.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp.h"
#include "RTE_Components.h"
#ifdef RTE_Compiler_EventRecorder
#include "EventRecorder.h"
#endif
#include "cmsis_os2.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t HAL_GetTick (void) {
static uint32_t ticks = 0U;
uint32_t i;
if (osKernelGetState () == osKernelRunning) {
return ((uint32_t)osKernelGetTickCount ());
}
/* If Kernel is not running wait approximately 1 ms then increment and return auxiliary tick counter value */
for (i = (SystemCoreClock >> 14U); i > 0U; i--) {
__NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); }
return ++ticks; }
extern void app_main (void* arg);
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
MX_TIM5_Init();
MX_UART4_Init();
MX_USART3_UART_Init();
MX_TIM4_Init();
MX_FMC_Init();
MX_CAN1_Init();
MX_TIM1_Init();
MX_TIM9_Init();
/* USER CODE BEGIN 2 */
bsp_Init();
#ifdef RTE_Compiler_EventRecorder
EventRecorderInitialize(EventRecordAll, 1);
#endif
SystemCoreClockUpdate();
osKernelInitialize(); // Initialize CMSIS-RTOS
osThreadNew(app_main, NULL, NULL); // Create application main thread
osKernelStart();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {
0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {
0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 8;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Activate the Over-Drive mode
*/
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
app_main.c
#include "bsp.h"
#include "cmsis_os2.h" // ::CMSIS:RTOS2
#include "app_main.h"
/* 任务句柄 */
osThreadId_t led_ids = NULL;
osThreadId_t main_ids = NULL;
osThreadId_t epos_ids = NULL;
osThreadId_t temperature_ids = NULL;
/*timer*/
osTimerId_t timer0_id;//200ms 定时器
osTimerId_t timer1_id;//1000mS定时器
/*Mutex &semaphore & queue handle*/
osSemaphoreId_t sid_Semaphore_softtimer0;
osSemaphoreId_t sid_Semaphore_softtimer1;
osSemaphoreId_t sid_Semaphore_exit;//外部中断
osMessageQueueId_t mid_MsgQueue_ICAPTURE;//输入捕获值
osMessageQueueId_t mid_MsgQueue_EPOS_rx;
osMessageQueueId_t mid_MsgQueue_WLAN_rx;
osMessageQueueId_t mid_MsgQueue_Set_EPOS;
osMessageQueueId_t mid_MsgQueue_Set_temperature;
osMessageQueueId_t mid_MsgQueue_feedbak_temperature;
osMessageQueueId_t mid_MsgQueue_feedback_EPOS;
/*event*/
osEventFlagsId_t mid_event_Inited;
uint64_t appmain_StackMemory[STACK_SIZE2K] __attribute__((at(0x20028000))); //0x20028000 ~ 0x200287ff size 2k
uint64_t SR2_StackMemory[STACK_SIZE2K] __attribute__((at(0x20028800))); //0x20028800 ~ 0x200289ff size 2K
uint64_t led_StackMemory[STACK_SIZE2K] __attribute__((at(0x20029000))); //0x20028a00 ~ 0x20028bff size 2K
uint64_t epos_StackMemory[STACK_SIZE2K] __attribute__((at(0x20029800))); //0x20028c00 ~ 0x20028fff size 2k
/*----------------------------------------------------------------------------
Initilise main thread
*---------------------------------------------------------------------------*/
static const osThreadAttr_t ThreadAttr_MAIN = {
.name = "Main_Thread", //This decleration requires C99 to be selected in the project compiler options
.priority=osPriorityNormal,
.stack_mem = &appmain_StackMemory[0],
.stack_size =sizeof(appmain_StackMemory)
};
/*----------------------------------------------------------------------------
Initilise LED thread
*---------------------------------------------------------------------------*/
static const osThreadAttr_t ThreadAttr_LED = {
.name = "LED_Thread", //This decleration requires C99 to be selected in the project compiler options
.priority=osPriorityRealtime,//osPriorityAboveNormal,//osPriorityNormal,//,
.stack_mem = &led_StackMemory[0],
.stack_size =sizeof(led_StackMemory),
};
/*----------------------------------------------------------------------------
Initilise SR2
*---------------------------------------------------------------------------*/
static const osThreadAttr_t ThreadAttr_TEMP = {
.name = "SR2_Thread", //This decleration requires C99 to be selected in the project compiler options
.priority=osPriorityNormal,
.stack_mem = &SR2_StackMemory[0],
.stack_size =sizeof(SR2_StackMemory),
};
/*----------------------------------------------------------------------------
Initilise SR2
*---------------------------------------------------------------------------*/
static const osThreadAttr_t ThreadAttr_EPOS = {
.name = "EPOS_Thread", //This decleration requires C99 to be selected in the project compiler options
.priority=osPriorityNormal,
.stack_mem = &epos_StackMemory[0],
.stack_size =sizeof(epos_StackMemory),
};
static const osSemaphoreAttr_t semAttr_SEM1 = {
.name = "sid_Semaphore_exit",
};
static const osEventFlagsAttr_t EventFlagAttr_Inited= {
.name = "Inite_Events",
};
/*
*********************************************************************************************************
* 函 数 名: AppTaskCreate
* 功能说明: 创建应用任务
* 形 参: 无
* 返 回 值: 无
*********************************************************************************************************
*/
static void AppTaskCreate (void)
{
led_ids=osThreadNew(led_thread, NULL, &ThreadAttr_LED);
temperature_ids=osThreadNew(temperature_thread, NULL, &ThreadAttr_TEMP);
main_ids=osThreadNew(main_thread, NULL, &ThreadAttr_MAIN);
epos_ids=osThreadNew(epos_thread, NULL, &ThreadAttr_EPOS);
}
/*
*********************************************************************************************************
* 函 数 名: AppObjCreate
* 功能说明: 创建任务通信机制
* 形 参: 无
* 返 回 值: 无
*********************************************************************************************************
*/
static const osTimerAttr_t timerAttr_timer0 = {
.name = "timer_0",
};
static const osTimerAttr_t timerAttr_timer1 = {
.name = "timer_1",
};
static const osMessageQueueAttr_t MessageAttr_epos4rx = {
.name = "EPOS4_rx",
};
static const osMessageQueueAttr_t MessageAttr_epos4set = {
.name = "EPOS4_set",
};
//软件定时器0中断回调函数
void callback0(void *param)
{
switch( (uint32_t) param){
case 0:
osSemaphoreRelease(sid_Semaphore_softtimer0);//200mS
break;
}
}
//软件定时器1中断回调函数
void callback1(void *param)
{
switch( (uint32_t) param){
case 0:
osSemaphoreRelease(sid_Semaphore_softtimer1);//1000mS
break;
}
}
/*----------------------------------------------------------------------------
* Message Queue creation & usage
* Initilise the LED's, Create the semaphore
*---------------------------------------------------------------------------*/
int AppObjCreate (void)
{
/*timer*/
timer0_id = osTimerNew(&callback0, osTimerPeriodic,(void *)0, &timerAttr_timer0); //软件定时器timer0 200ms
if (timer0_id == NULL)
return -1;
osTimerStart(timer0_id, 200);//200mS定时开始计时
timer1_id = osTimerNew(&callback1, osTimerPeriodic,(void *)0, &timerAttr_timer1); //软件定时器timer1 1S
if (timer1_id == NULL)
return -1;
// osTimerStart(timer1_id, 1000);//1S定时开始计时
/*Creat queue*/
mid_MsgQueue_EPOS_rx = osMessageQueueNew(msgQUEUE_OBJECTS,sizeof(loopData_Typedef), &MessageAttr_epos4rx);
if (mid_MsgQueue_EPOS_rx == NULL) {
return -1;
}
mid_MsgQueue_WLAN_rx = osMessageQueueNew(modBUS_Rx_frame_item,sizeof(loopData_Typedef), NULL);
if (mid_MsgQueue_WLAN_rx == NULL) {
return -1;
}
mid_MsgQueue_ICAPTURE=osMessageQueueNew(msgQUEUE_OBJECTS,sizeof(icapture_t), NULL);
if (mid_MsgQueue_ICAPTURE == NULL) {
return -1;
}
mid_MsgQueue_Set_EPOS = osMessageQueueNew(msgQUEUE_OBJECTS,sizeof(val_set_t), &MessageAttr_epos4set);
if (mid_MsgQueue_Set_EPOS == NULL) {
return -1;
}
mid_MsgQueue_Set_temperature = osMessageQueueNew(msgQUEUE_OBJECTS,sizeof(val_set_t), NULL);
if (mid_MsgQueue_Set_temperature == NULL) {
return -1;
}
mid_MsgQueue_feedback_EPOS = osMessageQueueNew(msgQUEUE_OBJECTS,sizeof(fb_EPOS_t), NULL);
if (mid_MsgQueue_feedback_EPOS == NULL) {
return -1;
}
mid_MsgQueue_feedbak_temperature = osMessageQueueNew(msgQUEUE_OBJECTS,sizeof(fb_TEMP_t), NULL);
if (mid_MsgQueue_feedbak_temperature == NULL) {
return -1; // Message Queue object not created, handle failure
}
/* Creat Semaphore */
/*timer*/
sid_Semaphore_softtimer0 = osSemaphoreNew(1, 0, NULL );
if(sid_Semaphore_softtimer0 == NULL){
return -1;}
sid_Semaphore_softtimer1 = osSemaphoreNew(1, 0, NULL );
if(sid_Semaphore_softtimer1 == NULL){
return -1;}
sid_Semaphore_exit = osSemaphoreNew(1, 0, &semAttr_SEM1 );
if(sid_Semaphore_exit == NULL){
return -1;// Semaphore object not created, handle failure
}
/*event*/
mid_event_Inited = osEventFlagsNew(&EventFlagAttr_Inited);
if(mid_event_Inited == NULL){
return -1;
}
return (0);
}
void app_main (void const* arg)
{
/* 创建任务 */
AppTaskCreate();
/* 创建任务通信机制 */
AppObjCreate();
while(1)
{
if(sfr_Disp.runState == EError)
{
LED_RUNNING_OFF();
LED_ERROR_Toggle();
}
else
{
LED_ERROR_OFF();//拉低
LED_RUNNING_Toggle();
}
osDelay(200);
}
}
app_main.h
#ifndef __APP_MAIN_H__
#define __APP_MAIN_H__
#include "cmsis_os2.h"
#define STACK_SIZE2K 256
#define STACK_SIZE512 64
#define msgQUEUE_OBJECTS 5
/* 任务句柄 */
extern osThreadId_t smc_ids;
extern osThreadId_t main_ids;
extern osThreadId_t epos_ids;
extern osThreadId_t temperature_ids;
extern osThreadId_t updata_ids;
/*queue*/
extern osMessageQueueId_t mid_MsgQueue_ICAPTURE;//输入捕获
extern osMessageQueueId_t mid_MsgQueue_EPOS_rx;
extern osMessageQueueId_t mid_MsgQueue_WLAN_rx;
extern osMessageQueueId_t mid_MsgQueue_Set_EPOS;
extern osMessageQueueId_t mid_MsgQueue_Set_temperature;
extern osMessageQueueId_t mid_MsgQueue_feedbak_temperature;
extern osMessageQueueId_t mid_MsgQueue_feedback_EPOS;
/*semaphore*/
extern osSemaphoreId_t sid_Semaphore_softtimer0;
extern osSemaphoreId_t sid_Semaphore_softtimer1;
extern osSemaphoreId_t sid_Semaphore_exit;//epos
/*timer*/
extern osTimerId_t timer0_id;//200ms 定时
extern osTimerId_t timer1_id;//1000mS 定时
/*event*/
extern osEventFlagsId_t mid_event_Inited;
/* 供外部调用的函数声明 */
static void AppTaskCreate (void);
int AppObjCreate (void);
#endif
bsp.c
#include "bsp.h"
/*
*********************************************************************************************************
* 函 数 名: bsp_Init
* 功能说明: 初始化硬件设备。只需要调用一次。该函数配置CPU寄存器和外设的寄存器并初始化一些全局变量。
* 全局变量。
* 形 参: 无
* 返 回 值: 无
*********************************************************************************************************
*/
void bsp_Init(void)
{
/*
由于ST固件库的启动文件已经执行了CPU系统时钟的初始化,所以不必再次重复配置系统时钟。
启动文件配置了CPU主时钟频率、内部Flash访问速度和可选的外部SRAM FSMC初始化。
系统时钟缺省配置为72MHz,如果需要更改,可以修改 system_stm32f10x.c 文件
*/
SDRAM_Initialization_Sequence(&hsdram1);/*SDRAM初始化*/
bsp_InitUart(); /* 初始化串口 */
bsp_InitPWM();
bsp_InitIcapture();
// W25QXX_Init();
// bsp_InitLed(); /* 初始LED指示灯端口 */
// bsp_InitKey(); /* 初始化按键 */
// bsp_InitLcd(); /* 初始lcd */
// bsp_InitSram(); /* 初始sram 测试 */
// bsp_Initcan(); /* 初始can1 */
}
bsp.h
/*
*********************************************************************************************************
*
* 模块名称 : 驱动模块
* 文件名称 : bsp.h
* 版 本 : V1.0
* 说 明 : 头文件
*
*
*********************************************************************************************************
*/
#ifndef __BSP_H__
#define __BSP_H__
/*C lib*/
#include
#include
#include "stm32f4xx_hal.h"
#include "main.h"
/*thread*/
#include "temperture_thread.h"
#include "epos_thread.h"
#include "main_thread.h"
#include "led_thread.h"
bsp.h
/*stm32 lib*/
#include "usart.h"
/*bsp*/
#include "app_main.h"
#include "bsp_usart.h"
#include "bsp_w25qxx.h"
#include "bsp_led.h"
#include "pub_delay.h"
#include "bsp_SDRAM.h"
#include "bsp_ModbusSlave.h"
#include "bsp_pwm.h"
#include "bsp_icapture.h"
/* 供外部调用的函数声明 */
void bsp_Init(void);
#endif