Toby's Study Blog
article thumbnail
Published 2023. 9. 18. 21:14
[stm32] RTOS ์‹ค์Šต embedded

๐Ÿšƒ ์‹ค์Šต ๋ชฉํ‘œ

FreeRTOS๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‘ ๊ฐœ์˜ ์Šค๋ ˆ๋“œ ๊ฐ„ ํ†ต์‹  ์˜ˆ์ œ ๊ตฌํ˜„ 

 

1.1 ๋‘ ๊ฐœ์˜ Task ์ƒ์„ฑ ๋ฐ osEventFlags, osThreadFlags
    (FreeRTOS์—์„œ ์ œ๊ณตํ•˜๋Š” ์ด๋ฒคํŠธ ํ”Œ๋ž˜๊ทธ ๊ด€๋ฆฌ API)  ์ƒ์„ฑ

 1.1 EventFlag๊ฐ€ define ๋˜์–ด์žˆ์„ ๊ฒฝ์šฐ thread1_Task() ์—์„œ  osEventFlags ์ƒ์„ฑ 
 1.2 EventFlag๊ฐ€ define ๋˜์–ด์žˆ์ง€ ์•Š์„ ๊ฒฝ์šฐ thread1_Task() ์—์„œ osThreadFlags ์ƒ์„ฑ
 1.3 EventFlag๊ฐ€ define ๋˜์–ด์žˆ์„ ๊ฒฝ์šฐ thread2_Task() ์—์„œ ์ด๋ฒคํŠธ ํ”Œ๋ž˜๊ทธ ๊ฐ์ฒด ๋Œ€๊ธฐ
 1.4 EventFlag๊ฐ€ define ๋˜์–ด์žˆ์ง€ ์•Š์„ ๊ฒฝ์šฐ thread2_Task() ์—์„œ thread1์˜ ํ”Œ๋ž˜๊ทธ ๋Œ€๊ธฐ


1.2 LED2, LED3 ํ† ๊ธ€ ๋ฐ flag๋ฅผ ์ถœ๋ ฅ์‹œ์ผœ ๋‘ ๊ฐœ์˜ task(์Šค๋ ˆ๋“œ) ๊ฐ„ ํ†ต์‹  ์ˆ˜ํ–‰ ํ™•์ธ   

 

๐Ÿšƒ RTOS ์„ธํŒ…

์ตœ์ดˆ ์„ค์ •๊ณผ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•˜์˜€์œผ๋ฉฐ ์„ธํŒ… ์š”์†Œ๋“ค์— ๋Œ€ํ•ด ์กฐ์‚ฌ๋Š” ์‹œ๊ฐ„ ๊ด€๊ณ„์ƒ ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.
 

๐Ÿšƒ ์‹ค์Šต ๊ฒฐ๊ณผ

https://github.com/Tobbyvv/stm32-project/tree/main/rtos_cli

1. ์Šค๋ ˆ๋“œ 1์—์„œ ์ด๋ฒคํŠธ ํ”Œ๋ž˜๊ทธ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ 100ms๋งˆ๋‹ค 0 ๋ถ€ํ„ฐ 3์˜ ์ด๋ฒคํŠธ ํ”Œ๋ž˜๊ทธ๋ฅผ ์ƒ์„ฑ
2. ์Šค๋ ˆ๋“œ 2์—์„œ 1์ดˆ ๊ฐ„๊ฒฉ์œผ๋กœ ์ด๋ฒคํŠธ ํ”Œ๋ž˜๊ทธ ์ˆ˜์‹  ์ฒดํฌ
3. ์Šค๋ ˆ๋“œ 2์—์„œ ์ด๋ฒคํŠธ ํ”Œ๋ž˜๊ทธ๊ฐ€ ์ฒดํฌ๋˜๋ฉด ํ•ด๋‹น ์ด๋ฒคํŠธ ํ”Œ๋ž˜๊ทธ ์ˆซ์ž๋ฅผ ์ถœ๋ ฅ
    flags ๋ณ€์ˆ˜์˜ 0๋ฒˆ์งธ๋ถ€ํ„ฐ 3๋ฒˆ์งธ ๋น„ํŠธ๋ฅผ ์ฐจ๋ก€๋กœ 1๋กœ ์„ค์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 1,2,4,8 ์ด๋ผ๋Š” ๊ฐ’์ด ๋‚˜์˜ด 
4. i๊ฐ€ 0์ผ ๋•Œ ๋ฐœ์ƒํ•˜๋Š” delay ๋•Œ ์ด๋ฒคํŠธ ํ”Œ๋ž˜๊ทธ๋ฅผ ์ˆ˜์‹  ๋ฐ›์ง€ ๋ชปํ•ด flags timeout ๋ฐœ์ƒ

#include <stdio.h>
#include "main.h"
#include "string.h"
#include "cmsis_os.h"
#include "app.h"
#define EVENT_FLAGS
// ์Šค๋ ˆ๋“œ1, ์Šค๋ ˆ๋“œ2 ID ๋ณ€์ˆ˜
osThreadId_t thread1_handle, thread2_handle;

const osThreadAttr_t thread1_attributes = {
  .name = "thread1",  // ์Šค๋ ˆ๋“œ ์ด๋ฆ„ ์ง€์ •
  .stack_size = 256 * 4,  // ์Šค๋ ˆ๋“œ ์Šคํƒ ํฌ๊ธฐ 256๋ฐ”์ดํŠธ๋กœ ์„ค์ •
  .priority = (osPriority_t) osPriorityNormal,  // ์ค‘๊ฐ„ ์šฐ์„ ์ˆœ์œ„๋กœ ์„ค์ •
};
const osThreadAttr_t thread2_attributes = {
  .name = "thread2",
  .stack_size = 256 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

static void thread1_Task(void *arg)
{
  uint8_t i = 0;
/* 
EVENT_FLAGS๊ฐ€ define ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ
osEventFlagsNew๋ฅผ ํ†ตํ•ด ์ด๋ฒคํŠธ ํ”Œ๋ž˜๊ทธ ๊ฐ์ฒด ์ƒ์„ฑ
*/  
#if defined(EVENT_FLAGS)
  osEventFlagsId_t evt_handle = osEventFlagsNew(NULL);
#endif
  for(;;) {
    osDelay(100);
#if defined(EVENT_FLAGS)
    osEventFlagsSet(evt_handle, 1 << i); // ์ด๋ฒคํŠธ ํ”Œ๋ž˜๊ทธ ์„ค์ •
#else
    osThreadFlagsSet(thread2_handle, 1 << i); // ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ์— ํ”Œ๋ž˜๊ทธ ์„ค์ •
#endif
    i++; i%=4; //i ๊ฐ’ -> 0~3
    if (i==0) osDelay(1000); 
    //i๊ฐ€ 0์ผ๋•Œ 1์ดˆ ํ›„ toggle 0์ด ์•„๋‹๋•Œ 0.1์ดˆ ํ›„ toggle 
    HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
  }}
  
static void thread2_Task(void *arg)
{
  uint32_t flags;
  for(;;) {
#if defined(EVENT_FLAGS)
    // ์ด๋ฒคํŠธ ํ”Œ๋ž˜๊ทธ ๋Œ€๊ธฐ
    flags = osEventFlagsWait(evt_handle, 0x000f, osFlagsWaitAny, 1000); //osWaitForever);
#else
    // ๋‹ค๋ฅธ ์Šค๋ ˆ๋“œ๋กœ๋ถ€ํ„ฐ ํ”Œ๋ž˜๊ทธ ๋Œ€๊ธฐ
    flags = osThreadFlagsWait(0x000f, osFlagsWaitAny, 1000);
#endif
    printf("flags : %08x : ", flags);
    if (flags != osFlagsErrorTimeout ) {
      // flags์˜ ๊ฐ ๋น„ํŠธ๋ฅผ ๊ฒ€์‚ฌํ•˜์—ฌ ์„ค์ •๋œ ํ”Œ๋ž˜๊ทธ๋ฅผ ์ถœ๋ ฅ
      if (flags & 0x0001) { printf("1\r\n");  }
      if (flags & 0x0002) { printf("2\r\n");  }
      if (flags & 0x0004) { printf("4\r\n");  }
      if (flags & 0x0008) { printf("8\r\n");  }
    } else {
			printf("flags timeout\r\n");
    }
    // LED ํ† ๊ธ€
    HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
  }}
void app(void)
{
  // ์šด์˜์ฒด์ œ ์ดˆ๊ธฐํ™”
  osKernelInitialize();
  // ์Šค๋ ˆ๋“œ ์ƒ์„ฑ
  thread1_handle = osThreadNew(thread1_Task, NULL, &thread2_attributes);
  thread2_handle = osThreadNew(thread2_Task, NULL, &thread2_attributes);
  // ์šด์˜์ฒด์ œ ์‹œ์ž‘
  osKernelStart();
}

 

profile

Toby's Study Blog

@Toby12

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!

๊ฒ€์ƒ‰ ํƒœ๊ทธ