Resized nearly all task stacks Also: - Menu fixes for insertion. Almost done, just need to fix the negative voltage insertion for mcp and cathode - Added Device parameters, must be filled in git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@271 05563f52-14a8-4384-a975-3d1654cca0fa
238 lines
6.3 KiB
C
238 lines
6.3 KiB
C
// -----------------------------------------------------------------------------
|
|
/// @file CachedStorage.c
|
|
/// @brief EEPROM driver including local caching (for one page)
|
|
// -----------------------------------------------------------------------------
|
|
// Micro-Key bv
|
|
// Industrieweg 28, 9804 TG Noordhorn
|
|
// Postbus 92, 9800 AB Zuidhorn
|
|
// The Netherlands
|
|
// Tel: +31 594 503020
|
|
// Fax: +31 594 505825
|
|
// Email: support@microkey.nl
|
|
// Web: www.microkey.nl
|
|
// -----------------------------------------------------------------------------
|
|
/// $Revision$
|
|
/// $Author$
|
|
/// $Date$
|
|
// (c) 2017 Micro-Key bv
|
|
// -----------------------------------------------------------------------------
|
|
|
|
/// @file CachedStorage.c
|
|
/// @ingroup {group_name}
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Include files
|
|
// -----------------------------------------------------------------------------
|
|
|
|
#include "Logger.h"
|
|
#include <string.h>
|
|
#include "CachedStorage.h"
|
|
|
|
#include "InternalFlash.h"
|
|
|
|
#include "stm32f10x_flash.h"
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Constant and macro definitions
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Type definitions
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// File-scope variables
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Function declarations
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
// Function definitions
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
ErrorStatus CachedStorage_construct(struct CachedStorage* self, struct MemoryDevice* memoryDevice, unsigned int pageNumber, size_t cacheSize)
|
|
{
|
|
ErrorStatus returnValue = SUCCESS;
|
|
if (!self->initialized)
|
|
{
|
|
if (returnValue == SUCCESS)
|
|
{
|
|
if (pageNumber > 0)
|
|
{
|
|
self->pageNumber = pageNumber;
|
|
}
|
|
else
|
|
{
|
|
returnValue = ERROR;
|
|
}
|
|
}
|
|
|
|
if (returnValue == SUCCESS)
|
|
{
|
|
self->memoryDevice = memoryDevice;
|
|
|
|
}
|
|
|
|
if (returnValue == SUCCESS)
|
|
{
|
|
self->cacheSize = cacheSize;
|
|
self->tempBuffer = pvPortMalloc(cacheSize * 4);
|
|
if (self->tempBuffer == NULL)
|
|
{
|
|
returnValue = ERROR;
|
|
LOGGER_ERROR(mainLog, "Failed to malloc to tempbuffer");
|
|
}
|
|
else
|
|
{
|
|
LOGGER_INFO(mainLog, "Created tempBuffer memory with size %x at address %p", self->cacheSize, self->tempBuffer);
|
|
}
|
|
}
|
|
|
|
if (returnValue == SUCCESS)
|
|
{
|
|
self->storage = pvPortMalloc(cacheSize * 4);
|
|
if (self->storage == NULL)
|
|
{
|
|
returnValue = ERROR;
|
|
LOGGER_ERROR(mainLog, "Failed to malloc to storage");
|
|
}
|
|
else
|
|
{
|
|
LOGGER_INFO(mainLog, "Created storage memory with size %x at address %p", self->cacheSize, self->storage);
|
|
}
|
|
}
|
|
|
|
if (returnValue == SUCCESS)
|
|
{
|
|
MemoryDevice_read(self->memoryDevice, self->storage, self->memoryDevice->startAddress + self->memoryDevice->pageSize * self->pageNumber, self->cacheSize);
|
|
self->initialized = true;
|
|
self->dirty = false;
|
|
|
|
LOGGER_DEBUG(mainLog, "Created Cached Storage at page: %d", self->pageNumber);
|
|
}
|
|
}
|
|
return returnValue;
|
|
}
|
|
|
|
void CachedStorage_destruct(struct CachedStorage* self)
|
|
{
|
|
if (self->initialized)
|
|
{
|
|
vPortFree(self->storage);
|
|
vPortFree(self->tempBuffer);
|
|
LOGGER_INFO(mainLog, "Free'd buffers");
|
|
self->initialized = false;
|
|
}
|
|
}
|
|
|
|
void CachedStorage_writeWord(struct CachedStorage* self, int offset, uint32_t _value)
|
|
{
|
|
if (self->initialized)
|
|
{
|
|
if(offset < self->cacheSize)
|
|
{
|
|
|
|
self->storage[offset] = _value;
|
|
self->dirty = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
void CachedStorage_writeBlob(struct CachedStorage* self, int offset, const void* blob, size_t blobSize)
|
|
{
|
|
if (self->initialized)
|
|
{
|
|
if(offset + blobSize <= self->cacheSize)
|
|
{
|
|
memcpy(&self->storage[offset], blob, blobSize * 4);
|
|
self->dirty = true;
|
|
}
|
|
else
|
|
{
|
|
LOGGER_ERROR(mainLog, "WriteBlob failed at offset %d, blobsize %d --- %p", offset, blobSize, self->storage[offset]);
|
|
}
|
|
}
|
|
}
|
|
|
|
uint32_t CachedStorage_readWord(struct CachedStorage* self, int offset)
|
|
{
|
|
if (self->initialized)
|
|
{
|
|
if(offset < self->cacheSize)
|
|
{
|
|
return self->storage[offset];
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const void* CachedStorage_readBlob(struct CachedStorage* self, int offset)
|
|
{
|
|
if (self->initialized)
|
|
{
|
|
if(offset < self->cacheSize)
|
|
{
|
|
return &self->storage[offset];
|
|
}
|
|
else
|
|
{
|
|
LOGGER_ERROR(mainLog, "ReadBlob failed at offset %d --- %p", offset, self->storage[offset]);
|
|
return NULL;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
void CachedStorage_commit(struct CachedStorage* self)
|
|
{
|
|
if (self->initialized)
|
|
{
|
|
if(self->dirty)
|
|
{
|
|
MemoryDevice_read(self->memoryDevice, self->tempBuffer, self->memoryDevice->startAddress + self->memoryDevice->pageSize * self->pageNumber, self->cacheSize);
|
|
|
|
|
|
if(memcmp(self->tempBuffer, self->storage, (self->cacheSize * 4)) != 0)
|
|
{
|
|
if (self->memoryDevice->needsEraseBeforeWrite)
|
|
{
|
|
MemoryDevice_erasePage(self->memoryDevice, self->pageNumber);
|
|
}
|
|
MemoryDevice_write(self->memoryDevice, self->storage, self->memoryDevice->startAddress + self->memoryDevice->pageSize * self->pageNumber, self->cacheSize);
|
|
}
|
|
else
|
|
{
|
|
LOGGER_DEBUG(mainLog, "CachedStorage content unchanged, did not write");
|
|
}
|
|
|
|
self->dirty = false;
|
|
}
|
|
else
|
|
{
|
|
LOGGER_DEBUG(mainLog, "CachedStorage content unchanged, did not write");
|
|
}
|
|
}
|
|
}
|