[N32G430] 【N32G430C8L7开发板体验】+SPI外设通讯速度测试

[复制链接]
3285|5
 楼主| suncat0504 发表于 2024-4-7 13:21 | 显示全部楼层 |阅读模式
【N32G430C8L7开发板体验】+SPI外设通讯速度测试
根据用户手册,SPI接口的最高速度,在主模式下可以达到28 Mbps(不带CRC),20Mbps(带CRC),从模式可以达到32Mbps。
按照这个说明,启动SPI的通讯例程,选择的是SPI_INT例程,为了能顺利捕捉波形,稍微修改了代码,是SPI的收发处理一直被循环。
Main处理被修改为:
  1. int main(void)

  2. {

  3.     /* At this stage the microcontroller clock setting is already configured,

  4.        this is done through SystemInit() function which is called from startup

  5.        file (startup_N32G430.s) before to branch to application main.

  6.        To reconfigure the default setting of SystemInit() function, refer to

  7.        system_N32G430.c file

  8.      */

  9.     /* System clocks configuration ---------------------------------------------*/

  10.     RCC_Configuration();

  11.     /* GPIO configuration ------------------------------------------------------*/

  12.     GPIO_Configuration();

  13.   

  14.     NVIC_Configuration();

  15.     /* log configuration ------------------------------------------------------*/

  16.     log_init();   

  17.     log_info("This is SPI INT demo!\r\n");

  18.     /* SPI_MASTER configuration ------------------------------------------------------*/

  19.     SPI_Initializes_Structure(&SPI_InitStructure);

  20.     SPI_InitStructure.DataDirection = SPI_DIR_SINGLELINE_TX;

  21.     SPI_InitStructure.SpiMode       = SPI_MODE_MASTER;

  22.     SPI_InitStructure.DataLen       = SPI_DATA_SIZE_16BITS;

  23.     SPI_InitStructure.CLKPOL        = SPI_CLKPOL_LOW;

  24.     SPI_InitStructure.CLKPHA        = SPI_CLKPHA_SECOND_EDGE;

  25.     SPI_InitStructure.NSS           = SPI_NSS_HARD;

  26.     /* It is recommended that the SPI master mode of the C version chips should not exceed 18MHz */

  27.     SPI_InitStructure.BaudRatePres  = SPI_BR_PRESCALER_8;

  28.     SPI_InitStructure.FirstBit      = SPI_FB_MSB;

  29.     SPI_InitStructure.CRCPoly       = 7;

  30.     SPI_Initializes(SPI_MASTER, &SPI_InitStructure);

  31.     SPI_SS_Output_Enable(SPI_MASTER);



  32.     /* SPI_SLAVE configuration ------------------------------------------------------*/

  33.     SPI_InitStructure.SpiMode = SPI_MODE_SLAVE;

  34.     SPI_InitStructure.DataDirection = SPI_DIR_SINGLELINE_RX;

  35.     SPI_Initializes(SPI_SLAVE, &SPI_InitStructure);



  36.     /* Enable the master and slave SPI interrupts ----------------------------------*/

  37.     SPI_I2S_Interrupts_Enable(SPI_MASTER, SPI_I2S_INT_TE);

  38.     SPI_I2S_Interrupts_Enable(SPI_SLAVE, SPI_I2S_INT_RNE);



  39.     /* Enable SPI_SLAVE */

  40.     SPI_ON(SPI_SLAVE);        

  41.     /* Enable SPI_MASTER */

  42.     SPI_ON(SPI_MASTER);



  43.     /* Transfer procedure */

  44.     while (RxIdx < BufferSize);

  45.    

  46.     /* Check the received data with the send ones */

  47.     TransferStatus = Buffercmp(SPI_Slave_Buffer_Rx, SPI_Master_Buffer_Tx, BufferSize);



  48.     if(TransferStatus == PASSED) {

  49.         log_info("Test PASS!\r\n");

  50.     } else {

  51.         log_info("Test ERR!\r\n");

  52.     }



  53.     TxIdx = 0;

  54.     RxIdx = 0;

  55.     SPI_I2S_Data_Transmit(SPI1, SPI_Master_Buffer_Tx[0]);

  56.    

  57.     while(1) {

  58.         /* Transfer procedure */

  59.         while (RxIdx < 1);

  60.         // 为了测试发送速度,不断重复发送

  61.         TxIdx = 0;

  62.         RxIdx = 0;

  63.         SPI_I2S_Data_Transmit(SPI1, SPI_Master_Buffer_Tx[0]);



  64.     }

  65. }

中断处理被改为:
  1. /**

  2. *\*\name    SPI1_IRQHandler.

  3. *\*\fun     This function handles SPI1 Handler.

  4. *\*\param   none

  5. *\*\return  none

  6. **/

  7. void SPI1_IRQHandler(void) {

  8.     if(SPI_I2S_Interrupt_Flag_Status_Get(SPI1, SPI_I2S_INT_FLAG_TE) != RESET) {

  9.         /* Send SPI_MASTER data */

  10.         SPI_I2S_Data_Transmit(SPI1, SPI_Master_Buffer_Tx[TxIdx++]);

  11.         if(TxIdx == BufferSize) {

  12.             /* Disable SPI_MATER TXE interrupt */

  13.             //SPI_I2S_Interrupts_Disable(SPI1, SPI_I2S_INT_TE);

  14.         }

  15.     }

  16. }



  17. /**

  18. *\*\name    SPI2_IRQHandler.

  19. *\*\fun     This function handles SPI2 Handler.

  20. *\*\param   none

  21. *\*\return  none

  22. **/

  23. void SPI2_IRQHandler(void) {

  24.     if(SPI_I2S_Interrupt_Flag_Status_Get(SPI2, SPI_I2S_INT_FLAG_RNE) == SET) {

  25.         /* Store SPI_SLAVE received data */

  26.         SPI_Slave_Buffer_Rx[RxIdx++] = SPI_I2S_Data_Get(SPI2);

  27.     }

  28. }


然后测试,捕捉的通讯SCK脉冲波形如下:
图片1.png

波形不是方波,时钟周期是120ns的样子,确实比用GPIO模拟的方式快啊,差别很明显。

weifeng90 发表于 2024-4-7 19:27 来自手机 | 显示全部楼层
这肯定的啊,软件模拟怎么能比硬件快。
 楼主| suncat0504 发表于 2024-4-7 22:26 | 显示全部楼层
weifeng90 发表于 2024-4-7 19:27
这肯定的啊,软件模拟怎么能比硬件快。

确实快,但是时钟波形和预想的方波差太多了。差点以为自己的示波器出了问题。哈哈哈。
chenqianqian 发表于 2024-4-8 07:57 来自手机 | 显示全部楼层
因为SPI是边沿采样捕获数据
戈壁滩中的辉煌 发表于 2024-4-8 11:42 | 显示全部楼层
这要是驱动屏幕效果也是不错的
黑心单片机 发表于 2024-4-10 15:03 | 显示全部楼层
在用这款,巨好用
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:大连伊飞特信息技术有限公司软件工程师
简介:本人于1993年毕业于大连理工大学。毕业后从事单片机开发工作5年,之后转入软件开发工作至今。

158

主题

4507

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部