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

@@ -27,6 +27,7 @@ stm32f10x_it.o \
CathodeMCP.o \
gpio.o \
internalADC.o \
InternalFlash.o \
keypadMatrix.o \
oli_stm32_h107.o \
PCBA.o \

View File

@@ -0,0 +1,144 @@
// -----------------------------------------------------------------------------
/// @file InternalFlash.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 InternalFlash.h
/// @ingroup {group_name}
#ifndef INC_INTERNALFLASH_H_
#define INC_INTERNALFLASH_H_
// -----------------------------------------------------------------------------
// Include files
// -----------------------------------------------------------------------------
#include <stdbool.h>
#include "stm32f10x.h"
#include "stm32f10x_flash.h"
#include "MemoryDevice.h"
// -----------------------------------------------------------------------------
// Constant and macro definitions
// -----------------------------------------------------------------------------
#define INTERNAL_FLASH_BASE_ADDRESS ((uint32_t)0x08000000)
#define INTERNAL_FLASH_PAGE_SIZE ((uint32_t)0x800)
#define INTERNAL_FLASH_NUMBER_OF_PAGES ((uint32_t)0x80)
// -----------------------------------------------------------------------------
// Type definitions.
// -----------------------------------------------------------------------------
struct InternalFlash
{
struct MemoryDevice memoryDevice;
bool initialized;
uint32_t startAddress;
uint32_t endAddress;
uint32_t pageSize;
};
// -----------------------------------------------------------------------------
// Function declarations
// -----------------------------------------------------------------------------
/** ----------------------------------------------------------------------------
* InternalFlash_construct
* Description of function
*
* @param self
*
* @return ErrorStatus
*
* @todo
* -----------------------------------------------------------------------------
*/
extern ErrorStatus InternalFlash_construct(struct InternalFlash* self);
/** ----------------------------------------------------------------------------
* InternalFlash_destruct
* Description of function
*
* @param self
* @param
* @return void
*
* @todo
* -----------------------------------------------------------------------------
*/
extern void InternalFlash_destruct(struct InternalFlash* self);
/** ----------------------------------------------------------------------------
* InternalFlash_write
* Description of function
*
* @param self
* @param buffer
* @param address
* @param length
*
* @return ErrorStatus
*
* @todo
* -----------------------------------------------------------------------------
*/
extern ErrorStatus InternalFlash_write(const struct InternalFlash* self, uint32_t* buffer, uint32_t address, unsigned int length);
/** ----------------------------------------------------------------------------
* InternalFlash_read
* Description of function
*
* @param self
* @param buffer
* @param address
* @param length
*
* @return ErrorStatus
*
* @todo
* -----------------------------------------------------------------------------
*/
extern ErrorStatus InternalFlash_read(const struct InternalFlash* self, uint32_t* buffer, uint32_t address, unsigned int length);
/** ----------------------------------------------------------------------------
* InternalFlash_erasePage
* Description of function
*
* @param self
* @param page
*
* @param length
*
* @return ErrorStatus
*
* @todo
* -----------------------------------------------------------------------------
*/
extern ErrorStatus InternalFlash_erasePage(const struct InternalFlash* self, unsigned int page);
#endif /* INC_INTERNALFLASH_H_ */

View File

@@ -49,7 +49,7 @@
// Constant and macro definitions
// -----------------------------------------------------------------------------
#define CACHED_STORAGE_PAGESIZE ((uint16_t)0x200)
// -----------------------------------------------------------------------------
// Type definitions.
@@ -79,6 +79,8 @@ extern struct SpiDevice* const spiEEPROM;
// Export of Keypad
extern struct Keypad* const keypad;
extern struct Storm700* const storm700;
// internal FLASH
extern struct InternalFlash* const iFlash;
// Export of GPIOs
extern struct Gpio* const ledGreen;
extern struct Gpio* const ledOrange;

View File

@@ -0,0 +1,241 @@
// -----------------------------------------------------------------------------
/// @file InternalFlash.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 InternalFlash.c
/// @ingroup {group_name}
// -----------------------------------------------------------------------------
// Include files
// -----------------------------------------------------------------------------
#include "Logger.h"
#include "MemoryDevice.h"
#include "InternalFlash.h"
// -----------------------------------------------------------------------------
// Constant and macro definitions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Type definitions
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// File-scope variables
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Function declarations
// -----------------------------------------------------------------------------
static ErrorStatus read(const struct MemoryDevice* self, uint32_t* buffer, uint32_t address, unsigned int length);
static ErrorStatus write(const struct MemoryDevice* self, uint32_t* buffer, uint32_t address, unsigned int length);
static ErrorStatus erasePage(const struct MemoryDevice* self, unsigned int page);
// -----------------------------------------------------------------------------
// Function definitions
// -----------------------------------------------------------------------------
ErrorStatus InternalFlash_construct(struct InternalFlash* self)
{
ErrorStatus returnValue = SUCCESS;
if (!self->initialized)
{
if (returnValue == SUCCESS)
{
returnValue = MemoryDevice_construct(&self->memoryDevice, INTERNAL_FLASH_BASE_ADDRESS, (INTERNAL_FLASH_BASE_ADDRESS + (INTERNAL_FLASH_NUMBER_OF_PAGES * INTERNAL_FLASH_PAGE_SIZE)), read, write, erasePage);
}
if (returnValue == SUCCESS)
{
self->startAddress = INTERNAL_FLASH_BASE_ADDRESS;
self->endAddress = (INTERNAL_FLASH_BASE_ADDRESS + (INTERNAL_FLASH_NUMBER_OF_PAGES * INTERNAL_FLASH_PAGE_SIZE));
self->pageSize = INTERNAL_FLASH_PAGE_SIZE;
self->initialized = true;
}
}
else
{
returnValue = ERROR;
}
return returnValue;
}
void InternalFlash_destruct(struct InternalFlash* self)
{
if (self->initialized)
{
MemoryDevice_destruct(&self->memoryDevice);
self->initialized = false;
}
}
ErrorStatus InternalFlash_write(const struct InternalFlash* self, uint32_t* buffer, uint32_t address, unsigned int length)
{
ErrorStatus returnValue = SUCCESS;
FLASH_Status FLASHStatus = FLASH_COMPLETE;
uint32_t _address = address;
uint32_t _endAddress = address + (length * 4);
if (self->initialized)
{
if (returnValue == SUCCESS)
{
// Verify start address boundaries
if ((_address < self->startAddress) && (_address >= self->endAddress))
{
// Start address is NOT OK
returnValue = ERROR;
}
}
if (returnValue == SUCCESS)
{
// Verify end address boundaries
if ((_endAddress >= self->endAddress))
{
// End address is NOT OK
returnValue = ERROR;
}
}
// Boundaries OK - Write to FLASH
if (returnValue == SUCCESS)
{
// Unlock the FLASH bank
FLASH_Unlock();
// Loop writing until end address is reached
int bufferIndex = 0;
while((_address < _endAddress) && (FLASHStatus == FLASH_COMPLETE))
{
FLASHStatus = FLASH_ProgramWord(_address, buffer[bufferIndex++]);
// 32bit data register requires increment by 4 for next word address
_address = _address + 4;
}
// After programming, lock the FLASH
FLASH_Lock();
}
}
else
{
returnValue = ERROR;
LOGGER_ERROR(mainLog, "BOEH");
}
return returnValue;
}
ErrorStatus InternalFlash_read(const struct InternalFlash* self, uint32_t* buffer, uint32_t address, unsigned int length)
{
ErrorStatus returnValue = SUCCESS;
uint32_t _address = address;
uint32_t _endAddress = address + (length * 4);
if (self->initialized)
{
if (returnValue == SUCCESS)
{
// Verify start address boundaries
if ((_address < self->startAddress) && (_address >= self->endAddress))
{
// Start address is NOT OK
returnValue = ERROR;
}
}
if (returnValue == SUCCESS)
{
// Verify end address boundaries
if ((_endAddress >= self->endAddress))
{
// End address is NOT OK
returnValue = ERROR;
}
}
// Boundaries OK - Read from FLASH
if (returnValue == SUCCESS)
{
// Loop reading until end address is reached
int bufferIndex = 0;
while(_address < _endAddress)
{
buffer[bufferIndex++] = *(uint32_t*) _address;
// 32bit data register requires increment by 4 for next word address
_address = _address + 4;
}
}
}
else
{
returnValue = ERROR;
}
return returnValue;
}
ErrorStatus InternalFlash_erasePage(const struct InternalFlash* self, unsigned int page)
{
ErrorStatus returnValue = SUCCESS;
FLASH_Status FLASHStatus;
if (self->initialized)
{
// Unlock the FLASH bank
FLASH_Unlock();
FLASHStatus = FLASH_ErasePage(self->startAddress + (self->pageSize * page));
// After programming, lock the FLASH
FLASH_Lock();
if (FLASHStatus != FLASH_COMPLETE)
{
returnValue = ERROR;
}
}
else
{
returnValue = ERROR;
}
return returnValue;
}
static ErrorStatus read(const struct MemoryDevice* self, uint32_t* buffer, uint32_t address, unsigned int length)
{
return InternalFlash_read((const struct InternalFlash*)self, buffer, address, length);
}
static ErrorStatus write(const struct MemoryDevice* self, uint32_t* buffer, uint32_t address, unsigned int length)
{
return InternalFlash_write((const struct InternalFlash*)self, buffer, address, length);
}
static ErrorStatus erasePage(const struct MemoryDevice* self, unsigned int page)
{
return InternalFlash_erasePage((const struct InternalFlash*)self, page);
}

View File

@@ -43,6 +43,7 @@
#include "gpio.h"
#include "Interlock.h"
#include "internalADC.h"
#include "InternalFlash.h"
#include "keypadMatrix.h"
#include "MAX5715.h"
#include "nhd0420.h"
@@ -131,6 +132,9 @@ static struct SpiDevice _spiEEPROM = {.initialized = false};
static struct Keypad _keypad = {.initialized = false};
static struct Storm700 _storm700 = {.initialized = false};
// Interal FLASH
static struct InternalFlash _iFlash = {.initialized = false};
// GPIOs
static struct Gpio _ledGreen = {.initialized = false};
static struct Gpio _ledOrange = {.initialized = false};
@@ -182,6 +186,8 @@ struct SpiParameters* const spiEEPROMParam = &_spi3EEPROMParameters;
struct Keypad* const keypad = &_keypad;
struct Storm700* const storm700 = &_storm700;
struct InternalFlash* const iFlash = &_iFlash;
struct Gpio* const ledGreen = &_ledGreen;
struct Gpio* const ledOrange = &_ledOrange;
@@ -602,6 +608,7 @@ static ErrorStatus initPeriphery(void)
IRQ_setInterruptProperties(RTC_IRQn, 13, 0, ENABLE);
RTC_construct(rtc);
/* --------------------------------------------------------------------*/
/* USART1 */
/* --------------------------------------------------------------------*/
@@ -690,6 +697,13 @@ static ErrorStatus initPeriphery(void)
IRQ_setInterruptProperties(EXTI9_5_IRQn, 12, 12, ENABLE);
/* --------------------------------------------------------------------*/
/* INTERNAL FLASH MEMORY */
/* --------------------------------------------------------------------*/
InternalFlash_construct(iFlash);
/* --------------------------------------------------------------------*/
/* GPIOs */
/* --------------------------------------------------------------------*/