Skip to content

Commit 5e02fe4

Browse files
committed
zephyr: Add contrib/zephyr/samples/arch
This is a Zephyr version of examples/csp_arch.c. The change is kept minimum, so that users can take a diff against the original. Signed-off-by: Yasushi SHOJI <[email protected]>
1 parent 1391417 commit 5e02fe4

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
project(csp_arch)
4+
cmake_minimum_required(VERSION 3.20)
5+
6+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
7+
target_sources(app PRIVATE main.c)
8+
target_link_libraries(app PRIVATE libcsp)

contrib/zephyr/samples/arch/main.c

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
Cubesat Space Protocol - A small network-layer protocol designed for Cubesats
3+
Copyright (C) 2012 Gomspace ApS (http://www.gomspace.com)
4+
Copyright (C) 2012 AAUSAT3 Project (http://aausat3.space.aau.dk)
5+
6+
This library is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
This library is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; if not, write to the Free Software
18+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#define CSP_USE_ASSERT 1 // always enable CSP assert
22+
23+
#include <csp/csp_debug.h>
24+
#include <csp/arch/csp_thread.h>
25+
#include <csp/arch/csp_clock.h>
26+
#include <csp/arch/csp_time.h>
27+
#include <csp/arch/csp_queue.h>
28+
#include <csp/arch/csp_semaphore.h>
29+
30+
#include <stdlib.h>
31+
32+
static bool thread_executed = false;
33+
34+
void csp_assert_fail_action(const char *assertion, const char *file, int line) {
35+
printf("assertion: [%s], file: %s:%d\r\n", assertion, file, line);
36+
exit(1);
37+
}
38+
39+
CSP_DEFINE_TASK(thread_func) {
40+
csp_log_info("Thread started");
41+
thread_executed = true;
42+
csp_sleep_ms(10000); // safty - ensure process terminates
43+
exit(1);
44+
return CSP_TASK_RETURN;
45+
}
46+
47+
#define STACKSIZE 1024
48+
K_THREAD_STACK_DEFINE(stack, STACKSIZE);
49+
50+
int main(int argc, char * argv[]) {
51+
52+
// debug/log - enable all levels
53+
for (int i = 0; i <= CSP_LOCK; ++i) {
54+
csp_debug_set_level(i, true);
55+
}
56+
csp_log_error("csp_log_error(...), level: %d", CSP_ERROR);
57+
csp_log_warn("csp_log_warn(...), level: %d", CSP_WARN);
58+
csp_log_info("csp_log_info((...), level: %d", CSP_INFO);
59+
csp_log_buffer("csp_log_buffer(...), level: %d", CSP_BUFFER);
60+
csp_log_packet("csp_log_packet(...), level: %d", CSP_PACKET);
61+
csp_log_protocol("csp_log_protocol(...), level: %d", CSP_PROTOCOL);
62+
csp_log_lock("csp_log_lock(...), level: %d", CSP_LOCK);
63+
64+
// create a thread - csp_thread doesn't support join
65+
csp_thread_handle_t tid;
66+
csp_thread_t new_thread;
67+
68+
tid = csp_thread_create_static(&new_thread, "thread",
69+
stack, K_THREAD_STACK_SIZEOF(stack),
70+
thread_func, NULL, 0);
71+
//csp_assert(res == CSP_ERR_NONE);
72+
//csp_assert(thread != 0);
73+
74+
// clock
75+
csp_timestamp_t csp_clock = {};
76+
csp_clock_get_time(&csp_clock);
77+
csp_assert(csp_clock.tv_sec != 0);
78+
csp_log_info("csp_clock_get_time(..) -> sec:nsec = %"PRIu32":%"PRIu32, csp_clock.tv_sec, csp_clock.tv_nsec);
79+
80+
// relative time
81+
const uint32_t msec1 = csp_get_ms();
82+
const uint32_t msec2 = csp_get_ms_isr();
83+
const uint32_t sec1 = csp_get_s();
84+
const uint32_t sec2 = csp_get_s_isr();
85+
csp_sleep_ms(2000);
86+
87+
csp_assert(csp_get_ms() >= (msec1 + 500));
88+
csp_assert(csp_get_ms_isr() >= (msec2 + 500));
89+
csp_assert(csp_get_s() >= (sec1 + 1));
90+
csp_assert(csp_get_s_isr() >= (sec2 + 1));
91+
92+
// check thread actually executed
93+
csp_assert(thread_executed != false);
94+
95+
// queue handling
96+
uint32_t value;
97+
csp_static_queue_t sq;
98+
csp_queue_handle_t q;
99+
char buf[3 * sizeof(value)];
100+
q = csp_queue_create_static(3, sizeof(value), buf, &sq);
101+
csp_assert(csp_queue_size(q) == 0);
102+
csp_assert(csp_queue_size_isr(q) == 0);
103+
csp_assert(csp_queue_dequeue(q, &value, 0) == CSP_QUEUE_ERROR);
104+
csp_assert(csp_queue_dequeue(q, &value, 200) == CSP_QUEUE_ERROR);
105+
csp_assert(csp_queue_dequeue_isr(q, &value, NULL) == CSP_QUEUE_ERROR);
106+
value = 1;
107+
csp_assert(csp_queue_enqueue(q, &value, 0) == CSP_QUEUE_OK);
108+
value = 2;
109+
csp_assert(csp_queue_enqueue(q, &value, 200) == CSP_QUEUE_OK);
110+
value = 3;
111+
csp_assert(csp_queue_enqueue_isr(q, &value, NULL) == CSP_QUEUE_OK);
112+
csp_assert(csp_queue_size(q) == 3);
113+
csp_assert(csp_queue_size_isr(q) == 3);
114+
value = 10;
115+
csp_assert(csp_queue_enqueue(q, &value, 0) == CSP_QUEUE_ERROR);
116+
value = 20;
117+
csp_assert(csp_queue_enqueue(q, &value, 200) == CSP_QUEUE_ERROR);
118+
value = 30;
119+
csp_assert(csp_queue_enqueue_isr(q, &value, NULL) == CSP_QUEUE_ERROR);
120+
value = 100;
121+
csp_assert(csp_queue_dequeue(q, &value, 0) == CSP_QUEUE_OK);
122+
csp_assert(value == 1);
123+
csp_assert(csp_queue_dequeue(q, &value, 200) == CSP_QUEUE_OK);
124+
csp_assert(value == 2);
125+
csp_assert(csp_queue_dequeue_isr(q, &value, NULL) == CSP_QUEUE_OK);
126+
csp_assert(value == 3);
127+
128+
// mutex - the actual mutex lock can't be tested from a single thread
129+
csp_mutex_t m;
130+
csp_mutex_create_static(&m, NULL);
131+
csp_assert(csp_mutex_lock(&m, 0) == CSP_MUTEX_OK);
132+
#if (CSP_WINDOWS || CSP_ZEPHYR) // implementations differ in return value if already locked
133+
csp_assert(csp_mutex_lock(&m, 200) == CSP_MUTEX_OK);
134+
#else
135+
csp_assert(csp_mutex_lock(&m, 200) == CSP_MUTEX_ERROR);
136+
#endif
137+
csp_assert(csp_mutex_unlock(&m) == CSP_MUTEX_OK);
138+
csp_assert(csp_mutex_lock(&m, 200) == CSP_MUTEX_OK);
139+
csp_assert(csp_mutex_unlock(&m) == CSP_MUTEX_OK);
140+
141+
// semaphore
142+
csp_bin_sem_handle_t s;
143+
csp_bin_sem_create_static(&s, NULL);
144+
csp_assert(csp_bin_sem_wait(&s, 0) == CSP_SEMAPHORE_OK);
145+
csp_assert(csp_bin_sem_post(&s) == CSP_SEMAPHORE_OK);
146+
#if (CSP_POSIX || CSP_ZEPHYR) // implementations differ in return value if already posted/signaled
147+
csp_assert(csp_bin_sem_post_isr(&s, NULL) == CSP_SEMAPHORE_OK);
148+
#else
149+
csp_assert(csp_bin_sem_post_isr(&s, NULL) == CSP_SEMAPHORE_ERROR);
150+
#endif
151+
csp_assert(csp_bin_sem_wait(&s, 200) == CSP_SEMAPHORE_OK);
152+
csp_assert(csp_bin_sem_wait(&s, 200) == CSP_SEMAPHORE_ERROR);
153+
154+
return 0;
155+
}

contrib/zephyr/samples/arch/prj.conf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONFIG_LIBCSP=y
2+
CONFIG_LIBCSP_DEBUG=y
3+
CONFIG_LIBCSP_LOG_LEVEL_DBG=y
4+
5+
CONFIG_MINIMAL_LIBC_RAND=y
6+
CONFIG_LOG=y
7+
CONFIG_LOG2_MODE_DEFERRED=y
8+
CONFIG_LOG_SPEED=y

0 commit comments

Comments
 (0)