1 /*
2  * Copyright (c) 2015, 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_i2c.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     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_I2C_Init(I2C_Type * base,uint32_t clkSrc_Hz)32 void BOARD_I2C_Init(I2C_Type *base, uint32_t clkSrc_Hz)
33 {
34     i2c_master_config_t i2cConfig = {0};
35 
36     I2C_MasterGetDefaultConfig(&i2cConfig);
37     I2C_MasterInit(base, &i2cConfig, clkSrc_Hz);
38 }
39 
BOARD_I2C_Send(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * txBuff,uint8_t txBuffSize)40 status_t BOARD_I2C_Send(I2C_Type *base,
41                         uint8_t deviceAddress,
42                         uint32_t subAddress,
43                         uint8_t subaddressSize,
44                         uint8_t *txBuff,
45                         uint8_t txBuffSize)
46 {
47     i2c_master_transfer_t masterXfer;
48 
49     /* Prepare transfer structure. */
50     masterXfer.slaveAddress   = deviceAddress;
51     masterXfer.direction      = kI2C_Write;
52     masterXfer.subaddress     = subAddress;
53     masterXfer.subaddressSize = subaddressSize;
54     masterXfer.data           = txBuff;
55     masterXfer.dataSize       = txBuffSize;
56     masterXfer.flags          = kI2C_TransferDefaultFlag;
57 
58     return I2C_MasterTransferBlocking(base, &masterXfer);
59 }
60 
BOARD_I2C_Receive(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)61 status_t BOARD_I2C_Receive(I2C_Type *base,
62                            uint8_t deviceAddress,
63                            uint32_t subAddress,
64                            uint8_t subaddressSize,
65                            uint8_t *rxBuff,
66                            uint8_t rxBuffSize)
67 {
68     i2c_master_transfer_t masterXfer;
69 
70     /* Prepare transfer structure. */
71     masterXfer.slaveAddress   = deviceAddress;
72     masterXfer.subaddress     = subAddress;
73     masterXfer.subaddressSize = subaddressSize;
74     masterXfer.data           = rxBuff;
75     masterXfer.dataSize       = rxBuffSize;
76     masterXfer.direction      = kI2C_Read;
77     masterXfer.flags          = kI2C_TransferDefaultFlag;
78 
79     return I2C_MasterTransferBlocking(base, &masterXfer);
80 }
81 
BOARD_Accel_I2C_Init(void)82 void BOARD_Accel_I2C_Init(void)
83 {
84     BOARD_I2C_Init(BOARD_ACCEL_I2C_BASEADDR, BOARD_ACCEL_I2C_CLOCK_FREQ);
85 }
86 
BOARD_Accel_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint32_t txBuff)87 status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
88 {
89     uint8_t data = (uint8_t)txBuff;
90 
91     return BOARD_I2C_Send(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
92 }
93 
BOARD_Accel_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)94 status_t BOARD_Accel_I2C_Receive(
95     uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
96 {
97     return BOARD_I2C_Receive(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
98 }
99 #endif /* SDK_I2C_BASED_COMPONENT_USED */
100