fsl_common.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2018 NXP
  4. * All rights reserved.
  5. *
  6. *
  7. * SPDX-License-Identifier: BSD-3-Clause
  8. */
  9. #include "fsl_common.h"
  10. #define SDK_MEM_MAGIC_NUMBER 12345U
  11. typedef struct _mem_align_control_block
  12. {
  13. uint16_t identifier; /*!< Identifier for the memory control block. */
  14. uint16_t offset; /*!< offset from aligned address to real address */
  15. } mem_align_cb_t;
  16. /* Component ID definition, used by tools. */
  17. #ifndef FSL_COMPONENT_ID
  18. #define FSL_COMPONENT_ID "platform.drivers.common"
  19. #endif
  20. #ifndef __GIC_PRIO_BITS
  21. #if defined(ENABLE_RAM_VECTOR_TABLE)
  22. uint32_t InstallIRQHandler(IRQn_Type irq, uint32_t irqHandler)
  23. {
  24. /* Addresses for VECTOR_TABLE and VECTOR_RAM come from the linker file */
  25. #if defined(__CC_ARM) || defined(__ARMCC_VERSION)
  26. extern uint32_t Image$$VECTOR_ROM$$Base[];
  27. extern uint32_t Image$$VECTOR_RAM$$Base[];
  28. extern uint32_t Image$$RW_m_data$$Base[];
  29. #define __VECTOR_TABLE Image$$VECTOR_ROM$$Base
  30. #define __VECTOR_RAM Image$$VECTOR_RAM$$Base
  31. #define __RAM_VECTOR_TABLE_SIZE (((uint32_t)Image$$RW_m_data$$Base - (uint32_t)Image$$VECTOR_RAM$$Base))
  32. #elif defined(__ICCARM__)
  33. extern uint32_t __RAM_VECTOR_TABLE_SIZE[];
  34. extern uint32_t __VECTOR_TABLE[];
  35. extern uint32_t __VECTOR_RAM[];
  36. #elif defined(__GNUC__)
  37. extern uint32_t __VECTOR_TABLE[];
  38. extern uint32_t __VECTOR_RAM[];
  39. extern uint32_t __RAM_VECTOR_TABLE_SIZE_BYTES[];
  40. uint32_t __RAM_VECTOR_TABLE_SIZE = (uint32_t)(__RAM_VECTOR_TABLE_SIZE_BYTES);
  41. #endif /* defined(__CC_ARM) || defined(__ARMCC_VERSION) */
  42. uint32_t n;
  43. uint32_t ret;
  44. uint32_t irqMaskValue;
  45. irqMaskValue = DisableGlobalIRQ();
  46. if (SCB->VTOR != (uint32_t)__VECTOR_RAM)
  47. {
  48. /* Copy the vector table from ROM to RAM */
  49. for (n = 0; n < ((uint32_t)__RAM_VECTOR_TABLE_SIZE) / sizeof(uint32_t); n++)
  50. {
  51. __VECTOR_RAM[n] = __VECTOR_TABLE[n];
  52. }
  53. /* Point the VTOR to the position of vector table */
  54. SCB->VTOR = (uint32_t)__VECTOR_RAM;
  55. }
  56. ret = __VECTOR_RAM[irq + 16];
  57. /* make sure the __VECTOR_RAM is noncachable */
  58. __VECTOR_RAM[irq + 16] = irqHandler;
  59. EnableGlobalIRQ(irqMaskValue);
  60. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  61. exception return operation might vector to incorrect interrupt */
  62. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  63. __DSB();
  64. #endif
  65. return ret;
  66. }
  67. #endif /* ENABLE_RAM_VECTOR_TABLE. */
  68. #endif /* __GIC_PRIO_BITS. */
  69. #if (defined(FSL_FEATURE_SOC_SYSCON_COUNT) && (FSL_FEATURE_SOC_SYSCON_COUNT > 0))
  70. #if !(defined(FSL_FEATURE_SYSCON_STARTER_DISCONTINUOUS) && FSL_FEATURE_SYSCON_STARTER_DISCONTINUOUS)
  71. void EnableDeepSleepIRQ(IRQn_Type interrupt)
  72. {
  73. uint32_t intNumber = (uint32_t)interrupt;
  74. uint32_t index = 0;
  75. while (intNumber >= 32u)
  76. {
  77. index++;
  78. intNumber -= 32u;
  79. }
  80. SYSCON->STARTERSET[index] = 1u << intNumber;
  81. EnableIRQ(interrupt); /* also enable interrupt at NVIC */
  82. }
  83. void DisableDeepSleepIRQ(IRQn_Type interrupt)
  84. {
  85. uint32_t intNumber = (uint32_t)interrupt;
  86. DisableIRQ(interrupt); /* also disable interrupt at NVIC */
  87. uint32_t index = 0;
  88. while (intNumber >= 32u)
  89. {
  90. index++;
  91. intNumber -= 32u;
  92. }
  93. SYSCON->STARTERCLR[index] = 1u << intNumber;
  94. }
  95. #endif /* FSL_FEATURE_SYSCON_STARTER_DISCONTINUOUS */
  96. #endif /* FSL_FEATURE_SOC_SYSCON_COUNT */
  97. void *SDK_Malloc(size_t size, size_t alignbytes)
  98. {
  99. mem_align_cb_t *p_cb = NULL;
  100. uint32_t alignedsize = SDK_SIZEALIGN(size, alignbytes) + alignbytes + sizeof(mem_align_cb_t);
  101. void *p_align_addr, *p_addr = malloc(alignedsize);
  102. if (!p_addr)
  103. {
  104. return NULL;
  105. }
  106. p_align_addr = (void *)SDK_SIZEALIGN((uint32_t)p_addr + sizeof(mem_align_cb_t), alignbytes);
  107. p_cb = (mem_align_cb_t *)((uint32_t)p_align_addr - 4);
  108. p_cb->identifier = SDK_MEM_MAGIC_NUMBER;
  109. p_cb->offset = (uint32_t)p_align_addr - (uint32_t)p_addr;
  110. return (void *)p_align_addr;
  111. }
  112. void SDK_Free(void *ptr)
  113. {
  114. mem_align_cb_t *p_cb = (mem_align_cb_t *)((uint32_t)ptr - 4);
  115. if (p_cb->identifier != SDK_MEM_MAGIC_NUMBER)
  116. {
  117. return;
  118. }
  119. free((void *)((uint32_t)ptr - p_cb->offset));
  120. }