Skip to content

Commit 630c1e9

Browse files
committed
zephyr: Add Zephyr RTOS support
This commit add a RTOS called Zephyr[1]. This commit is Zephyr specific code placed under src/arch/zephyr. Because Zephyr's build system is CMake based, this commit doesn't touch other build system that libcsp supports, Meson and Waf. To build it for Zephyr you will need Zephyr module code, which will follow this commit. This is preliminary and has some minor isues. - rand_r() and sscanf() are not in Zephyr proper. We have them in arch/zephyr/csp_libc.c. But it still generates implicit declaration warnings. For sscanf(), the function is empty and always fails. - <endian.h> is not provided by the minimal libc. You have to use other libc or use the following PR. zephyrproject-rtos/zephyr#38939 [1]: https://zephyrproject.org/ Signed-off-by: Yasushi SHOJI <[email protected]>
1 parent a7ef177 commit 630c1e9

17 files changed

+576
-2
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ option(CSP_USE_DEDUP "Packet deduplication" ON)
4747

4848
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
4949
set(CSP_POSIX 1)
50+
elseif(CMAKE_SYSTEM_NAME STREQUAL "Zephyr")
51+
set(CSP_ZEPHYR 1)
5052
endif()
5153

5254
if(CMAKE_C_BYTE_ORDER STREQUAL "BIG_ENDIAN")
@@ -72,6 +74,8 @@ target_include_directories(libcsp PUBLIC ${csp_inc})
7274

7375
if(CSP_POSIX)
7476
set(CSP_C_ARGS -Wshadow -Wcast-align -Wwrite-strings -Wno-unused-parameter)
77+
elseif(CSP_ZEPHYR)
78+
set(CSP_C_ARGS -Wwrite-strings -Wno-unused-parameter)
7579
endif()
7680
target_compile_options(libcsp PRIVATE ${CSP_C_ARGS})
7781

csp_autoconfig.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#cmakedefine01 CSP_POSIX
2+
#cmakedefine01 CSP_ZEPHYR
23

34
#cmakedefine01 CSP_DEBUG
45
#cmakedefine01 CSP_LOG_LEVEL_ERROR

include/csp/arch/csp_queue.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ csp_queue_handle_t csp_queue_create(int length, size_t item_size);
7171

7272
#if (CSP_FREERTOS)
7373
typedef StaticQueue_t csp_static_queue_t;
74+
#elif (CSP_ZEPHYR)
75+
#include <zephyr.h>
76+
typedef struct k_msgq csp_static_queue_t;
7477
#else
7578
typedef void * csp_static_queue_t;
7679
#endif

include/csp/arch/csp_semaphore.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ typedef StaticSemaphore_t csp_mutex_buffer_t;
9090

9191
#endif // CSP_FREERTOS
9292

93+
/* Zephyr RTOS Interface */
94+
#if (CSP_ZEPHYR)
95+
96+
#include <zephyr.h>
97+
98+
#define CSP_SEMAPHORE_OK 1
99+
#define CSP_SEMAPHORE_ERROR 2
100+
101+
typedef struct k_mutex csp_mutex_t;
102+
typedef struct k_sem csp_bin_sem_handle_t;
103+
104+
/* These types are not used for static API */
105+
struct csp_empty_t {};
106+
typedef struct csp_empty_t csp_mutex_buffer_t;
107+
typedef struct csp_empty_t csp_bin_sem_t;
108+
109+
#endif // CSP_ZEPHYR
110+
93111
/**
94112
Mutex call OK.
95113

include/csp/arch/csp_thread.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ typedef csp_thread_return_t (* csp_thread_func_t)(void *);
104104

105105
#endif // CSP_FREERTOS
106106

107+
/*
108+
Zephyr interface
109+
*/
110+
#if (CSP_ZEPHYR)
111+
112+
#include <zephyr.h>
113+
114+
typedef k_tid_t csp_thread_handle_t;
115+
typedef struct k_thread csp_thread_t;
116+
typedef k_thread_stack_t csp_stack_t;
117+
typedef k_thread_entry_t csp_thread_func_t;
118+
119+
#define CSP_DEFINE_TASK(task_name) void task_name(void *p1, void *p2, void *p3)
120+
#define CSP_TASK_RETURN
121+
122+
#endif // CSP_ZEPHYR
123+
107124
/**
108125
Create thread (task).
109126
@@ -117,6 +134,12 @@ typedef csp_thread_return_t (* csp_thread_func_t)(void *);
117134
*/
118135
int csp_thread_create(csp_thread_func_t func, const char * const name, unsigned int stack_size, void * parameter, unsigned int priority, csp_thread_handle_t * handle);
119136

137+
csp_thread_handle_t
138+
csp_thread_create_static(csp_thread_t *new_thread, const char * const name,
139+
csp_stack_t *stack, unsigned int stack_size,
140+
csp_thread_func_t func, void * parameter,
141+
unsigned int priority);
142+
120143
/**
121144
Exit current thread.
122145
@note Not supported on all platforms.

src/arch/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
1010
elseif(CMAKE_SYSTEM_NAME STREQUAL "FreeRTOS")
1111
set(CSP_FREERTOS 1)
1212
add_subdirectory(freertos)
13+
elseif(CMAKE_SYSTEM_NAME STREQUAL "Zephyr")
14+
set(CSP_Zephyr 1)
15+
add_subdirectory(zephyr)
1316
else()
1417
message(FATAL_ERROR "invalid system ${CMAKE_SYSTEM_NAME}")
1518
endif()

src/arch/zephyr/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
target_sources(libcsp PRIVATE
2+
csp_clock.c
3+
csp_libc.c
4+
csp_queue.c
5+
csp_semaphore.c
6+
csp_system.c
7+
csp_thread.c
8+
csp_time.c
9+
csp_zephyr_init.c
10+
)

src/arch/zephyr/csp_clock.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
3+
* Copyright (C) 2021 Space Cubics, LLC.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <csp/arch/csp_clock.h>
20+
21+
#include <zephyr.h>
22+
#include <posix/time.h>
23+
#include <logging/log.h>
24+
LOG_MODULE_DECLARE(libcsp);
25+
26+
__attribute__((weak)) void csp_clock_get_time(csp_timestamp_t *time)
27+
{
28+
struct timespec ts;
29+
int ret;
30+
31+
ret = clock_gettime(CLOCK_REALTIME, &ts);
32+
if (ret < 0) {
33+
LOG_WRN("clock_gettime() failed, retruning with 0s");
34+
time->tv_sec = 0;
35+
time->tv_nsec = 0;
36+
} else {
37+
time->tv_sec = ts.tv_sec;
38+
time->tv_nsec = ts.tv_nsec;
39+
}
40+
}
41+
42+
__attribute__((weak)) int csp_clock_set_time(const csp_timestamp_t *time)
43+
{
44+
int ret;
45+
struct timespec ts;
46+
47+
ts.tv_sec = time->tv_sec;
48+
ts.tv_nsec = time->tv_nsec;
49+
50+
ret = clock_settime(CLOCK_REALTIME, &ts);
51+
if (ret < 0) {
52+
return CSP_ERR_INVAL;
53+
}
54+
55+
return CSP_ERR_NONE;
56+
}

src/arch/zephyr/csp_libc.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
3+
* Copyright (C) 2021 Space Cubics, LLC.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <stdlib.h>
20+
21+
int sscanf(const char *s, const char *format, ...)
22+
{
23+
return 0;
24+
}
25+
26+
int rand_r(unsigned int *seedp)
27+
{
28+
srand(*seedp);
29+
return rand();
30+
}

src/arch/zephyr/csp_queue.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
3+
* Copyright (C) 2021 Space Cubics, LLC.
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 2.1 of the License, or (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#include <csp/arch/csp_queue.h>
20+
21+
#include <zephyr.h>
22+
#include <logging/log.h>
23+
LOG_MODULE_DECLARE(libcsp);
24+
25+
csp_queue_handle_t csp_queue_create_static(int length, size_t item_size,
26+
char *buf, csp_static_queue_t *queue)
27+
{
28+
struct k_msgq *q = (struct k_msgq *)queue;
29+
30+
k_msgq_init(q, buf, item_size, length);
31+
32+
return q;
33+
}
34+
35+
static int csp_errno_zephyr_to_csp(int err)
36+
{
37+
int ret;
38+
39+
if (err == 0) {
40+
ret = CSP_QUEUE_OK;
41+
} else {
42+
ret = CSP_QUEUE_ERROR;
43+
}
44+
45+
return ret;
46+
}
47+
48+
int csp_queue_enqueue(csp_queue_handle_t queue, const void * value, uint32_t timeout)
49+
{
50+
int ret;
51+
struct k_msgq *q = (struct k_msgq *)queue;
52+
53+
ret = k_msgq_put(q, value, K_MSEC(timeout));
54+
55+
return csp_errno_zephyr_to_csp(ret);
56+
}
57+
58+
int csp_queue_enqueue_isr(csp_queue_handle_t queue, const void *value, int *unused)
59+
{
60+
int ret;
61+
struct k_msgq *q = (struct k_msgq *)queue;
62+
63+
ARG_UNUSED(unused);
64+
65+
ret = k_msgq_put(q, value, K_NO_WAIT);
66+
67+
return csp_errno_zephyr_to_csp(ret);
68+
}
69+
70+
int csp_queue_dequeue(csp_queue_handle_t queue, void * buf, uint32_t timeout)
71+
{
72+
int ret;
73+
struct k_msgq *q = (struct k_msgq *)queue;
74+
75+
ret = k_msgq_get(q, buf, K_MSEC(timeout));
76+
77+
return csp_errno_zephyr_to_csp(ret);
78+
}
79+
80+
int csp_queue_dequeue_isr(csp_queue_handle_t queue, void *buf, int *unused)
81+
{
82+
int ret;
83+
struct k_msgq *q = (struct k_msgq *)queue;
84+
85+
ARG_UNUSED(unused);
86+
87+
ret = k_msgq_get(q, buf, K_NO_WAIT);
88+
89+
return csp_errno_zephyr_to_csp(ret);
90+
}
91+
92+
int csp_queue_size(csp_queue_handle_t queue)
93+
{
94+
struct k_msgq *q = (struct k_msgq *)queue;
95+
96+
return k_msgq_num_used_get(q);
97+
}
98+
99+
int csp_queue_size_isr(csp_queue_handle_t queue)
100+
{
101+
return csp_queue_size(queue);
102+
}

0 commit comments

Comments
 (0)