我在做SPI时用了一个GPIO 作为SPI外设的片选信号,在SPI发送数据期间,这个片选要一直保持低电平。但是发现SPI发送时会把原来GPIO输出的低电平状态破坏,导致外部设备无法片选,求高人指教啊!
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
// Prototype statements for functions found within this file.
#define BUF_SIZE 8192
#define FPGA_ADD_BASE (0x280000)
#define AD_DATA_LEN 8192
void delay_loop(void);
void spi_xmit(Uint16 a);
void spi_fifo_init(void);
void spi_init(void);
void error(void);
interrupt void xint2_isr(void);
void init_zone7(void);
#pragma DATA_SECTION(DatBuf1,"DMARAMH3");
volatile Uint16 DatBuf1[BUF_SIZE];
volatile Uint16 *DMADest;
volatile Uint16 *DMASource;
// Global variables for this example
volatile Uint32 Xint1Count;
volatile Uint32 Xint2Count;
Uint32 LoopCount;
Uint16 rLen = 0 ;
#define DELAY 35.700L
void main(void)
{
Uint32 i = 0;
Uint16 sdata; // send data
Uint16 rdata; // received data
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2834x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP2834x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2834x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2834x_DefaultIsr.c.
// This function is found in DSP2834x_PieVect.c.
InitPieVectTable();
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.XINT2 = &xint2_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2834x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
// Step 5. User specific code, enable interrupts:
// Clear the counters
Xint1Count = 0; // Count Xint1 interrupts
Xint2Count = 0; // Count XINT2 interrupts
LoopCount = 0; // Count times through idle loop
// Enable Xint1 and XINT2 in the PIE: Group 1 interrupt 4 & 5
// Enable int1 which is connected to WAKEINT:
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block
PieCtrlRegs.PIEIER1.bit.INTx4 = 1; // Enable PIE Gropu 1 INT4
PieCtrlRegs.PIEIER1.bit.INTx5 = 1; // Enable PIE Gropu 1 INT5
IER |= M_INT1; // Enable CPU int1
EINT; // Enable Global Interrupts
//Gpio 0 EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 0; // Gpio0 as Gpio
GpioCtrlRegs.GPADIR.bit.GPIO2 = 1; // Gpio0 as Output
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1; // Clear Gpio0 EDIS;
// GPIO0 and GPIO1 are inputs
EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0; // GPIO
GpioCtrlRegs.GPADIR.bit.GPIO1 = 0; // input
GpioCtrlRegs.GPAQSEL1.bit.GPIO1 = 0;
// GpioCtrlRegs.GPAQSEL1.bit.GPIO1 = 2; // XINT2 Qual using 6 samples
//GpioCtrlRegs.GPACTRL.bit.QUALPRD0 = 0xFF; // Each sampling window is 510*SYSCLKOUT
EDIS;
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO21 = 0; // Enable pullup on GPIO8
GpioDataRegs.GPASET.bit.GPIO21 = 1; // Load output latch
GpioCtrlRegs.GPAMUX2.bit.GPIO21 = 0; // GPIO8 = GPIO8
GpioCtrlRegs.GPADIR.bit.GPIO21 = 1; // GPIO8 = output
GpioCtrlRegs.GPAPUD.bit.GPIO13 = 0; // Enable pullup on GPIO9
GpioDataRegs.GPASET.bit.GPIO13 = 1; // Load output latch
GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0; // GPIO9 = GPIO9
GpioCtrlRegs.GPADIR.bit.GPIO13 = 1;
GpioCtrlRegs.GPAPUD.bit.GPIO9 = 0; // Enable pullup on GPIO9
GpioDataRegs.GPASET.bit.GPIO9 = 1; // Load output latch
GpioCtrlRegs.GPAMUX1.bit.GPIO9 = 0; // GPIO9 = GPIO9
GpioCtrlRegs.GPADIR.bit.GPIO9 = 1; // GPIO9 = output
EDIS;
// GPIO0 is XINT1, GPIO1 is XINT2
EALLOW;
GpioIntRegs.GPIOXINT2SEL.bit.GPIOSEL = 1; // XINT2 is GPIO1
EDIS;
// Configure XINT1
XIntruptRegs.XINT2CR.bit.POLARITY = 0; // Falling edge interrupt
// Enable XINT1 and XINT2
XIntruptRegs.XINT2CR.bit.ENABLE = 1; // Enable XINT2
// Initalize XINTF Zone 7
init_zone7();
InitSpiaGpio();
spi_fifo_init(); // Initialize the Spi FIFO
spi_init(); // init SPI
sdata = 0xa000;
// AD convert test:
for(;;)
{
//start once AD sample #if 0
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1; // Clear Gpio0 DELAY_US(5); GpioDataRegs.GPASET.bit.GPIO2 = 1;
DELAY_US(5); #endif // Transmit data
GpioDataRegs.GPACLEAR.bit.GPIO9 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO13 = 1;
spi_xmit(sdata);
GpioDataRegs.GPASET.bit.GPIO13 = 1;
sdata++;
}
}
就是下面这几行:
GpioDataRegs.GPACLEAR.bit.GPIO13 = 1;
spi_xmit(sdata);
GpioDataRegs.GPASET.bit.GPIO13 = 1;
GPIO13只有一个下脉冲,并不是我我想要的,能保持16个SPICLK的低电平。
robbin gao:
补充下:用的TMS320C28346
我在做SPI时用了一个GPIO 作为SPI外设的片选信号,在SPI发送数据期间,这个片选要一直保持低电平。但是发现SPI发送时会把原来GPIO输出的低电平状态破坏,导致外部设备无法片选,求高人指教啊!
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
// Prototype statements for functions found within this file.
#define BUF_SIZE 8192
#define FPGA_ADD_BASE (0x280000)
#define AD_DATA_LEN 8192
void delay_loop(void);
void spi_xmit(Uint16 a);
void spi_fifo_init(void);
void spi_init(void);
void error(void);
interrupt void xint2_isr(void);
void init_zone7(void);
#pragma DATA_SECTION(DatBuf1,"DMARAMH3");
volatile Uint16 DatBuf1[BUF_SIZE];
volatile Uint16 *DMADest;
volatile Uint16 *DMASource;
// Global variables for this example
volatile Uint32 Xint1Count;
volatile Uint32 Xint2Count;
Uint32 LoopCount;
Uint16 rLen = 0 ;
#define DELAY 35.700L
void main(void)
{
Uint32 i = 0;
Uint16 sdata; // send data
Uint16 rdata; // received data
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2834x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP2834x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2834x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2834x_DefaultIsr.c.
// This function is found in DSP2834x_PieVect.c.
InitPieVectTable();
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.XINT2 = &xint2_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2834x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
// Step 5. User specific code, enable interrupts:
// Clear the counters
Xint1Count = 0; // Count Xint1 interrupts
Xint2Count = 0; // Count XINT2 interrupts
LoopCount = 0; // Count times through idle loop
// Enable Xint1 and XINT2 in the PIE: Group 1 interrupt 4 & 5
// Enable int1 which is connected to WAKEINT:
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block
PieCtrlRegs.PIEIER1.bit.INTx4 = 1; // Enable PIE Gropu 1 INT4
PieCtrlRegs.PIEIER1.bit.INTx5 = 1; // Enable PIE Gropu 1 INT5
IER |= M_INT1; // Enable CPU int1
EINT; // Enable Global Interrupts
//Gpio 0 EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 0; // Gpio0 as Gpio
GpioCtrlRegs.GPADIR.bit.GPIO2 = 1; // Gpio0 as Output
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1; // Clear Gpio0 EDIS;
// GPIO0 and GPIO1 are inputs
EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0; // GPIO
GpioCtrlRegs.GPADIR.bit.GPIO1 = 0; // input
GpioCtrlRegs.GPAQSEL1.bit.GPIO1 = 0;
// GpioCtrlRegs.GPAQSEL1.bit.GPIO1 = 2; // XINT2 Qual using 6 samples
//GpioCtrlRegs.GPACTRL.bit.QUALPRD0 = 0xFF; // Each sampling window is 510*SYSCLKOUT
EDIS;
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO21 = 0; // Enable pullup on GPIO8
GpioDataRegs.GPASET.bit.GPIO21 = 1; // Load output latch
GpioCtrlRegs.GPAMUX2.bit.GPIO21 = 0; // GPIO8 = GPIO8
GpioCtrlRegs.GPADIR.bit.GPIO21 = 1; // GPIO8 = output
GpioCtrlRegs.GPAPUD.bit.GPIO13 = 0; // Enable pullup on GPIO9
GpioDataRegs.GPASET.bit.GPIO13 = 1; // Load output latch
GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0; // GPIO9 = GPIO9
GpioCtrlRegs.GPADIR.bit.GPIO13 = 1;
GpioCtrlRegs.GPAPUD.bit.GPIO9 = 0; // Enable pullup on GPIO9
GpioDataRegs.GPASET.bit.GPIO9 = 1; // Load output latch
GpioCtrlRegs.GPAMUX1.bit.GPIO9 = 0; // GPIO9 = GPIO9
GpioCtrlRegs.GPADIR.bit.GPIO9 = 1; // GPIO9 = output
EDIS;
// GPIO0 is XINT1, GPIO1 is XINT2
EALLOW;
GpioIntRegs.GPIOXINT2SEL.bit.GPIOSEL = 1; // XINT2 is GPIO1
EDIS;
// Configure XINT1
XIntruptRegs.XINT2CR.bit.POLARITY = 0; // Falling edge interrupt
// Enable XINT1 and XINT2
XIntruptRegs.XINT2CR.bit.ENABLE = 1; // Enable XINT2
// Initalize XINTF Zone 7
init_zone7();
InitSpiaGpio();
spi_fifo_init(); // Initialize the Spi FIFO
spi_init(); // init SPI
sdata = 0xa000;
// AD convert test:
for(;;)
{
//start once AD sample #if 0
GpioDataRegs.GPACLEAR.bit.GPIO2 = 1; // Clear Gpio0 DELAY_US(5); GpioDataRegs.GPASET.bit.GPIO2 = 1;
DELAY_US(5); #endif // Transmit data
GpioDataRegs.GPACLEAR.bit.GPIO9 = 1;
GpioDataRegs.GPACLEAR.bit.GPIO13 = 1;
spi_xmit(sdata);
GpioDataRegs.GPASET.bit.GPIO13 = 1;
sdata++;
}
}
就是下面这几行:
GpioDataRegs.GPACLEAR.bit.GPIO13 = 1;
spi_xmit(sdata);
GpioDataRegs.GPASET.bit.GPIO13 = 1;
GPIO13只有一个下脉冲,并不是我我想要的,能保持16个SPICLK的低电平。
robbin gao:
补充一张图片:
图中上面信号为GPIO13,下面信号为SPICLK。可见GPIO13只保持了很短时间,就被SPI发送改了状态。