[DemoCode下载] SPI接口的一般用法

[复制链接]
1453|2
 楼主| 小灵通2018 发表于 2024-5-19 19:06 | 显示全部楼层 |阅读模式
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 2 $
  5. * $Date: 20/06/10 10:56a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Implement SPI Master loop back transfer. This sample code needs to
  7. *           connect MISO_0 pin and MOSI_0 pin together. It will compare the
  8. *           received data with transmitted data.
  9. *
  10. * @note
  11. * SPDX-License-Identifier: Apache-2.0
  12. * Copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
  13. *
  14. *****************************************************************************/
  15. #include <stdio.h>
  16. #include "NuMicro.h"

  17. #define TEST_CYCLE      0x1000
  18. #define DATA_COUNT      64
  19. #define SPI_CLK_FREQ    2000000


  20. uint32_t g_au32SourceData[DATA_COUNT];
  21. uint32_t g_au32DestinationData[DATA_COUNT];

  22. /* Function prototype declaration */
  23. void SYS_Init(void);
  24. void SPI_Init(void);

  25. /* ------------- */
  26. /* Main function */
  27. /* ------------- */
  28. int main(void)
  29. {
  30.     uint32_t u32DataCount, u32TestCycle, u32Err;
  31.     uint32_t u32TimeOutCount;

  32.     /* Unlock protected registers */
  33.     SYS_UnlockReg();
  34.     /* Init System, IP clock and multi-function I/O. */
  35.     SYS_Init();
  36.     /* Lock protected registers */
  37.     SYS_LockReg();

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

  40.     /* Init SPI */
  41.     SPI_Init();

  42.     printf("\n\n");
  43.     printf("+--------------------------------------------------------------------+\n");
  44.     printf("|                   M030G SPI Driver Sample Code                      |\n");
  45.     printf("+--------------------------------------------------------------------+\n");
  46.     printf("\n");
  47.     printf("\nThis sample code demonstrates SPI0 self loop back data transfer.\n");
  48.     printf(" SPI0 configuration:\n");
  49.     printf("     Master mode; data width 32 bits.\n");
  50.     printf(" I/O connection:\n");
  51.     printf("     SPI0_MOSI(PA.0) <--> SPI0_MISO(PA.1) \n");

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

  53.     u32Err = 0;
  54.     for(u32TestCycle = 0; u32TestCycle < TEST_CYCLE; u32TestCycle++)
  55.     {
  56.         /* set the source data and clear the destination buffer */
  57.         for(u32DataCount = 0; u32DataCount < DATA_COUNT; u32DataCount++)
  58.         {
  59.             g_au32SourceData[u32DataCount] = u32DataCount;
  60.             g_au32DestinationData[u32DataCount] = 0;
  61.         }

  62.         u32DataCount = 0;

  63.         if((u32TestCycle & 0x1FF) == 0)
  64.         {
  65.             putchar('.');
  66.         }

  67.         while(1)
  68.         {
  69.             /* setup timeout */
  70.             u32TimeOutCount = SystemCoreClock;

  71.             /* Write to TX register */
  72.             SPI_WRITE_TX(SPI0, g_au32SourceData[u32DataCount]);
  73.             /* Check SPI0 busy status */
  74.             while(SPI_IS_BUSY(SPI0))
  75.             {
  76.                 if(u32TimeOutCount == 0)
  77.                 {
  78.                     printf("\nSomething is wrong, please check if pin connection is correct. \n");
  79.                     while(1);
  80.                 }
  81.                 u32TimeOutCount--;
  82.             }

  83.             /* Read received data */
  84.             g_au32DestinationData[u32DataCount] = SPI_READ_RX(SPI0);
  85.             u32DataCount++;
  86.             if(u32DataCount >= DATA_COUNT)
  87.                 break;
  88.         }

  89.         /*  Check the received data */
  90.         for(u32DataCount = 0; u32DataCount < DATA_COUNT; u32DataCount++)
  91.         {
  92.             if(g_au32DestinationData[u32DataCount] != g_au32SourceData[u32DataCount])
  93.                 u32Err = 1;
  94.         }

  95.         if(u32Err)
  96.             break;
  97.     }

  98.     if(u32Err)
  99.         printf(" [FAIL]\n\n");
  100.     else
  101.         printf(" [PASS]\n\n");

  102.     /* Close SPI0 */
  103.     SPI_Close(SPI0);

  104.     while(1);
  105. }

  106. void SYS_Init(void)
  107. {
  108.     /*---------------------------------------------------------------------------------------------------------*/
  109.     /* Init System Clock                                                                                       */
  110.     /*---------------------------------------------------------------------------------------------------------*/
  111.     /* Enable HIRC clock */
  112.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  113.     /* Waiting for HIRC clock ready */
  114.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  115.     /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
  116.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  117.     /* Select HIRC as the clock source of UART0 */
  118.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  119.     /* Select PCLK1 as the clock source of SPI0 */
  120.     CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL2_SPI0SEL_PCLK1, MODULE_NoMsk);

  121.     /* Enable UART peripheral clock */
  122.     CLK_EnableModuleClock(UART0_MODULE);

  123.     /* Enable SPI0 peripheral clock */
  124.     CLK_EnableModuleClock(SPI0_MODULE);

  125.     /*---------------------------------------------------------------------------------------------------------*/
  126.     /* Init I/O Multi-function                                                                                 */
  127.     /*---------------------------------------------------------------------------------------------------------*/
  128.     /* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
  129.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
  130.                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  131.     /* Setup SPI0 multi-function pins */
  132.     /* PA.3 is SPI0_SS,   PA.2 is SPI0_CLK,
  133.        PA.1 is SPI0_MISO, PA.0 is SPI0_MOSI*/
  134.     SYS->GPA_MFPL = (SYS->GPA_MFPL & ~(SYS_GPA_MFPL_PA3MFP_Msk |
  135.                                        SYS_GPA_MFPL_PA2MFP_Msk |
  136.                                        SYS_GPA_MFPL_PA1MFP_Msk |
  137.                                        SYS_GPA_MFPL_PA0MFP_Msk)) |
  138.                     (SYS_GPA_MFPL_PA3MFP_SPI0_SS |
  139.                      SYS_GPA_MFPL_PA2MFP_SPI0_CLK |
  140.                      SYS_GPA_MFPL_PA1MFP_SPI0_MISO |
  141.                      SYS_GPA_MFPL_PA0MFP_SPI0_MOSI);

  142.     /* Update System Core Clock */
  143.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
  144.     SystemCoreClockUpdate();
  145. }

  146. void SPI_Init(void)
  147. {
  148.     /*---------------------------------------------------------------------------------------------------------*/
  149.     /* Init SPI                                                                                                */
  150.     /*---------------------------------------------------------------------------------------------------------*/
  151.     /* Configure as a master, clock idle low, 32-bit transaction, drive output on falling clock edge and latch input on rising edge. */
  152.     /* Set IP clock divider. SPI clock rate = 2MHz */
  153.     SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 32, SPI_CLK_FREQ);

  154.     /* Enable the automatic hardware slave select function. Select the SS pin and configure as low-active. */
  155.     SPI_EnableAutoSS(SPI0, SPI_SS, SPI_SS_ACTIVE_LOW);
  156. }

  157. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/


 楼主| 小灵通2018 发表于 2024-5-19 19:07 | 显示全部楼层
SPI的一般用法比较容易实现,打开,使能相关设置即可。
 楼主| 小灵通2018 发表于 2024-5-19 19:08 | 显示全部楼层
SPI是一种比I2C更快的串行接口,一般用于TFT彩色屏幕,或者用于高速的存储芯片。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

158

主题

1732

帖子

4

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