cmem7_misc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. *****************************************************************************
  3. * @file cmem7_misc.c
  4. *
  5. * @brief CMEM7 miscellaneous file
  6. *
  7. *
  8. * @version V1.0
  9. * @date 3. September 2013
  10. *
  11. * @note
  12. *
  13. *****************************************************************************
  14. * @attention
  15. *
  16. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  17. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  18. * TIME. AS A RESULT, CAPITAL-MICRO SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  19. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  20. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  21. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  22. *
  23. * <h2><center>&copy; COPYRIGHT 2013 Capital-micro </center></h2>
  24. *****************************************************************************
  25. */
  26. #include "cmem7_misc.h"
  27. #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
  28. void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
  29. {
  30. /* Check the parameters */
  31. assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));
  32. /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
  33. SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
  34. }
  35. void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
  36. {
  37. uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;
  38. /* Check the parameters */
  39. assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));
  40. assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));
  41. if (NVIC_InitStruct->NVIC_IRQChannelCmd != FALSE)
  42. {
  43. /* Compute the Corresponding IRQ Priority --------------------------------*/
  44. tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
  45. tmppre = (0x4 - tmppriority);
  46. tmpsub = tmpsub >> tmppriority;
  47. tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
  48. tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;
  49. tmppriority = tmppriority << 0x04;
  50. NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
  51. /* Enable the Selected IRQ Channels --------------------------------------*/
  52. NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
  53. (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  54. }
  55. else
  56. {
  57. /* Disable the Selected IRQ Channels -------------------------------------*/
  58. NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
  59. (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  60. }
  61. }
  62. void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)
  63. {
  64. /* Check the parameters */
  65. assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));
  66. assert_param(IS_NVIC_OFFSET(Offset));
  67. SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
  68. }
  69. void NVIC_SystemLPConfig(uint8_t LowPowerMode, BOOL NewState)
  70. {
  71. /* Check the parameters */
  72. assert_param(IS_NVIC_LP(LowPowerMode));
  73. if (!NewState)
  74. {
  75. SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode);
  76. } else {
  77. SCB->SCR |= LowPowerMode;
  78. }
  79. }
  80. #define DEF_IBUS_OFFSET 0x1FFE0000
  81. #define DEF_EXT_ADDR 0x08020000
  82. static BOOL isMappingOn() {
  83. /* If default values aren't changed */
  84. if ((GLOBAL_CTRL->IBUSOFF == DEF_IBUS_OFFSET) &&
  85. (GLOBAL_CTRL->EXTADDR == DEF_EXT_ADDR)) {
  86. return FALSE;
  87. }
  88. return TRUE;
  89. }
  90. void GLB_MMAP(uint32_t from, uint32_t to, BOOL isIcacheOn) {
  91. volatile int n;
  92. GLOBAL_CTRL->IBUSOFF = GLOBAL_CTRL->DBUSOFF = (from - to);
  93. GLOBAL_CTRL->EXTADDR = to;
  94. // Delay several cycles
  95. for (n = 0; n < 100; n++);
  96. GLOBAL_CTRL->ICACHE_b.EN = isIcacheOn;
  97. for (n = 0; n < 100; n++);
  98. }
  99. /*
  100. * ------------------------------------------------------------------
  101. * | 0 - 0x20000 | --> 0x20000000 | -> 0x40000000 | -> 0xFFFFFFFF |
  102. * | code SRAM | map to region | data SRAM | map from region |
  103. * ------------------------------------------------------------------
  104. */
  105. #define MAPPING_FROM_REGION_START 0x40000000
  106. #define MAPPING_TO_REGION_END 0x20000000
  107. uint32_t GLB_ConvertToMappingFromAddr(uint32_t to) {
  108. if (!isMappingOn()) {
  109. return to;
  110. }
  111. if ((to > MAPPING_TO_REGION_END) || (to < GLOBAL_CTRL->EXTADDR)) {
  112. return to;
  113. }
  114. return (to + GLOBAL_CTRL->IBUSOFF);
  115. }
  116. uint32_t GLB_ConvertToMappingToAddr(uint32_t from) {
  117. if (!isMappingOn()) {
  118. return from;
  119. }
  120. if (from < MAPPING_FROM_REGION_START) {
  121. return from;
  122. }
  123. return (from - GLOBAL_CTRL->IBUSOFF);
  124. }
  125. void GLB_SetNmiIrqNum(uint32_t irq) {
  126. GLOBAL_CTRL->NMI_SEL_b.NMI = irq;
  127. }
  128. void GLB_SelectSysClkSource(uint8_t source) {
  129. switch (source) {
  130. case SYS_CLK_SEL_DLL :
  131. // M7's DLL clock should be fixed at PLL loation 2
  132. // In constrast, it's C2R1.
  133. // Wait DLL clock stable
  134. while (PDLOCK->GCLK_b.C2R1D == 0) ;
  135. GLOBAL_CTRL->CLK_SEL_1_b.SYS_CLK = SYS_CLK_SEL_DLL;
  136. break;
  137. case SYS_CLK_SEL_CRYSTAL :
  138. GLOBAL_CTRL->CLK_SEL_1_b.SYS_CLK = SYS_CLK_SEL_CRYSTAL;
  139. break;
  140. case SYS_CLK_SEL_EXTERNAL :
  141. // TODO, Add the condition that makes sure input
  142. // external clock is stable
  143. // For example :
  144. // PLL location 0
  145. // while (PDLOCK->GCLK_b.C1R1P == 0) ;
  146. // DLL location 0
  147. // while (PDLOCK->GCLK_b.C1R1D == 0) ;
  148. GLOBAL_CTRL->CLK_SEL_1_b.SYS_CLK = SYS_CLK_SEL_EXTERNAL;
  149. break;
  150. case SYS_CLK_SEL_OSC :
  151. // Fall through
  152. default :
  153. GLOBAL_CTRL->CLK_SEL_1_b.SYS_CLK = SYS_CLK_SEL_OSC;
  154. break;
  155. }
  156. }