66
Dependencies: mbed-dev-f303 FastPWM3
FlashWriter/FlashWriter.cpp@54:4ce8f97be6ae, 2022-06-12 (annotated)
- Committer:
- yezhong
- Date:
- Sun Jun 12 12:31:38 2022 +0000
- Revision:
- 54:4ce8f97be6ae
- Parent:
- 23:2adf23ee0305
- Child:
- 50:9d762c5d05c3
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benkatz | 23:2adf23ee0305 | 1 | #include "stm32f4xx_flash.h" |
benkatz | 23:2adf23ee0305 | 2 | #include "FlashWriter.h" |
benkatz | 23:2adf23ee0305 | 3 | |
benkatz | 23:2adf23ee0305 | 4 | FlashWriter::FlashWriter(int sector) { |
benkatz | 23:2adf23ee0305 | 5 | if (sector > 7) sector = 7; |
benkatz | 23:2adf23ee0305 | 6 | __sector = sector; |
benkatz | 23:2adf23ee0305 | 7 | __base = __SECTOR_ADDRS[sector]; |
benkatz | 23:2adf23ee0305 | 8 | __ready = false; |
benkatz | 23:2adf23ee0305 | 9 | } |
benkatz | 23:2adf23ee0305 | 10 | |
benkatz | 23:2adf23ee0305 | 11 | bool FlashWriter::ready() { |
benkatz | 23:2adf23ee0305 | 12 | return __ready; |
benkatz | 23:2adf23ee0305 | 13 | } |
benkatz | 23:2adf23ee0305 | 14 | |
benkatz | 23:2adf23ee0305 | 15 | void FlashWriter::open() { |
benkatz | 23:2adf23ee0305 | 16 | FLASH_Unlock(); |
benkatz | 23:2adf23ee0305 | 17 | FLASH_ClearFlag( FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); |
benkatz | 23:2adf23ee0305 | 18 | FLASH_EraseSector(__SECTORS[__sector], VoltageRange_3); |
benkatz | 23:2adf23ee0305 | 19 | __ready = true; |
benkatz | 23:2adf23ee0305 | 20 | } |
benkatz | 23:2adf23ee0305 | 21 | |
benkatz | 23:2adf23ee0305 | 22 | void FlashWriter::write(uint32_t index, int x) { |
benkatz | 23:2adf23ee0305 | 23 | union {int a; uint32_t b;}; |
benkatz | 23:2adf23ee0305 | 24 | a = x; |
benkatz | 23:2adf23ee0305 | 25 | FLASH_ProgramWord(__base + 4 * index, b); |
benkatz | 23:2adf23ee0305 | 26 | } |
benkatz | 23:2adf23ee0305 | 27 | |
benkatz | 23:2adf23ee0305 | 28 | void FlashWriter::write(uint32_t index, unsigned int x) { |
benkatz | 23:2adf23ee0305 | 29 | FLASH_ProgramWord(__base + 4 * index, x); |
benkatz | 23:2adf23ee0305 | 30 | } |
benkatz | 23:2adf23ee0305 | 31 | |
benkatz | 23:2adf23ee0305 | 32 | void FlashWriter::write(uint32_t index, float x) { |
benkatz | 23:2adf23ee0305 | 33 | union {float a; uint32_t b;}; |
benkatz | 23:2adf23ee0305 | 34 | a = x; |
benkatz | 23:2adf23ee0305 | 35 | FLASH_ProgramWord(__base + 4 * index, b); |
benkatz | 23:2adf23ee0305 | 36 | } |
benkatz | 23:2adf23ee0305 | 37 | |
benkatz | 23:2adf23ee0305 | 38 | void FlashWriter::close() { |
benkatz | 23:2adf23ee0305 | 39 | FLASH_Lock(); |
benkatz | 23:2adf23ee0305 | 40 | __ready = false; |
benkatz | 23:2adf23ee0305 | 41 | } |
benkatz | 23:2adf23ee0305 | 42 | |
benkatz | 23:2adf23ee0305 | 43 | int flashReadInt(uint32_t sector, uint32_t index) { |
benkatz | 23:2adf23ee0305 | 44 | return *(int*) (__SECTOR_ADDRS[sector] + 4 * index); |
benkatz | 23:2adf23ee0305 | 45 | } |
benkatz | 23:2adf23ee0305 | 46 | |
benkatz | 23:2adf23ee0305 | 47 | uint32_t flashReadUint(uint32_t sector, uint32_t index) { |
benkatz | 23:2adf23ee0305 | 48 | return *(uint32_t*) (__SECTOR_ADDRS[sector] + 4 * index); |
benkatz | 23:2adf23ee0305 | 49 | } |
benkatz | 23:2adf23ee0305 | 50 | |
benkatz | 23:2adf23ee0305 | 51 | float flashReadFloat(uint32_t sector, uint32_t index) { |
benkatz | 23:2adf23ee0305 | 52 | return *(float*) (__SECTOR_ADDRS[sector] + 4 * index); |
benkatz | 23:2adf23ee0305 | 53 | } |