pal_os_lock.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * MIT License
  3. *
  4. * Copyright (c) 2018 Infineon Technologies AG
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE
  23. *
  24. *
  25. * \file pal_os_lock.c
  26. *
  27. * \brief This file implements the platform abstraction layer APIs for os locks (e.g. semaphore).
  28. *
  29. * \addtogroup grPAL
  30. * @{
  31. */
  32. #include "optiga/pal/pal_os_lock.h"
  33. #include "nrf_atomic.h"
  34. #include "nrf_pwr_mgmt.h"
  35. /**
  36. * @brief PAL OS lock structure. Might be extended if needed
  37. */
  38. typedef struct pal_os_lock
  39. {
  40. nrf_atomic_flag_t lock;
  41. } pal_os_lock_t;
  42. static volatile pal_os_lock_t pal_os_lock = {.lock = 0};
  43. pal_status_t pal_os_lock_acquire(void)
  44. {
  45. // wait until previous value was false, this indicates the lock was free and we own it now.
  46. while(nrf_atomic_flag_set_fetch(&pal_os_lock.lock)) {
  47. nrf_pwr_mgmt_run();
  48. }
  49. return PAL_STATUS_SUCCESS;
  50. }
  51. void pal_os_lock_release(void)
  52. {
  53. (void)nrf_atomic_flag_clear(&pal_os_lock.lock);
  54. }
  55. /**
  56. * @}
  57. */