M051的SPI的主机FIFO模式使用方法

[复制链接]
1035|2
 楼主| xixi2017 发表于 2024-11-20 21:09 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.0
  4. * $Revision: 2 $
  5. * $Date: 15/05/22 3:03p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Configure SPI0 as Master mode and demonstrate how to communicate
  7. *           with an off-chip SPI Slave device with FIFO mode. This sample
  8. *           code needs to work with SPI_SlaveFifoMode sample code.
  9. *
  10. * @note
  11. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  12. *
  13. ******************************************************************************/
  14. #include <stdio.h>
  15. #include "M051Series.h"


  16. #define TEST_COUNT 16

  17. uint32_t g_au32SourceData[TEST_COUNT];
  18. uint32_t g_au32DestinationData[TEST_COUNT];
  19. volatile uint32_t g_u32TxDataCount;
  20. volatile uint32_t g_u32RxDataCount;

  21. /* Function prototype declaration */
  22. void SYS_Init(void);
  23. void SPI_Init(void);
  24. extern char GetChar(void);

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

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

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

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

  41.     printf("\n\n");
  42.     printf("+----------------------------------------------------------------------+\n");
  43.     printf("|             SPI Master Mode Sample Code (M05xxDN/DE only)            |\n");
  44.     printf("+----------------------------------------------------------------------+\n");
  45.     printf("\n");
  46.     printf("Configure SPI0 as a master.\n");
  47.     printf("Bit length of a transaction: 32\n");
  48.     printf("The I/O connection for SPI0:\n");
  49.     printf("    SPISS0(P1.4)\n    SPICLK0(P1.7)\n");
  50.     printf("    MISO_0(P1.6)\n    MOSI_0(P1.5)\n\n");
  51.     printf("SPI controller will enable FIFO mode and transfer %d data to a off-chip slave device.\n", TEST_COUNT);
  52.     printf("In the meanwhile the SPI controller will receive %d data from the off-chip slave device.\n", TEST_COUNT);
  53.     printf("After the transfer is done, the %d received data will be printed out.\n", TEST_COUNT);
  54.     printf("The SPI master configuration is ready.\n");

  55.     for(u32DataCount = 0; u32DataCount < TEST_COUNT; u32DataCount++)
  56.     {
  57.         /* Write the initial value to source buffer */
  58.         g_au32SourceData[u32DataCount] = 0x00550000 + u32DataCount;
  59.         /* Clear destination buffer */
  60.         g_au32DestinationData[u32DataCount] = 0;
  61.     }

  62.     printf("Before starting the data transfer, make sure the slave device is ready. Press any key to start the transfer.\n");
  63.     GetChar();
  64.     printf("\n");

  65.     /* Set TX FIFO threshold, enable TX FIFO threshold interrupt and RX FIFO time-out interrupt */
  66.     SPI_EnableFIFO(SPI0, 4, 4);
  67.     SPI_EnableInt(SPI0, SPI_FIFO_TX_INT_MASK | SPI_FIFO_TIMEOUT_INT_MASK);
  68.    
  69.     g_u32TxDataCount = 0;
  70.     g_u32RxDataCount = 0;
  71.     NVIC_EnableIRQ(SPI0_IRQn);

  72.     /* Wait for transfer done */
  73.     while(g_u32RxDataCount < TEST_COUNT);

  74.     /* Print the received data */
  75.     printf("Received data:\n");
  76.     for(u32DataCount = 0; u32DataCount < TEST_COUNT; u32DataCount++)
  77.     {
  78.         printf("%d:\t0x%X\n", u32DataCount, g_au32DestinationData[u32DataCount]);
  79.     }
  80.     /* Disable TX FIFO threshold interrupt and RX FIFO time-out interrupt */
  81.     SPI_DisableInt(SPI0, SPI_FIFO_TX_INT_MASK | SPI_FIFO_TIMEOUT_INT_MASK);
  82.     NVIC_DisableIRQ(SPI0_IRQn);
  83.     printf("The data transfer was done.\n");

  84.     printf("\n\nExit SPI driver sample code.\n");

  85.     /* Reset SPI0 */
  86.     SPI_Close(SPI0);
  87.     while(1);
  88. }

  89. void SYS_Init(void)
  90. {
  91.     /*---------------------------------------------------------------------------------------------------------*/
  92.     /* Init System Clock                                                                                       */
  93.     /*---------------------------------------------------------------------------------------------------------*/
  94.    
  95.     /* Enable external 12MHz XTAL */
  96.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  97.     /* Waiting for clock ready */
  98.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  99.     /* Switch HCLK clock source to HXT and set HCLK divider to 1 */
  100.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HXT, CLK_CLKDIV_HCLK(1));

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

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

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

  109.     /* Update System Core Clock */
  110.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CyclesPerUs automatically. */
  111.     SystemCoreClockUpdate();

  112.     /*---------------------------------------------------------------------------------------------------------*/
  113.     /* Init I/O Multi-function                                                                                 */
  114.     /*---------------------------------------------------------------------------------------------------------*/
  115.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  116.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;

  117.     /* Setup SPI0 multi-function pins */
  118.     SYS->P1_MFP = SYS_MFP_P14_SPISS0 | SYS_MFP_P15_MOSI_0 | SYS_MFP_P16_MISO_0 | SYS_MFP_P17_SPICLK0;
  119. }

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

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

  131. void SPI0_IRQHandler(void)
  132. {
  133.     /* Check RX EMPTY flag */
  134.     while(SPI_GET_RX_FIFO_EMPTY_FLAG(SPI0) == 0)
  135.     {
  136.         /* Read RX FIFO */
  137.         g_au32DestinationData[g_u32RxDataCount++] = SPI_READ_RX0(SPI0);
  138.     }
  139.     /* Check TX FULL flag and TX data count */
  140.     while((SPI_GET_TX_FIFO_FULL_FLAG(SPI0) == 0) && (g_u32TxDataCount < TEST_COUNT))
  141.     {
  142.         /* Write to TX FIFO */
  143.         SPI_WRITE_TX0(SPI0, g_au32SourceData[g_u32TxDataCount++]);
  144.     }
  145.     if(g_u32TxDataCount >= TEST_COUNT)
  146.          SPI_DisableInt(SPI0, SPI_FIFO_TX_INT_MASK); /* Disable TX FIFO threshold interrupt */

  147.     /* Check the RX FIFO time-out interrupt flag */
  148.     if(SPI_GetIntFlag(SPI0, SPI_FIFO_TIMEOUT_INT_MASK))
  149.     {
  150.         /* If RX FIFO is not empty, read RX FIFO. */
  151.         while((SPI0->STATUS & SPI_STATUS_RX_EMPTY_Msk) == 0)
  152.             g_au32DestinationData[g_u32RxDataCount++] = SPI_READ_RX0(SPI0);
  153.     }
  154. }


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



 楼主| xixi2017 发表于 2024-11-20 21:10 | 显示全部楼层
这个模式只要把数据丢到FIFO里面,就会自动完成发送了,有点类似DMA的那种风格。
比神乐 发表于 2024-11-21 14:53 | 显示全部楼层
这种帖子不该发到这个模块吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则

145

主题

2034

帖子

2

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