board.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright 2018-2019 NXP
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include "fsl_common.h"
  8. #include "fsl_debug_console.h"
  9. #include "board.h"
  10. #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
  11. #include "fsl_lpi2c.h"
  12. #endif /* SDK_I2C_BASED_COMPONENT_USED */
  13. #include "fsl_iomuxc.h"
  14. /*******************************************************************************
  15. * Variables
  16. ******************************************************************************/
  17. /*******************************************************************************
  18. * Code
  19. ******************************************************************************/
  20. /* Get debug console frequency. */
  21. uint32_t BOARD_DebugConsoleSrcFreq(void)
  22. {
  23. uint32_t freq;
  24. /* To make it simple, we assume default PLL and divider settings, and the only variable
  25. from application is use PLL3 source or OSC source */
  26. if (CLOCK_GetMux(kCLOCK_UartMux) == 0) /* PLL3 div6 80M */
  27. {
  28. freq = (CLOCK_GetPllFreq(kCLOCK_PllUsb1) / 6U) / (CLOCK_GetDiv(kCLOCK_UartDiv) + 1U);
  29. }
  30. else
  31. {
  32. freq = CLOCK_GetOscFreq() / (CLOCK_GetDiv(kCLOCK_UartDiv) + 1U);
  33. }
  34. return freq;
  35. }
  36. /* Initialize debug console. */
  37. void BOARD_InitDebugConsole(void)
  38. {
  39. uint32_t uartClkSrcFreq = BOARD_DebugConsoleSrcFreq();
  40. DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, uartClkSrcFreq);
  41. }
  42. /* MPU configuration. */
  43. void BOARD_ConfigMPU(void)
  44. {
  45. /* Disable I cache and D cache */
  46. if (SCB_CCR_IC_Msk == (SCB_CCR_IC_Msk & SCB->CCR))
  47. {
  48. SCB_DisableICache();
  49. }
  50. if (SCB_CCR_DC_Msk == (SCB_CCR_DC_Msk & SCB->CCR))
  51. {
  52. SCB_DisableDCache();
  53. }
  54. /* Disable MPU */
  55. ARM_MPU_Disable();
  56. /* MPU configure:
  57. * Use ARM_MPU_RASR(DisableExec, AccessPermission, TypeExtField, IsShareable, IsCacheable, IsBufferable,
  58. * SubRegionDisable, Size)
  59. * API in mpu_armv7.h.
  60. * param DisableExec Instruction access (XN) disable bit,0=instruction fetches enabled, 1=instruction fetches
  61. * disabled.
  62. * param AccessPermission Data access permissions, allows you to configure read/write access for User and
  63. * Privileged mode.
  64. * Use MACROS defined in mpu_armv7.h:
  65. * ARM_MPU_AP_NONE/ARM_MPU_AP_PRIV/ARM_MPU_AP_URO/ARM_MPU_AP_FULL/ARM_MPU_AP_PRO/ARM_MPU_AP_RO
  66. * Combine TypeExtField/IsShareable/IsCacheable/IsBufferable to configure MPU memory access attributes.
  67. * TypeExtField IsShareable IsCacheable IsBufferable Memory Attribtue Shareability Cache
  68. * 0 x 0 0 Strongly Ordered shareable
  69. * 0 x 0 1 Device shareable
  70. * 0 0 1 0 Normal not shareable Outer and inner write
  71. * through no write allocate
  72. * 0 0 1 1 Normal not shareable Outer and inner write
  73. * back no write allocate
  74. * 0 1 1 0 Normal shareable Outer and inner write
  75. * through no write allocate
  76. * 0 1 1 1 Normal shareable Outer and inner write
  77. * back no write allocate
  78. * 1 0 0 0 Normal not shareable outer and inner
  79. * noncache
  80. * 1 1 0 0 Normal shareable outer and inner
  81. * noncache
  82. * 1 0 1 1 Normal not shareable outer and inner write
  83. * back write/read acllocate
  84. * 1 1 1 1 Normal shareable outer and inner write
  85. * back write/read acllocate
  86. * 2 x 0 0 Device not shareable
  87. * Above are normal use settings, if your want to see more details or want to config different inner/outter cache
  88. * policy.
  89. * please refer to Table 4-55 /4-56 in arm cortex-M7 generic user guide <dui0646b_cortex_m7_dgug.pdf>
  90. * param SubRegionDisable Sub-region disable field. 0=sub-region is enabled, 1=sub-region is disabled.
  91. * param Size Region size of the region to be configured. use ARM_MPU_REGION_SIZE_xxx MACRO in
  92. * mpu_armv7.h.
  93. */
  94. /*
  95. * Add default region to deny access to whole address space to workaround speculative prefetch.
  96. * Refer to Arm errata 1013783-B for more details.
  97. *
  98. */
  99. /* Region 0 setting: Instruction access disabled, No data access permission. */
  100. MPU->RBAR = ARM_MPU_RBAR(0, 0x00000000U);
  101. MPU->RASR = ARM_MPU_RASR(1, ARM_MPU_AP_NONE, 0, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_4GB);
  102. /* Region 1 setting: Memory with Device type, not shareable, non-cacheable. */
  103. MPU->RBAR = ARM_MPU_RBAR(1, 0x80000000U);
  104. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_512MB);
  105. /* Region 2 setting: Memory with Device type, not shareable, non-cacheable. */
  106. MPU->RBAR = ARM_MPU_RBAR(2, 0x60000000U);
  107. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_512MB);
  108. #if defined(XIP_EXTERNAL_FLASH) && (XIP_EXTERNAL_FLASH == 1)
  109. /* Region 3 setting: Memory with Normal type, not shareable, outer/inner write back. */
  110. MPU->RBAR = ARM_MPU_RBAR(3, 0x60000000U);
  111. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_RO, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_8MB);
  112. #endif
  113. /* Region 4 setting: Memory with Device type, not shareable, non-cacheable. */
  114. MPU->RBAR = ARM_MPU_RBAR(4, 0x00000000U);
  115. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_1GB);
  116. /* Region 5 setting: Memory with Normal type, not shareable, outer/inner write back */
  117. MPU->RBAR = ARM_MPU_RBAR(5, 0x00000000U);
  118. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_64KB);
  119. /* Region 6 setting: Memory with Normal type, not shareable, outer/inner write back */
  120. MPU->RBAR = ARM_MPU_RBAR(6, 0x20000000U);
  121. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_64KB);
  122. /* Region 7 setting: Memory with Normal type, not shareable, outer/inner write back */
  123. MPU->RBAR = ARM_MPU_RBAR(7, 0x20200000U);
  124. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_128KB);
  125. /* Region 8 setting: Memory with Normal type, not shareable, outer/inner write back */
  126. MPU->RBAR = ARM_MPU_RBAR(8, 0x80000000U);
  127. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_32MB);
  128. /* Region 10 setting: Memory with Device type, not shareable, non-cacheable */
  129. MPU->RBAR = ARM_MPU_RBAR(10, 0x40000000);
  130. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_4MB);
  131. /* Enable MPU */
  132. ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk);
  133. /* Enable I cache and D cache */
  134. SCB_EnableDCache();
  135. SCB_EnableICache();
  136. }
  137. #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
  138. void BOARD_LPI2C_Init(LPI2C_Type *base, uint32_t clkSrc_Hz)
  139. {
  140. lpi2c_master_config_t lpi2cConfig = {0};
  141. /*
  142. * lpi2cConfig.debugEnable = false;
  143. * lpi2cConfig.ignoreAck = false;
  144. * lpi2cConfig.pinConfig = kLPI2C_2PinOpenDrain;
  145. * lpi2cConfig.baudRate_Hz = 100000U;
  146. * lpi2cConfig.busIdleTimeout_ns = 0;
  147. * lpi2cConfig.pinLowTimeout_ns = 0;
  148. * lpi2cConfig.sdaGlitchFilterWidth_ns = 0;
  149. * lpi2cConfig.sclGlitchFilterWidth_ns = 0;
  150. */
  151. LPI2C_MasterGetDefaultConfig(&lpi2cConfig);
  152. LPI2C_MasterInit(base, &lpi2cConfig, clkSrc_Hz);
  153. }
  154. status_t BOARD_LPI2C_Send(LPI2C_Type *base,
  155. uint8_t deviceAddress,
  156. uint32_t subAddress,
  157. uint8_t subAddressSize,
  158. uint8_t *txBuff,
  159. uint8_t txBuffSize)
  160. {
  161. lpi2c_master_transfer_t xfer;
  162. xfer.flags = kLPI2C_TransferDefaultFlag;
  163. xfer.slaveAddress = deviceAddress;
  164. xfer.direction = kLPI2C_Write;
  165. xfer.subaddress = subAddress;
  166. xfer.subaddressSize = subAddressSize;
  167. xfer.data = txBuff;
  168. xfer.dataSize = txBuffSize;
  169. return LPI2C_MasterTransferBlocking(base, &xfer);
  170. }
  171. status_t BOARD_LPI2C_Receive(LPI2C_Type *base,
  172. uint8_t deviceAddress,
  173. uint32_t subAddress,
  174. uint8_t subAddressSize,
  175. uint8_t *rxBuff,
  176. uint8_t rxBuffSize)
  177. {
  178. lpi2c_master_transfer_t xfer;
  179. xfer.flags = kLPI2C_TransferDefaultFlag;
  180. xfer.slaveAddress = deviceAddress;
  181. xfer.direction = kLPI2C_Read;
  182. xfer.subaddress = subAddress;
  183. xfer.subaddressSize = subAddressSize;
  184. xfer.data = rxBuff;
  185. xfer.dataSize = rxBuffSize;
  186. return LPI2C_MasterTransferBlocking(base, &xfer);
  187. }
  188. void BOARD_Accel_I2C_Init(void)
  189. {
  190. BOARD_LPI2C_Init(BOARD_ACCEL_I2C_BASEADDR, BOARD_ACCEL_I2C_CLOCK_FREQ);
  191. }
  192. status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
  193. {
  194. uint8_t data = (uint8_t)txBuff;
  195. return BOARD_LPI2C_Send(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
  196. }
  197. status_t BOARD_Accel_I2C_Receive(
  198. uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
  199. {
  200. return BOARD_LPI2C_Receive(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
  201. }
  202. void BOARD_Codec_I2C_Init(void)
  203. {
  204. BOARD_LPI2C_Init(BOARD_CODEC_I2C_BASEADDR, BOARD_CODEC_I2C_CLOCK_FREQ);
  205. }
  206. status_t BOARD_Codec_I2C_Send(
  207. uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
  208. {
  209. return BOARD_LPI2C_Send(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
  210. txBuffSize);
  211. }
  212. status_t BOARD_Codec_I2C_Receive(
  213. uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
  214. {
  215. return BOARD_LPI2C_Receive(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
  216. }
  217. #endif /* SDK_I2C_BASED_COMPONENT_USED */