Skip to content

Commit c451e8c

Browse files
committed
Minimal C RTOS API
1 parent dabe49e commit c451e8c

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

rtos/rtos_c.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2019, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "rtos/rtos_c.h"
19+
#include "rtos/Kernel.h"
20+
#include "rtos/ThisThread.h"
21+
22+
extern "C" {
23+
24+
uint64_t get_ms_count(void)
25+
{
26+
return rtos::Kernel::get_ms_count();
27+
}
28+
29+
void thread_sleep_for(uint32_t millisec)
30+
{
31+
return rtos::ThisThread::sleep_for(millisec);
32+
}
33+
34+
void thread_sleep_until(uint64_t millisec)
35+
{
36+
return rtos::ThisThread::sleep_until(millisec);
37+
38+
}
39+
40+
}

rtos/rtos_c.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2019, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef MBED_RTOS_C_H_
19+
#define MBED_RTOS_C_H_
20+
21+
#include <stdint.h>
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
/** Generic thread functions.
28+
*
29+
* These are C versions of functions provided in C++ via rtos::Thread and rtos::ThisThread
30+
*/
31+
32+
/** Read the current RTOS kernel millisecond tick count.
33+
The tick count corresponds to the tick count the RTOS uses for timing
34+
purposes. It increments monotonically from 0 at boot, so it effectively
35+
never wraps. If the underlying RTOS only provides a 32-bit tick count,
36+
this method expands it to 64 bits.
37+
@return RTOS kernel current tick count
38+
@note Mbed OS always uses millisecond RTOS ticks, and this could only wrap
39+
after half a billion years.
40+
@note In a non-RTOS build, this computes an equivalent time in milliseconds,
41+
based on a HAL timer. The time may be referenced as 0 on first call.
42+
@note You cannot call this function from ISR context.
43+
@note The equivalent functionality is accessible in C++ via rtos::Kernel::get_ms_count.
44+
*/
45+
uint64_t get_ms_count(void);
46+
47+
/** Sleep for a specified time period in millisec:
48+
@param millisec time delay value
49+
@note You cannot call this function from ISR context.
50+
@note The equivalent functionality is accessible in C++ via rtos::ThisThread::sleep_for.
51+
*/
52+
void thread_sleep_for(uint32_t millisec);
53+
54+
/** Sleep until a specified time in millisec
55+
The specified time is according to Kernel::get_ms_count().
56+
@param millisec absolute time in millisec
57+
@note You cannot call this function from ISR context.
58+
@note if millisec is equal to or lower than the current tick count, this
59+
returns immediately.
60+
@note The equivalent functionality is accessible in C++ via ThisThread::sleep_until.
61+
*/
62+
void thread_sleep_until(uint64_t millisec);
63+
64+
#ifdef __cplusplus
65+
}
66+
#endif
67+
68+
#endif /* MBED_THREAD_API_H_ */

0 commit comments

Comments
 (0)