Mark B / Mbed 2 deprecated i2s

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers i2s.cpp Source File

i2s.cpp

00001 /*****************************************************************************
00002  *   i2s.c:  I2S C file for NXP LPC17xx Family Microprocessors
00003  *
00004  *   Copyright(C) 2009, NXP Semiconductor
00005  *   All rights reserved.
00006  *
00007  *   History
00008  *   2009.05.26  ver 1.00    Prelimnary version, first Release
00009  *
00010 *****************************************************************************/
00011 #include "mbed.h"
00012 #include "type.h"
00013 #include "i2s.h"
00014 #include "dma.h"
00015 
00016 /* treat I2S TX and RX as a constant address, make the code and buffer 
00017 easier for both DMA and non-DMA test */
00018 volatile uint8_t *I2STXBuffer = (uint8_t *)(DMA_SRC); 
00019 volatile uint8_t *I2SRXBuffer = (uint8_t *)(DMA_DST);
00020 volatile uint32_t I2SReadLength = 0;
00021 volatile uint32_t I2SWriteLength = 0;
00022 volatile uint32_t I2SRXDone = 0, I2STXDone = 0;
00023 
00024 /*****************************************************************************
00025 ** Function name:       I2S_IRQHandler
00026 **
00027 ** Descriptions:        I2S interrupt handler, only RX interrupt is enabled
00028 **                      for simplicity.
00029 **
00030 ** parameters:          None
00031 ** Returned value:      None
00032 ** 
00033 *****************************************************************************/
00034 void I2S_IRQHandler (void)  
00035 {
00036   uint32_t RxCount = 0;
00037 
00038   if ( LPC_I2S->I2SSTATE & 0x01 )
00039   {
00040     RxCount = (LPC_I2S->I2SSTATE >> 8) & 0xFF;
00041     if ( (RxCount != RXFIFO_EMPTY) && !I2SRXDone )
00042     {
00043       while ( RxCount > 0 )
00044       {
00045         if ( I2SReadLength == BUFSIZE )
00046         {
00047           LPC_I2S->I2SDAI |= ((0x01 << 3) | (0x01 << 4));
00048           LPC_I2S->I2SIRQ &= ~(0x01 << 0);  /* Disable RX */    
00049           I2SRXDone = 1;
00050           break;
00051         }
00052         else
00053         {
00054           I2SRXBuffer[I2SReadLength++] = LPC_I2S->I2SRXFIFO;
00055         }
00056         RxCount--;
00057       }
00058     }
00059   }
00060   return;
00061 }
00062 
00063 /*****************************************************************************
00064 ** Function name:       I2SStart
00065 **
00066 ** Descriptions:        Start I2S DAI and DAO
00067 **
00068 ** parameters:          None
00069 ** Returned value:      None
00070 ** 
00071 *****************************************************************************/
00072 void I2SStart( void )
00073 {
00074   uint32_t DAIValue, DAOValue;
00075   
00076   /* Audio output is the master, audio input is the slave, */
00077   /* 16 bit data, stereo, reset, master mode, not mute. */
00078   DAOValue = LPC_I2S->I2SDAO;
00079   DAIValue = LPC_I2S->I2SDAI;
00080   LPC_I2S->I2SDAO = DAOValue & (~((0x01 << 4)|(0x01 <<3)));
00081   /* 16 bit data, stereo, reset, slave mode, not mute. */
00082   LPC_I2S->I2SDAI = DAIValue & (~((0x01 << 4)|(0x01 <<3)));
00083   return;
00084 }
00085 
00086 /*****************************************************************************
00087 ** Function name:       I2SStop
00088 **
00089 ** Descriptions:        Stop I2S DAI and DAO
00090 **
00091 ** parameters:          None
00092 ** Returned value:      None
00093 ** 
00094 *****************************************************************************/
00095 void I2SStop( void )
00096 {
00097   uint32_t DAIValue, DAOValue;
00098 
00099   /* Stop the I2S to start. Audio output is master, audio input is the slave. */
00100   /* 16 bit data, set STOP and RESET bits to reset the channels */
00101   DAOValue = LPC_I2S->I2SDAO;
00102   /* Switch to master mode, TX channel, no mute */
00103   DAOValue &= ~((0x01 << 5)|(0x01 << 15));
00104   DAIValue = LPC_I2S->I2SDAI;
00105   DAIValue &= ~(0x01 << 15);
00106   LPC_I2S->I2SDAO = (0x01 << 4) | (0x01 << 3) | DAOValue;   /* Master */
00107   LPC_I2S->I2SDAI = (0x01 << 4) | (0x01 << 3) | DAIValue;   /* Slave */
00108   return;
00109 }
00110 
00111 /*****************************************************************************
00112 ** Function name:       I2SInit
00113 **
00114 ** Descriptions:        Initialize I2S controller
00115 **
00116 ** parameters:          None
00117 ** Returned value:      true or false, return false if the I2S
00118 **                      interrupt handler was not installed correctly
00119 ** 
00120 *****************************************************************************/
00121 uint32_t I2SInit( void ) 
00122 {
00123 
00124   /*enable I2S in the PCONP register. I2S is disabled on reset*/
00125   LPC_SC->PCONP |= (1 << 27);
00126 
00127   /*connect the I2S sigals to port pins(P0.4-P0.9)*/
00128   LPC_PINCON->PINSEL0 &= ~0x000FFF00;
00129   LPC_PINCON->PINSEL0 |= 0x00055500;
00130 
00131   /* Please note, in order to generate accurate TX/RX clock rate for I2S, 
00132   PCLK and CCLK needs to be carefully reconsidered. For this test 
00133   program, the TX is looped back to RX without external I2S device, 
00134   clock rate is not critical in this matter. */
00135   LPC_I2S->I2STXRATE = 0x241;
00136   LPC_I2S->I2SRXRATE = 0x241;
00137 
00138   I2SStop();
00139 
00140   NVIC_EnableIRQ(I2S_IRQn);
00141   return( TRUE );
00142 }
00143 
00144 /******************************************************************************
00145 **                            End Of File
00146 ******************************************************************************/
00147