forked from ARMmbed/mbed-os
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathequeue_mbed.cpp
More file actions
147 lines (115 loc) · 3.59 KB
/
Copy pathequeue_mbed.cpp
File metadata and controls
147 lines (115 loc) · 3.59 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
/*
* Implementation for the mbed library
* https://github.com/mbedmicro/mbed
*
* Copyright (c) 2016 Christopher Haster
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "equeue/equeue_platform.h"
#if defined(EQUEUE_PLATFORM_MBED)
#include <stdbool.h>
#include "mbed.h"
// Ticker operations
static bool equeue_tick_inited = false;
static unsigned equeue_minutes = 0;
static unsigned equeue_timer[
(sizeof(Timer)+sizeof(unsigned)-1)/sizeof(unsigned)];
static unsigned equeue_ticker[
(sizeof(Ticker)+sizeof(unsigned)-1)/sizeof(unsigned)];
static void equeue_tick_update() {
reinterpret_cast<Timer*>(equeue_timer)->reset();
equeue_minutes += 1;
}
static void equeue_tick_init() {
MBED_STATIC_ASSERT(sizeof(equeue_timer) >= sizeof(Timer),
"The equeue_timer buffer must fit the class Timer");
MBED_STATIC_ASSERT(sizeof(equeue_ticker) >= sizeof(Ticker),
"The equeue_ticker buffer must fit the class Ticker");
new (equeue_timer) Timer;
new (equeue_ticker) Ticker;
equeue_minutes = 0;
reinterpret_cast<Timer*>(equeue_timer)->start();
reinterpret_cast<Ticker*>(equeue_ticker)
->attach_us(equeue_tick_update, (1 << 16)*1000);
equeue_tick_inited = true;
}
unsigned equeue_tick() {
if (!equeue_tick_inited) {
equeue_tick_init();
}
unsigned equeue_ms = reinterpret_cast<Timer*>(equeue_timer)->read_ms();
return (equeue_minutes << 16) + equeue_ms;
}
// Mutex operations
int equeue_mutex_create(equeue_mutex_t *m) { return 0; }
void equeue_mutex_destroy(equeue_mutex_t *m) { }
void equeue_mutex_lock(equeue_mutex_t *m) {
core_util_critical_section_enter();
}
void equeue_mutex_unlock(equeue_mutex_t *m) {
core_util_critical_section_exit();
}
// Semaphore operations
#ifdef MBED_CONF_RTOS_PRESENT
int equeue_sema_create(equeue_sema_t *s) {
MBED_STATIC_ASSERT(sizeof(equeue_sema_t) >= sizeof(Semaphore),
"The equeue_sema_t must fit the class Semaphore");
new (s) Semaphore(0);
return 0;
}
void equeue_sema_destroy(equeue_sema_t *s) {
reinterpret_cast<Semaphore*>(s)->~Semaphore();
}
void equeue_sema_signal(equeue_sema_t *s) {
reinterpret_cast<Semaphore*>(s)->release();
}
bool equeue_sema_wait(equeue_sema_t *s, int ms) {
if (ms < 0) {
ms = osWaitForever;
}
return (reinterpret_cast<Semaphore*>(s)->wait(ms) > 0);
}
#else
// Semaphore operations
int equeue_sema_create(equeue_sema_t *s) {
*s = false;
return 0;
}
void equeue_sema_destroy(equeue_sema_t *s) {
}
void equeue_sema_signal(equeue_sema_t *s) {
*s = 1;
}
static void equeue_sema_timeout(equeue_sema_t *s) {
*s = -1;
}
bool equeue_sema_wait(equeue_sema_t *s, int ms) {
int signal = 0;
Timeout timeout;
if (ms > 0) {
timeout.attach_us(callback(equeue_sema_timeout, s), ms*1000);
}
core_util_critical_section_enter();
while (!*s) {
sleep();
core_util_critical_section_exit();
core_util_critical_section_enter();
}
signal = *s;
*s = false;
core_util_critical_section_exit();
return (signal > 0);
}
#endif
#endif