LINKED LIST TEST on mbed

Dependencies:   mbed

Committer:
lynxeyed_atsu
Date:
Sat Feb 26 03:55:12 2011 +0000
Revision:
0:e8bfffbb3ab6

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lynxeyed_atsu 0:e8bfffbb3ab6 1 /***********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 2 * @file lpc17xx_i2s.c
lynxeyed_atsu 0:e8bfffbb3ab6 3 * @brief Contains all functions support for I2S firmware library on LPC17xx
lynxeyed_atsu 0:e8bfffbb3ab6 4 * @version 3.0
lynxeyed_atsu 0:e8bfffbb3ab6 5 * @date 18. June. 2010
lynxeyed_atsu 0:e8bfffbb3ab6 6 * @author NXP MCU SW Application Team
lynxeyed_atsu 0:e8bfffbb3ab6 7 **************************************************************************
lynxeyed_atsu 0:e8bfffbb3ab6 8 * Software that is described herein is for illustrative purposes only
lynxeyed_atsu 0:e8bfffbb3ab6 9 * which provides customers with programming information regarding the
lynxeyed_atsu 0:e8bfffbb3ab6 10 * products. This software is supplied "AS IS" without any warranties.
lynxeyed_atsu 0:e8bfffbb3ab6 11 * NXP Semiconductors assumes no responsibility or liability for the
lynxeyed_atsu 0:e8bfffbb3ab6 12 * use of the software, conveys no license or title under any patent,
lynxeyed_atsu 0:e8bfffbb3ab6 13 * copyright, or mask work right to the product. NXP Semiconductors
lynxeyed_atsu 0:e8bfffbb3ab6 14 * reserves the right to make changes in the software without
lynxeyed_atsu 0:e8bfffbb3ab6 15 * notification. NXP Semiconductors also make no representation or
lynxeyed_atsu 0:e8bfffbb3ab6 16 * warranty that such application will be suitable for the specified
lynxeyed_atsu 0:e8bfffbb3ab6 17 * use without further testing or modification.
lynxeyed_atsu 0:e8bfffbb3ab6 18 **********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 19
lynxeyed_atsu 0:e8bfffbb3ab6 20 /* Peripheral group ----------------------------------------------------------- */
lynxeyed_atsu 0:e8bfffbb3ab6 21 /** @addtogroup I2S
lynxeyed_atsu 0:e8bfffbb3ab6 22 * @{
lynxeyed_atsu 0:e8bfffbb3ab6 23 */
lynxeyed_atsu 0:e8bfffbb3ab6 24
lynxeyed_atsu 0:e8bfffbb3ab6 25 /* Includes ------------------------------------------------------------------- */
lynxeyed_atsu 0:e8bfffbb3ab6 26 //#include "mbed.h"
lynxeyed_atsu 0:e8bfffbb3ab6 27 #include "lpc17xx_i2s.h"
lynxeyed_atsu 0:e8bfffbb3ab6 28 #include "lpc17xx_clkpwr.h"
lynxeyed_atsu 0:e8bfffbb3ab6 29 #include "mbed.h"
lynxeyed_atsu 0:e8bfffbb3ab6 30
lynxeyed_atsu 0:e8bfffbb3ab6 31
lynxeyed_atsu 0:e8bfffbb3ab6 32 /* If this source file built with example, the LPC17xx FW library configuration
lynxeyed_atsu 0:e8bfffbb3ab6 33 * file in each example directory ("lpc17xx_libcfg.h") must be included,
lynxeyed_atsu 0:e8bfffbb3ab6 34 * otherwise the default FW library configuration file must be included instead
lynxeyed_atsu 0:e8bfffbb3ab6 35 */
lynxeyed_atsu 0:e8bfffbb3ab6 36 #ifdef __BUILD_WITH_EXAMPLE__
lynxeyed_atsu 0:e8bfffbb3ab6 37 #include "lpc17xx_libcfg.h"
lynxeyed_atsu 0:e8bfffbb3ab6 38 #else
lynxeyed_atsu 0:e8bfffbb3ab6 39 #include "lpc17xx_libcfg_default.h"
lynxeyed_atsu 0:e8bfffbb3ab6 40 #endif /* __BUILD_WITH_EXAMPLE__ */
lynxeyed_atsu 0:e8bfffbb3ab6 41
lynxeyed_atsu 0:e8bfffbb3ab6 42
lynxeyed_atsu 0:e8bfffbb3ab6 43 #ifdef _I2S
lynxeyed_atsu 0:e8bfffbb3ab6 44
lynxeyed_atsu 0:e8bfffbb3ab6 45 /* Private Functions ---------------------------------------------------------- */
lynxeyed_atsu 0:e8bfffbb3ab6 46
lynxeyed_atsu 0:e8bfffbb3ab6 47 static uint8_t i2s_GetWordWidth(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode);
lynxeyed_atsu 0:e8bfffbb3ab6 48 static uint8_t i2s_GetChannel(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode);
lynxeyed_atsu 0:e8bfffbb3ab6 49
lynxeyed_atsu 0:e8bfffbb3ab6 50 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 51 * @brief Get I2S wordwidth value
lynxeyed_atsu 0:e8bfffbb3ab6 52 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 53 * @param[in] TRMode is the I2S mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 54 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 55 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 56 * @return The wordwidth value, should be: 8,16 or 32
lynxeyed_atsu 0:e8bfffbb3ab6 57 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 58 static uint8_t i2s_GetWordWidth(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode) {
lynxeyed_atsu 0:e8bfffbb3ab6 59 uint8_t value;
lynxeyed_atsu 0:e8bfffbb3ab6 60
lynxeyed_atsu 0:e8bfffbb3ab6 61 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 62 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 63
lynxeyed_atsu 0:e8bfffbb3ab6 64 if (TRMode == I2S_TX_MODE) {
lynxeyed_atsu 0:e8bfffbb3ab6 65 value = (I2Sx->I2SDAO) & 0x03; /* get wordwidth bit */
lynxeyed_atsu 0:e8bfffbb3ab6 66 } else {
lynxeyed_atsu 0:e8bfffbb3ab6 67 value = (I2Sx->I2SDAI) & 0x03; /* get wordwidth bit */
lynxeyed_atsu 0:e8bfffbb3ab6 68 }
lynxeyed_atsu 0:e8bfffbb3ab6 69 switch (value) {
lynxeyed_atsu 0:e8bfffbb3ab6 70 case I2S_WORDWIDTH_8:
lynxeyed_atsu 0:e8bfffbb3ab6 71 return 8;
lynxeyed_atsu 0:e8bfffbb3ab6 72 case I2S_WORDWIDTH_16:
lynxeyed_atsu 0:e8bfffbb3ab6 73 return 16;
lynxeyed_atsu 0:e8bfffbb3ab6 74 default:
lynxeyed_atsu 0:e8bfffbb3ab6 75 return 32;
lynxeyed_atsu 0:e8bfffbb3ab6 76 }
lynxeyed_atsu 0:e8bfffbb3ab6 77 }
lynxeyed_atsu 0:e8bfffbb3ab6 78
lynxeyed_atsu 0:e8bfffbb3ab6 79 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 80 * @brief Get I2S channel value
lynxeyed_atsu 0:e8bfffbb3ab6 81 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 82 * @param[in] TRMode is the I2S mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 83 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 84 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 85 * @return The channel value, should be: 1(mono) or 2(stereo)
lynxeyed_atsu 0:e8bfffbb3ab6 86 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 87 static uint8_t i2s_GetChannel(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode) {
lynxeyed_atsu 0:e8bfffbb3ab6 88 uint8_t value;
lynxeyed_atsu 0:e8bfffbb3ab6 89
lynxeyed_atsu 0:e8bfffbb3ab6 90 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 91 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 92
lynxeyed_atsu 0:e8bfffbb3ab6 93 if (TRMode == I2S_TX_MODE) {
lynxeyed_atsu 0:e8bfffbb3ab6 94 value = (I2Sx->I2SDAO) & 0x04; /* get bit[2] */
lynxeyed_atsu 0:e8bfffbb3ab6 95 } else {
lynxeyed_atsu 0:e8bfffbb3ab6 96 value = (I2Sx->I2SDAI) & 0x04; /* get bit[2] */
lynxeyed_atsu 0:e8bfffbb3ab6 97 }
lynxeyed_atsu 0:e8bfffbb3ab6 98 if(value == I2S_MONO) return 1;
lynxeyed_atsu 0:e8bfffbb3ab6 99 return 2;
lynxeyed_atsu 0:e8bfffbb3ab6 100 }
lynxeyed_atsu 0:e8bfffbb3ab6 101
lynxeyed_atsu 0:e8bfffbb3ab6 102 /* End of Private Functions --------------------------------------------------- */
lynxeyed_atsu 0:e8bfffbb3ab6 103
lynxeyed_atsu 0:e8bfffbb3ab6 104
lynxeyed_atsu 0:e8bfffbb3ab6 105 /* Public Functions ----------------------------------------------------------- */
lynxeyed_atsu 0:e8bfffbb3ab6 106 /** @addtogroup I2S_Public_Functions
lynxeyed_atsu 0:e8bfffbb3ab6 107 * @{
lynxeyed_atsu 0:e8bfffbb3ab6 108 */
lynxeyed_atsu 0:e8bfffbb3ab6 109
lynxeyed_atsu 0:e8bfffbb3ab6 110 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 111 * @brief Initialize I2S
lynxeyed_atsu 0:e8bfffbb3ab6 112 * - Turn on power and clock
lynxeyed_atsu 0:e8bfffbb3ab6 113 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 114 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 115 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 116 void I2S_Init(LPC_I2S_TypeDef *I2Sx) {
lynxeyed_atsu 0:e8bfffbb3ab6 117 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 118
lynxeyed_atsu 0:e8bfffbb3ab6 119 // Turn on power and clock
lynxeyed_atsu 0:e8bfffbb3ab6 120 CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCI2S, ENABLE);
lynxeyed_atsu 0:e8bfffbb3ab6 121 LPC_I2S->I2SDAI = LPC_I2S->I2SDAO = 0x00;
lynxeyed_atsu 0:e8bfffbb3ab6 122 }
lynxeyed_atsu 0:e8bfffbb3ab6 123
lynxeyed_atsu 0:e8bfffbb3ab6 124 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 125 * @brief Configuration I2S, setting:
lynxeyed_atsu 0:e8bfffbb3ab6 126 * - master/slave mode
lynxeyed_atsu 0:e8bfffbb3ab6 127 * - wordwidth value
lynxeyed_atsu 0:e8bfffbb3ab6 128 * - channel mode
lynxeyed_atsu 0:e8bfffbb3ab6 129 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 130 * @param[in] TRMode transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 131 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 132 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 133 * @param[in] ConfigStruct pointer to I2S_CFG_Type structure
lynxeyed_atsu 0:e8bfffbb3ab6 134 * which will be initialized.
lynxeyed_atsu 0:e8bfffbb3ab6 135 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 136 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 137 void I2S_Config(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode, I2S_CFG_Type* ConfigStruct)
lynxeyed_atsu 0:e8bfffbb3ab6 138 {
lynxeyed_atsu 0:e8bfffbb3ab6 139 uint32_t bps, config;
lynxeyed_atsu 0:e8bfffbb3ab6 140
lynxeyed_atsu 0:e8bfffbb3ab6 141 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 142
lynxeyed_atsu 0:e8bfffbb3ab6 143 CHECK_PARAM(PARAM_I2S_WORDWIDTH(ConfigStruct->wordwidth));
lynxeyed_atsu 0:e8bfffbb3ab6 144 CHECK_PARAM(PARAM_I2S_CHANNEL(ConfigStruct->mono));
lynxeyed_atsu 0:e8bfffbb3ab6 145 CHECK_PARAM(PARAM_I2S_STOP(ConfigStruct->stop));
lynxeyed_atsu 0:e8bfffbb3ab6 146 CHECK_PARAM(PARAM_I2S_RESET(ConfigStruct->reset));
lynxeyed_atsu 0:e8bfffbb3ab6 147 CHECK_PARAM(PARAM_I2S_WS_SEL(ConfigStruct->ws_sel));
lynxeyed_atsu 0:e8bfffbb3ab6 148 CHECK_PARAM(PARAM_I2S_MUTE(ConfigStruct->mute));
lynxeyed_atsu 0:e8bfffbb3ab6 149
lynxeyed_atsu 0:e8bfffbb3ab6 150 /* Setup clock */
lynxeyed_atsu 0:e8bfffbb3ab6 151 bps = (ConfigStruct->wordwidth +1)*8;
lynxeyed_atsu 0:e8bfffbb3ab6 152
lynxeyed_atsu 0:e8bfffbb3ab6 153 /* Calculate audio config */
lynxeyed_atsu 0:e8bfffbb3ab6 154 config = (bps - 1)<<6 | (ConfigStruct->ws_sel)<<5 | (ConfigStruct->reset)<<4 |
lynxeyed_atsu 0:e8bfffbb3ab6 155 (ConfigStruct->stop)<<3 | (ConfigStruct->mono)<<2 | (ConfigStruct->wordwidth);
lynxeyed_atsu 0:e8bfffbb3ab6 156
lynxeyed_atsu 0:e8bfffbb3ab6 157 if(TRMode == I2S_RX_MODE){
lynxeyed_atsu 0:e8bfffbb3ab6 158 LPC_I2S->I2SDAI = config;
lynxeyed_atsu 0:e8bfffbb3ab6 159 }else{
lynxeyed_atsu 0:e8bfffbb3ab6 160 LPC_I2S->I2SDAO = config;
lynxeyed_atsu 0:e8bfffbb3ab6 161 }
lynxeyed_atsu 0:e8bfffbb3ab6 162 }
lynxeyed_atsu 0:e8bfffbb3ab6 163
lynxeyed_atsu 0:e8bfffbb3ab6 164 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 165 * @brief DeInitial both I2S transmit or receive
lynxeyed_atsu 0:e8bfffbb3ab6 166 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 167 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 168 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 169 void I2S_DeInit(LPC_I2S_TypeDef *I2Sx) {
lynxeyed_atsu 0:e8bfffbb3ab6 170 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 171
lynxeyed_atsu 0:e8bfffbb3ab6 172 // Turn off power and clock
lynxeyed_atsu 0:e8bfffbb3ab6 173 CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCI2S, DISABLE);
lynxeyed_atsu 0:e8bfffbb3ab6 174 }
lynxeyed_atsu 0:e8bfffbb3ab6 175
lynxeyed_atsu 0:e8bfffbb3ab6 176 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 177 * @brief Get I2S Buffer Level
lynxeyed_atsu 0:e8bfffbb3ab6 178 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 179 * @param[in] TRMode Transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 180 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 181 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 182 * @return current level of Transmit/Receive Buffer
lynxeyed_atsu 0:e8bfffbb3ab6 183 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 184 uint8_t I2S_GetLevel(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode)
lynxeyed_atsu 0:e8bfffbb3ab6 185 {
lynxeyed_atsu 0:e8bfffbb3ab6 186 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 187 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 188
lynxeyed_atsu 0:e8bfffbb3ab6 189 if(TRMode == I2S_TX_MODE)
lynxeyed_atsu 0:e8bfffbb3ab6 190 {
lynxeyed_atsu 0:e8bfffbb3ab6 191 return ((I2Sx->I2SSTATE >> 16) & 0xFF);
lynxeyed_atsu 0:e8bfffbb3ab6 192 }
lynxeyed_atsu 0:e8bfffbb3ab6 193 else
lynxeyed_atsu 0:e8bfffbb3ab6 194 {
lynxeyed_atsu 0:e8bfffbb3ab6 195 return ((I2Sx->I2SSTATE >> 8) & 0xFF);
lynxeyed_atsu 0:e8bfffbb3ab6 196 }
lynxeyed_atsu 0:e8bfffbb3ab6 197 }
lynxeyed_atsu 0:e8bfffbb3ab6 198
lynxeyed_atsu 0:e8bfffbb3ab6 199 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 200 * @brief I2S Start: clear all STOP,RESET and MUTE bit, ready to operate
lynxeyed_atsu 0:e8bfffbb3ab6 201 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 202 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 203 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 204 void I2S_Start(LPC_I2S_TypeDef *I2Sx)
lynxeyed_atsu 0:e8bfffbb3ab6 205 {
lynxeyed_atsu 0:e8bfffbb3ab6 206 //Clear STOP,RESET and MUTE bit
lynxeyed_atsu 0:e8bfffbb3ab6 207 I2Sx->I2SDAO &= ~I2S_DAI_RESET;
lynxeyed_atsu 0:e8bfffbb3ab6 208 I2Sx->I2SDAI &= ~I2S_DAI_RESET;
lynxeyed_atsu 0:e8bfffbb3ab6 209 I2Sx->I2SDAO &= ~I2S_DAI_STOP;
lynxeyed_atsu 0:e8bfffbb3ab6 210 I2Sx->I2SDAI &= ~I2S_DAI_STOP;
lynxeyed_atsu 0:e8bfffbb3ab6 211 I2Sx->I2SDAO &= ~I2S_DAI_MUTE;
lynxeyed_atsu 0:e8bfffbb3ab6 212 }
lynxeyed_atsu 0:e8bfffbb3ab6 213
lynxeyed_atsu 0:e8bfffbb3ab6 214 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 215 * @brief I2S Send data
lynxeyed_atsu 0:e8bfffbb3ab6 216 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 217 * @param[in] BufferData pointer to uint32_t is the data will be send
lynxeyed_atsu 0:e8bfffbb3ab6 218 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 219 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 220 void I2S_Send(LPC_I2S_TypeDef *I2Sx, uint32_t BufferData) {
lynxeyed_atsu 0:e8bfffbb3ab6 221 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 222
lynxeyed_atsu 0:e8bfffbb3ab6 223 I2Sx->I2STXFIFO = BufferData;
lynxeyed_atsu 0:e8bfffbb3ab6 224 }
lynxeyed_atsu 0:e8bfffbb3ab6 225
lynxeyed_atsu 0:e8bfffbb3ab6 226 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 227 * @brief I2S Receive Data
lynxeyed_atsu 0:e8bfffbb3ab6 228 * @param[in] I2Sx pointer to LPC_I2S_TypeDef
lynxeyed_atsu 0:e8bfffbb3ab6 229 * @return received value
lynxeyed_atsu 0:e8bfffbb3ab6 230 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 231 uint32_t I2S_Receive(LPC_I2S_TypeDef* I2Sx) {
lynxeyed_atsu 0:e8bfffbb3ab6 232 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 233
lynxeyed_atsu 0:e8bfffbb3ab6 234 return (I2Sx->I2SRXFIFO);
lynxeyed_atsu 0:e8bfffbb3ab6 235
lynxeyed_atsu 0:e8bfffbb3ab6 236 }
lynxeyed_atsu 0:e8bfffbb3ab6 237
lynxeyed_atsu 0:e8bfffbb3ab6 238 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 239 * @brief I2S Pause
lynxeyed_atsu 0:e8bfffbb3ab6 240 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 241 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 242 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 243 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 244 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 245 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 246 void I2S_Pause(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode) {
lynxeyed_atsu 0:e8bfffbb3ab6 247 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 248 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 249
lynxeyed_atsu 0:e8bfffbb3ab6 250 if (TRMode == I2S_TX_MODE) //Transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 251 {
lynxeyed_atsu 0:e8bfffbb3ab6 252 I2Sx->I2SDAO |= I2S_DAO_STOP;
lynxeyed_atsu 0:e8bfffbb3ab6 253 } else //Receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 254 {
lynxeyed_atsu 0:e8bfffbb3ab6 255 I2Sx->I2SDAI |= I2S_DAI_STOP;
lynxeyed_atsu 0:e8bfffbb3ab6 256 }
lynxeyed_atsu 0:e8bfffbb3ab6 257 }
lynxeyed_atsu 0:e8bfffbb3ab6 258
lynxeyed_atsu 0:e8bfffbb3ab6 259 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 260 * @brief I2S Mute
lynxeyed_atsu 0:e8bfffbb3ab6 261 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 262 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 263 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 264 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 265 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 266 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 267 void I2S_Mute(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode) {
lynxeyed_atsu 0:e8bfffbb3ab6 268 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 269 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 270
lynxeyed_atsu 0:e8bfffbb3ab6 271 if (TRMode == I2S_TX_MODE) //Transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 272 {
lynxeyed_atsu 0:e8bfffbb3ab6 273 I2Sx->I2SDAO |= I2S_DAO_MUTE;
lynxeyed_atsu 0:e8bfffbb3ab6 274 } else //Receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 275 {
lynxeyed_atsu 0:e8bfffbb3ab6 276 I2Sx->I2SDAI |= I2S_DAI_MUTE;
lynxeyed_atsu 0:e8bfffbb3ab6 277 }
lynxeyed_atsu 0:e8bfffbb3ab6 278 }
lynxeyed_atsu 0:e8bfffbb3ab6 279
lynxeyed_atsu 0:e8bfffbb3ab6 280 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 281 * @brief I2S Stop
lynxeyed_atsu 0:e8bfffbb3ab6 282 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 283 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 284 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 285 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 286 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 287 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 288 void I2S_Stop(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode) {
lynxeyed_atsu 0:e8bfffbb3ab6 289 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 290 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 291
lynxeyed_atsu 0:e8bfffbb3ab6 292 if (TRMode == I2S_TX_MODE) //Transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 293 {
lynxeyed_atsu 0:e8bfffbb3ab6 294 I2Sx->I2SDAO &= ~I2S_DAO_MUTE;
lynxeyed_atsu 0:e8bfffbb3ab6 295 I2Sx->I2SDAO |= I2S_DAO_STOP;
lynxeyed_atsu 0:e8bfffbb3ab6 296 I2Sx->I2SDAO |= I2S_DAO_RESET;
lynxeyed_atsu 0:e8bfffbb3ab6 297 } else //Receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 298 {
lynxeyed_atsu 0:e8bfffbb3ab6 299 I2Sx->I2SDAI |= I2S_DAI_STOP;
lynxeyed_atsu 0:e8bfffbb3ab6 300 I2Sx->I2SDAI |= I2S_DAI_RESET;
lynxeyed_atsu 0:e8bfffbb3ab6 301 }
lynxeyed_atsu 0:e8bfffbb3ab6 302 }
lynxeyed_atsu 0:e8bfffbb3ab6 303
lynxeyed_atsu 0:e8bfffbb3ab6 304 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 305 * @brief Set frequency for I2S
lynxeyed_atsu 0:e8bfffbb3ab6 306 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 307 * @param[in] Freq is the frequency for I2S will be set. It can range
lynxeyed_atsu 0:e8bfffbb3ab6 308 * from 16-96 kHz(16, 22.05, 32, 44.1, 48, 96kHz)
lynxeyed_atsu 0:e8bfffbb3ab6 309 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 310 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 311 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 312 * @return Status: ERROR or SUCCESS
lynxeyed_atsu 0:e8bfffbb3ab6 313 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 314 Status I2S_FreqConfig(LPC_I2S_TypeDef *I2Sx, uint32_t Freq, uint8_t TRMode) {
lynxeyed_atsu 0:e8bfffbb3ab6 315
lynxeyed_atsu 0:e8bfffbb3ab6 316 /* Calculate bit rate
lynxeyed_atsu 0:e8bfffbb3ab6 317 * The formula is:
lynxeyed_atsu 0:e8bfffbb3ab6 318 * bit_rate = channel*wordwidth - 1
lynxeyed_atsu 0:e8bfffbb3ab6 319 * 48kHz sample rate for 16 bit stereo date requires
lynxeyed_atsu 0:e8bfffbb3ab6 320 * a bit rate of 48000*16*2=1536MHz (MCLK)
lynxeyed_atsu 0:e8bfffbb3ab6 321 */
lynxeyed_atsu 0:e8bfffbb3ab6 322 uint32_t i2sPclk;
lynxeyed_atsu 0:e8bfffbb3ab6 323 uint64_t divider;
lynxeyed_atsu 0:e8bfffbb3ab6 324 uint8_t bitrate, channel, wordwidth;
lynxeyed_atsu 0:e8bfffbb3ab6 325 uint32_t x, y;
lynxeyed_atsu 0:e8bfffbb3ab6 326 uint16_t dif;
lynxeyed_atsu 0:e8bfffbb3ab6 327 uint16_t error;
lynxeyed_atsu 0:e8bfffbb3ab6 328 uint8_t x_divide, y_divide=0;
lynxeyed_atsu 0:e8bfffbb3ab6 329 uint16_t ErrorOptimal = 0xFFFF;
lynxeyed_atsu 0:e8bfffbb3ab6 330
lynxeyed_atsu 0:e8bfffbb3ab6 331 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 332 CHECK_PARAM(PRAM_I2S_FREQ(Freq));
lynxeyed_atsu 0:e8bfffbb3ab6 333 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 334
lynxeyed_atsu 0:e8bfffbb3ab6 335 i2sPclk = CLKPWR_GetPCLK(CLKPWR_PCLKSEL_I2S);
lynxeyed_atsu 0:e8bfffbb3ab6 336 printf("I2S_PCLK = %dHz\r\n",i2sPclk);
lynxeyed_atsu 0:e8bfffbb3ab6 337 if(TRMode == I2S_TX_MODE)
lynxeyed_atsu 0:e8bfffbb3ab6 338 {
lynxeyed_atsu 0:e8bfffbb3ab6 339 channel = i2s_GetChannel(I2Sx,I2S_TX_MODE);
lynxeyed_atsu 0:e8bfffbb3ab6 340 wordwidth = i2s_GetWordWidth(I2Sx,I2S_TX_MODE);
lynxeyed_atsu 0:e8bfffbb3ab6 341 }
lynxeyed_atsu 0:e8bfffbb3ab6 342 else
lynxeyed_atsu 0:e8bfffbb3ab6 343 {
lynxeyed_atsu 0:e8bfffbb3ab6 344 channel = i2s_GetChannel(I2Sx,I2S_RX_MODE);
lynxeyed_atsu 0:e8bfffbb3ab6 345 wordwidth = i2s_GetWordWidth(I2Sx,I2S_RX_MODE);
lynxeyed_atsu 0:e8bfffbb3ab6 346 }
lynxeyed_atsu 0:e8bfffbb3ab6 347 bitrate = channel * wordwidth - 1;
lynxeyed_atsu 0:e8bfffbb3ab6 348 if (TRMode == I2S_TX_MODE)// Transmitter
lynxeyed_atsu 0:e8bfffbb3ab6 349 {
lynxeyed_atsu 0:e8bfffbb3ab6 350 I2Sx->I2STXBITRATE = bitrate;
lynxeyed_atsu 0:e8bfffbb3ab6 351 } else //Receiver
lynxeyed_atsu 0:e8bfffbb3ab6 352 {
lynxeyed_atsu 0:e8bfffbb3ab6 353 I2Sx->I2SRXBITRATE = bitrate;
lynxeyed_atsu 0:e8bfffbb3ab6 354 }
lynxeyed_atsu 0:e8bfffbb3ab6 355 printf("TX CHANNEL= %d\r\n",i2s_GetChannel(I2Sx,I2S_TX_MODE));
lynxeyed_atsu 0:e8bfffbb3ab6 356 printf("TX WORDWIDTH= %dbits \r\n",i2s_GetWordWidth(I2Sx,I2S_TX_MODE));
lynxeyed_atsu 0:e8bfffbb3ab6 357 printf("I2STXBITRATE= %d,bitrate= %d \r\n",I2Sx->I2STXBITRATE,bitrate);
lynxeyed_atsu 0:e8bfffbb3ab6 358 /* Calculate X and Y divider
lynxeyed_atsu 0:e8bfffbb3ab6 359 * The MCLK rate for the I2S transmitter is determined by the value
lynxeyed_atsu 0:e8bfffbb3ab6 360 * in the I2STXRATE/I2SRXRATE register. The required I2STXRATE/I2SRXRATE
lynxeyed_atsu 0:e8bfffbb3ab6 361 * setting depends on the desired audio sample rate desired, the format
lynxeyed_atsu 0:e8bfffbb3ab6 362 * (stereo/mono) used, and the data size.
lynxeyed_atsu 0:e8bfffbb3ab6 363 * The formula is:
lynxeyed_atsu 0:e8bfffbb3ab6 364 * I2S_MCLK = PCLK * (X/Y) / 2
lynxeyed_atsu 0:e8bfffbb3ab6 365 * We have:
lynxeyed_atsu 0:e8bfffbb3ab6 366 * I2S_MCLK = Freq * bit_rate;
lynxeyed_atsu 0:e8bfffbb3ab6 367 * So: (X/Y) = (Freq * bit_rate)/PCLK*2
lynxeyed_atsu 0:e8bfffbb3ab6 368 * We use a loop function to chose the most suitable X,Y value
lynxeyed_atsu 0:e8bfffbb3ab6 369 */
lynxeyed_atsu 0:e8bfffbb3ab6 370
lynxeyed_atsu 0:e8bfffbb3ab6 371 divider = ((uint64_t)(Freq *( bitrate+1)/* * 2*/)<<16) / i2sPclk;
lynxeyed_atsu 0:e8bfffbb3ab6 372 for (y = 255; y > 0; y--) {
lynxeyed_atsu 0:e8bfffbb3ab6 373 x = y * divider;
lynxeyed_atsu 0:e8bfffbb3ab6 374 dif = x & 0xFFFF;
lynxeyed_atsu 0:e8bfffbb3ab6 375 if(dif>0x8000) error = 0x10000-dif;
lynxeyed_atsu 0:e8bfffbb3ab6 376 else error = dif;
lynxeyed_atsu 0:e8bfffbb3ab6 377 if (error == 0)
lynxeyed_atsu 0:e8bfffbb3ab6 378 {
lynxeyed_atsu 0:e8bfffbb3ab6 379 y_divide = y;
lynxeyed_atsu 0:e8bfffbb3ab6 380 break;
lynxeyed_atsu 0:e8bfffbb3ab6 381 }
lynxeyed_atsu 0:e8bfffbb3ab6 382 else if (error < ErrorOptimal)
lynxeyed_atsu 0:e8bfffbb3ab6 383 {
lynxeyed_atsu 0:e8bfffbb3ab6 384 ErrorOptimal = error;
lynxeyed_atsu 0:e8bfffbb3ab6 385 y_divide = y;
lynxeyed_atsu 0:e8bfffbb3ab6 386 }
lynxeyed_atsu 0:e8bfffbb3ab6 387 }
lynxeyed_atsu 0:e8bfffbb3ab6 388 //y_divide--;//for test
lynxeyed_atsu 0:e8bfffbb3ab6 389 x_divide = (y_divide * Freq *( bitrate+1)* 2)/i2sPclk;
lynxeyed_atsu 0:e8bfffbb3ab6 390 if (TRMode == I2S_TX_MODE)// Transmitter
lynxeyed_atsu 0:e8bfffbb3ab6 391 {
lynxeyed_atsu 0:e8bfffbb3ab6 392 I2Sx->I2STXRATE = y_divide | (x_divide << 8);
lynxeyed_atsu 0:e8bfffbb3ab6 393 } else //Receiver
lynxeyed_atsu 0:e8bfffbb3ab6 394 {
lynxeyed_atsu 0:e8bfffbb3ab6 395 I2Sx->I2SRXRATE = y_divide | (x_divide << 8);
lynxeyed_atsu 0:e8bfffbb3ab6 396 }
lynxeyed_atsu 0:e8bfffbb3ab6 397
lynxeyed_atsu 0:e8bfffbb3ab6 398 printf("I2STXRATE=%d, x_divide=%d, y_divide=%d \r\n",I2Sx->I2STXRATE,x_divide,y_divide);
lynxeyed_atsu 0:e8bfffbb3ab6 399 return SUCCESS;
lynxeyed_atsu 0:e8bfffbb3ab6 400 }
lynxeyed_atsu 0:e8bfffbb3ab6 401
lynxeyed_atsu 0:e8bfffbb3ab6 402 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 403 * @brief I2S set bitrate
lynxeyed_atsu 0:e8bfffbb3ab6 404 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 405 * @param[in] bitrate value will be set, it can be calculate as follows:
lynxeyed_atsu 0:e8bfffbb3ab6 406 * bitrate = channel * wordwidth - 1
lynxeyed_atsu 0:e8bfffbb3ab6 407 * bitrate value should be in range: 0 .. 63
lynxeyed_atsu 0:e8bfffbb3ab6 408 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 409 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 410 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 411 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 412 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 413 void I2S_SetBitRate(LPC_I2S_TypeDef *I2Sx, uint8_t bitrate, uint8_t TRMode)
lynxeyed_atsu 0:e8bfffbb3ab6 414 {
lynxeyed_atsu 0:e8bfffbb3ab6 415 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 416 CHECK_PARAM(PARAM_I2S_BITRATE(bitrate));
lynxeyed_atsu 0:e8bfffbb3ab6 417 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 418
lynxeyed_atsu 0:e8bfffbb3ab6 419 if(TRMode == I2S_TX_MODE)
lynxeyed_atsu 0:e8bfffbb3ab6 420 {
lynxeyed_atsu 0:e8bfffbb3ab6 421 I2Sx->I2STXBITRATE = bitrate;
lynxeyed_atsu 0:e8bfffbb3ab6 422 }
lynxeyed_atsu 0:e8bfffbb3ab6 423 else
lynxeyed_atsu 0:e8bfffbb3ab6 424 {
lynxeyed_atsu 0:e8bfffbb3ab6 425 I2Sx->I2SRXBITRATE = bitrate;
lynxeyed_atsu 0:e8bfffbb3ab6 426 }
lynxeyed_atsu 0:e8bfffbb3ab6 427 }
lynxeyed_atsu 0:e8bfffbb3ab6 428
lynxeyed_atsu 0:e8bfffbb3ab6 429 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 430 * @brief Configuration operating mode for I2S
lynxeyed_atsu 0:e8bfffbb3ab6 431 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 432 * @param[in] ModeConfig pointer to I2S_MODEConf_Type will be used to
lynxeyed_atsu 0:e8bfffbb3ab6 433 * configure
lynxeyed_atsu 0:e8bfffbb3ab6 434 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 435 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 436 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 437 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 438 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 439 void I2S_ModeConfig(LPC_I2S_TypeDef *I2Sx, I2S_MODEConf_Type* ModeConfig,
lynxeyed_atsu 0:e8bfffbb3ab6 440 uint8_t TRMode)
lynxeyed_atsu 0:e8bfffbb3ab6 441 {
lynxeyed_atsu 0:e8bfffbb3ab6 442 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 443 CHECK_PARAM(PARAM_I2S_CLKSEL(ModeConfig->clksel));
lynxeyed_atsu 0:e8bfffbb3ab6 444 CHECK_PARAM(PARAM_I2S_4PIN(ModeConfig->fpin));
lynxeyed_atsu 0:e8bfffbb3ab6 445 CHECK_PARAM(PARAM_I2S_MCLK(ModeConfig->mcena));
lynxeyed_atsu 0:e8bfffbb3ab6 446 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 447
lynxeyed_atsu 0:e8bfffbb3ab6 448 if (TRMode == I2S_TX_MODE) {
lynxeyed_atsu 0:e8bfffbb3ab6 449 I2Sx->I2STXMODE &= ~0x0F; //clear bit 3:0 in I2STXMODE register
lynxeyed_atsu 0:e8bfffbb3ab6 450 if (ModeConfig->clksel == I2S_CLKSEL_MCLK) {
lynxeyed_atsu 0:e8bfffbb3ab6 451 I2Sx->I2STXMODE |= 0x02;
lynxeyed_atsu 0:e8bfffbb3ab6 452 }
lynxeyed_atsu 0:e8bfffbb3ab6 453 if (ModeConfig->fpin == I2S_4PIN_ENABLE) {
lynxeyed_atsu 0:e8bfffbb3ab6 454 I2Sx->I2STXMODE |= (1 << 2);
lynxeyed_atsu 0:e8bfffbb3ab6 455 }
lynxeyed_atsu 0:e8bfffbb3ab6 456 if (ModeConfig->mcena == I2S_MCLK_ENABLE) {
lynxeyed_atsu 0:e8bfffbb3ab6 457 I2Sx->I2STXMODE |= (1 << 3);
lynxeyed_atsu 0:e8bfffbb3ab6 458 }
lynxeyed_atsu 0:e8bfffbb3ab6 459 } else {
lynxeyed_atsu 0:e8bfffbb3ab6 460 I2Sx->I2SRXMODE &= ~0x0F; //clear bit 3:0 in I2STXMODE register
lynxeyed_atsu 0:e8bfffbb3ab6 461 if (ModeConfig->clksel == I2S_CLKSEL_MCLK) {
lynxeyed_atsu 0:e8bfffbb3ab6 462 I2Sx->I2SRXMODE |= 0x02;
lynxeyed_atsu 0:e8bfffbb3ab6 463 }
lynxeyed_atsu 0:e8bfffbb3ab6 464 if (ModeConfig->fpin == I2S_4PIN_ENABLE) {
lynxeyed_atsu 0:e8bfffbb3ab6 465 I2Sx->I2SRXMODE |= (1 << 2);
lynxeyed_atsu 0:e8bfffbb3ab6 466 }
lynxeyed_atsu 0:e8bfffbb3ab6 467 if (ModeConfig->mcena == I2S_MCLK_ENABLE) {
lynxeyed_atsu 0:e8bfffbb3ab6 468 I2Sx->I2SRXMODE |= (1 << 3);
lynxeyed_atsu 0:e8bfffbb3ab6 469 }
lynxeyed_atsu 0:e8bfffbb3ab6 470 }
lynxeyed_atsu 0:e8bfffbb3ab6 471 }
lynxeyed_atsu 0:e8bfffbb3ab6 472
lynxeyed_atsu 0:e8bfffbb3ab6 473 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 474 * @brief Configure DMA operation for I2S
lynxeyed_atsu 0:e8bfffbb3ab6 475 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 476 * @param[in] DMAConfig pointer to I2S_DMAConf_Type will be used to configure
lynxeyed_atsu 0:e8bfffbb3ab6 477 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 478 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 479 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 480 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 481 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 482 void I2S_DMAConfig(LPC_I2S_TypeDef *I2Sx, I2S_DMAConf_Type* DMAConfig,
lynxeyed_atsu 0:e8bfffbb3ab6 483 uint8_t TRMode)
lynxeyed_atsu 0:e8bfffbb3ab6 484 {
lynxeyed_atsu 0:e8bfffbb3ab6 485 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 486 CHECK_PARAM(PARAM_I2S_DMA(DMAConfig->DMAIndex));
lynxeyed_atsu 0:e8bfffbb3ab6 487 CHECK_PARAM(PARAM_I2S_DMA_DEPTH(DMAConfig->depth));
lynxeyed_atsu 0:e8bfffbb3ab6 488 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 489
lynxeyed_atsu 0:e8bfffbb3ab6 490 if (TRMode == I2S_RX_MODE) {
lynxeyed_atsu 0:e8bfffbb3ab6 491 if (DMAConfig->DMAIndex == I2S_DMA_1) {
lynxeyed_atsu 0:e8bfffbb3ab6 492 LPC_I2S->I2SDMA1 = (DMAConfig->depth) << 8;
lynxeyed_atsu 0:e8bfffbb3ab6 493 } else {
lynxeyed_atsu 0:e8bfffbb3ab6 494 LPC_I2S->I2SDMA2 = (DMAConfig->depth) << 8;
lynxeyed_atsu 0:e8bfffbb3ab6 495 }
lynxeyed_atsu 0:e8bfffbb3ab6 496 } else {
lynxeyed_atsu 0:e8bfffbb3ab6 497 if (DMAConfig->DMAIndex == I2S_DMA_1) {
lynxeyed_atsu 0:e8bfffbb3ab6 498 LPC_I2S->I2SDMA1 = (DMAConfig->depth) << 16;
lynxeyed_atsu 0:e8bfffbb3ab6 499 } else {
lynxeyed_atsu 0:e8bfffbb3ab6 500 LPC_I2S->I2SDMA2 = (DMAConfig->depth) << 16;
lynxeyed_atsu 0:e8bfffbb3ab6 501 }
lynxeyed_atsu 0:e8bfffbb3ab6 502 }
lynxeyed_atsu 0:e8bfffbb3ab6 503 }
lynxeyed_atsu 0:e8bfffbb3ab6 504
lynxeyed_atsu 0:e8bfffbb3ab6 505 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 506 * @brief Enable/Disable DMA operation for I2S
lynxeyed_atsu 0:e8bfffbb3ab6 507 * @param[in] I2Sx: I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 508 * @param[in] DMAIndex chose what DMA is used, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 509 * - I2S_DMA_1 = 0: DMA1
lynxeyed_atsu 0:e8bfffbb3ab6 510 * - I2S_DMA_2 = 1: DMA2
lynxeyed_atsu 0:e8bfffbb3ab6 511 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 512 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 513 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 514 * @param[in] NewState is new state of DMA operation, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 515 * - ENABLE
lynxeyed_atsu 0:e8bfffbb3ab6 516 * - DISABLE
lynxeyed_atsu 0:e8bfffbb3ab6 517 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 518 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 519 void I2S_DMACmd(LPC_I2S_TypeDef *I2Sx, uint8_t DMAIndex, uint8_t TRMode,
lynxeyed_atsu 0:e8bfffbb3ab6 520 FunctionalState NewState)
lynxeyed_atsu 0:e8bfffbb3ab6 521 {
lynxeyed_atsu 0:e8bfffbb3ab6 522 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 523 CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
lynxeyed_atsu 0:e8bfffbb3ab6 524 CHECK_PARAM(PARAM_I2S_DMA(DMAIndex));
lynxeyed_atsu 0:e8bfffbb3ab6 525 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 526
lynxeyed_atsu 0:e8bfffbb3ab6 527 if (TRMode == I2S_RX_MODE) {
lynxeyed_atsu 0:e8bfffbb3ab6 528 if (DMAIndex == I2S_DMA_1) {
lynxeyed_atsu 0:e8bfffbb3ab6 529 if (NewState == ENABLE)
lynxeyed_atsu 0:e8bfffbb3ab6 530 I2Sx->I2SDMA1 |= 0x01;
lynxeyed_atsu 0:e8bfffbb3ab6 531 else
lynxeyed_atsu 0:e8bfffbb3ab6 532 I2Sx->I2SDMA1 &= ~0x01;
lynxeyed_atsu 0:e8bfffbb3ab6 533 } else {
lynxeyed_atsu 0:e8bfffbb3ab6 534 if (NewState == ENABLE)
lynxeyed_atsu 0:e8bfffbb3ab6 535 I2Sx->I2SDMA2 |= 0x01;
lynxeyed_atsu 0:e8bfffbb3ab6 536 else
lynxeyed_atsu 0:e8bfffbb3ab6 537 I2Sx->I2SDMA2 &= ~0x01;
lynxeyed_atsu 0:e8bfffbb3ab6 538 }
lynxeyed_atsu 0:e8bfffbb3ab6 539 } else {
lynxeyed_atsu 0:e8bfffbb3ab6 540 if (DMAIndex == I2S_DMA_1) {
lynxeyed_atsu 0:e8bfffbb3ab6 541 if (NewState == ENABLE)
lynxeyed_atsu 0:e8bfffbb3ab6 542 I2Sx->I2SDMA1 |= 0x02;
lynxeyed_atsu 0:e8bfffbb3ab6 543 else
lynxeyed_atsu 0:e8bfffbb3ab6 544 I2Sx->I2SDMA1 &= ~0x02;
lynxeyed_atsu 0:e8bfffbb3ab6 545 } else {
lynxeyed_atsu 0:e8bfffbb3ab6 546 if (NewState == ENABLE)
lynxeyed_atsu 0:e8bfffbb3ab6 547 I2Sx->I2SDMA2 |= 0x02;
lynxeyed_atsu 0:e8bfffbb3ab6 548 else
lynxeyed_atsu 0:e8bfffbb3ab6 549 I2Sx->I2SDMA2 &= ~0x02;
lynxeyed_atsu 0:e8bfffbb3ab6 550 }
lynxeyed_atsu 0:e8bfffbb3ab6 551 }
lynxeyed_atsu 0:e8bfffbb3ab6 552 }
lynxeyed_atsu 0:e8bfffbb3ab6 553
lynxeyed_atsu 0:e8bfffbb3ab6 554 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 555 * @brief Configure IRQ for I2S
lynxeyed_atsu 0:e8bfffbb3ab6 556 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 557 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 558 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 559 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 560 * @param[in] level is the FIFO level that triggers IRQ request
lynxeyed_atsu 0:e8bfffbb3ab6 561 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 562 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 563 void I2S_IRQConfig(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode, uint8_t level) {
lynxeyed_atsu 0:e8bfffbb3ab6 564 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 565 CHECK_PARAM(PARAM_I2S_TRX(TRMode));
lynxeyed_atsu 0:e8bfffbb3ab6 566 CHECK_PARAM(PARAM_I2S_IRQ_LEVEL(level));
lynxeyed_atsu 0:e8bfffbb3ab6 567
lynxeyed_atsu 0:e8bfffbb3ab6 568 if (TRMode == I2S_RX_MODE) {
lynxeyed_atsu 0:e8bfffbb3ab6 569 I2Sx->I2SIRQ |= (level << 8);
lynxeyed_atsu 0:e8bfffbb3ab6 570 } else {
lynxeyed_atsu 0:e8bfffbb3ab6 571 I2Sx->I2SIRQ |= (level << 16);
lynxeyed_atsu 0:e8bfffbb3ab6 572 }
lynxeyed_atsu 0:e8bfffbb3ab6 573 }
lynxeyed_atsu 0:e8bfffbb3ab6 574
lynxeyed_atsu 0:e8bfffbb3ab6 575 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 576 * @brief Enable/Disable IRQ for I2S
lynxeyed_atsu 0:e8bfffbb3ab6 577 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 578 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 579 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 580 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 581 * @param[in] NewState is new state of DMA operation, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 582 * - ENABLE
lynxeyed_atsu 0:e8bfffbb3ab6 583 * - DISABLE
lynxeyed_atsu 0:e8bfffbb3ab6 584 * @return none
lynxeyed_atsu 0:e8bfffbb3ab6 585 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 586 void I2S_IRQCmd(LPC_I2S_TypeDef *I2Sx, uint8_t TRMode, FunctionalState NewState) {
lynxeyed_atsu 0:e8bfffbb3ab6 587 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 588 CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
lynxeyed_atsu 0:e8bfffbb3ab6 589
lynxeyed_atsu 0:e8bfffbb3ab6 590 if (TRMode == I2S_RX_MODE) {
lynxeyed_atsu 0:e8bfffbb3ab6 591 if (NewState == ENABLE)
lynxeyed_atsu 0:e8bfffbb3ab6 592 I2Sx->I2SIRQ |= 0x01;
lynxeyed_atsu 0:e8bfffbb3ab6 593 else
lynxeyed_atsu 0:e8bfffbb3ab6 594 I2Sx->I2SIRQ &= ~0x01;
lynxeyed_atsu 0:e8bfffbb3ab6 595 //Enable DMA
lynxeyed_atsu 0:e8bfffbb3ab6 596
lynxeyed_atsu 0:e8bfffbb3ab6 597 } else {
lynxeyed_atsu 0:e8bfffbb3ab6 598 if (NewState == ENABLE)
lynxeyed_atsu 0:e8bfffbb3ab6 599 I2Sx->I2SIRQ |= 0x02;
lynxeyed_atsu 0:e8bfffbb3ab6 600 else
lynxeyed_atsu 0:e8bfffbb3ab6 601 I2Sx->I2SIRQ &= ~0x02;
lynxeyed_atsu 0:e8bfffbb3ab6 602 }
lynxeyed_atsu 0:e8bfffbb3ab6 603 }
lynxeyed_atsu 0:e8bfffbb3ab6 604
lynxeyed_atsu 0:e8bfffbb3ab6 605 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 606 * @brief Get I2S interrupt status
lynxeyed_atsu 0:e8bfffbb3ab6 607 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 608 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 609 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 610 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 611 * @return FunctionState should be:
lynxeyed_atsu 0:e8bfffbb3ab6 612 * - ENABLE: interrupt is enable
lynxeyed_atsu 0:e8bfffbb3ab6 613 * - DISABLE: interrupt is disable
lynxeyed_atsu 0:e8bfffbb3ab6 614 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 615 FunctionalState I2S_GetIRQStatus(LPC_I2S_TypeDef *I2Sx,uint8_t TRMode)
lynxeyed_atsu 0:e8bfffbb3ab6 616 {
lynxeyed_atsu 0:e8bfffbb3ab6 617 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 618 if(TRMode == I2S_TX_MODE)
lynxeyed_atsu 0:e8bfffbb3ab6 619 return (FunctionalState)((I2Sx->I2SIRQ >> 1)&0x01);
lynxeyed_atsu 0:e8bfffbb3ab6 620 else
lynxeyed_atsu 0:e8bfffbb3ab6 621 return (FunctionalState)((I2Sx->I2SIRQ)&0x01);
lynxeyed_atsu 0:e8bfffbb3ab6 622 }
lynxeyed_atsu 0:e8bfffbb3ab6 623
lynxeyed_atsu 0:e8bfffbb3ab6 624 /********************************************************************//**
lynxeyed_atsu 0:e8bfffbb3ab6 625 * @brief Get I2S interrupt depth
lynxeyed_atsu 0:e8bfffbb3ab6 626 * @param[in] I2Sx I2S peripheral selected, should be: LPC_I2S
lynxeyed_atsu 0:e8bfffbb3ab6 627 * @param[in] TRMode is transmit/receive mode, should be:
lynxeyed_atsu 0:e8bfffbb3ab6 628 * - I2S_TX_MODE = 0: transmit mode
lynxeyed_atsu 0:e8bfffbb3ab6 629 * - I2S_RX_MODE = 1: receive mode
lynxeyed_atsu 0:e8bfffbb3ab6 630 * @return depth of FIFO level on which to create an irq request
lynxeyed_atsu 0:e8bfffbb3ab6 631 *********************************************************************/
lynxeyed_atsu 0:e8bfffbb3ab6 632 uint8_t I2S_GetIRQDepth(LPC_I2S_TypeDef *I2Sx,uint8_t TRMode)
lynxeyed_atsu 0:e8bfffbb3ab6 633 {
lynxeyed_atsu 0:e8bfffbb3ab6 634 CHECK_PARAM(PARAM_I2Sx(I2Sx));
lynxeyed_atsu 0:e8bfffbb3ab6 635 if(TRMode == I2S_TX_MODE)
lynxeyed_atsu 0:e8bfffbb3ab6 636 return (((I2Sx->I2SIRQ)>>16)&0xFF);
lynxeyed_atsu 0:e8bfffbb3ab6 637 else
lynxeyed_atsu 0:e8bfffbb3ab6 638 return (((I2Sx->I2SIRQ)>>8)&0xFF);
lynxeyed_atsu 0:e8bfffbb3ab6 639 }
lynxeyed_atsu 0:e8bfffbb3ab6 640 /**
lynxeyed_atsu 0:e8bfffbb3ab6 641 * @}
lynxeyed_atsu 0:e8bfffbb3ab6 642 */
lynxeyed_atsu 0:e8bfffbb3ab6 643
lynxeyed_atsu 0:e8bfffbb3ab6 644 #endif /* _I2S */
lynxeyed_atsu 0:e8bfffbb3ab6 645
lynxeyed_atsu 0:e8bfffbb3ab6 646 /**
lynxeyed_atsu 0:e8bfffbb3ab6 647 * @}
lynxeyed_atsu 0:e8bfffbb3ab6 648 */
lynxeyed_atsu 0:e8bfffbb3ab6 649
lynxeyed_atsu 0:e8bfffbb3ab6 650 /* --------------------------------- End Of File ------------------------------ */