tinyproto
tiny_types.h
Go to the documentation of this file.
1 /*
2  Copyright 2016-2025 (C) Alexey Dynda
3 
4  This file is part of Tiny Protocol Library.
5 
6  GNU General Public License Usage
7 
8  Protocol Library is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Protocol Library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
20 
21  Commercial License Usage
22 
23  Licensees holding valid commercial Tiny Protocol licenses may use this file in
24  accordance with the commercial license agreement provided in accordance with
25  the terms contained in a written agreement between you and Alexey Dynda.
26  For further information contact via email on github account.
27 */
28 
36 #pragma once
37 
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #endif
42 
43 #if defined(CONFIG_ENABLE_CPP_HAL)
44 #include "cpp/cpp_hal.h"
45 #elif defined(TINY_CUSTOM_PLATFORM)
46 #include "no_platform/no_platform_hal.h"
47 #elif defined(__AVR__)
48 #include "avr/avr_hal.h"
49 #elif defined(__XTENSA__)
50 #include "esp32/esp32_hal.h"
51 #elif defined(ARDUINO)
52 #include "arduino/arduino_hal.h"
53 #elif defined(__linux__)
54 #include "linux/linux_hal.h"
55 #elif defined(__APPLE__) && defined(__MACH__)
56 #include "macos/macos_hal.h"
57 #elif defined(__MINGW32__)
58 #include "mingw32/mingw32_hal.h"
59 #elif defined(_WIN32)
60 #include "win32/win32_hal.h"
61 #elif defined(CPU_S32K144HFT0VLLT)
62 #include "freertos/freertos_hal.h"
63 #else
64 #info "Platform not supported. Multithread support is disabled"
65 #include "no_platform/no_platform_hal.h"
66 #endif
67 
68 #include <stdint.h>
69 #include <stdbool.h>
70 
71 
72 #ifndef CONFIG_TINYHAL_THREAD_SUPPORT
73 
76 #define CONFIG_TINYHAL_THREAD_SUPPORT 0
77 #endif
78 
79 
80 #if defined(_MSC_VER)
81 #define TINY_ALIGNED(x) __declspec(align(x))
83 #elif defined(__GNUC__)
84 #define TINY_ALIGNED(x) __attribute__ ((aligned (x)))
86 #else
87 #define TINY_ALIGNED(x)
89 #endif
90 
91 
92 #if defined(__TARGET_CPU_CORTEX_M0) || defined(__TARGET_CPU_CORTEX_M0_) || defined(__ARM_ARCH_6M__) || \
93  defined(__TARGET_CPU_CORTEX_M3) || defined(__TARGET_CPU_CORTEX_M4) || defined(__ARM_ARCH_7EM__) || \
94  defined(__ARM_ARCH_7M__)
95 
97 #define TINY_ALIGN_STRUCT_VALUE (sizeof(uintptr_t))
98 
99 #elif defined(_MSC_VER)
100 
101 // MS Compiler at least in MSVC 2019 doesn't support sizeof and braces in __declspec(align(x)).
103 #define TINY_ALIGN_STRUCT_VALUE 8
104 
105 #else
106 
108 #define TINY_ALIGN_STRUCT_VALUE (uintptr_t)(sizeof(uintptr_t))
109 
110 #endif
111 
113 #define TINY_ALIGNED_STRUCT TINY_ALIGNED(TINY_ALIGN_STRUCT_VALUE)
114 
116 #define TINY_ALIGN_BUFFER(x) ((uint8_t *)( ((uintptr_t)x + TINY_ALIGN_STRUCT_VALUE - 1) & (~(TINY_ALIGN_STRUCT_VALUE - 1)) ))
117 
122 #define TINY_SUCCESS (0)
124 #define TINY_ERR_FAILED (-1)
126 #define TINY_ERR_TIMEOUT (-2)
128 #define TINY_ERR_DATA_TOO_LARGE (-3)
130 #define TINY_ERR_INVALID_DATA (-4)
132 #define TINY_ERR_BUSY (-5)
134 #define TINY_ERR_OUT_OF_SYNC (-6)
136 #define TINY_ERR_AGAIN (-7)
138 #define TINY_ERR_WRONG_CRC (-8)
140 #define TINY_ERR_OUT_OF_MEMORY (-9)
142 #define TINY_ERR_UNKNOWN_PEER (-10)
144 #define TINY_ERR_IO (-11)
146 
154 #define TINY_FLAG_NO_WAIT (0)
156 #define TINY_FLAG_READ_ALL (1)
158 #define TINY_FLAG_LOCK_SEND (2)
160 #define TINY_FLAG_WAIT_FOREVER (0x80)
162 
174  typedef int (*write_block_cb_t)(void *pdata, const void *buffer, int size);
175 
185  typedef int (*read_block_cb_t)(void *pdata, void *buffer, int size);
186 
194  typedef void (*on_frame_cb_t)(void *udata, uint8_t *pdata, int size);
195 
203  typedef void (*on_tx_frame_cb_t)(void *udata, const uint8_t *pdata, int size);
204 
213  typedef void (*on_frame_read_cb_t)(void *udata, uint8_t address, uint8_t *pdata, int size);
214 
223  typedef void (*on_frame_send_cb_t)(void *udata, uint8_t address, const uint8_t *pdata, int size);
224 
232  typedef void (*on_connect_event_cb_t)(void *udata, uint8_t address, bool connected);
233 
234 #define EVENT_BITS_ALL 0xFF
235 #define EVENT_BITS_CLEAR 1
236 #define EVENT_BITS_LEAVE 0
237 
238 
247  void tiny_mutex_create(tiny_mutex_t *mutex);
248 
253  void tiny_mutex_destroy(tiny_mutex_t *mutex);
254 
259  void tiny_mutex_lock(tiny_mutex_t *mutex);
260 
267  uint8_t tiny_mutex_try_lock(tiny_mutex_t *mutex);
268 
273  void tiny_mutex_unlock(tiny_mutex_t *mutex);
274 
286  void tiny_events_create(tiny_events_t *events);
287 
292  void tiny_events_destroy(tiny_events_t *events);
293 
303  uint8_t tiny_events_wait(tiny_events_t *event, uint8_t bits, uint8_t clear, uint32_t timeout);
304 
314  uint8_t tiny_events_check_int(tiny_events_t *event, uint8_t bits, uint8_t clear);
315 
321  void tiny_events_set(tiny_events_t *event, uint8_t bits);
322 
328  void tiny_events_clear(tiny_events_t *event, uint8_t bits);
329 
341  void tiny_sleep(uint32_t ms);
342 
347  void tiny_sleep_us(uint32_t us);
348 
352  uint32_t tiny_millis();
353 
357  uint32_t tiny_micros();
358 
365  void tiny_log_level(uint8_t level);
366 
367 #ifdef __cplusplus
368 }
369 #endif
void tiny_mutex_destroy(tiny_mutex_t *mutex)
Destroys cross-platform mutex.
Definition: cpp_hal.inl:37
void tiny_mutex_unlock(tiny_mutex_t *mutex)
Unlocks cross-platform mutex.
Definition: cpp_hal.inl:49
void(* on_frame_cb_t)(void *udata, uint8_t *pdata, int size)
on_frame_cb_t is a callback function, which is called every time new frame is received.
Definition: tiny_types.h:194
void tiny_events_set(tiny_events_t *event, uint8_t bits)
Sets bits for cross-platform event group object.
Definition: cpp_hal.inl:69
void tiny_mutex_lock(tiny_mutex_t *mutex)
Locks cross-platform mutex.
Definition: cpp_hal.inl:41
void(* on_connect_event_cb_t)(void *udata, uint8_t address, bool connected)
on_connect_event_cb_t is a callback function, which is called every time connection is established or...
Definition: tiny_types.h:232
void(* on_frame_send_cb_t)(void *udata, uint8_t address, const uint8_t *pdata, int size)
on_frame_send_cb_t is a callback function, which is called every time new frame is sent...
Definition: tiny_types.h:223
void tiny_mutex_create(tiny_mutex_t *mutex)
Creates cross-platform mutex.
Definition: cpp_hal.inl:33
void tiny_sleep(uint32_t ms)
Sleeps for specified period in milliseconds.
Definition: arduino_hal.inl:84
uint32_t tiny_millis()
Returns timestamp in milliseconds since system started up.
Definition: arduino_hal.inl:94
void tiny_sleep_us(uint32_t us)
Sleeps for specified period in microseconds.
Definition: arduino_hal.inl:89
int(* read_block_cb_t)(void *pdata, void *buffer, int size)
The function reads data from communication channel.
Definition: tiny_types.h:185
uint8_t tiny_events_check_int(tiny_events_t *event, uint8_t bits, uint8_t clear)
Checks specified bits.
Definition: cpp_hal.inl:65
void tiny_events_destroy(tiny_events_t *events)
Destroys cross platform event group object.
Definition: cpp_hal.inl:57
uint32_t tiny_micros()
Returns timestamp in microseconds since system started up.
Definition: arduino_hal.inl:99
void tiny_events_create(tiny_events_t *events)
Creates cross platform event group object.
Definition: cpp_hal.inl:53
void(* on_frame_read_cb_t)(void *udata, uint8_t address, uint8_t *pdata, int size)
on_frame_read_cb_t is a callback function, which is called every time new frame is received...
Definition: tiny_types.h:213
int(* write_block_cb_t)(void *pdata, const void *buffer, int size)
The function writes data to communication channel port.
Definition: tiny_types.h:174
void(* on_tx_frame_cb_t)(void *udata, const uint8_t *pdata, int size)
on_frame_send_cb_t is a callback function, which is called every time new frame is sent...
Definition: tiny_types.h:203
void tiny_events_clear(tiny_events_t *event, uint8_t bits)
Clears bits for cross-platform event group object.
Definition: cpp_hal.inl:73
Events group type used by Tiny Protocol implementation.
Definition: cpp_hal.h:60
uint8_t tiny_events_wait(tiny_events_t *event, uint8_t bits, uint8_t clear, uint32_t timeout)
Waits until any of specified bits is set or timeout.
Definition: cpp_hal.inl:61
void tiny_log_level(uint8_t level)
Sets logging level if tiny library is compiled with logs.
Definition: tiny_types.c:59
uint8_t tiny_mutex_try_lock(tiny_mutex_t *mutex)
Attempts to lock cross-platform mutex.
Definition: cpp_hal.inl:45