cyhal_system.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /***************************************************************************//**
  2. * \file cyhal_system.c
  3. *
  4. * \brief
  5. * Provides a high level interface for interacting with the Infineon power
  6. * management and system clock configuration. This interface abstracts out the
  7. * chip specific details. If any chip specific functionality is necessary, or
  8. * performance is critical the low level functions can be used directly.
  9. *
  10. ********************************************************************************
  11. * \copyright
  12. * Copyright 2018-2022 Cypress Semiconductor Corporation (an Infineon company) or
  13. * an affiliate of Cypress Semiconductor Corporation
  14. *
  15. * SPDX-License-Identifier: Apache-2.0
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License");
  18. * you may not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS,
  25. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. *******************************************************************************/
  29. #include "cyhal_system.h"
  30. #if (defined(COMPONENT_CAT1A) || defined(COMPONENT_CAT1B) || defined(COMPONENT_CAT1C) || defined(COMPONENT_CAT1D))
  31. #include "cy_syslib.h"
  32. #endif
  33. #if defined(CY_RTOS_AWARE) || defined(COMPONENT_RTOS_AWARE)
  34. #include "cyabs_rtos.h"
  35. #endif
  36. #if (CYHAL_DRIVER_AVAILABLE_SYSTEM)
  37. #if defined(__cplusplus)
  38. extern "C"
  39. {
  40. #endif
  41. #define _CYHAL_SYSTEM_HZ_PER_MHZ 1000000
  42. #if (defined(COMPONENT_CAT1A) || defined(COMPONENT_CAT1B) || defined(COMPONENT_CAT1C) || defined(COMPONENT_CAT1D)) &&\
  43. !defined(CYHAL_DISABLE_WEAK_FUNC_IMPL)
  44. /* Overrides weak implemenation for Cy_SysLib_Rtos_Delay to provide a way
  45. * to call into a RTOS if so configured. This function is only available
  46. * in mtb-pdl-cat1 version 2.2.0 or later.
  47. */
  48. void Cy_SysLib_Rtos_Delay(uint32_t milliseconds)
  49. {
  50. cy_rslt_t result = cyhal_system_delay_ms(milliseconds);
  51. CY_ASSERT(CY_RSLT_SUCCESS == result);
  52. CY_UNUSED_PARAMETER(result);
  53. }
  54. #endif
  55. cy_rslt_t cyhal_system_delay_ms(uint32_t milliseconds)
  56. {
  57. #if defined(CY_RTOS_AWARE) || defined(COMPONENT_RTOS_AWARE)
  58. // The RTOS is configured to round down, while this API is intended to wait at least the
  59. // requested time. Add 1 to the requested time to make it behave the same.
  60. return cy_rtos_delay_milliseconds(milliseconds + 1);
  61. #else
  62. Cy_SysLib_Delay(milliseconds);
  63. return CY_RSLT_SUCCESS;
  64. #endif
  65. }
  66. cyhal_reset_reason_t cyhal_system_get_reset_reason(void)
  67. {
  68. uint32_t pdl_reason = Cy_SysLib_GetResetReason();
  69. cyhal_reset_reason_t reason = CYHAL_SYSTEM_RESET_NONE;
  70. if (CY_SYSLIB_RESET_SOFT & pdl_reason)
  71. reason |= CYHAL_SYSTEM_RESET_SOFT;
  72. if (CY_SYSLIB_RESET_HWWDT & pdl_reason)
  73. reason |= CYHAL_SYSTEM_RESET_WDT;
  74. #if defined(CY_IP_S8SRSSLT)
  75. if (CY_SYSLIB_PROT_FAULT & pdl_reason)
  76. reason |= CYHAL_SYSTEM_RESET_PROTECTION;
  77. #endif
  78. #if defined(CY_IP_MXS40SRSS) || defined(CY_IP_MXS40SSRSS) || defined(CY_IP_MXS22SSRSS)
  79. if (CY_SYSLIB_RESET_ACT_FAULT & pdl_reason)
  80. reason |= CYHAL_SYSTEM_RESET_ACTIVE_FAULT;
  81. if (CY_SYSLIB_RESET_DPSLP_FAULT & pdl_reason)
  82. reason |= CYHAL_SYSTEM_RESET_DEEPSLEEP_FAULT;
  83. if (CY_SYSLIB_RESET_HIB_WAKEUP & pdl_reason)
  84. reason |= CYHAL_SYSTEM_RESET_HIB_WAKEUP;
  85. if (CY_SYSLIB_RESET_CSV_LOSS_WAKEUP & pdl_reason)
  86. reason |= CYHAL_SYSTEM_RESET_SYS_CLK_ERR;
  87. if (CY_SYSLIB_RESET_CSV_ERROR_WAKEUP & pdl_reason)
  88. reason |= CYHAL_SYSTEM_RESET_SYS_CLK_ERR;
  89. if ((CY_SYSLIB_RESET_SWWDT0 | CY_SYSLIB_RESET_SWWDT1 | CY_SYSLIB_RESET_SWWDT2 | CY_SYSLIB_RESET_SWWDT3) & pdl_reason)
  90. reason |= CYHAL_SYSTEM_RESET_WDT;
  91. #endif
  92. #if (SRSS_WCOCSV_PRESENT != 0U)
  93. if (CY_SYSLIB_RESET_CSV_WCO_LOSS & pdl_reason)
  94. reason |= CYHAL_SYSTEM_RESET_WCO_ERR;
  95. #endif
  96. #if (SRSS_MASK_HFCSV != 0U)
  97. if ((CY_SYSLIB_RESET_HFCLK_LOSS | CY_SYSLIB_RESET_HFCLK_ERR) & pdl_reason)
  98. reason |= CYHAL_SYSTEM_RESET_SYS_CLK_ERR;
  99. #endif
  100. return reason;
  101. }
  102. cy_rslt_t cyhal_system_set_isr(int32_t irq_num, int32_t irq_src, uint8_t priority, cyhal_irq_handler handler)
  103. {
  104. CY_UNUSED_PARAMETER(irq_src); // Not used by most configurations as there is a 1:1 mapping from irq_src to irq_num
  105. cy_stc_sysint_t cfg =
  106. {
  107. #if defined (CY_IP_M7CPUSS)
  108. .intrSrc = (uint32_t)irq_src | ((uint32_t)irq_num << 16),
  109. #else
  110. .intrSrc = (IRQn_Type)irq_num,
  111. #endif
  112. .intrPriority = priority,
  113. #if (CY_CPU_CORTEX_M0P) && defined (CY_IP_M4CPUSS)
  114. .cm0pSrc = (cy_en_intr_t)irq_src,
  115. #endif
  116. };
  117. return Cy_SysInt_Init(&cfg, (cy_israddress)handler);
  118. }
  119. #if defined(__cplusplus)
  120. }
  121. #endif
  122. #endif /* CYHAL_DRIVER_AVAILABLE_SYSTEM */