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_port.h"
12 #include "clock_config.h"
13 #include "board.h"
14 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
15 #include "fsl_i2c.h"
16 #endif /* SDK_I2C_BASED_COMPONENT_USED */
17 #include "fsl_debug_console.h"
18 
19 /*******************************************************************************
20  * Variables
21  ******************************************************************************/
22 
23 /*******************************************************************************
24  * Code
25  ******************************************************************************/
26 /* Initialize debug console. */
BOARD_InitDebugConsole(void)27 void BOARD_InitDebugConsole(void)
28 {
29     uint32_t uartClkSrcFreq;
30 
31     /* SIM_SOPT2[27:26]:
32      *  00: Clock Disabled
33      *  01: MCGFLLCLK, or MCGPLLCLK, or IRC48M
34      *  10: OSCERCLK
35      *  11: MCGIRCCLK
36      */
37     CLOCK_SetLpuartClock(2);
38 
39     uartClkSrcFreq = BOARD_DEBUG_UART_CLK_FREQ;
40 
41     DbgConsole_Init(BOARD_DEBUG_UART_INSTANCE, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, uartClkSrcFreq);
42 }
43 #if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
BOARD_I2C_Init(I2C_Type * base,uint32_t clkSrc_Hz)44 void BOARD_I2C_Init(I2C_Type *base, uint32_t clkSrc_Hz)
45 {
46     i2c_master_config_t i2cConfig = {0};
47 
48     I2C_MasterGetDefaultConfig(&i2cConfig);
49     I2C_MasterInit(base, &i2cConfig, clkSrc_Hz);
50 }
51 
BOARD_I2C_Send(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * txBuff,uint8_t txBuffSize)52 status_t BOARD_I2C_Send(I2C_Type *base,
53                         uint8_t deviceAddress,
54                         uint32_t subAddress,
55                         uint8_t subaddressSize,
56                         uint8_t *txBuff,
57                         uint8_t txBuffSize)
58 {
59     i2c_master_transfer_t masterXfer;
60 
61     /* Prepare transfer structure. */
62     masterXfer.slaveAddress   = deviceAddress;
63     masterXfer.direction      = kI2C_Write;
64     masterXfer.subaddress     = subAddress;
65     masterXfer.subaddressSize = subaddressSize;
66     masterXfer.data           = txBuff;
67     masterXfer.dataSize       = txBuffSize;
68     masterXfer.flags          = kI2C_TransferDefaultFlag;
69 
70     return I2C_MasterTransferBlocking(base, &masterXfer);
71 }
72 
BOARD_I2C_Receive(I2C_Type * base,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)73 status_t BOARD_I2C_Receive(I2C_Type *base,
74                            uint8_t deviceAddress,
75                            uint32_t subAddress,
76                            uint8_t subaddressSize,
77                            uint8_t *rxBuff,
78                            uint8_t rxBuffSize)
79 {
80     i2c_master_transfer_t masterXfer;
81 
82     /* Prepare transfer structure. */
83     masterXfer.slaveAddress   = deviceAddress;
84     masterXfer.subaddress     = subAddress;
85     masterXfer.subaddressSize = subaddressSize;
86     masterXfer.data           = rxBuff;
87     masterXfer.dataSize       = rxBuffSize;
88     masterXfer.direction      = kI2C_Read;
89     masterXfer.flags          = kI2C_TransferDefaultFlag;
90 
91     return I2C_MasterTransferBlocking(base, &masterXfer);
92 }
93 
BOARD_Accel_I2C_Init(void)94 void BOARD_Accel_I2C_Init(void)
95 {
96     BOARD_I2C_Init(BOARD_ACCEL_I2C_BASEADDR, BOARD_ACCEL_I2C_CLOCK_FREQ);
97 }
98 
BOARD_Accel_I2C_Send(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint32_t txBuff)99 status_t BOARD_Accel_I2C_Send(uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint32_t txBuff)
100 {
101     uint8_t data = (uint8_t)txBuff;
102 
103     return BOARD_I2C_Send(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, &data, 1);
104 }
105 
BOARD_Accel_I2C_Receive(uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)106 status_t BOARD_Accel_I2C_Receive(
107     uint8_t deviceAddress, uint32_t subAddress, uint8_t subaddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
108 {
109     return BOARD_I2C_Receive(BOARD_ACCEL_I2C_BASEADDR, deviceAddress, subAddress, subaddressSize, rxBuff, rxBuffSize);
110 }
111 #endif /* SDK_I2C_BASED_COMPONENT_USED */
112