【N32G430C8L7开发板体验】+SPI外设通讯速度测试
根据用户手册,SPI接口的最高速度,在主模式下可以达到28 Mbps(不带CRC),20Mbps(带CRC),从模式可以达到32Mbps。
按照这个说明,启动SPI的通讯例程,选择的是SPI_INT例程,为了能顺利捕捉波形,稍微修改了代码,是SPI的收发处理一直被循环。
Main处理被修改为:
int main(void)
{
/* At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_N32G430.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_N32G430.c file
*/
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
NVIC_Configuration();
/* log configuration ------------------------------------------------------*/
log_init();
log_info("This is SPI INT demo!\r\n");
/* SPI_MASTER configuration ------------------------------------------------------*/
SPI_Initializes_Structure(&SPI_InitStructure);
SPI_InitStructure.DataDirection = SPI_DIR_SINGLELINE_TX;
SPI_InitStructure.SpiMode = SPI_MODE_MASTER;
SPI_InitStructure.DataLen = SPI_DATA_SIZE_16BITS;
SPI_InitStructure.CLKPOL = SPI_CLKPOL_LOW;
SPI_InitStructure.CLKPHA = SPI_CLKPHA_SECOND_EDGE;
SPI_InitStructure.NSS = SPI_NSS_HARD;
/* It is recommended that the SPI master mode of the C version chips should not exceed 18MHz */
SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_8;
SPI_InitStructure.FirstBit = SPI_FB_MSB;
SPI_InitStructure.CRCPoly = 7;
SPI_Initializes(SPI_MASTER, &SPI_InitStructure);
SPI_SS_Output_Enable(SPI_MASTER);
/* SPI_SLAVE configuration ------------------------------------------------------*/
SPI_InitStructure.SpiMode = SPI_MODE_SLAVE;
SPI_InitStructure.DataDirection = SPI_DIR_SINGLELINE_RX;
SPI_Initializes(SPI_SLAVE, &SPI_InitStructure);
/* Enable the master and slave SPI interrupts ----------------------------------*/
SPI_I2S_Interrupts_Enable(SPI_MASTER, SPI_I2S_INT_TE);
SPI_I2S_Interrupts_Enable(SPI_SLAVE, SPI_I2S_INT_RNE);
/* Enable SPI_SLAVE */
SPI_ON(SPI_SLAVE);
/* Enable SPI_MASTER */
SPI_ON(SPI_MASTER);
/* Transfer procedure */
while (RxIdx < BufferSize);
/* Check the received data with the send ones */
TransferStatus = Buffercmp(SPI_Slave_Buffer_Rx, SPI_Master_Buffer_Tx, BufferSize);
if(TransferStatus == PASSED) {
log_info("Test PASS!\r\n");
} else {
log_info("Test ERR!\r\n");
}
TxIdx = 0;
RxIdx = 0;
SPI_I2S_Data_Transmit(SPI1, SPI_Master_Buffer_Tx[0]);
while(1) {
/* Transfer procedure */
while (RxIdx < 1);
// 为了测试发送速度,不断重复发送
TxIdx = 0;
RxIdx = 0;
SPI_I2S_Data_Transmit(SPI1, SPI_Master_Buffer_Tx[0]);
}
}
中断处理被改为:
/**
*\*\name SPI1_IRQHandler.
*\*\fun This function handles SPI1 Handler.
*\*\param none
*\*\return none
**/
void SPI1_IRQHandler(void) {
if(SPI_I2S_Interrupt_Flag_Status_Get(SPI1, SPI_I2S_INT_FLAG_TE) != RESET) {
/* Send SPI_MASTER data */
SPI_I2S_Data_Transmit(SPI1, SPI_Master_Buffer_Tx[TxIdx++]);
if(TxIdx == BufferSize) {
/* Disable SPI_MATER TXE interrupt */
//SPI_I2S_Interrupts_Disable(SPI1, SPI_I2S_INT_TE);
}
}
}
/**
*\*\name SPI2_IRQHandler.
*\*\fun This function handles SPI2 Handler.
*\*\param none
*\*\return none
**/
void SPI2_IRQHandler(void) {
if(SPI_I2S_Interrupt_Flag_Status_Get(SPI2, SPI_I2S_INT_FLAG_RNE) == SET) {
/* Store SPI_SLAVE received data */
SPI_Slave_Buffer_Rx[RxIdx++] = SPI_I2S_Data_Get(SPI2);
}
}
然后测试,捕捉的通讯SCK脉冲波形如下:
波形不是方波,时钟周期是120ns的样子,确实比用GPIO模拟的方式快啊,差别很明显。
|