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 "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_lpi2c.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     CLOCK_SetIpSrc(kCLOCK_Lpuart0, BOARD_DEBUG_UART_CLKSRC);
27 
28     uint32_t uartClkSrcFreq = BOARD_DEBUG_UART_CLK_FREQ;
29 
30     DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, uartClkSrcFreq);
31 }
32 
33 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
BOARD_LPI2C_Init(LPI2C_Type * base,uint32_t clkSrc_Hz)34 void BOARD_LPI2C_Init(LPI2C_Type *base, uint32_t clkSrc_Hz)
35 {
36     lpi2c_master_config_t lpi2cConfig = {0};
37 
38     /*
39      * lpi2cConfig.debugEnable = false;
40      * lpi2cConfig.ignoreAck = false;
41      * lpi2cConfig.pinConfig = kLPI2C_2PinOpenDrain;
42      * lpi2cConfig.baudRate_Hz = 100000U;
43      * lpi2cConfig.busIdleTimeout_ns = 0;
44      * lpi2cConfig.pinLowTimeout_ns = 0;
45      * lpi2cConfig.sdaGlitchFilterWidth_ns = 0;
46      * lpi2cConfig.sclGlitchFilterWidth_ns = 0;
47      */
48     LPI2C_MasterGetDefaultConfig(&lpi2cConfig);
49     LPI2C_MasterInit(base, &lpi2cConfig, clkSrc_Hz);
50 }
51 
BOARD_LPI2C_Send(LPI2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * txBuff,uint8_t txBuffSize)52 status_t BOARD_LPI2C_Send(LPI2C_Type *base,
53                           uint8_t deviceAddress,
54                           uint32_t subAddress,
55                           uint8_t subAddressSize,
56                           uint8_t *txBuff,
57                           uint8_t txBuffSize)
58 {
59     lpi2c_master_transfer_t xfer;
60 
61     xfer.flags          = kLPI2C_TransferDefaultFlag;
62     xfer.slaveAddress   = deviceAddress;
63     xfer.direction      = kLPI2C_Write;
64     xfer.subaddress     = subAddress;
65     xfer.subaddressSize = subAddressSize;
66     xfer.data           = txBuff;
67     xfer.dataSize       = txBuffSize;
68 
69     return LPI2C_MasterTransferBlocking(base, &xfer);
70 }
71 
BOARD_LPI2C_Receive(LPI2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)72 status_t BOARD_LPI2C_Receive(LPI2C_Type *base,
73                              uint8_t deviceAddress,
74                              uint32_t subAddress,
75                              uint8_t subAddressSize,
76                              uint8_t *rxBuff,
77                              uint8_t rxBuffSize)
78 {
79     lpi2c_master_transfer_t xfer;
80 
81     xfer.flags          = kLPI2C_TransferDefaultFlag;
82     xfer.slaveAddress   = deviceAddress;
83     xfer.direction      = kLPI2C_Read;
84     xfer.subaddress     = subAddress;
85     xfer.subaddressSize = subAddressSize;
86     xfer.data           = rxBuff;
87     xfer.dataSize       = rxBuffSize;
88 
89     return LPI2C_MasterTransferBlocking(base, &xfer);
90 }
91 
BOARD_Accel_I2C_Init(void)92 void BOARD_Accel_I2C_Init(void)
93 {
94     BOARD_LPI2C_Init(BOARD_ACCEL_I2C_BASEADDR, BOARD_ACCEL_I2C_CLOCK_FREQ);
95 }
96 
BOARD_Accel_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint32_t txBuff)97 status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
98 {
99     uint8_t data = (uint8_t)txBuff;
100 
101     return BOARD_LPI2C_Send(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
102 }
103 
BOARD_Accel_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)104 status_t BOARD_Accel_I2C_Receive(
105     uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
106 {
107     return BOARD_LPI2C_Receive(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
108 }
109 
BOARD_Codec_I2C_Init(void)110 void BOARD_Codec_I2C_Init(void)
111 {
112     BOARD_LPI2C_Init(BOARD_CODEC_I2C_BASEADDR, BOARD_CODEC_I2C_CLOCK_FREQ);
113 }
114 
BOARD_Codec_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,const uint8_t * txBuff,uint8_t txBuffSize)115 status_t BOARD_Codec_I2C_Send(
116     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
117 {
118     return BOARD_LPI2C_Send(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
119                             txBuffSize);
120 }
121 
BOARD_Codec_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)122 status_t BOARD_Codec_I2C_Receive(
123     uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
124 {
125     return BOARD_LPI2C_Receive(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff, rxBuffSize);
126 }
127 #endif /* SDK_I2C_BASED_COMPONENT_USED */
128