1 // Copyright 2021 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
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