[DemoCode下载] M051的FIFO,SPI传输

[复制链接]
1283|4
 楼主| 小明的同学 发表于 2019-5-26 22:57 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V0.10
  4. * $Revision: 3 $
  5. * $Date: 14/01/28 11:43a $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    M051 Series SPI Flash Driver Sample Code
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *
  11. ******************************************************************************/
  12. #include <stdio.h>
  13. #include "M051Series.h"
  14. #include "spi_flash.h"
  15. #include "lcd_driver.h"

  16. #define TEST_PAGE_NUM 10  /* page numbers */
  17. #define BYTE_PER_PAGE 256 /* byte per page */

  18. uint8_t SrcArray[BYTE_PER_PAGE];
  19. uint8_t DestArray[BYTE_PER_PAGE];
  20. uint32_t g_u32SystickCount;

  21. /* Function prototype declaration */
  22. void SYS_Init(void);

  23. /* ------------- */
  24. /* Main function */
  25. /* ------------- */
  26. int main(void)
  27. {
  28.     uint32_t u32ByteCount, u32FlashAddress, u32PageNumber;
  29.     uint32_t u32Error = 0;
  30.     uint32_t u32ID;
  31.     uint32_t u32Duration_W, u32Duration_R;
  32.     char au8String[20];

  33.     /* Init System, IP clock and multi-function I/O.
  34.        In the end of SYS_Init() will issue SYS_LockReg()
  35.        to lock protected register. If user want to write
  36.        protected register, please issue SYS_UnlockReg()
  37.        to unlock protected register if necessary. */
  38.     SYS_Init();

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

  41.     /* Configure SPI1 as a master, MSB first, 8-bit transaction, SPI Mode-0 timing, clock is 6MHz */
  42.     SPI_Open(SPI1, SPI_MASTER, SPI_MODE_0, 8, 6000000);

  43.     /* Enable the automatic hardware slave select function. Select the SS0 pin and configure as low-active. */
  44.     SPI_EnableAutoSS(SPI1, SPI_SS, SPI_SS_ACTIVE_LOW);
  45.     /* Enable FIFO mode; set TX FIFO threshold and RX FIFO threshold to 2. */
  46.     SPI_EnableFIFO(SPI1, 2, 2);

  47.     printf("\n+---------------------------------------------------------------------+\n");
  48.     printf("|            M051 Series SPI Flash with FIFO Mode Sample              |\n");
  49.     printf("+---------------------------------------------------------------------+\n");
  50.     printf("\n");
  51.     printf("\nThis sample code demonstrates how to access a Winbond 25x16 SPI flash with FIFO buffers.\n");
  52.     printf("The whole SPI flash will be erased. Afterward this sample code will write, read and compare %d-page data.\n", TEST_PAGE_NUM);
  53.     printf("\nThe duration of read operation and write operation will be shown on LCD.\n");

  54.     /* Initial LCD panel. SPI clock and interface is configured in this function. */
  55.     LCD_Init();
  56.     /* Clear LCD screen. Do this before turn back light on */
  57.     LCD_ClearScreen();
  58.     /* Enable LCD back light */
  59.     LCD_EnableBackLight();
  60.     /* Show messages on LCD */
  61.     LCD_Print(0, " SPI flash test");
  62.     LCD_Print(1, "  with FIFO");

  63.     /* Wait ready */
  64.     SpiFlash_WaitReady(SPI1);

  65.     if((u32ID = SpiFlash_ReadMidDid(SPI1)) != 0xEF14)
  66.     {
  67.         printf("Wrong ID, 0x%x\n", u32ID);
  68.         while(1);
  69.     }
  70.     else
  71.     {
  72.         printf("Flash found: W25x16 ...\n");
  73.         LCD_Print(2, "Flash W25x16");
  74.     }

  75.     printf("Erase chip ...");
  76.     LCD_Print(3, "Erase chip ...");

  77.     /* Erase SPI flash */
  78.     SpiFlash_ChipErase(SPI1);

  79.     /* Wait ready */
  80.     SpiFlash_WaitReady(SPI1);

  81.     printf("[OK]\n");

  82.     /* init source data buffer */
  83.     for(u32ByteCount = 0; u32ByteCount < BYTE_PER_PAGE; u32ByteCount++)
  84.     {
  85.         SrcArray[u32ByteCount] = u32ByteCount;
  86.     }

  87.     printf("Start to write data to Flash ...");
  88.     LCD_Print(3, "Write SPI flash");

  89.     g_u32SystickCount = 0;

  90.     /* Configure SysTick */
  91.     /* 1 milli-second per tick */
  92.     SysTick->LOAD = 1000 * CyclesPerUs; /* 1000us */
  93.     /* Clear SysTick current value register and system tick counter flag */
  94.     SysTick->VAL  = (0x00);
  95.     /* Core clock used for SysTick timer; enable system tick interrupt; start counting. */
  96.     SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;

  97.     /* Program SPI flash */
  98.     u32FlashAddress = 0;
  99.     for(u32PageNumber = 0; u32PageNumber < TEST_PAGE_NUM; u32PageNumber++)
  100.     {
  101.         /* page program */
  102.         SpiFlash_PageProgram(SPI1, u32FlashAddress, SrcArray, BYTE_PER_PAGE);
  103.         SpiFlash_WaitReady(SPI1);
  104.         u32FlashAddress += 0x100;
  105.     }

  106.     /* Log the duration of write operation */
  107.     u32Duration_W = g_u32SystickCount;
  108.     /* Stop SysTick timer */
  109.     SysTick->CTRL = 0;

  110.     printf("[OK]\n");

  111.     /* clear destination data buffer */
  112.     for(u32ByteCount = 0; u32ByteCount < BYTE_PER_PAGE; u32ByteCount++)
  113.     {
  114.         DestArray[u32ByteCount] = 0;
  115.     }

  116.     printf("Read & Compare ...");
  117.     LCD_Print(3, "Read & compare ");

  118.     g_u32SystickCount = 0;
  119.     /* Core clock used for SysTick timer; enable system tick interrupt; start counting. */
  120.     SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;

  121.     /* Read SPI flash */
  122.     u32FlashAddress = 0;
  123.     for(u32PageNumber = 0; u32PageNumber < TEST_PAGE_NUM; u32PageNumber++)
  124.     {
  125.         /* page read */
  126.         SpiFlash_ReadData(SPI1, u32FlashAddress, DestArray, BYTE_PER_PAGE);
  127.         u32FlashAddress += 0x100;

  128.         for(u32ByteCount = 0; u32ByteCount < BYTE_PER_PAGE; u32ByteCount++)
  129.         {
  130.             if(DestArray[u32ByteCount] != SrcArray[u32ByteCount])
  131.                 u32Error ++;
  132.         }
  133.     }

  134.     /* Log the duration of read operation */
  135.     u32Duration_R = g_u32SystickCount;
  136.     /* Stop SysTick timer */
  137.     SysTick->CTRL = 0;

  138.     if(u32Error == 0)
  139.     {
  140.         printf("[OK]\n");
  141.         sprintf(au8String, "R/W: %d/%d ms", u32Duration_R, u32Duration_W);
  142.         LCD_Print(3, au8String);
  143.     }
  144.     else
  145.     {
  146.         printf("[FAIL]\n");
  147.         LCD_Print(3, "[FAIL]         ");
  148.     }

  149.     /* Reset SPI1 and disable SPI1 peripheral clock */
  150.     SPI_Close(SPI1);

  151.     while(1);
  152. }

  153. void SYS_Init(void)
  154. {
  155.     /*---------------------------------------------------------------------------------------------------------*/
  156.     /* Init System Clock                                                                                       */
  157.     /*---------------------------------------------------------------------------------------------------------*/
  158.     /* Unlock protected registers */
  159.     SYS_UnlockReg();

  160.     /* Set HCLK source form HXT and HCLK source divide 1  */
  161.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HXT, CLK_CLKDIV_HCLK(1));

  162.     /* Enable external 12MHz XTAL */
  163.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  164.     /* Waiting for clock ready */
  165.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

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

  168.     /* Select HXT as the clock source of SPI1 */
  169.     CLK_SetModuleClock(SPI1_MODULE, CLK_CLKSEL1_SPI1_S_HCLK, MODULE_NoMsk);

  170.     /* Enable UART peripheral clock */
  171.     CLK_EnableModuleClock(UART0_MODULE);
  172.     /* Enable SPI1 peripheral clock */
  173.     CLK_EnableModuleClock(SPI1_MODULE);

  174.     /* Update System Core Clock */
  175.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  176.     SystemCoreClockUpdate();

  177.     /*---------------------------------------------------------------------------------------------------------*/
  178.     /* Init I/O Multi-function                                                                                 */
  179.     /*---------------------------------------------------------------------------------------------------------*/
  180.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  181.     SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;

  182.     /* Set multi function pin for SPI1 */
  183.     SYS->P0_MFP = SYS_MFP_P04_SPISS1 | SYS_MFP_P05_MOSI_1 | SYS_MFP_P06_MISO_1 | SYS_MFP_P07_SPICLK1;

  184.     /* Lock protected registers */
  185.     SYS_LockReg();
  186. }

  187. void SysTick_Handler(void)
  188. {
  189.     g_u32SystickCount++;
  190.     /* Clear SysTick current value register and system tick counter flag */
  191.     SysTick->VAL  = (0x00);
  192. }


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




 楼主| 小明的同学 发表于 2019-5-26 22:57 | 显示全部楼层
看了手册,这个好像就支持SPI通信的FIFO操作。
21mengnan 发表于 2019-5-27 19:20 | 显示全部楼层
M451系列是不是支持更多的FIFO。
equivalent 发表于 2019-5-29 20:35 | 显示全部楼层
感谢分享!学习下
稳稳の幸福 发表于 2019-5-29 22:17 | 显示全部楼层
    SystemCoreClockUpdate();
这个一般是啥时候用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

159

主题

1640

帖子

2

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