Transplantation of STM32F103 UC / OS System

 

Article catalog

preface

μ C/OS-II, provided by Micrium, is a portable, curable, tailorable, preemptive multitasking real-time core. It is suitable for a variety of microprocessors, microcontrollers and digital processing chips (it has been transplanted to more than 100 microprocessor applications). At the same time, the source code of the system is open, neat and consistent, with detailed comments, which is suitable for system development. μ C/OS-II has been certified by the Federal Aviation Administration (FAA) as a commercial aircraft and complies with the DO-178B standard of the Aeronautical Radio Technical Committee (RTCA). Now the latest version is μ C/OS-III.

1, Get uc/OS-III source code

Baidu link: https://pan.baidu.com/s/14rrirGKKcvdtkkEJIaidxA  
Extraction code: o3co

2, Project import folder

2.1 arrangement of documents

(1) Create two new folders, UC BSP and UC config, which will be used later

  (2) Create bsp.c and bsp.h files in the UC BSP you just created

2.2 import source code

Copy the following file to the newly created file under UC config

 

2.3 stm32cubemx creation framework

Set pins PB1 and PB0 to GPIO pin OUT PUT to observe that the LED lights under the two tasks flash, and set PA10 to USART-RX, PA9 to USART-TX, pin PA13 to SYS and PD1 to RCC_OSC_IN, PD2 set to RCC_OSC_OUT,

In addition, SYS needs to be set

And set RCC

  Set the clock to 72MHZ

Then generate MDK5 project

   3, Add code

3.1 copy the source file to the specified folder

  3.2 adding files

Add six files CPU, LIB, PORT, SOURCE, CONFIG and BSP in MDK5

(1) Add the blank files bsp.c and bsp.h just created under BSPfile

(2) Add file8 under file CONFIG  

(3)   Add 20 files to the file SOURCE from the file directory mdk-arm / ucous-iii / SOURCE

  (4) Add 3 files under the file PORT, from the file directory MDK-ARM/uCOS-III/Ports/ARM-Cortex-M3/Geneic/RealView

(5) Add file6 under file CPU

Including 3 under UC CPU and 3 under mdk-arm / UC CPU / arm-cortex-m3 / RealView

(6) Add 10 under Lib

3.3 add file path

Add 8 files as shown in the figure

  4, Modify code

4.1 modify the start up file

PendSV_ Modify handler to OS_CPU_PendSVHandler

SysTick_ Modify handler to OS_CPU_SysTickHandler

There are two places in the function that need to be modified

4.2 modify CONFIG/app_cfg.h

#define APP_CFG_SERIAL_EN         DEF_ENABLED is changed to

#define APP_CFG_SERIAL_EN        DEF_DISABLED

#define APP_ TRACE BSP_ Ser_ Change printf to #define APP_TRACE (void)

   4.3 modify CONFIG/includes.h   

Add after #include < BSP. H >     # include “gpio.h”          # include “app_cfg.h”

Set #include < stm32f10x_ Change lib. H > to #include "stm32f1xx_hal.h"

4.4 modify BSP/bsp.c

// 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.5bsp.h

// bsp.h
#ifndef  __BSP_H__
#define  __BSP_H__
 
#include "stm32f1xx_hal.h"
 
void BSP_Init(void);
 
#endif

 

4.6CONFIG/lib_cfg.h

lib_ Modify the macro definition of heap space in CFG. H, which was originally 27K, but the total RAM of this board is only 20k, which is modified to 5k

4.7usart.c

(1) Parameter setting

(2) Add header file

(3) Add printf function

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

 

4.8main.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
#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_pb0(void *p_arg);
static  void  send_msg(void *p_arg);
static  void  led_pb1(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_pb0", 		
                 (OS_TASK_PTR )led_pb0, 			
                 (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_pb1", 		
                 (OS_TASK_PTR )led_pb1, 			
                 (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_pb0 (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(GPIOB,GPIO_PIN_0,GPIO_PIN_RESET);
		OSTimeDlyHMSM(0, 0, 1, 0,OS_OPT_TIME_HMSM_STRICT,&err);
		HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,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_pb1 (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(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET);
		OSTimeDlyHMSM(0, 0, 3, 0,OS_OPT_TIME_HMSM_STRICT,&err);
		HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,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****/

  Replace this part of the code with the original code snippet of main.c.

5, Operation results

 

experience  

        In the whole experiment, it is easy to make mistakes in the eight steps of adding files. It is easy to make mistakes in adding files. Because too many files are dazzling, you need to work harder. Moreover, it is easy to miss when changing the header file of the code later. This needs to be searched more carefully in all the code. Finally, make the experimental results, have a sense of achievement, and continue to work hard.

reference  

Porting uC/OS-III operating system based on stm32cubemx_ Laul Ken Yi's blog - CSDN blog directory 1. Obtain uC/OS-III SOURCE code 2. Folder sorting 2.1 create a new folder 2.2 create an empty file 2.3 import SOURCE code 2.4 create a basic stm32 framework 2.5 import files and add header function paths 2.6 open keil, add item 2.6.1, and add the newly created blank files bsp.c and bsp.h under BSP and file. 2.6.2 add file under CONFIG, 8 2.6.3 add file under SOURCE, 20 add file under 2.6.4 PORT, 3 add file under 2.6.5 CPU, 6 add 2https://blog.csdn.net/qq_60678931/article/details/121618935?spm=1001.2014.3001.5501STM32F103C8 porting uCOSIII (HAL Library)_ junseven164 blog - CSDN blog Youth 1. Essay 2. uCOSIII source code 3. Project import file sorting 4. Import file and add header function path 5. Code change 6. Reference 1. Essay transplantation of an embedded system took a day. It can only be said that I am worthy of being me. Without understanding, I still do less of my own operations and bury myself. Record the process and the pits you stepped on. 2, uCOSIII source code can be downloaded from the official website, but it's troublesome. I won't describe it much. Here is a source code (Baidu online disk) with extraction code 1111 https://pan.baidu.com/s/1jCoc2sfiij78J0Q-C6ChmA 3, Project import file collation creates these two files on this pagehttps://blog.csdn.net/junseven164/article/details/121534916STM32F103C8T6 porting uCOS based on HAL Library_ Smart orange blog - CSDN blog article directory 1. Use CubeMX to establish STM32F103C8T6HAL library 2. Prepare uCOSIII source code 3. Prepare before transplantation 1. Create bsp.c and bsp.h files for UC BSP Folder 2. Add the following files to UC config folder (copy from the following path) 3. Copy uCOS related files to the MDK-ARM folder of HAL project IV. start migration 1. Add uCOS files to the project 2. Add codes for bsp.c and bsp.h 3. Modify the code of main.c file 4. Modify the codes of other files 5. Parameter configuration 5. Operation 6. Summary 7. Reference link 1. Establish STM32F103C8T6HAL using CubeMXhttps://blog.csdn.net/qq_45659777/article/details/121570886

 

 

 

 

 

Tags: Machine Learning stm32

Posted on Fri, 03 Dec 2021 13:56:48 -0500 by kh411dz