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