ISSDK  1.8
IoT Sensing Software Development Kit
board.c
Go to the documentation of this file.
1 /*
2  * Copyright 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. */
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. */
45 {
46  uint32_t uartClkSrcFreq = BOARD_DebugConsoleSrcFreq();
47 
49 }
50 
51 /* MPU configuration. */
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 core_cm7.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 core_cm7.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 Attribtue 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  * core_cm7.h.
104  */
105 
106  /* Region 0 setting: Memory with Device type, not shareable, non-cacheable. */
107  MPU->RBAR = ARM_MPU_RBAR(0, 0xC0000000U);
108  MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_512MB);
109 
110  /* Region 1 setting: Memory with Device type, not shareable, non-cacheable. */
111  MPU->RBAR = ARM_MPU_RBAR(1, 0x80000000U);
112  MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_1GB);
113 
114 /* Region 2 setting */
115 #if defined(XIP_EXTERNAL_FLASH) && (XIP_EXTERNAL_FLASH == 1)
116  /* Setting Memory with Normal type, not shareable, outer/inner write back. */
117  MPU->RBAR = ARM_MPU_RBAR(2, 0x60000000U);
118  MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_RO, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_16MB);
119 #else
120  /* Setting Memory with Device type, not shareable, non-cacheable. */
121  MPU->RBAR = ARM_MPU_RBAR(2, 0x60000000U);
122  MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_RO, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_16MB);
123 #endif
124 
125  /* Region 3 setting: Memory with Device type, not shareable, non-cacheable. */
126  MPU->RBAR = ARM_MPU_RBAR(3, 0x00000000U);
127  MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 2, 0, 0, 0, 0, ARM_MPU_REGION_SIZE_1GB);
128 
129  /* Region 4 setting: Memory with Normal type, not shareable, outer/inner write back */
130  MPU->RBAR = ARM_MPU_RBAR(4, 0x00000000U);
131  MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_32KB);
132 
133  /* Region 5 setting: Memory with Normal type, not shareable, outer/inner write back */
134  MPU->RBAR = ARM_MPU_RBAR(5, 0x20000000U);
135  MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_32KB);
136 
137 #if defined(OCRAM_IS_SHAREABLE)
138  /* Region 6 setting: Memory with Normal type, shareable, outer/inner write back */
139  MPU->RBAR = ARM_MPU_RBAR(6, 0x20200000U);
140  MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 1, 1, 1, 0, ARM_MPU_REGION_SIZE_64KB);
141 #else
142  /* Region 6 setting: Memory with Normal type, not shareable, outer/inner write back */
143  MPU->RBAR = ARM_MPU_RBAR(6, 0x20200000U);
144  MPU->RASR = ARM_MPU_RASR(0, ARM_MPU_AP_FULL, 0, 0, 1, 1, 0, ARM_MPU_REGION_SIZE_64KB);
145 #endif
146 
147  /* Enable MPU */
148  ARM_MPU_Enable(MPU_CTRL_PRIVDEFENA_Msk);
149 
150  /* Enable I cache and D cache */
151  SCB_EnableDCache();
152  SCB_EnableICache();
153 }
154 
155 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
156 void BOARD_LPI2C_Init(LPI2C_Type *base, uint32_t clkSrc_Hz)
157 {
158  lpi2c_master_config_t lpi2cConfig = {0};
159 
160  /*
161  * lpi2cConfig.debugEnable = false;
162  * lpi2cConfig.ignoreAck = false;
163  * lpi2cConfig.pinConfig = kLPI2C_2PinOpenDrain;
164  * lpi2cConfig.baudRate_Hz = 100000U;
165  * lpi2cConfig.busIdleTimeout_ns = 0;
166  * lpi2cConfig.pinLowTimeout_ns = 0;
167  * lpi2cConfig.sdaGlitchFilterWidth_ns = 0;
168  * lpi2cConfig.sclGlitchFilterWidth_ns = 0;
169  */
170  LPI2C_MasterGetDefaultConfig(&lpi2cConfig);
171  LPI2C_MasterInit(base, &lpi2cConfig, clkSrc_Hz);
172 }
173 
174 status_t BOARD_LPI2C_Send(LPI2C_Type *base,
175  uint8_t deviceAddress,
176  uint32_t subAddress,
177  uint8_t subAddressSize,
178  uint8_t *txBuff,
179  uint8_t txBuffSize)
180 {
181  status_t reVal;
182 
183  /* Send master blocking data to slave */
184  reVal = LPI2C_MasterStart(base, deviceAddress, kLPI2C_Write);
185  if (kStatus_Success == reVal)
186  {
187  while (LPI2C_MasterGetStatusFlags(base) & kLPI2C_MasterNackDetectFlag)
188  {
189  }
190 
191  reVal = LPI2C_MasterSend(base, &subAddress, subAddressSize);
192  if (reVal != kStatus_Success)
193  {
194  return reVal;
195  }
196 
197  reVal = LPI2C_MasterSend(base, txBuff, txBuffSize);
198  if (reVal != kStatus_Success)
199  {
200  return reVal;
201  }
202 
203  reVal = LPI2C_MasterStop(base);
204  if (reVal != kStatus_Success)
205  {
206  return reVal;
207  }
208  }
209 
210  return reVal;
211 }
212 
213 status_t BOARD_LPI2C_Receive(LPI2C_Type *base,
214  uint8_t deviceAddress,
215  uint32_t subAddress,
216  uint8_t subAddressSize,
217  uint8_t *rxBuff,
218  uint8_t rxBuffSize)
219 {
220  status_t reVal;
221 
222  reVal = LPI2C_MasterStart(base, deviceAddress, kLPI2C_Write);
223  if (kStatus_Success == reVal)
224  {
225  while (LPI2C_MasterGetStatusFlags(base) & kLPI2C_MasterNackDetectFlag)
226  {
227  }
228 
229  reVal = LPI2C_MasterSend(base, &subAddress, subAddressSize);
230  if (reVal != kStatus_Success)
231  {
232  return reVal;
233  }
234 
235  reVal = LPI2C_MasterRepeatedStart(base, deviceAddress, kLPI2C_Read);
236  if (reVal != kStatus_Success)
237  {
238  return reVal;
239  }
240 
241  reVal = LPI2C_MasterReceive(base, rxBuff, rxBuffSize);
242  if (reVal != kStatus_Success)
243  {
244  return reVal;
245  }
246 
247  reVal = LPI2C_MasterStop(base);
248  if (reVal != kStatus_Success)
249  {
250  return reVal;
251  }
252  }
253  return reVal;
254 }
255 
256 void BOARD_Accel_I2C_Init(void)
257 {
259 }
260 
261 status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
262 {
263  uint8_t data = (uint8_t)txBuff;
264 
265  return BOARD_LPI2C_Send(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
266 }
267 
268 status_t BOARD_Accel_I2C_Receive(
269  uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
270 {
271  return BOARD_LPI2C_Receive(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
272 }
273 
274 void BOARD_Codec_I2C_Init(void)
275 {
277 }
278 
279 status_t BOARD_Codec_I2C_Send(
280  uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
281 {
282  return BOARD_LPI2C_Send(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
283  txBuffSize);
284 }
285 
286 status_t BOARD_Codec_I2C_Receive(
287  uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
288 {
289  return BOARD_LPI2C_Receive(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
290 }
291 #endif /*SDK_I2C_BASED_COMPONENT_USED */
#define BOARD_ACCEL_I2C_CLOCK_FREQ
Definition: board.h:35
#define BOARD_CODEC_I2C_CLOCK_FREQ
Definition: board.h:56
uint32_t BOARD_DebugConsoleSrcFreq(void)
Definition: board.c:25
#define BOARD_DEBUG_UART_BAUDRATE
Definition: board.h:31
#define BOARD_ACCEL_I2C_BASEADDR
Definition: board.h:129
uint8_t data[FXLS8962_DATA_SIZE]
#define BOARD_DEBUG_UART_INSTANCE
Definition: board.h:26
#define BOARD_CODEC_I2C_BASEADDR
Definition: board.h:55
void BOARD_ConfigMPU(void)
Definition: board.c:52
void BOARD_InitDebugConsole(void)
Definition: board.c:15
#define BOARD_DEBUG_UART_TYPE
Definition: board.h:23