tilz0R

T.Percic flash

Mar 14th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. #define FLASH_CONFIG_START 0x00000000 //Napisi naslov flasha kjer se konfiguracija začne
  2.  
  3. typedef struct {
  4. uint8_t config1;
  5. uint16_t config2;
  6. uint32_t config3;
  7. uint8_t config4;
  8. uint8_t config5;
  9. } FLASH_Struct;
  10.  
  11. //podatki: - kaj se zapise
  12. //count: - koliko zapisat
  13. void FLASH_Write(uint8_t* podatki, uint16_t count) {
  14. //Koda za zapis
  15.  
  16. //Najprej pobrišeš sektor, ki ga imaš za nastavitve, potem zapišeš notri.
  17. }
  18.  
  19. //podatki: - kam se prebere
  20. //count: - koliko prebrat
  21. void FLASH_Read(uint8_t* podatki, uint16_t count) {
  22. //Koda za prebrat
  23. //Branje je enostavno, samo memory copy iz flasha v RAM kadar rabiš:
  24. memcpy(podatki, (uint8_t *) FLASH_CONFIG_START, count);
  25. }
  26.  
  27.  
  28. //Uporaba
  29. FLASH_Struct config;
  30.  
  31. //Preberi nastavitve iz flasha
  32. FLASH_Read((uint8_t *)&config, sizeof(FLASH_Struct));
  33.  
  34. //Zamenjaj kar imaš :D
  35. config.config5 = nekaj;
  36.  
  37. //Zapiši nastavitve iz flasha, vse ponovno
  38. FLASH_Write((uint8_t *)&config, sizeof(FLASH_Struct));
  39.  
  40.  
  41.  
  42.  
  43. uint32_t TM_FLASHLOADER_ProcessPacket(uint8_t* data, uint32_t count) {
  44. uint32_t status;
  45. uint32_t blocks;
  46.  
  47. /* Check if sectors erased */
  48. if (TM_FLASHLOADER_EraseSectors()) {
  49. /* Return error */
  50. return 1;
  51. }
  52.  
  53. /* Clear all flash flags */
  54. FLASH_ClearFlag(FLAG_ALL);
  55.  
  56. /* Status flag */
  57. status = 0;
  58.  
  59. /* Get number of blocks in buffer */
  60. blocks = count >> 2;
  61.  
  62. /* Write data to flash memory */
  63. while (blocks--) {
  64. /* Check if application end address has reached */
  65. if (FLASHLOADER.WritePointer >= FLASHLOADER.ApplicationAddress + FLASHLOADER.ApplicationProgramSize) {
  66. /* No memory available for program anymore */
  67. return 2;
  68. }
  69.  
  70. /* Program flash as words */
  71. status += FLASH_ProgramWord(FLASHLOADER.WritePointer, *(uint32_t *)data) != FLASH_COMPLETE;
  72.  
  73. /* Increase pointer */
  74. FLASHLOADER.WritePointer += 4;
  75. data += 4;
  76. }
  77.  
  78. /* Write remaining data */
  79. count = count % 4;
  80.  
  81. /* Write to flash */
  82. while (count--) {
  83. /* Check if application end address has reached */
  84. if (FLASHLOADER.WritePointer >= FLASHLOADER.ApplicationAddress + FLASHLOADER.ApplicationProgramSize) {
  85. /* No memory available for program anymore */
  86. return 2;
  87. }
  88.  
  89. /* Program flash as bytes */
  90. status += FLASH_ProgramByte(FLASHLOADER.WritePointer++, *(__IO uint8_t *)data++) != FLASH_COMPLETE;
  91. }
  92.  
  93. /* Everything OK = status = 0 */
  94. return status;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment