flash.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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.c
  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. #include "flash.h"
  40. #include "log.h"
  41. #include "common.h"
  42. #include "error.h"
  43. #include "hal_mcu.h"
  44. //#include ""
  45. // spif config register
  46. //typedef unsigned char uint8_t;
  47. //typedef unsigned long uint32_t;
  48. uint8_t flash_write_ucds(uint32_t addr,uint32_t value)
  49. {
  50. if(addr>0x3ffc || (addr&0x03)>0)
  51. {
  52. return PPlus_ERR_INVALID_DATA;
  53. }
  54. else
  55. {
  56. uint32_t temp = value;
  57. uint32_t offset = (addr + FLASH_UCDS_ADDR_BASE) & 0xffffff;
  58. if(ProgramWord (offset, (uint8_t *) &temp, 4)==0) return PPlus_ERR_FATAL;
  59. }
  60. return PPlus_SUCCESS;
  61. }
  62. uint8_t flash_write_ucds_block(uint32_t addr,uint32_t length,uint32_t *buf)
  63. {
  64. if(addr>0x3ffc||((addr+(length<<2))>0x3ffc || (addr&0x03)>0))
  65. {
  66. return PPlus_ERR_INVALID_DATA;
  67. }
  68. else
  69. {
  70. for(uint32_t i=0;i<length;i++)
  71. {
  72. flash_write_ucds(addr+(i<<2),buf[i]);
  73. }
  74. }
  75. return PPlus_SUCCESS;
  76. }
  77. uint8_t flash_read_ucds_block(uint32_t addr,uint32_t length,uint32_t *tobuf)
  78. {
  79. if(addr>0x3ffc||((addr+(length<<2))>0x3ffc) || (addr&0x03)>0 )
  80. {
  81. return PPlus_ERR_INVALID_DATA;
  82. }
  83. else
  84. {
  85. for(uint32_t i=0;i<length;i++)
  86. {
  87. tobuf[i]=flash_read_ucds(addr+(i<<2));
  88. }
  89. }
  90. return PPlus_SUCCESS;
  91. }
  92. uint8_t flash_write_ucds_block_byte(uint32_t addr,uint32_t length,uint8_t *buf)
  93. {
  94. if(addr>0x3ffc||((addr+(length))>0x3ffc)|| (addr&0x03)>0)
  95. {
  96. return PPlus_ERR_INVALID_DATA;
  97. }
  98. else
  99. {
  100. uint32_t i;
  101. for(i=0;i<(length>>2);i++)
  102. {
  103. flash_write_ucds(addr+(i<<2), (buf[4*i+3]<<24 ) | (buf[4*i+2]<<16)| (buf[4*i+1]<<8) | buf[4*i] );
  104. }
  105. if(length-(i<<2)==3)
  106. {
  107. flash_write_ucds(addr+(i<<2), (buf[4*i+2]<<16)| (buf[4*i+1]<<8) | buf[4*i] );
  108. }
  109. else if(length-(i<<2)==2)
  110. {
  111. flash_write_ucds(addr+(i<<2), (buf[4*i+1]<<8) | buf[4*i] );
  112. }
  113. else if(length-(i<<2)==1)
  114. {
  115. flash_write_ucds(addr+(i<<2), buf[4*i] );
  116. }
  117. }
  118. return PPlus_SUCCESS;
  119. }
  120. uint8_t flash_read_ucds_block_byte(uint32_t addr,uint32_t length,uint8_t *tobuf)
  121. {
  122. if(addr>0x3ffc||((addr+(length))>0x3ffc)|| (addr&0x03)>0)
  123. {
  124. return PPlus_ERR_INVALID_DATA;
  125. }
  126. else
  127. {
  128. uint32_t i,wbuf;
  129. for(i=0;i<(length>>2);i++)
  130. {
  131. wbuf = flash_read_ucds(addr+(i<<2));
  132. tobuf[(i<<2) ] = 0x000000ff & wbuf;
  133. tobuf[(i<<2)+1] = (0x0000ff00 & wbuf)>>8;
  134. tobuf[(i<<2)+2] = (0x00ff0000 & wbuf)>>16;
  135. tobuf[(i<<2)+3] = (0xff000000 & wbuf)>>24;
  136. }
  137. if(length-(i<<2)==3)
  138. {
  139. wbuf = flash_read_ucds(addr+(i<<2));
  140. tobuf[(i<<2) ] = 0x000000ff & wbuf;
  141. tobuf[(i<<2)+1] = (0x0000ff00 & wbuf)>>8;
  142. tobuf[(i<<2)+2] = (0x00ff0000 & wbuf)>>16;
  143. }
  144. else if(length-(i<<2)==2)
  145. {
  146. wbuf = flash_read_ucds(addr+(i<<2));
  147. tobuf[(i<<2) ] = 0x000000ff & wbuf;
  148. tobuf[(i<<2)+1] = (0x0000ff00 & wbuf)>>8;
  149. }
  150. else if(length-(i<<2)==1)
  151. {
  152. wbuf = flash_read_ucds(addr+(i<<2));
  153. tobuf[(i<<2) ] = 0x000000ff & wbuf;
  154. }
  155. }
  156. return PPlus_SUCCESS;
  157. }
  158. uint32_t flash_read_ucds(uint32_t addr)
  159. {
  160. if(addr>0x3ffc)
  161. {
  162. return 0xffffffff;
  163. }
  164. else
  165. {
  166. return read_reg(addr+FLASH_UCDS_ADDR_BASE);
  167. }
  168. }
  169. void flash_erase_ucds_all(void)
  170. {
  171. flash_sector_erase(0x11005000);
  172. flash_sector_erase(0x11006000);
  173. flash_sector_erase(0x11007000);
  174. flash_sector_erase(0x11008000);
  175. }
  176. uint8_t flash_erase_ucds(uint32_t addr)
  177. {
  178. if(addr>0x3ffc)
  179. {
  180. return PPlus_ERR_INVALID_DATA;
  181. }
  182. if(addr<0x0fff)
  183. {
  184. flash_sector_erase(0x11005000);
  185. }
  186. else if(addr<0x1fff)
  187. {
  188. flash_sector_erase(0x11006000);
  189. }
  190. else if(addr<0x2fff)
  191. {
  192. flash_sector_erase(0x11007000);
  193. }
  194. else if(addr<0x3fff)
  195. {
  196. flash_sector_erase(0x11008000);
  197. }
  198. return PPlus_SUCCESS;
  199. }