1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2018 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include <stdint.h>
10 #include "fsl_common.h"
11 #include "clock_config.h"
12 #include "board.h"
13 #include "fsl_debug_console.h"
14 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
15 #include "fsl_i2c.h"
16 #endif /* SDK_I2C_BASED_COMPONENT_USED */
17 #if defined BOARD_USE_CODEC
18 #include "fsl_wm8904.h"
19 #endif
20 /*******************************************************************************
21  * Variables
22  ******************************************************************************/
23 
24 /* Clock rate on the CLKIN pin */
25 const uint32_t ExtClockIn = BOARD_EXTCLKINRATE;
26 
27 /*******************************************************************************
28  * Code
29  ******************************************************************************/
30 /* Initialize debug console. */
BOARD_InitDebugConsole(void)31 status_t BOARD_InitDebugConsole(void)
32 {
33 #if ((SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK) || defined(SDK_DEBUGCONSOLE_UART))
34     status_t result;
35     /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
36     CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
37     RESET_PeripheralReset(BOARD_DEBUG_UART_RST);
38     result = DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE,
39                              BOARD_DEBUG_UART_CLK_FREQ);
40     assert(kStatus_Success == result);
41     return result;
42 #else
43     return kStatus_Success;
44 #endif
45 }
46 
BOARD_InitDebugConsole_Core1(void)47 status_t BOARD_InitDebugConsole_Core1(void)
48 {
49 #if ((SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK) || defined(SDK_DEBUGCONSOLE_UART))
50     status_t result;
51     /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
52     CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH_CORE1);
53     RESET_PeripheralReset(BOARD_DEBUG_UART_RST_CORE1);
54     result = DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE_CORE1, BOARD_DEBUG_UART_BAUDRATE_CORE1,
55                              BOARD_DEBUG_UART_TYPE_CORE1, BOARD_DEBUG_UART_CLK_FREQ_CORE1);
56     assert(kStatus_Success == result);
57     return result;
58 #else
59     return kStatus_Success;
60 #endif
61 }
62 
63 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
BOARD_I2C_Init(I2C_Type * base,uint32_t clkSrc_Hz)64 void BOARD_I2C_Init(I2C_Type *base, uint32_t clkSrc_Hz)
65 {
66     i2c_master_config_t i2cConfig = {0};
67 
68     I2C_MasterGetDefaultConfig(&i2cConfig);
69     I2C_MasterInit(base, &i2cConfig, clkSrc_Hz);
70 }
71 
BOARD_I2C_Send(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * txBuff,uint8_t txBuffSize)72 status_t BOARD_I2C_Send(I2C_Type *base,
73                         uint8_t deviceAddress,
74                         uint32_t subAddress,
75                         uint8_t subaddressSize,
76                         uint8_t *txBuff,
77                         uint8_t txBuffSize)
78 {
79     i2c_master_transfer_t masterXfer;
80 
81     /* Prepare transfer structure. */
82     masterXfer.slaveAddress   = deviceAddress;
83     masterXfer.direction      = kI2C_Write;
84     masterXfer.subaddress     = subAddress;
85     masterXfer.subaddressSize = subaddressSize;
86     masterXfer.data           = txBuff;
87     masterXfer.dataSize       = txBuffSize;
88     masterXfer.flags          = kI2C_TransferDefaultFlag;
89 
90     return I2C_MasterTransferBlocking(base, &masterXfer);
91 }
92 
BOARD_I2C_Receive(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)93 status_t BOARD_I2C_Receive(I2C_Type *base,
94                            uint8_t deviceAddress,
95                            uint32_t subAddress,
96                            uint8_t subaddressSize,
97                            uint8_t *rxBuff,
98                            uint8_t rxBuffSize)
99 {
100     i2c_master_transfer_t masterXfer;
101 
102     /* Prepare transfer structure. */
103     masterXfer.slaveAddress   = deviceAddress;
104     masterXfer.subaddress     = subAddress;
105     masterXfer.subaddressSize = subaddressSize;
106     masterXfer.data           = rxBuff;
107     masterXfer.dataSize       = rxBuffSize;
108     masterXfer.direction      = kI2C_Read;
109     masterXfer.flags          = kI2C_TransferDefaultFlag;
110 
111     return I2C_MasterTransferBlocking(base, &masterXfer);
112 }
113 
BOARD_Codec_I2C_Init(void)114 void BOARD_Codec_I2C_Init(void)
115 {
116     BOARD_I2C_Init(BOARD_CODEC_I2C_BASEADDR, BOARD_CODEC_I2C_CLOCK_FREQ);
117 }
118 
BOARD_Codec_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)119 status_t BOARD_Codec_I2C_Send(
120     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
121 {
122     return BOARD_I2C_Send(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
123                           txBuffSize);
124 }
125 
BOARD_Codec_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)126 status_t BOARD_Codec_I2C_Receive(
127     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
128 {
129     return BOARD_I2C_Receive(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
130 }
131 #endif /* SDK_I2C_BASED_COMPONENT_USED */
132