1 /*
2 * Copyright 2019 NXP
3 * All rights reserved.
4 *
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 */
8
9 #include "fsl_codec_i2c.h"
10 /*******************************************************************************
11 * Definitions
12 ******************************************************************************/
13
14 /*******************************************************************************
15 * Variables
16 ******************************************************************************/
17
18 /*******************************************************************************
19 * Code
20 ******************************************************************************/
21 /*!
22 * brief Codec i2c bus initilization.
23 *
24 * param handle i2c master handle.
25 * param i2CInstance instance number of the i2c bus, such as 0 is corresponding to I2C0.
26 * param i2cBaudrate i2c baudrate.
27 * param i2cSourceClockHz i2c source clock frequency.
28 * return kStatus_HAL_I2cSuccess is success, else initial failed.
29 */
CODEC_I2C_Init(void * handle,uint32_t i2cInstance,uint32_t i2cBaudrate,uint32_t i2cSourceClockHz)30 status_t CODEC_I2C_Init(void *handle, uint32_t i2cInstance, uint32_t i2cBaudrate, uint32_t i2cSourceClockHz)
31 {
32 hal_i2c_master_config_t masterConfig;
33
34 masterConfig.enableMaster = true;
35 masterConfig.baudRate_Bps = i2cBaudrate;
36 masterConfig.srcClock_Hz = i2cSourceClockHz;
37 masterConfig.instance = (uint8_t)i2cInstance;
38
39 return (status_t)HAL_I2cMasterInit((hal_i2c_master_handle_t *)handle, &masterConfig);
40 }
41
42 /*!
43 * brief Codec i2c de-initilization.
44 *
45 * param handle i2c master handle.
46 * return kStatus_HAL_I2cSuccess is success, else deinitial failed.
47 */
CODEC_I2C_Deinit(void * handle)48 status_t CODEC_I2C_Deinit(void *handle)
49 {
50 return (status_t)HAL_I2cMasterDeinit((hal_i2c_master_handle_t *)handle);
51 }
52
53 /*!
54 * brief codec i2c send function.
55 *
56 * param handle i2c master handle.
57 * param deviceAddress codec device address.
58 * param subAddress register address.
59 * param subaddressSize register address width.
60 * param txBuff tx buffer pointer.
61 * param txBuffSize tx buffer size.
62 * return kStatus_HAL_I2cSuccess is success, else send failed.
63 */
CODEC_I2C_Send(void * handle,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * txBuff,uint8_t txBuffSize)64 status_t CODEC_I2C_Send(void *handle,
65 uint8_t deviceAddress,
66 uint32_t subAddress,
67 uint8_t subaddressSize,
68 uint8_t *txBuff,
69 uint8_t txBuffSize)
70 {
71 hal_i2c_master_transfer_t masterXfer;
72
73 masterXfer.slaveAddress = deviceAddress;
74 masterXfer.direction = kHAL_I2cWrite;
75 masterXfer.subaddress = (uint32_t)subAddress;
76 masterXfer.subaddressSize = subaddressSize;
77 masterXfer.data = txBuff;
78 masterXfer.dataSize = txBuffSize;
79 masterXfer.flags = (uint32_t)kHAL_I2cTransferDefaultFlag;
80
81 return (status_t)HAL_I2cMasterTransferBlocking((hal_i2c_master_handle_t *)handle, &masterXfer);
82 }
83
84 /*!
85 * brief codec i2c receive function.
86 *
87 * param handle i2c master handle.
88 * param deviceAddress codec device address.
89 * param subAddress register address.
90 * param subaddressSize register address width.
91 * param rxBuff rx buffer pointer.
92 * param rxBuffSize rx buffer size.
93 * return kStatus_HAL_I2cSuccess is success, else receive failed.
94 */
CODEC_I2C_Receive(void * handle,uint8_t deviceAddress,uint32_t subAddress,uint8_t subaddressSize,uint8_t * rxBuff,uint8_t rxBuffSize)95 status_t CODEC_I2C_Receive(void *handle,
96 uint8_t deviceAddress,
97 uint32_t subAddress,
98 uint8_t subaddressSize,
99 uint8_t *rxBuff,
100 uint8_t rxBuffSize)
101 {
102 hal_i2c_master_transfer_t masterXfer;
103
104 masterXfer.slaveAddress = deviceAddress;
105 masterXfer.direction = kHAL_I2cRead;
106 masterXfer.subaddress = (uint32_t)subAddress;
107 masterXfer.subaddressSize = subaddressSize;
108 masterXfer.data = rxBuff;
109 masterXfer.dataSize = rxBuffSize;
110 masterXfer.flags = (uint32_t)kHAL_I2cTransferDefaultFlag;
111
112 return (status_t)HAL_I2cMasterTransferBlocking((hal_i2c_master_handle_t *)handle, &masterXfer);
113 }
114