aboutsummaryrefslogtreecommitdiff
path: root/core.h
blob: 6dd53483991a1c646e45587b0d4382a7061a6801 (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
/************************************************************************/
/*                                                                      */
/*  File:           core.h                                              */
/*  Description:    armen internals                                     */
/*  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    */
/*                                                                      */
/************************************************************************/

#ifndef _ARMEN_CORE_H_
#define _ARMEN_CORE_H_

#include "armen.h"

#ifdef __AVR_ARCH__
  #include "avr/core.h"
#endif

/*
 * number of threads
*/
#define ARMEN_THREAD_MIN    2
#define ARMEN_THREAD_MAX    8
#if defined(THREAD)
  #if (THREAD < 0) || (THREAD > (ARMEN_THREAD_MAX-ARMEN_THREAD_MIN))
    #error "invalid number of threads"
  #endif
#else
  #define THREAD            1
#endif
#define ARMEN_THREADS       (ARMEN_THREAD_MIN+THREAD)

/*
 * number of timers
*/
#define ARMEN_TIMER_MIN     1
#define ARMEN_TIMER_MAX     2
#if defined(TIMER)
  #if (TIMER < ARMEN_TIMER_MIN) || (TIMER > ARMEN_TIMER_MAX)
    #error "invalid number of timers"
  #endif
  #define ARMEN_TIMERS      TIMER
#else
  #define ARMEN_TIMERS      ARMEN_TIMER_MIN
#endif

typedef struct {
    uint8_t lock;
    uint8_t value;
    uint8_t count;
    uint8_t pad;
} _sem_t;

typedef struct {
    uint8_t  status;            /* status (see below) */
    uint8_t  rank;              /* priority set by user */
    uint8_t  order;             /* priority set by kernel */
    uint8_t  joinid;            /* thread where waiting for termination */
    uint16_t events;            /* events for whitch thread is suspended  */
    uint16_t event;             /* last event */
    timer_t timer1;             /* timer counter 1 (used by sleep function) */
#if (ARMEN_TIMERS == 2)
    timer_t timer2;             /* timer counter 2 */
#endif
    void* (*entry)(void*);      /* thread entry point */
    void*    param;             /* calling argument or returned value */
    void*    istack;            /* initial value of stack pointer */
    void*    cstack;            /* current stack pointer */
#if defined(USE_SEM)
    void*    sem;               /* semaphore on thread is waiting */
#endif
} _thread_t;

/*
 * status of a thread
*/
#define TS_UNUSED           0
#define TS_RUNNING          1
#define TS_SUSPENDED        2

/*
 * events (see also armen.h)
*/
#define EVENT_JOIN          0x4000
#define EVENT_SEM           0x8000

extern uint8_t volatile _cur_thread;    /* id of current thread */
extern _thread_t _threads[];            /* table of threads */

extern void  _thread_exit( void );

extern void* _idle( void* );
extern void  _schedule( void );
extern uint8_t _disable_intr( void );
extern void  _enable_intr( uint8_t flag );
extern void* _init_context( void* (*entry)(void*), void* param, void* sp );
extern void* _save_context( void );
extern void  _restore_context( void* sp );

extern void  _sys_init( void* ram_start, void* ram_end );
extern void  _sys_clock( void );
extern void  _scheduler( void* sp );
extern void  _wakeup( uint16_t event, void* param );

extern void  _uart_init( void );

#endif