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

📄 main.c

📁 at91rm9200处理器i2s接口源代码
💻 C
字号:
//*----------------------------------------------------------------------------
//*         ATMEL Microcontroller Software Support  -  ROUSSET  -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name           : main.c
//* Object              : basic I2S application
//* Creation            : NLe  27/11/2002 
//* Rewrite for green hills and optimization : JPP 16/Jan/2003
//* Rewrite for AT91RM9200 16/May/2003
//*----------------------------------------------------------------------------
#include "main.h"

extern void debug_message(void);
extern void menu(char value);

int pool;
volatile unsigned int data_out;
volatile unsigned int data_out0;
volatile unsigned int data_in[2];

AT91PS_SSC	pSSC = AT91C_BASE_SSC1;

#define MCK	60000000
#define FILE_SAMPLING_FREQ	44100 // in Hz

// Constant declarations used by I2S mode
#define SLOT_BY_FRAME	2
#define BITS_BY_SLOT	16
// IRQ level declaration
#define IRQ_LEVEL_I2S	5	

// SSC_TCMR  configuration for I2S setting
// I2S_ASY_MASTER_TX_SETTING(BITS_BY_SLOT, SLOT_BY_FRAME);
#define I2S_ASY_MASTER_TX_SETTING(nb_bit_by_slot, nb_slot_by_frame)( +\
AT91C_SSC_CKS_DIV   +\
AT91C_SSC_CKO_CONTINOUS      +\
AT91C_SSC_START_FALL_RF +\
((1<<16) & AT91C_SSC_STTDLY) +\
((((nb_bit_by_slot*nb_slot_by_frame)/2)-1) <<24))

//AT91C_SSC_STTOUT  +\  with out no work FOR VERSION BEFORE Rev F
//AT91C_SSC_CKG_NONE    +\

//* Configuration to set in the SSC_TFMR Transmit Frame Mode Register
//* Parameters : nb_bit_by_slot : 8, 16 or 32 bits
//* 			 nb_slot_by_frame : number of channels
#define I2S_ASY_TX_FRAME_SETTING(nb_bit_by_slot, nb_slot_by_frame)( +\
(nb_bit_by_slot-1)  +\
AT91C_SSC_MSBF   +\
(((nb_slot_by_frame-1)<<8) & AT91C_SSC_DATNB)  +\
(((nb_bit_by_slot-1)<<16) & AT91C_SSC_FSLEN) +\
AT91C_SSC_FSOS_NEGATIVE)

//*--------------------------------------------------------------------------------------
//* Function Name       : stand_I2S_done
//* Object              : Standart I2S out 
//* Input Parameters    : none. 
//* Output Parameters   : none.
//*--------------------------------------------------------------------------------------
void stand_I2S_done(void)
{
    // ============================= Init SSC1 in Output mode =============================
	//* Configure SSC1 PIOs  TF/TK/TD 
	
	//* Configure SSC1 PIOs  TF/TK/TD  
	*AT91C_PIOB_PDR=    ((unsigned int) AT91C_PB7_TK1     ) |
		((unsigned int) AT91C_PB8_TD1     ) |
		((unsigned int) AT91C_PB6_TF1     );
    
    //* Configure PMC by enabling SSC1 clock    
	AT91F_SSC1_CfgPMC(); 
    //* Reset All the Peripheral 
	pSSC->SSC_CR = AT91C_SSC_SWRST ;

    //* Clear Transmit and Receive Counters
	AT91F_PDC_Close((AT91PS_PDC) &(pSSC->SSC_RPR));

    //* Define the Clock Mode Register at 2*16*44100 => 1.4112 MHz
	AT91F_SSC_SetBaudrate(pSSC, MCK, FILE_SAMPLING_FREQ*(BITS_BY_SLOT*SLOT_BY_FRAME));

    //* Write the Transmit Frame Mode Register
	pSSC->SSC_TFMR =  I2S_ASY_TX_FRAME_SETTING(BITS_BY_SLOT, SLOT_BY_FRAME);

    //* Configure AIC controller to handle SSC interrupts
	AT91F_AIC_ConfigureIt (
		AT91C_BASE_AIC,                        // AIC base address
		AT91C_ID_SSC1,                         // System peripheral ID
		IRQ_LEVEL_I2S,                         // Max priority
		AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, // Level sensitive
		AT91F_ASM_I2S_Handler );
    //* Enable SSC interrupt in AIC
	AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_SSC1);

    //* PDC configuration
	AT91F_PDC_SetTx ((AT91PS_PDC) &(pSSC->SSC_RPR),		// PDC SSC base address	
					 (char *)wav_file,	// pointer to data
					 AT91C_WAV_FILE_SIZE/2);// Number of 16 bits words
	AT91F_PDC_SetNextTx ((AT91PS_PDC) &(pSSC->SSC_RPR),	// PDC SSC base address
				         (char *)wav_file, 	// pointer to next data
				         AT91C_WAV_FILE_SIZE/2);// Number of 16 bits words
						 
    //* SSC Enable Interrupt 
	AT91F_SSC_EnableIt (pSSC, AT91C_SSC_ENDTX);
    //* Enable PDC feature
	AT91F_PDC_EnableTx ((AT91PS_PDC) &(pSSC->SSC_RPR));	

    //* Enable Transmit AND Start music !!!
        //* Write the Transmit Clock Mode Register and Enable TK and TF
	pSSC->SSC_TCMR =  I2S_ASY_MASTER_TX_SETTING(BITS_BY_SLOT, SLOT_BY_FRAME);
	//* Enable TX
	AT91F_SSC_EnableTx (pSSC);
	AT91F_DBGU_Printk("Check the output on J5 !!! (loop mode)\n\r");
}


//*--------------------------------------------------------------------------------------
//* Function Name       : polling
//* Object              : Use I2S in polling mode for debug only
//* Input Parameters    : none. 
//* Output Parameters   : none.
//*--------------------------------------------------------------------------------------
void polling(void)
{
    //* Configure SSC1 PIOs  TF/TK/TD
	*AT91C_PIOB_PDR= ((unsigned int) AT91C_PB7_TK1     ) |
			 ((unsigned int) AT91C_PB8_TD1     ) |
			 ((unsigned int) AT91C_PB6_TF1     );

    //* Configure PMC by enabling SSC1 clock  
       	*AT91C_PMC_PCER |= 1 << AT91C_ID_SSC1;    /* enable the SSC1 peripheral clock */
    //* Reset All the Peripheral 
     	*AT91C_SSC1_CR = AT91C_SSC_SWRST ;
    //* Clear Transmit and Receive Counters
	AT91F_PDC_Close((AT91PS_PDC) &(pSSC->SSC_RPR));
    //* Define the Clock Mode Register
    // for MCK ei 60000000 => 21 
     *AT91C_SSC1_CMR = 0x15;        
  
     *AT91C_SSC1_TCMR =  ((((BITS_BY_SLOT*SLOT_BY_FRAME)/2) -1) <<24) |
   		   ((1<<16) & AT91C_SSC_STTDLY) |
		   AT91C_SSC_START_FALL_RF | 
		   AT91C_SSC_CKO_CONTINOUS | 	// continuous transmit clock
		   AT91C_SSC_CKS_DIV; 		// Divided clock
     //set AT91C_SSC_STTOUT in TCMT +\  with out no work FOR VERSION BEFORE Rev F
     *AT91C_SSC1_TFMR =  
     		AT91C_SSC_FSOS_NEGATIVE 	|
		(((BITS_BY_SLOT-1)<<16) & AT91C_SSC_FSLEN)  |   // Fslen => 16 Clock
		(((SLOT_BY_FRAME-1)<<8) & AT91C_SSC_DATNB) |   // 2 Data by frame
		 AT91C_SSC_MSBF	    		|	// MSB in first
		 (BITS_BY_SLOT-1) ;		// 16 bits
	//* Enable TX
	*AT91C_SSC1_CR = AT91C_SSC_TXEN;    /* Enable Tx */
	
data_out=   0x8002;	
data_out0= 0x4001;	
	pool =1;
}

//*--------------------------------------------------------------------------------------
//* Function Name       : Main
//* Object              : Software entry point 
//* Input Parameters    : none. 
//* Output Parameters   : none.
//*--------------------------------------------------------------------------------------
int main()
{

    char value;
 	
	debug_message();
 	pool =0;
	AT91F_US_EnableRx((AT91PS_USART) AT91C_BASE_DBGU);
			
	AT91F_DBGU_Printk("Check the output on J5 !!! (loop mode)\n\r");
	//  AT91RM9200    connector        Pin
	// AT91C_PB8_TD1  J5           =>  4	
	// AT91C_PB7_TK1  J5           =>  5
	// AT91C_PB6_TF1  J5           =>  6 
	//     GND        J5           =>  1 
	//     GND        J5           =>  2 

	while (1)
	{
	  if ( pool ==1)
	  {
	  *AT91C_SSC1_THR = data_out;   /* Write the TD data */
	  while((*AT91C_SSC1_SR & AT91C_SSC_TXRDY)==0){ } /* Wait ready */	  
	  *AT91C_SSC1_THR = data_out0;   /* Write the TD data */
	  while((*AT91C_SSC1_SR & AT91C_SSC_TXRDY)==0){ } /* Wait ready */	  

	  }
	 if (AT91F_US_Get(&value) == 0 ) menu(value);
	}
}

⌨️ 快捷键说明

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