Bayley Wang
/
flash_configuration
flash based config testing
FlashWriter/FlashWriter.cpp@3:82c00c8e2cb4, 2017-03-07 (annotated)
- Committer:
- bwang
- Date:
- Tue Mar 07 08:06:39 2017 +0000
- Revision:
- 3:82c00c8e2cb4
- Parent:
- 0:941e150e78b6
lot's more commands
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bwang | 0:941e150e78b6 | 1 | #include "stm32f4xx_flash.h" |
bwang | 0:941e150e78b6 | 2 | #include "FlashWriter.h" |
bwang | 0:941e150e78b6 | 3 | |
bwang | 0:941e150e78b6 | 4 | FlashWriter::FlashWriter(int sector) { |
bwang | 0:941e150e78b6 | 5 | if (sector > 7) sector = 7; |
bwang | 0:941e150e78b6 | 6 | __sector = sector; |
bwang | 0:941e150e78b6 | 7 | __base = __SECTOR_ADDRS[sector]; |
bwang | 0:941e150e78b6 | 8 | __ready = false; |
bwang | 0:941e150e78b6 | 9 | } |
bwang | 0:941e150e78b6 | 10 | |
bwang | 0:941e150e78b6 | 11 | bool FlashWriter::ready() { |
bwang | 0:941e150e78b6 | 12 | return __ready; |
bwang | 0:941e150e78b6 | 13 | } |
bwang | 0:941e150e78b6 | 14 | |
bwang | 0:941e150e78b6 | 15 | void FlashWriter::open() { |
bwang | 0:941e150e78b6 | 16 | FLASH_Unlock(); |
bwang | 0:941e150e78b6 | 17 | FLASH_ClearFlag( FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); |
bwang | 0:941e150e78b6 | 18 | FLASH_EraseSector(__SECTORS[__sector], VoltageRange_3); |
bwang | 0:941e150e78b6 | 19 | __ready = true; |
bwang | 0:941e150e78b6 | 20 | } |
bwang | 0:941e150e78b6 | 21 | |
bwang | 0:941e150e78b6 | 22 | void FlashWriter::write(uint32_t index, int x) { |
bwang | 0:941e150e78b6 | 23 | union {int a; uint32_t b;}; |
bwang | 0:941e150e78b6 | 24 | a = x; |
bwang | 0:941e150e78b6 | 25 | FLASH_ProgramWord(__base + 4 * index, b); |
bwang | 0:941e150e78b6 | 26 | } |
bwang | 0:941e150e78b6 | 27 | |
bwang | 0:941e150e78b6 | 28 | void FlashWriter::write(uint32_t index, unsigned int x) { |
bwang | 0:941e150e78b6 | 29 | FLASH_ProgramWord(__base + 4 * index, x); |
bwang | 0:941e150e78b6 | 30 | } |
bwang | 0:941e150e78b6 | 31 | |
bwang | 0:941e150e78b6 | 32 | void FlashWriter::write(uint32_t index, float x) { |
bwang | 0:941e150e78b6 | 33 | union {float a; uint32_t b;}; |
bwang | 0:941e150e78b6 | 34 | a = x; |
bwang | 0:941e150e78b6 | 35 | FLASH_ProgramWord(__base + 4 * index, b); |
bwang | 0:941e150e78b6 | 36 | } |
bwang | 0:941e150e78b6 | 37 | |
bwang | 0:941e150e78b6 | 38 | void FlashWriter::close() { |
bwang | 0:941e150e78b6 | 39 | FLASH_Lock(); |
bwang | 0:941e150e78b6 | 40 | __ready = false; |
bwang | 0:941e150e78b6 | 41 | } |
bwang | 0:941e150e78b6 | 42 | |
bwang | 0:941e150e78b6 | 43 | int flashReadInt(uint32_t sector, uint32_t index) { |
bwang | 0:941e150e78b6 | 44 | return *(int*) (__SECTOR_ADDRS[sector] + 4 * index); |
bwang | 0:941e150e78b6 | 45 | } |
bwang | 0:941e150e78b6 | 46 | |
bwang | 0:941e150e78b6 | 47 | uint32_t flashReadUint(uint32_t sector, uint32_t index) { |
bwang | 0:941e150e78b6 | 48 | return *(uint32_t*) (__SECTOR_ADDRS[sector] + 4 * index); |
bwang | 0:941e150e78b6 | 49 | } |
bwang | 0:941e150e78b6 | 50 | |
bwang | 0:941e150e78b6 | 51 | float flashReadFloat(uint32_t sector, uint32_t index) { |
bwang | 0:941e150e78b6 | 52 | return *(float*) (__SECTOR_ADDRS[sector] + 4 * index); |
bwang | 0:941e150e78b6 | 53 | } |