flash.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_ */