periph_ctrl.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2015-2020 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 "freertos/FreeRTOS.h"
  15. #include "hal/clk_gate_ll.h"
  16. #include "driver/periph_ctrl.h"
  17. static portMUX_TYPE periph_spinlock = portMUX_INITIALIZER_UNLOCKED;
  18. static uint8_t ref_counts[PERIPH_MODULE_MAX] = {0};
  19. void periph_module_enable(periph_module_t periph)
  20. {
  21. assert(periph < PERIPH_MODULE_MAX);
  22. portENTER_CRITICAL_SAFE(&periph_spinlock);
  23. if (ref_counts[periph] == 0) {
  24. periph_ll_enable_clk_clear_rst(periph);
  25. }
  26. ref_counts[periph]++;
  27. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  28. }
  29. void periph_module_disable(periph_module_t periph)
  30. {
  31. assert(periph < PERIPH_MODULE_MAX);
  32. portENTER_CRITICAL_SAFE(&periph_spinlock);
  33. ref_counts[periph]--;
  34. if (ref_counts[periph] == 0) {
  35. periph_ll_disable_clk_set_rst(periph);
  36. }
  37. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  38. }
  39. void periph_module_reset(periph_module_t periph)
  40. {
  41. assert(periph < PERIPH_MODULE_MAX);
  42. portENTER_CRITICAL_SAFE(&periph_spinlock);
  43. periph_ll_reset(periph);
  44. portEXIT_CRITICAL_SAFE(&periph_spinlock);
  45. }