compare_set.c 1.2 KB

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