compare_set.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #if __XTENSA__ && SOC_SPIRAM_SUPPORTED
  18. static spinlock_t global_extram_lock = SPINLOCK_INITIALIZER;
  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. spinlock_acquire(&global_extram_lock, SPINLOCK_WAIT_FOREVER);
  25. old_value = *addr;
  26. if (old_value == compare) {
  27. *addr = *set;
  28. }
  29. spinlock_release(&global_extram_lock);
  30. __asm__ __volatile__ ("memw \n"
  31. "wsr %0, ps\n"
  32. :: "r"(intlevel));
  33. *set = old_value;
  34. }
  35. #else // __XTENSA__ && SOC_SPIRAM_SUPPORTED
  36. void compare_and_set_extram(volatile uint32_t *addr, uint32_t compare, uint32_t *set)
  37. {
  38. compare_and_set_native(addr, compare, set);
  39. }
  40. #endif // endif