Skip to content
Snippets Groups Projects
Unverified Commit 2a9e2b20 authored by Dimitrios Stoupis's avatar Dimitrios Stoupis
Browse files

Partially implemented memory limit check

parent b443c78d
No related branches found
No related tags found
No related merge requests found
......@@ -2,10 +2,18 @@
#define ECSS_SERVICES_MEMMANGSERVICE_HPP
// Memory limits definitions
#define FIRST_ADRESS_FLASH 0x08000000
#define LAST_ADDRESS_FLASH 0x0801FFFF // todo: Define the last memory address based on the MCU
#define FIRST_ADDRESS_SRAM 0x20000000
#define SRAM_MEMORY_SIZE 16 // Specify the RAM size in kBytes
#define DTCMRAM_LOWER_LIM 0x20000000UL
#define DTCMRAM_UPPER_LIM 0x20020000UL
#define ITCMRAM_LOWER_LIM 0x00000000UL
#define ITCMRAM_UPPER_LIM 0x00010000UL
#define RAM_D1_LOWER_LIM 0x24000000UL
#define RAM_D1_UPPER_LIM 0x24080000UL
#define RAM_D2_LOWER_LIM 0x30000000UL
#define RAM_D2_UPPER_LIM 0x30048000UL
#define RAM_D3_LOWER_LIM 0x38000000UL
#define RAM_D3_UPPER_LIM 0x38010000UL
#define FLASH_LOWER_LIM 0x08000000UL
#define FLASH_UPPER_LIM 0x08200000UL
#include "Service.hpp"
......@@ -16,9 +24,13 @@ class MemoryManagementService : public Service {
public:
// Memory type ID's
enum MemoryID {
RAM = 0,
FLASH = 1,
EXTERNAL = 2
DTCMRAM = 0,
RAM_D1,
RAM_D2,
RAM_D3,
ITCMRAM,
FLASH,
EXTERNAL
};
MemoryManagementService();
......@@ -34,6 +46,21 @@ public:
private:
MemoryManagementService &mainService; // Used to access main class's members
/**
* Check whether the provided address is valid or not, based on the defined limit values
*
* @param memId: The ID of the memory to check is passed
* @param address: Takes the address to be checked for validity
*/
bool addressValidator(MemoryManagementService::MemoryID memId, uint64_t address);
/**
* Check if the provided memory ID is valid
*
* @param memId: The memory ID for validation
*/
bool memoryIdValidator(MemoryManagementService::MemoryID memId);
public:
explicit RawDataMemoryManagement(MemoryManagementService &parent);
......
......@@ -31,19 +31,22 @@ void MemoryManagementService::RawDataMemoryManagement::loadRawData(Message &requ
uint8_t memoryID = request.readEnum8(); // Read the memory ID from the request
uint16_t iterationCount = request.readUint16(); // Get the iteration count
if (memoryID == MemoryManagementService::MemoryID::RAM) {
for (std::size_t j = 0; j < iterationCount; j++) {
uint64_t startAddress = request.readUint64(); // Start address of the memory
uint16_t dataLength = request.readOctetString(readData); // Data length to load
// todo: Error logging has to be included, if memory allocation above fails
// todo: Continue only if the checksum passes (when the checksum will be implemented)
for (std::size_t i = 0; i < dataLength; i++) {
*(reinterpret_cast<uint8_t *>(startAddress) + i) = readData[i];
// Check for a valid memory ID first
if (memoryIdValidator(MemoryManagementService::MemoryID(memoryID))) {
if (memoryID == MemoryManagementService::MemoryID::FLASH) {
// todo: Define FLASH specific access code when we transfer to embedded
} else {
for (std::size_t j = 0; j < iterationCount; j++) {
uint64_t startAddress = request.readUint64(); // Start address of the memory
uint16_t dataLength = request.readOctetString(readData); // Data length to load
// todo: Error logging has to be included, if memory allocation above fails
// todo: Continue only if the checksum passes (when the checksum will be implemented)
for (std::size_t i = 0; i < dataLength; i++) {
*(reinterpret_cast<uint8_t *>(startAddress) + i) = readData[i];
}
}
}
} else if (memoryID == MemoryManagementService::MemoryID::FLASH) {
// todo: Define FLASH specific access code when we transfer to embedded
}
}
......@@ -61,30 +64,41 @@ void MemoryManagementService::RawDataMemoryManagement::dumpRawData(Message &requ
uint8_t memoryID = request.readEnum8(); // Read the memory ID from the request
// todo: Add checks depending on the memory type
uint16_t iterationCount = request.readUint16(); // Get the iteration count
// Check for a valid memory ID first
if (memoryIdValidator(MemoryManagementService::MemoryID(memoryID))) {
uint16_t iterationCount = request.readUint16(); // Get the iteration count
// Append the data to report message
report.appendEnum8(memoryID); // Memory ID
report.appendUint16(iterationCount); // Iteration count
// Iterate N times, as specified in the command message
for (std::size_t j = 0; j < iterationCount; j++) {
uint64_t startAddress = request.readUint64(); // Data length to read
uint16_t readLength = request.readUint16(); // Start address for the memory read
// Append the data to report message
report.appendEnum8(memoryID); // Memory ID
report.appendUint16(iterationCount); // Iteration count
// Read memory data, an octet at a time
for (std::size_t i = 0; i < readLength; i++) {
readData[i] = *(reinterpret_cast<uint8_t *>(startAddress) + i);
// Iterate N times, as specified in the command message
for (std::size_t j = 0; j < iterationCount; j++) {
uint64_t startAddress = request.readUint64(); // Data length to read
uint16_t readLength = request.readUint16(); // Start address for the memory read
// Read memory data, an octet at a time, checking for a valid address first
if (addressValidator(MemoryManagementService::MemoryID(memoryID), startAddress) &&
addressValidator(MemoryManagementService::MemoryID(memoryID),
startAddress + readLength)) {
for (std::size_t i = 0; i < readLength; i++) {
readData[i] = *(reinterpret_cast<uint8_t *>(startAddress) + i);
}
// This part is repeated N-times (N = iteration count)
report.appendUint64(startAddress); // Start address
report.appendOctetString(readLength, readData); // Save the read data
} else {
/* Send wrong address failure report */
}
}
// todo: implement and append the checksum part of the reporting packet
// This part is repeated N-times (N = iteration count)
report.appendUint64(startAddress); // Start address
report.appendOctetString(readLength, readData); // Save the read data
mainService.storeMessage(report); // Save the report message
request.resetRead(); // Reset the reading count
} else {
/* Generate a false start report */
}
// todo: implement and append the checksum part of the reporting packet
mainService.storeMessage(report); // Save the report message
request.resetRead(); // Reset the reading count
}
void MemoryManagementService::RawDataMemoryManagement::checkRawData(Message &request) {
......@@ -128,3 +142,60 @@ void MemoryManagementService::RawDataMemoryManagement::checkRawData(Message &req
mainService.storeMessage(report); // Save the report message
request.resetRead(); // Reset the reading count
}
// Private function declaration section
bool MemoryManagementService::RawDataMemoryManagement::addressValidator(
MemoryManagementService::MemoryID memId, uint64_t address) {
bool validIndicator = false;
switch (memId) {
case MemoryManagementService::MemoryID::DTCMRAM:
if (address >= DTCMRAM_LOWER_LIM && address <= DTCMRAM_UPPER_LIM) {
validIndicator = true;
}
break;
case MemoryManagementService::MemoryID::ITCMRAM:
if (address >= ITCMRAM_LOWER_LIM && address <= ITCMRAM_UPPER_LIM) {
validIndicator = true;
}
break;
case MemoryManagementService::MemoryID::RAM_D1:
if (address >= RAM_D1_LOWER_LIM && address <= RAM_D1_UPPER_LIM) {
validIndicator = true;
}
break;
case MemoryManagementService::MemoryID::RAM_D2:
if (address >= RAM_D2_LOWER_LIM && address <= RAM_D2_UPPER_LIM) {
validIndicator = true;
}
break;
case MemoryManagementService::MemoryID::RAM_D3:
if (address >= RAM_D3_LOWER_LIM && address <= RAM_D3_UPPER_LIM) {
validIndicator = true;
}
break;
case MemoryManagementService::MemoryID::FLASH:
if (address >= FLASH_LOWER_LIM && address <= FLASH_UPPER_LIM) {
validIndicator = true;
}
break;
default:
validIndicator = true; // todo: Implemented so addresses from PC can be read. Remove.
break;
}
return validIndicator;
}
inline bool MemoryManagementService::RawDataMemoryManagement::memoryIdValidator(
MemoryManagementService::MemoryID memId) {
return (memId == MemoryManagementService::MemoryID::RAM_D1) ||
(memId == MemoryManagementService::MemoryID::RAM_D2) ||
(memId == MemoryManagementService::MemoryID::RAM_D3) ||
(memId == MemoryManagementService::MemoryID::DTCMRAM) ||
(memId == MemoryManagementService::MemoryID::ITCMRAM) ||
(memId == MemoryManagementService::MemoryID::FLASH) ||
(memId == MemoryManagementService::MemoryID::EXTERNAL);
}
......@@ -40,7 +40,7 @@ int main() {
MemoryManagementService memMangService;
Message rcvPack = Message(6, 5, Message::TC, 1);
rcvPack.appendEnum8(MemoryManagementService::MemoryID::RAM); // Memory ID
rcvPack.appendEnum8(MemoryManagementService::MemoryID::EXTERNAL); // Memory ID
rcvPack.appendUint16(3); // Iteration count
rcvPack.appendUint64(reinterpret_cast<uint64_t >(string)); // Start address
rcvPack.appendUint16(sizeof(string)/ sizeof(string[0])); // Data read length
......@@ -55,7 +55,7 @@ int main() {
rcvPack = Message(6, 2, Message::TC, 1);
uint8_t data[2] = {'h', 'R'};
rcvPack.appendEnum8(MemoryManagementService::MemoryID::RAM); // Memory ID
rcvPack.appendEnum8(MemoryManagementService::MemoryID::EXTERNAL); // Memory ID
rcvPack.appendUint16(2); // Iteration count
rcvPack.appendUint64(reinterpret_cast<uint64_t >(pStr)); // Start address
rcvPack.appendOctetString(2, data);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment