TI论坛
直播中

梁宏满

7年用户 958经验值
私信 关注
[问答]

TM4C1290 SPI通信使用官方的例程spi_master.c无法实现自收功能

最近在学习使用TM4C1290NCPDT中的spi通信,使用官方的例程spi_master.c无法实现自收功能,使用串口打印出接收FIFO中的数据为空,以下是我的代码,希望各位前辈帮忙解答,谢谢!
int main(void)
[
uint32_t pui32DataTx[NUM_SSI_DATA];
uint32_t pui32DataRx[NUM_SSI_DATA];
uint32_t ui32Index = 0;
//设置系统时钟 120 MHZ
ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_16MHZ |
SYSCTL_OSC_MAIN |
SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 120000000 );
//初始化Uart1
InitConsole();
// 打印欢迎信息
UARTprintf("nSPI Example Appn");
UARTprintf("Type something to see it show up on the other terminal: nn");
UARTprintf("SSI ->n");
UARTprintf(" Mode: SPIn");
UARTprintf(" Data: 8-bitnn");

// 使能SSI0外设
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI0);
// 使能GPIO端口A,才能使用这些引脚。
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
// 将端口A2,A3,A4和A5配置为SSI0功能的引脚复用。
GPIOPinConfigure(GPIO_PA2_SSI0CLK);
GPIOPinConfigure(GPIO_PA3_SSI0FSS);
GPIOPinConfigure(GPIO_PA4_SSI0XDAT0); // SPI_DO
GPIOPinConfigure(GPIO_PA5_SSI0XDAT1); // SPI_DI
// 配置SSI引脚的GPIO设置。
// The pins are assigned as follows:
// PA5 - SSI0Tx
// PA4 - SSI0Rx
// PA3 - SSI0Fss
// PA2 - SSI0CLK
GPIOPinTypeSSI(GPIO_PORTA_BASE, GPIO_PIN_5 | GPIO_PIN_4 | GPIO_PIN_3 |
GPIO_PIN_2);

// 配置和使能SPI主模式的SSI端口。
// 在SPI模式,主模式,1MHz SSI频率和8位数据中使用SSI0,系统时钟供应,空闲时钟电平低电平和有效低电平时钟。
SSIConfigSetExpClk(SSI0_BASE, ui32SysClock, SSI_FRF_MOTO_MODE_0,
SSI_MODE_MASTER, 1000000, 8);
// 使能 SSI0 模块.
SSIEnable(SSI0_BASE);
// Read any residual data from the SSI port. This makes sure the receive
// FIFOs are empty, so we don't read any unwanted junk. This is done here
// because the SPI SSI mode is full-duplex, which allows you to send and
// receive at the same time. The SSIDataGetNonBlocking function returns
// "true" when data was returned, and "false" when no data was returned.
// The "non-blocking" function checks if there is any data in the receive
// FIFO and does not "hang" if there isn't.
while(SSIDataGetNonBlocking(SSI0_BASE, &pui32DataRx[0]))
[
]
// Initialize the data to send.
pui32DataTx[0] = 'S';
pui32DataTx[1] = 'P';
pui32DataTx[2] = 'I';
// Display indication that the SSI is transmitting data.
UARTprintf("Sent:n ");
// Send 3 bytes of data.
for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++)
[
// Display the data that SSI is transferring.
UARTprintf("'%c' ", pui32DataTx[ui32Index]);
// Send the data using the "blocking" put function. This function
// will wait until there is room in the send FIFO before returning.
// This allows you to assure that all the data you send makes it into
// the send FIFO.
SSIDataPut(SSI0_BASE, pui32DataTx[ui32Index]);
]
// Wait until SSI0 is done transferring all the data in the transmit FIFO.
while(SSIBusy(SSI0_BASE))
[
]
// Display indication that the SSI is receiving data.
UARTprintf("nReceived:n ");
// Receive 3 bytes of data.
for(ui32Index = 0; ui32Index < NUM_SSI_DATA; ui32Index++)
[
// Receive the data using the "blocking" Get function. This function
// will wait until there is data in the receive FIFO before returning.
SSIDataGet(SSI0_BASE, &(pui32DataRx[ui32Index]));
// Since we are using 8-bit data, mask off the MSB.
pui32DataRx[ui32Index] &= 0x00FF;
// Display the data that SSI0 received.
UARTprintf("'%c' ", pui32DataRx[ui32Index]);
]

return(0);
]

回帖(3)

韦明

2018-8-15 07:35:32
SPI没有回环模式,无法实现自收发的,需要外部给他发,他才能收到。
关于这段代码的介绍,其实开头就有的。
//! This example shows how to configure the SSI0 as SPI Master. The code will
//! send three characters on the master Tx then polls the receive FIFO until
//! 3 characters are received on the master Rx.
需要Rx有3个字符的接收才输出的。
举报

梁宏满

2018-8-15 07:50:58
引用: ljmlvmd 发表于 2018-8-15 07:35
SPI没有回环模式,无法实现自收发的,需要外部给他发,他才能收到。
关于这段代码的介绍,其实开头就有的。
//! This example shows how to configure the SSI0 as SPI Master. The code will

配置成主机模式自己发给自己是可以实现自收发的,我现在问题已解决,配置代码没问题,问题在于我没将收发端短接,搞笑了
举报

韦明

2018-8-15 07:59:27
引用: uwjfisgw 发表于 2018-8-15 07:50
配置成主机模式自己发给自己是可以实现自收发的,我现在问题已解决,配置代码没问题,问题在于我没将收发端短接,搞笑了<img alt=" "/>

确实有点坑。我还以为你要配置为类似CAN的那种回环模式呢?短接肯定是可以的,因为有单独的Rx和Tx。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分