nu_ebi.c 7.3 KB

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