1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include "soc/compare_set.h"
7 #include "soc/spinlock.h"
8 #include "soc/soc_caps.h"
9
10 #if __XTENSA__ && SOC_SPIRAM_SUPPORTED
11
12 static spinlock_t global_extram_lock = SPINLOCK_INITIALIZER;
13
compare_and_set_extram(volatile uint32_t * addr,uint32_t compare,uint32_t * set)14 void compare_and_set_extram(volatile uint32_t *addr, uint32_t compare, uint32_t *set)
15 {
16 uint32_t intlevel, old_value;
17 __asm__ __volatile__ ("rsil %0, " XTSTR(XCHAL_EXCM_LEVEL) "\n"
18 : "=r"(intlevel));
19
20 spinlock_acquire(&global_extram_lock, SPINLOCK_WAIT_FOREVER);
21
22 old_value = *addr;
23 if (old_value == compare) {
24 *addr = *set;
25 }
26
27 spinlock_release(&global_extram_lock);
28
29 __asm__ __volatile__ ("memw \n"
30 "wsr %0, ps\n"
31 :: "r"(intlevel));
32
33 *set = old_value;
34 }
35 #else // __XTENSA__ && SOC_SPIRAM_SUPPORTED
36
compare_and_set_extram(volatile uint32_t * addr,uint32_t compare,uint32_t * set)37 void compare_and_set_extram(volatile uint32_t *addr, uint32_t compare, uint32_t *set)
38 {
39 compare_and_set_native(addr, compare, set);
40 }
41 #endif // endif
42