1 /***************************************************************************/ /**
2  * @file  rsi_os.h
3  *******************************************************************************
4  * # License
5  * <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b>
6  *******************************************************************************
7  *
8  * SPDX-License-Identifier: Zlib
9  *
10  * The licensor of this software is Silicon Laboratories Inc.
11  *
12  * This software is provided 'as-is', without any express or implied
13  * warranty. In no event will the authors be held liable for any damages
14  * arising from the use of this software.
15  *
16  * Permission is granted to anyone to use this software for any purpose,
17  * including commercial applications, and to alter it and redistribute it
18  * freely, subject to the following restrictions:
19  *
20  * 1. The origin of this software must not be misrepresented; you must not
21  *    claim that you wrote the original software. If you use this software
22  *    in a product, an acknowledgment in the product documentation would be
23  *    appreciated but is not required.
24  * 2. Altered source versions must be plainly marked as such, and must not be
25  *    misrepresented as being the original software.
26  * 3. This notice may not be removed or altered from any source distribution.
27  *
28  ******************************************************************************/
29 
30 #ifndef RSI_OS_H
31 #define RSI_OS_H
32 
33 #include "rsi_error.h"
34 #include <stdint.h>
35 /******************************************************
36  * *                      Macros
37  * ******************************************************/
38 // Macro to increment a value
39 #define RSI_ATOMIC_INCREMENT(value) \
40   {                                 \
41     (value)++;                      \
42   }
43 
44 // Macro to decrement a value
45 #define RSI_ATOMIC_DECREMENT(value) \
46   {                                 \
47     (value)--;                      \
48   }
49 // Error none (success)
50 #define RSI_ERR_NONE (0)
51 
52 // Error returned when invalid arguments are given
53 #define RSI_ERR_INVALID_ARGS (1)
54 
55 // Error returned when timeout error occurs
56 #define RSI_ERR_TIMEOUT (3)
57 
58 // Mutex unlock value
59 #define RSI_NO_OS_MUTEX_UNLOCKED (0)
60 
61 // Mutex lock value
62 #define RSI_NO_OS_MUTEX_LOCKED (1)
63 
64 // Macro to set the mutex lock
65 #define RSI_NO_OS_ATOMIC_MUTEX_SET(mutex, value) (mutex) = value
66 
67 // Macro for checking whether mutex is locked or not
68 #define RSI_NO_OS_ATOMIC_MUTEX_CHECK(mutex, value) (((mutex) == value) ? 1 : 0)
69 /******************************************************
70  * *                    Constants
71  * ******************************************************/
72 /******************************************************
73  * *                   Enumerations
74  * ******************************************************/
75 /******************************************************
76  * *                 Type Definitions
77  * ******************************************************/
78 typedef uint32_t rsi_reg_flags_t;
79 // Handle to manage Semaphores.
80 typedef uint32_t rsi_semaphore_handle_t;
81 // Handle to manage Mutex.
82 typedef uint32_t rsi_mutex_handle_t;
83 
84 // Task handler
85 typedef void *rsi_task_handle_t;
86 
87 typedef long rsi_base_type_t;
88 // Task function
89 typedef void (*rsi_task_function_t)(void *function);
90 /******************************************************
91  * *                    Structures
92  * ******************************************************/
93 /******************************************************
94  * *                 Global Variables
95  * ******************************************************/
96 /******************************************************
97  * *               Function Declarations
98  * ******************************************************/
99 /* --------- CRITICAL SECTION FUNCTIONS --------- */
100 rsi_reg_flags_t rsi_critical_section_entry(void);
101 void rsi_critical_section_exit(rsi_reg_flags_t flags);
102 /* --------------  MUTEX FUNCTIONS -------------- */
103 rsi_error_t rsi_mutex_create(rsi_mutex_handle_t *p_mutex);
104 rsi_error_t rsi_mutex_lock(volatile rsi_mutex_handle_t *p_mutex);
105 void rsi_mutex_lock_from_isr(volatile rsi_mutex_handle_t *mutex);
106 rsi_error_t rsi_mutex_unlock(volatile rsi_mutex_handle_t *p_mutex);
107 void rsi_mutex_unlock_from_isr(volatile rsi_mutex_handle_t *mutex);
108 rsi_error_t rsi_mutex_destroy(rsi_mutex_handle_t *p_mutex);
109 
110 /*  ------------- SEMAPHORE FUNCTIONS ----------- */
111 rsi_error_t rsi_semaphore_create(rsi_semaphore_handle_t *p_sem, uint32_t cnt);
112 rsi_error_t rsi_semaphore_destroy(rsi_semaphore_handle_t *p_sem);
113 rsi_error_t rsi_semaphore_check_and_destroy(rsi_semaphore_handle_t *p_sem);
114 rsi_error_t rsi_semaphore_wait(rsi_semaphore_handle_t *p_sem, uint32_t timeout);
115 rsi_error_t rsi_semaphore_post(rsi_semaphore_handle_t *p_sem);
116 rsi_error_t rsi_semaphore_post_from_isr(rsi_semaphore_handle_t *semaphore);
117 rsi_error_t rsi_semaphore_reset(rsi_semaphore_handle_t *p_sem);
118 
119 /*  ------------- TASK FUNCTIONS ----------- */
120 rsi_error_t rsi_task_create(rsi_task_function_t task_function,
121                             uint8_t *task_name,
122                             uint32_t stack_size,
123                             void *parameters,
124                             uint32_t task_priority,
125                             rsi_task_handle_t *task_handle);
126 
127 void rsi_task_destroy(rsi_task_handle_t *task_handle);
128 void rsi_task_delete(rsi_task_handle_t *task_handle);
129 void rsi_os_task_delay(uint32_t timeout_ms);
130 void rsi_task_suspend(rsi_task_handle_t *task_handle);
131 void rsi_start_os_scheduler(void);
132 void rsi_wireless_driver_task_create(void);
133 /*  ---------- OS MEMORY MAPPING FUNCTIONS -------- */
134 void *rsi_virtual_to_physical_address(void *x);
135 void *rsi_physical_to_virtual_address(void *x);
136 void *rsi_malloc(uint32_t size);
137 void rsi_free(void *p);
138 void rsi_vport_enter_critical(void);
139 void rsi_vport_exit_critical(void);
140 int32_t rsi_get_error(int32_t sockID);
141 void rsi_set_os_errno(int32_t error);
142 #endif
143