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