1 /*
2  * Copyright 2021-2022 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include <stdint.h>
10 #include "fsl_common.h"
11 #include "fsl_debug_console.h"
12 #include "board.h"
13 #include "fsl_clock.h"
14 #include "fsl_flexspi.h"
15 #include "fsl_cache.h"
16 #include "fsl_power.h"
17 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
18 #include "fsl_i2c.h"
19 #endif /* SDK_I2C_BASED_COMPONENT_USED */
20 #if defined BOARD_USE_CODEC
21 #include "fsl_i3c.h"
22 #endif
23 
24 /*******************************************************************************
25  * Definitions
26  ******************************************************************************/
27 #define BOARD_FLEXSPI_DLL_LOCK_RETRY (10)
28 #if (__ARM_FEATURE_CMSE & 0x2) && defined(__ARMCC_VERSION)
29 /* For the Trustzone examples built with ARM Compiler, the RAM targets will also run in flash(XIP) to do initialization
30  * copy. */
31 #define BOARD_IS_XIP_FLEXSPI() (true)
32 #else
33 #define BOARD_IS_XIP_FLEXSPI()                                                                                  \
34     ((((uint32_t)BOARD_InitDebugConsole >= 0x08000000U) && ((uint32_t)BOARD_InitDebugConsole < 0x10000000U)) || \
35      (((uint32_t)BOARD_InitDebugConsole >= 0x18000000U) && ((uint32_t)BOARD_InitDebugConsole < 0x20000000U)))
36 #endif
37 /*******************************************************************************
38  * Variables
39  ******************************************************************************/
40 static status_t flexspi_hyper_ram_write_mcr(FLEXSPI_Type *base, uint8_t regAddr, uint32_t *mrVal);
41 static status_t flexspi_hyper_ram_get_mcr(FLEXSPI_Type *base, uint8_t regAddr, uint32_t *mrVal);
42 static status_t flexspi_hyper_ram_reset(FLEXSPI_Type *base);
43 AT_QUICKACCESS_SECTION_DATA(static uint32_t s_ispPin[3]);
44 AT_QUICKACCESS_SECTION_DATA(static uint32_t s_flexspiPin[6]);
45 /*******************************************************************************
46  * Code
47  ******************************************************************************/
48 /* Initialize debug console. */
BOARD_InitDebugConsole(void)49 void BOARD_InitDebugConsole(void)
50 {
51     uint32_t uartClkSrcFreq;
52 
53     /* attach FRG0 clock to FLEXCOMM0 (debug console) */
54     CLOCK_SetFRGClock(BOARD_DEBUG_UART_FRG_CLK);
55     CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
56 
57     uartClkSrcFreq = BOARD_DEBUG_UART_CLK_FREQ;
58 
59     DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, uartClkSrcFreq);
60 }
61 
flexspi_hyper_ram_write_mcr(FLEXSPI_Type * base,uint8_t regAddr,uint32_t * mrVal)62 static status_t flexspi_hyper_ram_write_mcr(FLEXSPI_Type *base, uint8_t regAddr, uint32_t *mrVal)
63 {
64     flexspi_transfer_t flashXfer;
65     status_t status;
66 
67     /* Write data */
68     flashXfer.deviceAddress = regAddr;
69     flashXfer.port          = kFLEXSPI_PortA1;
70     flashXfer.cmdType       = kFLEXSPI_Write;
71     flashXfer.SeqNumber     = 1;
72     flashXfer.seqIndex      = 3;
73     flashXfer.data          = mrVal;
74     flashXfer.dataSize      = 1;
75 
76     status = FLEXSPI_TransferBlocking(base, &flashXfer);
77 
78     return status;
79 }
80 
flexspi_hyper_ram_get_mcr(FLEXSPI_Type * base,uint8_t regAddr,uint32_t * mrVal)81 static status_t flexspi_hyper_ram_get_mcr(FLEXSPI_Type *base, uint8_t regAddr, uint32_t *mrVal)
82 {
83     flexspi_transfer_t flashXfer;
84     status_t status;
85 
86     /* Read data */
87     flashXfer.deviceAddress = regAddr;
88     flashXfer.port          = kFLEXSPI_PortA1;
89     flashXfer.cmdType       = kFLEXSPI_Read;
90     flashXfer.SeqNumber     = 1;
91     flashXfer.seqIndex      = 2;
92     flashXfer.data          = mrVal;
93     flashXfer.dataSize      = 2;
94 
95     status = FLEXSPI_TransferBlocking(base, &flashXfer);
96 
97     return status;
98 }
99 
flexspi_hyper_ram_reset(FLEXSPI_Type * base)100 static status_t flexspi_hyper_ram_reset(FLEXSPI_Type *base)
101 {
102     flexspi_transfer_t flashXfer;
103     status_t status;
104 
105     /* Write data */
106     flashXfer.deviceAddress = 0x0U;
107     flashXfer.port          = kFLEXSPI_PortA1;
108     flashXfer.cmdType       = kFLEXSPI_Command;
109     flashXfer.SeqNumber     = 1;
110     flashXfer.seqIndex      = 4;
111 
112     status = FLEXSPI_TransferBlocking(base, &flashXfer);
113 
114     if (status == kStatus_Success)
115     {
116         /* for loop of 50000 is about 1ms (@200 MHz CPU) */
117         for (uint32_t i = 2000000U; i > 0; i--)
118         {
119             __NOP();
120         }
121     }
122     return status;
123 }
124 
125 /* Initialize psram. */
BOARD_InitPsRam(void)126 status_t BOARD_InitPsRam(void)
127 {
128     flexspi_device_config_t deviceconfig = {
129         .flexspiRootClk       = 396000000, /* 396MHZ SPI serial clock, DDR serial clock 198M */
130         .isSck2Enabled        = false,
131         .flashSize            = 0x2000, /* 64Mb/KByte */
132         .CSIntervalUnit       = kFLEXSPI_CsIntervalUnit1SckCycle,
133         .CSInterval           = 5,
134         .CSHoldTime           = 3,
135         .CSSetupTime          = 3,
136         .dataValidTime        = 1,
137         .columnspace          = 0,
138         .enableWordAddress    = false,
139         .AWRSeqIndex          = 1,
140         .AWRSeqNumber         = 1,
141         .ARDSeqIndex          = 0,
142         .ARDSeqNumber         = 1,
143         .AHBWriteWaitUnit     = kFLEXSPI_AhbWriteWaitUnit2AhbCycle,
144         .AHBWriteWaitInterval = 0,
145         .enableWriteMask      = true,
146     };
147 
148     uint32_t customLUT[64] = {
149         /* Read Data */
150         [0] =
151             FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_8PAD, 0x20, kFLEXSPI_Command_RADDR_DDR, kFLEXSPI_8PAD, 0x20),
152         [1] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_DUMMY_RWDS_DDR, kFLEXSPI_8PAD, 0x07, kFLEXSPI_Command_READ_DDR,
153                               kFLEXSPI_8PAD, 0x04),
154 
155         /* Write Data */
156         [4] =
157             FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_8PAD, 0xA0, kFLEXSPI_Command_RADDR_DDR, kFLEXSPI_8PAD, 0x20),
158         [5] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_DUMMY_RWDS_DDR, kFLEXSPI_8PAD, 0x07, kFLEXSPI_Command_WRITE_DDR,
159                               kFLEXSPI_8PAD, 0x04),
160 
161         /* Read Register */
162         [8] =
163             FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_8PAD, 0x40, kFLEXSPI_Command_RADDR_DDR, kFLEXSPI_8PAD, 0x20),
164         [9] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_DUMMY_RWDS_DDR, kFLEXSPI_8PAD, 0x07, kFLEXSPI_Command_READ_DDR,
165                               kFLEXSPI_8PAD, 0x04),
166 
167         /* Write Register */
168         [12] =
169             FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_8PAD, 0xC0, kFLEXSPI_Command_RADDR_DDR, kFLEXSPI_8PAD, 0x20),
170         [13] = FLEXSPI_LUT_SEQ(kFLEXSPI_Command_WRITE_DDR, kFLEXSPI_8PAD, 0x08, kFLEXSPI_Command_STOP, kFLEXSPI_1PAD,
171                                0x00),
172 
173         /* reset */
174         [16] =
175             FLEXSPI_LUT_SEQ(kFLEXSPI_Command_SDR, kFLEXSPI_8PAD, 0xFF, kFLEXSPI_Command_DUMMY_SDR, kFLEXSPI_8PAD, 0x03),
176 
177     };
178 
179     uint32_t mr0mr1[1];
180     uint32_t mr4mr8[1];
181     uint32_t mr0Val[1];
182     uint32_t mr4Val[1];
183     uint32_t mr8Val[1];
184     flexspi_config_t config;
185     cache64_config_t cacheCfg;
186     status_t status = kStatus_Success;
187 
188     POWER_DisablePD(kPDRUNCFG_APD_FLEXSPI_SRAM);
189     POWER_DisablePD(kPDRUNCFG_PPD_FLEXSPI_SRAM);
190     POWER_ApplyPD();
191 
192     CLOCK_AttachClk(kAUX0_PLL_to_FLEXSPI_CLK);
193     CLOCK_SetClkDiv(kCLOCK_DivFlexspiClk, 1);
194 
195     RESET_PeripheralReset(kFLEXSPI_RST_SHIFT_RSTn);
196     /* Explicitly enable FlexSPI clock for PSRAM loader case which need to set FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL. */
197     CLOCK_EnableClock(kCLOCK_Flexspi);
198 
199     /* As cache depends on FlexSPI power and clock, cache must be initialized after FlexSPI power/clock is set */
200     CACHE64_GetDefaultConfig(&cacheCfg);
201     CACHE64_Init(CACHE64_POLSEL, &cacheCfg);
202 #if BOARD_ENABLE_PSRAM_CACHE
203     CACHE64_EnableWriteBuffer(CACHE64, true);
204     CACHE64_EnableCache(CACHE64);
205 #endif
206 
207     /* Get FLEXSPI default settings and configure the flexspi. */
208     FLEXSPI_GetDefaultConfig(&config);
209 
210     /* Init FLEXSPI. */
211     config.rxSampleClock = kFLEXSPI_ReadSampleClkExternalInputFromDqsPad;
212     /*Set AHB buffer size for reading data through AHB bus. */
213     config.ahbConfig.enableAHBPrefetch    = true;
214     config.ahbConfig.enableAHBBufferable  = true;
215     config.ahbConfig.enableAHBCachable    = true;
216     config.ahbConfig.enableReadAddressOpt = true;
217     for (uint8_t i = 1; i < FSL_FEATURE_FLEXSPI_AHB_BUFFER_COUNT - 1; i++)
218     {
219         config.ahbConfig.buffer[i].bufferSize = 0;
220     }
221     /* FlexSPI has total 1KB RX buffer.
222      * Set DMA0 master to use AHB Rx Buffer0.
223      */
224     config.ahbConfig.buffer[0].masterIndex    = 4;   /* DMA0 */
225     config.ahbConfig.buffer[0].bufferSize     = 512; /* Allocate 512B bytes for DMA0 */
226     config.ahbConfig.buffer[0].enablePrefetch = true;
227     config.ahbConfig.buffer[0].priority       = 0;
228     /* All other masters use last buffer with 512B bytes. */
229     config.ahbConfig.buffer[FSL_FEATURE_FLEXSPI_AHB_BUFFER_COUNT - 1].bufferSize = 512;
230 #if !(defined(FSL_FEATURE_FLEXSPI_HAS_NO_MCR0_COMBINATIONEN) && FSL_FEATURE_FLEXSPI_HAS_NO_MCR0_COMBINATIONEN)
231     config.enableCombination = true;
232 #endif
233     FLEXSPI_Init(BOARD_FLEXSPI_PSRAM, &config);
234 
235     /* Configure flash settings according to serial flash feature. */
236     FLEXSPI_SetFlashConfig(BOARD_FLEXSPI_PSRAM, &deviceconfig, kFLEXSPI_PortA1);
237 
238     /* Update LUT table. */
239     FLEXSPI_UpdateLUT(BOARD_FLEXSPI_PSRAM, 0, customLUT, ARRAY_SIZE(customLUT));
240 
241     /* Do software reset. */
242     FLEXSPI_SoftwareReset(BOARD_FLEXSPI_PSRAM);
243 
244     /* Reset hyper ram. */
245     status = flexspi_hyper_ram_reset(BOARD_FLEXSPI_PSRAM);
246     if (status != kStatus_Success)
247     {
248         return status;
249     }
250 
251     status = flexspi_hyper_ram_get_mcr(BOARD_FLEXSPI_PSRAM, 0x0, mr0mr1);
252     if (status != kStatus_Success)
253     {
254         return status;
255     }
256 
257     status = flexspi_hyper_ram_get_mcr(BOARD_FLEXSPI_PSRAM, 0x4, mr4mr8);
258     if (status != kStatus_Success)
259     {
260         return status;
261     }
262 
263     /* Enable RBX, burst length set to 1K. - MR8 */
264     mr8Val[0] = (mr4mr8[0] & 0xFF00U) >> 8U;
265     mr8Val[0] = mr8Val[0] | 0x0F;
266     status    = flexspi_hyper_ram_write_mcr(BOARD_FLEXSPI_PSRAM, 0x8, mr8Val);
267     if (status != kStatus_Success)
268     {
269         return status;
270     }
271 
272     /* Set LC code to 0x04(LC=7, maximum frequency 200M) - MR0. */
273     mr0Val[0] = mr0mr1[0] & 0x00FFU;
274     mr0Val[0] = (mr0Val[0] & ~0x3CU) | (4U << 2U);
275     status    = flexspi_hyper_ram_write_mcr(BOARD_FLEXSPI_PSRAM, 0x0, mr0Val);
276     if (status != kStatus_Success)
277     {
278         return status;
279     }
280 
281     /* Set WLC code to 0x01(WLC=7, maximum frequency 200M) - MR4. */
282     mr4Val[0] = mr4mr8[0] & 0x00FFU;
283     mr4Val[0] = (mr4Val[0] & ~0xE0U) | (1U << 5U);
284     status    = flexspi_hyper_ram_write_mcr(BOARD_FLEXSPI_PSRAM, 0x4, mr4Val);
285     if (status != kStatus_Success)
286     {
287         return status;
288     }
289 
290     return status;
291 }
292 
BOARD_DeinitXip(FLEXSPI_Type * base)293 void BOARD_DeinitXip(FLEXSPI_Type *base)
294 {
295     /* Enable FLEXSPI clock again */
296     CLKCTL0->PSCCTL0_SET = CLKCTL0_PSCCTL0_SET_FLEXSPI_OTFAD_CLK_MASK;
297 
298     /* Enable FLEXSPI module */
299     base->MCR0 &= ~FLEXSPI_MCR0_MDIS_MASK;
300 
301     /* Wait until FLEXSPI is not busy */
302     while (!((base->STS0 & FLEXSPI_STS0_ARBIDLE_MASK) && (base->STS0 & FLEXSPI_STS0_SEQIDLE_MASK)))
303     {
304     }
305     /* Disable module during the reset procedure */
306     base->MCR0 |= FLEXSPI_MCR0_MDIS_MASK;
307 }
308 
BOARD_InitXip(FLEXSPI_Type * base)309 void BOARD_InitXip(FLEXSPI_Type *base)
310 {
311     uint32_t status;
312     uint32_t lastStatus;
313     uint32_t retry;
314     uint32_t mask = 0;
315 
316     /* Enable FLEXSPI module */
317     base->MCR0 &= ~FLEXSPI_MCR0_MDIS_MASK;
318 
319     base->MCR0 |= FLEXSPI_MCR0_SWRESET_MASK;
320     while (base->MCR0 & FLEXSPI_MCR0_SWRESET_MASK)
321     {
322     }
323 
324     /* Need to wait DLL locked if DLL enabled */
325     if (0U != (base->DLLCR[0] & FLEXSPI_DLLCR_DLLEN_MASK))
326     {
327         lastStatus = base->STS2;
328         retry      = BOARD_FLEXSPI_DLL_LOCK_RETRY;
329         /* Flash on port A */
330         if (((base->FLSHCR0[0] & FLEXSPI_FLSHCR0_FLSHSZ_MASK) > 0) ||
331             ((base->FLSHCR0[1] & FLEXSPI_FLSHCR0_FLSHSZ_MASK) > 0))
332         {
333             mask |= FLEXSPI_STS2_AREFLOCK_MASK | FLEXSPI_STS2_ASLVLOCK_MASK;
334         }
335         /* Flash on port B */
336         if (((base->FLSHCR0[2] & FLEXSPI_FLSHCR0_FLSHSZ_MASK) > 0) ||
337             ((base->FLSHCR0[3] & FLEXSPI_FLSHCR0_FLSHSZ_MASK) > 0))
338         {
339             mask |= FLEXSPI_STS2_BREFLOCK_MASK | FLEXSPI_STS2_BSLVLOCK_MASK;
340         }
341         /* Wait slave delay line locked and slave reference delay line locked. */
342         do
343         {
344             status = base->STS2;
345             if ((status & mask) == mask)
346             {
347                 /* Locked */
348                 retry = 100;
349                 break;
350             }
351             else if (status == lastStatus)
352             {
353                 /* Same delay cell number in calibration */
354                 retry--;
355             }
356             else
357             {
358                 retry      = BOARD_FLEXSPI_DLL_LOCK_RETRY;
359                 lastStatus = status;
360             }
361         } while (retry > 0);
362         /* According to ERR011377, need to delay at least 100 NOPs to ensure the DLL is locked. */
363         for (; retry > 0U; retry--)
364         {
365             __NOP();
366         }
367     }
368 }
369 
370 /* BOARD_SetFlexspiClock run in RAM used to configure FlexSPI clock source and divider when XIP. */
BOARD_SetFlexspiClock(uint32_t src,uint32_t divider)371 void BOARD_SetFlexspiClock(uint32_t src, uint32_t divider)
372 {
373     if ((CLKCTL0->FLEXSPIFCLKSEL != CLKCTL0_FLEXSPIFCLKSEL_SEL(src)) ||
374         ((CLKCTL0->FLEXSPIFCLKDIV & CLKCTL0_FLEXSPIFCLKDIV_DIV_MASK) != (divider - 1)))
375     {
376         if (BOARD_IS_XIP_FLEXSPI())
377         {
378             BOARD_DeinitXip(FLEXSPI);
379         }
380         /* Disable clock before changing clock source */
381         CLKCTL0->PSCCTL0_CLR = CLKCTL0_PSCCTL0_CLR_FLEXSPI_OTFAD_CLK_MASK;
382         /* Update flexspi clock. */
383         CLKCTL0->FLEXSPIFCLKSEL = CLKCTL0_FLEXSPIFCLKSEL_SEL(src);
384         CLKCTL0->FLEXSPIFCLKDIV |= CLKCTL0_FLEXSPIFCLKDIV_RESET_MASK; /* Reset the divider counter */
385         CLKCTL0->FLEXSPIFCLKDIV = CLKCTL0_FLEXSPIFCLKDIV_DIV(divider - 1);
386         while ((CLKCTL0->FLEXSPIFCLKDIV) & CLKCTL0_FLEXSPIFCLKDIV_REQFLAG_MASK)
387         {
388         }
389         /* Enable FLEXSPI clock again */
390         CLKCTL0->PSCCTL0_SET = CLKCTL0_PSCCTL0_SET_FLEXSPI_OTFAD_CLK_MASK;
391         if (BOARD_IS_XIP_FLEXSPI())
392         {
393             BOARD_InitXip(FLEXSPI);
394         }
395     }
396 }
397 
398 /* This function is used to change FlexSPI clock to a stable source before clock sources(Such as PLL and Main clock)
399  * updating in case XIP(execute code on FLEXSPI memory.) */
BOARD_FlexspiClockSafeConfig(void)400 void BOARD_FlexspiClockSafeConfig(void)
401 {
402     /* Move FLEXSPI clock source from main clock to FFRO to avoid instruction/data fetch issue in XIP when
403      * updating PLL and main clock.
404      */
405     BOARD_SetFlexspiClock(3U, 1U);
406 }
407 
BOARD_SetDeepSleepPinConfig(void)408 void BOARD_SetDeepSleepPinConfig(void)
409 {
410     /* Backup Pin configuration. */
411     s_ispPin[0]     = IOPCTL->PIO[1][15];
412     s_ispPin[1]     = IOPCTL->PIO[1][16];
413     s_ispPin[2]     = IOPCTL->PIO[1][17];
414     s_flexspiPin[0] = IOPCTL->PIO[1][29];
415     s_flexspiPin[1] = IOPCTL->PIO[2][19];
416     s_flexspiPin[2] = IOPCTL->PIO[1][11];
417     s_flexspiPin[3] = IOPCTL->PIO[1][12];
418     s_flexspiPin[4] = IOPCTL->PIO[1][13];
419     s_flexspiPin[5] = IOPCTL->PIO[1][14];
420 
421     /* Disable ISP Pin pull-ups and input buffers to avoid current leakage */
422     IOPCTL->PIO[1][15] = 0;
423     IOPCTL->PIO[1][16] = 0;
424     IOPCTL->PIO[1][17] = 0;
425 
426     /* Disable unnecessary input buffers */
427     IOPCTL->PIO[1][29] &= ~IOPCTL_PIO_IBENA_MASK;
428     IOPCTL->PIO[2][19] &= ~IOPCTL_PIO_IBENA_MASK;
429 
430     /* Enable pull-ups floating FlexSPI0 pins */
431     IOPCTL->PIO[1][11] |= IOPCTL_PIO_PUPDENA_MASK | IOPCTL_PIO_PUPDSEL_MASK;
432     IOPCTL->PIO[1][12] |= IOPCTL_PIO_PUPDENA_MASK | IOPCTL_PIO_PUPDSEL_MASK;
433     IOPCTL->PIO[1][13] |= IOPCTL_PIO_PUPDENA_MASK | IOPCTL_PIO_PUPDSEL_MASK;
434     IOPCTL->PIO[1][14] |= IOPCTL_PIO_PUPDENA_MASK | IOPCTL_PIO_PUPDSEL_MASK;
435 }
436 
BOARD_RestoreDeepSleepPinConfig(void)437 void BOARD_RestoreDeepSleepPinConfig(void)
438 {
439     /* Restore the Pin configuration. */
440     IOPCTL->PIO[1][15] = s_ispPin[0];
441     IOPCTL->PIO[1][16] = s_ispPin[1];
442     IOPCTL->PIO[1][17] = s_ispPin[2];
443 
444     IOPCTL->PIO[1][29] = s_flexspiPin[0];
445     IOPCTL->PIO[2][19] = s_flexspiPin[1];
446     IOPCTL->PIO[1][11] = s_flexspiPin[2];
447     IOPCTL->PIO[1][12] = s_flexspiPin[3];
448     IOPCTL->PIO[1][13] = s_flexspiPin[4];
449     IOPCTL->PIO[1][14] = s_flexspiPin[5];
450 }
451 
BOARD_EnterDeepSleep(const uint32_t exclude_from_pd[4])452 void BOARD_EnterDeepSleep(const uint32_t exclude_from_pd[4])
453 {
454     BOARD_SetDeepSleepPinConfig();
455     POWER_EnterDeepSleep(exclude_from_pd);
456     BOARD_RestoreDeepSleepPinConfig();
457 }
458 
BOARD_EnterDeepPowerDown(const uint32_t exclude_from_pd[4])459 void BOARD_EnterDeepPowerDown(const uint32_t exclude_from_pd[4])
460 {
461     BOARD_SetDeepSleepPinConfig();
462     POWER_EnterDeepPowerDown(exclude_from_pd);
463     /* After deep power down wakeup, the code will restart and cannot reach here. */
464     BOARD_RestoreDeepSleepPinConfig();
465 }
466 
467 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
BOARD_I2C_Init(I2C_Type * base,uint32_t clkSrc_Hz)468 void BOARD_I2C_Init(I2C_Type *base, uint32_t clkSrc_Hz)
469 {
470     i2c_master_config_t i2cConfig = {0};
471 
472     I2C_MasterGetDefaultConfig(&i2cConfig);
473     I2C_MasterInit(base, &i2cConfig, clkSrc_Hz);
474 }
475 
BOARD_I2C_Send(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * txBuff,uint8_t txBuffSize)476 status_t BOARD_I2C_Send(I2C_Type *base,
477                         uint8_t deviceAddress,
478                         uint32_t subAddress,
479                         uint8_t subaddressSize,
480                         uint8_t *txBuff,
481                         uint8_t txBuffSize)
482 {
483     i2c_master_transfer_t masterXfer;
484 
485     /* Prepare transfer structure. */
486     masterXfer.slaveAddress   = deviceAddress;
487     masterXfer.direction      = kI2C_Write;
488     masterXfer.subaddress     = subAddress;
489     masterXfer.subaddressSize = subaddressSize;
490     masterXfer.data           = txBuff;
491     masterXfer.dataSize       = txBuffSize;
492     masterXfer.flags          = kI2C_TransferDefaultFlag;
493 
494     return I2C_MasterTransferBlocking(base, &masterXfer);
495 }
496 
BOARD_I2C_Receive(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)497 status_t BOARD_I2C_Receive(I2C_Type *base,
498                            uint8_t deviceAddress,
499                            uint32_t subAddress,
500                            uint8_t subaddressSize,
501                            uint8_t *rxBuff,
502                            uint8_t rxBuffSize)
503 {
504     i2c_master_transfer_t masterXfer;
505 
506     /* Prepare transfer structure. */
507     masterXfer.slaveAddress   = deviceAddress;
508     masterXfer.subaddress     = subAddress;
509     masterXfer.subaddressSize = subaddressSize;
510     masterXfer.data           = rxBuff;
511     masterXfer.dataSize       = rxBuffSize;
512     masterXfer.direction      = kI2C_Read;
513     masterXfer.flags          = kI2C_TransferDefaultFlag;
514 
515     return I2C_MasterTransferBlocking(base, &masterXfer);
516 }
517 #endif
518 
519 #if defined BOARD_USE_CODEC
BOARD_Codec_I2C_Init(void)520 void BOARD_Codec_I2C_Init(void)
521 {
522     BOARD_I2C_Init(BOARD_CODEC_I2C_BASEADDR, BOARD_CODEC_I2C_CLOCK_FREQ);
523 }
524 
BOARD_Codec_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)525 status_t BOARD_Codec_I2C_Send(
526     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
527 {
528     return BOARD_I2C_Send(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
529                           txBuffSize);
530 }
531 
BOARD_Codec_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)532 status_t BOARD_Codec_I2C_Receive(
533     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
534 {
535     return BOARD_I2C_Receive(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
536 }
537 #endif
538 
539 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
BOARD_PMIC_I2C_Init(void)540 void BOARD_PMIC_I2C_Init(void)
541 {
542     BOARD_I2C_Init(BOARD_PMIC_I2C_BASEADDR, BOARD_PMIC_I2C_CLOCK_FREQ);
543 }
544 
BOARD_PMIC_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)545 status_t BOARD_PMIC_I2C_Send(
546     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
547 {
548     return BOARD_I2C_Send(BOARD_PMIC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
549                           txBuffSize);
550 }
551 
BOARD_PMIC_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)552 status_t BOARD_PMIC_I2C_Receive(
553     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
554 {
555     return BOARD_I2C_Receive(BOARD_PMIC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
556 }
557 
BOARD_Accel_I2C_Init(void)558 void BOARD_Accel_I2C_Init(void)
559 {
560     BOARD_I2C_Init(BOARD_ACCEL_I2C_BASEADDR, BOARD_ACCEL_I2C_CLOCK_FREQ);
561 }
562 
BOARD_Accel_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint32_t txBuff)563 status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
564 {
565     uint8_t data = (uint8_t)txBuff;
566 
567     return BOARD_I2C_Send(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
568 }
569 
BOARD_Accel_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)570 status_t BOARD_Accel_I2C_Receive(
571     uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
572 {
573     return BOARD_I2C_Receive(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
574 }
575 
576 #endif /* SDK_I2C_BASED_COMPONENT_USED */
577