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