osal_snv.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include <stdint.h>
  29. #include "osal.h"
  30. #include "flash.h"
  31. #include "error.h"
  32. #include "osal_snv.h"
  33. #include "log.h"
  34. #ifdef USE_FS
  35. #include "fs.h"
  36. #endif
  37. #ifndef USE_FS
  38. #define USE_FS 0
  39. #endif
  40. static void print_hex (const uint8 *data, uint16 len)
  41. {
  42. uint16 i;
  43. for (i = 0; i < len - 1; i++)
  44. {
  45. LOG("%x,",data[i]);
  46. LOG(" ");
  47. }
  48. LOG("%x\n",data[i]);
  49. }
  50. #if (USE_FS == 0)
  51. #define NVM_BASE_ADDR 0x11070000 //64K bytes
  52. extern int ProgramWord (unsigned long offset, const unsigned char *buf,uint8_t size); // size must be 4
  53. static int snvwr(uint32_t addr, const uint8_t* buf, uint32_t size)
  54. {
  55. int i;
  56. addr &= 0x00ffffff;
  57. if((addr%4)){
  58. return PPlus_ERR_DATA_ALIGN;
  59. }
  60. size = size +3;
  61. size = size - (size%4);
  62. for(i = 0; i< size; i+=4){
  63. if(ProgramWord (addr, buf, 4) ==0 )
  64. return PPlus_ERR_SPI_FLASH;
  65. addr += 4;
  66. buf += 4;
  67. }
  68. return PPlus_SUCCESS;
  69. }
  70. static uint32_t snv_calc_addr(osalSnvId_t id)
  71. {
  72. if(id == 0x70 || id == 0x71)
  73. return NVM_BASE_ADDR + 0x1000*12 + (id-0x70)*0x1000;
  74. else if(id >= 0x20 && id < 0x2c)
  75. return NVM_BASE_ADDR + 0x1000*(id-0x20);
  76. else
  77. return 0;
  78. }
  79. uint8 osal_snv_init( void )
  80. {
  81. return SUCCESS;
  82. }
  83. uint8 osal_snv_read( osalSnvId_t id, osalSnvLen_t len, void *pBuf)
  84. {
  85. uint32_t* pNv = (uint32_t*)snv_calc_addr(id);
  86. LOG("osal_snv_read:%x\n",id);
  87. if(pNv == NULL)
  88. return NV_OPER_FAILED;
  89. if(pNv[0] == 0xffffffff)
  90. return NV_OPER_FAILED;
  91. osal_memcpy(pBuf, (void*)(pNv+1), (uint32_t)len);
  92. print_hex(pBuf, len);
  93. return SUCCESS;
  94. }
  95. uint8 osal_snv_write( osalSnvId_t id, osalSnvLen_t len, void *pBuf)
  96. {
  97. int ret = PPlus_SUCCESS;
  98. uint32_t tmp[16];
  99. uint8_t* pSrc = (uint8_t*)pBuf;
  100. uint32_t addr = snv_calc_addr(id);
  101. uint8_t len1 = 0;
  102. LOG("osal_snv_write:%x,%d\n",id,len);
  103. print_hex(pBuf, len);
  104. if(addr == 0)
  105. return NV_OPER_FAILED;
  106. flash_sector_erase(addr);
  107. osal_memset(tmp, 0, 16*4);
  108. tmp[0] = (uint32_t)id;
  109. len1 = len > 16*4-4 ? 16*4-4 : len;
  110. osal_memcpy((void*)(tmp+1), pSrc, len1);
  111. ret = snvwr(addr, (const uint8_t*)tmp, len1+4);
  112. len -= len1;
  113. pSrc += len1;
  114. if(ret !=0)
  115. return NV_OPER_FAILED;
  116. while(len){
  117. len1 = len > 16*4 ? 16*4 : len;
  118. osal_memcpy((void*)tmp, pSrc, len1);
  119. ret = snvwr(addr, (const uint8_t*)tmp, len1);
  120. len -= len1;
  121. pSrc += len1;
  122. }
  123. if(ret !=0)
  124. return NV_OPER_FAILED;
  125. //LOG("Success\n");
  126. return SUCCESS;
  127. }
  128. uint8 osal_snv_compact( uint8 threshold )
  129. {
  130. return SUCCESS;
  131. }
  132. #else
  133. uint8 osal_snv_init( void )
  134. {
  135. if(!hal_fs_initialized())
  136. return NV_OPER_FAILED;
  137. return SUCCESS;
  138. }
  139. uint8 osal_snv_read( osalSnvId_t id, osalSnvLen_t len, void *pBuf)
  140. {
  141. int ret;
  142. LOG("osal_snv_read:%x\n",id);
  143. ret = hal_fs_item_read((uint16_t)id,(uint8_t *) pBuf, (uint16_t)len,NULL);
  144. if(ret != PPlus_SUCCESS){
  145. LOG("rd_ret:%d\n",ret);
  146. return NV_OPER_FAILED;
  147. }
  148. print_hex(pBuf, len);
  149. return SUCCESS;
  150. }
  151. uint8 osal_snv_write( osalSnvId_t id, osalSnvLen_t len, void *pBuf)
  152. {
  153. int ret = PPlus_SUCCESS;
  154. LOG("osal_snv_write:%x,%d\n",id,len);
  155. print_hex(pBuf, len);
  156. if(hal_fs_get_free_size() < len+32){
  157. if(hal_fs_get_garbage_size(NULL) > len+32){
  158. hal_fs_garbage_collect();
  159. }
  160. else
  161. {
  162. return NV_OPER_FAILED;
  163. }
  164. }
  165. ret = hal_fs_item_write((uint16_t) id, (uint8_t *) pBuf, (uint16_t) len);
  166. if(ret !=0){
  167. LOG("wr_ret:%d\n",ret);
  168. return NV_OPER_FAILED;
  169. }
  170. //LOG("Success\n");
  171. return SUCCESS;
  172. }
  173. uint8 osal_snv_compact( uint8 threshold ){
  174. return 0;
  175. }
  176. #endif