DSP28335 Mcbsp SPI實現軟體片選
阿新 • • 發佈:2018-12-30
思路是把FSX(片選)引腳設定為GPIO腳,控制輸出
具體程式碼如下
管腳初始化函式修改如下
InitMcbspbGpio(void)
{
……
GpioCtrlRegs.GPAMUX2.bit.GPIO27 = 0; // 把GPIO27設為GPIO腳
GpioCtrlRegs.GPADIR.bit.GPIO27 = 1; //引腳GPIO27改為GPIO腳,用程式碼控制輸出
……
}
McBSP初始化函式修改如下
void init_mcbsp_spi()
{
// McBSP-A register settings
McbspbRegs.SPCR 2.all=0x0000; // Reset FS generator, sample rate generator & transmitter
McbspbRegs.SPCR1.all=0x0000; // Reset Receiver, Right justify word, Digital loopback dis.
/*CLKXM bit of PCR :This bit determines whether CLKX is an input signal (McBSP as slave) or an output
signal (McBSP as master).
FSXM = 0 Transmit frame synchronization is supplied by an external source via the FSX pin.
FSXM = 1 Transmit frame synchronization is supplied by the McBSP, as determined by the FSGM bit of SRGR2.*/
McbspbRegs.PCR.all=0x0F08; //(CLKXM=CLKRM=FSXM=FSRM= 1, FSXP = 1)
McbspbRegs.SPCR1.bit.DLB = 0; //數字迴環模式(不開啟)
/*Low inactive state without delay: The McBSP transmits data on the rising edge of CLKX and
receives data on the falling edge of MCLKR.*/
McbspbRegs.SPCR1.bit.CLKSTP = 2 ; // Together with CLKXP/CLKRP determines clocking scheme
McbspbRegs.PCR.bit.CLKXP = 1; // CPOL = 0, CPHA = 0 rising edge no delay
McbspbRegs.PCR.bit.CLKRP = 0;
McbspbRegs.RCR2.bit.RDATDLY = 01; // FSX setup time 1 in master mode. 0 for slave mode (Receive)
McbspbRegs.XCR2.bit.XDATDLY = 01; // FSX setup time 1 in master mode. 0 for slave mode (Transmit)
/*You must use a transmit frame length of 1 serial word (XCR1.XFRLEN1 = 0).
* You must use a receive frame length of 1 serial word (RCR1.RFRLEN1 = 0).
* You must use a single-phase transmit frame (XCR2.XPHASE = 0).
* You must use a single-phase receive frame (RCR2.RPHASE = 0).*/
//McbspbRegs.SRGR2.bit.FPER = 1;
if(data_size == 8) // Run a loopback test in 8-bit mode
{
McbspbRegs.RCR1.bit.RWDLEN1=0; // 8-bit word
McbspbRegs.XCR1.bit.XWDLEN1=0; // 8-bit word
}
if(data_size == 16) // Run a loopback test in 16-bit mode
{
McbspbRegs.RCR1.bit.RWDLEN1=2; // 16-bit word
McbspbRegs.XCR1.bit.XWDLEN1=2; // 16-bit word
}
if(data_size == 32) // Run a loopback test in 32-bit mode
{
McbspbRegs.RCR1.bit.RWDLEN1=5; // 32-bit word
McbspbRegs.XCR1.bit.XWDLEN1=5; // 32-bit word
}
/*PCR.SCLKME = 0, SRGR.CLKSM = 1,Input Clock For Sample Rate Generator = LSPCLK
* CLKG frequency = (Input clock frequency)/ (CLKGDV + 1)*/
McbspbRegs.SRGR2.all=0x2000; // CLKSM=1, FPER = 1 CLKG periodR
McbspbRegs.SRGR1.all= 0x0004; // Frame Width = 1 CLKG period, CLKGDV=16
McbspbRegs.SPCR2.bit.GRST=1; // Enable the sample rate generator
delay_loop(); // Wait at least 2 SRG clock cycles
McbspbRegs.SPCR2.bit.XRST=1; // Release TX from Reset
McbspbRegs.SPCR1.bit.RRST=1; // Release RX from Reset
McbspbRegs.SPCR2.bit.FRST=1; // Frame Sync Generator reset
GpioDataRegs.GPASET.bit.GPIO27 = 1; //把FSX(CS)片選引腳拉高,空閒時片選都拉高
}
呼叫函式如下
void Example(void)
{
GpioDataRegs.GPATOGGLE.bit.GPIO27 = 1;
mcbsp_xmit(0xB0); //命令碼
mcbsp_xmit(0x83); //操作碼
GpioDataRegs.GPATOGGLE.bit.GPIO27 = 1;
DELAY_US(1);
}
樓主是在配置編碼器時用到這個功能,希望能幫到大家。