uc/OS system transplantation and task implementation based on STM32F103

uc/OS system transplantation and task implementation based on STM32F103

1. Establishment of stm32cubemx project

Select chip STM32C8

Configure RCC

Configure SYS

Set PB8 to GPIO_Output is used to light the LED lamp [PC13 is used later]

Set serial port USART1

Project management: set the following options one by one

Click GENERATE CODE to GENERATE the project

Click OpenProject to enter the Keil project file

2 Keil engineering modification

2.1 modify code

Add the following loop statement in the main.c file:

while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
		HAL_Delay(500);
		HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
		HAL_Delay(500);
  }
  /* USER CODE END 3 */

2.2 burning LED code

C8T6 burning, boot0-1, boot1-0.
F lymcu software burning:

2.3 operation results

Note: there is no problem with the Keil file, so you can perform the following operations.

3 uc/OS system migration

3.1 download source code

Prepare uCOSIII source code: Download from the official website

Open the software file. The folders UC BSP and UC config are two newly created folders.

3.2 preparation of relevant documents

Create new bsp.c and bsp.h files for the UC BSP folder.
And add the following files to the folder UC config:

Copy the Software related files to the MDK-ARM folder of HAL project


Go back to keil project file and create a new folder for the project (as shown in figure ② below)
Add files to the new folder respectively:

  1. CPU folder

  2. LIB folder
  3. PORT folder
  4. SOURCE folder
  5. CONFIG folder
  6. BSP folder:

Be sure to click OK!

Project directory at this time:

Import file path


File path to import

3.3 coding

  1. Add code for the bsp.c and bsp.h files in the BSP folder

    // 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
    
    
  2. Modify the main.c code

    ------------------------------------------------------------------*/
    #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 task heap size 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 will also be initialized in BSP initialization  
    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 the 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 the critical area} / * * function function: start the task function body. * input parameters:
    p_arg Is a formal parameter passed when the task is created   * Return value: nothing   * say    Ming: 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 creating the task * 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****/
    
    

  3. Change PendSV_Handler and SysTick_Handler to
    OS_CPU_PendSVHandler and OS_CPU_SysTickHandler


  4. Change DEF_ENABLED to
    DEF_DISABLEDAdd #define app#u trace bsp#ser#u printf
    Change to #define APP_TRACE (void)


  5. Add #include "gpio.h" and
    #include "app_cfg.h"

    Change #include < stm32f10x_lib. H > to #include "stm32f1xx_hal.h"


  6. Change the number here to
    5u is enough


  7. Add code here

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

3.4 configuration parameters

Click "magic wand"

3.5 compilation and operation

Compile build:
No error!

Operation results after burning:

LED flashing

Serial port sending data

4 three tasks

Three tasks: two of them control the LED on-off in 1s and 3s cycles respectively; the other task sends "hello uc/OS! Welcome to RTOS multitasking environment!" through the serial port in 2s cycles.

4.1 code modification

To modify the code in gpio.c file is to modify, not copy and paste all. You can comment out the previous code. (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 the main.c code (be careful not to forget the header file)

/* 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 will also be initialized in BSP initialization
  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 the 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 is 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 is 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.2 compilation and operation

Serial port sending data

Flashing of both LED s

5. Waveform analysis by logic analyzer

5.1 waveform in keil simulation

Click Debug and select Debug to Debug

Select logic analyzer

Enter the dir vtreg command in the lower left corner to see which pins are detected

Click Setup to set

Click ① to run at full speed to observe the waveform. To stop, click ② to stop. As shown in the following figure:

Final waveform:

5.2 waveform in logic analyzer

Please refer to the operating instructions. pdf file of the latest SALEAE16 software for the operating instructions

Final output waveform:
Compared with the waveform simulated by Keil engineering, it is almost consistent. Channel0 is PORTA.3,Channel1 is usart_sr, and channel2 is PORTC.13

It can be seen that the actual cycle of A3 is 2.998s

The actual cycle of C13 is 2s
It can be seen that there is a certain gap between the actual data and the theoretical data.

Zoom in at the designated position of the mouse (where the serial port waveform is downward), and you can see:
Set the level to view each character

It can be seen that there are white dots (read from right to left): "h" corresponds to ASClI code 0x68 (0110 1000), "e" corresponds to ASCII code 0x65(0110 0101), "I" corresponds to ASCII code 0x6B (01101100), with free bits in the middle.

6 Summary

  • When adding a SOURCE file, don't forget the. h file. The default is. c file. Remember to check the integrity after adding.
  • After modification in the main function, the header file is forgotten and 30 errors are reported. Pay attention to what cannot be modified when modifying the code.
  • When the simulation debugging is stuck, an. ini file should be used. Refer to the references for details.

7 reference

STM32F103C8T6 porting uCOS based on HAL Library
What is UCOS operating system and its features
KEIL5 software simulation * * * error 65: access violation at 0xe0001004: no 'read' permission

Keywords: Single-Chip Microcomputer stm32 ARM

Added by Dujo on Wed, 08 Dec 2021 07:00:32 +0200