-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathapp_thread.c
More file actions
153 lines (131 loc) · 4.51 KB
/
Copy pathapp_thread.c
File metadata and controls
153 lines (131 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/***************************************************************************
*
* Copyright 2015-2019 BES.
* All rights reserved. All unpublished rights reserved.
*
* No part of this work may be used or reproduced in any form or by any
* means, or stored in a database or retrieval system, without prior written
* permission of BES.
*
* Use of this work is governed by a license granted by BES.
* This work contains confidential and proprietary information of
* BES. which is protected by copyright, trade secret,
* trademark and other intellectual property rights.
*
****************************************************************************/
#include "app_thread.h"
#include "app_utils.h"
#include "cmsis_os.h"
#include "hal_timer.h"
#include "hal_trace.h"
static APP_MOD_HANDLER_T mod_handler[APP_MODUAL_NUM];
static void app_thread(void const *argument);
osThreadDef(app_thread, osPriorityHigh, 1, 1024 * 3, "app_thread");
osMailQDef(app_mailbox, APP_MAILBOX_MAX, APP_MESSAGE_BLOCK);
static osMailQId app_mailbox = NULL;
static uint8_t app_mailbox_cnt = 0;
osThreadId app_thread_tid;
static int app_mailbox_init(void) {
app_mailbox = osMailCreate(osMailQ(app_mailbox), NULL);
if (app_mailbox == NULL) {
TRACE(0, "Failed to Create app_mailbox\n");
return -1;
}
app_mailbox_cnt = 0;
return 0;
}
int app_mailbox_put(APP_MESSAGE_BLOCK *msg_src) {
osStatus status;
APP_MESSAGE_BLOCK *msg_p = NULL;
msg_p = (APP_MESSAGE_BLOCK *)osMailAlloc(app_mailbox, 0);
if (!msg_p) {
osEvent evt;
TRACE_IMM(0, "osMailAlloc error dump");
for (uint8_t i = 0; i < APP_MAILBOX_MAX; i++) {
evt = osMailGet(app_mailbox, 0);
if (evt.status == osEventMail) {
TRACE_IMM(
9,
"cnt:%d mod:%d src:%08x tim:%d id:%x ptr:%08x para:%08x/%08x/%08x",
i, ((APP_MESSAGE_BLOCK *)(evt.value.p))->mod_id,
((APP_MESSAGE_BLOCK *)(evt.value.p))->src_thread,
((APP_MESSAGE_BLOCK *)(evt.value.p))->system_time,
((APP_MESSAGE_BLOCK *)(evt.value.p))->msg_body.message_id,
((APP_MESSAGE_BLOCK *)(evt.value.p))->msg_body.message_ptr,
((APP_MESSAGE_BLOCK *)(evt.value.p))->msg_body.message_Param0,
((APP_MESSAGE_BLOCK *)(evt.value.p))->msg_body.message_Param1,
((APP_MESSAGE_BLOCK *)(evt.value.p))->msg_body.message_Param2);
} else {
TRACE_IMM(2, "cnt:%d %d", i, evt.status);
break;
}
}
TRACE_IMM(0, "osMailAlloc error dump end");
}
ASSERT(msg_p, "osMailAlloc error");
msg_p->src_thread = (uint32_t)osThreadGetId();
msg_p->dest_thread = (uint32_t)NULL;
msg_p->system_time = hal_sys_timer_get();
msg_p->mod_id = msg_src->mod_id;
msg_p->msg_body.message_id = msg_src->msg_body.message_id;
msg_p->msg_body.message_ptr = msg_src->msg_body.message_ptr;
msg_p->msg_body.message_Param0 = msg_src->msg_body.message_Param0;
msg_p->msg_body.message_Param1 = msg_src->msg_body.message_Param1;
msg_p->msg_body.message_Param2 = msg_src->msg_body.message_Param2;
status = osMailPut(app_mailbox, msg_p);
if (osOK == status)
app_mailbox_cnt++;
return (int)status;
}
int app_mailbox_free(APP_MESSAGE_BLOCK *msg_p) {
osStatus status;
status = osMailFree(app_mailbox, msg_p);
if (osOK == status)
app_mailbox_cnt--;
return (int)status;
}
int app_mailbox_get(APP_MESSAGE_BLOCK **msg_p) {
osEvent evt;
evt = osMailGet(app_mailbox, osWaitForever);
if (evt.status == osEventMail) {
*msg_p = (APP_MESSAGE_BLOCK *)evt.value.p;
return 0;
}
return -1;
}
static void app_thread(void const *argument) {
while (1) {
APP_MESSAGE_BLOCK *msg_p = NULL;
if (!app_mailbox_get(&msg_p)) {
if (msg_p->mod_id < APP_MODUAL_NUM) {
if (mod_handler[msg_p->mod_id]) {
int ret = mod_handler[msg_p->mod_id](&(msg_p->msg_body));
if (ret)
TRACE(2, "mod_handler[%d] ret=%d", msg_p->mod_id, ret);
}
}
app_mailbox_free(msg_p);
}
}
}
int app_os_init(void) {
if (app_mailbox_init())
return -1;
app_thread_tid = osThreadCreate(osThread(app_thread), NULL);
if (app_thread_tid == NULL) {
TRACE(0, "Failed to Create app_thread\n");
return 0;
}
return 0;
}
int app_set_threadhandle(enum APP_MODUAL_ID_T mod_id,
APP_MOD_HANDLER_T handler) {
if (mod_id >= APP_MODUAL_NUM)
return -1;
mod_handler[mod_id] = handler;
return 0;
}
void *app_os_tid_get(void) { return (void *)app_thread_tid; }
bool app_is_module_registered(enum APP_MODUAL_ID_T mod_id) {
return mod_handler[mod_id];
}