aboutsummaryrefslogtreecommitdiff
path: root/core.c
blob: 6b00e3b351aa097b6363a43c9427f1751f46aed0 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/************************************************************************/
/*                                                                      */
/*  File:           core.c                                              */
/*  Description:    kernel entry point                                  */
/*  Version:        1.0                                                 */
/*  Author:         Otto Mattik                                         */
/*                                                                      */
/*  (C)Copyright Otto Mattik 2014-2021.                                 */
/*                                                                      */
/*  This file is a part of 'armen' (a tiny operating system).           */
/*  'armen' is distributed under the CeCILL-V2.1 licence. For more      */
/*  details about this licence, please visit the website cecill.info    */
/*                                                                      */
/************************************************************************/

#include "core.h"

static uint32_t _time;

extern void* main( void* );

/*
 * called from armen_start (see assembly code)
 *
 * input:
 *  ram_start   beginning of free ram
 *  ram_end     end of ram
 * output:
*/
void _sys_init( void* ram_start, void* ram_end )
{
    register uint8_t i;
    register uint16_t stack_per_thread;
    register void* stack;

    /*
     * initialize stacks in table of threads
    */
    stack_per_thread = (ram_end - ram_start - IDLE_STACK_SIZE) / (ARMEN_THREADS - 1);
    stack = ram_end;
    for( i = 1; i < ARMEN_THREADS; i++ )
    {
        _threads[i].istack = stack;
        stack -= stack_per_thread;
    }

    /*
     * thread0 = idle
    */
    _threads[0].istack = stack;
    _threads[0].cstack = _init_context( _idle, 0, stack );
    _threads[0].rank =
    _threads[0].order = 255;
    _threads[0].entry = _idle;
    _threads[0].status = TS_RUNNING;

    /*
     * thread1 = main
    */
    _threads[1].cstack = _threads[1].istack;
    _threads[1].rank = 127;
    _threads[1].entry = main;
    _threads[1].status = TS_RUNNING;
    _cur_thread = 1;

#if defined(UART) && (UART != 0)
    _uart_init( );
#endif
}

/*
 * called by timer every millisecond (see assembly code)
 *
 * input:
 * output:
*/
void _sys_clock( void )
{
    register uint8_t i;
    register _thread_t* pthread;

    ++_time;

    /*
     * update priorities
    */
    for( i = 1; i < ARMEN_THREADS; i++ )
    {
        pthread = &_threads[i];

        if( pthread->status == TS_UNUSED )
            continue;
        if( pthread->status == TS_RUNNING )
        {
            if( i == _cur_thread )
            {
                if( pthread->order != 254 )
                    ++pthread->order;
            }
            else
            {
                if( pthread->order != 0 )
                    --pthread->order;
            }
        }

        /*
         * update thread timer(s)
        */
        else
        {
            if( pthread->timer1 != 0 && --pthread->timer1 == 0
            &&  (pthread->events & EVENT_TIMER1) != 0
            )
            {
                pthread->events &= ~EVENT_TIMER1;
                pthread->event |= EVENT_TIMER1;
                pthread->order = 0;
                pthread->status = TS_RUNNING;
            }
#if (ARMEN_TIMERS == 2)
            if( pthread->timer2 != 0 && --pthread->timer2 == 0
            &&  (pthread->events & EVENT_TIMER2) != 0
            )
            {
                pthread->events &= ~EVENT_TIMER2;
                pthread->event |= EVENT_TIMER2;
                pthread->order = 0;
                pthread->status = TS_RUNNING;
            }
#endif
        }
    }
}

/*
 * thread election
 *
 * input:
 *  sp          current value of stack pointer
 * output:
*/
void _scheduler( void* sp )
{
    register uint8_t i, rank, order, elected;
    register _thread_t* pthread;

    elected = 0;
    rank = order = 255;
    _threads[_cur_thread].cstack = sp;
    for( i = 1; i < ARMEN_THREADS; i++ )
    {
        pthread = &_threads[i];

        if( pthread->status != TS_RUNNING
        ||  pthread->order > order
        ||  (pthread->order == order && pthread->rank > rank ) )
            continue;
        elected = i;
        rank = pthread->rank;
        order = pthread->order;
    }

    if( elected != _cur_thread )
    {
        _cur_thread = elected;
        _restore_context( _threads[elected].cstack );
        /* no return */
    }
}

/*
 * get current time (milliseconds since start-up)
 *
 * input:
 * output:
 *  count of milliseconds
*/
uint32_t get_time( void )
{
    return( _time );
}

/*
 * suspend execution of the calling thread
 *
 * input:
 *  events      mask of events
 * output:
 *  event occured
*/
uint16_t wait_event( uint16_t events )
{
    uint16_t ret = 0;

    if( events != 0 )
    {
        do
        {
            _disable_intr( );
            _threads[_cur_thread].events |= events;
            _threads[_cur_thread].status = TS_SUSPENDED;
            _schedule( );
        }
        while( (_threads[_cur_thread].event & events) == 0 ) ;

        ret = _threads[_cur_thread].event & events;
        _threads[_cur_thread].event &= ~events;
    }
    return( ret );
}

/*
 * current value of timer
 *
 * input:
 *  timer       timer identifier
 * output:
 *  timer value if success, otherwise -1
*/
timer_t get_timer( uint8_t timer )
{
#ifdef CHECK_PARAM
    if( timer < 1 || timer > ARMEN_TIMERS )
        return( (timer_t)-1 );
#endif
#if (ARMEN_TIMERS == 2)
    if( timer == 2 )
        return( _threads[_cur_thread].timer2 );
#endif
    return( _threads[_cur_thread].timer1 );
}

/*
 * suspend current thread during a period
 *
 * input:
 *  timer       timer identifier
 *  msecs       period in milliseconds
 * output:
 *  0 if success, otherwise -1
*/
int8_t set_timer( uint8_t timer, timer_t msecs )
{
#ifdef CHECK_PARAM
    if( timer < 1 || timer > ARMEN_TIMERS )
        return( -1 );
#endif
#if (ARMEN_TIMERS == 2)
    if( timer == 2 )
    {
        _threads[_cur_thread].timer2 = msecs;
        if( msecs != 0 )
            _threads[_cur_thread].events &= ~EVENT_TIMER2;
        else
            _threads[_cur_thread].events |= EVENT_TIMER2;
    }
    else
#endif
    {
        _threads[_cur_thread].timer1 = msecs;
        if( msecs != 0 )
            _threads[_cur_thread].events &= ~EVENT_TIMER1;
        else
            _threads[_cur_thread].events |= EVENT_TIMER1;
    }
    return( 0 );
}
void sleep( timer_t msecs )
{
    if( msecs != 0 )
    {
        _threads[_cur_thread].events |= EVENT_TIMER1;
        _threads[_cur_thread].timer1 = msecs;
        wait_event( EVENT_TIMER1 );
    }
}

/*
 * wake up thread(s) on event
 *
 * input:
 *  event       reason for wake up
 *  param       depends on event
 * output:
*/
void _wakeup( uint16_t event, void* param )
{
    register uint8_t i;
    register _thread_t* pthread;

    for( i = 1; i < ARMEN_THREADS; i++ )
    {
        pthread = &_threads[i];

        if( pthread->status != TS_SUSPENDED
        ||  (pthread->events & event) == 0
#if defined(USE_SEM)
        ||  (event == EVENT_SEM && pthread->sem != param)
#endif
        ||  (event == EVENT_JOIN && pthread->joinid != _cur_thread) )
            continue;

        if( event == EVENT_JOIN )
            pthread->param = param;
        pthread->events &= ~event;
        pthread->event |= event;
        pthread->order = 0;
        pthread->status = TS_RUNNING;
    }
}
void send_user_event( void )
{
    register uint8_t iflag;

    iflag = _disable_intr( );
    _wakeup( EVENT_USER, 0 );
    _enable_intr( iflag );
}