Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
TivaEEPROMEmulation.cxx
Go to the documentation of this file.
1
35
36#include <cstring>
37
38const size_t EEPROMEmulation::BLOCK_SIZE = 4;
40
45TivaEEPROMEmulation::TivaEEPROMEmulation(const char *name, size_t file_size_bytes)
46 : EEPROMEmulation(name, file_size_bytes)
47{
48 HASSERT(SECTOR_SIZE >= (file_size() * 2));
49 mount();
50}
51
52inline const uint32_t *TivaEEPROMEmulation::get_block(
53 unsigned sector, unsigned offset)
54{
55 return (uint32_t*)(&__eeprom_start + sector * EEPROMEmulation::SECTOR_SIZE + offset * EEPROMEmulation::BLOCK_SIZE);
56}
57
64const uint32_t* TivaEEPROMEmulation::block(unsigned sector, unsigned offset) {
65 return get_block(sector, offset);
66}
67
68
73{
74 HASSERT(sector < sectorCount_);
75
76 /* because the TM4C123 device family has small flash sectors, we allow
77 * multiple physical sectors to be ganged together to form one bigger
78 * virtual sector for larger EEPROM file storage.
79 */
80 size_t sectors_per_sector = (SECTOR_SIZE / FAMILY);
81
82 auto* address = get_block(sector, 0);
83
84 /* because the first "real" sector is always used to check block integrity,
85 * we need to be sure that we erase that sector last. We assume that the
86 * driver never tries to erase a sector that is marked as intact.
87 */
88 while (sectors_per_sector)
89 {
90 portENTER_CRITICAL();
91 MAP_FlashErase((uint32_t)address + (FAMILY * --sectors_per_sector));
92 portEXIT_CRITICAL();
93 }
94}
95
104 unsigned sector, unsigned start_block, uint32_t *data, uint32_t byte_count)
105{
106 HASSERT(sector < sectorCount_);
107 HASSERT((byte_count % BLOCK_SIZE) == 0);
108 HASSERT(start_block + (byte_count / BLOCK_SIZE) <= rawBlockCount_);
109 auto* address = get_block(sector, start_block);
110
111 portENTER_CRITICAL();
112 MAP_FlashProgram(data, (uint32_t)address, byte_count);
113 portEXIT_CRITICAL();
114}
const char __eeprom_start
Linker-defined symbol where in the memory space (flash) the eeprom emulation data starts.
Emulates EEPROM in FLASH for the Tiva, LPC17xx and LPC40xx platforms.
static const size_t BLOCK_SIZE
block size in bytes
const uint16_t rawBlockCount_
How many blocks are there in a sector.
static const size_t SECTOR_SIZE
Sector size in bytes.
void mount()
Mount the EEPROM file.
static const size_t BYTES_PER_BLOCK
useful data bytes size in bytes
const uint8_t sectorCount_
Total number of sectors available.
size_t file_size()
Get the maximum file size of the EEPROM file.
Definition EEPROM.hxx:81
static const unsigned FAMILY
Family that device belongs to
void flash_program(unsigned sector, unsigned start_block, uint32_t *data, uint32_t byte_count) override
Simple hardware abstraction for FLASH program API.
TivaEEPROMEmulation()
Default constructor.
void flash_erase(unsigned sector) override
Simple hardware abstraction for FLASH erase API.
const uint32_t * block(unsigned sector, unsigned offset) override
Computes the pointer to load the data stored in a specific block from.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
Definition macros.h:138