[活动专区] 【开源活动】-基于国民N32G45x的SD卡IAP升级开发

[复制链接]
9098|0
 楼主| 6552918 发表于 2023-4-2 23:12 | 显示全部楼层 |阅读模式
本帖最后由 6552918 于 2023-4-3 09:35 编辑

#申请原创# #技术资源#@安小芯
  
测试条件
  
IDE
硬件
编译器
软件包
MDK 5.38
N32G457XVL-STB V1.1
  SD卡读写模块-SPI接口
  
SD卡、TF卡.MMC卡
ARM Compiler 6(AC6)
SD_SPI
  
FATFS
  
FLASH_IAP

1.   硬件部分说明1.1 硬件说明
使用N32G457XVL-STB V1.1开发板,因为板子上并没有集成SD卡槽,所有需要扩展一个SD卡槽,SD卡支持两种驱动模式,一种是SPI驱动,另一种是SDIO驱动,因为我手上只有SPI接口的SD卡模块,所以本例程使用的是SPI驱动SD卡进行的演示。
1.jpg
SD卡读写模块-SPI接口 模块
2.jpg
SD卡读写模块-SPI接口 模块 原理图
3.png
为了做兼容性测试 我准备了3种卡有SD卡 TF卡 MMC卡
4.jpg
1.2 FLASH说明
N32G457的FLASH有256个页,每个页大小为2KB,需要根据使用情况对FLASH进行划分。
5.png
本例程FLASH划分:
  
  
起始地址
结束地址
大小
页数
Bootloader区
0x8000000
0x80037FF
14KB
7
升级标志去
0x8003800
0x8003FFF
2KB
1
Application区
0x8004000
0x8026000
486KB
248
6.png
7.png
2.   软件部分说明
首先要准备一个编译无错误并运行正常的工程文件(建议使用厂家例程库里的工程,我这里使用的是uart的printf工程)
8.png
2.1   软件包移植
SD_SPI驱动移植
在工程内添加SD_SPI的驱动文件
9.png
添加.c文件
10.png
添加.h头文件路径
11.png
sdspi_port.c文件是移植的接口文件,里面需要根据实际使用情况实现接口函数,包含SPI初始化、SPI时钟速率变更及SPI数据收发函数,我这里使用的是SPI1
12.png
13.png
相关接口函数如下:
14.png
15.png
16.png
  1. /*
  2. * Copyright 2022 MindMotion Microelectronics Co., Ltd.
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <stdio.h>
  8. #include <stdint.h>
  9. #include "sdspi.h"
  10. #include "n32g45x_spi.h"

  11. /* pins:
  12. * tx : PA7/SPI_MOSI
  13. * rx : PA6/SPI0_MISO
  14. * clk: PA5/SPI1_SCK
  15. * cs : PA4/SPI0_PCS0
  16. */

  17. #define BOARD_SDSPI_TX_GPIO_PORT  GPIOA
  18. #define BOARD_SDSPI_TX_GPIO_PIN   GPIO_PIN_7

  19. #define BOARD_SDSPI_RX_GPIO_PORT  GPIOA
  20. #define BOARD_SDSPI_RX_GPIO_PIN   GPIO_PIN_6

  21. #define BOARD_SDSPI_CLK_GPIO_PORT GPIOA
  22. #define BOARD_SDSPI_CLK_GPIO_PIN  GPIO_PIN_5

  23. #define BOARD_SDSPI_CS_GPIO_PORT  GPIOA
  24. #define BOARD_SDSPI_CS_GPIO_PIN   GPIO_PIN_4

  25. SDSPI_ApiRetStatus_Type sdspi_spi_init(void);
  26. SDSPI_ApiRetStatus_Type sdspi_spi_freq(uint32_t hz);
  27. SDSPI_ApiRetStatus_Type sdspi_spi_xfer(uint8_t *in, uint8_t *out, uint32_t len);

  28. const SDSPI_Interface_Type board_sdspi_if =
  29. {
  30.     .baudrate = 1000000u, /* 1mhz. */
  31.     .spi_init = sdspi_spi_init,
  32.     .spi_freq = sdspi_spi_freq,
  33.     .spi_xfer = sdspi_spi_xfer
  34. };

  35. uint32_t board_sdspi_delay_count;

  36. static void board_sdspi_delay(uint32_t count)
  37. {
  38.     for (uint32_t i = count; i > 0u; i--)
  39.     {
  40.         __NOP();
  41.     }
  42. }

  43. SDSPI_ApiRetStatus_Type sdspi_spi_init(void)
  44. {
  45.                 SPI_InitType SPI_InitStructure;
  46.         
  47.                 GPIO_SetBits(BOARD_SDSPI_CS_GPIO_PORT, BOARD_SDSPI_CS_GPIO_PIN);
  48.         
  49.                 /*!< SPI configuration */
  50.     SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
  51.     SPI_InitStructure.SpiMode       = SPI_MODE_MASTER;
  52.     SPI_InitStructure.DataLen       = SPI_DATA_SIZE_8BITS;
  53.     SPI_InitStructure.CLKPOL        = SPI_CLKPOL_HIGH;
  54.     SPI_InitStructure.CLKPHA        = SPI_CLKPHA_SECOND_EDGE;
  55.     SPI_InitStructure.NSS           = SPI_NSS_SOFT;

  56.     SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_128;                //        562.5K SDMMC_CLOCK_400KHZ

  57.     SPI_InitStructure.FirstBit = SPI_FB_MSB;
  58.     SPI_InitStructure.CRCPoly  = 7;
  59.     SPI_Init(SPI1, &SPI_InitStructure);

  60.     /*!< Enable the SPI1  */
  61.     SPI_Enable(SPI1, ENABLE);

  62.     return SDSPI_ApiRetStatus_Success;
  63. }

  64. SDSPI_ApiRetStatus_Type sdspi_spi_freq(uint32_t hz)
  65. {
  66.                 SPI_InitType SPI_InitStructure;
  67.         
  68.                 GPIO_SetBits(BOARD_SDSPI_CS_GPIO_PORT, BOARD_SDSPI_CS_GPIO_PIN);
  69.         
  70.                 SPI_Enable(SPI1, DISABLE);
  71.         
  72.                 /*!< SPI configuration */
  73.     SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
  74.     SPI_InitStructure.SpiMode       = SPI_MODE_MASTER;
  75.     SPI_InitStructure.DataLen       = SPI_DATA_SIZE_8BITS;
  76.     SPI_InitStructure.CLKPOL        = SPI_CLKPOL_HIGH;
  77.     SPI_InitStructure.CLKPHA        = SPI_CLKPHA_SECOND_EDGE;
  78.     SPI_InitStructure.NSS           = SPI_NSS_SOFT;

  79.     SPI_InitStructure.FirstBit = SPI_FB_MSB;
  80.     SPI_InitStructure.CRCPoly  = 7;
  81.    
  82.                
  83.     switch (hz)
  84.     {
  85.     case SDMMC_CLOCK_400KHZ:
  86.                                 SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_128;                //        562.5K SDMMC_CLOCK_400KHZ
  87.         break;
  88.     default:
  89.                                 SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_4;                //        18M SD_CLOCK_25MHZ
  90.         break;
  91.     }
  92.                
  93.                 SPI_Init(SPI1, &SPI_InitStructure);
  94.                 /*!< Enable the sFLASH_SPI  */
  95.     SPI_Enable(SPI1, ENABLE);
  96.                
  97.     return SDSPI_ApiRetStatus_Success;
  98. }

  99. /* SPI tx. */
  100. void app_spi_putbyte(uint8_t c)
  101. {
  102.     /*!< Loop while DAT register in not emplty */
  103.     while (SPI_I2S_GetStatus(SPI1, SPI_I2S_TE_FLAG) == RESET);

  104.     /*!< Send byte through the SPI1 peripheral */
  105.     SPI_I2S_TransmitData(SPI1, c);
  106. }

  107. /* SPI rx. */
  108. uint8_t app_spi_getbyte(void)
  109. {
  110.     /*!< Wait to receive a byte */
  111.     while (SPI_I2S_GetStatus(SPI1, SPI_I2S_RNE_FLAG) == RESET);

  112.     /*!< Return the byte read from the SPI bus */
  113.     return SPI_I2S_ReceiveData(SPI1);
  114. }

  115. uint8_t spi_xfer(uint8_t tx_dat)
  116. {        
  117.                 /*!< Loop while DAT register in not emplty */
  118.     while (SPI_I2S_GetStatus(SPI1, SPI_I2S_TE_FLAG) == RESET)
  119.         ;

  120.     /*!< Send byte through the SPI1 peripheral */
  121.     SPI_I2S_TransmitData(SPI1, tx_dat);

  122.     /*!< Wait to receive a byte */
  123.     while (SPI_I2S_GetStatus(SPI1, SPI_I2S_RNE_FLAG) == RESET)
  124.         ;

  125.     /*!< Return the byte read from the SPI bus */
  126.     return SPI_I2S_ReceiveData(SPI1);
  127. }

  128. SDSPI_ApiRetStatus_Type sdspi_spi_xfer(uint8_t *in, uint8_t *out, uint32_t len)
  129. {
  130.     uint8_t inbuf, outbuf;

  131.                 GPIO_ResetBits(BOARD_SDSPI_CS_GPIO_PORT, BOARD_SDSPI_CS_GPIO_PIN);

  132.     for (uint32_t i = 0u; i < len; i++)
  133.     {
  134.         inbuf = (in == NULL) ? SDSPI_DUMMY_DATA: *in++;
  135.         outbuf = spi_xfer(inbuf);
  136.         if (out)
  137.         {
  138.             *out = outbuf;
  139.             out++;
  140.         }
  141.     }

  142.                 GPIO_SetBits(BOARD_SDSPI_CS_GPIO_PORT, BOARD_SDSPI_CS_GPIO_PIN);

  143.     return SDSPI_ApiRetStatus_Success;
  144. }

  145. /* EOF. */


FATFS移植
因为SD卡都是需要文件系统支持才能进行读写操作,因此,需要移植FATFS文件系统
fatfs 的官方网站: http://elm-chan.org/fsw/ff/00index_e.html
我这里使用的是最新的R0.15 (November 6, 2022)版本
17.png
在工程内添加FATFS的驱动文件
18.png
添加.c文件
19.png
添加.h文件路径
20.png
diskio.c文件是移植的接口文件,里面需要根据实际使用情况实现接口函数
disk_status、disk_initialize、disk_read、disk_write、disk_ioctl等函数
这些函数的具体操作需要调用SD_SPI中的先关API
21.png
  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module SKELETON for FatFs     (C)ChaN, 2019        */
  3. /*-----------------------------------------------------------------------*/
  4. /* If a working storage control module is available, it should be        */
  5. /* attached to the FatFs via a glue function rather than modifying it.   */
  6. /* This is an example of glue functions to attach various exsisting      */
  7. /* storage control modules to the FatFs module with a defined API.       */
  8. /*-----------------------------------------------------------------------*/

  9. #include "ff.h"                        /* Obtains integer types */
  10. #include "diskio.h"                /* Declarations of disk functions */
  11. #include "sdspi.h"

  12. /* Definitions of physical drive number for each drive */
  13. #define DEV_RAM                0        /* Example: Map Ramdisk to physical drive 0 */
  14. #define DEV_MMC                1        /* Example: Map MMC/SD card to physical drive 1 */
  15. #define DEV_USB                2        /* Example: Map USB MSD to physical drive 2 */

  16. SDSPI_ApiRetStatus_Type app_sdspi_ret;
  17. SDSPI_CardHandler_Type app_sdspi_card;
  18. extern const SDSPI_Interface_Type board_sdspi_if;

  19. /*-----------------------------------------------------------------------*/
  20. /* Get Drive Status                                                      */
  21. /*-----------------------------------------------------------------------*/

  22. DSTATUS disk_status (
  23.         BYTE pdrv                /* Physical drive nmuber to identify the drive */
  24. )
  25. {
  26.         DSTATUS stat;
  27.         int result;

  28.         switch (pdrv) {
  29.         case DEV_RAM :
  30.                 //result = RAM_disk_status();

  31.                 // translate the reslut code here

  32.                 return stat;

  33.         case DEV_MMC :
  34.                 //result = MMC_disk_status();
  35.                 stat = RES_OK;
  36.                 // translate the reslut code here

  37.                 return stat;

  38.         case DEV_USB :
  39.                 //result = USB_disk_status();

  40.                 // translate the reslut code here

  41.                 return stat;
  42.         }
  43.         
  44.         return STA_NOINIT;
  45. }



  46. /*-----------------------------------------------------------------------*/
  47. /* Inidialize a Drive                                                    */
  48. /*-----------------------------------------------------------------------*/

  49. DSTATUS disk_initialize (
  50.         BYTE pdrv                                /* Physical drive nmuber to identify the drive */
  51. )
  52. {
  53.         DSTATUS stat;
  54.         //int result;

  55.         switch (pdrv) {
  56.         case DEV_RAM :
  57.                 //result = RAM_disk_initialize();

  58.                 // translate the reslut code here

  59.                 return stat;

  60.         case DEV_MMC :
  61.                 //result = MMC_disk_initialize();

  62.                 if(!SDSPI_Init(&app_sdspi_card, &board_sdspi_if)){
  63.                         stat = RES_OK;
  64.                 }else{
  65.                         stat = STA_NOINIT;
  66.                 }
  67.                 // translate the reslut code here

  68.                 return stat;

  69.         case DEV_USB :
  70.                 //result = USB_disk_initialize();

  71.                 // translate the reslut code here

  72.                 return stat;
  73.         }
  74.         return STA_NOINIT;
  75. }



  76. /*-----------------------------------------------------------------------*/
  77. /* Read Sector(s)                                                        */
  78. /*-----------------------------------------------------------------------*/

  79. DRESULT disk_read (
  80.         BYTE pdrv,                /* Physical drive nmuber to identify the drive */
  81.         BYTE *buff,                /* Data buffer to store read data */
  82.         LBA_t sector,        /* Start sector in LBA */
  83.         UINT count                /* Number of sectors to read */
  84. )
  85. {
  86.         DRESULT res;
  87.         //int result;
  88.         uint8_t i;

  89.         switch (pdrv) {
  90.         case DEV_RAM :
  91.                 // translate the arguments here

  92.                 //result = RAM_disk_read(buff, sector, count);

  93.                 // translate the reslut code here

  94.                 return res;

  95.         case DEV_MMC :
  96.                 // translate the arguments here
  97.                         if(!SDSPI_ReadBlocks(&app_sdspi_card,buff, sector, count))
  98.                         {
  99.                                 res = RES_OK;
  100.                         }else{
  101.                                 res = RES_ERROR;
  102.                         }
  103.                 //result = MMC_disk_read(buff, sector, count);
  104.                

  105.                 // translate the reslut code here

  106.                 return res;

  107.         case DEV_USB :
  108.                 // translate the arguments here

  109.                 //result = USB_disk_read(buff, sector, count);

  110.                 // translate the reslut code here

  111.                 return res;
  112.         }

  113.         return RES_PARERR;
  114. }



  115. /*-----------------------------------------------------------------------*/
  116. /* Write Sector(s)                                                       */
  117. /*-----------------------------------------------------------------------*/

  118. #if FF_FS_READONLY == 0

  119. DRESULT disk_write (
  120.         BYTE pdrv,                        /* Physical drive nmuber to identify the drive */
  121.         const BYTE *buff,        /* Data to be written */
  122.         LBA_t sector,                /* Start sector in LBA */
  123.         UINT count                        /* Number of sectors to write */
  124. )
  125. {
  126.         DRESULT res;
  127.         //int result;

  128.         switch (pdrv) {
  129.         case DEV_RAM :
  130.                 // translate the arguments here

  131.                 //result = RAM_disk_write(buff, sector, count);

  132.                 // translate the reslut code here

  133.                 return res;

  134.         case DEV_MMC :
  135.                 // translate the arguments here

  136.                 //result = MMC_disk_write(buff, sector, count);
  137.                 if(!SDSPI_WriteBlocks(&app_sdspi_card,(uint8_t *)buff, sector, count))
  138.                 {
  139.                         res = RES_OK;
  140.                 }else{
  141.                         res = RES_ERROR;
  142.                 }

  143.                 // translate the reslut code here

  144.                 return res;

  145.         case DEV_USB :
  146.                 // translate the arguments here

  147.                 //result = USB_disk_write(buff, sector, count);

  148.                 // translate the reslut code here

  149.                 return res;
  150.         }

  151.         return RES_PARERR;
  152. }

  153. #endif


  154. /*-----------------------------------------------------------------------*/
  155. /* Miscellaneous Functions                                               */
  156. /*-----------------------------------------------------------------------*/

  157. DRESULT disk_ioctl (
  158.         BYTE pdrv,                /* Physical drive nmuber (0..) */
  159.         BYTE cmd,                /* Control code */
  160.         void *buff                /* Buffer to send/receive control data */
  161. )
  162. {
  163.         DRESULT res;
  164.         int result;

  165.         switch (pdrv) {
  166.         case DEV_RAM :

  167.                 // Process of the command for the RAM drive

  168.                 return res;

  169.         case DEV_MMC :

  170.                 // Process of the command for the MMC/SD card
  171.                 switch(cmd)
  172.                 {
  173.                         case GET_SECTOR_COUNT:
  174.          *(DWORD *)buff = app_sdspi_card.blockCount;
  175.          res = RES_OK;
  176.          break;

  177.       case GET_BLOCK_SIZE:
  178.          *(DWORD *)buff = SDSPI_DEFAULT_BLOCK_SIZE;
  179.          res = RES_OK;
  180.          break;
  181.                 }

  182.                 return res;

  183.         case DEV_USB :

  184.                 // Process of the command the USB drive

  185.                 return res;
  186.         }

  187.         return RES_PARERR;
  188. }


FATFS定义了RAM MMC(SD/TF) USB设备的盘符,这些盘符在操作API时会用到
22.png
  1. /* Definitions of physical drive number for each drive */
  2. #define DEV_RAM                0        /* Example: Map Ramdisk to physical drive 0 */
  3. #define DEV_MMC                1        /* Example: Map MMC/SD card to physical drive 1 */
  4. #define DEV_USB                2        /* Example: Map USB MSD to physical drive 2 */

ffconf.h内是FATFS的配置文件,相关配置项需要根据自己的实际情况进行配置
23.png
  1. /*---------------------------------------------------------------------------/
  2. /  Configurations of FatFs Module
  3. /---------------------------------------------------------------------------*/

  4. #define FFCONF_DEF        80286        /* Revision ID */

  5. /*---------------------------------------------------------------------------/
  6. / Function Configurations
  7. /---------------------------------------------------------------------------*/

  8. #define FF_FS_READONLY        0
  9. /* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
  10. /  Read-only configuration removes writing API functions, f_write(), f_sync(),
  11. /  f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
  12. /  and optional writing functions as well. */


  13. #define FF_FS_MINIMIZE        0
  14. /* This option defines minimization level to remove some basic API functions.
  15. /
  16. /   0: Basic functions are fully enabled.
  17. /   1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
  18. /      are removed.
  19. /   2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
  20. /   3: f_lseek() function is removed in addition to 2. */


  21. #define FF_USE_FIND                0
  22. /* This option switches filtered directory read functions, f_findfirst() and
  23. /  f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */


  24. #define FF_USE_MKFS                1
  25. /* This option switches f_mkfs() function. (0:Disable or 1:Enable) */


  26. #define FF_USE_FASTSEEK        0
  27. /* This option switches fast seek function. (0:Disable or 1:Enable) */


  28. #define FF_USE_EXPAND        0
  29. /* This option switches f_expand function. (0:Disable or 1:Enable) */


  30. #define FF_USE_CHMOD        0
  31. /* This option switches attribute manipulation functions, f_chmod() and f_utime().
  32. /  (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */


  33. #define FF_USE_LABEL        0
  34. /* This option switches volume label functions, f_getlabel() and f_setlabel().
  35. /  (0:Disable or 1:Enable) */


  36. #define FF_USE_FORWARD        0
  37. /* This option switches f_forward() function. (0:Disable or 1:Enable) */


  38. #define FF_USE_STRFUNC        1
  39. #define FF_PRINT_LLI                0
  40. #define FF_PRINT_FLOAT        0
  41. #define FF_STRF_ENCODE        0
  42. /* FF_USE_STRFUNC switches string functions, f_gets(), f_putc(), f_puts() and
  43. /  f_printf().
  44. /
  45. /   0: Disable. FF_PRINT_LLI, FF_PRINT_FLOAT and FF_STRF_ENCODE have no effect.
  46. /   1: Enable without LF-CRLF conversion.
  47. /   2: Enable with LF-CRLF conversion.
  48. /
  49. /  FF_PRINT_LLI = 1 makes f_printf() support long long argument and FF_PRINT_FLOAT = 1/2
  50. /  makes f_printf() support floating point argument. These features want C99 or later.
  51. /  When FF_LFN_UNICODE >= 1 with LFN enabled, string functions convert the character
  52. /  encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE
  53. /  to be read/written via those functions.
  54. /
  55. /   0: ANSI/OEM in current CP
  56. /   1: Unicode in UTF-16LE
  57. /   2: Unicode in UTF-16BE
  58. /   3: Unicode in UTF-8
  59. */


  60. /*---------------------------------------------------------------------------/
  61. / Locale and Namespace Configurations
  62. /---------------------------------------------------------------------------*/

  63. #define FF_CODE_PAGE        932
  64. /* This option specifies the OEM code page to be used on the target system.
  65. /  Incorrect code page setting can cause a file open failure.
  66. /
  67. /   437 - U.S.
  68. /   720 - Arabic
  69. /   737 - Greek
  70. /   771 - KBL
  71. /   775 - Baltic
  72. /   850 - Latin 1
  73. /   852 - Latin 2
  74. /   855 - Cyrillic
  75. /   857 - Turkish
  76. /   860 - Portuguese
  77. /   861 - Icelandic
  78. /   862 - Hebrew
  79. /   863 - Canadian French
  80. /   864 - Arabic
  81. /   865 - Nordic
  82. /   866 - Russian
  83. /   869 - Greek 2
  84. /   932 - Japanese (DBCS)
  85. /   936 - Simplified Chinese (DBCS)
  86. /   949 - Korean (DBCS)
  87. /   950 - Traditional Chinese (DBCS)
  88. /     0 - Include all code pages above and configured by f_setcp()
  89. */


  90. #define FF_USE_LFN                0
  91. #define FF_MAX_LFN                255
  92. /* The FF_USE_LFN switches the support for LFN (long file name).
  93. /
  94. /   0: Disable LFN. FF_MAX_LFN has no effect.
  95. /   1: Enable LFN with static  working buffer on the BSS. Always NOT thread-safe.
  96. /   2: Enable LFN with dynamic working buffer on the STACK.
  97. /   3: Enable LFN with dynamic working buffer on the HEAP.
  98. /
  99. /  To enable the LFN, ffunicode.c needs to be added to the project. The LFN function
  100. /  requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
  101. /  additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.
  102. /  The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can
  103. /  be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN
  104. /  specification.
  105. /  When use stack for the working buffer, take care on stack overflow. When use heap
  106. /  memory for the working buffer, memory management functions, ff_memalloc() and
  107. /  ff_memfree() exemplified in ffsystem.c, need to be added to the project. */


  108. #define FF_LFN_UNICODE        0
  109. /* This option switches the character encoding on the API when LFN is enabled.
  110. /
  111. /   0: ANSI/OEM in current CP (TCHAR = char)
  112. /   1: Unicode in UTF-16 (TCHAR = WCHAR)
  113. /   2: Unicode in UTF-8 (TCHAR = char)
  114. /   3: Unicode in UTF-32 (TCHAR = DWORD)
  115. /
  116. /  Also behavior of string I/O functions will be affected by this option.
  117. /  When LFN is not enabled, this option has no effect. */


  118. #define FF_LFN_BUF                255
  119. #define FF_SFN_BUF                12
  120. /* This set of options defines size of file name members in the FILINFO structure
  121. /  which is used to read out directory items. These values should be suffcient for
  122. /  the file names to read. The maximum possible length of the read file name depends
  123. /  on character encoding. When LFN is not enabled, these options have no effect. */


  124. #define FF_FS_RPATH                0
  125. /* This option configures support for relative path.
  126. /
  127. /   0: Disable relative path and remove related functions.
  128. /   1: Enable relative path. f_chdir() and f_chdrive() are available.
  129. /   2: f_getcwd() function is available in addition to 1.
  130. */


  131. /*---------------------------------------------------------------------------/
  132. / Drive/Volume Configurations
  133. /---------------------------------------------------------------------------*/

  134. #define FF_VOLUMES                2
  135. /* Number of volumes (logical drives) to be used. (1-10) */


  136. #define FF_STR_VOLUME_ID        0
  137. #define FF_VOLUME_STRS                "RAM","NAND","CF","SD","SD2","USB","USB2","USB3"
  138. /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
  139. /  When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
  140. /  number in the path name. FF_VOLUME_STRS defines the volume ID strings for each
  141. /  logical drives. Number of items must not be less than FF_VOLUMES. Valid
  142. /  characters for the volume ID strings are A-Z, a-z and 0-9, however, they are
  143. /  compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is
  144. /  not defined, a user defined volume string table is needed as:
  145. /
  146. /  const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",...
  147. */


  148. #define FF_MULTI_PARTITION        0
  149. /* This option switches support for multiple volumes on the physical drive.
  150. /  By default (0), each logical drive number is bound to the same physical drive
  151. /  number and only an FAT volume found on the physical drive will be mounted.
  152. /  When this function is enabled (1), each logical drive number can be bound to
  153. /  arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
  154. /  function will be available. */


  155. #define FF_MIN_SS                512
  156. #define FF_MAX_SS                512
  157. /* This set of options configures the range of sector size to be supported. (512,
  158. /  1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and
  159. /  harddisk, but a larger value may be required for on-board flash memory and some
  160. /  type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured
  161. /  for variable sector size mode and disk_ioctl() function needs to implement
  162. /  GET_SECTOR_SIZE command. */


  163. #define FF_LBA64                0
  164. /* This option switches support for 64-bit LBA. (0:Disable or 1:Enable)
  165. /  To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */


  166. #define FF_MIN_GPT                0x10000000
  167. /* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and
  168. /  f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */


  169. #define FF_USE_TRIM                0
  170. /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)
  171. /  To enable Trim function, also CTRL_TRIM command should be implemented to the
  172. /  disk_ioctl() function. */



  173. /*---------------------------------------------------------------------------/
  174. / System Configurations
  175. /---------------------------------------------------------------------------*/

  176. #define FF_FS_TINY                0
  177. /* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
  178. /  At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes.
  179. /  Instead of private sector buffer eliminated from the file object, common sector
  180. /  buffer in the filesystem object (FATFS) is used for the file data transfer. */


  181. #define FF_FS_EXFAT                0
  182. /* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
  183. /  To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1)
  184. /  Note that enabling exFAT discards ANSI C (C89) compatibility. */


  185. #define FF_FS_NORTC                1
  186. #define FF_NORTC_MON        1
  187. #define FF_NORTC_MDAY        1
  188. #define FF_NORTC_YEAR        2022
  189. /* The option FF_FS_NORTC switches timestamp feature. If the system does not have
  190. /  an RTC or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable the
  191. /  timestamp feature. Every object modified by FatFs will have a fixed timestamp
  192. /  defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time.
  193. /  To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be
  194. /  added to the project to read current time form real-time clock. FF_NORTC_MON,
  195. /  FF_NORTC_MDAY and FF_NORTC_YEAR have no effect.
  196. /  These options have no effect in read-only configuration (FF_FS_READONLY = 1). */


  197. #define FF_FS_NOFSINFO        0
  198. /* If you need to know correct free space on the FAT32 volume, set bit 0 of this
  199. /  option, and f_getfree() function at the first time after volume mount will force
  200. /  a full FAT scan. Bit 1 controls the use of last allocated cluster number.
  201. /
  202. /  bit0=0: Use free cluster count in the FSINFO if available.
  203. /  bit0=1: Do not trust free cluster count in the FSINFO.
  204. /  bit1=0: Use last allocated cluster number in the FSINFO if available.
  205. /  bit1=1: Do not trust last allocated cluster number in the FSINFO.
  206. */


  207. #define FF_FS_LOCK                0
  208. /* The option FF_FS_LOCK switches file lock function to control duplicated file open
  209. /  and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
  210. /  is 1.
  211. /
  212. /  0:  Disable file lock function. To avoid volume corruption, application program
  213. /      should avoid illegal open, remove and rename to the open objects.
  214. /  >0: Enable file lock function. The value defines how many files/sub-directories
  215. /      can be opened simultaneously under file lock control. Note that the file
  216. /      lock control is independent of re-entrancy. */


  217. #define FF_FS_REENTRANT        0
  218. #define FF_FS_TIMEOUT        1000
  219. /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
  220. /  module itself. Note that regardless of this option, file access to different
  221. /  volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
  222. /  and f_fdisk() function, are always not re-entrant. Only file/directory access
  223. /  to the same volume is under control of this featuer.
  224. /
  225. /   0: Disable re-entrancy. FF_FS_TIMEOUT have no effect.
  226. /   1: Enable re-entrancy. Also user provided synchronization handlers,
  227. /      ff_mutex_create(), ff_mutex_delete(), ff_mutex_take() and ff_mutex_give()
  228. /      function, must be added to the project. Samples are available in ffsystem.c.
  229. /
  230. /  The FF_FS_TIMEOUT defines timeout period in unit of O/S time tick.
  231. */



  232. /*--- End of configuration options ---*/

片内FLASH_IAP移植
24.png
此部分直接使用原厂提供的IAP函数即可,将文件添加到工程
添加.c文件
25.png
添加.h文件路径
26.png
  1. #include <stdio.h>
  2. #include "iap.h"
  3. #include "string.h"

  4. iapfun jump2app;  
  5. uint8_t uart_receiveBIN_ok;

  6. uint8_t pages_number = 0;
  7. uint32_t ready_write_addr = 0;

  8. uint8_t flash_buf[2048];
  9. extern uint8_t receive_app_done;
  10. extern void IAP_UPDATE_APP(void);

  11. /**================================================================
  12.                 读取Flash
  13. ================================================================*/
  14. uint32_t FLASH_ReadWord(uint32_t address)
  15. {
  16.   return *(__IO uint32_t*)address;
  17. }
  18. /**================================================================
  19.                 APP 跳转
  20.                 appxaddr:用户代码起始地址.
  21. ================================================================*/
  22. void iap_load_app(u32 appxaddr)
  23. {
  24.         if(((*(vu32*)appxaddr)&0x0FFFFFFF) < 1024*512)                // 检查栈顶地址是否合法.
  25.         {
  26.                 jump2app = (iapfun)*(vu32*)(appxaddr+4);                                
  27.                 __set_MSP(*(vu32*)appxaddr);                                                // 初始化堆栈指针
  28.                 jump2app();                                                                                // 跳转到APP.
  29.         }
  30. }               
  31. /**================================================================
  32. ================================================================*/
  33. int32_t app_flag_write(uint32_t data ,uint32_t start_add)
  34. {
  35.         FLASH_Unlock();
  36.         //
  37.         FLASH_EraseOnePage(start_add);                        //写之前先擦一遍,每次擦2K
  38.         if (FLASH_COMPL != FLASH_ProgramWord(start_add, data))                //写
  39.         {
  40.                 FLASH_Lock();
  41.                 //printf("flash write fail! \r\n");
  42.                 return 1;
  43.         }
  44.         FLASH_Lock();
  45.     return 0;
  46. }
  47. /**================================================================
  48. ================================================================*/
  49. #define FLASH_PAGE_SIZE                 2048                                 

  50. /**
  51. * [url=home.php?mod=space&uid=247401]@brief[/url]
  52. * @param void
  53. * @return
  54. * - `SUCCESS: 表示操作成功
  55. * - 其它值表示出错
  56. */
  57. int32_t app_flash_write(uint32_t *data ,uint32_t Flash_address)
  58. {
  59.     uint32_t i;
  60.         uint32_t start_add;
  61.         start_add = Flash_address;
  62.                
  63.         FLASH_Unlock();
  64.         //
  65.         for(i = 0;i<FLASH_PAGE_SIZE/FLASH_PAGE_SIZE;i++)
  66.         {
  67.                 FLASH_EraseOnePage(start_add+i*FLASH_PAGE_SIZE);                        //写之前先擦一遍,每次擦2K
  68.         }
  69.         //
  70.         for(i=0;i<FLASH_PAGE_SIZE/4 ;i++)
  71.         {
  72.                 if (FLASH_COMPL != FLASH_ProgramWord(start_add+i*4, data[i]))                //写
  73.                 {
  74.                         FLASH_Lock();
  75.                         //printf("flash write fail! \r\n");
  76. //                        receive_app_done = 0;
  77.                         return 1;
  78.                 }
  79.         }
  80.         FLASH_Lock();
  81.     return 0;
  82. }
  83. /**================================================================
  84.                 //升级APP
  85. ================================================================*/
  86. void IAP_UPDATE_APP(void)
  87. {
  88.         ready_write_addr = FLASH_APP_BASE_ADDR + pages_number*2048;
  89.         //
  90.         while(app_flash_write((uint32_t *)flash_buf , ready_write_addr));                //IAP每次升级2K
  91.         //
  92.         memset(flash_buf,0x00,2048);
  93.         pages_number++;

  94. }

至此,软件包的移植工作就都完成了,接下来实现具体的逻辑操作。
2.2   升级流程说明
本实例仅实现基础功能,在实际使用过程中需要根据实际情况进行修改
整体流程如下:
27.png
整个方案流程如下
1     MCU启动
2      bootloader判断升级标志状态,标志不为0x12345678,进入步骤3,标志为0x12345678,进行跳转至app,进入步骤7
3      bootloader初始化外设
4      bootloader初始化文件系统
5      bootloader检查app升级文件是否存在,升级文件存在,输出提示信息,并等待升级命令。未收到升级命令则正常运行。
6      bootloader收到升级命令,读取SD卡内升级文件写入app区,写入完成,跳转至APP
7      app判断升级标志状态,标志不为0x12345678,代表第一次进入app,需要将升级标志写为0x12345678。标志为0x12345678,代表从bootloader正常启动。
8      app初始化外设
9      app初始化文件系统
10    app检查app升级文件是否存在,升级文件存在,输出提示信息,并等待升级命令
11    app收到升级命令,需要先擦除升级标志,并进行系统复位,回到步骤1。未收到升级命令则正常运行。

我的这个SD_IAP实例因为BootLoader和application功能都十分类似,因此使用一个工程下的不同项目进行维护
28.png
在不同点使用宏定义进行编译选择。在app内添加SD_SPI_APP宏定义作为编译开关。
29.png
30.png
不同项目输出的bin文件路径也分别进行了设置,防止出现错误
30-1.png
30-2.png
30-3.png
这里重点说一个MDK的小技巧,在进入bootloader和application后都要进行中断向量的重新映射才能使程序正常运行,例如:
31.png
一般的做法都是在bootloader和application使用不同的宏定义区实现,比如
32.png
这种方式在修改时有时会遗忘,比较麻烦,MDK可采用下图的方式修改,一劳永逸。不用再为不同起始地址配置不同的宏定义,一切都由MDK根据FLASH地址的配置自动设置。
33.png
  1. void System_Init(void)
  2. {
  3.     /* 设置中断向量表后,开启总中断 */
  4.     extern int Image$ER_IROM1$Base;
  5.     __disable_irq();
  6.     SCB->VTOR = (uint32_t)&Image$ER_IROM1$Base;
  7.     __enable_irq();
  8. }

3.   升级兼容性测试
为了测试此升级程序的兼容性,我准备了SD卡(512MB)TF卡(16GB)MMC卡(1GB)分别使用不同的文件格式进行了测试
SD卡(512MB)FAT格式,升级正常
34.png
SD卡(512MB)FAT32格式,升级正常
35.png
SD卡(512MB)exFAT格式,升级失败
36.png
TF卡(16GB),FAT32格式,升级正常,不支持FAT,没测试
37.png
TF卡(16GB),exFAT格式,升级失败
MMC卡(1GB)任何格式升级都失败

失败原因应该是文件系统的配置有问题,理论上3中卡都可以支持,如需对各种格式的卡进行兼容需要仔细研究ffconf.h内的配置项,根据需求进行配置。

源码: Nationstech.N32G45x_Library.2.1.0(SD_IAP).zip (5.3 MB, 下载次数: 35)
视频:

  
  
您需要登录后才可以回帖 登录 | 注册

本版积分规则

认证:北京汇冠触摸技术有限公司/电子工程师
简介:电子工程师,嵌入式应用爱好者。

102

主题

1290

帖子

10

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