PageRenderTime 89ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/hardware/msp430/libraries/MspFlash/MspFlash.cpp

https://gitlab.com/murixteam/energia-0101e0012-windows-mwc
C++ | 89 lines | 56 code | 9 blank | 24 comment | 2 complexity | ed53e8728b702d78be7574f4d67c74a0 MD5 | raw file
  1. /*
  2. MspFlash.h - Read/Write flash memory library for MSP430 Energia
  3. Copyright (c) 2012 Peter Brier. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #include "MspFlash.h"
  17. #include <msp430.h>
  18. #include <Energia.h>
  19. // The Flash clock must be between 200 and 400 kHz to operate correctly
  20. // TODO: calculate correct devider (0..64) depending on clock frequenct (F_CPU)
  21. // Now we set F_CPU/64 is 250khz at F_CPU = 16MHz
  22. #define FLASHCLOCK FSSEL1+((F_CPU/400000L) & 63); // SCLK
  23. // erase flash, make sure pointer is in the segment you wish to erase, otherwise you may erase you program or some data
  24. void MspFlashClass::erase(unsigned char *flash)
  25. {
  26. disableWatchDog(); // Disable WDT
  27. #ifdef __MSP430_HAS_FLASH2__
  28. FCTL2 = FWKEY+FLASHCLOCK; // SMCLK/2
  29. FCTL3 = FWKEY; // Clear LOCK
  30. FCTL1 = FWKEY+ERASE; //Enable segment erase
  31. *flash = 0; // Dummy write, erase Segment
  32. FCTL3 = FWKEY+LOCK; // Done, set LOCK
  33. #else
  34. while (FCTL3 & BUSY) // Wait for till busy flag is 0
  35. {
  36. __no_operation();
  37. }
  38. FCTL3 = FWKEY; // Clear Lock bit
  39. FCTL1 = FWKEY+ERASE; // Set Erase bit
  40. *flash = 0; // Dummy write, erase Segment
  41. while (FCTL3 & BUSY) // Wait for till busy flag is 0
  42. {
  43. __no_operation();
  44. }
  45. FCTL3 = FWKEY+LOCK; // Set LOCK bit
  46. #endif
  47. enableWatchDog(); // Enable WDT
  48. }
  49. // load from memory, at segment boundary
  50. void MspFlashClass::read(unsigned char *flash, unsigned char *dest, int len)
  51. {
  52. while(len--)
  53. *(dest++) = *(flash++);
  54. }
  55. // save in to flash (at segment boundary)
  56. void MspFlashClass::write(unsigned char *flash, unsigned char *src, int len)
  57. {
  58. disableWatchDog(); // Disable WDT
  59. #ifdef __MSP430_HAS_FLASH2__
  60. FCTL2 = FWKEY+FLASHCLOCK; // SMCLK/2
  61. FCTL3 = FWKEY; // Clear LOCK
  62. FCTL1 = FWKEY+WRT; // Enable write
  63. while(len--) // Copy data
  64. *(flash++) = *(src++);
  65. FCTL1 = FWKEY; //Done. Clear WRT
  66. FCTL3 = FWKEY+LOCK; // Set LOCK
  67. #else
  68. FCTL3 = FWKEY; // Clear Lock bit
  69. FCTL1 = FWKEY+WRT; // Set Erase bit
  70. while(len--) // Copy data
  71. *(flash++) = *(src++);
  72. FCTL1 = FWKEY; // Done. Clear WRT
  73. FCTL3 = FWKEY+LOCK; // Set LOCK
  74. #endif
  75. enableWatchDog(); // Enable WDT
  76. }
  77. MspFlashClass Flash;