Last active
July 9, 2019 13:32
-
-
Save surinoel/504900d4a7eef485bbdadb4c8763739a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment