⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2s.c

📁 基于TI公司Cortex-M3的uart超级通信开发
💻 C
📖 第 1 页 / 共 3 页
字号:
//*****************************************************************************
//
// i2s.c - Driver for the I2S controller.
//
// Copyright (c) 2008-2009 Luminary Micro, Inc.  All rights reserved.
// Software License Agreement
// 
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
// 
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws.  All rights are reserved.  You may not combine
// this software with "viral" open-source software in order to form a larger
// program.  Any use in violation of the foregoing restrictions may subject
// the user to criminal sanctions under applicable laws, as well as to civil
// liability for the breach of the terms and conditions of this license.
// 
// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 5228 of the Stellaris Peripheral Driver Library.
//
//*****************************************************************************

//*****************************************************************************
//
//! \addtogroup i2s_api
//! @{
//
//*****************************************************************************

#include "inc/hw_i2s.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/i2s.h"
#include "driverlib/interrupt.h"

//*****************************************************************************
//
//! Enables the I2S transmit module for operation.
//!
//! \param ulBase is the I2S module base address.
//!
//! This function enables the transmit module for operation.  The module
//! should be enabled after configuration.  When the module is disabled,
//! no data or clocks will be generated on the I2S signals.
//!
//! \return None.
//
//*****************************************************************************
void
I2STxEnable(unsigned long ulBase)
{
    //
    // Check the arguments.
    //
    ASSERT(ulBase == I2S0_BASE);

    //
    // Enable the tx FIFO service request.
    //
    HWREG(ulBase + I2S_O_TXISM) = I2S_TXISM_FFM;

    //
    // Read-modify-write the enable bit.
    //
    HWREG(ulBase + I2S_O_CFG) |= I2S_CFG_TXEN;
}

//*****************************************************************************
//
//! Disables the I2S transmit module for operation.
//!
//! \param ulBase is the I2S module base address.
//!
//! This function disables the transmit module for operation.  The module
//! should be disabled before configuration.  When the module is disabled,
//! no data or clocks will be generated on the I2S signals.
//!
//! \return None.
//
//*****************************************************************************
void
I2STxDisable(unsigned long ulBase)
{
    //
    // Check the arguments.
    //
    ASSERT(ulBase == I2S0_BASE);

    //
    // Read-modify-write the enable bit.
    //
    HWREG(ulBase + I2S_O_CFG) &= ~I2S_CFG_TXEN;
}

//*****************************************************************************
//
//! Writes data samples to the I2S transmit FIFO with blocking.
//!
//! \param ulBase is the I2S module base address.
//! \param ulData is the single or dual channel I2S data.
//!
//! This function writes a single channel sample or combined left-right
//! samples to the I2S transmit FIFO.  The format of the sample is determined
//! by the configuration that was used with the function I2STxConfigSet().
//! If the transmit mode is I2S_MODE_DUAL_STEREO then the \e ulData parameter
//! contains either the left or right sample.  The left and right sample
//! alternate with each write to the FIFO, left sample first.  If the transmit
//! mode is I2S_MODE_COMPACT_STEREO_16 or I2S_MODE_COMPACT_STEREO_8, then the
//! \e ulData parameter contains both the left and right samples.  If the
//! transmit mode is I2S_MODE_SINGLE_MONO then the \e ulData parameter
//! contains the single channel sample.
//!
//! For the compact modes, both the left and right samples are written at
//! the same time.  If 16-bit compact mode is used, then the least significant
//! 16 bits contain the left sample, and the most significant 16 bits contain
//! the right sample.  If 8-bit compact mode is used, then the lower 8 bits
//! contain the left sample, and the next 8 bits contain the right sample,
//! with the upper 16 bits unused.
//!
//! If there is no room in the transmit FIFO, then this function will wait
//! in a polling loop until the data can be written.
//!
//! \return None.
//
//*****************************************************************************
void
I2STxDataPut(unsigned long ulBase, unsigned long ulData)
{
    //
    // Check the arguments.
    //
    ASSERT(ulBase == I2S0_BASE);

    //
    // Wait until there is space.
    //
    while(HWREG(ulBase + I2S_O_TXLEV) >= 16)
    {
    }

    //
    // Write the data to the I2S.
    //
    HWREG(ulBase + I2S_O_TXFIFO) = ulData;
}

//*****************************************************************************
//
//! Writes data samples to the I2S transmit FIFO without blocking.
//!
//! \param ulBase is the I2S module base address.
//! \param ulData is the single or dual channel I2S data.
//!
//! This function writes a single channel sample or combined left-right
//! samples to the I2S transmit FIFO.  The format of the sample is determined
//! by the configuration that was used with the function I2STxConfigSet().
//! If the transmit mode is I2S_MODE_DUAL_STEREO then the \e ulData parameter
//! contains either the left or right sample.  The left and right sample
//! alternate with each write to the FIFO, left sample first.  If the transmit
//! mode is I2S_MODE_COMPACT_STEREO_16 or I2S_MODE_COMPACT_STEREO_8, then the
//! \e ulData parameter contains both the left and right samples.  If the
//! transmit mode is I2S_MODE_SINGLE_MONO then the \e ulData parameter
//! contains the single channel sample.
//!
//! For the compact modes, both the left and right samples are written at
//! the same time.  If 16-bit compact mode is used, then the least significant
//! 16 bits contain the left sample, and the most significant 16 bits contain
//! the right sample.  If 8-bit compact mode is used, then the lower 8 bits
//! contain the left sample, and the next 8 bits contain the right sample,
//! with the upper 16 bits unused.
//!
//! If there is no room in the transmit FIFO, then this function will return
//! immediately without writing any data to the FIFO.
//!
//! \return The number of elements written to the I2S transmit FIFO (1 or 0).
//
//*****************************************************************************
long
I2STxDataPutNonBlocking(unsigned long ulBase, unsigned long ulData)
{
    //
    // Check the arguments.
    //
    ASSERT(ulBase == I2S0_BASE);

    //
    // Check for space to write.
    //
    if(HWREG(ulBase + I2S_O_TXLEV) < 16)
    {
        HWREG(ulBase + I2S_O_TXFIFO) = ulData;
        return(1);
    }
    else
    {
        return(0);
    }
}

//*****************************************************************************
//
//! Configures the I2S transmit module.
//!
//! \param ulBase is the I2S module base address.
//! \param ulConfig is the logical OR of the configuration options.
//!
//! This function is used to configure the options for the I2S transmit
//! channel.  The parameter \e ulConfig is the logical OR of the following
//! options:
//!
//! - \b I2S_CONFIG_FORMAT_I2S for standard I2S format,
//! \b I2S_CONFIG_FORMAT_LEFT_JUST for left justified format, or
//! \b I2S_CONFIG_FORMAT_RIGHT_JUST for right justified format.
//! - \b I2S_CONFIG_SCLK_INVERT to invert the polarity of the serial bit clock.
//! - \b I2S_CONFIG_MODE_DUAL for dual channel stereo,
//! \b I2S_CONFIG_MODE_COMPACT_16 for 16-bit compact stereo mode,
//! \b I2S_CONFIG_MODE_COMPACT_8 for 8-bit compact stereo mode, or
//! \b I2S_CONFIG_MODE_MONO for single channel mono format.
//! - \b I2S_CONFIG_CLK_MASTER or \b I2S_CONFIG_CLK_SLAVE to select whether
//! the I2S transmitter is the clock master or slave.
//! - \b I2S_CONFIG_SAMPLE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
//! to select the number of bits per sample.
//! - \b I2S_CONFIG_WIRE_SIZE_32, \b _24, \b _20, \b _16, or \b _8
//! to select the number of bits per word that are transferred on the data
//! line.
//! - \b I2S_CONFIG_EMPTY_ZERO or \b I2S_CONFIG_EMPTY_REPEAT to select whether
//! the module transmits zeroes or repeats the last sample when the FIFO is
//! empty.
//!
//! \return None.
//
//*****************************************************************************
void
I2STxConfigSet(unsigned long ulBase, unsigned long ulConfig)
{
    //
    // Check the arguments.
    //
    ASSERT(ulBase == I2S0_BASE);
    ASSERT((ulConfig & (I2S_CONFIG_FORMAT_MASK | I2S_CONFIG_MODE_MASK |
                        I2S_CONFIG_EMPTY_MASK | I2S_CONFIG_CLK_MASK |
                        I2S_CONFIG_SAMPLE_SIZE_MASK |
                        I2S_CONFIG_WIRE_SIZE_MASK)) == ulConfig);

    //
    // Check to see if a compact mode is used.
    //
    if((ulConfig & I2S_CONFIG_MODE_MASK) == I2S_CONFIG_MODE_COMPACT_8)
    {
        //
        // If compact 8 mode is used, then need to adjust some bits
        // before writing the config register.  Also set the FIFO
        // config register for 8 bit compact samples.
        //
        ulConfig &= ~I2S_CONFIG_MODE_MONO;
        HWREG(ulBase + I2S_O_TXFIFOCFG) = I2S_TXFIFOCFG_CSS;
    }
    else
    {
        //
        // If compact 8 mode is not used, then set the FIFO config
        // register for 16 bit.  This is okay if a compact mode is
        // not used.
        //
        HWREG(ulBase + I2S_O_TXFIFOCFG) = 0;
    }

    //
    // Write the configuration register.  Since all the fields are
    // specified by the configuration parameter, it is not necessary
    // to do a read-modify-write.
    //
    HWREG(ulBase + I2S_O_TXCFG) = ulConfig;
}

//*****************************************************************************
//
//! Sets the FIFO level at which a service request is generated.
//!
//! \param ulBase is the I2S module base address.
//! \param ulLevel is the FIFO service request limit.
//!
//! This function is used to set the transmit FIFO fullness level at which
//! a service request will occur.  The service request is used to generate
//! an interrupt or a DMA transfer request.  The transmit FIFO will
//! generate a service request when the number of items in the FIFO is
//! less than the level specified in the \e ulLevel parameter.  For example,
//! if \e ulLevel is 8, then a service request will be generated when
//! there are less than 8 samples remaining in the transmit FIFO.
//!
//! For the purposes of counting the FIFO level, a left-right sample pair
//! counts as 2, whether the mode is dual or compact stereo.  When mono
//! mode is used, internally the mono sample is still treated as a sample
//! pair, so a single mono sample counts as 2.  Since the FIFO always deals
//! with sample pairs, the level must be an even number from 0 to 16.  The
//! maximum value is 16, which will cause a service request when there
//! is any room in the FIFO.  The minimum value is 0, which disables the
//! service request.
//!
//! \return None.
//
//*****************************************************************************
void
I2STxFIFOLimitSet(unsigned long ulBase, unsigned long ulLevel)
{
    //
    // Check the arguments.
    //
    ASSERT(ulBase == I2S0_BASE);
    ASSERT(ulLevel <= 16);

    //
    // Write the FIFO limit
    //
    HWREG(ulBase + I2S_O_TXLIMIT) = ulLevel;
}

//*****************************************************************************
//
//! Gets the current setting of the FIFO service request level.
//!
//! \param ulBase is the I2S module base address.
//!
//! This function is used to get the value of the transmit FIFO service
//! request level.  This value is set using the I2STxFIFOLimitSet()
//! function.
//!
//! \return Returns the current value of the FIFO service request limit.
//
//*****************************************************************************
unsigned long
I2STxFIFOLimitGet(unsigned long ulBase)
{
    //
    // Check the arguments.
    //
    ASSERT(ulBase == I2S0_BASE);

    //
    // Read and return the FIFO limit
    //
    return(HWREG(ulBase + I2S_O_TXLIMIT));
}

//*****************************************************************************
//
//! Gets the number of samples in the transmit FIFO.
//!
//! \param ulBase is the I2S module base address.
//!
//! This function is used to get the number of samples in the transmit
//! FIFO.  For the purposes of measuring the FIFO level, a left-right sample
//! pair counts as 2, whether the mode is dual or compact stereo.  When mono
//! mode is used, internally the mono sample is still treated as a sample
//! pair, so a single mono sample counts as 2.  Since the FIFO always deals
//! with sample pairs, normally the level will be an even number from 0 to
//! 16.  If dual stereo mode is used and only the left sample has been
//! written without the matching right sample, then the FIFO level will be an
//! odd value.  If the FIFO level is odd, it indicates a left-right sample
//! mismatch.
//!
//! \return Returns the number of samples in the transmit FIFO, which will
//! normally be an even number.
//
//*****************************************************************************
unsigned long
I2STxFIFOLevelGet(unsigned long ulBase)
{
    //
    // Check the arguments.
    //
    ASSERT(ulBase == I2S0_BASE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -