Loader_Src.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "stm32f10x.h"
  2. #include "stm32_eval_spi_flash.h"
  3. #include "stm3210e_eval.h"
  4. /**
  5. * Description :
  6. * Initilize the MCU Clock, the GPIO Pins corresponding to the
  7. * device and initilize the FSMC with the chosen configuration
  8. * Inputs :
  9. * None
  10. * outputs :
  11. * R0 : "1" : Operation succeeded
  12. * "0" : Operation failure
  13. * Note: Mandatory for all types of device
  14. */
  15. int Init (void)
  16. {
  17. SystemInit();
  18. sFLASH_Init();
  19. return 1;
  20. }
  21. /**
  22. * Description :
  23. * Read data from the device
  24. * Inputs :
  25. * Address : Write location
  26. * Size : Length in bytes
  27. * buffer : Address where to get the data to write
  28. * outputs :
  29. * R0 : "1" : Operation succeeded
  30. * "0" : Operation failure
  31. * Note: Mandatory for all types except SRAM and PSRAM
  32. */
  33. int Read (uint32_t Address, uint32_t Size, uint8_t* buffer)
  34. {
  35. sFLASH_ReadBuffer(buffer, Address, Size);
  36. return 1;
  37. }
  38. /**
  39. * Description :
  40. * Write data from the device
  41. * Inputs :
  42. * Address : Write location
  43. * Size : Length in bytes
  44. * buffer : Address where to get the data to write
  45. * outputs :
  46. * R0 : "1" : Operation succeeded
  47. * "0" : Operation failure
  48. * Note: Mandatory for all types except SRAM and PSRAM
  49. */
  50. int Write (uint32_t Address, uint32_t Size, uint8_t* buffer)
  51. {
  52. sFLASH_WriteBuffer(buffer, Address, Size);
  53. return 1;
  54. }
  55. /**
  56. * Description :
  57. * Erase a full sector in the device
  58. * Inputs :
  59. * None
  60. * outputs :
  61. * R0 : "1" : Operation succeeded
  62. * "0" : Operation failure
  63. * Note: Not Mandatory for SRAM PSRAM and NOR_FLASH
  64. */
  65. int MassErase (void)
  66. {
  67. sFLASH_EraseBulk();
  68. return 1;
  69. }
  70. /**
  71. * Description :
  72. * Erase a full sector in the device
  73. * Inputs :
  74. * SectrorAddress : Start of sector
  75. * Size : Size (in WORD)
  76. * InitVal : Initial CRC value
  77. * outputs :
  78. * R0 : "1" : Operation succeeded
  79. * "0" : Operation failure
  80. * Note: Not Mandatory for SRAM PSRAM and NOR_FLASH
  81. */
  82. int SectorErase (uint32_t EraseStartAddress ,uint32_t EraseEndAddress)
  83. {
  84. EraseStartAddress = EraseStartAddress - EraseStartAddress%0x10000;
  85. while (EraseEndAddress>=EraseStartAddress)
  86. {
  87. sFLASH_EraseSector(EraseStartAddress);
  88. EraseStartAddress += 0x10000;
  89. }
  90. return 1;
  91. }
  92. /**
  93. * Description :
  94. * Calculates checksum value of the memory zone
  95. * Inputs :
  96. * StartAddress : Flash start address
  97. * Size : Size (in WORD)
  98. * InitVal : Initial CRC value
  99. * outputs :
  100. * R0 : Checksum value
  101. * Note: Optional for all types of device
  102. */
  103. uint32_t CheckSum(uint32_t StartAddress, uint32_t Size, uint32_t InitVal)
  104. {
  105. uint8_t missalignementAddress = StartAddress%4;
  106. uint8_t missalignementSize = Size ;
  107. int cnt;
  108. uint32_t Val;
  109. uint8_t value;
  110. StartAddress-=StartAddress%4;
  111. Size += (Size%4==0)?0:4-(Size%4);
  112. for(cnt=0; cnt<Size ; cnt+=4)
  113. {
  114. sFLASH_ReadBuffer(&value, StartAddress ,1);
  115. Val = value;
  116. sFLASH_ReadBuffer(&value, StartAddress + 1,1);
  117. Val+= value<<8;
  118. sFLASH_ReadBuffer(&value, StartAddress + 2,1);
  119. Val+= value<<16;
  120. sFLASH_ReadBuffer(&value, StartAddress + 3,1);
  121. Val+= value<<24;
  122. if(missalignementAddress)
  123. {
  124. switch (missalignementAddress)
  125. {
  126. case 1:
  127. InitVal += (uint8_t) (Val>>8 & 0xff);
  128. InitVal += (uint8_t) (Val>>16 & 0xff);
  129. InitVal += (uint8_t) (Val>>24 & 0xff);
  130. missalignementAddress-=1;
  131. break;
  132. case 2:
  133. InitVal += (uint8_t) (Val>>16 & 0xff);
  134. InitVal += (uint8_t) (Val>>24 & 0xff);
  135. missalignementAddress-=2;
  136. break;
  137. case 3:
  138. InitVal += (uint8_t) (Val>>24 & 0xff);
  139. missalignementAddress-=3;
  140. break;
  141. }
  142. }
  143. else if((Size-missalignementSize)%4 && (Size-cnt) <=4)
  144. {
  145. switch (Size-missalignementSize)
  146. {
  147. case 1:
  148. InitVal += (uint8_t) Val;
  149. InitVal += (uint8_t) (Val>>8 & 0xff);
  150. InitVal += (uint8_t) (Val>>16 & 0xff);
  151. missalignementSize-=1;
  152. break;
  153. case 2:
  154. InitVal += (uint8_t) Val;
  155. InitVal += (uint8_t) (Val>>8 & 0xff);
  156. missalignementSize-=2;
  157. break;
  158. case 3:
  159. InitVal += (uint8_t) Val;
  160. missalignementSize-=3;
  161. break;
  162. }
  163. }
  164. else
  165. {
  166. InitVal += (uint8_t) Val;
  167. InitVal += (uint8_t) (Val>>8 & 0xff);
  168. InitVal += (uint8_t) (Val>>16 & 0xff);
  169. InitVal += (uint8_t) (Val>>24 & 0xff);
  170. }
  171. StartAddress+=4;
  172. }
  173. return (InitVal);
  174. }
  175. /**
  176. * Description :
  177. * Verify flash memory with RAM buffer and calculates checksum value of
  178. * the programmed memory
  179. * Inputs :
  180. * FlashAddr : Flash address
  181. * RAMBufferAddr : RAM buffer address
  182. * Size : Size (in WORD)
  183. * InitVal : Initial CRC value
  184. * outputs :
  185. * R0 : Operation failed (address of failure)
  186. * R1 : Checksum value
  187. * Note: Optional for all types of device
  188. */
  189. uint64_t Verify (uint32_t MemoryAddr, uint32_t RAMBufferAddr, uint32_t Size, uint32_t missalignement)
  190. {
  191. uint32_t InitVal = 0;
  192. uint32_t VerifiedData = 0;
  193. uint8_t TmpBuffer = 0x00;
  194. uint64_t checksum;
  195. Size*=4;
  196. checksum = CheckSum((uint32_t)MemoryAddr + (missalignement & 0xf), Size - ((missalignement >> 16) & 0xF), InitVal);
  197. while (Size>VerifiedData)
  198. {
  199. sFLASH_ReadBuffer(&TmpBuffer, MemoryAddr+VerifiedData, 1);
  200. if (TmpBuffer != *((uint8_t*)RAMBufferAddr+VerifiedData))
  201. return ((checksum<<32) + MemoryAddr+VerifiedData);
  202. VerifiedData++;
  203. }
  204. return (checksum<<32);
  205. }