[DemoCode下载] M0516的SPI_Loopback,就是自发自收

[复制链接]
 楼主| 天灵灵地灵灵 发表于 2016-8-28 23:32 | 显示全部楼层 |阅读模式
本帖最后由 天灵灵地灵灵 于 2016-8-28 23:36 编辑
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V2.00
  4. * $Revision: 2 $
  5. * $Date: 14/12/25 10:24a $
  6. * @brief
  7. *           Implement SPI Master loop back transfer.
  8. *           This sample code needs to connect SPI0_MISO0 pin and SPI0_MOSI0 pin together.
  9. *           It will compare the received data with transmitted data.
  10. * @note
  11. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  12. *****************************************************************************/
  13. #include <stdio.h>
  14. #include "M0518.h"

  15. #define TEST_COUNT             64

  16. uint32_t g_au32SourceData[TEST_COUNT];
  17. uint32_t g_au32DestinationData[TEST_COUNT];

  18. /* Function prototype declaration */
  19. void SYS_Init(void);
  20. void SPI_Init(void);

  21. /* ------------- */
  22. /* Main function */
  23. /* ------------- */
  24. int main(void)
  25. {
  26.     uint32_t u32DataCount, u32TestCount, u32Err;

  27.     /* Unlock protected registers */
  28.     SYS_UnlockReg();

  29.     SYS_Init();

  30.     /* Lock protected registers */
  31.     SYS_LockReg();

  32.     /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
  33.     UART_Open(UART0, 115200);

  34.     /* Init SPI */
  35.     SPI_Init();

  36.     printf("\n\n");
  37.     printf("+--------------------------------------------------------------------+\n");
  38.     printf("|                   M0518 SPI Driver Sample Code                    |\n");
  39.     printf("+--------------------------------------------------------------------+\n");
  40.     printf("\n");
  41.     printf("\nThis sample code demonstrates SPI0 self loop back data transfer.\n");
  42.     printf(" SPI0 configuration:\n");
  43.     printf("     Master mode; data width 32 bits.\n");
  44.     printf(" I/O connection:\n");
  45.     printf("     PC.3 SPI0_MOSI0 <--> PC.2 SPI0_MISO0 \n");

  46.     printf("\nSPI0 Loopback test ");

  47.     u32Err = 0;
  48.     for(u32TestCount = 0; u32TestCount < 0x1000; u32TestCount++)
  49.     {
  50.         /* set the source data and clear the destination buffer */
  51.         for(u32DataCount = 0; u32DataCount < TEST_COUNT; u32DataCount++)
  52.         {
  53.             g_au32SourceData[u32DataCount] = u32DataCount;
  54.             g_au32DestinationData[u32DataCount] = 0;
  55.         }

  56.         u32DataCount = 0;

  57.         if((u32TestCount & 0x1FF) == 0)
  58.         {
  59.             putchar('.');
  60.         }

  61.         while(1)
  62.         {
  63.             /* Write to TX register */
  64.             SPI_WRITE_TX(SPI0, g_au32SourceData[u32DataCount]);
  65.             /* Trigger SPI data transfer */
  66.             SPI_TRIGGER(SPI0);
  67.             /* Check SPI0 busy status */
  68.             while(SPI_IS_BUSY(SPI0));
  69.             /* Read received data */
  70.             g_au32DestinationData[u32DataCount] = SPI_READ_RX(SPI0);
  71.             u32DataCount++;
  72.             if(u32DataCount > TEST_COUNT)
  73.                 break;
  74.         }

  75.         /*  Check the received data */
  76.         for(u32DataCount = 0; u32DataCount < TEST_COUNT; u32DataCount++)
  77.         {
  78.             if(g_au32DestinationData[u32DataCount] != g_au32SourceData[u32DataCount])
  79.                 u32Err = 1;
  80.         }

  81.         if(u32Err)
  82.             break;
  83.     }

  84.     if(u32Err)
  85.         printf(" [FAIL]\n\n");
  86.     else
  87.         printf(" [PASS]\n\n");

  88.     /* Close SPI0 */
  89.     SPI_Close(SPI0);

  90.     while(1);
  91. }

  92. void SYS_Init(void)
  93. {

  94.     /*---------------------------------------------------------------------------------------------------------*/
  95.     /* Init System Clock                                                                                       */
  96.     /*---------------------------------------------------------------------------------------------------------*/

  97.     /* Enable external 12MHz XTAL */
  98.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  99.     /* Waiting for clock ready */
  100.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  101.     /* Switch HCLK clock source to HXT and HCLK source divide 1 */
  102.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HXT, CLK_CLKDIV_HCLK(1));

  103.     /* Select HXT as the clock source of UART0 */
  104.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));

  105.     /* Select HCLK as the clock source of SPI0 */
  106.     CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL1_SPI0_S_HCLK, MODULE_NoMsk);

  107.     /* Enable UART peripheral clock */
  108.     CLK_EnableModuleClock(UART0_MODULE);
  109.     /* Enable SPI0 peripheral clock */
  110.     CLK_EnableModuleClock(SPI0_MODULE);

  111.     /*---------------------------------------------------------------------------------------------------------*/
  112.     /* Init I/O Multi-function                                                                                 */
  113.     /*---------------------------------------------------------------------------------------------------------*/

  114.     /* Set PB multi-function pins for UART0 RXD and TXD */
  115.     SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;

  116.     /* Setup SPI0 multi-function pins */
  117.     SYS->GPC_MFP = SYS_GPC_MFP_PC0_SPI0_SS0 | SYS_GPC_MFP_PC1_SPI0_CLK | SYS_GPC_MFP_PC2_SPI0_MISO0 | SYS_GPC_MFP_PC3_SPI0_MOSI0;
  118.     SYS->ALT_MFP = SYS_ALT_MFP_PC0_SPI0_SS0 | SYS_ALT_MFP_PC1_SPI0_CLK | SYS_ALT_MFP_PC2_SPI0_MISO0 | SYS_ALT_MFP_PC3_SPI0_MOSI0;

  119.     /* Update System Core Clock */
  120.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
  121.     SystemCoreClockUpdate();
  122. }

  123. void SPI_Init(void)
  124. {
  125.     /*---------------------------------------------------------------------------------------------------------*/
  126.     /* Init SPI                                                                                                */
  127.     /*---------------------------------------------------------------------------------------------------------*/
  128.     /* Configure as a master, clock idle low, 32-bit transaction, drive output on falling clock edge and latch input on rising edge. */
  129.     /* Set IP clock divider. SPI clock rate = 2MHz */
  130.     SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 32, 2000000);

  131.     /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
  132.     SPI_EnableAutoSS(SPI0, SPI_SS0, SPI_SS_ACTIVE_LOW);
  133. }

  134. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/

1.gif
 楼主| 天灵灵地灵灵 发表于 2016-8-28 23:41 | 显示全部楼层
在学习的时候,如果你没有SPI设备,就可以这种方式,自发自收。
dongnanxibei 发表于 2016-8-29 20:21 | 显示全部楼层
就是环回的意思,就是发出去的,最后收回来。
 楼主| 天灵灵地灵灵 发表于 2016-9-1 21:05 | 显示全部楼层
自发自收也是一种学习的好方法,在你只有一个设备的时候就可以这么做来验证。
huangcunxiake 发表于 2016-9-3 08:41 | 显示全部楼层
SPI原来是这么配置的,以前没用过。
huangcunxiake 发表于 2016-9-3 08:44 | 显示全部楼层
看了一下初始化内容:1,配置模块的时钟系,2,使能模块时钟,然后就是配置管脚的功能选择。这样就可以用SPI了。
zhuotuzi 发表于 2016-9-4 09:51 | 显示全部楼层
SystemCoreClockUpdate();
为什么总是要有这么一句
 楼主| 天灵灵地灵灵 发表于 2016-9-4 10:04 | 显示全部楼层
因为只有使用那个更新,才会让配置的时钟系统生效。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

182

主题

3469

帖子

13

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