nu_ebi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**************************************************************************//**
  2. * @file ebi.c
  3. * @brief External Bus Interface(EBI) driver source file
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  7. *****************************************************************************/
  8. #include "NuMicro.h"
  9. /** @addtogroup Standard_Driver Standard Driver
  10. @{
  11. */
  12. /** @addtogroup EBI_Driver EBI Driver
  13. @{
  14. */
  15. /** @addtogroup EBI_EXPORTED_FUNCTIONS EBI Exported Functions
  16. @{
  17. */
  18. /**
  19. * @brief Initialize EBI for specify Bank
  20. *
  21. * @param[in] u32Bank Bank number for EBI. Valid values are:
  22. * - \ref EBI_BANK0
  23. * - \ref EBI_BANK1
  24. * - \ref EBI_BANK2
  25. * @param[in] u32DataWidth Data bus width. Valid values are:
  26. * - \ref EBI_BUSWIDTH_8BIT
  27. * - \ref EBI_BUSWIDTH_16BIT
  28. * @param[in] u32TimingClass Default timing configuration. Valid values are:
  29. * - \ref EBI_TIMING_FASTEST
  30. * - \ref EBI_TIMING_VERYFAST
  31. * - \ref EBI_TIMING_FAST
  32. * - \ref EBI_TIMING_NORMAL
  33. * - \ref EBI_TIMING_SLOW
  34. * - \ref EBI_TIMING_VERYSLOW
  35. * - \ref EBI_TIMING_SLOWEST
  36. * @param[in] u32BusMode Set EBI bus operate mode. Valid values are:
  37. * - \ref EBI_OPMODE_NORMAL
  38. * - \ref EBI_OPMODE_CACCESS
  39. * - \ref EBI_OPMODE_ADSEPARATE
  40. * @param[in] u32CSActiveLevel CS is active High/Low. Valid values are:
  41. * - \ref EBI_CS_ACTIVE_HIGH
  42. * - \ref EBI_CS_ACTIVE_LOW
  43. *
  44. * @return None
  45. *
  46. * @details This function is used to open specify EBI bank with different bus width, timing setting and \n
  47. * active level of CS pin to access EBI device.
  48. * @note Write Buffer Enable(WBUFEN) and Extend Time Of ALE(TALE) are only available in EBI bank0 control register.
  49. */
  50. void EBI_Open(uint32_t u32Bank, uint32_t u32DataWidth, uint32_t u32TimingClass, uint32_t u32BusMode, uint32_t u32CSActiveLevel)
  51. {
  52. uint32_t u32Index0 = (uint32_t)&EBI->CTL0 + (uint32_t)u32Bank * 0x10U;
  53. uint32_t u32Index1 = (uint32_t)&EBI->TCTL0 + (uint32_t)u32Bank * 0x10U;
  54. volatile uint32_t *pu32EBICTL = (uint32_t *)(u32Index0);
  55. volatile uint32_t *pu32EBITCTL = (uint32_t *)(u32Index1);
  56. if (u32DataWidth == EBI_BUSWIDTH_8BIT)
  57. {
  58. *pu32EBICTL &= ~EBI_CTL_DW16_Msk;
  59. }
  60. else
  61. {
  62. *pu32EBICTL |= EBI_CTL_DW16_Msk;
  63. }
  64. *pu32EBICTL |= u32BusMode;
  65. switch (u32TimingClass)
  66. {
  67. case EBI_TIMING_FASTEST:
  68. *pu32EBICTL = (*pu32EBICTL & ~(EBI_CTL_MCLKDIV_Msk | EBI_CTL_TALE_Msk)) |
  69. (EBI_MCLKDIV_1 << EBI_CTL_MCLKDIV_Pos) |
  70. (u32CSActiveLevel << EBI_CTL_CSPOLINV_Pos) | EBI_CTL_EN_Msk;
  71. *pu32EBITCTL = 0x0U;
  72. break;
  73. case EBI_TIMING_VERYFAST:
  74. *pu32EBICTL = (*pu32EBICTL & ~(EBI_CTL_MCLKDIV_Msk | EBI_CTL_TALE_Msk)) |
  75. (EBI_MCLKDIV_1 << EBI_CTL_MCLKDIV_Pos) |
  76. (u32CSActiveLevel << EBI_CTL_CSPOLINV_Pos) | EBI_CTL_EN_Msk |
  77. (0x3U << EBI_CTL_TALE_Pos) ;
  78. *pu32EBITCTL = 0x03003318U;
  79. break;
  80. case EBI_TIMING_FAST:
  81. *pu32EBICTL = (*pu32EBICTL & ~(EBI_CTL_MCLKDIV_Msk | EBI_CTL_TALE_Msk)) |
  82. (EBI_MCLKDIV_2 << EBI_CTL_MCLKDIV_Pos) |
  83. (u32CSActiveLevel << EBI_CTL_CSPOLINV_Pos) | EBI_CTL_EN_Msk;
  84. *pu32EBITCTL = 0x0U;
  85. break;
  86. case EBI_TIMING_NORMAL:
  87. *pu32EBICTL = (*pu32EBICTL & ~(EBI_CTL_MCLKDIV_Msk | EBI_CTL_TALE_Msk)) |
  88. (EBI_MCLKDIV_2 << EBI_CTL_MCLKDIV_Pos) |
  89. (u32CSActiveLevel << EBI_CTL_CSPOLINV_Pos) | EBI_CTL_EN_Msk |
  90. (0x3U << EBI_CTL_TALE_Pos) ;
  91. *pu32EBITCTL = 0x03003318U;
  92. break;
  93. case EBI_TIMING_SLOW:
  94. *pu32EBICTL = (*pu32EBICTL & ~(EBI_CTL_MCLKDIV_Msk | EBI_CTL_TALE_Msk)) |
  95. (EBI_MCLKDIV_2 << EBI_CTL_MCLKDIV_Pos) |
  96. (u32CSActiveLevel << EBI_CTL_CSPOLINV_Pos) | EBI_CTL_EN_Msk |
  97. (0x7U << EBI_CTL_TALE_Pos) ;
  98. *pu32EBITCTL = 0x07007738U;
  99. break;
  100. case EBI_TIMING_VERYSLOW:
  101. *pu32EBICTL = (*pu32EBICTL & ~(EBI_CTL_MCLKDIV_Msk | EBI_CTL_TALE_Msk)) |
  102. (EBI_MCLKDIV_8 << EBI_CTL_MCLKDIV_Pos) |
  103. (u32CSActiveLevel << EBI_CTL_CSPOLINV_Pos) | EBI_CTL_EN_Msk |
  104. (0x7U << EBI_CTL_TALE_Pos) ;
  105. *pu32EBITCTL = 0x07007738U;
  106. break;
  107. case EBI_TIMING_SLOWEST:
  108. *pu32EBICTL = (*pu32EBICTL & ~(EBI_CTL_MCLKDIV_Msk | EBI_CTL_TALE_Msk)) |
  109. (EBI_MCLKDIV_16 << EBI_CTL_MCLKDIV_Pos) |
  110. (u32CSActiveLevel << EBI_CTL_CSPOLINV_Pos) | EBI_CTL_EN_Msk |
  111. (0x7U << EBI_CTL_TALE_Pos) ;
  112. *pu32EBITCTL = 0x07007738U;
  113. break;
  114. default:
  115. *pu32EBICTL &= ~EBI_CTL_EN_Msk;
  116. break;
  117. }
  118. }
  119. /**
  120. * @brief Disable EBI on specify Bank
  121. *
  122. * @param[in] u32Bank Bank number for EBI. Valid values are:
  123. * - \ref EBI_BANK0
  124. * - \ref EBI_BANK1
  125. * - \ref EBI_BANK2
  126. *
  127. * @return None
  128. *
  129. * @details This function is used to close specify EBI function.
  130. */
  131. void EBI_Close(uint32_t u32Bank)
  132. {
  133. uint32_t u32Index = (uint32_t)&EBI->CTL0 + u32Bank * 0x10U;
  134. volatile uint32_t *pu32EBICTL = (uint32_t *)(u32Index);
  135. *pu32EBICTL &= ~EBI_CTL_EN_Msk;
  136. }
  137. /**
  138. * @brief Set EBI Bus Timing for specify Bank
  139. *
  140. * @param[in] u32Bank Bank number for EBI. Valid values are:
  141. * - \ref EBI_BANK0
  142. * - \ref EBI_BANK1
  143. * - \ref EBI_BANK2
  144. * @param[in] u32TimingConfig Configure EBI timing settings, includes TACC, TAHD, W2X and R2R setting.
  145. * @param[in] u32MclkDiv Divider for MCLK. Valid values are:
  146. * - \ref EBI_MCLKDIV_1
  147. * - \ref EBI_MCLKDIV_2
  148. * - \ref EBI_MCLKDIV_4
  149. * - \ref EBI_MCLKDIV_8
  150. * - \ref EBI_MCLKDIV_16
  151. * - \ref EBI_MCLKDIV_32
  152. * - \ref EBI_MCLKDIV_64
  153. * - \ref EBI_MCLKDIV_128
  154. *
  155. * @return None
  156. *
  157. * @details This function is used to configure specify EBI bus timing for access EBI device.
  158. */
  159. void EBI_SetBusTiming(uint32_t u32Bank, uint32_t u32TimingConfig, uint32_t u32MclkDiv)
  160. {
  161. uint32_t u32Index0 = (uint32_t)&EBI->CTL0 + (uint32_t)u32Bank * 0x10U;
  162. uint32_t u32Index1 = (uint32_t)&EBI->TCTL0 + (uint32_t)u32Bank * 0x10U;
  163. volatile uint32_t *pu32EBICTL = (uint32_t *)(u32Index0);
  164. volatile uint32_t *pu32EBITCTL = (uint32_t *)(u32Index1);
  165. *pu32EBICTL = (*pu32EBICTL & ~EBI_CTL_MCLKDIV_Msk) | (u32MclkDiv << EBI_CTL_MCLKDIV_Pos);
  166. *pu32EBITCTL = u32TimingConfig;
  167. }
  168. /*@}*/ /* end of group EBI_EXPORTED_FUNCTIONS */
  169. /*@}*/ /* end of group EBI_Driver */
  170. /*@}*/ /* end of group Standard_Driver */