fsl_common.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016 NXP
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * o Redistributions of source code must retain the above copyright notice, this list
  10. * of conditions and the following disclaimer.
  11. *
  12. * o Redistributions in binary form must reproduce the above copyright notice, this
  13. * list of conditions and the following disclaimer in the documentation and/or
  14. * other materials provided with the distribution.
  15. *
  16. * o Neither the name of the copyright holder nor the names of its
  17. * contributors may be used to endorse or promote products derived from this
  18. * software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  24. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  27. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "fsl_common.h"
  32. #define SDK_MEM_MAGIC_NUMBER 12345U
  33. typedef struct _mem_align_control_block
  34. {
  35. uint16_t identifier; /*!< Identifier for the memory control block. */
  36. uint16_t offset; /*!< offset from aligned adress to real address */
  37. } mem_align_cb_t;
  38. #ifndef __GIC_PRIO_BITS
  39. #if defined(ENABLE_RAM_VECTOR_TABLE)
  40. uint32_t InstallIRQHandler(IRQn_Type irq, uint32_t irqHandler)
  41. {
  42. /* Addresses for VECTOR_TABLE and VECTOR_RAM come from the linker file */
  43. #if defined(__CC_ARM)
  44. extern uint32_t Image$$VECTOR_ROM$$Base[];
  45. extern uint32_t Image$$VECTOR_RAM$$Base[];
  46. extern uint32_t Image$$RW_m_data$$Base[];
  47. #define __VECTOR_TABLE Image$$VECTOR_ROM$$Base
  48. #define __VECTOR_RAM Image$$VECTOR_RAM$$Base
  49. #define __RAM_VECTOR_TABLE_SIZE (((uint32_t)Image$$RW_m_data$$Base - (uint32_t)Image$$VECTOR_RAM$$Base))
  50. #elif defined(__ICCARM__)
  51. extern uint32_t __RAM_VECTOR_TABLE_SIZE[];
  52. extern uint32_t __VECTOR_TABLE[];
  53. extern uint32_t __VECTOR_RAM[];
  54. #elif defined(__GNUC__)
  55. extern uint32_t __VECTOR_TABLE[];
  56. extern uint32_t __VECTOR_RAM[];
  57. extern uint32_t __RAM_VECTOR_TABLE_SIZE_BYTES[];
  58. uint32_t __RAM_VECTOR_TABLE_SIZE = (uint32_t)(__RAM_VECTOR_TABLE_SIZE_BYTES);
  59. #endif /* defined(__CC_ARM) */
  60. uint32_t n;
  61. uint32_t ret;
  62. uint32_t irqMaskValue;
  63. irqMaskValue = DisableGlobalIRQ();
  64. if (SCB->VTOR != (uint32_t)__VECTOR_RAM)
  65. {
  66. /* Copy the vector table from ROM to RAM */
  67. for (n = 0; n < ((uint32_t)__RAM_VECTOR_TABLE_SIZE) / sizeof(uint32_t); n++)
  68. {
  69. __VECTOR_RAM[n] = __VECTOR_TABLE[n];
  70. }
  71. /* Point the VTOR to the position of vector table */
  72. SCB->VTOR = (uint32_t)__VECTOR_RAM;
  73. }
  74. ret = __VECTOR_RAM[irq + 16];
  75. /* make sure the __VECTOR_RAM is noncachable */
  76. __VECTOR_RAM[irq + 16] = irqHandler;
  77. EnableGlobalIRQ(irqMaskValue);
  78. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  79. exception return operation might vector to incorrect interrupt */
  80. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  81. __DSB();
  82. #endif
  83. return ret;
  84. }
  85. #endif /* ENABLE_RAM_VECTOR_TABLE. */
  86. #endif /* __GIC_PRIO_BITS. */
  87. #ifndef QN908XC_SERIES
  88. #if (defined(FSL_FEATURE_SOC_SYSCON_COUNT) && (FSL_FEATURE_SOC_SYSCON_COUNT > 0))
  89. void EnableDeepSleepIRQ(IRQn_Type interrupt)
  90. {
  91. uint32_t index = 0;
  92. uint32_t intNumber = (uint32_t)interrupt;
  93. while (intNumber >= 32u)
  94. {
  95. index++;
  96. intNumber -= 32u;
  97. }
  98. SYSCON->STARTERSET[index] = 1u << intNumber;
  99. EnableIRQ(interrupt); /* also enable interrupt at NVIC */
  100. }
  101. void DisableDeepSleepIRQ(IRQn_Type interrupt)
  102. {
  103. uint32_t index = 0;
  104. uint32_t intNumber = (uint32_t)interrupt;
  105. while (intNumber >= 32u)
  106. {
  107. index++;
  108. intNumber -= 32u;
  109. }
  110. DisableIRQ(interrupt); /* also disable interrupt at NVIC */
  111. SYSCON->STARTERCLR[index] = 1u << intNumber;
  112. }
  113. #endif /* FSL_FEATURE_SOC_SYSCON_COUNT */
  114. #endif /* QN908XC_SERIES */
  115. void *SDK_Malloc(size_t size, size_t alignbytes)
  116. {
  117. mem_align_cb_t *p_cb = NULL;
  118. uint32_t alignedsize = SDK_SIZEALIGN(size, alignbytes) + alignbytes + sizeof(mem_align_cb_t);
  119. void *p_align_addr, *p_addr = malloc(alignedsize);
  120. if (!p_addr)
  121. {
  122. return NULL;
  123. }
  124. p_align_addr = (void *)SDK_SIZEALIGN((uint32_t)p_addr + sizeof(mem_align_cb_t), alignbytes);
  125. p_cb = (mem_align_cb_t *)((uint32_t)p_align_addr - 4);
  126. p_cb->identifier = SDK_MEM_MAGIC_NUMBER;
  127. p_cb->offset = (uint32_t)p_align_addr - (uint32_t)p_addr;
  128. return (void *)p_align_addr;
  129. }
  130. void SDK_Free(void *ptr)
  131. {
  132. mem_align_cb_t *p_cb = (mem_align_cb_t *)((uint32_t)ptr - 4);
  133. if (p_cb->identifier != SDK_MEM_MAGIC_NUMBER)
  134. {
  135. return;
  136. }
  137. free((void *)((uint32_t)ptr - p_cb->offset));
  138. }