[技术问答] NV32F100x Flash模块编程 M32()函数原型怎样?

[复制链接]
1599|8
 楼主| qqqqqqqw 发表于 2017-7-25 14:10 | 显示全部楼层 |阅读模式
NV32F100x参考手册中并没有看到有关于flash地址的寄存器,M32()函数原型也没看到。
几时再见 发表于 2017-7-25 20:53 | 显示全部楼层
好像有一个专门的flash的文档的。
几时再见 发表于 2017-7-25 20:54 | 显示全部楼层
nv32f100x_flash_读写时序和参数说明.pdf (200.17 KB, 下载次数: 12)
几时再见 发表于 2017-7-25 20:55 | 显示全部楼层
  1. /******************************************************************************
  2. ******************************************************************************
  3. *
  4. * [url=home.php?mod=space&uid=288409]@file[/url] flash.c
  5. *
  6. * [url=home.php?mod=space&uid=247401]@brief[/url] application entry point which performs application specific tasks.
  7. *
  8. *******************************************************************************
  9. *
  10. * provide a demo for how to initialize the NV32, output messages via SCI,
  11. * flash operations, etc.
  12. * NOTE:
  13. *        printf call may occupy a lot of memory (around 1924 bytes), so please
  14. *        consider your code size before using printf.
  15. ******************************************************************************
  16. *
  17. * provide FLASH driver
  18. *
  19. ******************************************************************************/

  20. #include "flash.h"
  21. /******************************************************************************
  22. * Global variables
  23. ******************************************************************************/

  24. /******************************************************************************
  25. * Constants and macros
  26. ******************************************************************************/


  27. /******************************************************************************
  28. * Local types
  29. ******************************************************************************/

  30. /******************************************************************************
  31. * Local function prototypes
  32. ******************************************************************************/

  33. /******************************************************************************
  34. * Local variables
  35. ******************************************************************************/

  36. /******************************************************************************
  37. * Local functions
  38. ******************************************************************************/

  39. /******************************************************************************
  40. * Global functions
  41. ******************************************************************************/
  42. /*****************************************************************************//*!
  43. +FUNCTION----------------------------------------------------------------
  44. * [url=home.php?mod=space&uid=42490]@function[/url] name: Flash_CopyInRAM
  45. *
  46. * @brief This section of the code is the one that copies the routine into RAM.
  47. * It is following the steps  documented in Technical Note 228
  48. *        
  49. * @param  
  50. *
  51. * [url=home.php?mod=space&uid=266161]@return[/url] none
  52. *
  53. * [url=home.php?mod=space&uid=72445]@[/url] Pass/ Fail criteria: none
  54. *****************************************************************************/

  55. #define FLASH_ENABLE_STALLING_FLASH_CONTROLLER


  56. /*****************************************************************************//*!
  57. +FUNCTION----------------------------------------------------------------
  58. * @function name: Flash_Init
  59. *
  60. * @brief initialize flash driver
  61. *        
  62. * @param  
  63. *
  64. * @return none
  65. *
  66. * @ Pass/ Fail criteria: none
  67. *****************************************************************************/

  68. uint16_t Flash_Init(void)
  69. {
  70.         uint16_t err   = FLASH_ERR_SUCCESS;
  71.         uint32_t clkDIV = BUS_CLK_HZ/1000000L - 1;
  72.         uint32_t Tpgs  =(285 *(BUS_CLK_HZ/100))/1000000L;  //update 2016.8.4 by 光脚板のGG
  73.         uint32_t Tprog =(675*(BUS_CLK_HZ/100))/1000000L;   //by 光脚板のGG
  74. //        printf("Tpgs= %x \n" , Tpgs);
  75. //        printf("Tprog= %x \n" , Tprog);   
  76.         
  77.         EFMCR=(clkDIV<<24) + 0x00000001; //divide to 1M hz
  78.         EFMETM0=(Tpgs<<16)  + 0x00001194;  //0x00281194; //
  79.         EFMETM1=(Tprog<<16) + 0x000088B8; //
  80. //        printf("EFMCR= %x \n" , EFMCR);
  81. //        printf("EFMETM0= %x \n" , EFMETM0);         
  82. //        printf("EFMETM1= %x \n" , EFMETM1);
  83.   return(err);
  84. }

  85. /*****************************************************************************//*!
  86. +FUNCTION----------------------------------------------------------------
  87. * @function name: FlashProgram
  88. *
  89. * @brief program flash routine, each program operation supports up to 2 longwords
  90. *                  programming
  91. *        
  92. * @param  
  93. *
  94. * @return none
  95. *
  96. * @ Pass/ Fail criteria: none
  97. *****************************************************************************/

  98. uint16_t Flash_Program(uint32_t wNVMTargetAddress, uint8_t *pData, uint16_t sizeBytes)
  99. {
  100.         uint16_t err = FLASH_ERR_SUCCESS;
  101.         uint16_t w2LongWordCount = sizeBytes>>3;
  102.         uint8_t  wLeftBytes = (sizeBytes & 0x07);
  103.         uint16_t wLeftLongWords = wLeftBytes>>2;
  104.         uint32_t wTargetAddress = wNVMTargetAddress;
  105.         uint32_t dwData0,dwData1;
  106.         uint32_t *pdwData = (uint32_t*)pData;
  107.         int  i;
  108. //printf("\n adr : 0x%x ,data = 0x%x\n",w2LongWordCount,wLeftLongWords );
  109.         // Check address to see if it is aligned to 4 bytes
  110.         // Global address [1:0] must be 00.
  111.         if(wNVMTargetAddress & 0x03)
  112.         {
  113.                 err = FLASH_ERR_INVALID_PARAM;
  114.                 return (err);
  115.         }
  116.         // Loop for the two longwords (8 bytes) programming
  117.         for(i = 0; i < w2LongWordCount; i++)
  118.         {
  119.                 dwData0 = *pdwData++;
  120.                 dwData1 = *pdwData++;
  121.                 err = Flash_Program2LongWords(wTargetAddress, dwData0, dwData1);
  122.                 if(err)
  123.                 {
  124.                         goto EndP;
  125.                         //break;
  126.                 }
  127.                 wTargetAddress += 8;
  128.         }
  129.         // Loop for the single longword (4 bytes) programming
  130.         for(i = 0; i < wLeftLongWords; i++)
  131.         {
  132.                 dwData0 = *pdwData++;
  133.                 //printf("\n adr : 0x%x ,data = 0x%x\n",i,dwData0 );
  134.                 err = Flash_Program1LongWord(wTargetAddress, dwData0);
  135.     //printf("\n adr : 0x%x ,data = 0x%x\n",i,dwData0 );
  136.                 if(err)
  137.                 {                       
  138.                         goto EndP;
  139.                         //break;
  140.                 }
  141.                 wTargetAddress += 4;
  142.         }
  143.         wLeftBytes = (wLeftBytes-(wLeftLongWords<<2));        // calculate the # of bytes that are not programmed
  144.         if(!wLeftBytes){
  145.                 return (err);
  146.         }
  147.         
  148. #if     defined(BIG_ENDIAN)               
  149.         dwData0 = 0;
  150.         pData = (uint8_t*)pdwData;        // pointer to the left bytes
  151.         for(i = wLeftBytes; i >0; i--)
  152.         {
  153.                 dwData0 <<= 8;
  154.                 dwData0 |= *pData++;        // MSB byte first
  155.         }
  156.         // Calculate how many bytes need to be filled with 0xFFs
  157.         // in order to form a single longword for the left bytes of data
  158.         wLeftBytes = 4 - wLeftBytes;       
  159.         //  
  160.         for(i = wLeftBytes; i >0; i--)
  161.         {
  162.                 dwData0 <<= 8;
  163.                 dwData0 |= 0xFF;        // MSB byte first
  164.         }
  165. #else
  166.         dwData0 = 0xFFFFFFFFL;        
  167.         pData = (uint8_t*)pdwData+wLeftBytes-1;        // pointer to the left bytes
  168.         for(i = wLeftBytes; i >0; i--)
  169.         {
  170.                 dwData0 <<= 8;
  171.                 dwData0 |= *pData--;        // MSB byte first
  172.         }
  173. #endif       
  174.         // Now program the last longword
  175.         err = Flash_Program1LongWord(wTargetAddress, dwData0);       
  176. EndP:       
  177.         return (err);
  178. }

  179. uint16_t Flash_Program1LongWord(uint32_t wNVMTargetAddress, uint32_t dwData)
  180. {
  181.         uint16_t err = FLASH_ERR_SUCCESS;
  182.        
  183.         // Check address to see if it is aligned to 4 bytes
  184.         // Global address [1:0] must be 00.
  185.         if(wNVMTargetAddress & 0x03)
  186.         {
  187.                 err = FLASH_ERR_INVALID_PARAM;
  188.                 return (err);
  189.         }
  190.         // Clear error flags
  191.         EFMCMD = FLASH_CMD_CLEAR;
  192.         // Write index to specify the command code to be loaded
  193.         M32(wNVMTargetAddress) = dwData;
  194.         // Write command code and memory address bits[23:16]       
  195.         EFM_LaunchCMD(FLASH_CMD_PROGRAM);
  196.         return (err);
  197. }


  198. uint16_t Flash_Program2LongWords(uint32_t wNVMTargetAddress, uint32_t dwData0, uint32_t dwData1)
  199. {
  200.         uint16_t err = FLASH_ERR_SUCCESS;

  201.        
  202.         // Check address to see if it is aligned to 4 bytes
  203.         // Global address [1:0] must be 00.
  204.         if(wNVMTargetAddress & 0x03)
  205.         {
  206.                 err = FLASH_ERR_INVALID_PARAM;
  207.                 return (err);
  208.         }
  209.         // Clear error flags

  210.         EFMCMD = FLASH_CMD_CLEAR;

  211. //        printf("\n write data  adr : 0x%x ,data = 0x%x\n",dwData0,dwData1 );
  212.         // Write index to specify the command code to be loaded
  213.         M32(wNVMTargetAddress) = dwData0;
  214.         // Write command code and memory address bits[23:16]       
  215.   EFM_LaunchCMD(FLASH_CMD_PROGRAM);
  216.         wNVMTargetAddress = wNVMTargetAddress +4;
  217.        
  218.         //        printf("\n write data  adr : 0x%x ,data = 0x%x\n",wNVMTargetAddress,dwData1 );
  219.         // Clear error flags
  220.                 EFMCMD = FLASH_CMD_CLEAR;
  221.         // Write index to specify the command code to be loaded
  222.         M32(wNVMTargetAddress) = dwData1;
  223.         // Write command code and memory address bits[23:16]       
  224.   EFM_LaunchCMD(FLASH_CMD_PROGRAM);
  225. //        printf("\n write data  adr : 0x%x ,data = 0x%x\n",wNVMTargetAddress,dwData1 );
  226.         return (err);
  227. }





  228. /*****************************************************************************//*!
  229. +FUNCTION----------------------------------------------------------------
  230. * @function name: Flash_EraseSector
  231. *
  232. * @brief erase flash sector, each flash sector is of 512 bytes long,
  233. * global address [1:0] = 00.
  234. *        
  235. * @param  
  236. *
  237. * @return none
  238. *
  239. * @ Pass/ Fail criteria: none
  240. *****************************************************************************/
  241. uint16_t  Flash_EraseSector(uint32_t wNVMTargetAddress)
  242. {
  243.         uint16_t err = FLASH_ERR_SUCCESS;
  244.         // Check address to see if it is aligned to 4 bytes
  245.         // Global address [1:0] must be 00.
  246.         if(wNVMTargetAddress & 0x03)
  247.         {
  248.                 err = FLASH_ERR_INVALID_PARAM;
  249.                 return (err);
  250.         }
  251.         // Clear error flags
  252.                 EFMCMD = FLASH_CMD_CLEAR;
  253.           M32(wNVMTargetAddress) = 0xffffffff;
  254.           EFM_LaunchCMD(FLASH_CMD_ERASE_SECTOR);
  255.         return (err);
  256. }

  257. uint16_t Flash_VerifyBackdoorKey()
  258. {
  259.         uint16_t err = FLASH_ERR_SUCCESS;
  260. //        int i;
  261.         
  262.         // Clear error flags
  263.                 EFMCMD = FLASH_CMD_CLEAR;
  264.         // Write index to specify the command code to be loaded
  265.          Custombkd = FLASH_FACTORY_KEY;
  266.         return (err);
  267. }

  268. /*****************************************************************************//*!
  269. +FUNCTION----------------------------------------------------------------
  270. * @function name: NVM_EraseAll
  271. *
  272. * @brief erase all block,both flash and EEPROM
  273. *        
  274. * @param  
  275. *       
  276. * @return none
  277. *
  278. * @ Pass/ Fail criteria: none
  279. *****************************************************************************/
  280. uint16_t NVM_EraseAll(void)
  281. {
  282.         uint16_t err = FLASH_ERR_SUCCESS;
  283.                 EFMCMD = FLASH_CMD_CLEAR;
  284.           EFM_LaunchCMD(FLASH_CMD_ERASE_ALL);
  285.         // Clear error flags
  286.     return err;
  287. }

  288. /*****************************************************************************//*!
  289. +FUNCTION----------------------------------------------------------------
  290. * @function name: NVM_Unsecure
  291. *
  292. * @brief unsecure
  293. *        
  294. * @param  
  295. *       
  296. * @return none
  297. *
  298. * @ Pass/ Fail criteria: none
  299. *****************************************************************************/
  300. uint16_t NVM_Unsecure(void)
  301. {
  302.         uint16_t err = FLASH_ERR_SUCCESS;

  303.     return err;
  304. }

  305. #ifdef IAR
  306. void __ramfunc EFM_LaunchCMD(uint32_t EFM_CMD)
  307. #else
  308. void EFM_LaunchCMD(uint32_t EFM_CMD)
  309. #endif
  310. {
  311.   DisableInterrupts;
  312.                 if((EFMCMD&EFM_DONE_MASK)== EFM_STATUS_READY)
  313.                 {
  314.           EFMCMD = EFM_CMD;
  315.                 }
  316.         while(1)
  317.         {
  318.                 if((EFMCMD&EFM_DONE_MASK) == EFM_STATUS_DONE) break;
  319.         }
  320. EnableInterrupts;
  321. }
几时再见 发表于 2017-7-25 20:56 | 显示全部楼层
  1. /******************************************************************************
  2. ******************************************************************************
  3. *
  4. * @file flash.h
  5. *
  6. * @brief application entry point which performs application specific tasks.
  7. *
  8. *******************************************************************************
  9. *
  10. * provide a demo for how to initialize the NV32, output messages via SCI,
  11. * flash operations, etc.
  12. * NOTE:
  13. *        printf call may occupy a lot of memory (around 1924 bytes), so please
  14. *        consider your code size before using printf.
  15. ******************************************************************************
  16. *
  17. * provide FLASH driver
  18. *
  19. ******************************************************************************/



  20. #ifndef FLASH_H_
  21. #define FLASH_H_

  22. /******************************************************************************
  23. * Includes
  24. ******************************************************************************/
  25. #include "common.h"
  26. /******************************************************************************
  27. * Constants
  28. ******************************************************************************/

  29. /******************************************************************************
  30. * Macros
  31. ******************************************************************************/
  32. /* Uncomment the following line to support programming flash while running code from flash */
  33. // #define FLASH_ENABLE_STALLING_FLASH_CONTROLLER

  34. #define ETMRH_FSTAT_MGSTAT0_MASK  (1)
  35. #define ETMRH_FSTAT_MGSTAT1_MASK  (1<<1)

  36. #define FLASH_SECTOR_SIZE        512                // in bytes

  37. /* Flash driver errors */
  38. #define FLASH_ERR_BASE                                0x3000
  39. #define FLASH_ERR_SUCCESS                        0
  40. #define FLASH_ERR_INVALID_PARAM                (FLASH_ERR_BASE+1)        // invalid parameter error code
  41. #define EEPROM_ERR_SINGLE_BIT_FAULT        (FLASH_ERR_BASE+2)        // EEPROM single bit fault error code
  42. #define EEPROM_ERR_DOUBLE_BIT_FAULT        (FLASH_ERR_BASE+4)        // EEPROM double bits fault error code
  43. #define FLASH_ERR_ACCESS                        (FLASH_ERR_BASE+8)        // flash access error code
  44. #define FLASH_ERR_PROTECTION                (FLASH_ERR_BASE+0x10)        // flash protection error code
  45. #define FLASH_ERR_MGSTAT0                        (FLASH_ERR_BASE+0x11)        // flash verification error code
  46. #define FLASH_ERR_MGSTAT1                        (FLASH_ERR_BASE+0x12)        // flash non-correctable error code
  47. #define FLASH_ERR_INIT_CCIF                        (FLASH_ERR_BASE+0x14)        // flash driver init error with CCIF = 1
  48. #define FLASH_ERR_INIT_FDIV                        (FLASH_ERR_BASE+0x18)        // flash driver init error with wrong FDIV

  49. /* Flash and EEPROM commands */


  50. #define FLASH_CMD_PROGRAM                        0x20000000
  51. #define FLASH_CMD_CLEAR       0x00005000
  52. #define FLASH_CMD_ERASE_ALL   0x41000000
  53. #define FLASH_CMD_ERASE_SECTOR                0x40000000
  54. #define FLASH_FACTORY_KEY 0x0065fe9a

  55. #define EFM_DONE_MASK    0x00006000
  56. #define EFM_STATUS_DONE  0x00006000
  57. #define EFM_STATUS_READY 0x00002000

  58. #define FLASH_ACCERR_MASK 0x10

  59. #define M8(adr)  (*((volatile unsigned char  *) (adr)))
  60. #define M16(adr) (*((volatile unsigned short *) (adr)))
  61. #define M32(adr) (*((volatile unsigned long  *) (adr)))



  62. /******************************************************************************
  63. * Types
  64. ******************************************************************************/
  65. typedef  uint16_t (*TFlash_Fun1)(uint32_t wNVMTargetAddress, uint8_t *pbData, uint8_t bByteCount);
  66. typedef  uint16_t (*TFlash_Fun2)(uint32_t wNVMTargetAddress, uint32_t dwData0, uint32_t dwData1);
  67. typedef  uint16_t (*TFlash_Fun3)(uint32_t wNVMTargetAddress, uint32_t dwData);

  68. /******************************************************************************
  69. * Global variables
  70. ******************************************************************************/

  71. /******************************************************************************
  72. * Global functions
  73. ******************************************************************************/
  74. uint16_t Flash_Program(uint32_t wNVMTargetAddress, uint8_t *pData, uint16_t sizeBytes);
  75. uint16_t Flash_Program1LongWord(uint32_t wNVMTargetAddress, uint32_t dwData);
  76. uint16_t Flash_Program2LongWords(uint32_t wNVMTargetAddress, uint32_t dwData0, uint32_t dwData1);

  77. uint16_t Flash_EraseSector(uint32_t wNVMTargetAddress);

  78. uint16_t Flash_VerifyBackdoorKey(void);

  79. uint16_t NVM_EraseAll(void);

  80. uint16_t NVM_Unsecure(void);

  81. uint16_t Flash_Init(void);

  82. #ifdef IAR
  83. void __ramfunc EFM_LaunchCMD(uint32_t EFM_CMD);
  84. #else
  85. void EFM_LaunchCMD(uint32_t EFM_CMD);
  86. #endif


  87. void        Flash_CopyInRAM(void);
  88. void        Flash_CopyRouinte2RAM(char *func, uint16_t sizeFunc);
  89. /********************************************************************/

  90. #endif /* FLASH_H_ */
jp_chen 发表于 2017-7-26 16:34 | 显示全部楼层
有了呀,手册中有flash的寄存器描述的
jp_chen 发表于 2017-7-26 16:36 | 显示全部楼层
QQ浏览器截图_20170726163524_79ABB1380D934150A138BD5D85B71AF9.jpg
这不就是吗?
Dennis-Zhou 发表于 2017-7-31 16:04 | 显示全部楼层
M32(),有这么一个函数吗?我在库中没有找到
5574293 发表于 2017-8-24 17:00 | 显示全部楼层
不错 借鉴借鉴
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

12

帖子

0

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