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 "board.h"
10 #include "fsl_debug_console.h"
11 #include "fsl_common.h"
12 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
13 #include "fsl_i2c.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     uint32_t uartClkSrcFreq;
27     /* SIM_SOPT2[27:26]:
28      *  00: Clock Disabled
29      *  01: IRC48M
30      *  10: OSCERCLK
31      *  11: MCGIRCCLK
32      */
33     CLOCK_SetLpuart0Clock(1);
34 
35     uartClkSrcFreq = BOARD_DEBUG_UART_CLK_FREQ;
36 
37     DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, uartClkSrcFreq);
38 }
39 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
BOARD_I2C_Init(I2C_Type * base,uint32_t clkSrc_Hz)40 void BOARD_I2C_Init(I2C_Type *base, uint32_t clkSrc_Hz)
41 {
42     i2c_master_config_t i2cConfig = {0};
43 
44     I2C_MasterGetDefaultConfig(&i2cConfig);
45     I2C_MasterInit(base, &i2cConfig, clkSrc_Hz);
46 }
47 
BOARD_I2C_Send(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * txBuff,uint8_t txBuffSize)48 status_t BOARD_I2C_Send(I2C_Type *base,
49                         uint8_t deviceAddress,
50                         uint32_t subAddress,
51                         uint8_t subaddressSize,
52                         uint8_t *txBuff,
53                         uint8_t txBuffSize)
54 {
55     i2c_master_transfer_t masterXfer;
56 
57     /* Prepare transfer structure. */
58     masterXfer.slaveAddress   = deviceAddress;
59     masterXfer.direction      = kI2C_Write;
60     masterXfer.subaddress     = subAddress;
61     masterXfer.subaddressSize = subaddressSize;
62     masterXfer.data           = txBuff;
63     masterXfer.dataSize       = txBuffSize;
64     masterXfer.flags          = kI2C_TransferDefaultFlag;
65 
66     return I2C_MasterTransferBlocking(base, &masterXfer);
67 }
68 
BOARD_I2C_Receive(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)69 status_t BOARD_I2C_Receive(I2C_Type *base,
70                            uint8_t deviceAddress,
71                            uint32_t subAddress,
72                            uint8_t subaddressSize,
73                            uint8_t *rxBuff,
74                            uint8_t rxBuffSize)
75 {
76     i2c_master_transfer_t masterXfer;
77 
78     /* Prepare transfer structure. */
79     masterXfer.slaveAddress   = deviceAddress;
80     masterXfer.subaddress     = subAddress;
81     masterXfer.subaddressSize = subaddressSize;
82     masterXfer.data           = rxBuff;
83     masterXfer.dataSize       = rxBuffSize;
84     masterXfer.direction      = kI2C_Read;
85     masterXfer.flags          = kI2C_TransferDefaultFlag;
86 
87     return I2C_MasterTransferBlocking(base, &masterXfer);
88 }
89 
BOARD_Accel_I2C_Init(void)90 void BOARD_Accel_I2C_Init(void)
91 {
92     BOARD_I2C_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_I2C_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_I2C_Receive(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
106 }
107 #endif /* SDK_I2C_BASED_COMPONENT_USED */
108