aboutsummaryrefslogtreecommitdiff
path: root/src/osal/lv_cmsis_rtos2.c
blob: ed414c8dab0e319af503684704a1f3d808d8d256 (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
/**
 * @file lv_cmsis_rtos2.c
 *
 */

/*
 * Copyright (C) 2023 Arm Limited or its affiliates. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_os.h"

#if LV_USE_OS == LV_OS_CMSIS_RTOS2

#include "../misc/lv_log.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

lv_result_t lv_thread_init(lv_thread_t * thread, lv_thread_prio_t prio, void (*callback)(void *), size_t stack_size,
                           void * user_data)
{
    static const osPriority_t prio_map[] = {
        [LV_THREAD_PRIO_LOWEST] = osPriorityLow,
        [LV_THREAD_PRIO_LOW] = osPriorityBelowNormal,
        [LV_THREAD_PRIO_MID] = osPriorityNormal,
        [LV_THREAD_PRIO_HIGH] = osPriorityHigh,
        [LV_THREAD_PRIO_HIGHEST] = osPriorityRealtime7,
    };

    osThreadAttr_t c_tThreadAttribute = {
        .stack_size = stack_size,
        .priority = prio_map[prio],
    };

    *thread = osThreadNew(callback, user_data, &c_tThreadAttribute);

    if(NULL == *thread) {
        LV_LOG_WARN("Error: Failed to create a cmsis-rtos2 thread.");
        return LV_RESULT_INVALID;
    }

    return LV_RESULT_OK;

}

lv_result_t lv_thread_delete(lv_thread_t * thread)
{
    osThreadDetach(*thread);
    osStatus_t status = osThreadTerminate(*thread);
    if(status == osOK) {
        return LV_RESULT_OK;
    }
    return LV_RESULT_INVALID;
}

lv_result_t lv_mutex_init(lv_mutex_t * mutex)
{
    const osMutexAttr_t Thread_Mutex_attr = {
        "LVGLMutex",
        osMutexRecursive | osMutexPrioInherit | osMutexRobust,
    };

    *mutex = osMutexNew(&Thread_Mutex_attr);
    if(*mutex == NULL)  {
        LV_LOG_WARN("Error: failed to create cmsis-rtos mutex");
        return LV_RESULT_INVALID;
    }

    return LV_RESULT_OK;

}

lv_result_t lv_mutex_lock(lv_mutex_t * mutex)
{
    osStatus_t status = osMutexAcquire(*mutex, 0U);
    if(status != osOK)  {
        LV_LOG_WARN("Error: failed to lock cmsis-rtos2 mutex %d", (int)status);
        return LV_RESULT_INVALID;
    }

    return LV_RESULT_OK;
}

lv_result_t lv_mutex_lock_isr(lv_mutex_t * mutex)
{
    osStatus_t status = osMutexAcquire(*mutex, 0U);
    if(status != osOK)  {
        LV_LOG_WARN("Error: failed to lock cmsis-rtos2 mutex in an ISR %d", (int)status);
        return LV_RESULT_INVALID;
    }

    return LV_RESULT_OK;
}

lv_result_t lv_mutex_unlock(lv_mutex_t * mutex)
{
    osStatus_t status = osMutexRelease(*mutex);
    if(status != osOK)  {
        LV_LOG_WARN("Error: failed to release cmsis-rtos2 mutex %d", (int)status);
        return LV_RESULT_INVALID;
    }

    return LV_RESULT_OK;
}

lv_result_t lv_mutex_delete(lv_mutex_t * mutex)
{
    osStatus_t status = osMutexDelete(*mutex);
    if(status != osOK)  {
        LV_LOG_WARN("Error: failed to delete cmsis-rtos2 mutex %d", (int)status);
        return LV_RESULT_INVALID;
    }

    return LV_RESULT_OK;
}

lv_result_t lv_thread_sync_init(lv_thread_sync_t * sync)
{
    *sync = osEventFlagsNew(NULL);
    if(NULL == *sync) {
        LV_LOG_WARN("Error: failed to create a cmsis-rtos2 EventFlag");
        return LV_RESULT_INVALID;
    }
    return LV_RESULT_OK;
}

lv_result_t lv_thread_sync_wait(lv_thread_sync_t * sync)
{
    uint32_t ret = osEventFlagsWait(*sync, 0x01, osFlagsWaitAny, osWaitForever);
    if(ret & (1 << 31)) {
        LV_LOG_WARN("Error: failed to wait a cmsis-rtos2 EventFlag %d", ret);
        return LV_RESULT_INVALID;
    }

    return LV_RESULT_OK;
}

lv_result_t lv_thread_sync_signal(lv_thread_sync_t * sync)
{
    uint32_t ret = osEventFlagsSet(*sync, 0x01);
    if(ret & (1 << 31)) {
        LV_LOG_WARN("Error: failed to set a cmsis-rtos2 EventFlag %d", ret);
        return LV_RESULT_INVALID;
    }

    return LV_RESULT_OK;
}

lv_result_t lv_thread_sync_signal_isr(lv_thread_sync_t * sync)
{
    return lv_thread_sync_signal(sync);
}

lv_result_t lv_thread_sync_delete(lv_thread_sync_t * sync)
{
    osStatus_t status = osEventFlagsDelete(*sync);
    if(status != osOK)  {
        LV_LOG_WARN("Error: failed to delete a cmsis-rtos2 EventFlag %d", (int)status);
        return LV_RESULT_INVALID;
    }

    return LV_RESULT_OK;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

#endif /*LV_USE_OS == LV_OS_CMSIS_RTOS2*/