#include "Arduino.h" #include <Wire.h> #include <SPI.h>
#include "ASCFont.h" #include "KSFont.h"
#include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h>
#define OLED_PCLK 18 #define OLED_PDATA 23 #define OLED_PRESET 17 #define OLED_PCMD 16 #define OLED_PCS 5
#define LED 2
//----- FreeRtos QueueHandle_t queue; int queueSize = 10;
QueueHandle_t xQueue;
typedef struct{ int sender; int counter; }Data;
// 세마포어 핸들을 선언합니다.->선언될때만 TASK 실행 SemaphoreHandle_t sem;
//The setup function is called once at startup of the sketch void setup() { // Add your initialization code here pinMode(LED, OUTPUT);
Serial.begin(115200); delay(10);
//--------------------------------- Serial.println("Start..");
//Free Rtos Q Set queue = xQueueCreate( queueSize, sizeof( int ) );
if(queue == NULL){ Serial.println("Error creating the queue"); }
xQueue = xQueueCreate(5, sizeof(Data));
if(xQueue == NULL){ Serial.println("Error creating the queue"); }
//세마포어를 초기화 합니다. 세마포어는 동시에 1개만 생성됩니다. sem = xSemaphoreCreateCounting(1, 0);
//Free Rtos Task Set xTaskCreate( taskOne, /* Task function. */ "TaskOne", /* String with name of task. */ 1024, /* Stack size in words. 10000->1024*/ NULL, /* Parameter passed as input of the task */ 1, /* Priority of the task. */ NULL); /* Task handle. */
xTaskCreate( taskTwo, /* Task function. */ "TaskTwo", /* String with name of task. */ 1024, /* Stack size in words. */ NULL, /* Parameter passed as input of the task */ 1, /* Priority of the task. */ NULL); /* Task handle. */
xTaskCreate( taskSAM, /* Task function. */ "taskSAM", /* String with name of task. */ 1024, /* Stack size in words. */ NULL, /* Parameter passed as input of the task */ 1, /* Priority of the task. */ NULL); /* Task handle. */
}
// The loop function is called in an endless loop void loop() { delay(1000);
}
void taskOne( void * parameter ) { /* keep the status of sending data */ BaseType_t xStatus; /* time to block the task until the queue has free space */ const TickType_t xTicksToWait = pdMS_TO_TICKS(100);
/* create data to send */ Data data;
/* sender 1 has id is 1 */ data.sender = 1; data.counter = 1;
for(;;){ for( int i = 0;i<10;i++ ){
Serial.println("Hello from task 1"); digitalWrite(LED, 1); //digitalWrite(LED, 0);
xQueueSend(queue, &i, portMAX_DELAY);
xStatus = xQueueSendToFront( xQueue, &data, xTicksToWait ); /* check whether sending is ok or not */ if( xStatus == pdPASS ) { /* increase counter of sender 1 */ data.counter = data.counter + 1; }
delay(1000); } } Serial.println("Ending task 1"); vTaskDelete( NULL );
}
void taskTwo( void * parameter) { /* keep the status of receiving data */ BaseType_t xStatus; /* time to block the task until data is available */ const TickType_t xTicksToWait = pdMS_TO_TICKS(100); Data data;
int element;
for(;;){ for( int i = 0;i<10;i++ ){
xQueueReceive(queue, &element, portMAX_DELAY); Serial.print(element); Serial.print(" -> ");
/* receive data from the queue */ xStatus = xQueueReceive( xQueue, &data, xTicksToWait ); /* check whether receiving is ok or not */ if(xStatus == pdPASS){ /* print the data to terminal */ Serial.print("receiveTask got data: "); Serial.print("sender = "); Serial.print(data.sender); Serial.print(" counter = "); Serial.println(data.counter); }
Serial.println("Hello from task 2"); delay(1000);
} } Serial.println("Ending task 2"); vTaskDelete( NULL );
}
void taskSAM( void * parameter) { while (1) {
//세마포어를 받을때까지 스레드는 계속 기다리게 됩니다. xSemaphoreTake(sem, portMAX_DELAY);
Serial.println(">>> Semaphore Run!");
}
}
|