Skip to content

Commit 35d15f1

Browse files
committed
Added AnalogRead_DigitalRead and Blink_AnalogRead examples
Based on examples from: https://github.com/feilipu/Arduino_FreeRTOS_Library Signed-off-by: Frederic Pillon <[email protected]>
1 parent d570975 commit 35d15f1

File tree

2 files changed

+249
-0
lines changed

2 files changed

+249
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Based on AnalogRead_DigitalRead example from: https://github.com/feilipu/Arduino_FreeRTOS_Library
3+
* Modified by: Frederic Pillon <frederic.pillon (at) st.com>
4+
*/
5+
#include <STM32FreeRTOS.h>
6+
7+
// If no default pin for user button (USER_BTN) is defined, define it
8+
#ifndef USER_BTN
9+
#define USER_BTN 2
10+
#endif
11+
12+
// Declare a mutex Semaphore Handle which we will use to manage the Serial Port.
13+
// It will be used to ensure only only one Task is accessing this resource at any time.
14+
SemaphoreHandle_t xSerialSemaphore;
15+
16+
// define two Tasks for DigitalRead & AnalogRead
17+
void TaskDigitalRead( void *pvParameters );
18+
void TaskAnalogRead( void *pvParameters );
19+
20+
// the setup function runs once when you press reset or power the board
21+
void setup() {
22+
23+
// initialize serial communication at 9600 bits per second:
24+
Serial.begin(9600);
25+
26+
while (!Serial) {
27+
; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
28+
}
29+
30+
// Semaphores are useful to stop a Task proceeding, where it should be paused to wait,
31+
// because it is sharing a resource, such as the Serial port.
32+
// Semaphores should only be used whilst the scheduler is running, but we can set it up here.
33+
if ( xSerialSemaphore == NULL ) // Check to confirm that the Serial Semaphore has not already been created.
34+
{
35+
xSerialSemaphore = xSemaphoreCreateMutex(); // Create a mutex semaphore we will use to manage the Serial Port
36+
if ( ( xSerialSemaphore ) != NULL )
37+
xSemaphoreGive( ( xSerialSemaphore ) ); // Make the Serial Port available for use, by "Giving" the Semaphore.
38+
}
39+
40+
// Now set up two Tasks to run independently.
41+
xTaskCreate(
42+
TaskDigitalRead
43+
, (const portCHAR *)"DigitalRead" // A name just for humans
44+
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
45+
, NULL
46+
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
47+
, NULL );
48+
49+
xTaskCreate(
50+
TaskAnalogRead
51+
, (const portCHAR *) "AnalogRead"
52+
, 128 // Stack size
53+
, NULL
54+
, 1 // Priority
55+
, NULL );
56+
57+
// start scheduler
58+
vTaskStartScheduler();
59+
Serial.println("Insufficient RAM");
60+
while(1);
61+
}
62+
63+
void loop()
64+
{
65+
// Empty. Things are done in Tasks.
66+
}
67+
68+
/*--------------------------------------------------*/
69+
/*---------------------- Tasks ---------------------*/
70+
/*--------------------------------------------------*/
71+
72+
void TaskDigitalRead( void *pvParameters __attribute__((unused)) ) // This is a Task.
73+
{
74+
/*
75+
DigitalReadSerial
76+
Reads a digital input on pin defined with USER_BTN, prints the result to the serial monitor
77+
78+
This example code is in the public domain.
79+
*/
80+
81+
// defined USER_BTN digital pin has a pushbutton attached to it. Give it a name:
82+
uint8_t pushButton = USER_BTN;
83+
84+
// make the pushbutton's pin an input:
85+
pinMode(pushButton, INPUT);
86+
87+
for (;;) // A Task shall never return or exit.
88+
{
89+
// read the input pin:
90+
int buttonState = digitalRead(pushButton);
91+
92+
// See if we can obtain or "Take" the Serial Semaphore.
93+
// If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free.
94+
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE )
95+
{
96+
// We were able to obtain or "Take" the semaphore and can now access the shared resource.
97+
// We want to have the Serial Port for us alone, as it takes some time to print,
98+
// so we don't want it getting stolen during the middle of a conversion.
99+
// print out the state of the button:
100+
Serial.print("Button state: ");
101+
Serial.println(buttonState);
102+
103+
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
104+
}
105+
106+
vTaskDelay(1); // one tick delay (15ms) in between reads for stability
107+
}
108+
}
109+
110+
void TaskAnalogRead( void *pvParameters __attribute__((unused)) ) // This is a Task.
111+
{
112+
113+
for (;;)
114+
{
115+
// read the input on analog pin 0:
116+
int sensorValue = analogRead(A0);
117+
118+
// See if we can obtain or "Take" the Serial Semaphore.
119+
// If the semaphore is not available, wait 5 ticks of the Scheduler to see if it becomes free.
120+
if ( xSemaphoreTake( xSerialSemaphore, ( TickType_t ) 5 ) == pdTRUE )
121+
{
122+
// We were able to obtain or "Take" the semaphore and can now access the shared resource.
123+
// We want to have the Serial Port for us alone, as it takes some time to print,
124+
// so we don't want it getting stolen during the middle of a conversion.
125+
// print out the value you read:
126+
Serial.println(sensorValue);
127+
128+
xSemaphoreGive( xSerialSemaphore ); // Now free or "Give" the Serial Port for others.
129+
}
130+
131+
vTaskDelay(1); // one tick delay (15ms) in between reads for stability
132+
}
133+
}
134+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Based on Blink_AnalogRead example from: https://github.com/feilipu/Arduino_FreeRTOS_Library
3+
* Modified by: Frederic Pillon <frederic.pillon (at) st.com>
4+
*/
5+
#include <STM32FreeRTOS.h>
6+
7+
// define two tasks for Blink & AnalogRead
8+
void TaskBlink( void *pvParameters );
9+
void TaskAnalogRead( void *pvParameters );
10+
11+
// the setup function runs once when you press reset or power the board
12+
void setup() {
13+
14+
// initialize serial communication at 9600 bits per second:
15+
Serial.begin(9600);
16+
17+
while (!Serial) {
18+
; // wait for serial port to connect. Needed for native USB, on LEONARDO, MICRO, YUN, and other 32u4 based boards.
19+
}
20+
21+
// Now set up two tasks to run independently.
22+
xTaskCreate(
23+
TaskBlink
24+
, (const portCHAR *)"Blink" // A name just for humans
25+
, 128 // This stack size can be checked & adjusted by reading the Stack Highwater
26+
, NULL
27+
, 2 // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
28+
, NULL );
29+
30+
xTaskCreate(
31+
TaskAnalogRead
32+
, (const portCHAR *) "AnalogRead"
33+
, 128 // Stack size
34+
, NULL
35+
, 1 // Priority
36+
, NULL );
37+
38+
// start scheduler
39+
vTaskStartScheduler();
40+
Serial.println("Insufficient RAM");
41+
while(1);
42+
}
43+
44+
void loop()
45+
{
46+
// Empty. Things are done in Tasks.
47+
}
48+
49+
/*--------------------------------------------------*/
50+
/*---------------------- Tasks ---------------------*/
51+
/*--------------------------------------------------*/
52+
53+
void TaskBlink(void *pvParameters) // This is a task.
54+
{
55+
(void) pvParameters;
56+
57+
/*
58+
Blink
59+
Turns on an LED on for one second, then off for one second, repeatedly.
60+
61+
Most Arduinos have an on-board LED you can control. On the UNO, LEONARDO, MEGA, and ZERO
62+
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN takes care
63+
of use the correct LED pin whatever is the board used.
64+
65+
The MICRO does not have a LED_BUILTIN available. For the MICRO board please substitute
66+
the LED_BUILTIN definition with either LED_BUILTIN_RX or LED_BUILTIN_TX.
67+
e.g. pinMode(LED_BUILTIN_RX, OUTPUT); etc.
68+
69+
If you want to know what pin the on-board LED is connected to on your Arduino model, check
70+
the Technical Specs of your board at https://www.arduino.cc/en/Main/Products
71+
72+
This example code is in the public domain.
73+
74+
modified 8 May 2014
75+
by Scott Fitzgerald
76+
77+
modified 2 Sep 2016
78+
by Arturo Guadalupi
79+
*/
80+
81+
// initialize digital LED_BUILTIN on pin 13 as an output.
82+
pinMode(LED_BUILTIN, OUTPUT);
83+
84+
for (;;) // A Task shall never return or exit.
85+
{
86+
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
87+
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
88+
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
89+
vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
90+
}
91+
}
92+
93+
void TaskAnalogRead(void *pvParameters) // This is a task.
94+
{
95+
(void) pvParameters;
96+
97+
/*
98+
AnalogReadSerial
99+
Reads an analog input on pin 0, prints the result to the serial monitor.
100+
Graphical representation is available using serial plotter (Tools > Serial Plotter menu)
101+
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
102+
103+
This example code is in the public domain.
104+
*/
105+
106+
for (;;)
107+
{
108+
// read the input on analog pin 0:
109+
int sensorValue = analogRead(A0);
110+
// print out the value you read:
111+
Serial.println(sensorValue);
112+
vTaskDelay(1); // one tick delay (15ms) in between reads for stability
113+
}
114+
}
115+

0 commit comments

Comments
 (0)