Add Semaphore part to RIdhaOS
This commit is contained in:
parent
f7cd7387dc
commit
aa087399ba
@ -3,6 +3,7 @@
|
||||
|
||||
#include "RIDHAOS_CONF.h"
|
||||
#include "ridhaOsScheduler.h"
|
||||
#include "ridhaOsSemaphore.h"
|
||||
/* This Struct used to restore next thread and save the running thread */
|
||||
typedef struct tControlBlock
|
||||
{
|
||||
|
||||
11
RidhaOs/Inc/ridhaOsSemaphore.h
Normal file
11
RidhaOs/Inc/ridhaOsSemaphore.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef __R_SEMAPHORE_H__
|
||||
#define __R_SEMAPHORE_H__
|
||||
|
||||
#include "ridhaOs.h"
|
||||
|
||||
void ridhaOsSemaphoreInit(int32_t *semaphore, int32_t value);
|
||||
void ridhaOsSemaphoreSet(int32_t * semaphore);
|
||||
void ridhaOsSemaphoreWait(int32_t * semaphore);
|
||||
|
||||
|
||||
#endif /* __R_SEMAPHORE_H__ */
|
||||
25
RidhaOs/Src/ridhaOsSemaphore.c
Normal file
25
RidhaOs/Src/ridhaOsSemaphore.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include "ridhaOsSemaphore.h"
|
||||
|
||||
void ridhaOsSemaphoreInit(int32_t *semaphore, int32_t value)
|
||||
{
|
||||
*semaphore = value;
|
||||
}
|
||||
|
||||
void ridhaOsSemaphoreSet(int32_t * semaphore)
|
||||
{
|
||||
__disable_irq();
|
||||
*semaphore += 1;
|
||||
__enable_irq();
|
||||
}
|
||||
|
||||
void ridhaOsSemaphoreWait(int32_t * semaphore)
|
||||
{
|
||||
__disable_irq();
|
||||
while (*semaphore <= 0)
|
||||
{
|
||||
__disable_irq();
|
||||
__enable_irq();
|
||||
}
|
||||
*semaphore -= 1;
|
||||
__enable_irq();
|
||||
}
|
||||
22
main.c
22
main.c
@ -5,6 +5,8 @@
|
||||
#include "fsl_common.h"
|
||||
#include "ridhaOs.h"
|
||||
|
||||
int32_t semaphore1, semaphore2;
|
||||
|
||||
|
||||
typedef uint32_t TaskProfiler;
|
||||
TaskProfiler Task0_Profiler, Task1_Profiler, Task2_Profiler;
|
||||
@ -22,12 +24,18 @@ void SystemInitHook()
|
||||
int main(void){
|
||||
|
||||
SystemInitHook();
|
||||
uart_init();
|
||||
|
||||
/* Init Semaphore */
|
||||
ridhaOsSemaphoreInit(&semaphore1, 1);
|
||||
ridhaOsSemaphoreInit(&semaphore2, 0);
|
||||
|
||||
/* Add threads */
|
||||
ridhaOsAddThreads(&task0, &task1, &task2);
|
||||
ridhaOsStart();
|
||||
|
||||
/* We can't reach this point of program */
|
||||
|
||||
while(1);
|
||||
}
|
||||
|
||||
|
||||
@ -36,7 +44,10 @@ void task0(void)
|
||||
Task0_Profiler = 0;
|
||||
while(1)
|
||||
{
|
||||
//PRINTF("Task 0");
|
||||
ridhaOsSemaphoreWait(&semaphore1);
|
||||
PRINTF("Task 0\n");
|
||||
ridhaOsSemaphoreSet(&semaphore2);
|
||||
|
||||
Task0_Profiler++;
|
||||
}
|
||||
}
|
||||
@ -46,7 +57,10 @@ void task1(void)
|
||||
Task1_Profiler = 0;
|
||||
while(1)
|
||||
{
|
||||
//PRINTF("Task 1");
|
||||
ridhaOsSemaphoreWait(&semaphore2);
|
||||
PRINTF("Task 1\n");
|
||||
ridhaOsSemaphoreSet(&semaphore1);
|
||||
|
||||
Task1_Profiler++;
|
||||
}
|
||||
}
|
||||
@ -56,7 +70,7 @@ void task2(void)
|
||||
Task2_Profiler = 0;
|
||||
while(1)
|
||||
{
|
||||
//PRINTF("Task 2");
|
||||
//PRINTF("Task 2\n");
|
||||
Task2_Profiler++;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user