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;
28     /* SIM_SOPT2[27:26]:
29      *  00: Clock Disabled
30      *  01: IRC48M
31      *  10: OSCERCLK
32      *  11: MCGIRCCLK
33      */
34     CLOCK_SetLpuart0Clock(1);
35 
36     uartClkSrcFreq = BOARD_DEBUG_UART_CLK_FREQ;
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