MCUXpresso_MKS22FN256xxx12/boards/mapsks22/fatfs_examples/fatfs_sdspi/board.c
2022-06-18 14:53:46 +08:00

181 lines
6.1 KiB
C

/*
* The Clear BSD License
* Copyright (c) 2015, Freescale Semiconductor, Inc.
* Copyright 2016-2017 NXP
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted (subject to the limitations in the disclaimer below) provided
* that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdint.h>
#include "fsl_device_registers.h"
#include "fsl_debug_console.h"
#include "board.h"
#if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
#include "fsl_lpi2c.h"
#endif /* SDK_I2C_BASED_COMPONENT_USED */
#if defined BOARD_USE_CODEC
#include "fsl_wm8960.h"
#endif
/*******************************************************************************
* Variables
******************************************************************************/
#if defined BOARD_USE_CODEC
codec_config_t boardCodecConfig = {
.I2C_SendFunc = BOARD_Codec_I2C_Send,
.I2C_ReceiveFunc = BOARD_Codec_I2C_Receive,
.op.Init = WM8960_Init,
.op.Deinit = WM8960_Deinit,
.op.SetFormat = WM8960_ConfigDataFormat
};
#endif
/*******************************************************************************
* Code
******************************************************************************/
/* Initialize debug console. */
void BOARD_InitDebugConsole(void)
{
uint32_t uartClkSrcFreq = BOARD_DEBUG_UART_CLK_FREQ;
DbgConsole_Init(BOARD_DEBUG_UART_BASEADDR, BOARD_DEBUG_UART_BAUDRATE, BOARD_DEBUG_UART_TYPE, uartClkSrcFreq);
}
#if defined(SDK_I2C_BASED_COMPONENT_USED) && SDK_I2C_BASED_COMPONENT_USED
void BOARD_LPI2C_Init(LPI2C_Type *base, uint32_t clkSrc_Hz)
{
lpi2c_master_config_t lpi2cConfig = {0};
/*
* lpi2cConfig.debugEnable = false;
* lpi2cConfig.ignoreAck = false;
* lpi2cConfig.pinConfig = kLPI2C_2PinOpenDrain;
* lpi2cConfig.baudRate_Hz = 100000U;
* lpi2cConfig.busIdleTimeout_ns = 0;
* lpi2cConfig.pinLowTimeout_ns = 0;
* lpi2cConfig.sdaGlitchFilterWidth_ns = 0;
* lpi2cConfig.sclGlitchFilterWidth_ns = 0;
*/
LPI2C_MasterGetDefaultConfig(&lpi2cConfig);
LPI2C_MasterInit(base, &lpi2cConfig, clkSrc_Hz);
}
status_t BOARD_LPI2C_Send(LPI2C_Type *base, uint8_t deviceAddress, uint32_t subAddress,
uint8_t subAddressSize, uint8_t *txBuff, uint8_t txBuffSize)
{
status_t reVal;
/* Send master blocking data to slave */
reVal = LPI2C_MasterStart(base, deviceAddress, kLPI2C_Write);
if (kStatus_Success == reVal)
{
while (LPI2C_MasterGetStatusFlags(base) & kLPI2C_MasterNackDetectFlag)
{
}
reVal = LPI2C_MasterSend(base, &subAddress, subAddressSize);
if (reVal != kStatus_Success)
{
return reVal;
}
reVal = LPI2C_MasterSend(base, txBuff, txBuffSize);
if (reVal != kStatus_Success)
{
return reVal;
}
reVal = LPI2C_MasterStop(base);
if (reVal != kStatus_Success)
{
return reVal;
}
}
return reVal;
}
status_t BOARD_LPI2C_Receive(LPI2C_Type *base, uint8_t deviceAddress, uint32_t subAddress,
uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
{
status_t reVal;
reVal = LPI2C_MasterStart(base, deviceAddress, kLPI2C_Write);
if (kStatus_Success == reVal)
{
while (LPI2C_MasterGetStatusFlags(base) & kLPI2C_MasterNackDetectFlag)
{
}
reVal = LPI2C_MasterSend(base, &subAddress, subAddressSize);
if (reVal != kStatus_Success)
{
return reVal;
}
reVal = LPI2C_MasterRepeatedStart(base, deviceAddress, kLPI2C_Read);
if (reVal != kStatus_Success)
{
return reVal;
}
reVal = LPI2C_MasterReceive(base, rxBuff, rxBuffSize);
if (reVal != kStatus_Success)
{
return reVal;
}
reVal = LPI2C_MasterStop(base);
if (reVal != kStatus_Success)
{
return reVal;
}
}
return reVal;
}
void BOARD_Codec_I2C_Init(void)
{
BOARD_LPI2C_Init(BOARD_CODEC_I2C_BASEADDR, BOARD_CODEC_I2C_CLOCK_FREQ);
}
status_t BOARD_Codec_I2C_Send(
uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, const uint8_t *txBuff, uint8_t txBuffSize)
{
return BOARD_LPI2C_Send(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, (uint8_t *)txBuff,
txBuffSize);
}
status_t BOARD_Codec_I2C_Receive(
uint8_t deviceAddress, uint32_t subAddress, uint8_t subAddressSize, uint8_t *rxBuff, uint8_t rxBuffSize)
{
return BOARD_LPI2C_Receive(BOARD_CODEC_I2C_BASEADDR, deviceAddress, subAddress, subAddressSize, rxBuff,
rxBuffSize);
}
#endif /* SDK_I2C_BASED_COMPONENT_USED */