STM32F103C8T6 Porting uC/OS-III Based on HAL Library

Keywords: Single-Chip Microcomputer stm32 ARM

1. Get uC/OS-III source code

1. Download on the official website

Enter the download center of Micrium's official website: Micrium Software and Documentation - Silicon Labs
Select the ST series and click View all STMicroelectronics.

 

 

2. Building stm32 hal Library Project

Open stm32 cubeMX, select the chip stm32f103c8, configure the system clock to 72M, as a porting test, connect the LED to the two ports PB0, PB1 is configured as GPIO_Output, based on the phenomenon of LED, can be used as the basis of whether we have successfully transplanted.

Folder after generating keil project:

3. Copy uC/OS-III files to the project folder

1. In the generated keil project folder f103c8_ UCOSIII_ 1_ Create a new UCOSIII folder in test and copy the three folders in the source code we downloaded: Uc-CPU, uC-LIB, Ucos-III into the folder we created:

2. Create a new OS folder under the Scr folder: 

3. Open the source you just downloaded and put the path to: EvalBoardsMicriumuC-Eval-STM32F107uCOS-III:
app.c, app_cfg.h, cpu_cfg.h, include.h, lib_cfg.h, os_app_hooks.c, os_app_hook.h, os_cfg.h, os_cfg_app.h copies to the OS folder you created in the previous step, creating three new blank files at the same time: bsp.c, bsp.h, app.h:

4. Add project components and header file paths

1. Add Project Groups

1. Open f103c8_uCOSIII_1_test project, add six new groups as shown in the figure: bsp, uCOSIII_CPU, uCOSIII_LIB, uCOSIII_Ports, uCOSIII_Source, OS_cfg:

2. Add files to groups
File directory is: Src/OS, add bsp.c and bsp.h files to the BSP group, and add app.c to the Application/User group 

uCOSIII_CPU component, click the Add Files... button, jump the file directory to: UCOSIII/uC-CPU, select ALL files file type, click Add to add three of the files, and then open: ARM-Cortex-M3\RealView, also select ALL files file type, add three files to uCOSIII_CPU Group

 

 

Add uCOSIII_LIB component file: select uCOSIII_LIB group, click the Add Files... button, jump the file directory to: UCOSIII/uCLIB, select ALL files file type, add nine of them to uCOSIII_LIB group; Then continue opening: Ports/ARM-Cortex-M3/Realview, add lib_mem_a.asm file

Select uCOSIII_Ports group, click the Add Files... button to adjust the file directory to: UCOSIII/UcosIII/Ports/RAM-Cortex-M3/Generic/RealView. Select the ALL files file type and add three of them to uCOSIII_Ports Group

Select uCOSIII_ Source group, click the Add Files... button to adjust the file directory to: UCOSIII/UcosIII/Source. Select the ALL files file type and add twenty of them to uCOSIII_Sourc group. 

Select OS_cfg group, click the Add Files... button to adjust the file directory to: Src/OS. Select the ALLfiles file type to add eight files to uCOSIII_Sourc group: 

Project file structure after all component files have been added: 

 

2. Add header file path 

 

5. Modify the contents of the document

1. Startup File

 

2. app_cfg.h
First modification:
Before modification

#define APP_CFG_SERIAL_EN DEF_ENABLED

Modified

#define APP_CFG_SERIAL_EN DEF_DISABLED

Second modification:
Before modification

#define APP_TRACE BSP_Ser_Printf

Modified

#define APP_TRACE (void)

3. includes.h
First modification: Add related header file
Before modification

#include <bsp.h>

Modified

#include <bsp.h>
#include "gpio.h"
#include "app_cfg.h"
#include "app.h"

Second modification: adding the HAL Library
Before modification

#include <stm32f10x_lib.h>

Modified

#include "stm32f1xx_hal.h"
 

4. bsp.c and bsp.h

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

// bsp.h
#ifndef  __BSP_H__
#define  __BSP_H__

#include "stm32f1xx_hal.h"

void BSP_Init(void);

#endif


 

  5. app.c and app.h

// app.c
#include <includes.h>

// app.h
#ifndef  __APP_H__
#define  __APP_H__

#include <includes.h>

#endif /* __APP_H__ */

 6. main.c

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <includes.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 */
//Task Control Block
static  OS_TCB   AppTaskStartTCB;

//task stack
static  CPU_STK  AppTaskStartStk[APP_TASK_START_STK_SIZE];

/* Private function prototype----------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static  void  AppTaskCreate(void);
static  void  AppObjCreate(void);
static  void  AppTaskStart(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)
{
  /* USER CODE BEGIN 1 */
	OS_ERR  err;
  /* 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 */
	OSInit(&err);    
  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
//  MX_GPIO_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
                                                                                 
	/* Create Task */
	OSTaskCreate((OS_TCB     *)&AppTaskStartTCB,                /* Create the start task                                */
				 (CPU_CHAR   *)"App Task Start",
				 (OS_TASK_PTR ) AppTaskStart,
				 (void       *) 0,
				 (OS_PRIO     ) APP_TASK_START_PRIO,
				 (CPU_STK    *)&AppTaskStartStk[0],
				 (CPU_STK_SIZE) APP_TASK_START_STK_SIZE / 10,
				 (CPU_STK_SIZE) APP_TASK_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 a multitasking system and give control to uC/OS-III */
	OSStart(&err);            /* Start multitasking (i.e. give control to uC/OS-III). */
               
}


/**
  * Function function: Start the task function body.
  * Input parameter: p_arg is a parameter passed in when the task is created
  * Return value: None
  * Description: None
  */
static  void  AppTaskStart (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_TogglePin(LED0_GPIO_Port,LED0_Pin);
		HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin, 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 */
}


/* USER CODE BEGIN 4 */
/**
  * Functional functions: Create application tasks
  * Input parameter: p_arg is a parameter passed in when the task is created
  * Return value: None
  * Description: None
  */
static  void  AppTaskCreate (void)
{
  
}


/**
  * Function function: uCOSIII kernel object creation
  * Input parameter: 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****/


 

7. lib_cfg.h

There is a macro definition in this header file:

#define  LIB_MEM_CFG_HEAP_SIZE          27u * 1024u

 

8.app.c

#include "includes.h"

6. Operation results

 

 

 

Posted by hadingrh on Sat, 04 Dec 2021 11:13:47 -0800