[活动专区] 【AT-START-F407测评】3.运行SPIM示例代码

[复制链接]
937|1
 楼主| xinmeng_wit 发表于 2021-2-16 21:30 | 显示全部楼层 |阅读模式
AT32F407可以通过SPIM外扩SPI接口的flash芯片,AT32F407预留了bank3作为外扩FLASH,闪存模块组织如下:

1.jpg

bank1和bank2给内部flash使用,bank3留给外部flash使用,最大容量16M字节。
对bank3的操作与bank1和bank2相同。

硬件接口

2.jpg

接口初始化

  1. void FLASH_InitExtFlash(void)
  2. {
  3.   GPIO_InitType GPIO_InitStructure;
  4.   
  5.   /* Enable ext.flash GPIO clock */
  6.   RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_AFIO|RCC_APB2PERIPH_GPIOA|RCC_APB2PERIPH_GPIOB, ENABLE);
  7.   
  8.   /* Configure ext.flash pin */
  9.   GPIO_StructInit(&GPIO_InitStructure);
  10.   GPIO_InitStructure.GPIO_Pins = GPIO_Pins_8;
  11.   GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
  12.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  13.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  14.   GPIO_InitStructure.GPIO_Pins = GPIO_Pins_1|GPIO_Pins_6|GPIO_Pins_7|GPIO_Pins_10|GPIO_Pins_11;
  15.   GPIO_Init(GPIOB, &GPIO_InitStructure);
  16.   
  17.   /* Select PB10, PB11 as SPIF_TX and SPIF_RX */
  18.   GPIO_PinsRemapConfig(GPIO_Remap_EXT_FLASH, ENABLE);
  19.   GPIO_PinsRemapConfig(AFIO_MAP7_SPIF_1001, ENABLE);
  20.   
  21.   /* In this example, use on-board EN25QH128A as ext.flash */
  22.   FLASH->B3SEL = FLASH_SPIM_TYPE2;
  23.   
  24.   /* Unlock the ext.flash program erase controller */  
  25.   while (BIT_READ(FLASH->STS3, FLASH_STS_BSY));
  26.   FLASH->FCKEY3 = FLASH_KEY1;
  27.   FLASH->FCKEY3 = FLASH_KEY2;
  28.   while (BIT_READ(FLASH->CTRL3, FLASH_CTRL_LCK));
  29.   
  30.   /* If the data written to ext.flash need to be scrambled, please specify the scrambled range */
  31.   FLASH->DA = 0;
  32.   
  33.   return;
  34. }


读page

  1. void ReadPage (unsigned long adr, unsigned long sz, unsigned char *buf)
  2. {
  3.   while (sz)
  4.   {
  5.     *(UINT32 *)buf = *(UINT32 *)adr;   
  6.     sz -= sizeof(UINT32);
  7.     adr += sizeof(UINT32);
  8.     buf += sizeof(UINT32);
  9.   }   
  10. }


测试步骤:
1.擦除
2.写入指定内容
3.读出内容,与写入的比对是否一致。

  1. void SPIM_test(void)
  2. {
  3.   u16 i=0;
  4.   
  5.   /* Configures the ext.flash */
  6.   printf("init SPIM.\r\n");   
  7.   FLASH_InitExtFlash();
  8.   
  9.   /* Fill the content to be writed to ext.flash */
  10.   for(i=0;i<SPIM_PAGE_SIZE;i++)
  11.   {
  12.     WriteBuffer[i]=i%256;
  13.   }
  14.   
  15.   /* Erases an ext.flash page */
  16.   printf("erase one page.\r\n");
  17.   FLASH_ErasePage(SPIM_TEST_ADDR);
  18.   
  19.   /* Read an ext.flash page */
  20.   memset(ReadBuffer,0,SPIM_PAGE_SIZE);
  21.   ReadPage(SPIM_TEST_ADDR, SPIM_PAGE_SIZE, ReadBuffer);
  22.   
  23.   /* Check if the desired page are erased */
  24.   for(i=0;i<SPIM_PAGE_SIZE;i++)
  25.   {
  26.     if(ReadBuffer[i]!=0xff)
  27.     {
  28.       printf("operate SPIM fail.\r\n");
  29.       return;
  30.     }   
  31.   }
  32.   
  33.   /* Program an ext.flash page */
  34.   printf("write one page.\r\n");
  35.   i=0;
  36.   while(i<SPIM_PAGE_SIZE)
  37.   {
  38.     FLASH_ProgramWord (SPIM_TEST_ADDR+i,*(u32 *)(WriteBuffer+i));
  39.     i=i+4;  
  40.   }
  41.   
  42.   /* Read an ext.flash page */
  43.   printf("read one page.\r\n");
  44.   memset(ReadBuffer,0,SPIM_PAGE_SIZE);
  45.   ReadPage(SPIM_TEST_ADDR, SPIM_PAGE_SIZE, ReadBuffer);
  46.   
  47.   /* Check if reading result and writing content are the same */
  48.   printf("compare the WriteBuffer/ReadBuffer.\r\n");
  49.   for(i=0;i<SPIM_PAGE_SIZE;i++)
  50.   {
  51.     if(ReadBuffer[i]!=WriteBuffer[i])
  52.     {
  53.       /* Print the ext.flash testing result */
  54.       printf("test SPIM fail.\r\n");
  55.       return;
  56.     }  
  57.     printf("ReadBuffer[%d] = %d\t", i,ReadBuffer[i]);
  58.   }

  59.   /* Print the ext.flash testing result */
  60.   printf("test SPIM success!\r\n");
  61. }


串口打印:

3.jpg

读出的内容与写入的一致。
Tanxjxj120a 发表于 2021-2-17 14:32 | 显示全部楼层
谢谢分享,很详细
您需要登录后才可以回帖 登录 | 注册

本版积分规则

70

主题

279

帖子

2

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