diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/Makefile b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/Makefile index a750278..8d0fc0f 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/Makefile +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/Makefile @@ -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 \ diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/CachedStorage.h b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/CachedStorage.h new file mode 100644 index 0000000..737cc7f --- /dev/null +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/CachedStorage.h @@ -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 +#include +#include + +#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 diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/MemoryDevice.h b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/MemoryDevice.h new file mode 100644 index 0000000..58397f3 --- /dev/null +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/inc/MemoryDevice.h @@ -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 + +#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_ */ diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/CachedStorage.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/CachedStorage.c new file mode 100644 index 0000000..1f8e144 --- /dev/null +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/CachedStorage.c @@ -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 +#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); + } + } +} diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/MemoryDevice.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/MemoryDevice.c new file mode 100644 index 0000000..2fa9639 --- /dev/null +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/HAL/src/MemoryDevice.c @@ -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; + } +} diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/Makefile b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/Makefile index 3c9183d..5c1bac3 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/Makefile +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/Makefile @@ -27,6 +27,7 @@ stm32f10x_it.o \ CathodeMCP.o \ gpio.o \ internalADC.o \ +InternalFlash.o \ keypadMatrix.o \ oli_stm32_h107.o \ PCBA.o \ diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/InternalFlash.h b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/InternalFlash.h new file mode 100644 index 0000000..994d3f0 --- /dev/null +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/InternalFlash.h @@ -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 + +#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_ */ diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/platform.h b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/platform.h index 81479ee..3e1f353 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/platform.h +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/inc/platform.h @@ -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; diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/InternalFlash.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/InternalFlash.c new file mode 100644 index 0000000..71ebf91 --- /dev/null +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/InternalFlash.c @@ -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); +} diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/oli_stm32_h107.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/oli_stm32_h107.c index d9d2ac7..ab7fa1f 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/oli_stm32_h107.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/Platform/src/oli_stm32_h107.c @@ -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 */ /* --------------------------------------------------------------------*/ diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/.settings/language.settings.xml b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/.settings/language.settings.xml index b045136..695e355 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/.settings/language.settings.xml +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/.settings/language.settings.xml @@ -6,7 +6,7 @@ - + @@ -18,7 +18,7 @@ - + diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/linker/LinkerScript.ld b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/linker/LinkerScript.ld index 2de5144..50b754b 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/linker/LinkerScript.ld +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/linker/LinkerScript.ld @@ -62,7 +62,7 @@ _Min_Stack_Size = 0x400; /* required amount of stack */ MEMORY { RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K - ROM (rx) : ORIGIN = 0x8000000, LENGTH = 256K + ROM (rx) : ORIGIN = 0x8000000, LENGTH = 254K } /* Sections */ diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/DAConverter.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/DAConverter.c index b7d52c6..76ea669 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/DAConverter.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/DAConverter.c @@ -25,6 +25,8 @@ // Include files // ----------------------------------------------------------------------------- +#include + #include "DAConverter.h" #include "Logger.h" @@ -91,7 +93,7 @@ extern ErrorStatus DAConverter_setOutputVoltage(const struct DAConverter* self, uint32_t dacValue; dacValue = calculateDACValue(self, voltage); DACDevice_write(self->dacDevice, dacValue); - LOGGER_DEBUG(mainLog, "Voltage %d --- value %X", voltage, dacValue); + LOGGER_DEBUG(mainLog, "Voltage %d --- value %X", voltage, (unsigned int)dacValue); } } else diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/hsb-mrts.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/hsb-mrts.c index 455e280..6668e8e 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/hsb-mrts.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/hsb-mrts.c @@ -25,6 +25,7 @@ // Include files // ----------------------------------------------------------------------------- +#include #include #include "hsb-mrts.h" diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c index 066aec6..f9fae19 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/main.c @@ -47,6 +47,7 @@ #include "misc.h" #include "stm32f10x_rcc.h" +#include "CachedStorage.h" #include "DisplayDevice.h" #include "KeyboardDevice.h" #include "MAX5715.h" @@ -56,10 +57,12 @@ #include "CathodeMCP.h" #include "Interlock.h" #include "internalADC.h" +#include "InternalFlash.h" #include "gpio.h" #include "IODevice.h" #include "keypadMatrix.h" #include "Logger.h" +#include "MemoryDevice.h" #include "PCBA.h" #include "uart.h" #include "spi.h" @@ -102,6 +105,7 @@ static struct HwValidationMenuItems hwTestItems; struct HwValidationMenu* hwValidation = &_hwValidation; +static struct CachedStorage cs = {.initialized = false}; // ----------------------------------------------------------------------------- // Function declarations @@ -125,7 +129,7 @@ int main (void) ledTaskArguments.led = ledOrange; ledTaskArguments.frequency = 1; - xTaskCreate(initTask, (const char* const)"initTask", 1024, NULL, 5, &initTaskHandle); + xTaskCreate(initTask, (const char* const)"initTask", 2048, NULL, 5, &initTaskHandle); /* Start the scheduler. */ vTaskStartScheduler(); @@ -199,17 +203,17 @@ static void initTask(void* parameters) xTaskCreate(ledBlinkTask, (const char* const)"ledTask", 100, &ledTaskArguments, 0, &ledTaskHandle); // Construct the displays - Displays_construct(); +// Displays_construct(); // Construct the AD Converters - ADConverters_construct(); +// ADConverters_construct(); // Construct the DA Converters - DAConverters_construct(); +// DAConverters_construct(); - hsb_generateStartScreen(mainDisplay); +// hsb_generateStartScreen(mainDisplay); // Let start screen stay for 5 seconds - vTaskDelay(INIT_START_SCREEN_DELAY); +// vTaskDelay(INIT_START_SCREEN_DELAY); ///TODO MUST BE UPDATED @@ -232,7 +236,35 @@ static void initTask(void* parameters) // HwValidationMenu_construct(hwValidation, &uart1->device, &hwTestItems, 1, 1024); // Construct the repair menu - repairMenus_construct(); +// repairMenus_construct(); + + + uint32_t buffer[128]; + int i; + for (i = 0; i < 128; i++) + { + buffer[i] = i + 4; + vTaskDelay(20); + } +// +// vTaskDelay(5000); +// + CachedStorage_construct(&cs, &iFlash->memoryDevice, 127, CACHED_STORAGE_PAGESIZE); + + MemoryDevice_erasePage(&iFlash->memoryDevice, 127); + + CachedStorage_writeBlob(&cs, 0, buffer, sizeof(buffer)); + CachedStorage_commit(&cs); +//// +// vTaskDelay(1000); +// + const uint32_t* buffer2; + buffer2 = CachedStorage_readBlob(&cs, 0); + for (i = 0; i < 128; i++) + { + LOGGER_DEBUG(mainLog, "Value %d ---> %d", i, (unsigned int)buffer2[i]); + vTaskDelay(20); + } // Create task that repeats to print out TASK information on the logger xTaskCreate(printSystemInfoTask, (const char* const)"SysInfoTask", 512, NULL, 0, &sysTaskHandle); diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/repairMenu.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/repairMenu.c index 393e0ee..5254bc5 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/repairMenu.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/repairMenu.c @@ -922,6 +922,7 @@ static ErrorStatus repairMenu_createMenu(struct RepairMenu* self) repairMenu_addKeyAction_SCROLLUP(&self->menuArray[RM_PRESET_PRINT], 'U', PRESSED); repairMenu_addKeyAction_SCROLLDOWN(&self->menuArray[RM_PRESET_PRINT], 'D', PRESSED); repairMenu_addKeyAction_GOTOSTATE(&self->menuArray[RM_PRESET_PRINT], 'X', PRESSED, PRESETMENU); + repairMenu_addKeyAction_GOTOSTATE(&self->menuArray[PRESETMENU], 'L', PRESSED, PRESETMENU); repairMenu_createMenuPage(&self->menuArray[REPAIR_RUNNING], MENU_HAS_NO_CURSOR, 4); repairMenu_addKeyAction_GOTOSTATE(&self->menuArray[REPAIR_RUNNING], 'X', PRESSED, REPAIR_ASK_PAUSE); diff --git a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/repairProcess.c b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/repairProcess.c index 5848a34..bc0e8b9 100644 --- a/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/repairProcess.c +++ b/S - Software/0 - HSB MRTS Kathode-MCP/3 - Implementation/0 - Code/hsb-mrts/src/repairProcess.c @@ -229,7 +229,7 @@ static void repairProcess_task(void* parameters) // The signal profile is identical for all rows in the regulation process SignalProfileGenerator_calculate(&self->signalProfileGenerator); - LOGGER_DEBUG(mainLog, "Signal: %d, TimeToRemain %d", self->signalProfileGenerator.signal, SignalProfileGenerator_getRemainingTime(&self->signalProfileGenerator)); + LOGGER_DEBUG(mainLog, "Signal: %d, TimeToRemain %d", self->signalProfileGenerator.signal, (unsigned int)SignalProfileGenerator_getRemainingTime(&self->signalProfileGenerator)); // Check for correct signal // if (self->signalProfileGenerator.signal >= 0)