device.cpp
资源名称:se06.rar [点击查看]
上传用户:kepeng103
上传日期:2022-07-27
资源大小:2653k
文件大小:5k
源码类别:
DSP编程
开发平台:
C/C++
- #include "..commonhal.h"
- #include "device.h"
- #include "buf.h"
- int active = 0;
- int mcasp_wdt = 0;
- extern "C" __interrupt void timer0_isr(void)
- {
- if(++ mcasp_wdt > 10) {
- mcasp_wdt = 0;
- mcasp_reset();
- }
- }
- extern "C" __interrupt void mcasp1_xmt_isr(void)
- {
- static int lr = 0;
- static int16 data;
- if(lr == 0)
- data = data_out();
- REG32(MCASP1_XBUF0) = data;
- lr ^= 0x1;
- mcasp_wdt = 0;
- }
- extern "C" __interrupt void mcbsp0_recv_isr(void)
- {
- if(REG32(MCBSP0_SPCR) & 0x00000002) {
- data_in(REG32(MCBSP0_DRR));
- }
- }
- void gpio_init()
- {
- //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- //REG32(GPIO_GPEN) = 0x00002e02;
- REG32(GPIO_GPEN) = 0x00002ee3;
- REG32(GPIO_GPDIR) = 0x00002e02;
- REG32(GPIO_GPVAL) = 0x00000002;
- }
- void dac_reset(void)
- {
- DAC_MS_HI();
- REG32(GPIO_GPVAL) &= (~MASK_DAC_PD);
- delay_ms(100);
- REG32(GPIO_GPVAL) |= MASK_DAC_PD;
- delay_ms(10);
- }
- int mcasp_init(void)
- {
- //Reset mcasp1
- REG32(MCASP1_GBLCTL) = 0x00000000;
- REG32(MCASP1_XMASK) = 0xFFFFFFFF;
- //MSB,16bit,CFG
- REG32(MCASP1_XFMT) = 0x0000807C;
- //2 slot TDM, internal sync, sync starts with rising edge.
- REG32(MCASP1_AFSXCTL) = 0x00000112;
- //Internal bit clock. CLKXDIV = 3
- REG32(MCASP1_ACLKXCTL) = 0x000000E3;
- //Internal High speed clock. 16kHz: HCLKDIV = 7, 8kHz: HCLKDIV = F
- REG32(MCASP1_AHCLKXCTL) = 0x0000C00F;
- //TDM slot 0 and 1 is used.
- REG32(MCASP1_XTDM) = 0xFFFFFFFF;
- //Disable all interrupts
- REG32(MCASP1_XINTCTL) = 0x00000020;
- //Not used.
- REG32(MCASP1_XCLKCHK) = 0x00000000;
- //Transmitter
- REG32(MCASP1_SRCTL0) = 0x00000001;
- //AXR1[0] mcasp
- REG32(MCASP1_PFUNC) &= (~0xFE000001);
- REG32(MCASP1_PFUNC) |= 0x0000003E;
- //AXR1[0] output
- REG32(MCASP1_PDIR) |= 0xFC00003F;
- //TMD
- REG32(MCASP1_DITCTL) = 0;
- REG32(MCASP1_DLBCTL) = 0;
- REG32(MCASP1_AMUTE) = 0;
- REG32(MCASP1_GBLCTL) |= 0x00000202;
- while((REG32(MCASP1_GBLCTL) & 0x00000202) != 0x00000202);
- REG32(MCASP1_GBLCTL) |= 0x00000101;
- while((REG32(MCASP1_GBLCTL) & 0x00000101) != 0x00000101);
- REG32(MCASP1_GBLCTL) |= 0x00000404;
- while((REG32(MCASP1_GBLCTL) & 0x00000404) != 0x00000404);
- REG32(INT_MUXH) &= (~0x0000001F);
- REG32(INT_MUXH) |= 0x0000001E;
- IER |= 0x00000400;
- REG32(MCASP1_XBUF0) = 0x0000;
- while(REG32(MCASP1_XSTAT) & 0x20);
- // REG32(MCASP1_GBLCTL) |= 0x00000808; !!!!!!!!!!!!!!!!!!!!
- while((REG32(MCASP1_GBLCTL) & 0x00000808) != 0x00000808)
- REG32(MCASP1_GBLCTL) |= 0x00000808;
- REG32(MCASP1_GBLCTL) |= 0x00001000;
- while((REG32(MCASP1_GBLCTL) & 0x00001000) != 0x00001000);
- if(REG32(MCASP1_GBLCTL) != 0x00001F0F) {
- return -1;
- }
- return 0;
- }
- void mcasp_reset(void)
- {
- REG32(MCASP1_GBLCTL) &= (~0x00001808);
- while((REG32(MCASP1_GBLCTL) & 0x00000808) != 0x00000808)
- REG32(MCASP1_GBLCTL) |= 0x00000808;
- REG32(MCASP1_GBLCTL) |= 0x00001000;
- while((REG32(MCASP1_GBLCTL) & 0x00001000) != 0x00001000);
- }
- void set_led(uint8 mask, uint8 code)
- {
- REG32(MCASP1_PDOUT) &= (~(mask<<1));
- REG32(MCASP1_PDOUT) |= (((code & mask) << 1) & 0x3e);
- }
- void mcbsp0_init_serial(void)
- {
- REG32(MCBSP0_SPCR) = 0x00000000;
- //Free-running mode,
- REG32(MCBSP0_SPCR) |= 0x02000000;
- delay_ms(10);
- //FSGM=0
- REG32(MCBSP0_SRGR) = 0x00000000;
- //XRST=1;
- REG32(MCBSP0_PCR) = 0x00000000;
- REG32(MCBSP0_XCR) = 0x00450040;
- REG32(MCBSP0_RCR) = 0x00450040;
- REG32(MCBSP0_SPCR) |= 0x00010000;
- delay_ms(10);
- if(REG32(MCBSP0_SPCR) & 0x00080000)
- REG32(MCBSP0_SPCR) &= (~0x00010000);
- REG32(MCBSP0_SPCR) |= 0x00010001;
- //McBSP0 receive uses INT13
- REG32(INT_MUXH) &= (~0x001F0000);
- REG32(INT_MUXH) |= 0x000D0000;
- IER |= 0x00002000;
- REG32(MCBSP0_DXR) = 0x00000000;
- }
- void adc_reg_write(uint16 code)
- {
- code |= 0x8000;
- while(! (REG32(MCBSP0_SPCR) & 0x00020000));
- REG32(MCBSP0_DXR) = code;
- }
- void adc_init(void)
- {
- //Reset
- ADC_SE_LO();
- REG32(GPIO_GPVAL) &= (~MASK_ADC_RESET);
- delay_ms(100);
- REG32(GPIO_GPVAL) |= MASK_ADC_RESET;
- delay_ms(10);
- ADC_SE_HI();
- delay_ms(10);
- //A, set device count
- adc_reg_write(MAKE_ADC_CTL_WORD(0x0, 0x0, 0x00));
- //B 8kHz:0x00,16kHz:0x05
- adc_reg_write(MAKE_ADC_CTL_WORD(0x0, 0x1, 0x00));
- //C
- adc_reg_write(MAKE_ADC_CTL_WORD(0x0, 0x2, 0x60));
- //D, Input gain select 0db:0x08, 6db:0x09, 12db:0x0a,18db:0x0b
- adc_reg_write(MAKE_ADC_CTL_WORD(0x0, 0x3, 0x0b));//b
- //E
- adc_reg_write(MAKE_ADC_CTL_WORD(0x0, 0x4, 0x00));
- //F
- adc_reg_write(MAKE_ADC_CTL_WORD(0x0, 0x5, 0x00));
- //G
- adc_reg_write(MAKE_ADC_CTL_WORD(0x0, 0x6, 0xBF));
- //H
- adc_reg_write(MAKE_ADC_CTL_WORD(0x0, 0x7, 0x00));
- //A
- adc_reg_write(MAKE_ADC_CTL_WORD(0x0, 0x0, 0x01));
- delay_ms(10);
- ADC_SE_LO();
- }
- void timer0_init(void)
- {
- REG32(TIMER0_CTL) = 0x00000000;
- REG32(TIMER0_PRD) = 0x00050000;
- REG32(TIMER0_CTL) |= 0x00000307;
- REG32(TIMER0_CNT) = 0x00000000;
- REG32(TIMER0_CTL) |= 0x000000c0;
- IER |= 0x00004000;
- }
- void timer1_init(void)
- {
- REG32(TIMER1_CTL) = 0x00000000;
- REG32(TIMER1_PRD) = 0xFFFFFFFF;
- REG32(TIMER1_CTL) |= 0x00000200;
- REG32(TIMER1_CNT) = 0x00000000;
- REG32(TIMER1_CTL) |= 0x000000C0;
- }
- void delay_ms(uint16 msec)
- {
- uint32 limit = msec * TIMER_CLOCK;
- uint32 start = REG32(TIMER1_CNT);
- uint32 current;
- do {
- current = REG32(TIMER1_CNT);
- } while((current - start) < limit);
- }