-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpico_osal_noos.c
166 lines (141 loc) · 3.29 KB
/
pico_osal_noos.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* pico_osal.h
*
* Created on: December 2014
* Author: Maxime Vincent
* Description: OS Abstraction Layer between PicoTCP and No Operating System
*
*/
/* PicoTCP includes */
#include "pico_defines.h"
#include "pico_config.h"
#include "pico_stack.h"
#include "pico_osal.h"
#define osal_dbg(...)
//#define osal_dbg(...) printf(__VA_ARGS__)
/*****************************************************************************
* Public functions
****************************************************************************/
/* ============= */
/* == MUTEXES == */
/* ============= */
struct osal_mutex {
volatile int want_to_take; /* for ISR safety, basically a mutex for the mutex */
volatile int mutex;
int idx; /* only to keep track of the amount/idx, no real function .. */
};
static uint8_t mtx_number = 0;
void * pico_mutex_init(void)
{
struct osal_mutex * mutex;
mutex = pico_zalloc(sizeof(struct osal_mutex));
osal_dbg("mi: %p for %p\n", mutex, __builtin_return_address(0));
if (!mutex)
return NULL;
mutex->mutex = 1;
mutex->idx = mtx_number++;
return mutex;
}
void pico_mutex_deinit(void * mutex)
{
struct osal_mutex * mtx = mutex;
pico_free(mutex);
}
int pico_mutex_lock_timeout(void * mutex, int timeout)
{
int retval = 0;
if(mutex != NULL)
{
struct osal_mutex * mtx = mutex;
pico_time timestamp = PICO_TIME_MS();
while (mtx->mutex == 0)
{
pico_stack_tick();
#ifdef _POSIX_VERSION
usleep(500);
#endif
/* break on timeout unless infinite timeout */
if ((timeout != -1) && (PICO_TIME_MS() > (timestamp + timeout)))
break;
}
if (mtx->mutex == 1)
{
mtx->mutex = 0; /* take the mutex */
}
else
{
retval = -1; /* timeout */
}
}
return retval;
}
void pico_mutex_lock(void * mutex)
{
pico_mutex_lock_timeout(mutex, -1);
}
void pico_mutex_unlock(void * mutex)
{
if(mutex != NULL)
{
struct osal_mutex * mtx = mutex;
mtx->mutex = 1;
}
}
void pico_mutex_unlock_ISR(void * mutex)
{
if(mutex != NULL)
{
struct osal_mutex * mtx = mutex;
// tricky stuff needed or not?
mtx->mutex = 1;
}
}
/* ============= */
/* == SIGNALS == */
/* ============= */
void * pico_signal_init(void)
{
void * signal = pico_mutex_init();
pico_mutex_lock(signal);
return signal;
}
void pico_signal_deinit(void * signal)
{
pico_mutex_deinit(signal);
}
void pico_signal_wait(void * signal)
{
pico_signal_wait_timeout(signal, -1);
}
int pico_signal_wait_timeout(void * signal, int timeout)
{
return pico_mutex_lock_timeout(signal, timeout);
}
void pico_signal_send(void * signal)
{
pico_mutex_unlock(signal);
}
void pico_signal_send_ISR(void * signal)
{
pico_mutex_unlock_ISR(signal);
}
/* ============= */
/* == THREADS == */
/* ============= */
pico_thread_t pico_thread_create(pico_thread_fn thread, void *arg, int stack_size, int prio)
{
(void)thread;
(void)arg;
(void)stack_size;
(void)prio;
return NULL;
}
void pico_thread_destroy(pico_thread_t t)
{
return;
}
void pico_msleep(int ms)
{
pico_time now = PICO_TIME_MS();
while ((pico_time)(now + ms) < PICO_TIME_MS());
}