cpu_util.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 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 "esp_attr.h"
  15. #include "soc/cpu.h"
  16. #include "soc/soc.h"
  17. #include "soc/rtc_periph.h"
  18. #include "sdkconfig.h"
  19. #include "hal/cpu_hal.h"
  20. #include "esp_debug_helpers.h"
  21. #include "hal/cpu_types.h"
  22. #include "hal/mpu_hal.h"
  23. #include "hal/soc_hal.h"
  24. #include "soc/soc_caps.h"
  25. #include "sdkconfig.h"
  26. void IRAM_ATTR esp_cpu_stall(int cpu_id)
  27. {
  28. #if SOC_CPU_CORES_NUM > 1
  29. soc_hal_stall_core(cpu_id);
  30. #endif
  31. }
  32. void IRAM_ATTR esp_cpu_unstall(int cpu_id)
  33. {
  34. #if SOC_CPU_CORES_NUM > 1
  35. soc_hal_unstall_core(cpu_id);
  36. #endif
  37. }
  38. void IRAM_ATTR esp_cpu_reset(int cpu_id)
  39. {
  40. soc_hal_reset_core(cpu_id);
  41. }
  42. esp_err_t IRAM_ATTR esp_set_watchpoint(int no, void *adr, int size, int flags)
  43. {
  44. watchpoint_trigger_t trigger;
  45. switch (flags)
  46. {
  47. case ESP_WATCHPOINT_LOAD:
  48. trigger = WATCHPOINT_TRIGGER_ON_RO;
  49. break;
  50. case ESP_WATCHPOINT_STORE:
  51. trigger = WATCHPOINT_TRIGGER_ON_WO;
  52. break;
  53. case ESP_WATCHPOINT_ACCESS:
  54. trigger = WATCHPOINT_TRIGGER_ON_RW;
  55. break;
  56. default:
  57. return ESP_ERR_INVALID_ARG;
  58. }
  59. cpu_hal_set_watchpoint(no, adr, size, trigger);
  60. return ESP_OK;
  61. }
  62. void IRAM_ATTR esp_clear_watchpoint(int no)
  63. {
  64. cpu_hal_clear_watchpoint(no);
  65. }
  66. bool IRAM_ATTR esp_cpu_in_ocd_debug_mode(void)
  67. {
  68. #if CONFIG_ESP32_DEBUG_OCDAWARE || \
  69. CONFIG_ESP32S2_DEBUG_OCDAWARE || \
  70. CONFIG_ESP32S3_DEBUG_OCDAWARE || \
  71. CONFIG_ESP32C3_DEBUG_OCDAWARE
  72. return cpu_ll_is_debugger_attached();
  73. #else
  74. return false; // Always return false if "OCD aware" is disabled
  75. #endif
  76. }
  77. void IRAM_ATTR esp_set_breakpoint_if_jtag(void *fn)
  78. {
  79. if (esp_cpu_in_ocd_debug_mode()) {
  80. cpu_hal_set_breakpoint(0, fn);
  81. }
  82. }
  83. #if __XTENSA__
  84. void esp_cpu_configure_region_protection(void)
  85. {
  86. /* Note: currently this is configured the same on all Xtensa targets
  87. *
  88. * Both chips have the address space divided into 8 regions, 512MB each.
  89. */
  90. const int illegal_regions[] = {0, 4, 5, 6, 7}; // 0x00000000, 0x80000000, 0xa0000000, 0xc0000000, 0xe0000000
  91. for (size_t i = 0; i < sizeof(illegal_regions) / sizeof(illegal_regions[0]); ++i) {
  92. mpu_hal_set_region_access(illegal_regions[i], MPU_REGION_ILLEGAL);
  93. }
  94. mpu_hal_set_region_access(1, MPU_REGION_RW); // 0x20000000
  95. }
  96. #endif