Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 22nd, 2010 | Syntax: None | Size: 11.04 KB | Hits: 115 | Expires: Never
Copy text to clipboard
  1. //codec. c
  2.  
  3. #include <4510addr.h>
  4. #include <codec.h>
  5.  
  6. /*************************************************************************
  7. Constant define for AIC12
  8. *************************************************************************/
  9. #define AIC_CONTROL1    0x01
  10. #define AIC_CONTROL2    0x02
  11. #define AIC_CONTROL3    0x03
  12. #define AIC_CONTROL4    0x04
  13. #define AIC_CONTROL5    0x05
  14. #define AIC_CONTROL6    0x06
  15.  
  16. UC mic_gain, spk_gain, sampling_rate;
  17. UI aic_in, aic_out;
  18. UL record_count;
  19. UI *aic_record_ptr, *aic_play_ptr, *aic_buf_ptr;
  20. UC aic_record_on, aic_play_on;
  21. UI AIC_dummy;
  22. UL aic_buf_size;
  23. extern unsigned   short cpld_ctrl;
  24.  
  25. float call = 0;
  26. float freq = 0;
  27. int highLow = 0;
  28. float Medium[7] = {30.53, 27.11, 24.09, 22.72, 20.25, 18.05, 16.09}; // 中音音階
  29. float High[7] = {15.26, 13.60, 12.12, 11.44, 10.19, 9.08, 8.09}; // 高音音階
  30. float Low[7] = {61.06, 54.42, 48.48, 45.71, 40.81, 36.36, 32.38}; // 低音音階
  31.  
  32. /////////////////////////////////////////////////////////////////////////////////////////
  33. //for test
  34. /////////////////////////////////////////////////////////////////////////////////////////
  35. void IRQ_CODEC( int irq, void *dev_id, struct pt_regs *regs)
  36. {
  37.     UI data_in;
  38.     data_in = CODEC_DATA;       //get data and release IRQ2
  39.    
  40.     if(aic_record_on == OK) {
  41.         if(aic_record_ptr != 0) {
  42.             *aic_record_ptr = data_in;
  43.             if(aic_record_ptr < (UI *)(aic_buf_size+aic_buf_ptr)) {
  44.                 aic_record_ptr++;
  45.             }
  46.         }
  47.     }
  48.     if(aic_play_on == OK) {
  49.         if(aic_play_ptr != 0) {
  50.             if(aic_play_ptr >= aic_record_ptr) {
  51.                 aic_play_ptr = aic_buf_ptr;
  52.             }
  53.             CODEC_DATA = *aic_play_ptr++;
  54.         }
  55.         else {
  56.             CODEC_DATA = 0;
  57.         }
  58.     }
  59.  
  60.         call++; // 代表每次呼叫時增加1
  61.        
  62.         if(call > 8000/262)
  63.         {
  64.                 highLow ^= 1;
  65.                 call = 0;
  66.         }
  67.        
  68.         if(highLow)
  69.                 CODEC_DATA = 0x9999;
  70.         else
  71.                 CODEC_DATA = 0x0000;
  72.  
  73.    
  74.     ClearPending(BIT_EXT2);
  75.    
  76. }
  77.  
  78. /*************************************************************************
  79. Utlitiy functions
  80. *************************************************************************/
  81. void CODEC_malloc(UL size)
  82. {
  83.     aic_buf_size = size;
  84.     aic_buf_ptr = (UI *)malloc(size);
  85.  
  86. }
  87.  
  88. //////////////////////////////////////////////////////////////////////////
  89. void CODEC_mfree(void)
  90. {
  91.     aic_buf_size = 0;
  92.     free(aic_buf_ptr);
  93.     aic_buf_ptr = 0;
  94. }
  95.  
  96. //////////////////////////////////////////////////////////////////////////
  97. void CODEC_Start(void)
  98. {
  99.  
  100.     pISR_EXT2 = IRQ_CODEC;
  101.     EnableInterrupt(BIT_EXT2);
  102.     AIC_dummy = CODEC_DATA;
  103.        
  104. }
  105.  
  106. //////////////////////////////////////////////////////////////////////////
  107. void CODEC_Stop(void)
  108. {
  109.     //disable IRQ
  110.     DisableInterrupt(BIT_EXT2);
  111.  
  112. }
  113.  
  114. //////////////////////////////////////////////////////////////////////////
  115. void CODEC_Record_Start(void)
  116. {
  117.        
  118.     //ADC_in -> buffer++
  119.     if(aic_record_on != OK) {
  120.         aic_record_ptr = aic_buf_ptr;
  121.         aic_record_on = OK;
  122.     }
  123. }
  124.  
  125. //////////////////////////////////////////////////////////////////////////
  126. void CODEC_Record_Stop(void)
  127. {
  128.     //ADC_in -> null
  129.     aic_record_on = UM;
  130. }
  131.  
  132. //////////////////////////////////////////////////////////////////////////
  133. void CODEC_Play_Start(void)
  134. {
  135.     //buffer++ -> DAC_out
  136.     if(aic_play_on != OK) {
  137.         aic_play_ptr = aic_buf_ptr;
  138.         aic_play_on = OK;
  139.         cpld_ctrl |= 0x02;      //AIC_ON
  140.         CPLD_CTRL = cpld_ctrl;
  141.     }
  142.    
  143. }
  144.  
  145. //////////////////////////////////////////////////////////////////////////
  146. void CODEC_Play_Stop(void)
  147. {
  148.     //0000 -> DAC_out
  149.     aic_play_on = UM;
  150.     cpld_ctrl &= 0xfd;  //AIC_OFF
  151.     CPLD_CTRL = cpld_ctrl;
  152. }
  153.  
  154. /*************************************************************************
  155. initial and exit
  156. *************************************************************************/
  157. void init_codec(void)
  158. {
  159.  
  160.     //CODEC chip initialization (8K Sampling rate)
  161.     IICWriteAIC(4, 0x8a);       //CR4 : M=10
  162.     IICWriteAIC(4, 0x42);       //CR4 : N=4, P=2
  163.     IICWriteAIC(2, 0xa0);       //CR2 : TURBO MODE
  164.     IICWriteAIC(3, 0x01);       //CR3 : Normal
  165.     IICWriteAIC(5, 0x34);       //CR5 : A2D Gain = +10dB
  166.     IICWriteAIC(5, 0x56);       //CR5 : D2A Gain = -20dB
  167.     IICWriteAIC(5, 0xbb);       //CR5 : A2D Preamp = 24dB
  168.     IICWriteAIC(6, 0x9a);       //CR6 :
  169.    
  170.     //Variables initialization
  171.     aic_record_on = UM;
  172.     aic_play_on = UM;
  173.     aic_record_ptr = (UI *)0x000000;
  174.     aic_play_ptr = (UI *)0x000000;
  175.     aic_buf_size = 0;
  176.    
  177.     //Dummy read for INT2=1
  178.     aic_in = CODEC_DATA;       
  179.    
  180.     //Turn OFF the CODEC function
  181.     cpld_ctrl &= 0xfd;         
  182.     CPLD_CTRL = cpld_ctrl;
  183.    
  184.     //Setup the CPU IRQ2 for CODEC operation
  185.     //P10 = input = IRQ2 ; IRQ2 = Falling edge
  186.     IOPCON = (IOPCON & 0xffff83ff) | (0x12 << 10);     
  187.     IOPMOD = IOPMOD & 0xfffffbff;      
  188.     IOPDATA = 0x00000000;
  189.        
  190.         call = 0;
  191.         highLow = 0;
  192.        
  193.     return 0;
  194. }
  195.  
  196. //////////////////////////////////////////////////////////////////////////
  197. void exit_codec(void)
  198. {
  199.     CODEC_Stop();
  200.     CODEC_mfree();
  201.  
  202. }
  203.  
  204. void Playnote(int mode, int key)
  205. {
  206.         LCD_printf("hello\n");
  207.         if(mode == 1)
  208.         {
  209.                 switch(key)
  210.                 {
  211.                         case 1:
  212.                                 freq = Low[0];
  213.                                 break;
  214.                         case 2:
  215.                                 freq = Low[1];
  216.                                 break;
  217.                         case 3:
  218.                                 freq = Low[2];
  219.                                 break;
  220.                         case 4:
  221.                                 freq = Low[3];
  222.                                 break;
  223.                         case 5:
  224.                                 freq = Low[4];
  225.                                 break;
  226.                         case 6:
  227.                                 freq = Low[5];
  228.                                 break;
  229.                         case 7:
  230.                                 freq = Low[6];
  231.                                 break;                                         
  232.                 }
  233.         }
  234.         else if(mode == 2)
  235.         {
  236.                 switch(key)
  237.                 {
  238.                         case 1:
  239.                                 freq = Medium[0];
  240.                                 break;
  241.                         case 2:
  242.                                 freq = Medium[1];
  243.                                 break;
  244.                         case 3:
  245.                                 freq = Medium[2];
  246.                                 break;
  247.                         case 4:
  248.                                 freq = Medium[3];
  249.                                 break;
  250.                         case 5:
  251.                                 freq = Medium[4];
  252.                                 break;
  253.                         case 6:
  254.                                 freq = Medium[5];
  255.                                 break;
  256.                         case 7:
  257.                                 freq = Medium[6];
  258.                                 break;                 
  259.                 }
  260.         }
  261.         else if(mode == 3)
  262.         {
  263.                 switch(key)
  264.                 {
  265.                         case 1:
  266.                                 freq = High[0];
  267.                                 break;
  268.                         case 2:
  269.                                 freq = High[1];
  270.                                 break;
  271.                         case 3:
  272.                                 freq = High[2];
  273.                                 break;
  274.                         case 4:
  275.                                 freq = High[3];
  276.                                 break;
  277.                         case 5:
  278.                                 freq = High[4];
  279.                                 break;
  280.                         case 6:
  281.                                 freq = High[5];
  282.                                 break;
  283.                         case 7:
  284.                                 freq = High[6];
  285.                                 break;                 
  286.                 }
  287.         }
  288.         Delay(500);
  289. }
  290.  
  291.  
  292.  
  293.  
  294. // --------------------------------------------------------------------
  295. //
  296. //   Title     :  MAIN
  297. //             :
  298. //   Library   :
  299. //             :
  300. //   Developers:  MICROTIME MDS group
  301. //             :
  302. //   Purpose   :  Main Program for Creator Board
  303. //             :
  304. //   Limitation:
  305. //             :
  306. //   Note      :
  307. //             :
  308. // --------------------------------------------------------------------
  309. //   modification history :
  310. // --------------------------------------------------------------------
  311. //   Version| mod. date: |
  312. //   V1.0   | 03/31/2003 | First release
  313. // --------------------------------------------------------------------
  314. //
  315. // Note:
  316. //
  317. //       MICROTIME COMPUTER INC.
  318. //
  319. //
  320.  
  321.  
  322. /*************************************************************************
  323. Include files
  324. *************************************************************************/
  325. #include <stdio.h>
  326. #include <stdlib.h>
  327. #include <math.h>
  328. #include "driver.h"
  329. #include "codec.h"
  330. #include "lcd.h"
  331.  
  332.  
  333. /*************************************************************************
  334. Function prototypes
  335. *************************************************************************/
  336. //void  WaitChar(void);
  337. //UC    Test_AIC(void);
  338.  
  339. void PressKey(int);
  340.  
  341. /*************************************************************************
  342. Variables
  343. *************************************************************************/
  344. int mode = 0;
  345.  
  346. /*************************************************************************
  347. System Initialize
  348. *************************************************************************/
  349. void Initial_Creator(void)
  350. {
  351.        
  352.         //S3C4510 Peripherals Initialization
  353.         Initial_TIMER0();               //Generate 1ms interrupt
  354.         IOPMOD = 0x00030000;    //for testing purposes
  355.         IOPCON = 0x40000000;    //for testing purposes
  356.  
  357.         //Initial IO_REG0
  358.         io_reg0 = 0xc000;
  359.         IO_REG0 = io_reg0;
  360.         Delay(1);
  361.  
  362.         //Initial CPLD_CTRL
  363.         cpld_ctrl = 0x3C;
  364.         CPLD_CTRL = cpld_ctrl;
  365.         Delay(500);
  366.         cpld_ctrl |= 0x01;    //Release IO reset
  367.         CPLD_CTRL = cpld_ctrl;
  368.  
  369.         Delay(500);
  370.        
  371.         Initial_LCD();
  372.         LCD_EnableCursor(TRUE);
  373.         Initial_KEYPAD();
  374.         init_codec();
  375.                
  376.         mode = 0;
  377. }
  378.  
  379.  
  380.  
  381.  
  382. /*************************************************************************
  383. MAIN Program
  384. *************************************************************************/
  385. int main(void)
  386. {
  387.         Initial_Creator();
  388.         EnableInterrupt(BIT_GMASK);
  389.  
  390.         LCD_printf("Choose mode:\n");
  391.         LCD_printf("1 : Low scale\n");
  392.         LCD_printf("2 : Medium scale\n");
  393.         LCD_printf("3 : High scale\n");
  394. /*     
  395.         while(mode == 0)
  396.         {
  397.                 if(IO_REG1 & 1)
  398.                         mode = 1;
  399.                 else if(IO_REG1 & 2)
  400.                         mode = 2;
  401.                 else if(IO_REG1 & 4)
  402.                         mode = 3;
  403.         }
  404. */                     
  405.         PressKey(mode);
  406.        
  407.         LCD_printf("The End!\n");
  408.     DisableInterrupt(BIT_GMASK);
  409.     exit_codec();
  410.     return(0);
  411. }
  412.  
  413. void PressKey(int mode)
  414. {
  415.         UC key;
  416.         LCD_printf("hihi\n");
  417.        
  418.     CODEC_malloc(0x400000);
  419.     CODEC_Start();
  420.  
  421.         LCD_printf("Press 1~7 to play notes\n");
  422. /*     
  423.         do{
  424.                 while(KEYPAD_get_key(&key) != OK);
  425.                 Playnote(mode, key);
  426.         }while(key != 0);
  427. */     
  428.         CODEC_Stop();
  429.         LCD_printf("\n\nEOF CODEC Test\n");
  430.         CODEC_mfree();
  431. }
  432.  
  433.  
  434.  
  435. /*************************************************************************
  436. Wait key
  437. *************************************************************************
  438. void WaitChar(void)
  439. {
  440.         UC key;
  441.         while(KEYPAD_get_key(&key) != OK);
  442. }
  443. */
  444. /*************************************************************************
  445. Get Key
  446. *************************************************************************
  447. UC GetIntNumRange (UC MinNum, UC MaxNum)
  448. {
  449.         UC ch;
  450.         if(KEYPAD_get_key(&ch))
  451.                 if(ch >= MinNum && ch <= MaxNum) return(ch);
  452.         return (0xff);
  453. }
  454. */
  455. /*************************************************************************
  456. Test Codec
  457. *************************************************************************
  458. UC Test_AIC(void)
  459. {
  460.         UC num;
  461.  
  462.         LCD_ClearScreen();
  463.         LCD_printf("CODEC Test...\n\n");
  464.        
  465.         LCD_printf("0=QUIT\n");
  466.         LCD_printf("1=Record  Start\n");
  467.         LCD_printf("2=Play    Start\n");
  468.         LCD_printf("3=Rec/Play Stop\n\n");
  469.        
  470.         CODEC_malloc(0x400000);
  471.        
  472.         CODEC_Start();
  473.         do {
  474.                 while((num=GetIntNumRange (0, 9)) == 0xff);
  475.                 LCD_Cursor(0, 7);
  476.                 switch(num) {
  477.                         case 1 :        //start record
  478.                         LCD_printf("Start Recording");
  479.                                 CODEC_Record_Start();
  480.                                 break;
  481.                         case 2 :        //start play
  482.                                 LCD_printf("Start Playing  ");
  483.                                 CODEC_Play_Start();
  484.                                 break;
  485.                         case 3 :        //stop
  486.                                 LCD_printf("Stop Rec/Play  ");
  487.                                 CODEC_Record_Stop();
  488.                                 CODEC_Play_Stop();
  489.                                 break;
  490.                         case 0 :        //quit
  491.                                 LCD_printf("Quit CODEC Test");
  492.                                 CODEC_Record_Stop();
  493.                                 CODEC_Play_Stop();
  494.                                 break;
  495.                 }
  496.         }while(num != 0);
  497.         CODEC_Stop();
  498.         LCD_printf("\n\nEOF CODEC Test\n");
  499.         CODEC_mfree();
  500.         return(OK);
  501. }
  502. */