1 // Copyright 2020 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include "FreeRTOS.h" 17 #include "pw_assert/assert.h" 18 #include "pw_thread/thread.h" 19 #include "pw_thread_freertos/config.h" 20 #include "pw_thread_freertos/context.h" 21 #include "task.h" 22 23 namespace pw::thread::freertos { 24 25 // pw::thread::Options for FreeRTOS. 26 // 27 // Example usage: 28 // 29 // // Uses the default stack size and priority, but specifies a custom name. 30 // pw::thread::Thread example_thread( 31 // pw::thread::freertos::Options() 32 // .set_name("example_thread"), 33 // example_thread_function); 34 // 35 // // Provides the name, priority, and pre-allocated context. 36 // pw::thread::Thread static_example_thread( 37 // pw::thread::freertos::Options() 38 // .set_name("static_example_thread") 39 // .set_priority(kFooPriority) 40 // .set_static_context(static_example_thread_context), 41 // example_thread_function); 42 // 43 class Options : public thread::Options { 44 public: 45 constexpr Options() = default; 46 constexpr Options(const Options&) = default; 47 constexpr Options(Options&& other) = default; 48 49 // Sets the name for the FreeRTOS task, note that this will be truncated 50 // based on configMAX_TASK_NAME_LEN. 51 // This is deep copied by FreeRTOS into the task's task control block (TCB). set_name(const char * name)52 constexpr Options& set_name(const char* name) { 53 name_ = name; 54 return *this; 55 } 56 57 // Sets the priority for the FreeRTOS task. This must be a value between 58 // 0 to PW_THREAD_FREERTOS_CONFIG_MAXIMUM_PRIORITY. Higher priority 59 // values have a higher priority. 60 // 61 // Note that the idle task priority, tskIDLE_PRIORITY, is fixed to 0. 62 // See the FreeRTOS documentation on the idle task for more details. 63 // 64 // Precondition: This must be <= PW_THREAD_FREERTOS_CONFIG_MAXIMUM_PRIORITY. set_priority(UBaseType_t priority)65 constexpr Options& set_priority(UBaseType_t priority) { 66 PW_DASSERT(priority <= config::kMaximumPriority); 67 priority_ = priority; 68 return *this; 69 } 70 71 #if PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED 72 // Set the stack size of dynamic thread allocations. 73 // 74 // Precondition: size_words must be >= configMINIMAL_STACK_SIZE set_stack_size(size_t size_words)75 constexpr Options& set_stack_size(size_t size_words) { 76 PW_DASSERT(size_words >= config::kMinimumStackSizeWords); 77 stack_size_words_ = size_words; 78 return *this; 79 } 80 #endif // PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED 81 82 // Set the pre-allocated context (all memory needed to run a thread), see the 83 // pw::thread::freertos::StaticContext for more detail. set_static_context(StaticContext & context)84 constexpr Options& set_static_context(StaticContext& context) { 85 context_ = &context; 86 return *this; 87 } 88 89 private: 90 friend thread::Thread; 91 // FreeRTOS requires a valid name when asserts are enabled, 92 // configMAX_TASK_NAME_LEN may be as small as one character. 93 static constexpr char kDefaultName[] = "pw::Thread"; 94 name()95 const char* name() const { return name_; } priority()96 UBaseType_t priority() const { return priority_; } 97 #if PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED stack_size_words()98 size_t stack_size_words() const { return stack_size_words_; } 99 #endif // PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED static_context()100 StaticContext* static_context() const { return context_; } 101 102 const char* name_ = kDefaultName; 103 UBaseType_t priority_ = config::kDefaultPriority; 104 #if PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED 105 size_t stack_size_words_ = config::kDefaultStackSizeWords; 106 #endif // PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED 107 StaticContext* context_ = nullptr; 108 }; 109 110 } // namespace pw::thread::freertos 111