1 /*
2 * Copyright 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_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 status_t reVal;
59
60 /* Send master blocking data to slave */
61 reVal = LPI2C_MasterStart(base, deviceAddress, kLPI2C_Write);
62 if (kStatus_Success == reVal)
63 {
64 while (LPI2C_MasterGetStatusFlags(base) & kLPI2C_MasterNackDetectFlag)
65 {
66 }
67
68 reVal = LPI2C_MasterSend(base, &subAddress, subAddressSize);
69 if (reVal != kStatus_Success)
70 {
71 return reVal;
72 }
73
74 reVal = LPI2C_MasterSend(base, txBuff, txBuffSize);
75 if (reVal != kStatus_Success)
76 {
77 return reVal;
78 }
79
80 reVal = LPI2C_MasterStop(base);
81 if (reVal != kStatus_Success)
82 {
83 return reVal;
84 }
85 }
86
87 return reVal;
88 }
89
BOARD_LPI2C_Receive(LPI2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subAddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)90 status_t BOARD_LPI2C_Receive(LPI2C_Type *base,
91 uint8_t deviceAddress,
92 uint32_t subAddress,
93 uint8_t subAddressSize,
94 uint8_t *rxBuff,
95 uint8_t rxBuffSize)
96 {
97 status_t reVal;
98
99 reVal = LPI2C_MasterStart(base, deviceAddress, kLPI2C_Write);
100 if (kStatus_Success == reVal)
101 {
102 while (LPI2C_MasterGetStatusFlags(base) & kLPI2C_MasterNackDetectFlag)
103 {
104 }
105
106 reVal = LPI2C_MasterSend(base, &subAddress, subAddressSize);
107 if (reVal != kStatus_Success)
108 {
109 return reVal;
110 }
111
112 reVal = LPI2C_MasterRepeatedStart(base, deviceAddress, kLPI2C_Read);
113 if (reVal != kStatus_Success)
114 {
115 return reVal;
116 }
117
118 reVal = LPI2C_MasterReceive(base, rxBuff, rxBuffSize);
119 if (reVal != kStatus_Success)
120 {
121 return reVal;
122 }
123
124 reVal = LPI2C_MasterStop(base);
125 if (reVal != kStatus_Success)
126 {
127 return reVal;
128 }
129 }
130 return reVal;
131 }
132
BOARD_Accel_I2C_Init(void)133 void BOARD_Accel_I2C_Init(void)
134 {
135 BOARD_LPI2C_Init(BOARD_ACCEL_I2C_BASEADDR, BOARD_ACCEL_I2C_CLOCK_FREQ);
136 }
137
BOARD_Accel_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint32_t txBuff)138 status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
139 {
140 uint8_t data = (uint8_t)txBuff;
141
142 return BOARD_LPI2C_Send(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
143 }
144
BOARD_Accel_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)145 status_t BOARD_Accel_I2C_Receive(
146 uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
147 {
148 return BOARD_LPI2C_Receive(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
149 }
150 #endif /* SDK_I2C_BASED_COMPONENT_USED */
151