implemented memory device, flashStorage and cached storage

git-svn-id: https://svn.vbchaos.nl/svn/hsb/trunk@268 05563f52-14a8-4384-a975-3d1654cca0fa
This commit is contained in:
mmi
2017-10-31 07:35:17 +00:00
parent a50a10995f
commit 76783a6061
17 changed files with 1103 additions and 13 deletions

View File

@@ -24,6 +24,7 @@ ARFLAGS = rs
OBJECTS = \
ADCDevice.o \
CachedStorage.o \
CoverSolenoid.o \
DACDevice.o \
DisplayDevice.o \
@@ -33,6 +34,7 @@ IODevice.o \
KeyboardDevice.o \
Logger.o \
MAX5715.o \
MemoryDevice.o \
nhd0420.o \
Observable.o \
PID.o \

View File

@@ -0,0 +1,123 @@
// -----------------------------------------------------------------------------
/// @file CachedStorage.h
/// @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
// -----------------------------------------------------------------------------
/// @defgroup {group_name} {group_description}
/// Description
/// @file adc.h
/// @ingroup {group_name}
#ifndef _CACHEDEEPROM_H_
#define _CACHEDEEPROM_H_
// -----------------------------------------------------------------------------
// Include files
// -----------------------------------------------------------------------------
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "stm32f10x.h"
#include "platform.h"
// -----------------------------------------------------------------------------
// Constant and macro definitions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Type definitions.
// -----------------------------------------------------------------------------
struct CachedStorage
{
bool initialized;
unsigned int pageSize;
bool dirty;
unsigned int page;
struct MemoryDevice* memoryDevice;
uint8_t storage[CACHED_STORAGE_PAGESIZE];
uint8_t tempBuffer[CACHED_STORAGE_PAGESIZE];
};
// -----------------------------------------------------------------------------
// Function declarations
// -----------------------------------------------------------------------------
/**
* Initializes the EEPROM hardware and reads the flash page
*/
ErrorStatus CachedStorage_construct(struct CachedStorage* self, struct MemoryDevice* memoryDevice, unsigned int page, unsigned int pageSize);
/**
* Terminates the EEPROM hardware. SPI port is available again
*/
void CachedStorage_destruct(struct CachedStorage* self);
/**
* Writes one byte to the storage buffer
*/
void CachedStorage_writeByte(struct CachedStorage* self, int offset, uint8_t value);
/**
* Writes two bytes to the storage buffer
*/
void CachedStorage_writeHalfWord(struct CachedStorage* self, int offset, uint16_t value);
/**
* Writes four bytes to the storage buffer
*/
void CachedStorage_writeWord(struct CachedStorage* self, int offset, uint32_t value);
/**
* Writes binary data to the storage buffer
*/
void CachedStorage_writeBlob(struct CachedStorage* self, int offset, const void* blob, size_t blobSize);
/**
* Reads one byte from the storage buffer
*/
uint8_t CachedStorage_readByte(struct CachedStorage* self, int offset);
/**
* Reads two bytes from the storage buffer
*/
uint16_t CachedStorage_readHalfWord(struct CachedStorage* self, int offset);
/**
* Reads four bytes from the storage buffer
*/
uint32_t CachedStorage_readWord(struct CachedStorage* self, int offset);
/**
* Reads binary data from the storage buffer
*/
const void* CachedStorage_readBlob(struct CachedStorage* self, int offset);
/**
* Writes the storage buffer to EEPROM (only if the contents differ)
*/
void CachedStorage_commit(struct CachedStorage* self);
#endif

View File

@@ -0,0 +1,141 @@
// -----------------------------------------------------------------------------
/// @file MemoryDevice.h
/// @brief File description
// -----------------------------------------------------------------------------
// 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) 2015 Micro-Key bv
// -----------------------------------------------------------------------------
/// @defgroup {group_name} {group_description}
/// Description
/// @file MemoryDevice.h
/// @ingroup {group_name}
#ifndef INC_MEMORYDEVICE_H_
#define INC_MEMORYDEVICE_H_
// -----------------------------------------------------------------------------
// Include files
// -----------------------------------------------------------------------------
#include <stdbool.h>
#include "stm32f10x.h"
// -----------------------------------------------------------------------------
// Constant and macro definitions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Type definitions.
// -----------------------------------------------------------------------------
struct MemoryDevice;
typedef ErrorStatus (*MemoryReadFunction)(const struct MemoryDevice* self, uint32_t* buffer, uint32_t address, unsigned int length);
typedef ErrorStatus (*MemoryWriteFunction)(const struct MemoryDevice* self, uint32_t* buffer, uint32_t address, unsigned int length);
typedef ErrorStatus (*MemoryErasePageFunction)(const struct MemoryDevice* self, unsigned int page);
struct MemoryDevice
{
MemoryReadFunction _read;
MemoryWriteFunction _write;
MemoryErasePageFunction _erasePage;
uint32_t startAddress;
uint32_t endAddress;
};
// -----------------------------------------------------------------------------
// Function declarations
// -----------------------------------------------------------------------------
/** ----------------------------------------------------------------------------
* MemoryDevice_construct
* Description of function
*
* @param self
* @param startAddress
* @return ErrorStatus
*
* @todo
* -----------------------------------------------------------------------------
*/
extern ErrorStatus MemoryDevice_construct(struct MemoryDevice* self, uint32_t startAddress, uint32_t endAddress, MemoryReadFunction read, MemoryWriteFunction write, MemoryErasePageFunction erasePage);
/** ----------------------------------------------------------------------------
* MemoryDevice_destruct
* Description of function
*
* @param self
* @param
* @return void
*
* @todo
* -----------------------------------------------------------------------------
*/
extern void MemoryDevice_destruct(struct MemoryDevice* self);
/** ----------------------------------------------------------------------------
* MemoryDevice_read
* Description of function
*
* @param self
* @param buffer
* @param address
* @param length
*
* @return ErrorStatus
*
* @todo
* -----------------------------------------------------------------------------
*/
extern ErrorStatus MemoryDevice_write(const struct MemoryDevice* self, uint32_t* buffer, uint32_t address, unsigned int length);
/** ----------------------------------------------------------------------------
* MemoryDevice_read
* Description of function
*
* @param self
* @param buffer
* @param address
* @param length
*
* @return ErrorStatus
*
* @todo
* -----------------------------------------------------------------------------
*/
extern ErrorStatus MemoryDevice_read(const struct MemoryDevice* self, uint32_t* buffer, uint32_t address, unsigned int length);
/** ----------------------------------------------------------------------------
* MemoryDevice_erasePage
* Description of function
*
* @param self
* @param page
* @return ErrorStatus
*
* @todo
* -----------------------------------------------------------------------------
*/
extern ErrorStatus MemoryDevice_erasePage(struct MemoryDevice* self, unsigned int page);
#endif /* INC_MEMORYDEVICE_H_ */

View File

@@ -0,0 +1,271 @@
// -----------------------------------------------------------------------------
/// @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 page, unsigned int pageSize)
{
ErrorStatus returnValue = SUCCESS;
if (!self->initialized)
{
self->page = page;
self->dirty = false;
if (returnValue == SUCCESS)
{
self->memoryDevice = memoryDevice;
}
if (returnValue == SUCCESS)
{
MemoryDevice_read(self->memoryDevice, (uint32_t*)self->storage, (self->memoryDevice->startAddress + (self->page * self->pageSize)), self->pageSize);
self->initialized = true;
}
}
return returnValue;
}
void CachedStorage_destruct(struct CachedStorage* self)
{
if (self->initialized)
{
self->initialized = false;
}
}
void CachedStorage_writeByte(struct CachedStorage* self, int offset, uint8_t value)
{
if (self->initialized)
{
if(offset < CACHED_STORAGE_PAGESIZE)
{
if(value != self->storage[offset])
{
self->storage[offset] = value;
self->dirty = true;
}
}
}
}
void CachedStorage_writeHalfWord(struct CachedStorage* self, int offset, uint16_t _value)
{
if (self->initialized)
{
if(offset < CACHED_STORAGE_PAGESIZE - 1)
{
uint16_t value = _value;
if((value & 0xFF) != self->storage[offset + 1])
{
self->storage[offset + 1] = value & 0xFF;
self->dirty = true;
}
value >>= 8;
if((value & 0xFF) != self->storage[offset])
{
self->storage[offset] = value & 0xFF;
self->dirty = true;
}
}
}
}
void CachedStorage_writeWord(struct CachedStorage* self, int offset, uint32_t _value)
{
if (self->initialized)
{
if(offset < CACHED_STORAGE_PAGESIZE - 3)
{
uint32_t value = _value;
if((value & 0xFF) != self->storage[offset + 3])
{
self->storage[offset + 3] = value & 0xFF;
self->dirty = true;
}
value >>= 8;
if((value & 0xFF) != self->storage[offset + 2])
{
self->storage[offset + 2] = value & 0xFF;
self->dirty = true;
}
value >>= 8;
if((value & 0xFF) != self->storage[offset + 1])
{
self->storage[offset + 1] = value & 0xFF;
self->dirty = true;
}
value >>= 8;
if((value & 0xFF) != self->storage[offset])
{
self->storage[offset] = value & 0xFF;
self->dirty = true;
}
}
}
}
void CachedStorage_writeBlob(struct CachedStorage* self, int offset, const void* blob, size_t blobSize)
{
if (self->initialized)
{
if(offset + blobSize <= CACHED_STORAGE_PAGESIZE)
{
memcpy(&self->storage[offset], blob, blobSize);
self->dirty = true;
}
}
}
uint8_t CachedStorage_readByte(struct CachedStorage* self, int offset)
{
if (self->initialized)
{
if(offset < CACHED_STORAGE_PAGESIZE)
{
return self->storage[offset];
}
else
{
return 0;
}
}
}
uint16_t CachedStorage_readHalfWord(struct CachedStorage* self, int offset)
{
if (self->initialized)
{
if(offset < CACHED_STORAGE_PAGESIZE - 1)
{
return self->storage[offset + 1] | (self->storage[offset] << 8);
}
else
{
return 0;
}
}
}
uint32_t CachedStorage_readWord(struct CachedStorage* self, int offset)
{
if (self->initialized)
{
if(offset < CACHED_STORAGE_PAGESIZE - 3)
{
return self->storage[offset + 3] | (self->storage[offset + 2] << 8) | (self->storage[offset + 1] << 16) | (self->storage[offset] << 24);
}
else
{
return 0;
}
}
}
const void* CachedStorage_readBlob(struct CachedStorage* self, int offset)
{
if (self->initialized)
{
if(offset < CACHED_STORAGE_PAGESIZE)
{
return &self->storage[offset];
}
else
{
return NULL;
}
}
}
void CachedStorage_commit(struct CachedStorage* self)
{
if (self->initialized)
{
if(self->dirty)
{
MemoryDevice_read(self->memoryDevice, (uint32_t*)self->tempBuffer, (self->memoryDevice->startAddress + (self->page * self->pageSize)), self->pageSize);
if(memcmp(self->tempBuffer, self->storage, CACHED_STORAGE_PAGESIZE) != 0)
{
MemoryDevice_write(self->memoryDevice, (uint32_t*)self->storage, (self->memoryDevice->startAddress + (self->page * 0x800)), 20);
}
else
{
LOGGER_DEBUG(mainLog, "CachedStorage content on page %d unchanged, did not write", self->page);
}
self->dirty = false;
}
else
{
LOGGER_DEBUG(mainLog, "CachedStorage content on page %d unchanged, did not write", self->page);
}
}
}

View File

@@ -0,0 +1,115 @@
// -----------------------------------------------------------------------------
/// @file MemoryDevice.c
/// @brief Description
// -----------------------------------------------------------------------------
// 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 MemoryDevice.c
/// @ingroup {group_name}
// -----------------------------------------------------------------------------
// Include files
// -----------------------------------------------------------------------------
#include "stdio.h"
#include "MemoryDevice.h"
// -----------------------------------------------------------------------------
// Constant and macro definitions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Type definitions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// File-scope variables
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Function declarations
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Function definitions
// -----------------------------------------------------------------------------
ErrorStatus MemoryDevice_construct(struct MemoryDevice* self, uint32_t startAddress, uint32_t endAddress, MemoryReadFunction read, MemoryWriteFunction write, MemoryErasePageFunction erasePage)
{
ErrorStatus returnValue = SUCCESS;
self->_read = read;
self->_write = write;
self->_erasePage = erasePage;
self->startAddress = startAddress;
self->endAddress = endAddress;
return returnValue;
}
void MemoryDevice_destruct(struct MemoryDevice* self)
{
self->_read = NULL;
self->_write = NULL;
}
ErrorStatus MemoryDevice_write(const struct MemoryDevice* self, uint32_t* buffer, uint32_t address, unsigned int length)
{
ErrorStatus returnValue = SUCCESS;
if (self->_write != NULL)
{
returnValue = self->_write(self, buffer, address, length);
}
return returnValue;
}
ErrorStatus MemoryDevice_read(const struct MemoryDevice* self, uint32_t* buffer, uint32_t address, unsigned int length)
{
ErrorStatus returnValue = SUCCESS;
if (self->_read != NULL)
{
returnValue = self->_read(self, buffer, address, length);
}
return returnValue;
}
ErrorStatus MemoryDevice_erasePage(struct MemoryDevice* self, unsigned int page)
{
{
ErrorStatus returnValue = SUCCESS;
if (self->_erasePage != NULL)
{
returnValue = self->_erasePage(self, page);
}
return returnValue;
}
}