Skip to content

Commit

Permalink
Refactor FlashEmulator
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph2 committed Nov 8, 2020
1 parent f8f70d7 commit dd5b351
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 160 deletions.
69 changes: 62 additions & 7 deletions flsemu/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "flsemu.h"

/*
Expand All @@ -47,7 +50,7 @@ typedef enum tagFlsEmu_MemoryTypeType {
*
*/
typedef struct tagFlsEmu_SystemMemoryType {
uint32_t pageSize;
uint32_t AllocationGranularity;
} FlsEmu_SystemMemoryType;

/*
Expand All @@ -57,12 +60,10 @@ static FlsEmu_ModuleStateType FlsEmu_ModuleState = FLSEMU_UNINIT; /**< Module-st
static FlsEmu_SystemMemoryType FlsEmu_SystemMemory; /**< System memory configuration. */
static FlsEmu_ConfigType const * FlsEmu_Config = XCP_NULL; /**< Segment configuration. */


/*
** Global Functions.
*/


/** @brief Initializes flash-emulator system.
*
*
Expand All @@ -71,14 +72,12 @@ void FlsEmu_Init(FlsEmu_ConfigType const * config)
{
uint8_t idx;

FlsEmu_SystemMemory.pageSize = FlsEmu_GetPageSize();
FlsEmu_SystemMemory.AllocationGranularity = FlsEmu_GetAllocationGranularity();
FlsEmu_Config = config;
FlsEmu_ModuleState = FLSEMU_INIT;
for (idx = 0; idx < FlsEmu_GetConfig()->numSegments; ++idx) {
//printf("FlsEmu_Init-SEG-NAME: %s\n", FlsEmu_Config->segments[idx]->name);
FlsEmu_OpenCreate(idx);
}

}


Expand All @@ -93,6 +92,43 @@ void FlsEmu_DeInit(void)
FlsEmu_ModuleState = FLSEMU_UNINIT;
}

void FlsEmu_OpenCreate(uint8_t segmentIdx)
{
int length;
char rom[1024];
FlsEmu_SegmentType * segment;
FlsEmu_OpenCreateResultType result;
uint32_t fillerSize;
void * offset;
uint16_t numPages;
uint16_t pageIdx = 0U;

FLSEMU_ASSERT_INITIALIZED();
if (!FLSEMU_VALIDATE_SEGMENT_IDX(segmentIdx)) {
return;
}
segment = FlsEmu_GetConfig()->segments[segmentIdx];
segment->persistentArray = (FlsEmu_PersistentArrayType *)malloc(sizeof(FlsEmu_PersistentArrayType));
segment->currentPage = 0x00;
segment->alloctedPageSize = FlsEmu_AllocatedSize(segmentIdx);
length = strlen(segment->name);
strncpy((char *)rom, (char *)segment->name, length);
rom[length] = '\x00';
strcat((char *)rom, ".rom");
numPages = FlsEmu_NumPages(segmentIdx);
result = FlsEmu_OpenCreatePersitentArray(rom, segment->alloctedPageSize * numPages, segment->persistentArray);
if (result == OPEN_ERROR) {

} else if (result == NEW_FILE) {
fillerSize = segment->alloctedPageSize - segment->pageSize;
for (pageIdx = 0U; pageIdx < numPages; ++pageIdx) {
offset = segment->persistentArray->mappingAddress + (pageIdx * segment->alloctedPageSize);
XcpUtl_MemSet(offset, FLSEMU_ERASED_VALUE, segment->pageSize);
XcpUtl_MemSet(offset + segment->pageSize, FLSEMU_FILLER_VALUE, fillerSize);
}
}
}


void * FlsEmu_BasePointer(uint8_t segmentIdx)
{
Expand All @@ -104,7 +140,6 @@ void * FlsEmu_BasePointer(uint8_t segmentIdx)
return (void*)XCP_NULL;
}
segment = FlsEmu_GetConfig()->segments[segmentIdx];
//
return segment->persistentArray->mappingAddress;
}

Expand Down Expand Up @@ -207,6 +242,26 @@ Xcp_MemoryMappingResultType FlsEmu_MemoryMapper(Xcp_MtaType * dst, Xcp_MtaType c
return XCP_MEMORY_NOT_MAPPED;
}


/*!
* Align emulated page-size to OS allocation granularity.
*/
uint32_t FlsEmu_AllocatedSize(uint8_t segmentIdx)
{
FlsEmu_SegmentType const * segment;
FLSEMU_ASSERT_INITIALIZED();

if (!FLSEMU_VALIDATE_SEGMENT_IDX(segmentIdx)) {
return (uint32_t)0;
}
segment = FlsEmu_GetConfig()->segments[segmentIdx];

return FlsEmu_SystemMemory.AllocationGranularity * (
(segment->pageSize / FlsEmu_SystemMemory.AllocationGranularity) +
((segment->pageSize % FlsEmu_SystemMemory.AllocationGranularity) != 0) ? 1 : 0
);
}

FlsEmu_ConfigType const * FlsEmu_GetConfig(void)
{
return FlsEmu_Config;
Expand Down
17 changes: 17 additions & 0 deletions flsemu/flsemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
** Global Defines.
*/
#define FLSEMU_ERASED_VALUE (0xff) /**< Value of an erased Flash/EEPROM cell. */
#define FLSEMU_FILLER_VALUE (0xcc) /**< Value of an filler value (align to allocation granularity). */

/*
** Global Macros.
Expand Down Expand Up @@ -85,6 +86,7 @@ typedef struct tagFlsEmu_SegmentType {
uint32_t baseAddress;
FlsEmu_PersistentArrayType * persistentArray;
uint8_t currentPage;
uint32_t alloctedPageSize;
} FlsEmu_SegmentType;


Expand All @@ -93,6 +95,18 @@ typedef struct tagFlsEmu_ConfigType {
FlsEmu_SegmentType /*const*/ ** segments;
} FlsEmu_ConfigType;

typedef enum tagFlsEmu_StatusType {
FLSEMU_OK,
FLSEMU_NOT_OK
} FlsEmu_StatusType;


typedef enum tagFlsEmu_OpenCreateType {
OPEN_ERROR,
OPEN_EXSISTING,
NEW_FILE
} FlsEmu_OpenCreateResultType;


/*
** Global Functions / FlashEmu Interface.
Expand All @@ -103,13 +117,16 @@ void FlsEmu_Close(uint8_t segmentIdx);
FlsEmu_ConfigType const * FlsEmu_GetConfig(void);
void FlsEmu_OpenCreate(uint8_t segmentIdx);
FlsEmu_ModuleStateType * FlsEmu_GetModuleState(void);
FlsEmu_OpenCreateResultType FlsEmu_OpenCreatePersitentArray(char const * fileName, uint32_t size, FlsEmu_PersistentArrayType * persistentArray);
void * FlsEmu_BasePointer(uint8_t segmentIdx);
void FlsEmu_SelectPage(uint8_t segmentIdx, uint8_t page);
uint32_t FlsEmu_NumPages(uint8_t segmentIdx);
void FlsEmu_ErasePage(uint8_t segmentIdx, uint8_t page);
void FlsEmu_EraseSector(uint8_t segmentIdx, uint32_t address);
void FlsEmu_EraseBlock(uint8_t segmentIdx, uint16_t block);
uint32_t FlsEmu_GetPageSize(void);
uint32_t FlsEmu_GetAllocationGranularity(void);
Xcp_MemoryMappingResultType FlsEmu_MemoryMapper(Xcp_MtaType * dst, Xcp_MtaType const * src);
uint32_t FlsEmu_AllocatedSize(uint8_t segmentIdx);

#endif // __FLSEMU_H
92 changes: 19 additions & 73 deletions flsemu/posix/flsemu.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,15 @@
#define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define FLSEMU_ERASED_VALUE (0xff) /**< Value of an erased Flash/EEPROM cell. */

static int FlsEmu_OpenCreatePersitentArray(char const * fileName, uint32_t size, FlsEmu_PersistentArrayType * persistentArray);
static void FlsEmu_ClosePersitentArray(FlsEmu_PersistentArrayType const * persistentArray, uint32_t size);
static void FlsEmu_UnmapAddress(void * addr, uint32_t size);
static void FlsEmu_MapAddress(void * mappingAddress, int offset, uint32_t size, int fd);
static bool FlsEmu_Flush(uint8_t segmentIdx);


uint32_t FlsEmu_GetPageSize(void)
uint32_t FlsEmu_GetAllocationGranularity(void)
{
return sysconf(_SC_PAGE_SIZE);
return sysconf(_SC_PAGE_SIZE); /* don't use getpagesize() for portable appz */
}

void FlsEmu_Close(uint8_t segmentIdx)
Expand Down Expand Up @@ -87,6 +86,7 @@ static bool FlsEmu_Flush(uint8_t segmentIdx)
return XCP_TRUE;
}

#if 0
void FlsEmu_OpenCreate(uint8_t segmentIdx)
{
int result;
Expand All @@ -112,6 +112,7 @@ void FlsEmu_OpenCreate(uint8_t segmentIdx)
}

}
#endif

static void FlsEmu_ClosePersitentArray(FlsEmu_PersistentArrayType const * persistentArray, uint32_t size)
{
Expand All @@ -138,28 +139,33 @@ static void FlsEmu_UnmapAddress(void * addr, uint32_t size)
}
}

static int FlsEmu_OpenCreatePersitentArray(char const * fileName, uint32_t size, FlsEmu_PersistentArrayType * persistentArray)
FlsEmu_OpenCreateResultType FlsEmu_OpenCreatePersitentArray(char const * fileName, uint32_t size, FlsEmu_PersistentArrayType * persistentArray)
{
int fd;
void * addr;
FlsEmu_OpenCreateResultType result;
bool newFile = FALSE;

fd = open(fileName, O_RDWR | O_DIRECT | O_DSYNC, 0666);
if (fd == -1) {
if (errno == ENOENT) {
fd = open(fileName, O_RDWR | O_CREAT | O_TRUNC | O_EXCL | O_DIRECT | O_DSYNC, 0666);
if (fd == -1) {
handle_error("creat");
return -1;
return OPEN_ERROR;
} else {
if (fallocate(fd, 0, 0, size) == -1) {
handle_error("fallocate");
return -1;
return OPEN_ERROR;
}
newFile = TRUE;
}
} else {
handle_error("open");
return -1;
return OPEN_ERROR;
}
} else {
newFile = FALSE;
}
persistentArray->fileHandle = (MEM_HANDLE)fd;

Expand All @@ -173,7 +179,12 @@ static int FlsEmu_OpenCreatePersitentArray(char const * fileName, uint32_t size,
persistentArray->mappingHandle = NULL;
persistentArray->currentPage = 0;

return 1;
if (newFile) {
result = NEW_FILE;
} else {
result = OPEN_EXSISTING;
}
return result;
}


Expand Down Expand Up @@ -201,68 +212,3 @@ void FlsEmu_SelectPage(uint8_t segmentIdx, uint8_t page)
*/
}

#if 0
///
/// TESTING
///
#define FLS_SECTOR_SIZE (256)

#define FLS_PAGE_ADDR ((uint16_t)0x8000U)
#define FLS_PAGE_SIZE ((uint32_t)0x4000U)


static FlsEmu_SegmentType S12D512_PagedFlash = {
"XCPSIM_Flash",
FLSEMU_KB(512),
2,
FLS_SECTOR_SIZE,
FLS_PAGE_SIZE,
4,
0x8000,
XCP_NULL,
0,
};

static FlsEmu_SegmentType S12D512_EEPROM = {
"XCPSIM_EEPROM",
FLSEMU_KB(4),
2,
4,
FLSEMU_KB(4),
1,
0x4000,
XCP_NULL,
0,
};

static FlsEmu_SegmentType const * segments[] = {
&S12D512_PagedFlash,
&S12D512_EEPROM,
};

static const FlsEmu_ConfigType FlsEmu_Config = {
2,
(FlsEmu_SegmentType**)segments,
};

int main(void)
{
int pageSize = FlsEmu_GetPageSize();
void * ptr;

printf("PAGE_SIZE: %u\n", pageSize);

FlsEmu_Init(&FlsEmu_Config);

for (int i = 0; i < 32; ++i) {
FlsEmu_SelectPage(0, i);
ptr = FlsEmu_BasePointer(0);
//printf("\tBP: %p\n", ptr);
XcpUtl_MemSet(ptr, i, 16 * 1024);
}

FlsEmu_DeInit();
return 0;
}
#endif

0 comments on commit dd5b351

Please sign in to comment.