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