아두이노에 FreeRTOS 올리고 frBlink 예제 실행하기

2019. 7. 9. 22:29드론

https://github.com/greiman/FreeRTOS-Arduino에 접속해서 다운로드를 한다. 해당 파일을 아두이노 라이브러리 폴더로 넣는다

 

 

해당 라이브러리를 옮기면, 제공하는 예제 파일들을 실행시킬 수 있다 

 

 

가장 처음의 frBlink 예제를 실행시켜서 업로드를 해보자. 200ms 주기로 LED가 깜빡이는 모습을 볼 수 있다

 

/*
* Example to demonstrate thread definition, semaphores, and thread sleep.
*/
#include <FreeRTOS_AVR.h>
// The LED is attached to pin 13 on Arduino.
const uint8_t LED_PIN = 13;
// Declare a semaphore handle.
SemaphoreHandle_t sem;
//------------------------------------------------------------------------------
/*
* Thread 1, turn the LED off when signalled by thread 2.
*/
// Declare the thread function for thread 1.
static void Thread1(void* arg) {
while (1) {
// Wait for signal from thread 2.
// 내놓아진 세마포어를 취득
xSemaphoreTake(sem, portMAX_DELAY);
// Turn LED off.
digitalWrite(LED_PIN, LOW);
}
}
//------------------------------------------------------------------------------
/*
* Thread 2, turn the LED on and signal thread 1 to turn the LED off.
*/
// Declare the thread function for thread 2.
static void Thread2(void* arg) {
// 우선순위가 큰 쓰레드, 메인 쓰레드가 된다
while (1) {
// Turn LED on.
digitalWrite(LED_PIN, HIGH);
// Sleep for 200 milliseconds.
vTaskDelay((200L * configTICK_RATE_HZ) / 1000L);
// Signal thread 1 to turn LED off.
// 세마포어를 주면서 제어권을 Thread1으로
xSemaphoreGive(sem);
// Sleep for 200 milliseconds.
vTaskDelay((200L * configTICK_RATE_HZ) / 1000L);
}
}
//------------------------------------------------------------------------------
void setup() {
portBASE_TYPE s1, s2;
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
// initialize semaphore
sem = xSemaphoreCreateCounting(1, 0);
// create task at priority two
s1 = xTaskCreate(Thread1, NULL, configMINIMAL_STACK_SIZE, NULL, 2, NULL);
// create task at priority one
s2 = xTaskCreate(Thread2, NULL, configMINIMAL_STACK_SIZE, NULL, 1, NULL);
// check for creation errors
if (sem== NULL || s1 != pdPASS || s2 != pdPASS ) {
Serial.println(F("Creation problem"));
while(1);
}
// start scheduler
vTaskStartScheduler();
Serial.println(F("Insufficient RAM"));
while(1);
}
//------------------------------------------------------------------------------
// WARNING idle loop has a very small stack (configMINIMAL_STACK_SIZE)
// loop must never block
void loop() {
// Not used.
}

 

[참고] https://webnautes.tistory.com/599?category=754912