1 /*
2 * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://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,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "console.h"
16 #include "command.h"
17 #include "uart.h"
18
19 #ifdef CFG_RTOS
20 #include "al_rtos.h"
21
22 /*
23 * GLOBAL VARIABLES
24 ****************************************************************************************
25 */
26 /// Handle of the console task
27 static rtos_task_handle console_task_handle;
28 /// Indicates whether console task is suspended or not
29 static bool console_task_suspended;
30
31 /*
32 * FUNCTIONS
33 ****************************************************************************************
34 */
35 /**
36 ****************************************************************************************
37 * @brief Request the RTOS to suspend the console task.
38 * This function will return only when the console task is resumed by the function @ref
39 * console_task_resume
40 ****************************************************************************************
41 */
console_task_suspend(void)42 static void console_task_suspend(void)
43 {
44 console_task_suspended = true;
45
46 // wait for notification
47 rtos_task_wait_notification(-1);
48 }
49
50 /**
51 ****************************************************************************************
52 * @brief Request the RTOS to resume the console task.
53 * This function sends a msg to console_queue to realize the resume.
54 * Note that currently this function is supposed to be called from interrupt.
55 *
56 * @param[in] isr Indicates if called from ISR
57 ****************************************************************************************
58 */
console_task_resume(bool isr)59 static void console_task_resume(bool isr)
60 {
61 if (!console_task_suspended) {
62 return;
63 }
64 console_task_suspended = false;
65
66 // Notify the console task
67 rtos_task_notify(console_task_handle, 0, isr);
68 }
69
70 /**
71 ****************************************************************************************
72 * @brief console task implementation.
73 ****************************************************************************************
74 */
RTOS_TASK_FCT(console_task_routine)75 static RTOS_TASK_FCT(console_task_routine)
76 {
77 for (;;) {
78 console_schedule();
79
80 if (console_buf_empty() == 1) {
81 console_task_suspend();
82 }
83 }
84 }
85
console_task_init(void)86 int console_task_init(void)
87 {
88 // Initialize console
89 console_init();
90
91 console_task_suspended = false;
92 // Create the console task
93 if (rtos_task_create(console_task_routine, "CONSOLE", CONSOLE_TASK,
94 TASK_STACK_SIZE_CONSOLE, NULL, TASK_PRIORITY_CONSOLE, &console_task_handle)) {
95 return 1;
96 }
97
98 console_ntf_register(console_task_resume);
99
100 return 0;
101 }
102
103 #endif
104