2021-04-05 16:32:58 -04:00
|
|
|
/*
|
2020-01-14 23:30:39 -05:00
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Ha Thach (tinyusb.org)
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* This file is part of the TinyUSB stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _TUSB_COMMON_H_
|
|
|
|
#define _TUSB_COMMON_H_
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Macros Helper
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
#define TU_ARRAY_SIZE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) )
|
|
|
|
#define TU_MIN(_x, _y) ( ( (_x) < (_y) ) ? (_x) : (_y) )
|
|
|
|
#define TU_MAX(_x, _y) ( ( (_x) > (_y) ) ? (_x) : (_y) )
|
2024-01-15 18:42:39 +07:00
|
|
|
#define TU_DIV_CEIL(n, d) (((n) + (d) - 1) / (d))
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2022-03-04 19:26:54 +07:00
|
|
|
#define TU_U16(_high, _low) ((uint16_t) (((_high) << 8) | (_low)))
|
2021-10-24 13:11:21 +07:00
|
|
|
#define TU_U16_HIGH(_u16) ((uint8_t) (((_u16) >> 8) & 0x00ff))
|
|
|
|
#define TU_U16_LOW(_u16) ((uint8_t) ((_u16) & 0x00ff))
|
|
|
|
#define U16_TO_U8S_BE(_u16) TU_U16_HIGH(_u16), TU_U16_LOW(_u16)
|
|
|
|
#define U16_TO_U8S_LE(_u16) TU_U16_LOW(_u16), TU_U16_HIGH(_u16)
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-10-24 13:11:21 +07:00
|
|
|
#define TU_U32_BYTE3(_u32) ((uint8_t) ((((uint32_t) _u32) >> 24) & 0x000000ff)) // MSB
|
|
|
|
#define TU_U32_BYTE2(_u32) ((uint8_t) ((((uint32_t) _u32) >> 16) & 0x000000ff))
|
|
|
|
#define TU_U32_BYTE1(_u32) ((uint8_t) ((((uint32_t) _u32) >> 8) & 0x000000ff))
|
|
|
|
#define TU_U32_BYTE0(_u32) ((uint8_t) (((uint32_t) _u32) & 0x000000ff)) // LSB
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-10-24 13:11:21 +07:00
|
|
|
#define U32_TO_U8S_BE(_u32) TU_U32_BYTE3(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE0(_u32)
|
|
|
|
#define U32_TO_U8S_LE(_u32) TU_U32_BYTE0(_u32), TU_U32_BYTE1(_u32), TU_U32_BYTE2(_u32), TU_U32_BYTE3(_u32)
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-06-09 12:10:44 +07:00
|
|
|
#define TU_BIT(n) (1UL << (n))
|
2023-07-21 19:06:36 +07:00
|
|
|
|
|
|
|
// Generate a mask with bit from high (31) to low (0) set, e.g TU_GENMASK(3, 0) = 0b1111
|
2021-10-24 13:11:21 +07:00
|
|
|
#define TU_GENMASK(h, l) ( (UINT32_MAX << (l)) & (UINT32_MAX >> (31 - (h))) )
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Includes
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
|
|
// Standard Headers
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2024-04-17 20:06:13 +07:00
|
|
|
#include <inttypes.h>
|
2020-01-14 23:30:39 -05:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
// Tinyusb Common Headers
|
|
|
|
#include "tusb_option.h"
|
|
|
|
#include "tusb_compiler.h"
|
|
|
|
#include "tusb_verify.h"
|
|
|
|
#include "tusb_types.h"
|
2022-03-09 16:42:51 +07:00
|
|
|
#include "tusb_debug.h"
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2023-02-22 22:14:50 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Optional API implemented by application if needed
|
2024-10-11 12:58:18 +07:00
|
|
|
// TODO move to a more obvious place/file
|
2023-02-22 22:14:50 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
|
|
// flush data cache
|
|
|
|
TU_ATTR_WEAK extern void tusb_app_dcache_flush(uintptr_t addr, uint32_t data_size);
|
|
|
|
|
|
|
|
// invalidate data cache
|
|
|
|
TU_ATTR_WEAK extern void tusb_app_dcache_invalidate(uintptr_t addr, uint32_t data_size);
|
|
|
|
|
|
|
|
// Optional physical <-> virtual address translation
|
|
|
|
TU_ATTR_WEAK extern void* tusb_app_virt_to_phys(void *virt_addr);
|
|
|
|
TU_ATTR_WEAK extern void* tusb_app_phys_to_virt(void *phys_addr);
|
|
|
|
|
2021-08-20 18:01:10 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Internal Inline Functions
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
2021-04-06 19:32:01 +07:00
|
|
|
//------------- Mem -------------//
|
2020-01-14 23:30:39 -05:00
|
|
|
#define tu_memclr(buffer, size) memset((buffer), 0, (size))
|
|
|
|
#define tu_varclr(_var) tu_memclr(_var, sizeof(*(_var)))
|
|
|
|
|
2023-01-13 13:37:55 -08:00
|
|
|
// This is a backport of memset_s from c11
|
2023-07-24 17:54:24 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline int tu_memset_s(void *dest, size_t destsz, int ch, size_t count) {
|
2023-02-27 09:11:35 +07:00
|
|
|
// TODO may check if desst and src is not NULL
|
2023-07-24 17:54:24 +07:00
|
|
|
if ( count > destsz ) {
|
2023-02-27 09:11:35 +07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset(dest, ch, count);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-01-13 13:37:55 -08:00
|
|
|
|
|
|
|
// This is a backport of memcpy_s from c11
|
2023-07-24 17:54:24 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline int tu_memcpy_s(void *dest, size_t destsz, const void *src, size_t count) {
|
2023-02-27 09:11:35 +07:00
|
|
|
// TODO may check if desst and src is not NULL
|
2023-07-24 17:54:24 +07:00
|
|
|
if ( count > destsz ) {
|
2023-02-27 09:11:35 +07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memcpy(dest, src, count);
|
|
|
|
return 0;
|
|
|
|
}
|
2023-01-13 13:37:55 -08:00
|
|
|
|
2025-03-06 10:26:45 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline bool tu_mem_is_zero(const void *buffer, size_t size) {
|
|
|
|
const uint8_t* buf8 = (const uint8_t*) buffer;
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
if (buf8[i] != 0) { return false; }
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline bool tu_mem_is_ff(const void *buffer, size_t size) {
|
|
|
|
const uint8_t* buf8 = (const uint8_t*) buffer;
|
|
|
|
for (size_t i = 0; i < size; i++) {
|
|
|
|
if (buf8[i] != 0xff) { return false; }
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-13 13:37:55 -08:00
|
|
|
|
2021-04-06 19:32:01 +07:00
|
|
|
//------------- Bytes -------------//
|
2023-09-06 17:11:35 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_u32(uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0) {
|
2024-10-25 00:20:34 +07:00
|
|
|
return (((uint32_t)b3) << 24) | (((uint32_t)b2) << 16) | (((uint32_t)b1) << 8) | b0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_u32_from_u16(uint16_t high, uint16_t low) {
|
|
|
|
return (((uint32_t)high) << 16) | low;
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
|
|
|
|
2023-09-06 17:11:35 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u16(uint8_t high, uint8_t low) {
|
2024-10-25 00:20:34 +07:00
|
|
|
return (uint16_t)((((uint16_t)high) << 8) | low);
|
2020-01-14 23:30:39 -05:00
|
|
|
}
|
|
|
|
|
2021-09-18 13:35:26 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte3(uint32_t ui32) { return TU_U32_BYTE3(ui32); }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte2(uint32_t ui32) { return TU_U32_BYTE2(ui32); }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte1(uint32_t ui32) { return TU_U32_BYTE1(ui32); }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u32_byte0(uint32_t ui32) { return TU_U32_BYTE0(ui32); }
|
2021-04-26 17:42:49 +07:00
|
|
|
|
2021-10-29 16:08:19 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u32_high16(uint32_t ui32) { return (uint16_t) (ui32 >> 16); }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_u32_low16 (uint32_t ui32) { return (uint16_t) (ui32 & 0x0000ffffu); }
|
|
|
|
|
2021-09-18 13:35:26 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u16_high(uint16_t ui16) { return TU_U16_HIGH(ui16); }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_u16_low (uint16_t ui16) { return TU_U16_LOW(ui16); }
|
2021-04-06 19:32:01 +07:00
|
|
|
|
|
|
|
//------------- Bits -------------//
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_bit_set (uint32_t value, uint8_t pos) { return value | TU_BIT(pos); }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_bit_clear(uint32_t value, uint8_t pos) { return value & (~TU_BIT(pos)); }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline bool tu_bit_test (uint32_t value, uint8_t pos) { return (value & TU_BIT(pos)) ? true : false; }
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-04-06 19:32:01 +07:00
|
|
|
//------------- Min -------------//
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_min8 (uint8_t x, uint8_t y ) { return (x < y) ? x : y; }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_min16 (uint16_t x, uint16_t y) { return (x < y) ? x : y; }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_min32 (uint32_t x, uint32_t y) { return (x < y) ? x : y; }
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-04-06 19:32:01 +07:00
|
|
|
//------------- Max -------------//
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_max8 (uint8_t x, uint8_t y ) { return (x > y) ? x : y; }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_max16 (uint16_t x, uint16_t y) { return (x > y) ? x : y; }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_max32 (uint32_t x, uint32_t y) { return (x > y) ? x : y; }
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2021-04-06 19:32:01 +07:00
|
|
|
//------------- Align -------------//
|
2023-08-08 21:59:36 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align(uint32_t value, uint32_t alignment) {
|
2020-01-14 23:30:39 -05:00
|
|
|
return value & ((uint32_t) ~(alignment-1));
|
|
|
|
}
|
|
|
|
|
2023-08-08 21:59:36 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align4 (uint32_t value) { return (value & 0xFFFFFFFCUL); }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align8 (uint32_t value) { return (value & 0xFFFFFFF8UL); }
|
2021-04-06 19:32:01 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align16 (uint32_t value) { return (value & 0xFFFFFFF0UL); }
|
2021-04-26 14:43:58 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align32 (uint32_t value) { return (value & 0xFFFFFFE0UL); }
|
2021-04-06 19:32:01 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_align4k (uint32_t value) { return (value & 0xFFFFF000UL); }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_offset4k(uint32_t value) { return (value & 0xFFFUL); }
|
2020-01-14 23:30:39 -05:00
|
|
|
|
2023-07-24 17:54:24 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline bool tu_is_aligned32(uint32_t value) { return (value & 0x1FUL) == 0; }
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline bool tu_is_aligned64(uint64_t value) { return (value & 0x3FUL) == 0; }
|
|
|
|
|
2020-01-14 23:30:39 -05:00
|
|
|
//------------- Mathematics -------------//
|
2024-11-21 19:18:52 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_div_ceil(uint32_t v, uint32_t d) { return TU_DIV_CEIL(v, d); }
|
2024-11-28 15:56:47 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_round_up(uint32_t v, uint32_t f) { return tu_div_ceil(v, f) * f; }
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
// log2 of a value is its MSB's position
|
|
|
|
// TODO use clz TODO remove
|
2025-03-06 10:26:45 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_log2(uint32_t value) {
|
2020-01-14 23:30:39 -05:00
|
|
|
uint8_t result = 0;
|
2021-04-06 19:32:01 +07:00
|
|
|
while (value >>= 1) { result++; }
|
2020-01-14 23:30:39 -05:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-03-19 13:37:54 +01:00
|
|
|
//static inline uint8_t tu_log2(uint32_t value)
|
|
|
|
//{
|
|
|
|
// return sizeof(uint32_t) * CHAR_BIT - __builtin_clz(x) - 1;
|
|
|
|
//}
|
|
|
|
|
2025-03-06 10:26:45 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline bool tu_is_power_of_two(uint32_t value) {
|
2022-03-19 13:37:54 +01:00
|
|
|
return (value != 0) && ((value & (value - 1)) == 0);
|
|
|
|
}
|
|
|
|
|
2021-04-06 19:32:01 +07:00
|
|
|
//------------- Unaligned Access -------------//
|
|
|
|
#if TUP_ARCH_STRICT_ALIGN
|
|
|
|
|
2021-04-26 17:42:49 +07:00
|
|
|
// Rely on compiler to generate correct code for unaligned access
|
2021-04-06 19:32:01 +07:00
|
|
|
typedef struct { uint16_t val; } TU_ATTR_PACKED tu_unaligned_uint16_t;
|
|
|
|
typedef struct { uint32_t val; } TU_ATTR_PACKED tu_unaligned_uint32_t;
|
|
|
|
|
2025-03-06 10:26:45 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void *mem) {
|
|
|
|
tu_unaligned_uint32_t const *ua32 = (tu_unaligned_uint32_t const *) mem;
|
2021-04-06 19:32:01 +07:00
|
|
|
return ua32->val;
|
|
|
|
}
|
|
|
|
|
2025-03-06 10:26:45 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void *mem, uint32_t value) {
|
|
|
|
tu_unaligned_uint32_t *ua32 = (tu_unaligned_uint32_t *) mem;
|
2021-04-06 19:32:01 +07:00
|
|
|
ua32->val = value;
|
|
|
|
}
|
|
|
|
|
2025-03-06 10:26:45 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void *mem) {
|
|
|
|
tu_unaligned_uint16_t const *ua16 = (tu_unaligned_uint16_t const *) mem;
|
2021-04-06 19:32:01 +07:00
|
|
|
return ua16->val;
|
|
|
|
}
|
|
|
|
|
2025-03-06 10:26:45 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void *mem, uint16_t value) {
|
|
|
|
tu_unaligned_uint16_t *ua16 = (tu_unaligned_uint16_t *) mem;
|
2021-04-06 19:32:01 +07:00
|
|
|
ua16->val = value;
|
|
|
|
}
|
|
|
|
|
2021-04-26 17:42:49 +07:00
|
|
|
#elif TUP_MCU_STRICT_ALIGN
|
|
|
|
|
|
|
|
// MCU such as LPC_IP3511 Highspeed cannot access unaligned memory on USB_RAM although it is ARM M4.
|
|
|
|
// We have to manually pick up bytes since tu_unaligned_uint32_t will still generate unaligned code
|
|
|
|
// NOTE: volatile cast to memory to prevent compiler to optimize and generate unaligned code
|
|
|
|
// TODO Big Endian may need minor changes
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void* mem)
|
|
|
|
{
|
|
|
|
volatile uint8_t const* buf8 = (uint8_t const*) mem;
|
|
|
|
return tu_u32(buf8[3], buf8[2], buf8[1], buf8[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void* mem, uint32_t value)
|
|
|
|
{
|
|
|
|
volatile uint8_t* buf8 = (uint8_t*) mem;
|
|
|
|
buf8[0] = tu_u32_byte0(value);
|
|
|
|
buf8[1] = tu_u32_byte1(value);
|
|
|
|
buf8[2] = tu_u32_byte2(value);
|
|
|
|
buf8[3] = tu_u32_byte3(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void* mem)
|
|
|
|
{
|
|
|
|
volatile uint8_t const* buf8 = (uint8_t const*) mem;
|
|
|
|
return tu_u16(buf8[1], buf8[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void* mem, uint16_t value)
|
|
|
|
{
|
|
|
|
volatile uint8_t* buf8 = (uint8_t*) mem;
|
|
|
|
buf8[0] = tu_u16_low(value);
|
|
|
|
buf8[1] = tu_u16_high(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-06 19:32:01 +07:00
|
|
|
#else
|
|
|
|
|
2021-04-26 17:42:49 +07:00
|
|
|
// MCU that could access unaligned memory natively
|
2023-07-07 12:27:18 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void *mem) {
|
|
|
|
return *((uint32_t const *) mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void *mem) {
|
|
|
|
return *((uint16_t const *) mem);
|
|
|
|
}
|
2021-04-06 19:32:01 +07:00
|
|
|
|
2023-07-07 12:27:18 +07:00
|
|
|
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write32(void *mem, uint32_t value) {
|
|
|
|
*((uint32_t *) mem) = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline void tu_unaligned_write16(void *mem, uint16_t value) {
|
|
|
|
*((uint16_t *) mem) = value;
|
|
|
|
}
|
2021-04-06 19:32:01 +07:00
|
|
|
|
|
|
|
#endif
|
2020-01-14 23:30:39 -05:00
|
|
|
|
|
|
|
// To be removed
|
|
|
|
//------------- Binary constant -------------//
|
|
|
|
#if defined(__GNUC__) && !defined(__CC_ARM)
|
|
|
|
|
|
|
|
#define TU_BIN8(x) ((uint8_t) (0b##x))
|
|
|
|
#define TU_BIN16(b1, b2) ((uint16_t) (0b##b1##b2))
|
|
|
|
#define TU_BIN32(b1, b2, b3, b4) ((uint32_t) (0b##b1##b2##b3##b4))
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
// internal macro of B8, B16, B32
|
|
|
|
#define _B8__(x) (((x&0x0000000FUL)?1:0) \
|
|
|
|
+((x&0x000000F0UL)?2:0) \
|
|
|
|
+((x&0x00000F00UL)?4:0) \
|
|
|
|
+((x&0x0000F000UL)?8:0) \
|
|
|
|
+((x&0x000F0000UL)?16:0) \
|
|
|
|
+((x&0x00F00000UL)?32:0) \
|
|
|
|
+((x&0x0F000000UL)?64:0) \
|
|
|
|
+((x&0xF0000000UL)?128:0))
|
|
|
|
|
|
|
|
#define TU_BIN8(d) ((uint8_t) _B8__(0x##d##UL))
|
|
|
|
#define TU_BIN16(dmsb,dlsb) (((uint16_t)TU_BIN8(dmsb)<<8) + TU_BIN8(dlsb))
|
|
|
|
#define TU_BIN32(dmsb,db2,db3,dlsb) \
|
|
|
|
(((uint32_t)TU_BIN8(dmsb)<<24) \
|
|
|
|
+ ((uint32_t)TU_BIN8(db2)<<16) \
|
|
|
|
+ ((uint32_t)TU_BIN8(db3)<<8) \
|
|
|
|
+ TU_BIN8(dlsb))
|
|
|
|
#endif
|
|
|
|
|
2025-08-02 11:23:15 +07:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Descriptor helper
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
|
|
// return next descriptor
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t const * tu_desc_next(void const* desc) {
|
|
|
|
uint8_t const* desc8 = (uint8_t const*) desc;
|
|
|
|
return desc8 + desc8[DESC_OFFSET_LEN];
|
|
|
|
}
|
|
|
|
|
|
|
|
// get descriptor length
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_len(void const* desc) {
|
|
|
|
return ((uint8_t const*) desc)[DESC_OFFSET_LEN];
|
|
|
|
}
|
|
|
|
|
|
|
|
// get descriptor type
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_type(void const* desc) {
|
|
|
|
return ((uint8_t const*) desc)[DESC_OFFSET_TYPE];
|
|
|
|
}
|
|
|
|
|
|
|
|
// get descriptor subtype
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_subtype(void const* desc) {
|
|
|
|
return ((uint8_t const*) desc)[DESC_OFFSET_SUBTYPE];
|
|
|
|
}
|
|
|
|
|
|
|
|
TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_in_bounds(uint8_t const* p_desc, uint8_t const* desc_end) {
|
|
|
|
return (p_desc < desc_end) && (tu_desc_next(p_desc) <= desc_end);
|
|
|
|
}
|
|
|
|
|
|
|
|
// find descriptor that match byte1 (type)
|
|
|
|
uint8_t const * tu_desc_find(uint8_t const* desc, uint8_t const* end, uint8_t byte1);
|
|
|
|
|
|
|
|
// find descriptor that match byte1 (type) and byte2
|
|
|
|
uint8_t const * tu_desc_find2(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2);
|
|
|
|
|
|
|
|
// find descriptor that match byte1 (type) and byte2
|
|
|
|
uint8_t const * tu_desc_find3(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2, uint8_t byte3);
|
|
|
|
|
2020-01-14 23:30:39 -05:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* _TUSB_COMMON_H_ */
|