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