[DemoCode下载] M451用SPI flash做u盘的程序

[复制链接]
1659|7
 楼主| Nu_Micro 发表于 2016-11-23 09:58 | 显示全部楼层 |阅读模式
新唐官网BSP中有用Data flash做的Massstorage程序,好多客户需要用外部SPI flash作为Massstorage,该范例使用SPI2 接口驱动Winbond 25x16 SPI flash当作Massstorage;

[NUC451][APP] MassStorage_SPIFlash.rar

1.86 MB, 下载次数: 54

643757107 发表于 2016-11-23 19:22 | 显示全部楼层
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * @brief
  4. *           Use embedded data flash as storage to implement a USB Mass-Storage device.
  5. * @note
  6. * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
  7. ******************************************************************************/
  8. #include <stdio.h>
  9. #include "M451Series.h"
  10. #include "massstorage.h"



  11. /*--------------------------------------------------------------------------*/

  12. void SYS_Init(void)
  13. {
  14.     /*---------------------------------------------------------------------------------------------------------*/
  15.     /* Init System Clock                                                                                       */
  16.     /*---------------------------------------------------------------------------------------------------------*/

  17.     /* Enable Internal RC 22.1184MHz clock */
  18.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  19.     /* Waiting for Internal RC clock ready */
  20.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  21.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  22.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  23.     /* Enable external XTAL 12MHz clock */
  24.     CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);

  25.     /* Waiting for external XTAL clock ready */
  26.     CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);

  27.     /* Set Flash Access Delay */
  28.     FMC->FTCTL |= FMC_FTCTL_FOM_Msk;

  29.     /* Set core clock */
  30.     CLK_SetCoreClock(72000000);

  31.     /* Enable module clock */
  32.     CLK_EnableModuleClock(UART0_MODULE);
  33.     CLK_EnableModuleClock(USBD_MODULE);

  34.     /* Select module clock source */
  35.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));
  36.     CLK_SetModuleClock(USBD_MODULE, 0, CLK_CLKDIV0_USB(3));

  37.     /* Enable USB LDO33 */
  38.     SYS->USBPHY = SYS_USBPHY_LDO33EN_Msk;

  39.     /*---------------------------------------------------------------------------------------------------------*/
  40.     /* Init I/O Multi-function                                                                                 */
  41.     /*---------------------------------------------------------------------------------------------------------*/

  42.     /* Set GPD multi-function pins for UART0 RXD and TXD, and Clock Output */
  43.     SYS->GPD_MFPL = SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD | SYS_GPD_MFPL_PD6MFP_CLKO;

  44.     /* Enable CLKO (PD.6) for monitor HCLK. CLKO = HCLK/8 Hz */
  45.     CLK_EnableCKO(CLK_CLKSEL1_CLKOSEL_HCLK, 2, 0);
  46. }

  47. uint32_t u32SpiMidDid=0;
  48. unsigned char   SrcArray[256];
  49. unsigned char DestArray[256];

  50. /*---------------------------------------------------------------------------------------------------------*/
  51. /*  Main Function                                                                                          */
  52. /*---------------------------------------------------------------------------------------------------------*/
  53. int32_t main(void)
  54. {
  55. uint32_t        u32ByteCount;
  56.     /* Unlock protected registers */
  57.     SYS_UnlockReg();

  58.     SYS_Init();
  59.     /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
  60.     UART_Open(UART0, 115200);

  61.     printf("+-------------------------------------------------------+\n");
  62.     printf("|          NuMicro USB MassStorage Sample Code          |\n");
  63.     printf("+-------------------------------------------------------+\n");


  64.     SYS_UnlockReg();




  65.     /* Fill the Source Data and clear Destination Data Buffer */
  66.     for(u32ByteCount = 0; u32ByteCount < 256; u32ByteCount++)
  67.     {
  68.         SrcArray[u32ByteCount] = u32ByteCount;
  69.         DestArray[u32ByteCount] = 0;
  70.     }


  71.         SpiInit(SPI2);
  72.     /* read MID & DID */
  73.         u32SpiMidDid = SpiReadMidDid();
  74. //        SpiChipErase();
  75. //        SpiWrite(0,256, (uint32_t)SrcArray);
  76. //        SpiRead(0, 256, (uint32_t)DestArray);


  77. //    while(1);

  78.        

  79.     printf("NuMicro USB MassStorage Start!\n");

  80.     USBD_Open(&gsInfo, MSC_ClassRequest, NULL);

  81.     USBD_SetConfigCallback(MSC_SetConfig);

  82.     /* Endpoint configuration */
  83.     MSC_Init();
  84.     USBD_Start();
  85.     NVIC_EnableIRQ(USBD_IRQn);

  86.     while(1)
  87.     {
  88.         MSC_ProcessCmd();
  89.     }
  90. }



  91. /*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/


643757107 发表于 2016-11-23 19:23 | 显示全部楼层
BSP中有用Data flash做的Massstorage程序, 好多客户需要用外部SPI flash作为Massstorage, 该范例使用SPI2 接口驱动Winbond 25x16 SPI flash当作Massstorage  


zhuomuniao110 发表于 2016-11-23 20:17 | 显示全部楼层
属于做EEPROM还是直接在电脑上能操作的U盘?
dongnanxibei 发表于 2016-11-27 17:08 | 显示全部楼层
有的内部存储器不仅可以作为EEPROM用还可以作为Flash用,区别就是按字节擦写,还是按块擦写。
heisexingqisi 发表于 2016-11-27 18:31 | 显示全部楼层
没想到代码如此的简单就搞定了。
734774645 发表于 2016-11-27 20:39 | 显示全部楼层
原来是做U盘的控制器,读写存储颗粒。
sgr2000 发表于 2016-11-27 21:36 | 显示全部楼层
刚学Nu_Micro的分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

16

主题

29

帖子

0

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