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. set_name(const char * name)51 constexpr Options set_name(const char* name) { 52 name_ = name; 53 return *this; 54 } 55 56 // Sets the priority for the FreeRTOS task, see FreeRTOS xTaskCreate for more 57 // detail. set_priority(UBaseType_t priority)58 constexpr Options set_priority(UBaseType_t priority) { 59 priority_ = priority; 60 return *this; 61 } 62 63 #if PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED 64 // Set the stack size for dynamic thread allocations, see FreeRTOS xTaskCreate 65 // for more detail. set_stack_size(size_t size_words)66 constexpr Options set_stack_size(size_t size_words) { 67 PW_DASSERT(size_words >= config::kMinimumStackSizeWords); 68 stack_size_words_ = size_words; 69 return *this; 70 } 71 #endif // PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED 72 73 // Set the pre-allocated context (all memory needed to run a thread), see the 74 // pw::thread::freertos::StaticContext for more detail. set_static_context(StaticContext & context)75 constexpr Options set_static_context(StaticContext& context) { 76 context_ = &context; 77 return *this; 78 } 79 80 private: 81 friend thread::Thread; 82 // FreeRTOS requires a valid name when asserts are enabled, 83 // configMAX_TASK_NAME_LEN may be as small as one character. 84 static constexpr char kDefaultName[] = "pw::Thread"; 85 name()86 const char* name() const { return name_; } priority()87 UBaseType_t priority() const { return priority_; } 88 #if PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED stack_size_words()89 size_t stack_size_words() const { return stack_size_words_; } 90 #endif // PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED static_context()91 StaticContext* static_context() const { return context_; } 92 93 const char* name_ = kDefaultName; 94 UBaseType_t priority_ = config::kDefaultPriority; 95 #if PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED 96 size_t stack_size_words_ = config::kDefaultStackSizeWords; 97 #endif // PW_THREAD_FREERTOS_CONFIG_DYNAMIC_ALLOCATION_ENABLED 98 StaticContext* context_ = nullptr; 99 }; 100 101 } // namespace pw::thread::freertos 102