STM은 STM32CubeMX라는 프로그램을 사용하여 FreeRTOS와 같은 라이브러리를 쉽게 포팅할 수 있다. 하지만 한번쯤은 CubeMX는 기본 프로젝트만 생성하고 베어메탈(Bare Metal,하드웨어 상에 어떤 소프트웨어도 설치되어 있지 않은 상태) 상태에서 FreeRTOS를 포팅해보고 싶었다. 그 과정을 공유드립니다.
개발환경
STM32F7508-DK 보드 사용
STM32CubeIDE – 1.10.1
CubeMX – 6.7.0
STM32F7 MCU Package – 1.17.0
1. CubeMX로 프로젝트 생성
먼저 CubeMX를 이용하여 기본 프로젝트를 생성합니다. 제가 가진 보드에 맞게 아래와 같이 설정하였습니다. – GPIO_PI1 – GPIO_Output, LED 확인용 – USART1 – 로그 확인용 – HSE – Crystal/Ceramic Resonator – 외부 크리스탈 사용 – Debug – Serial Wrie – Sys – Timebase Source: TIM6
GPIO 설정
RCC 설정
Debug, Timebase Source 설정
USART1 설정
FreeRTOS 설정은 하지 않았습니다.
HCLK 설정은 216MHz로 설정
프로젝트 이름은 FreeRTOS_Porting, IDE는 STM32CubeIDE로 선택
“Generate peripheral initialization as a pair of ‘.c/.h’ files per peripheral” 체크
위와 같이 설정하고 GENERATE CODE를 합니다.
2. STM32CubeIDE로 프로젝트 열기
Impoert Projects…클릭
Existing Projects into Workspace 선택 후 Next >
생성한 위치에 가서 프로젝트 열고 Finish 클릭프로젝트 선택하여 Build 하면 다음과 같이 성공적으로 빌드되는 것을 확인
위와 같이 빌드가 성공적으로 되었으면 기본 프로젝트 생성은 완료 되었습니다.
3. FreeRTOS를 포팅하기 위한 프로젝트 구조 만들기
프로젝트에 Source Folder 추가
Lib라고 정하겠습니다.생성된 Source Lib 폴더에 오른쪽 클릭 -> New > Folder 선택
FreeRTOS 다운로드- 우측 상단에 Download FreeRTOS를 클릭하여 위 페이지에서 Download 버튼을 클릭하여 다운로드 할 수 있다. 2023-06-16기준 버전 202212.01
다운 받은 압축 파일을 풀고 폴더 안을 보면 다음과 같이 파일들이 존재합니다.
다운받은 FreeRTOS 파일들
여기서 저희는 FreeRTOS 폴더를 사용할 것입니다.
먼저, FreeRTOS폴더 안에 있는 License폴더를 프로젝트에서 생성한 FreeRTOS 폴더에 복사해줍니다.
또, FreeRTOS > Source 폴더로 가면 아래와 같이 파일이 있습니다. 선택한 파일들을 프로젝트에서 생성한 FreeRTOS 폴더에 복사해줍니다.
체크된 파일 복사
이제 프로젝트 폴더로 와서 Lib > FreeRTOS > Portable 폴더로 가면 다양한 폴더들이 존재합니다. 여기서 저희는 GCC 기반인 STM32CubeIDE를 사용할 것이므로 GCC 폴더와, 메모리 관련 코드가 들어있는 MemMang 폴더를 남겨두고 나머지 파일들은 삭제합니다
GCC와 MemMang를 제외하고 삭제
GCC, MemMang 폴더만 남기고 삭제
GCC 폴더로 들어가면 다양한 프로세서별 코드가 존재합니다. 여기서 저는 M7을 사용하므로 ARM_CM7을 제외하고 모두 삭제합니다. ( 사용자에 맞는 프로세서만 남기고 삭제)
M7을 제외하고 모두 삭제
또 MemMang 폴더로 가서 heap_4.c 를 제외하고 나머지는 삭제합니다.(안 그러면 충돌 일어나요)
heap_4.c만 남기고 모두 삭제
이제 CubeIDE로 와서 프로젝트를 Refresh 해줍니다.
프로젝트 Refresh
Refresh 된 프로젝트 구조
이제 프로젝트 빌드를 해봅니다.
에러 발생
오마이갓! 수 많은 에러가 나타납니다. 보통 이렇게 많은 에러가 발생하게 되면 겁먹고 여기서 중단하게 됩니다. 하지만 당황하지 말고 하나하나 처리해나가봅니다.
FreeRTOS.h 를 찾을 수 없는 에러
위 에러는 FreeRTOS.h를 찾을 수 없다는 에러인데 우리는 파일만 추가 했을 뿐 프로젝트에서 include시켜주진 않았습니다. 프로젝트 속성으로 가서
Project Properties
MCU GCC Compiler > Include paths 메뉴로 가서 다음을 추가해줍니다.
** 여기서 ARM_CM7 path는 사용하는 프로세서 마다 다르니 꼭 확인이 필요합니다.
위와 같이 Include Paths를 추가해줍니다.
추가 되었으면 Apply and Close를 누른 후 다시 Build해줍니다.
이번엔 FreeRTOSConfig.h가 없다고 메세지가 나오네요..
홈페이지에 가 봅니다..
FreeRTOSConfig.h 페이지
Kernal > Developer Docs > FreeRTOSConfig.h 에 들어가니 헤더 파일 내용이 있습니다. 이 내용을 복사하셔도 되고, 다운받은 FreeRTOS 라이브러리 안에 Demo 폴더에 보면 각 프로세스마다 파일이 있는데 여기서 복사하셔도 됩니다. 저는 Demo 폴더에서 FreeRTOSConfig.h를 가져오겠습니다.
STM32F7 Demo폴더에서 FreeRTOSConfig.h를 복사하여
프로젝트의 FreeRTOS 폴더에 붙여넣기
그러고 또 프로젝트 Build를 하면 다음과 같이 에러가 발생합니다.
SVC_Handler, PendSV_Handler, SysTick_Handler 중복 사용 에러
내용을 보니 SVC_Handler와 PendSV_Handler, SysTick_Handler가 중복 정의되어 충돌이 일어나는거 같습니다.
PendSV_Handler 사용처
확인해보니 다음 두 곳에서 정의되는거 같네요.
우리는 FreeRTOS를 사용할 거니까 stm32f7xx_it.c에서 충돌나는 Handler 함수를 지워줍니다.
저는 SVC_Handler와 PendSV_Handler, SysTick_Handler 앞에 __weak를 붙여 해결했습니다.
__weak를 붙여주면 다른 곳에 같은 이름의 함수가 사용될 경우 이 함수는 사용되지 않음.
다시 프로젝트 Build를 하면 다음과 같이 에러가 발생합니다.
undefined reference to vApplicationTickHook 에러 발생
이제 FreeRTOSConfig.h로 갑니다.
vApplicationTickHook과 관련된 define이 뭐가 있을까하고 Tick을 검색해봅니다.
configUSE_TICK_HOOK을 0으로 설정해줍니다.
configUSE_TICK_HOOK이 있네요. 이 것을 0으로 설정해주고 다시 Build해 봅니다.
vAssertCalled 발생
그러면 vApplicationTickHook 에러가 사라졌는데 vAssertCalled 에러가 발생했네요
FreeRTOS홈페이지의 FreeRTOSConfig.h 페이지에서 아래 내용을 확인, 복사합니다.
다시 프로젝트의 FreeRTOSConfig.h로 와서 configASSERT 정의를 찾아 바꿔줍니다.(위 구문 그대로 복사하면 에러가 납니다… configASSERT와 ( x ) 사이의 공백을 제거해 주세요..)
또 프로젝트 build를 하면 아래와 같이 에러가 나타납니다.
undefined reference to vApplicationStackOverflowHook 에러 발생
그래서 다시 FreeRTOSConfig.h 에서 Stack Overflow에 관련된 정의를 찾으니 configCHECK_FOR_STACK_OVERFLOW가 있네요. 이걸 0으로 바꿔줍니다.
configCHECK_FOR_STACK_OVERFLOW를 0으로 설정
그리고 다시 프로젝트 빌드를 하면
에러 없는 빌드
짠!!! 드디어 에러를 다 잡았습니다.
이제 main.c로 가서 예제 코드를 작성해봅니다.
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.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 */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MPU_Config(void);
/* USER CODE BEGIN PFP */
static void task1_handler(void* parameters);
static void task2_handler(void* parameters);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
TaskHandle_t task1_handle;
TaskHandle_t task2_handle;
BaseType_t status;
/* USER CODE END 1 */
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* 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 */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
status = xTaskCreate(task1_handler, "Task-1", 256, "Hello Task-1", 2, &task1_handle);
status = xTaskCreate(task2_handler, "Task-2", 256, "Hello Task-2", 2, &task2_handle);
vTaskStartScheduler();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 432;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Activate the Over-Drive mode
*/
if (HAL_PWREx_EnableOverDrive() != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses 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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
static void task1_handler(void* parameters)
{
while (1)
{
vTaskDelay(1);
printf("%s\r\n", (char*)parameters);
}
}
static void task2_handler(void* parameters)
{
while (1)
{
vTaskDelay(1);
printf("%s\r\n", (char*)parameters);
}
}
/* USER CODE END 4 */
/* MPU Configuration */
void MPU_Config(void)
{
MPU_Region_InitTypeDef MPU_InitStruct = {0};
/* Disables the MPU */
HAL_MPU_Disable();
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.BaseAddress = 0x0;
MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
MPU_InitStruct.SubRegionDisable = 0x87;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/* Enables the MPU */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
}
/**
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM6 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
* @param htim : TIM handle
* @retval None
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM6) {
HAL_IncTick();
}
/* USER CODE BEGIN Callback 1 */
/* USER CODE END Callback 1 */
}
/**
* @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 */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
위와같이 task1과 task2를 만들고 parameter를 출력하도록 작성하였습니다.
컴파일을 해봅니다.
vApplicationMallocFailedHook 에러 발생
vApplicationMallocFailedHook 에러가 발생했습니다.
이 에러를 제거하기 위해선 vApplicationMallocFailedHook 함수를 만들거나 FreeRTOSConfig.h에서 관련 config를 제거하는 방법이 있습니다. 저는 config를 제거하겠습니다.
vApplicationMallocFailedHook 에러를 제거하기 위해 configUSE_MALLOC_FAILED_HOOK config=0으로 설정
컴파일을 하면 에러가 없는 것을 확인하였고, 다운로드를 하면 아래와 같이 로그를 볼 수 있습니다.
다른 프로세서를 사용하거나 다른 데모에서 config 파일을 가져왔으면 과정이 조금씩 다를 수 있으나 큰 틀로 보면 비슷할 겁니다.
/**
* Copyright (c) 2014 - 2021, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/** @file
* @defgroup temperature_example_main main.c
* @{
* @ingroup temperature_example
* @brief Temperature Example Application main file.
* @details
* This file contains the source code for a sample application using the temperature sensor.
* This contains workaround for PAN_028 rev2.0A anomalies 28, 29,30 and 31. PAN 43 is not covered.
* - PAN_028 rev2.0A anomaly 28 - TEMP: Negative measured values are not represented correctly
* - PAN_028 rev2.0A anomaly 29 - TEMP: Stop task clears the TEMP register.
* - PAN_028 rev2.0A anomaly 30 - TEMP: Temp module analog front end does not power down when DATARDY event occurs.
* - PAN_028 rev2.0A anomaly 31 - TEMP: Temperature offset value has to be manually loaded to the TEMP module
* - PAN_028 rev2.0A anomaly 43 - TEMP: Using PPI between DATARDY event and START task is not functional.
*
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "nrf.h"
#include "nrf_delay.h"
#include "nrf_temp.h"
#include "app_error.h"
#include "bsp.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
/** @brief Function for main application entry.
*/
int main(void)
{
// This function contains workaround for PAN_028 rev2.0A anomalies 28, 29,30 and 31.
int32_t volatile temp;
nrf_temp_init();
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("Temperature example started.");
while (true)
{
NRF_TEMP->TASKS_START = 1; /** Start the temperature measurement. */
/* Busy wait while temperature measurement is not finished, you can skip waiting if you enable interrupt for DATARDY event and read the result in the interrupt. */
/*lint -e{845} // A zero has been given as right argument to operator '|'" */
while (NRF_TEMP->EVENTS_DATARDY == 0)
{
// Do nothing.
}
NRF_TEMP->EVENTS_DATARDY = 0;
/**@note Workaround for PAN_028 rev2.0A anomaly 29 - TEMP: Stop task clears the TEMP register. */
temp = (nrf_temp_read() / 4);
/**@note Workaround for PAN_028 rev2.0A anomaly 30 - TEMP: Temp module analog front end does not power down when DATARDY event occurs. */
NRF_TEMP->TASKS_STOP = 1; /** Stop the temperature measurement. */
NRF_LOG_INFO("Actual temperature: %d", (int)temp);
nrf_delay_ms(500);
NRF_LOG_FLUSH();
}
}
/** @} */
위는 500ms마다 온도 값을 읽는 동작을 하는 코드입니다.
위 코드를 참조하여 다른 ble_app_uart 와 같은 예제 프로그램에서 적용하니까 다음과 같은 에러가 나타나면서 Hard Fault가 발생하는 것을 보게 됩니다.