1,主要的就是分析下面的代码
/* Includes ------------------------------------------------------------------*/
#include "main.h" //包含了三个头文件
#include "stm32f1xx_hal.h"
#include "cmsis_os.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables 私有变量 ---------------------------------------------------------*/
osThreadId PeriodicHandle;//实时系统的线程(任务)的识别码(身份证)
//osThreadId 》typedef TaskHandle_t osThreadId;》typedef void * TaskHandle_t;
//最终的目标还是 TaskHandle_t 手册上看看什么意思
osThreadId HandlerHandle;
osSemaphoreId bSem01Handle;//信号量任务的身份证
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes 私有函数原型 -----------------------------------------------*/
void SystemClock_Config(void); //系统时钟的配置
static void MX_GPIO_Init(void);//GPIO的初始化
void PeriodicTask(void const * argument);//返回值为空,在MX中设置的函数PeriodicTask声明
void HandlerTask(void const * argument);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
*
* @retval None
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration-- MCU配置 ----------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
/* 复位所有外设,初始化FLASH接口和系统滴答时钟 */
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();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, 添加互斥 ... */
/* USER CODE END RTOS_MUTEX */
/* Create the semaphores(s) */
/* definition and creation of bSem01 */
/* 定义和创建信号量bSem01 */
osSemaphoreDef(bSem01);
bSem01Handle = osSemaphoreCreate(osSemaphore(bSem01), 1);
/* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */
/* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones,添加软件定时器 ... */
/* USER CODE END RTOS_TIMERS */
/* Create the thread(s) */
/* definition and creation of Periodic */
/*定义和创建Periodic任务*/
osThreadDef(Periodic, PeriodicTask, osPriorityNormal, 0, 128);
PeriodicHandle = osThreadCreate(osThread(Periodic), NULL);
/* definition and creation of Handler */
osThreadDef(Handler, HandlerTask, osPriorityIdle, 0, 128);
HandlerHandle = osThreadCreate(osThread(Handler), NULL);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
/* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */
/* USER CODE END RTOS_QUEUES */
/* Start scheduler 开始任务调度管理器 */
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
/*当任务调度管理器运行的时候,我们将永远不会运行到这里(下面的while)*/
/* 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;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Initializes the CPU, AHB and APB busses clocks 初始化CPU,AHB,APB总线的时钟
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;//复位与时钟控制器(Reset Clock Controller),高速
RCC_OscInitStruct.HSEState = RCC_HSE_ON;//高速时钟状态开启
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;//预分频值为1
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;//PLL状态为开启
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;//倍频为9
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**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;//AHB时钟总线
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;//APB1时钟总线
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;//APB2时钟总线
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure the Systick interrupt time 配置系统滴答中断时间
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);//1000分频
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
/* IRQ=Interrupt request中断请求 。系统中断请求配置 NVIC:中断嵌套配置 */
HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);
}
/** Configure pins as 将端口配置为模拟量、输入、输出、事件输出、中断
* Analog
* Input
* Output
* EVENT_OUT
* EXTI
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable GPIO端口时钟使能 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */ //配置GPIO引脚的输出电平
HAL_GPIO_WritePin(GPIOB, LED0_Pin|LED1_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : LED0_Pin LED1_Pin */
GPIO_InitStruct.Pin = LED0_Pin|LED1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;//推挽输出
GPIO_InitStruct.Pull = GPIO_PULLUP;//上拉输出
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/* PeriodicTask function 详细定义函数*/
void PeriodicTask(void const * argument)
{
/* USER CODE BEGIN 5 */
/* Infinite loop */
for(;;)
{
osDelay(1);//系统自带延时函数,不会一直占用CPU的资源
}
/* USER CODE END 5 */
}
/* HandlerTask function */
void HandlerTask(void const * argument)
{
/* USER CODE BEGIN HandlerTask */
/* Infinite loop */
for(;;)
{
osDelay(1);
}
/* USER CODE END HandlerTask */
}
/**
* @brief Period elapsed callback in non blocking mode 中断回调函数
* @note This function is called when TIM4 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
*当TIM4中断发生时,在HAL_TIM_IRQHandler()内部调用此函数。 它直接调用HAL_IncTick()来增加一个用作应用程序时基的全局变量“uwTick”
* @param htim : TIM handle
* @retval None
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM4) {
HAL_IncTick();
}
/* USER CODE BEGIN Callback 1 */
/* USER CODE END Callback 1 */
}
/**
* @brief This function is executed in case of error occurrence.
*当错误发生的时候执行这个函数
* @param file: The file name as string.
* @param line: The line in file as a number.
* @retval None
*/
void _Error_Handler(char *file, int line)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
while(1)
{
}
/* 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.
*报告源文件的名称和源行号 发生assert_param(断言参数)错误的地方。
* @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****/
2,分析结构
2-1 包含三个头文件
#include "main.h" //包含了三个头文件
#include "stm32f1xx_hal.h"
#include "cmsis_os.h"
2-2 定义私有变量
osThreadId PeriodicHandle;//实时系统的线程(任务)的识别码(身份证)
//osThreadId 》typedef TaskHandle_t osThreadId;》typedef void * TaskHandle_t;
typedef TaskHandle_t osThreadId;
注意 #define PI 3.1415926 两个位置相反
typedef void * TaskHandle_t 空返回值
typedef void* HANDLE 什么意思
就是用HANDLE来代表void*,也可理解为 HANDLE == void*
void类型的指针表示可以指向任意类型的数据,但是void类型指针不能直接使用,使用前必须先转换成某种确定的类型。
osThreadId HandlerHandle ->TaskHandle_t HandlerHandle ->void * HandlerHandle
//最终的目标还是 TaskHandle_t 手册上看看什么意思osThreadId HandlerHandle;
osSemaphoreId bSem01Handle;//信号量任务的身份证
osSemaphoreId->typedef SemaphoreHandle_t osSemaphoreId;->typedef QueueHandle_t SemaphoreHandle_t;
->typedef void * QueueHandle_t;
注意:在MX中第一任务时,只是第一了task name :Periodic . 而任务句柄PeriodicHandle是自动生成的
2-3 声明私有函数
/* Private function prototypes 私有函数原型 -----------------------------------------------*/
void SystemClock_Config (void); //系统时钟的配置
static void MX_GPIO_Init (void);//GPIO的初始化
void PeriodicTask (void const * argument);//返回值为空,在MX中设置的函数PeriodicTask声明
void HandlerTask (void const * argument);
2-4 int main(void)
先对HAL_Init()、 SystemClock_Config()、 MX_GPIO_Init();初始化配置
再/* 定义和创建信号量bSem01 */
osSemaphoreDef(bSem01);
bSem01Handle = osSemaphoreCreate(osSemaphore(bSem01), 1);
/* Create the thread(s) */
/* definition and creation of Periodic */
/*定义和创建Periodic任务*/
osThreadDef(Periodic, PeriodicTask, osPriorityNormal, 0, 128);
PeriodicHandle = osThreadCreate(osThread(Periodic), NULL);
最后,开始任务调度管理器
3 ,注意CubeMX的封装函数和原生API的区别。
MX下有些函数,原生API下面没有