1 /* 2 * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #pragma once 7 8 #include <string.h> 9 10 #undef asm 11 #define asm __asm__ 12 13 #undef typeof 14 #define typeof __typeof__ 15 16 #define HAL_SWAP16(d) __builtin_bswap16((d)) 17 #define HAL_SWAP32(d) __builtin_bswap32((d)) 18 #define HAL_SWAP64(d) __builtin_bswap64((d)) 19 20 /** @cond */ //Doxy command to hide preprocessor definitions from docs */ 21 22 /** 23 * @brief Macro to force a 32-bit read, modify, then write on a peripheral register 24 * 25 * Due to a GCC bug, the compiler may still try to optimize read/writes to peripheral register fields by using 8/16 bit 26 * access, even if they are marked volatile (i.e., -fstrict-volatile-bitfields has no effect). 27 * 28 * For ESP chips, the peripheral bus only allows 32-bit read/writes. The following macro works around the compiler issue 29 * by forcing a 32-bit read/modify/write. 30 * 31 * @note This macro should only be called on register fields of xxx_struct.h type headers, as it depends on the presence 32 * of a 'val' field of the register union. 33 * @note Current implementation reads into a uint32_t instead of copy base_reg direclty to temp_reg. The reason being 34 * that C++ does not create a copy constructor for volatile structs. 35 */ 36 #define HAL_FORCE_MODIFY_U32_REG_FIELD(base_reg, reg_field, field_val) \ 37 { \ 38 uint32_t temp_val = base_reg.val; \ 39 typeof(base_reg) temp_reg; \ 40 temp_reg.val = temp_val; \ 41 temp_reg.reg_field = (field_val); \ 42 (base_reg).val = temp_reg.val; \ 43 } 44 45 /** 46 * @brief Macro to force a 32-bit read on a peripheral register 47 * 48 * @note This macro should only be called on register fields of xxx_struct.h type headers. See description above for 49 * more details. 50 * @note Current implementation reads into a uint32_t. See description above for more details. 51 */ 52 #define HAL_FORCE_READ_U32_REG_FIELD(base_reg, reg_field) ({ \ 53 uint32_t temp_val = base_reg.val; \ 54 typeof(base_reg) temp_reg; \ 55 temp_reg.val = temp_val; \ 56 temp_reg.reg_field; \ 57 }) 58 59 /** @endcond */ 60 61 /** 62 * @brief Copy data from memory array to another memory 63 * 64 * This helps bypass the -Warray-bounds, -Wstringop-overread and -Wstringop-overflow bugs. 65 * 66 * @param dst_mem Pointer to the destination of data 67 * @param src_mem Pointer to the source of data to be copied 68 * @param len The number of bytes to be copied 69 * @return a pointer to destination 70 */ 71 #define hal_memcpy(dst_mem, src_mem, len) (__extension__({memcpy(dst_mem, src_mem, len);})) 72 73 /** 74 * @brief Sets the first num bytes of the block of memory pointed by ptr to the specified value 75 * 76 * This helps bypass the -Warray-bounds, -Wstringop-overread and -Wstringop-overflow bugs. 77 * 78 * @param dst_reg Pointer to the block of memory to fill 79 * @param value The value to be set. 80 * @param len The number of bytes to be copied 81 * @return a pointer to the memory area 82 */ 83 #define hal_memset(dst_mem, value, len) (__extension__({memset(dst_mem, value, len);})) 84