- //codec. c
- #include <4510addr.h>
- #include <codec.h>
- /*************************************************************************
- Constant define for AIC12
- *************************************************************************/
- #define AIC_CONTROL1 0x01
- #define AIC_CONTROL2 0x02
- #define AIC_CONTROL3 0x03
- #define AIC_CONTROL4 0x04
- #define AIC_CONTROL5 0x05
- #define AIC_CONTROL6 0x06
- UC mic_gain, spk_gain, sampling_rate;
- UI aic_in, aic_out;
- UL record_count;
- UI *aic_record_ptr, *aic_play_ptr, *aic_buf_ptr;
- UC aic_record_on, aic_play_on;
- UI AIC_dummy;
- UL aic_buf_size;
- extern unsigned short cpld_ctrl;
- float call = 0;
- float freq = 0;
- int highLow = 0;
- float Medium[7] = {30.53, 27.11, 24.09, 22.72, 20.25, 18.05, 16.09}; // 中音音階
- float High[7] = {15.26, 13.60, 12.12, 11.44, 10.19, 9.08, 8.09}; // 高音音階
- float Low[7] = {61.06, 54.42, 48.48, 45.71, 40.81, 36.36, 32.38}; // 低音音階
- /////////////////////////////////////////////////////////////////////////////////////////
- //for test
- /////////////////////////////////////////////////////////////////////////////////////////
- void IRQ_CODEC( int irq, void *dev_id, struct pt_regs *regs)
- {
- UI data_in;
- data_in = CODEC_DATA; //get data and release IRQ2
- if(aic_record_on == OK) {
- if(aic_record_ptr != 0) {
- *aic_record_ptr = data_in;
- if(aic_record_ptr < (UI *)(aic_buf_size+aic_buf_ptr)) {
- aic_record_ptr++;
- }
- }
- }
- if(aic_play_on == OK) {
- if(aic_play_ptr != 0) {
- if(aic_play_ptr >= aic_record_ptr) {
- aic_play_ptr = aic_buf_ptr;
- }
- CODEC_DATA = *aic_play_ptr++;
- }
- else {
- CODEC_DATA = 0;
- }
- }
- call++; // 代表每次呼叫時增加1
- if(call > 8000/262)
- {
- highLow ^= 1;
- call = 0;
- }
- if(highLow)
- CODEC_DATA = 0x9999;
- else
- CODEC_DATA = 0x0000;
- ClearPending(BIT_EXT2);
- }
- /*************************************************************************
- Utlitiy functions
- *************************************************************************/
- void CODEC_malloc(UL size)
- {
- aic_buf_size = size;
- aic_buf_ptr = (UI *)malloc(size);
- }
- //////////////////////////////////////////////////////////////////////////
- void CODEC_mfree(void)
- {
- aic_buf_size = 0;
- free(aic_buf_ptr);
- aic_buf_ptr = 0;
- }
- //////////////////////////////////////////////////////////////////////////
- void CODEC_Start(void)
- {
- pISR_EXT2 = IRQ_CODEC;
- EnableInterrupt(BIT_EXT2);
- AIC_dummy = CODEC_DATA;
- }
- //////////////////////////////////////////////////////////////////////////
- void CODEC_Stop(void)
- {
- //disable IRQ
- DisableInterrupt(BIT_EXT2);
- }
- //////////////////////////////////////////////////////////////////////////
- void CODEC_Record_Start(void)
- {
- //ADC_in -> buffer++
- if(aic_record_on != OK) {
- aic_record_ptr = aic_buf_ptr;
- aic_record_on = OK;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CODEC_Record_Stop(void)
- {
- //ADC_in -> null
- aic_record_on = UM;
- }
- //////////////////////////////////////////////////////////////////////////
- void CODEC_Play_Start(void)
- {
- //buffer++ -> DAC_out
- if(aic_play_on != OK) {
- aic_play_ptr = aic_buf_ptr;
- aic_play_on = OK;
- cpld_ctrl |= 0x02; //AIC_ON
- CPLD_CTRL = cpld_ctrl;
- }
- }
- //////////////////////////////////////////////////////////////////////////
- void CODEC_Play_Stop(void)
- {
- //0000 -> DAC_out
- aic_play_on = UM;
- cpld_ctrl &= 0xfd; //AIC_OFF
- CPLD_CTRL = cpld_ctrl;
- }
- /*************************************************************************
- initial and exit
- *************************************************************************/
- void init_codec(void)
- {
- //CODEC chip initialization (8K Sampling rate)
- IICWriteAIC(4, 0x8a); //CR4 : M=10
- IICWriteAIC(4, 0x42); //CR4 : N=4, P=2
- IICWriteAIC(2, 0xa0); //CR2 : TURBO MODE
- IICWriteAIC(3, 0x01); //CR3 : Normal
- IICWriteAIC(5, 0x34); //CR5 : A2D Gain = +10dB
- IICWriteAIC(5, 0x56); //CR5 : D2A Gain = -20dB
- IICWriteAIC(5, 0xbb); //CR5 : A2D Preamp = 24dB
- IICWriteAIC(6, 0x9a); //CR6 :
- //Variables initialization
- aic_record_on = UM;
- aic_play_on = UM;
- aic_record_ptr = (UI *)0x000000;
- aic_play_ptr = (UI *)0x000000;
- aic_buf_size = 0;
- //Dummy read for INT2=1
- aic_in = CODEC_DATA;
- //Turn OFF the CODEC function
- cpld_ctrl &= 0xfd;
- CPLD_CTRL = cpld_ctrl;
- //Setup the CPU IRQ2 for CODEC operation
- //P10 = input = IRQ2 ; IRQ2 = Falling edge
- IOPCON = (IOPCON & 0xffff83ff) | (0x12 << 10);
- IOPMOD = IOPMOD & 0xfffffbff;
- IOPDATA = 0x00000000;
- call = 0;
- highLow = 0;
- return 0;
- }
- //////////////////////////////////////////////////////////////////////////
- void exit_codec(void)
- {
- CODEC_Stop();
- CODEC_mfree();
- }
- void Playnote(int mode, int key)
- {
- LCD_printf("hello\n");
- if(mode == 1)
- {
- switch(key)
- {
- case 1:
- freq = Low[0];
- break;
- case 2:
- freq = Low[1];
- break;
- case 3:
- freq = Low[2];
- break;
- case 4:
- freq = Low[3];
- break;
- case 5:
- freq = Low[4];
- break;
- case 6:
- freq = Low[5];
- break;
- case 7:
- freq = Low[6];
- break;
- }
- }
- else if(mode == 2)
- {
- switch(key)
- {
- case 1:
- freq = Medium[0];
- break;
- case 2:
- freq = Medium[1];
- break;
- case 3:
- freq = Medium[2];
- break;
- case 4:
- freq = Medium[3];
- break;
- case 5:
- freq = Medium[4];
- break;
- case 6:
- freq = Medium[5];
- break;
- case 7:
- freq = Medium[6];
- break;
- }
- }
- else if(mode == 3)
- {
- switch(key)
- {
- case 1:
- freq = High[0];
- break;
- case 2:
- freq = High[1];
- break;
- case 3:
- freq = High[2];
- break;
- case 4:
- freq = High[3];
- break;
- case 5:
- freq = High[4];
- break;
- case 6:
- freq = High[5];
- break;
- case 7:
- freq = High[6];
- break;
- }
- }
- Delay(500);
- }
- // --------------------------------------------------------------------
- //
- // Title : MAIN
- // :
- // Library :
- // :
- // Developers: MICROTIME MDS group
- // :
- // Purpose : Main Program for Creator Board
- // :
- // Limitation:
- // :
- // Note :
- // :
- // --------------------------------------------------------------------
- // modification history :
- // --------------------------------------------------------------------
- // Version| mod. date: |
- // V1.0 | 03/31/2003 | First release
- // --------------------------------------------------------------------
- //
- // Note:
- //
- // MICROTIME COMPUTER INC.
- //
- //
- /*************************************************************************
- Include files
- *************************************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include "driver.h"
- #include "codec.h"
- #include "lcd.h"
- /*************************************************************************
- Function prototypes
- *************************************************************************/
- //void WaitChar(void);
- //UC Test_AIC(void);
- void PressKey(int);
- /*************************************************************************
- Variables
- *************************************************************************/
- int mode = 0;
- /*************************************************************************
- System Initialize
- *************************************************************************/
- void Initial_Creator(void)
- {
- //S3C4510 Peripherals Initialization
- Initial_TIMER0(); //Generate 1ms interrupt
- IOPMOD = 0x00030000; //for testing purposes
- IOPCON = 0x40000000; //for testing purposes
- //Initial IO_REG0
- io_reg0 = 0xc000;
- IO_REG0 = io_reg0;
- Delay(1);
- //Initial CPLD_CTRL
- cpld_ctrl = 0x3C;
- CPLD_CTRL = cpld_ctrl;
- Delay(500);
- cpld_ctrl |= 0x01; //Release IO reset
- CPLD_CTRL = cpld_ctrl;
- Delay(500);
- Initial_LCD();
- LCD_EnableCursor(TRUE);
- Initial_KEYPAD();
- init_codec();
- mode = 0;
- }
- /*************************************************************************
- MAIN Program
- *************************************************************************/
- int main(void)
- {
- Initial_Creator();
- EnableInterrupt(BIT_GMASK);
- LCD_printf("Choose mode:\n");
- LCD_printf("1 : Low scale\n");
- LCD_printf("2 : Medium scale\n");
- LCD_printf("3 : High scale\n");
- /*
- while(mode == 0)
- {
- if(IO_REG1 & 1)
- mode = 1;
- else if(IO_REG1 & 2)
- mode = 2;
- else if(IO_REG1 & 4)
- mode = 3;
- }
- */
- PressKey(mode);
- LCD_printf("The End!\n");
- DisableInterrupt(BIT_GMASK);
- exit_codec();
- return(0);
- }
- void PressKey(int mode)
- {
- UC key;
- LCD_printf("hihi\n");
- CODEC_malloc(0x400000);
- CODEC_Start();
- LCD_printf("Press 1~7 to play notes\n");
- /*
- do{
- while(KEYPAD_get_key(&key) != OK);
- Playnote(mode, key);
- }while(key != 0);
- */
- CODEC_Stop();
- LCD_printf("\n\nEOF CODEC Test\n");
- CODEC_mfree();
- }
- /*************************************************************************
- Wait key
- *************************************************************************
- void WaitChar(void)
- {
- UC key;
- while(KEYPAD_get_key(&key) != OK);
- }
- */
- /*************************************************************************
- Get Key
- *************************************************************************
- UC GetIntNumRange (UC MinNum, UC MaxNum)
- {
- UC ch;
- if(KEYPAD_get_key(&ch))
- if(ch >= MinNum && ch <= MaxNum) return(ch);
- return (0xff);
- }
- */
- /*************************************************************************
- Test Codec
- *************************************************************************
- UC Test_AIC(void)
- {
- UC num;
- LCD_ClearScreen();
- LCD_printf("CODEC Test...\n\n");
- LCD_printf("0=QUIT\n");
- LCD_printf("1=Record Start\n");
- LCD_printf("2=Play Start\n");
- LCD_printf("3=Rec/Play Stop\n\n");
- CODEC_malloc(0x400000);
- CODEC_Start();
- do {
- while((num=GetIntNumRange (0, 9)) == 0xff);
- LCD_Cursor(0, 7);
- switch(num) {
- case 1 : //start record
- LCD_printf("Start Recording");
- CODEC_Record_Start();
- break;
- case 2 : //start play
- LCD_printf("Start Playing ");
- CODEC_Play_Start();
- break;
- case 3 : //stop
- LCD_printf("Stop Rec/Play ");
- CODEC_Record_Stop();
- CODEC_Play_Stop();
- break;
- case 0 : //quit
- LCD_printf("Quit CODEC Test");
- CODEC_Record_Stop();
- CODEC_Play_Stop();
- break;
- }
- }while(num != 0);
- CODEC_Stop();
- LCD_printf("\n\nEOF CODEC Test\n");
- CODEC_mfree();
- return(OK);
- }
- */
