Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8187d8f

Browse files
committed
Added a spinlock semaphore for resource sharing
- Added image of the spinlock semaphore - In osSemaphoreWait we burn CPU cycles when signal has not been received - Updated main.c with semaphore example
1 parent 8c7d9aa commit 8187d8f

File tree

6 files changed

+56
-2
lines changed

6 files changed

+56
-2
lines changed

Semaphore/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@
88

99
![No Semphore for UART](images/example1_no_semaphore.PNG)
1010

11-
- In the above example we are writing data to the UART from thread 1 and thread 2
11+
- In this example we are writing data to the UART from thread 1 and thread 2
1212
- As you can see in the image above when writing to the UART Task1/Task2 might be pre-empted before the entire data can be written to the UART
1313
- The above problem of shared resources can be solved using Semaphores
1414
- Semaphores are essentially signals that can signal certain parts of the code when something is done/needs to be done.
1515

1616
# Spinlock Semaphore
1717

18-
> TODO,
18+
- Wait for a signal to be raised to gain access to a shared resource
19+
- During a **wait** this shared resource burns CPU cycles till it gets a signal from another thread.
20+
21+
![Spinlock Semaphore for UART](images/example2_spinlock_semaphore.PNG)
22+
23+
- In this example since UART is a common resource between the thread1 and thread2 we use a form of communication between the two threads
24+
- When the first thread has access to the UART the second thread is in the **wait** state (semaphore value is 0)
25+
- Once the first thread is done writing data to the UART it gives a **signal** to the second thread.
26+
- Conversely now the first thread is in the **wait** state. Once the second thread is done writing data to the UART it gives a **signal** to the first thread.
1927

2028
# Cooperative Semaphore
2129

Semaphore/application/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
target_sources(${USER_PROJECT_TARGET} PRIVATE
2+
# Scheduler
23
osKernel.c
34
osKernel.S
5+
6+
# Semaphore
7+
osSemaphore.c
48
)
59
target_include_directories(${USER_PROJECT_TARGET} PRIVATE
610
.

Semaphore/application/main.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "osKernel.h"
2+
#include "osSemaphore.h"
23

34
#include "gpio/gpio.h"
45
#include "uart/uart.h"
@@ -11,18 +12,23 @@ static UART_s uart_config;
1112

1213
// * Add these variables to the debug watch window
1314
static uint32_t i, j, k;
15+
static uint32_t semaphore_t1, semaphore_t2;
1416

1517
void t1(void) {
1618
while (1) {
19+
osSemaphoreWait(&semaphore_t1);
1720
i++;
1821
uart__write_string(&uart_config, "Hello World from Thread 1\r\n");
22+
osSemaphoreSignal(&semaphore_t2);
1923
}
2024
}
2125

2226
void t2(void) {
2327
while (1) {
28+
osSemaphoreWait(&semaphore_t2);
2429
j++;
2530
uart__write_string(&uart_config, "Hello World from Thread 2\r\n");
31+
osSemaphoreSignal(&semaphore_t1);
2632
}
2733
}
2834

@@ -33,6 +39,9 @@ void t3(void) {
3339
}
3440

3541
int main(void) {
42+
osSemaphoreInit(&semaphore_t1, 1);
43+
osSemaphoreInit(&semaphore_t2, 0);
44+
3645
main__uart_init();
3746
uart__write_string(&uart_config, "Hello World\r\n");
3847

Semaphore/application/osSemaphore.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "osSemaphore.h"
2+
3+
#include "stm32l4xx.h"
4+
5+
void osSemaphoreInit(uint32_t *semaphore, uint32_t value) {
6+
*semaphore = value;
7+
}
8+
9+
void osSemaphoreWait(uint32_t *semaphore) {
10+
while (*semaphore <= 0) {
11+
}
12+
13+
__disable_irq();
14+
*semaphore -= 1;
15+
__enable_irq();
16+
}
17+
18+
void osSemaphoreSignal(uint32_t *semaphore) {
19+
__disable_irq();
20+
*semaphore += 1;
21+
__enable_irq();
22+
}

Semaphore/application/osSemaphore.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef OS_SEMAPHORE_H_
2+
#define OS_SEMAPHORE_H_
3+
4+
#include <stdint.h>
5+
6+
void osSemaphoreInit(uint32_t *semaphore, uint32_t value);
7+
8+
void osSemaphoreWait(uint32_t *semaphore);
9+
void osSemaphoreSignal(uint32_t *semaphore);
10+
11+
#endif
14.7 KB
Loading

0 commit comments

Comments
 (0)