1 // Copyright 2015-2019 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 #include "soc/compare_set.h"
15 #include "soc/spinlock.h"
16 #include "soc/soc_caps.h"
17 
18 #if __XTENSA__ && SOC_SPIRAM_SUPPORTED
19 
20 static spinlock_t global_extram_lock = SPINLOCK_INITIALIZER;
21 
compare_and_set_extram(volatile uint32_t * addr,uint32_t compare,uint32_t * set)22 void compare_and_set_extram(volatile uint32_t *addr, uint32_t compare, uint32_t *set)
23 {
24     uint32_t intlevel, old_value;
25     __asm__ __volatile__ ("rsil %0, " XTSTR(XCHAL_EXCM_LEVEL) "\n"
26                           : "=r"(intlevel));
27 
28 	spinlock_acquire(&global_extram_lock, SPINLOCK_WAIT_FOREVER);
29 
30     old_value = *addr;
31     if (old_value == compare) {
32         *addr = *set;
33     }
34 
35 	spinlock_release(&global_extram_lock);
36 
37 	__asm__ __volatile__ ("memw \n"
38 						"wsr %0, ps\n"
39 						:: "r"(intlevel));
40 
41     *set = old_value;
42 }
43 #else // __XTENSA__ && SOC_SPIRAM_SUPPORTED
44 
compare_and_set_extram(volatile uint32_t * addr,uint32_t compare,uint32_t * set)45 void compare_and_set_extram(volatile uint32_t *addr, uint32_t compare, uint32_t *set)
46 {
47     compare_and_set_native(addr, compare, set);
48 }
49 #endif // endif
50