STM32 porting uCOS based on HAL Library

1, What is UCOS III

Ucosiiii is a tailorable, curable, and deprivable multitask system, which has no limit on the number of tasks. It is the third generation kernel of UCOS. Ucosiiii has the following important characteristics:

  • Deprivable multitasking Management: UCOSIII and UCOSII belong to the deprivable multitasking kernel and always execute the highest priority tasks currently ready.
  • Time slice rotation scheduling of tasks with the same priority: This is a big difference between ucosiiii and UCOSH. Ucosiiii allows a task priority to be used by multiple tasks. When the priority is in the highest ready state, ucosiiii will schedule all tasks at this priority in turn, and let each task run for a period of time specified by the user, which is called time slice.
  • Extremely short off interrupt time: ucosiiii can protect critical segment code by locking kernel scheduling rather than off interrupt, so as to minimize the off interrupt time, so that ucosiiii can respond to interrupt requests very quickly.
  • Unlimited number of tasks: UCOSIII itself has no limit on the number of tasks, but from the perspective of practical application, the number of tasks will be limited by the storage space used by the CPU, including code space and data space.
  • Unlimited number of priorities: ucosiiii supports unlimited task priorities.
  • Unlimited number of kernel objects: UCOSIII allows you to define any number of kernel objects. Kernel objects refer to tasks, semaphores, mutually exclusive semaphores, event flag groups, message queues, timers, storage buffers, etc.
  • Software timer: users can arbitrarily define "single" and "cycle" timers. The timer is a decreasing counter. When it decreases to zero, it will execute the pre-defined operations. Each timer can specify the required operation. The periodic timer will perform the specified operation when it decreases to zero and automatically reset the counter value.
  • Wait for multiple kernel objects at the same time: ucosiiii allows a task to wait for multiple events at the same time. That is, a task can be suspended on multiple semaphores or message queues. When any waiting event occurs, the waiting task will be awakened.
  • Send signals directly to tasks: ucosiiii allows interrupts or tasks to send signals directly to another task, avoiding the creation and use of kernel objects such as semaphores or event flags as intermediaries to send signals to other tasks. This feature effectively improves the system performance.
  • Send message directly to task: ucosiiii allows interrupt or task to send message directly to another task, avoiding creating and using message queue as intermediary.
  • Task register: several "task registers" can be set for each task. The task register is different from the CPU hardware register. It is mainly used to save the error information, ID identification information, measurement results of interrupt closing time, etc. of each task.
  • Task level clock beat processing: the clock beat of ucosiiii is completed through a special task, and the timing interrupt only triggers this task. Putting delay processing and timeout judgment on task level code completion can greatly reduce interrupt delay time.
  • Deadlock prevention: all "wait" functions of ucosiiii provide timeout detection mechanism to effectively avoid deadlock.
  • Timestamp: ucosiiii requires a 16 bit or 32-bit free running counter (time base counter) to measure time. When the system is running, the counter can be read to measure the time information of an event. For example, when ISR sends a message to a task, it automatically reads the value of the counter and attaches it to the message. When the task reads a message, it can get the time scale carried by the message. In this way, by reading the current time scale and calculating the difference between the two time scales, it can determine the exact time spent delivering the message.

 

 

  2, Project of establishing STM32F103C8T6

  • Configure RCC

 

 

 

  • Configure SYS

 

 

  • Set PC13 to GPIO_Output is used to turn on the LED

In the final display results, PC13 will continuously flash to remind us of success!

 

 

  •   Configure serial port USART1

      After the configuration is completed, the subsequent file output is as described in our previous article. Select MDK-ARM and check the output as. c\.h file. After we complete the configuration, we output the item as keil.

 

 

  The generated file is shown in the figure. After opening, add the following code in main.c:

/* Add a statement to the while loop in the main function */
while
(1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET); HAL_Delay(500); } /* USER CODE END 3 */

3, Prepare uC source code

The source code of uC and the subsequent configuration of the complete project have been uploaded to my github in the form of ZIP package.

 

 

  Here's how to get a connection: https://github.com/LinZJ0423/STM32/releases/tag/STM32

I also managed the unmodified uC source code to github

Get the connection as follows: https://github.com/LinZJ0423/STM32

File for this!!!

 

 

 

3.1 preparations before transplantation

 

 

  This is the source file we downloaded and extracted

Folders UC BSP and UC config are two folders that need to be created

In bsp, create two empty code files, as shown below:

 

 

 

Add the following files to the folder UC config (copy from the following path)

 

 

 

The files to be copied are as follows:

 

 

  Then put it into our new UC config folder.

Copy uCOS related files to MDK-ARM folder of HAL project:

 

 

  4, Start migration

Go back to HAL project opened by Keil

4.1 add uC files to the project

Click Manage Project Items

 

  Create a new folder for the project as follows:

 

 

Add files to the newly added folder respectively

Click CPU - > Add files... And select the following files to add

In the cpu, add:

 

  And add under this path

 

  Click LIB – > Add files... And select the following files to add

 

 

 

  Click PORT - > Add files... And select the following files to add

 

  Click SOURCE – > Add files... And select the following files to add

 

 

Click CONFIG - > Add files... And select the following files to add

 

 

  Click BSP – > Add files... And select the following files to add

 

 

 

  Finally, be sure to click OK, or everything will be wasted!

For the above operations, please pay attention to the path and location of the file. Don't configure it wrong, otherwise there will be many errors!!

Import file path

 

  Find from project path

 

 

4.2 add code for bsp.c and bsp.h

// bsp.h
#ifndef  __BSP_H__
#define  __BSP_H__

#include "stm32f1xx_hal.h"

void BSP_Init(void);

#endif
// bsp.c
#include "includes.h"

#define  DWT_CR      *(CPU_REG32 *)0xE0001000
#define  DWT_CYCCNT  *(CPU_REG32 *)0xE0001004
#define  DEM_CR      *(CPU_REG32 *)0xE000EDFC
#define  DBGMCU_CR   *(CPU_REG32 *)0xE0042004

#define  DEM_CR_TRCENA                   (1 << 24)
#define  DWT_CR_CYCCNTENA                (1 <<  0)

CPU_INT32U  BSP_CPU_ClkFreq (void)
{
    return HAL_RCC_GetHCLKFreq();
}

void BSP_Tick_Init(void)
{
    CPU_INT32U cpu_clk_freq;
    CPU_INT32U cnts;
    cpu_clk_freq = BSP_CPU_ClkFreq();
    
    #if(OS_VERSION>=3000u)
        cnts = cpu_clk_freq/(CPU_INT32U)OSCfg_TickRate_Hz;
    #else
        cnts = cpu_clk_freq/(CPU_INT32U)OS_TICKS_PER_SEC;
    #endif
    OS_CPU_SysTickInit(cnts);
}



void BSP_Init(void)
{
    BSP_Tick_Init();
    MX_GPIO_Init();
}


#if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
void  CPU_TS_TmrInit (void)
{
    CPU_INT32U  cpu_clk_freq_hz;


    DEM_CR         |= (CPU_INT32U)DEM_CR_TRCENA;                /* Enable Cortex-M3's DWT CYCCNT reg.                   */
    DWT_CYCCNT      = (CPU_INT32U)0u;
    DWT_CR         |= (CPU_INT32U)DWT_CR_CYCCNTENA;

    cpu_clk_freq_hz = BSP_CPU_ClkFreq();
    CPU_TS_TmrFreqSet(cpu_clk_freq_hz);
}
#endif


#if (CPU_CFG_TS_TMR_EN == DEF_ENABLED)
CPU_TS_TMR  CPU_TS_TmrRd (void)
{
    return ((CPU_TS_TMR)DWT_CYCCNT);
}
#endif


#if (CPU_CFG_TS_32_EN == DEF_ENABLED)
CPU_INT64U  CPU_TS32_to_uSec (CPU_TS32  ts_cnts)
{
    CPU_INT64U  ts_us;
  CPU_INT64U  fclk_freq;

 
  fclk_freq = BSP_CPU_ClkFreq();
  ts_us     = ts_cnts / (fclk_freq / DEF_TIME_NBR_uS_PER_SEC);

  return (ts_us);
}
#endif
 
 
#if (CPU_CFG_TS_64_EN == DEF_ENABLED)
CPU_INT64U  CPU_TS64_to_uSec (CPU_TS64  ts_cnts)
{
    CPU_INT64U  ts_us;
    CPU_INT64U  fclk_freq;


  fclk_freq = BSP_CPU_ClkFreq();
  ts_us     = ts_cnts / (fclk_freq / DEF_TIME_NBR_uS_PER_SEC);
    
  return (ts_us);
}
#endif

4.3 modify the code in main.c

/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"
#include "usart.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <includes.h>
#include "stm32f1xx_hal.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* Task priority */
#define START_TASK_PRIO        3
#define LED0_TASK_PRIO        4
#define MSG_TASK_PRIO        5

/* Task stack size    */
#define START_STK_SIZE         64
#define LED0_STK_SIZE         64
#define MSG_STK_SIZE         64//If the size of the task heap is too large, an error will be reported. You can try to make it smaller

/* Task stack */    
CPU_STK START_TASK_STK[START_STK_SIZE];
CPU_STK LED0_TASK_STK[LED0_STK_SIZE];
CPU_STK MSG_TASK_STK[MSG_STK_SIZE];
/* Task control block */
OS_TCB StartTaskTCB;
OS_TCB Led0TaskTCB;
OS_TCB MsgTaskTCB;
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* Task function definition */
void start_task(void *p_arg);
static  void  AppTaskCreate(void);
static  void  AppObjCreate(void);
static  void  led_pc13(void *p_arg);
static  void  send_msg(void *p_arg);
/* 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 */
/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /**Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != 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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
    OS_ERR  err;
    OSInit(&err);
  HAL_Init();
    SystemClock_Config();
    //MX_GPIO_Init(); This is BSP It will also be initialized in the initialization of
  MX_USART1_UART_Init();    
    /* Create task */
    OSTaskCreate((OS_TCB     *)&StartTaskTCB,                /* Create the start task                                */
                 (CPU_CHAR   *)"start task",
                 (OS_TASK_PTR ) start_task,
                 (void       *) 0,
                 (OS_PRIO     ) START_TASK_PRIO,
                 (CPU_STK    *)&START_TASK_STK[0],
                 (CPU_STK_SIZE) START_STK_SIZE/10,
                 (CPU_STK_SIZE) START_STK_SIZE,
                 (OS_MSG_QTY  ) 0,
                 (OS_TICK     ) 0,
                 (void       *) 0,
                 (OS_OPT      )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR),
                 (OS_ERR     *)&err);
    /* Start the multitasking system and give control to uC/OS-III */
    OSStart(&err);            /* Start multitasking (i.e. give control to uC/OS-III). */
               
}


void start_task(void *p_arg)
{
    OS_ERR err;
    CPU_SR_ALLOC();
    p_arg = p_arg;
    
    /* YangJie add 2021.05.20*/
  BSP_Init();                                                   /* Initialize BSP functions */
  //CPU_Init();
  //Mem_Init();                                                 /* Initialize Memory Management Module */

#if OS_CFG_STAT_TASK_EN > 0u
   OSStatTaskCPUUsageInit(&err);          //Statistical tasks                
#endif
    
#ifdef CPU_CFG_INT_DIS_MEAS_EN            //If enabled, measure the interrupt off time
    CPU_IntDisMeasMaxCurReset();    
#endif

#if    OS_CFG_SCHED_ROUND_ROBIN_EN          //When using time slice rotation
     //Enable time slice rotation scheduling function,The time slice length is 1 system clock beat, i.e. 1*5=5ms
    OSSchedRoundRobinCfg(DEF_ENABLED,1,&err);  
#endif        
    
    OS_CRITICAL_ENTER();    //Enter critical zone
    /* Create LED0 task */
    OSTaskCreate((OS_TCB     * )&Led0TaskTCB,        
                 (CPU_CHAR    * )"led_pc13",         
                 (OS_TASK_PTR )led_pc13,             
                 (void        * )0,                    
                 (OS_PRIO      )LED0_TASK_PRIO,     
                 (CPU_STK   * )&LED0_TASK_STK[0],    
                 (CPU_STK_SIZE)LED0_STK_SIZE/10,    
                 (CPU_STK_SIZE)LED0_STK_SIZE,        
                 (OS_MSG_QTY  )0,                    
                 (OS_TICK      )0,                    
                 (void       * )0,                    
                 (OS_OPT      )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
                 (OS_ERR     * )&err);                
                 
    /* Create LED1 task */
    OSTaskCreate((OS_TCB     * )&MsgTaskTCB,        
                 (CPU_CHAR    * )"send_msg",         
                 (OS_TASK_PTR )send_msg,             
                 (void        * )0,                    
                 (OS_PRIO      )MSG_TASK_PRIO,         
                 (CPU_STK   * )&MSG_TASK_STK[0],    
                 (CPU_STK_SIZE)MSG_STK_SIZE/10,    
                 (CPU_STK_SIZE)MSG_STK_SIZE,        
                 (OS_MSG_QTY  )0,                    
                 (OS_TICK      )0,                    
                 (void       * )0,                
                 (OS_OPT      )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, 
                 (OS_ERR     * )&err);
                 
    OS_TaskSuspend((OS_TCB*)&StartTaskTCB,&err);        //Suspend start task             
    OS_CRITICAL_EXIT();    //Enter critical zone
}
/**
  * Function function: start the task function body.
  * Input parameter: p_arg is the formal parameter passed when the task was created
  * Return value: None
  * Description: None
  */
static  void  led_pc13 (void *p_arg)
{
  OS_ERR      err;

  (void)p_arg;

  BSP_Init();                                                 /* Initialize BSP functions                             */
  CPU_Init();

  Mem_Init();                                                 /* Initialize Memory Management Module                  */

#if OS_CFG_STAT_TASK_EN > 0u
  OSStatTaskCPUUsageInit(&err);                               /* Compute CPU capacity with no task running            */
#endif

  CPU_IntDisMeasMaxCurReset();

  AppTaskCreate();                                            /* Create Application Tasks                             */

  AppObjCreate();                                             /* Create Application Objects                           */

  while (DEF_TRUE)
  {
        HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET);
        OSTimeDlyHMSM(0, 0, 0, 500,OS_OPT_TIME_HMSM_STRICT,&err);
        HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
        OSTimeDlyHMSM(0, 0, 0, 500,OS_OPT_TIME_HMSM_STRICT,&err);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
static  void  send_msg (void *p_arg)
{
  OS_ERR      err;

  (void)p_arg;

  BSP_Init();                                                 /* Initialize BSP functions                             */
  CPU_Init();

  Mem_Init();                                                 /* Initialize Memory Management Module                  */

#if OS_CFG_STAT_TASK_EN > 0u
  OSStatTaskCPUUsageInit(&err);                               /* Compute CPU capacity with no task running            */
#endif

  CPU_IntDisMeasMaxCurReset();

  AppTaskCreate();                                            /* Create Application Tasks                             */

  AppObjCreate();                                             /* Create Application Objects                           */

  while (DEF_TRUE)
  {
            printf("hello world \r\n");
        OSTimeDlyHMSM(0, 0, 0, 500,OS_OPT_TIME_HMSM_STRICT,&err);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}


/* USER CODE BEGIN 4 */
/**
  * Function function: create application task
  * Input parameter: p_arg is the formal parameter passed when the task was created
  * Return value: None
  * Description: None
  */
static  void  AppTaskCreate (void)
{
  
}


/**
  * Function function: uCOSIII kernel object creation
  * Input parameters: None
  * Return value: None
  * Description: None
  */
static  void  AppObjCreate (void)
{

}

/* 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****/

4.4 final minor adjustments

Select the file and find the file startup_stm32f103xb.s

 

 

 

Change the content in the code to be the same as the blue part in the figure above.

Check app_cfg.h

 

 

 

 

 

  Locate the file includes.h

 

 # Add at include < BSP. H >

 

 

 

  Find lib_cfg.h

 

  It is modified to 5 here (the macro definition here sets the size of heap space. The RAM of STM32F103C8T6 is only 20K, so it needs to be reduced)

Since we use the printf function, we need to add the following code to the usart.c file to complete the printf redirection

/* USER CODE BEGIN 1 */
int fputc(int ch,FILE *f){
    HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,0xffff);
    return ch;
}
/* USER CODE END 1 */

5, Code for three tasks

Three tasks: two of them control the LED on-off in 1s and 3s cycles respectively; Another task sends "hello world!" through the serial port in a 2s cycle.
Modify the code in gpio.c (add initialization PA3)

void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);


  /*Configure GPIO pin : PC13|PA3 */
  GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_3;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}
modify main.c

/*
Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* Task priority */ #define START_TASK_PRIO 3 #define LED0_TASK_PRIO 4 #define MSG_TASK_PRIO 5 #define LED1_TASK_PRIO 6 /* Task stack size */ #define START_STK_SIZE 96 #define LED0_STK_SIZE 64 #define MSG_STK_SIZE 64 #define LED1_STK_SIZE 64 /* Task stack */ CPU_STK START_TASK_STK[START_STK_SIZE]; CPU_STK LED0_TASK_STK[LED0_STK_SIZE]; CPU_STK MSG_TASK_STK[MSG_STK_SIZE]; CPU_STK LED1_TASK_STK[LED1_STK_SIZE]; /* Task control block */ OS_TCB StartTaskTCB; OS_TCB Led0TaskTCB; OS_TCB MsgTaskTCB; OS_TCB Led1TaskTCB; /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* Task function definition */ void start_task(void *p_arg); static void AppTaskCreate(void); static void AppObjCreate(void); static void led_pc13(void *p_arg); static void send_msg(void *p_arg); static void led_pa3(void *p_arg); /* 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 */ /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /**Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != 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_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { OS_ERR err; OSInit(&err); HAL_Init(); SystemClock_Config(); //MX_GPIO_Init(); This is BSP It will also be initialized in the initialization of MX_USART1_UART_Init(); /* Create task */ OSTaskCreate((OS_TCB *)&StartTaskTCB, /* Create the start task */ (CPU_CHAR *)"start task", (OS_TASK_PTR ) start_task, (void *) 0, (OS_PRIO ) START_TASK_PRIO, (CPU_STK *)&START_TASK_STK[0], (CPU_STK_SIZE) START_STK_SIZE/10, (CPU_STK_SIZE) START_STK_SIZE, (OS_MSG_QTY ) 0, (OS_TICK ) 0, (void *) 0, (OS_OPT )(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR), (OS_ERR *)&err); /* Start the multitasking system and give control to uC/OS-III */ OSStart(&err); /* Start multitasking (i.e. give control to uC/OS-III). */ } void start_task(void *p_arg) { OS_ERR err; CPU_SR_ALLOC(); p_arg = p_arg; /* YangJie add 2021.05.20*/ BSP_Init(); /* Initialize BSP functions */ //CPU_Init(); //Mem_Init(); /* Initialize Memory Management Module */ #if OS_CFG_STAT_TASK_EN > 0u OSStatTaskCPUUsageInit(&err); //Statistical tasks #endif #ifdef CPU_CFG_INT_DIS_MEAS_EN //If enabled, measure the interrupt off time CPU_IntDisMeasMaxCurReset(); #endif #if OS_CFG_SCHED_ROUND_ROBIN_EN //When using time slice rotation //Enable time slice rotation scheduling function,The time slice length is 1 system clock beat, i.e. 1*5=5ms OSSchedRoundRobinCfg(DEF_ENABLED,1,&err); #endif OS_CRITICAL_ENTER(); //Enter critical zone /* Create LED0 task */ OSTaskCreate((OS_TCB * )&Led0TaskTCB, (CPU_CHAR * )"led_pc13", (OS_TASK_PTR )led_pc13, (void * )0, (OS_PRIO )LED0_TASK_PRIO, (CPU_STK * )&LED0_TASK_STK[0], (CPU_STK_SIZE)LED0_STK_SIZE/10, (CPU_STK_SIZE)LED0_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void * )0, (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, (OS_ERR * )&err); /* Create LED1 task */ OSTaskCreate((OS_TCB * )&Led1TaskTCB, (CPU_CHAR * )"led_pa3", (OS_TASK_PTR )led_pa3, (void * )0, (OS_PRIO )LED1_TASK_PRIO, (CPU_STK * )&LED1_TASK_STK[0], (CPU_STK_SIZE)LED1_STK_SIZE/10, (CPU_STK_SIZE)LED1_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void * )0, (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, (OS_ERR * )&err); /* Create MSG task */ OSTaskCreate((OS_TCB * )&MsgTaskTCB, (CPU_CHAR * )"send_msg", (OS_TASK_PTR )send_msg, (void * )0, (OS_PRIO )MSG_TASK_PRIO, (CPU_STK * )&MSG_TASK_STK[0], (CPU_STK_SIZE)MSG_STK_SIZE/10, (CPU_STK_SIZE)MSG_STK_SIZE, (OS_MSG_QTY )0, (OS_TICK )0, (void * )0, (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, (OS_ERR * )&err); OS_TaskSuspend((OS_TCB*)&StartTaskTCB,&err); //Suspend start task OS_CRITICAL_EXIT(); //Enter critical zone } /** * Function function: start the task function body. * Input parameter: p_arg is the formal parameter passed when the task was created * Return value: None * Description: None */ static void led_pc13 (void *p_arg) { OS_ERR err; (void)p_arg; BSP_Init(); /* Initialize BSP functions */ CPU_Init(); Mem_Init(); /* Initialize Memory Management Module */ #if OS_CFG_STAT_TASK_EN > 0u OSStatTaskCPUUsageInit(&err); /* Compute CPU capacity with no task running */ #endif CPU_IntDisMeasMaxCurReset(); AppTaskCreate(); /* Create Application Tasks */ AppObjCreate(); /* Create Application Objects */ while (DEF_TRUE) { HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_RESET); OSTimeDlyHMSM(0, 0, 1, 0,OS_OPT_TIME_HMSM_STRICT,&err); HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET); OSTimeDlyHMSM(0, 0, 1, 0,OS_OPT_TIME_HMSM_STRICT,&err); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } static void led_pa3 (void *p_arg) { OS_ERR err; (void)p_arg; BSP_Init(); /* Initialize BSP functions */ CPU_Init(); Mem_Init(); /* Initialize Memory Management Module */ #if OS_CFG_STAT_TASK_EN > 0u OSStatTaskCPUUsageInit(&err); /* Compute CPU capacity with no task running */ #endif CPU_IntDisMeasMaxCurReset(); AppTaskCreate(); /* Create Application Tasks */ AppObjCreate(); /* Create Application Objects */ while (DEF_TRUE) { HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET); OSTimeDlyHMSM(0, 0, 3, 0,OS_OPT_TIME_HMSM_STRICT,&err); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_SET); OSTimeDlyHMSM(0, 0, 3, 0,OS_OPT_TIME_HMSM_STRICT,&err); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } static void send_msg (void *p_arg) { OS_ERR err; (void)p_arg; BSP_Init(); /* Initialize BSP functions */ CPU_Init(); Mem_Init(); /* Initialize Memory Management Module */ #if OS_CFG_STAT_TASK_EN > 0u OSStatTaskCPUUsageInit(&err); /* Compute CPU capacity with no task running */ #endif CPU_IntDisMeasMaxCurReset(); AppTaskCreate(); /* Create Application Tasks */ AppObjCreate(); /* Create Application Objects */ while (DEF_TRUE) { printf("hello uc/OS \r\n"); OSTimeDlyHMSM(0, 0, 2, 0,OS_OPT_TIME_HMSM_STRICT,&err); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /* USER CODE BEGIN 4 */ /** * Function function: create application task * Input parameter: p_arg is the formal parameter passed when the task was created * Return value: None * Description: None */ static void AppTaskCreate (void) { } /** * Function function: uCOSIII kernel object creation * Input parameters: None * Return value: None * Description: None */ static void AppObjCreate (void) { } /* 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****/

 

6, Operation results

tips: after the burning is completed, you need to set BOOT1 and BOOT0 of the single chip microcomputer to 0, and then press the RESET key, the green light will start to flash, and the single chip microcomputer will start to work!! This is important

 

 

 

 

7, Experience

The configuration process this time is very complex and cumbersome. Many problems were encountered in the configuration process, and even forgot to set the pin in the first configuration. It's really frustrating_ T. During the second configuration, I didn't know where the problem occurred. The single chip microcomputer didn't respond when burning it. Finally, I did it again, changed a USB port, and finally succeeded. This configuration process is very difficult, but I learned a lot from it and honed my patience. At the same time, being able to build a system by yourself is a very cool thing. I feel the powerful function of this system!

8, Reference link

https://blog.csdn.net/qq_45659777/article/details/121570886

https://blog.csdn.net/weixin_43116606/article/details/105532222

Posted by 9three on Sat, 04 Dec 2021 22:44:45 -0800