📄 audioinit2.c
字号:
<8bits unsigned mono> ------------------------*/int prog_codec(void){ UINT32 format; int lockkey; lockkey = intLock(); /* Program record/playback sampling rate -- 11025 */ /* DAC */ format = DMRn_DMA | DMRn_AUTO |DMRn_TR_READ|DMRn_USIGN|DMRn_SIZE8|DMRn_MONO; writel(format, CS4281_pBA0+BA0_DMR0); writel(0x04, CS4281_pBA0+BA0_DACSR); /* ADC */ format = DMRn_DMA | DMRn_AUTO |DMRn_TR_WRITE|DMRn_USIGN|DMRn_SIZE8|DMRn_MONO; writel(format, CS4281_pBA0+BA0_DMR1); writel(0x04, CS4281_pBA0+BA0_ADCSR); intUnlock(lockkey); return 0;}/*------------------------------------* * Interrupt Service Routine * for * DMA0, DMA1 *------------------------------------*/void cs4281_interrupt(int param){ UINT32 temp1; int lockkey; /*logMsg("CW:cs4281-->");*/ /* if it's not a DMA interrupt, jump out by issuing a EOI */ temp1 = readl(CS4281_pBA0+BA0_HISR); if(!(temp1 &(HISR_DMA0 | HISR_DMA1))) { writel(HICR_IEV| HICR_CHGM, CS4281_pBA0+BA0_HICR); return; } /* playback DMA0 */ if(temp1 & HISR_DMA0) { semGive(SEM_DMA_Playback); CNT_DMA_Playback++; if(0x00010000 & readl(CS4281_pBA0+BA0_HDSR0)) { /*DMA Terminal Count */ DTC_DMA_Playback = 1; /* logMsg("playbkDMA0:TC");*/ } else { DTC_DMA_Playback = 0;/* logMsg("playbkDMA0:Half_TC");*/ } } /* record DMA1 */ if(temp1 & HISR_DMA1) { semGive(SEM_DMA_Record); CNT_DMA_Record++; if(0x00010000 & readl(CS4281_pBA0+BA0_HDSR1)) { /*DMA Terminal Count */ DTC_DMA_Record = 1; /*logMsg("recordDMA1:TC");*/ } else { DTC_DMA_Record = 0; /*logMsg("recordDMA1:Half_TC");*/ } }/* logMsg("\n");*/ writel(HICR_IEV| HICR_CHGM, CS4281_pBA0+BA0_HICR);} /********************************************************* * Install the cs4281/ac97 driver: * -bring up the hardware * -allocate and set DMA buffer/DMA engine * -connect ISR to IRQ * -start_adc/dac ********************************************************/ int probe1(){ unsigned int pciBusNo, pciDevNo, pciFuncNo; unsigned char byte; int i,j; UINT32 s_pBA0, s_pBA1; /* To ensure that taskDelay(1) is 1ms delay */ sysClkRateSet(1000); if (pciConfigLibInit (PCI_MECHANISM_1, 0xCF8, 0xCFC, 0) != OK) { printf("PCI lib config error\n"); return 1; } /**************************** * Find SoundCard * Set BaseAddr0, BaseAddr1 ****************************/ if(!(pciFindDevice(PCI_VENDOR_ID_CIRRUS,PCI_DEVICE_ID_CRYSTAL_CS4281, 0, &pciBusNo, &pciDevNo, &pciFuncNo)==OK)) { printf("\n CS4281 sound card NOT FOUND!!! \n"); return 1; } printf("\n FOUND CS4281 sound card, configuring BA0,BA1... \n"); pciConfigOutLong( pciBusNo, pciDevNo, pciFuncNo, PCI_CFG_BASE_ADDRESS_0, CS4281_pBA0); /* pciConfigOutLong( pciBusNo, pciDevNo, pciFuncNo, PCI_CFG_BASE_ADDRESS_1, CS4281_pBA1); */ pciConfigInLong( pciBusNo, pciDevNo, pciFuncNo, PCI_CFG_BASE_ADDRESS_0, &s_pBA0); pciConfigInLong( pciBusNo, pciDevNo, pciFuncNo, PCI_CFG_BASE_ADDRESS_1, &s_pBA1 ); printf ("\npBusNo pDeviceNo pFuncNo pBA0 pBA1\n\n"); printf ("%.8x %.8x %.8x %.8x %.8x \n", pciBusNo, pciDevNo, pciFuncNo, s_pBA0,s_pBA1); /******************************** * Config PCI Device Capability * DMA Master * MEM mapped ********************************/ /* Set the INTA vector */ pciConfigInByte(pciBusNo, pciDevNo, pciFuncNo, PCI_CFG_DEV_INT_LINE, &cs4281_irq); printf("\nFound CS4281 configured for IRQ %d\n", cs4281_irq); pciConfigInByte(pciBusNo, pciDevNo, pciFuncNo, PCI_CFG_DEV_INT_PIN, &byte); printf("\tINT_PIN=%.8x\n", byte); /* Enable the device's capabilities as specified * Bus Master Enable/ Mem Space Enable */ pciConfigOutWord(pciBusNo, pciDevNo, pciFuncNo, PCI_CFG_COMMAND, (unsigned short)0x0006); /*************************** * BringUp Hardware ***************************/ if(cs4281_hw_init()) return 1; /**************************** * Allocate ADC_BUFFER * Allocate DAC_BUFFER * * Hook cs4281_interrupt * * Program CoDec ****************************/ if((DAC_BUFFER=valloc(DAC_BUFFER_SIZE))==NULL) { printf("\n DAC_BUFFER valloc failed!\n"); return 1; } for( i=0 ; i < DAC_BUFFER_SIZE ; i++ ) ((char *)ADC_BUFFER)[i]=0x7f; writel((UINT32)DAC_BUFFER+8,CS4281_pBA0 + BA0_DBA0); writel(DAC_BUFFER_SIZE-16, CS4281_pBA0 + BA0_DBC0); if((ADC_BUFFER=valloc(ADC_BUFFER_SIZE))==NULL) { printf("\n ADC_BUFFER valloc failed!\n"); return 1; } for( i=0 ; i < ADC_BUFFER_SIZE ; i++ ) ((char *)ADC_BUFFER)[i]=0x7f; writel((UINT32)ADC_BUFFER+8,CS4281_pBA0 + BA0_DBA1); writel(ADC_BUFFER_SIZE-16, CS4281_pBA0 + BA0_DBC1); /* connect interrupt */ printf("\n Hook cs4281_interrupt to vector %d\n", (INUM_TO_IVEC (cs4281_irq+INT_NUM_IRQ0))); pciIntConnect((INUM_TO_IVEC (cs4281_irq+INT_NUM_IRQ0)), (VOIDFUNCPTR)cs4281_interrupt, 0); sysIntEnablePIC(cs4281_irq); SEM_DMA_Playback = semBCreate(SEM_Q_FIFO,SEM_EMPTY); SEM_MY_Playback = semBCreate(SEM_Q_FIFO,SEM_EMPTY); SEM_DMA_Record = semBCreate(SEM_Q_FIFO,SEM_EMPTY); CNT_DMA_Playback = CNT_DMA_Record = 0; /* program coDec */ printf("\n Program CoDec (sample rate, DMA...)\n"); prog_codec(); /********************************************* * start dac/adc, interrupt is comming... *********************************************/ printf("\n\n>>>Start DAC/ADC !!!!!!!!!!\n"); start_dac(); /* start_adc();*/ return 0;}int tcpRecvTask( int sockfd/* server's socket fd */ ){ struct sockaddr_in serverAddr; /* server's socket address */ struct sockaddr_in clientAddr; /* client's socket address */ int sockAddrSize; /* size of socket address structure */ int sFd; /* socket file desciptor */ int newFd; /* socket desciptor from accept */ int nRead; char * serverName = "10.0.0.201"; bzero (adNauseam, MAX_LINE); /* set up the local address */ sockAddrSize = sizeof (struct sockaddr_in); bzero ((char *) &serverAddr, sockAddrSize); serverAddr.sin_family = AF_INET; serverAddr.sin_len = (u_char) sockAddrSize; serverAddr.sin_port = htons (SERVER_PORT_NUM); /*serverAddr.sin_addr.s_addr = htonl (INADDR_ANY);*/ if (((serverAddr.sin_addr.s_addr = inet_addr (serverName)) == ERROR) && ((serverAddr.sin_addr.s_addr = hostGetByName (serverName)) == ERROR)) { perror ("unknown server name"); close(sFd); return(ERROR); } /* create a TCP-based socket */ if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR) { perror ("socket"); return (ERROR); } /* bind socket to local address */ if (bind (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) { perror ("bind"); close (sFd); return (ERROR); } while(1) { /* create queue for client connection requets */ if (listen (sFd, SERVER_MAX_CONNECTIONS) == ERROR) { perror ("listen"); close (sFd); return (ERROR); } /* accept new connect requests and spawn tasks to process them */ if ((newFd = accept (sFd, (struct sockaddr *) &clientAddr, &sockAddrSize)) == ERROR) { perror ("accept"); close (sFd); return (ERROR); } printf("SERVER: ******** servicing connected client %d ********\n", newFd); while(1) { nRead = read(newFd, adNauseam, sizeof (adNauseam)); if ( nRead < 0 ) { perror ("read"); close (sFd); return (ERROR); } semTake(SEM_DMA_Playback, WAIT_FOREVER); if(DTC_DMA_Playback) memmove(DAC_BUFFER+(DAC_BUFFER_SIZE/2),adNauseam, (DAC_BUFFER_SIZE/2)); else memmove(DAC_BUFFER,adNauseam, DAC_BUFFER_SIZE/2); } /* end while awaiting frames */ } return 0;}int tcpSendTask( int sockfd/* server's socket fd */ ){ struct sockaddr_in serverAddr; /* server's socket address */ int sockAddrSize; /* size of socket address structure */ int sFd; /* socket file descriptor */ int mlen; /* length of message */ char * serverName = "10.0.0.202"; struct linger opt; int sockarg; char * tmp_buf; bzero (saved_samples, MAX_LINE); /* create client's socket */ if ((sFd = socket (AF_INET, SOCK_STREAM, 0)) == ERROR) { perror ("socket"); return (ERROR); } /* bind not required - port number is dynamic */ /* turn on zero linger time so that undelivered data is discarded when socket is closed */ opt.l_onoff = 1; opt.l_linger = 0; sockarg = 1; setsockopt(sFd, SOL_SOCKET, SO_LINGER, (char*) &opt, sizeof(opt)); setsockopt(sFd, IPPROTO_TCP, TCP_NODELAY, (char *)&sockarg, sizeof(int)); /* build server socket address */ sockAddrSize = sizeof (struct sockaddr_in); bzero ((char *) &serverAddr, sockAddrSize); serverAddr.sin_family = AF_INET; serverAddr.sin_len = (u_char) sockAddrSize; serverAddr.sin_port = htons (CLIENT_PORT_NUM); if (((serverAddr.sin_addr.s_addr = inet_addr (serverName)) == ERROR) && ((serverAddr.sin_addr.s_addr = hostGetByName (serverName)) == ERROR)) { perror ("unknown server name"); close(sFd); return(ERROR); } /* bind socket to local address */ /*if (bind (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) { perror ("bind"); close (sFd); return (ERROR); }*/ /* connect to server */ if (connect (sFd, (struct sockaddr *) &serverAddr, sockAddrSize) == ERROR) { perror ("connect"); close (sFd); return (ERROR); } /*loadBuffer(saved_samples);*/ while(1) { semTake(SEM_DMA_Record, WAIT_FOREVER); if(DTC_DMA_Record) { tmp_buf=(char *) ADC_BUFFER+(ADC_BUFFER_SIZE/2); memmove(saved_samples, ADC_BUFFER+(ADC_BUFFER_SIZE/2), (ADC_BUFFER_SIZE/2)); } else { tmp_buf=(char *)ADC_BUFFER; memmove(saved_samples, ADC_BUFFER, ADC_BUFFER_SIZE/2 ); } /*if ( memcmp(saved_samples,tmp_buf,ADC_BUFFER_SIZE/2) != 0) { logMsg("saved_samples:: %x %x %x %x\n",saved_samples[0],saved_samples[1],saved_samples[2],saved_samples[3]); logMsg("ADC_BUFFER:: %x %x %x %x\n",tmp_buf[0],tmp_buf[1],tmp_buf[2],tmp_buf[3]); }*/ /* send request to server */ /*logMsg("writing to socket File Descriptor\n");*/ if (write (sFd, saved_samples, sizeof (saved_samples)) == ERROR) { perror ("write"); close (sFd); return (ERROR); } } return 0;}/*char foo(){ return -1;}void char_test(){ if(foo() == -1) printf("Bang\n"); else printf("Splat\n");}*/void loadBuffer(char b[MAX_LINE]){ int i; for(i=0;i<MAX_LINE;i++) { b[i] = pbdata[i]; }} /* * zero ADC_BUFFER * GAINS and ATTenuations important- recommends setting mid-level * Linux Driver documented www.alsa.com -RECORD_GAIN_MIC ( getting rid of analog loopback) * Sampling rate = interrupt rate; crunch the numbers... * Double check mono setting * wvEvent calls to know where you are in windView * set task priorities to be RM type priorities * tNetTask priority - keep it in mind when setting priorites of send/recv. * buffer sizes are important - find the sweet spot */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -