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 
16 /*******************************************************************************
17  * Variables
18  ******************************************************************************/
19 
20 /*******************************************************************************
21  * Code
22  ******************************************************************************/
23 /* Initialize debug console. */
BOARD_InitDebugConsole(void)24 void BOARD_InitDebugConsole(void)
25 {
26     /* attach 12 MHz clock to FLEXCOMM0 (debug console) */
27     CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
28 
29     RESET_ClearPeripheralReset(BOARD_DEBUG_UART_RST);
30 
31     uint32_t uartClkSrcFreq = BOARD_DEBUG_UART_CLK_FREQ;
32 
33     DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, uartClkSrcFreq);
34 }
35 
36 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
BOARD_I2C_Init(I2C_Type * base,uint32_t clkSrc_Hz)37 void BOARD_I2C_Init(I2C_Type *base, uint32_t clkSrc_Hz)
38 {
39     i2c_master_config_t i2cConfig = {0};
40 
41     I2C_MasterGetDefaultConfig(&i2cConfig);
42     I2C_MasterInit(base, &i2cConfig, clkSrc_Hz);
43 }
44 
BOARD_I2C_Send(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * txBuff,uint8_t txBuffSize)45 status_t BOARD_I2C_Send(I2C_Type *base,
46                         uint8_t deviceAddress,
47                         uint32_t subAddress,
48                         uint8_t subaddressSize,
49                         uint8_t *txBuff,
50                         uint8_t txBuffSize)
51 {
52     i2c_master_transfer_t masterXfer;
53 
54     /* Prepare transfer structure. */
55     masterXfer.slaveAddress   = deviceAddress;
56     masterXfer.direction      = kI2C_Write;
57     masterXfer.subaddress     = subAddress;
58     masterXfer.subaddressSize = subaddressSize;
59     masterXfer.data           = txBuff;
60     masterXfer.dataSize       = txBuffSize;
61     masterXfer.flags          = kI2C_TransferDefaultFlag;
62 
63     return I2C_MasterTransferBlocking(base, &masterXfer);
64 }
65 
BOARD_I2C_Receive(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)66 status_t BOARD_I2C_Receive(I2C_Type *base,
67                            uint8_t deviceAddress,
68                            uint32_t subAddress,
69                            uint8_t subaddressSize,
70                            uint8_t *rxBuff,
71                            uint8_t rxBuffSize)
72 {
73     i2c_master_transfer_t masterXfer;
74 
75     /* Prepare transfer structure. */
76     masterXfer.slaveAddress   = deviceAddress;
77     masterXfer.subaddress     = subAddress;
78     masterXfer.subaddressSize = subaddressSize;
79     masterXfer.data           = rxBuff;
80     masterXfer.dataSize       = rxBuffSize;
81     masterXfer.direction      = kI2C_Read;
82     masterXfer.flags          = kI2C_TransferDefaultFlag;
83 
84     return I2C_MasterTransferBlocking(base, &masterXfer);
85 }
86 
BOARD_Accel_I2C_Init(void)87 void BOARD_Accel_I2C_Init(void)
88 {
89     BOARD_I2C_Init(BOARD_ACCEL_I2C_BASEADDR, BOARD_ACCEL_I2C_CLOCK_FREQ);
90 }
91 
BOARD_Accel_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint32_t txBuff)92 status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
93 {
94     uint8_t data = (uint8_t)txBuff;
95 
96     return BOARD_I2C_Send(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
97 }
98 
BOARD_Accel_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)99 status_t BOARD_Accel_I2C_Receive(
100     uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
101 {
102     return BOARD_I2C_Receive(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
103 }
104 
BOARD_Codec_I2C_Init(void)105 void BOARD_Codec_I2C_Init(void)
106 {
107     BOARD_I2C_Init(BOARD_CODEC_I2C_BASEADDR, BOARD_CODEC_I2C_CLOCK_FREQ);
108 }
109 
BOARD_Codec_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)110 status_t BOARD_Codec_I2C_Send(
111     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
112 {
113     return BOARD_I2C_Send(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
114                           txBuffSize);
115 }
116 
BOARD_Codec_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)117 status_t BOARD_Codec_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_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
121 }
122 #endif /* SDK_I2C_BASED_COMPONENT_USED */
123