board.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
  43. void BOARD_LPI2C_Init(LPI2C_Type *base, uint32_t clkSrc_Hz)
  44. {
  45. lpi2c_master_config_t lpi2cConfig = {0};
  46. /*
  47. * lpi2cConfig.debugEnable = false;
  48. * lpi2cConfig.ignoreAck = false;
  49. * lpi2cConfig.pinConfig = kLPI2C_2PinOpenDrain;
  50. * lpi2cConfig.baudRate_Hz = 100000U;
  51. * lpi2cConfig.busIdleTimeout_ns = 0;
  52. * lpi2cConfig.pinLowTimeout_ns = 0;
  53. * lpi2cConfig.sdaGlitchFilterWidth_ns = 0;
  54. * lpi2cConfig.sclGlitchFilterWidth_ns = 0;
  55. */
  56. LPI2C_MasterGetDefaultConfig(&lpi2cConfig);
  57. LPI2C_MasterInit(base, &lpi2cConfig, clkSrc_Hz);
  58. }
  59. status_t BOARD_LPI2C_Send(LPI2C_Type *base,
  60. uint8_t deviceAddress,
  61. uint32_t subAddress,
  62. uint8_t subAddressSize,
  63. uint8_t *txBuff,
  64. uint8_t txBuffSize)
  65. {
  66. lpi2c_master_transfer_t xfer;
  67. xfer.flags = kLPI2C_TransferDefaultFlag;
  68. xfer.slaveAddress = deviceAddress;
  69. xfer.direction = kLPI2C_Write;
  70. xfer.subaddress = subAddress;
  71. xfer.subaddressSize = subAddressSize;
  72. xfer.data = txBuff;
  73. xfer.dataSize = txBuffSize;
  74. return LPI2C_MasterTransferBlocking(base, &xfer);
  75. }
  76. status_t BOARD_LPI2C_Receive(LPI2C_Type *base,
  77. uint8_t deviceAddress,
  78. uint32_t subAddress,
  79. uint8_t subAddressSize,
  80. uint8_t *rxBuff,
  81. uint8_t rxBuffSize)
  82. {
  83. lpi2c_master_transfer_t xfer;
  84. xfer.flags = kLPI2C_TransferDefaultFlag;
  85. xfer.slaveAddress = deviceAddress;
  86. xfer.direction = kLPI2C_Read;
  87. xfer.subaddress = subAddress;
  88. xfer.subaddressSize = subAddressSize;
  89. xfer.data = rxBuff;
  90. xfer.dataSize = rxBuffSize;
  91. return LPI2C_MasterTransferBlocking(base, &xfer);
  92. }
  93. status_t BOARD_LPI2C_SendSCCB(LPI2C_Type *base,
  94. uint8_t deviceAddress,
  95. uint32_t subAddress,
  96. uint8_t subAddressSize,
  97. uint8_t *txBuff,
  98. uint8_t txBuffSize)
  99. {
  100. lpi2c_master_transfer_t xfer;
  101. xfer.flags = kLPI2C_TransferDefaultFlag;
  102. xfer.slaveAddress = deviceAddress;
  103. xfer.direction = kLPI2C_Write;
  104. xfer.subaddress = subAddress;
  105. xfer.subaddressSize = subAddressSize;
  106. xfer.data = txBuff;
  107. xfer.dataSize = txBuffSize;
  108. return LPI2C_MasterTransferBlocking(base, &xfer);
  109. }
  110. status_t BOARD_LPI2C_ReceiveSCCB(LPI2C_Type *base,
  111. uint8_t deviceAddress,
  112. uint32_t subAddress,
  113. uint8_t subAddressSize,
  114. uint8_t *rxBuff,
  115. uint8_t rxBuffSize)
  116. {
  117. status_t status;
  118. lpi2c_master_transfer_t xfer;
  119. xfer.flags = kLPI2C_TransferDefaultFlag;
  120. xfer.slaveAddress = deviceAddress;
  121. xfer.direction = kLPI2C_Write;
  122. xfer.subaddress = subAddress;
  123. xfer.subaddressSize = subAddressSize;
  124. xfer.data = NULL;
  125. xfer.dataSize = 0;
  126. status = LPI2C_MasterTransferBlocking(base, &xfer);
  127. if (kStatus_Success == status)
  128. {
  129. xfer.subaddressSize = 0;
  130. xfer.direction = kLPI2C_Read;
  131. xfer.data = rxBuff;
  132. xfer.dataSize = rxBuffSize;
  133. status = LPI2C_MasterTransferBlocking(base, &xfer);
  134. }
  135. return status;
  136. }
  137. void BOARD_Accel_I2C_Init(void)
  138. {
  139. BOARD_LPI2C_Init(BOARD_ACCEL_I2C_BASEADDR, BOARD_ACCEL_I2C_CLOCK_FREQ);
  140. }
  141. status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
  142. {
  143. uint8_t data = (uint8_t)txBuff;
  144. return BOARD_LPI2C_Send(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
  145. }
  146. status_t BOARD_Accel_I2C_Receive(
  147. uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
  148. {
  149. return BOARD_LPI2C_Receive(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
  150. }
  151. void BOARD_Codec_I2C_Init(void)
  152. {
  153. BOARD_LPI2C_Init(BOARD_CODEC_I2C_BASEADDR, BOARD_CODEC_I2C_CLOCK_FREQ);
  154. }
  155. status_t BOARD_Codec_I2C_Send(
  156. uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
  157. {
  158. return BOARD_LPI2C_Send(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
  159. txBuffSize);
  160. }
  161. status_t BOARD_Codec_I2C_Receive(
  162. uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
  163. {
  164. return BOARD_LPI2C_Receive(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
  165. }
  166. void BOARD_Camera_I2C_Init(void)
  167. {
  168. CLOCK_SetMux(kCLOCK_Lpi2cMux, BOARD_CAMERA_I2C_CLOCK_SOURCE_SELECT);
  169. CLOCK_SetDiv(kCLOCK_Lpi2cDiv, BOARD_CAMERA_I2C_CLOCK_SOURCE_DIVIDER);
  170. BOARD_LPI2C_Init(BOARD_CAMERA_I2C_BASEADDR, BOARD_CAMERA_I2C_CLOCK_FREQ);
  171. }
  172. status_t BOARD_Camera_I2C_Send(
  173. uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
  174. {
  175. return BOARD_LPI2C_Send(BOARD_CAMERA_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
  176. txBuffSize);
  177. }
  178. status_t BOARD_Camera_I2C_Receive(
  179. uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
  180. {
  181. return BOARD_LPI2C_Receive(BOARD_CAMERA_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff,
  182. rxBuffSize);
  183. }
  184. status_t BOARD_Camera_I2C_SendSCCB(
  185. uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
  186. {
  187. return BOARD_LPI2C_SendSCCB(BOARD_CAMERA_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
  188. txBuffSize);
  189. }
  190. status_t BOARD_Camera_I2C_ReceiveSCCB(
  191. uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
  192. {
  193. return BOARD_LPI2C_ReceiveSCCB(BOARD_CAMERA_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff,
  194. rxBuffSize);
  195. }
  196. #endif /* SDK_I2C_BASED_COMPONENT_USED */
  197. /* MPU configuration. */
  198. void BOARD_ConfigMPU(void)
  199. {
  200. #if defined(__CC_ARM) || defined(__ARMCC_VERSION)
  201. extern uint32_t Image$$RW_m_ncache$$Base[];
  202. /* RW_m_ncache_unused is a auxiliary region which is used to get the whole size of noncache section */
  203. extern uint32_t Image$$RW_m_ncache_unused$$Base[];
  204. extern uint32_t Image$$RW_m_ncache_unused$$ZI$$Limit[];
  205. uint32_t nonCacheStart = (uint32_t)Image$$RW_m_ncache$$Base;
  206. uint32_t size = ((uint32_t)Image$$RW_m_ncache_unused$$Base == nonCacheStart) ?
  207. 0 :
  208. ((uint32_t)Image$$RW_m_ncache_unused$$ZI$$Limit - nonCacheStart);
  209. #elif defined(__MCUXPRESSO)
  210. extern uint32_t __base_NCACHE_REGION;
  211. extern uint32_t __top_NCACHE_REGION;
  212. uint32_t nonCacheStart = (uint32_t)(&__base_NCACHE_REGION);
  213. uint32_t size = (uint32_t)(&__top_NCACHE_REGION) - nonCacheStart;
  214. #elif defined(__ICCARM__) || defined(__GNUC__)
  215. extern uint32_t __NCACHE_REGION_START[];
  216. extern uint32_t __NCACHE_REGION_SIZE[];
  217. uint32_t nonCacheStart = (uint32_t)__NCACHE_REGION_START;
  218. uint32_t size = (uint32_t)__NCACHE_REGION_SIZE;
  219. #endif
  220. uint32_t i = 0;
  221. /* Disable I cache and D cache */
  222. if (SCB_CCR_IC_Msk == (SCB_CCR_IC_Msk & SCB->CCR))
  223. {
  224. SCB_DisableICache();
  225. }
  226. if (SCB_CCR_DC_Msk == (SCB_CCR_DC_Msk & SCB->CCR))
  227. {
  228. SCB_DisableDCache();
  229. }
  230. /* Disable MPU */
  231. ARM_MPU_Disable();
  232. /* MPU configure:
  233. * Use ARM_MPU_RASR(DisableExec, AccessPermission, TypeExtField, IsShareable, IsCacheable, IsBufferable,
  234. * SubRegionDisable, Size)
  235. * API in mpu_armv7.h.
  236. * param DisableExec Instruction access (XN) disable bit,0=instruction fetches enabled, 1=instruction fetches
  237. * disabled.
  238. * param AccessPermission Data access permissions, allows you to configure read/write access for User and
  239. * Privileged mode.
  240. * Use MACROS defined in mpu_armv7.h:
  241. * ARM_MPU_AP_NONE/ARM_MPU_AP_PRIV/ARM_MPU_AP_URO/ARM_MPU_AP_FULL/ARM_MPU_AP_PRO/ARM_MPU_AP_RO
  242. * Combine TypeExtField/IsShareable/IsCacheable/IsBufferable to configure MPU memory access attributes.
  243. * TypeExtField IsShareable IsCacheable IsBufferable Memory Attribtue Shareability Cache
  244. * 0 x 0 0 Strongly Ordered shareable
  245. * 0 x 0 1 Device shareable
  246. * 0 0 1 0 Normal not shareable Outer and inner write
  247. * through no write allocate
  248. * 0 0 1 1 Normal not shareable Outer and inner write
  249. * back no write allocate
  250. * 0 1 1 0 Normal shareable Outer and inner write
  251. * through no write allocate
  252. * 0 1 1 1 Normal shareable Outer and inner write
  253. * back no write allocate
  254. * 1 0 0 0 Normal not shareable outer and inner
  255. * noncache
  256. * 1 1 0 0 Normal shareable outer and inner
  257. * noncache
  258. * 1 0 1 1 Normal not shareable outer and inner write
  259. * back write/read acllocate
  260. * 1 1 1 1 Normal shareable outer and inner write
  261. * back write/read acllocate
  262. * 2 x 0 0 Device not shareable
  263. * Above are normal use settings, if your want to see more details or want to config different inner/outter cache
  264. * policy.
  265. * please refer to Table 4-55 /4-56 in arm cortex-M7 generic user guide <dui0646b_cortex_m7_dgug.pdf>
  266. * param SubRegionDisable Sub-region disable field. 0=sub-region is enabled, 1=sub-region is disabled.
  267. * param Size Region size of the region to be configured. use ARM_MPU_REGION_SIZE_xxx MACRO in
  268. * mpu_armv7.h.
  269. */
  270. /* Region 0 setting: Memory with Device type, not shareable, non-cacheable. */
  271. MPU->RBAR = ARM_MPU_RBAR(0, 0x80000000U);
  272. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_512MB);
  273. /* Region 1 setting: Memory with Device type, not shareable, non-cacheable. */
  274. MPU->RBAR = ARM_MPU_RBAR(1, 0x60000000U);
  275. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_512MB);
  276. #if defined(XIP_EXTERNAL_FLASH) && (XIP_EXTERNAL_FLASH == 1)
  277. /* Region 2 setting: Memory with Normal type, not shareable, outer/inner write back. */
  278. MPU->RBAR = ARM_MPU_RBAR(2, 0x60000000U);
  279. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_RO, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_8MB);
  280. #endif
  281. /* Region 3 setting: Memory with Device type, not shareable, non-cacheable. */
  282. MPU->RBAR = ARM_MPU_RBAR(3, 0x00000000U);
  283. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_1GB);
  284. /* Region 4 setting: Memory with Normal type, not shareable, outer/inner write back */
  285. MPU->RBAR = ARM_MPU_RBAR(4, 0x00000000U);
  286. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_128KB);
  287. /* Region 5 setting: Memory with Normal type, not shareable, outer/inner write back */
  288. MPU->RBAR = ARM_MPU_RBAR(5, 0x20000000U);
  289. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_128KB);
  290. /* Region 6 setting: Memory with Normal type, not shareable, outer/inner write back */
  291. MPU->RBAR = ARM_MPU_RBAR(6, 0x20200000U);
  292. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_512KB);
  293. /* Region 7 setting: Memory with Normal type, not shareable, outer/inner write back */
  294. MPU->RBAR = ARM_MPU_RBAR(7, 0x20280000U);
  295. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_256KB);
  296. /* Region 8 setting: Memory with Normal type, not shareable, outer/inner write back */
  297. MPU->RBAR = ARM_MPU_RBAR(8, 0x80000000U);
  298. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_32MB);
  299. while ((size >> i) > 0x1U)
  300. {
  301. i++;
  302. }
  303. if (i != 0)
  304. {
  305. /* The MPU region size should be 2^N, 5<=N<=32, region base should be multiples of size. */
  306. assert(!(nonCacheStart % size));
  307. assert(size == (uint32_t)(1 << i));
  308. assert(i >= 5);
  309. /* Region 9 setting: Memory with Normal type, not shareable, non-cacheable */
  310. MPU->RBAR = ARM_MPU_RBAR(9, nonCacheStart);
  311. MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 1, 0, 0, 0, 0, i - 1);
  312. }
  313. /* Enable MPU */
  314. ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk);
  315. /* Enable I cache and D cache */
  316. SCB_EnableDCache();
  317. SCB_EnableICache();
  318. }
  319. void BOARD_SD_Pin_Config(uint32_t speed, uint32_t strength)
  320. {
  321. IOMUXC_SetPinConfig(IOMUXC_GPIO_SD_B0_00_USDHC1_CMD,
  322. IOMUXC_SW_PAD_CTL_PAD_SPEED(speed) | IOMUXC_SW_PAD_CTL_PAD_SRE_MASK |
  323. IOMUXC_SW_PAD_CTL_PAD_PKE_MASK | IOMUXC_SW_PAD_CTL_PAD_PUE_MASK |
  324. IOMUXC_SW_PAD_CTL_PAD_HYS_MASK | IOMUXC_SW_PAD_CTL_PAD_PUS(1) |
  325. IOMUXC_SW_PAD_CTL_PAD_DSE(strength));
  326. IOMUXC_SetPinConfig(IOMUXC_GPIO_SD_B0_01_USDHC1_CLK,
  327. IOMUXC_SW_PAD_CTL_PAD_SPEED(speed) | IOMUXC_SW_PAD_CTL_PAD_SRE_MASK |
  328. IOMUXC_SW_PAD_CTL_PAD_HYS_MASK | IOMUXC_SW_PAD_CTL_PAD_PUS(0) |
  329. IOMUXC_SW_PAD_CTL_PAD_DSE(strength));
  330. IOMUXC_SetPinConfig(IOMUXC_GPIO_SD_B0_02_USDHC1_DATA0,
  331. IOMUXC_SW_PAD_CTL_PAD_SPEED(speed) | IOMUXC_SW_PAD_CTL_PAD_SRE_MASK |
  332. IOMUXC_SW_PAD_CTL_PAD_PKE_MASK | IOMUXC_SW_PAD_CTL_PAD_PUE_MASK |
  333. IOMUXC_SW_PAD_CTL_PAD_HYS_MASK | IOMUXC_SW_PAD_CTL_PAD_PUS(1) |
  334. IOMUXC_SW_PAD_CTL_PAD_DSE(strength));
  335. IOMUXC_SetPinConfig(IOMUXC_GPIO_SD_B0_03_USDHC1_DATA1,
  336. IOMUXC_SW_PAD_CTL_PAD_SPEED(speed) | IOMUXC_SW_PAD_CTL_PAD_SRE_MASK |
  337. IOMUXC_SW_PAD_CTL_PAD_PKE_MASK | IOMUXC_SW_PAD_CTL_PAD_PUE_MASK |
  338. IOMUXC_SW_PAD_CTL_PAD_HYS_MASK | IOMUXC_SW_PAD_CTL_PAD_PUS(1) |
  339. IOMUXC_SW_PAD_CTL_PAD_DSE(strength));
  340. IOMUXC_SetPinConfig(IOMUXC_GPIO_SD_B0_04_USDHC1_DATA2,
  341. IOMUXC_SW_PAD_CTL_PAD_SPEED(speed) | IOMUXC_SW_PAD_CTL_PAD_SRE_MASK |
  342. IOMUXC_SW_PAD_CTL_PAD_PKE_MASK | IOMUXC_SW_PAD_CTL_PAD_PUE_MASK |
  343. IOMUXC_SW_PAD_CTL_PAD_HYS_MASK | IOMUXC_SW_PAD_CTL_PAD_PUS(1) |
  344. IOMUXC_SW_PAD_CTL_PAD_DSE(strength));
  345. IOMUXC_SetPinConfig(IOMUXC_GPIO_SD_B0_05_USDHC1_DATA3,
  346. IOMUXC_SW_PAD_CTL_PAD_SPEED(speed) | IOMUXC_SW_PAD_CTL_PAD_SRE_MASK |
  347. IOMUXC_SW_PAD_CTL_PAD_PKE_MASK | IOMUXC_SW_PAD_CTL_PAD_PUE_MASK |
  348. IOMUXC_SW_PAD_CTL_PAD_HYS_MASK | IOMUXC_SW_PAD_CTL_PAD_PUS(1) |
  349. IOMUXC_SW_PAD_CTL_PAD_DSE(strength));
  350. }
  351. void BOARD_MMC_Pin_Config(uint32_t speed, uint32_t strength)
  352. {
  353. }