flash.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file flash.h
  30. * @brief Contains all functions support for flash driver
  31. * @version 0.0
  32. * @date 27. Nov. 2017
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #ifndef _FLASH_H_
  40. #define _FLASH_H_
  41. #include "types.h"
  42. #include "gpio.h"
  43. //define flash ucds
  44. #define FLASH_UCDS_ADDR_BASE 0x11005000
  45. #define SPIF_CONFIG_BASE 0x11080000
  46. #define SPIF_RLEN (SPIF_CONFIG_BASE+0x88)
  47. #define RFIFO_DEPTH (SPIF_CONFIG_BASE+0x84)
  48. // commands defines
  49. #define PAGE_PROGRAM_CMD 0x2
  50. #define FAST_PROGRAM_CMD 0xF2
  51. #define WRITE_ENABLE_CMD 0x6
  52. #define WRITE_DISABLE_CMD 0x4
  53. #define READ_STATUS_CMD 0x5
  54. #define WRITE_STATUS_CMD 0x1
  55. #define SECT_ERASE_CMD 0x20
  56. #define BLOCK32_ERASE_CMD 0x52
  57. #define BLOCK64_ERASE_CMD 0xD8
  58. #define CHIP_ERASE_CMD 0xC7
  59. #define DEEP_DOWN_CMD 0xB9
  60. #define RELEASE_DOWN_CMD 0xAB
  61. #define MANUF_ID_CMD 0x90
  62. #define READ_ID_CMD 0x9F
  63. #define FAST_READ_CMD 0x0B
  64. #define READ_DATA_CMD 0x3
  65. #define WAIT_FOR_SPIF() {while(!( (*(volatile uint32_t *)(SPIF_CONFIG_BASE+0x90)) & 0x4)) ;}
  66. #define CHIP_ID_LENGTH 64
  67. #define CHIP_ID_PID_LEN 16
  68. #define CHIP_ID_LID_LEN 10
  69. #define CHIP_ID_MID_LEN 16
  70. #define CHIP_ID_TID_LEN 14
  71. #define CHIP_ID_SID_LEN 8
  72. #define CHIP_MADDR_LEN 6
  73. typedef enum{
  74. CHIP_ID_UNCHECK,
  75. CHIP_ID_EMPTY,
  76. CHIP_ID_VALID,
  77. CHIP_ID_INVALID,
  78. }CHIP_ID_STATUS_e;
  79. typedef struct{
  80. CHIP_ID_STATUS_e chipIdStatus;
  81. uint8_t pid[CHIP_ID_PID_LEN];
  82. uint8_t lid[CHIP_ID_LID_LEN];
  83. uint8_t mid[CHIP_ID_MID_LEN];
  84. uint8_t tid[CHIP_ID_TID_LEN];
  85. uint8_t sid[CHIP_ID_SID_LEN];
  86. }chipId_t;
  87. typedef struct{
  88. CHIP_ID_STATUS_e chipMAddrStatus;
  89. uint8_t mAddr[CHIP_MADDR_LEN];
  90. }chipMAddr_t;
  91. extern chipId_t g_chipId;
  92. extern chipMAddr_t g_chipMAddr;
  93. void init_spif(void);
  94. void close_spif(void);
  95. void read_deviceID(void);
  96. int read_status_register(void);
  97. void write_status_register(int value);
  98. void flash_block64_erase(unsigned int addr);
  99. void flash_sector_erase(unsigned int addr);
  100. int ProgramPage4 (unsigned long offset, uint32 data);
  101. int ProgramPage64 (unsigned long offset, const unsigned char *buf, int size);
  102. int ProgramWord (unsigned long offset, const unsigned char *buf,uint8_t size) ;
  103. int WriteFlash(unsigned int offset, uint32_t value);
  104. int WriteFlashShort(unsigned int offset, uint16_t value);
  105. uint8_t ReadFlash (unsigned int addr);
  106. uint32_t ReadFlashWord (unsigned int addr);
  107. uint8_t flash_write_ucds(uint32_t addr,uint32_t value);
  108. uint32_t flash_read_ucds(uint32_t addr);
  109. void flash_erase_ucds_all(void);
  110. uint8_t flash_erase_ucds(uint32_t addr);
  111. uint8_t flash_write_ucds_block(uint32_t addr,uint32_t length,uint32_t *buf);
  112. uint8_t flash_read_ucds_block(uint32_t addr,uint32_t length,uint32_t *tobuf);
  113. uint8_t flash_write_ucds_block_byte(uint32_t addr,uint32_t length,uint8_t *buf);
  114. uint8_t flash_read_ucds_block_byte(uint32_t addr,uint32_t length,uint8_t *tobuf);
  115. void check_chip_id(void);
  116. void check_chip_mAddr(void);
  117. void LOG_CHIP_ID(void);
  118. void LOG_CHIP_MADDR(void);
  119. /*******************************************************************************
  120. * @fn pplus_enter_programming_mode
  121. *
  122. * @brief force deive enter to programing mode.
  123. *
  124. * input parameters
  125. *
  126. * @param none.
  127. *
  128. * output parameters
  129. *
  130. * @param none.
  131. *
  132. * @return none.
  133. */
  134. void pplus_enter_programming_mode(void);
  135. /*******************************************************************************
  136. * @fn pplus_LoadMACFromChipMAddr
  137. *
  138. * @brief Used to load MAC Address from chip Maddr
  139. *
  140. * input parameters
  141. *
  142. * @param None.
  143. *
  144. * output parameters
  145. *
  146. * @param None.
  147. *
  148. * @return CHIP_ID_STATUS_e.
  149. */
  150. extern CHIP_ID_STATUS_e pplus_LoadMACFromChipMAddr(void);
  151. #endif