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