1 // Copyright 2021 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
15 #include "FreeRTOS.h"
16 #include "pw_system/config.h"
17 #include "pw_thread/detached_thread.h"
18 #include "pw_thread/thread.h"
19 #include "pw_thread_freertos/context.h"
20 #include "pw_thread_freertos/options.h"
21
22 namespace pw::system {
23
24 // Low to high priorities.
25 enum class ThreadPriority : UBaseType_t {
26 kWorkQueue = tskIDLE_PRIORITY + 1,
27 // TODO(amontanez): These should ideally be at different priority levels, but
28 // there's synchronization issues when they are.
29 kLog = kWorkQueue,
30 kRpc = kWorkQueue,
31 #if PW_SYSTEM_ENABLE_TRANSFER_SERVICE
32 kTransfer = kWorkQueue,
33 #endif // PW_SYSTEM_ENABLE_TRANSFER_SERVICE
34 kNumPriorities,
35 };
36
37 static_assert(static_cast<UBaseType_t>(ThreadPriority::kNumPriorities) <=
38 configMAX_PRIORITIES);
39
40 static constexpr size_t kLogThreadStackWords = 1024;
41 static thread::freertos::StaticContextWithStack<kLogThreadStackWords>
42 log_thread_context;
LogThreadOptions()43 const thread::Options& LogThreadOptions() {
44 static constexpr auto options =
45 pw::thread::freertos::Options()
46 .set_name("LogThread")
47 .set_static_context(log_thread_context)
48 .set_priority(static_cast<UBaseType_t>(ThreadPriority::kLog));
49 return options;
50 }
51
52 // Stack size set to 16K in order to accommodate tests with large stacks.
53 // TODO: https://pwbug.dev/325509758 - Lower once tests stack sizes are reduced.
54 static constexpr size_t kRpcThreadStackWords = 8192;
55 static thread::freertos::StaticContextWithStack<kRpcThreadStackWords>
56 rpc_thread_context;
RpcThreadOptions()57 const thread::Options& RpcThreadOptions() {
58 static constexpr auto options =
59 pw::thread::freertos::Options()
60 .set_name("RpcThread")
61 .set_static_context(rpc_thread_context)
62 .set_priority(static_cast<UBaseType_t>(ThreadPriority::kRpc));
63 return options;
64 }
65
66 #if PW_SYSTEM_ENABLE_TRANSFER_SERVICE
67 static constexpr size_t kTransferThreadStackWords = 512;
68 static thread::freertos::StaticContextWithStack<kTransferThreadStackWords>
69 transfer_thread_context;
TransferThreadOptions()70 const thread::Options& TransferThreadOptions() {
71 static constexpr auto options =
72 pw::thread::freertos::Options()
73 .set_name("TransferThread")
74 .set_static_context(transfer_thread_context)
75 .set_priority(static_cast<UBaseType_t>(ThreadPriority::kTransfer));
76 return options;
77 }
78 #endif // PW_SYSTEM_ENABLE_TRANSFER_SERVICE
79
80 static constexpr size_t kWorkQueueThreadStackWords = 512;
81 static thread::freertos::StaticContextWithStack<kWorkQueueThreadStackWords>
82 work_queue_thread_context;
WorkQueueThreadOptions()83 const thread::Options& WorkQueueThreadOptions() {
84 static constexpr auto options =
85 pw::thread::freertos::Options()
86 .set_name("WorkQueueThread")
87 .set_static_context(work_queue_thread_context)
88 .set_priority(static_cast<UBaseType_t>(ThreadPriority::kWorkQueue));
89 return options;
90 }
91
92 } // namespace pw::system
93