compare_set.c
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 17 static spinlock_t global_extram_lock = SPINLOCK_INITIALIZER; 18 19 void compare_and_set_extram(volatile uint32_t *addr, uint32_t compare, uint32_t *set) 20 { 21 uint32_t intlevel, old_value; 22 __asm__ __volatile__ ("rsil %0, " XTSTR(XCHAL_EXCM_LEVEL) "\n" 23 : "=r"(intlevel)); 24 25 spinlock_acquire(&global_extram_lock, SPINLOCK_WAIT_FOREVER); 26 27 old_value = *addr; 28 if (old_value == compare) { 29 *addr = *set; 30 } 31 32 spinlock_release(&global_extram_lock); 33 34 __asm__ __volatile__ ("memw \n" 35 "wsr %0, ps\n" 36 :: "r"(intlevel)); 37 38 *set = old_value; 39 } 40