1 /*! *********************************************************************************
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2024 NXP
4  * All rights reserved.
5  *
6  *
7  * This is the header file for the OS Abstraction layer for Zephyr.
8  *
9  * SPDX-License-Identifier: BSD-3-Clause
10  ********************************************************************************** */
11 #ifndef _FSL_OS_ABSTRACTION_ZEPHYR_H_
12 #define _FSL_OS_ABSTRACTION_ZEPHYR_H_
13 
14 #include <zephyr/kernel.h>
15 #include <zephyr/kernel/thread.h>
16 
17 /*!
18  * @addtogroup os_abstraction_zephyr
19  * @{
20  */
21 
22 /**
23  * INFORMATION for developers:
24  *
25  * To turn on ASSERTS of OSA layer, define and set macro CONFIG_OSA_DEBUG_ASSERT_ENABLED = 1.
26  * Turned off by default.
27  *
28  * Call OSA_Init() before using any task API (taskCreate, etc ...)
29  *
30  * List of differences between NXP's OSA API and Zephyr's API:
31  *      [OSA_TaskCreate] unused following variables in OSA's thread_def: tlink, useFloat, instances.
32  *      [OSA_TaskGetPriority] different priority formats. See function comment for more info.
33  *      [OSA_TaskSetPriority] different priority formats. See function comment for more info.
34  *      [OSA_Start] does nothing in Zephyr. Scheduler started automatically.
35  * List of unsupported OSA API:
36  *      [OSA_InstallIntHandler]
37  *
38  * CONFIG_EVENTS must be set in order to use Event api. Zephyr is not enabling its event API by default.
39  *
40  */
41 
42 /*******************************************************************************
43  * Definitions
44  ******************************************************************************/
45 
46 /** Zephyr does not check whether handle for specific OS primitive is correct.
47    This may lead to unexpected behavior. This OSA layer port adds checkmark to all
48    OS primitives checking if it is correct at every API call, returning back error
49    and rising assert if incorrect, preventing from unexpected behavior. Every checkmark
50    value prefixes with "OSA_CHECKMARK".
51  */
52 #define OSA_CHECKMARK_TASK  (0x05A05A01u)
53 #define OSA_CHECKMARK_MSGQ  (0x05A05A02u)
54 #define OSA_CHECKMARK_EVENT (0x05A05A03u)
55 #define OSA_CHECKMARK_MUTEX (0x05A05A04u)
56 #define OSA_CHECKMARK_SEM   (0x05A05A05u)
57 #define OSA_CHECKMARK_EMPTY (0x05A05A00u)
58 typedef uint32_t checkmark_t;
59 
60 /** List of Zephyr primitive handles.
61    Each time RTOS primitive is created, it is pointing to one of following structures
62    with naming convention: osa_zephyr_<primitiveName>Handle_t. For example:
63    osa_zephyr_taskHandle_t.
64 */
65 
66 /*! @brief Private Zephyr OSA task handle. Do not use directly. */
67 typedef struct _osa_zephyr_taskHandle
68 {
69     sys_dnode_t listNode;
70     k_tid_t handle;
71     k_thread_stack_t *stackPtr;
72     struct k_thread controlBlock;
73     checkmark_t checkmark;
74     struct k_sem notification;
75 } osa_zephyr_taskHandle_t;
76 
77 /*! @brief Private Zephyr OSA message queue handle. Do not use directly. */
78 typedef struct _osa_zephyr_msgQueueHandle
79 {
80     struct k_msgq handle;
81     checkmark_t checkmark;
82 } osa_zephyr_msgQueueHandle_t;
83 
84 /*! @brief Private Zephyr OSA event handle. Do not use directly. */
85 typedef struct _osa_zephyr_eventHandle
86 {
87 #ifdef CONFIG_EVENTS
88     struct k_event handle;
89     bool is_autoClear;
90     checkmark_t checkmark;
91 #endif
92 } osa_zephyr_eventHandle_t;
93 
94 /*! @brief Private Zephyr OSA mutex handle. Do not use directly. */
95 typedef struct _osa_zephyr_mutexHandle
96 {
97     struct k_mutex handle;
98     checkmark_t checkmark;
99 } osa_zephyr_mutexHandle_t;
100 
101 /*! @brief Private Zephyr OSA semaphore handle. Do not use directly. */
102 typedef struct _osa_zephyr_semHandle
103 {
104     struct k_sem handle;
105     checkmark_t checkmark;
106 } osa_zephyr_semHandle_t;
107 
108 /*! @brief Private Zephyr OSA timer handle. Do not use directly.*/
109 typedef struct _osa_zephyr_timerHandle
110 {
111     struct k_timer handle;
112     struct k_work work;
113     uint32_t padding[4];
114 } osa_zephyr_timerHandle_t;
115 
116 /*! @brief Zephyr OSA task handle size. */
117 #define OSA_TASK_HANDLE_SIZE (sizeof(osa_zephyr_taskHandle_t))
118 
119 /*! @brief Zephyr OSA event handle size. */
120 #define OSA_EVENT_HANDLE_SIZE (sizeof(osa_zephyr_eventHandle_t))
121 
122 /*! @brief Zephyr OSA semaphore handle size. */
123 #define OSA_SEM_HANDLE_SIZE (sizeof(osa_zephyr_semHandle_t))
124 
125 /*! @brief Zephyr OSA mutex handle size. */
126 #define OSA_MUTEX_HANDLE_SIZE (sizeof(osa_zephyr_mutexHandle_t))
127 
128 /*! @brief Zephyr OSA queue handle size. */
129 #define OSA_MSGQ_HANDLE_SIZE (sizeof(osa_zephyr_msgQueueHandle_t))
130 
131 /*! @brief Zephyr OSA timer handle size. */
132 #define OSA_TIMER_HANDLE_SIZE (sizeof(osa_zephyr_timerHandle_t))
133 
134 #define OSA_ZEPHYR_TASK_GET_PRIORITY_ERROR (0xDEAD)
135 
136 /**
137  * @}
138  */
139 
140 #endif /* _FSL_OS_ABSTRACTION_ZEPHYR_H_ */
141