fsl_common.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include <rtthread.h>
  33. #define PRINTF rt_kprintf
  34. #define SDK_MEM_MAGIC_NUMBER 12345U
  35. typedef struct _mem_align_control_block
  36. {
  37. uint16_t identifier; /*!< Identifier for the memory control block. */
  38. uint16_t offset; /*!< offset from aligned adress to real address */
  39. } mem_align_cb_t;
  40. #ifndef NDEBUG
  41. #if (defined(__CC_ARM)) || (defined(__ICCARM__))
  42. void __aeabi_assert(const char *failedExpr, const char *file, int line)
  43. {
  44. PRINTF("ASSERT ERROR \" %s \": file \"%s\" Line \"%d\" \n", failedExpr, file, line);
  45. for (;;)
  46. {
  47. __BKPT(0);
  48. }
  49. }
  50. #elif(defined(__GNUC__))
  51. void __assert_func(const char *file, int line, const char *func, const char *failedExpr)
  52. {
  53. PRINTF("ASSERT ERROR \" %s \": file \"%s\" Line \"%d\" function name \"%s\" \n", failedExpr, file, line, func);
  54. for (;;)
  55. {
  56. __BKPT(0);
  57. }
  58. }
  59. #endif /* (defined(__CC_ARM)) || (defined (__ICCARM__)) */
  60. #endif /* NDEBUG */
  61. #ifndef __GIC_PRIO_BITS
  62. uint32_t InstallIRQHandler(IRQn_Type irq, uint32_t irqHandler)
  63. {
  64. /* Addresses for VECTOR_TABLE and VECTOR_RAM come from the linker file */
  65. #if defined(__CC_ARM)
  66. extern uint32_t Image$$VECTOR_ROM$$Base[];
  67. extern uint32_t Image$$VECTOR_RAM$$Base[];
  68. extern uint32_t Image$$RW_m_data$$Base[];
  69. #define __VECTOR_TABLE Image$$VECTOR_ROM$$Base
  70. #define __VECTOR_RAM Image$$VECTOR_RAM$$Base
  71. #define __RAM_VECTOR_TABLE_SIZE (((uint32_t)Image$$RW_m_data$$Base - (uint32_t)Image$$VECTOR_RAM$$Base))
  72. #elif defined(__ICCARM__)
  73. extern uint32_t __RAM_VECTOR_TABLE_SIZE[];
  74. extern uint32_t __VECTOR_TABLE[];
  75. extern uint32_t __VECTOR_RAM[];
  76. #elif defined(__GNUC__)
  77. extern uint32_t __VECTOR_TABLE[];
  78. extern uint32_t __VECTOR_RAM[];
  79. extern uint32_t __RAM_VECTOR_TABLE_SIZE_BYTES[];
  80. uint32_t __RAM_VECTOR_TABLE_SIZE = (uint32_t)(__RAM_VECTOR_TABLE_SIZE_BYTES);
  81. #endif /* defined(__CC_ARM) */
  82. uint32_t n;
  83. uint32_t ret;
  84. uint32_t irqMaskValue;
  85. irqMaskValue = DisableGlobalIRQ();
  86. if (SCB->VTOR != (uint32_t)__VECTOR_RAM)
  87. {
  88. /* Copy the vector table from ROM to RAM */
  89. for (n = 0; n < ((uint32_t)__RAM_VECTOR_TABLE_SIZE) / sizeof(uint32_t); n++)
  90. {
  91. __VECTOR_RAM[n] = __VECTOR_TABLE[n];
  92. }
  93. /* Point the VTOR to the position of vector table */
  94. SCB->VTOR = (uint32_t)__VECTOR_RAM;
  95. }
  96. ret = __VECTOR_RAM[irq + 16];
  97. /* make sure the __VECTOR_RAM is noncachable */
  98. __VECTOR_RAM[irq + 16] = irqHandler;
  99. EnableGlobalIRQ(irqMaskValue);
  100. return ret;
  101. }
  102. #endif
  103. #ifndef CPU_QN908X
  104. #if (defined(FSL_FEATURE_SOC_SYSCON_COUNT) && (FSL_FEATURE_SOC_SYSCON_COUNT > 0))
  105. void EnableDeepSleepIRQ(IRQn_Type interrupt)
  106. {
  107. uint32_t index = 0;
  108. uint32_t intNumber = (uint32_t)interrupt;
  109. while (intNumber >= 32u)
  110. {
  111. index++;
  112. intNumber -= 32u;
  113. }
  114. SYSCON->STARTERSET[index] = 1u << intNumber;
  115. EnableIRQ(interrupt); /* also enable interrupt at NVIC */
  116. }
  117. void DisableDeepSleepIRQ(IRQn_Type interrupt)
  118. {
  119. uint32_t index = 0;
  120. uint32_t intNumber = (uint32_t)interrupt;
  121. while (intNumber >= 32u)
  122. {
  123. index++;
  124. intNumber -= 32u;
  125. }
  126. DisableIRQ(interrupt); /* also disable interrupt at NVIC */
  127. SYSCON->STARTERCLR[index] = 1u << intNumber;
  128. }
  129. #endif /* FSL_FEATURE_SOC_SYSCON_COUNT */
  130. #else
  131. void EnableDeepSleepIRQ(IRQn_Type interrupt)
  132. {
  133. uint32_t index = 0;
  134. uint32_t intNumber = (uint32_t)interrupt;
  135. while (intNumber >= 32u)
  136. {
  137. index++;
  138. intNumber -= 32u;
  139. }
  140. /* SYSCON->STARTERSET[index] = 1u << intNumber; */
  141. EnableIRQ(interrupt); /* also enable interrupt at NVIC */
  142. }
  143. void DisableDeepSleepIRQ(IRQn_Type interrupt)
  144. {
  145. uint32_t index = 0;
  146. uint32_t intNumber = (uint32_t)interrupt;
  147. while (intNumber >= 32u)
  148. {
  149. index++;
  150. intNumber -= 32u;
  151. }
  152. DisableIRQ(interrupt); /* also disable interrupt at NVIC */
  153. /* SYSCON->STARTERCLR[index] = 1u << intNumber; */
  154. }
  155. #endif /*CPU_QN908X */
  156. void *SDK_Malloc(size_t size, size_t alignbytes)
  157. {
  158. mem_align_cb_t *p_cb = NULL;
  159. uint32_t alignedsize = SDK_SIZEALIGN(size, alignbytes) + alignbytes + sizeof(mem_align_cb_t);
  160. void *p_align_addr, *p_addr = malloc(alignedsize);
  161. if (!p_addr)
  162. {
  163. return NULL;
  164. }
  165. p_align_addr = (void *)SDK_SIZEALIGN((uint32_t)p_addr + sizeof(mem_align_cb_t), alignbytes);
  166. p_cb = (mem_align_cb_t *)((uint32_t)p_align_addr - 4);
  167. p_cb->identifier = SDK_MEM_MAGIC_NUMBER;
  168. p_cb->offset = (uint32_t)p_align_addr - (uint32_t)p_addr;
  169. return (void *)p_align_addr;
  170. }
  171. void SDK_Free(void *ptr)
  172. {
  173. mem_align_cb_t *p_cb = (mem_align_cb_t *)((uint32_t)ptr - 4);
  174. if (p_cb->identifier != SDK_MEM_MAGIC_NUMBER)
  175. {
  176. return;
  177. }
  178. free((void *)((uint32_t)ptr - p_cb->offset));
  179. }