Skip to content

Commit 6dc4cf8

Browse files
authored
Merge pull request #56 from blackleg/interrupts
Add interruption example
2 parents bbea1cc + 9e46d42 commit 6dc4cf8

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

examples/Interrupts/Interrupts.ino

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Example of a Arduino interruption and RTOS Binary Semaphore
3+
* https://www.freertos.org/Embedded-RTOS-Binary-Semaphores.html
4+
*/
5+
6+
7+
// Include Arduino FreeRTOS library
8+
#include <Arduino_FreeRTOS.h>
9+
10+
// Include semaphore supoport
11+
#include <semphr.h>
12+
13+
/*
14+
* Declaring a global variable of type SemaphoreHandle_t
15+
*
16+
*/
17+
SemaphoreHandle_t interruptSemaphore;
18+
19+
void setup() {
20+
21+
// Configure pin 2 as an input and enable the internal pull-up resistor
22+
pinMode(2, INPUT_PULLUP);
23+
24+
// Create task for Arduino led
25+
xTaskCreate(TaskLed, // Task function
26+
"Led", // Task name
27+
128, // Stack size
28+
NULL,
29+
0, // Priority
30+
NULL );
31+
32+
/**
33+
* Create a binary semaphore.
34+
* https://www.freertos.org/xSemaphoreCreateBinary.html
35+
*/
36+
interruptSemaphore = xSemaphoreCreateBinary();
37+
if (interruptSemaphore != NULL) {
38+
// Attach interrupt for Arduino digital pin
39+
attachInterrupt(digitalPinToInterrupt(2), interruptHandler, LOW);
40+
}
41+
42+
43+
}
44+
45+
void loop() {}
46+
47+
48+
void interruptHandler() {
49+
/**
50+
* Give semaphore in the interrup handler
51+
* https://www.freertos.org/a00124.html
52+
*/
53+
54+
xSemaphoreGiveFromISR(interruptSemaphore, NULL);
55+
}
56+
57+
58+
/*
59+
* Led task.
60+
*/
61+
void TaskLed(void *pvParameters)
62+
{
63+
(void) pvParameters;
64+
65+
pinMode(LED_BUILTIN, OUTPUT);
66+
67+
for (;;) {
68+
69+
/**
70+
* Take the semaphore.
71+
* https://www.freertos.org/a00122.html
72+
*/
73+
if (xSemaphoreTake(interruptSemaphore, portMAX_DELAY) == pdPASS) {
74+
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
75+
}
76+
77+
}
78+
}

0 commit comments

Comments
 (0)