![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
for learning
FlashWriter/FlashWriter.cpp@16:903b5a4433b4, 2019-09-02 (annotated)
- Committer:
- Lightvalve
- Date:
- Mon Sep 02 13:32:33 2019 +0000
- Revision:
- 16:903b5a4433b4
190902
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lightvalve | 16:903b5a4433b4 | 1 | #include "stm32f4xx_flash.h" |
Lightvalve | 16:903b5a4433b4 | 2 | #include "FlashWriter.h" |
Lightvalve | 16:903b5a4433b4 | 3 | |
Lightvalve | 16:903b5a4433b4 | 4 | FlashWriter::FlashWriter(int sector) { |
Lightvalve | 16:903b5a4433b4 | 5 | if (sector > 7) sector = 7; |
Lightvalve | 16:903b5a4433b4 | 6 | __sector = sector; |
Lightvalve | 16:903b5a4433b4 | 7 | __base = __SECTOR_ADDRS[sector]; |
Lightvalve | 16:903b5a4433b4 | 8 | __ready = false; |
Lightvalve | 16:903b5a4433b4 | 9 | |
Lightvalve | 16:903b5a4433b4 | 10 | FLASH_Unlock(); |
Lightvalve | 16:903b5a4433b4 | 11 | FLASH_ClearFlag( FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); |
Lightvalve | 16:903b5a4433b4 | 12 | FLASH_EraseSector(__SECTORS[sector], VoltageRange_3); |
Lightvalve | 16:903b5a4433b4 | 13 | } |
Lightvalve | 16:903b5a4433b4 | 14 | |
Lightvalve | 16:903b5a4433b4 | 15 | bool FlashWriter::ready() { |
Lightvalve | 16:903b5a4433b4 | 16 | return __ready; |
Lightvalve | 16:903b5a4433b4 | 17 | } |
Lightvalve | 16:903b5a4433b4 | 18 | |
Lightvalve | 16:903b5a4433b4 | 19 | void FlashWriter::open() { |
Lightvalve | 16:903b5a4433b4 | 20 | FLASH_Unlock(); |
Lightvalve | 16:903b5a4433b4 | 21 | FLASH_ClearFlag( FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); |
Lightvalve | 16:903b5a4433b4 | 22 | FLASH_EraseSector(__SECTORS[__sector], VoltageRange_3); |
Lightvalve | 16:903b5a4433b4 | 23 | __ready = true; |
Lightvalve | 16:903b5a4433b4 | 24 | } |
Lightvalve | 16:903b5a4433b4 | 25 | |
Lightvalve | 16:903b5a4433b4 | 26 | void FlashWriter::write(uint32_t index, int x) { |
Lightvalve | 16:903b5a4433b4 | 27 | union {int a; uint32_t b;}; |
Lightvalve | 16:903b5a4433b4 | 28 | a = x; |
Lightvalve | 16:903b5a4433b4 | 29 | FLASH_ProgramWord(__base + 4 * index, b); |
Lightvalve | 16:903b5a4433b4 | 30 | } |
Lightvalve | 16:903b5a4433b4 | 31 | |
Lightvalve | 16:903b5a4433b4 | 32 | void FlashWriter::write(uint32_t index, unsigned int x) { |
Lightvalve | 16:903b5a4433b4 | 33 | FLASH_ProgramWord(__base + 4 * index, x); |
Lightvalve | 16:903b5a4433b4 | 34 | } |
Lightvalve | 16:903b5a4433b4 | 35 | |
Lightvalve | 16:903b5a4433b4 | 36 | void FlashWriter::write(uint32_t index, float x) { |
Lightvalve | 16:903b5a4433b4 | 37 | union {float a; uint32_t b;}; |
Lightvalve | 16:903b5a4433b4 | 38 | a = x; |
Lightvalve | 16:903b5a4433b4 | 39 | FLASH_ProgramWord(__base + 4 * index, b); |
Lightvalve | 16:903b5a4433b4 | 40 | } |
Lightvalve | 16:903b5a4433b4 | 41 | |
Lightvalve | 16:903b5a4433b4 | 42 | void FlashWriter::close() { |
Lightvalve | 16:903b5a4433b4 | 43 | FLASH_Lock(); |
Lightvalve | 16:903b5a4433b4 | 44 | } |
Lightvalve | 16:903b5a4433b4 | 45 | |
Lightvalve | 16:903b5a4433b4 | 46 | int flashReadInt(uint32_t sector, uint32_t index) { |
Lightvalve | 16:903b5a4433b4 | 47 | return *(int*) (__SECTOR_ADDRS[sector] + 4 * index); |
Lightvalve | 16:903b5a4433b4 | 48 | } |
Lightvalve | 16:903b5a4433b4 | 49 | |
Lightvalve | 16:903b5a4433b4 | 50 | uint32_t flashReadUint(uint32_t sector, uint32_t index) { |
Lightvalve | 16:903b5a4433b4 | 51 | return *(uint32_t*) (__SECTOR_ADDRS[sector] + 4 * index); |
Lightvalve | 16:903b5a4433b4 | 52 | } |
Lightvalve | 16:903b5a4433b4 | 53 | |
Lightvalve | 16:903b5a4433b4 | 54 | float flashReadFloat(uint32_t sector, uint32_t index) { |
Lightvalve | 16:903b5a4433b4 | 55 | return *(float*) (__SECTOR_ADDRS[sector] + 4 * index); |
Lightvalve | 16:903b5a4433b4 | 56 | } |