|
| 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 | + |
0 commit comments