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 <stdint.h>
10 #include "fsl_common.h"
11 #include "fsl_debug_console.h"
12 #include "board.h"
13 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
14 #include "fsl_lpi2c.h"
15 #endif /* SDK_I2C_BASED_COMPONENT_USED */
16 
17 /*******************************************************************************
18  * Variables
19  ******************************************************************************/
20 
21 /*******************************************************************************
22  * Code
23  ******************************************************************************/
24 /* Initialize debug console. */
BOARD_InitDebugConsole(void)25 void BOARD_InitDebugConsole(void)
26 {
27     CLOCK_SetIpSrc(kCLOCK_Lpuart1, BOARD_DEBUG_UART_CLKSRC);
28 
29     uint32_t uartClkSrcFreq = CLOCK_GetIpFreq(kCLOCK_Lpuart1);
30 
31     DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, uartClkSrcFreq);
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 #endif /* SDK_I2C_BASED_COMPONENT_USED */
110