peary/hal/Board.hpp

Hardware Board Base Class. More…

Namespaces

Name
peary
peary::hal

Classes

Name
class peary::hal::Board Base class for a hardware board.

Defines

Name
MEM_PATH

Detailed Description

Hardware Board Base Class.

Copyright: Copyright (c) 2016-2025 CERN and the Peary Caribou authors. This software is distributed under the terms of the LGPL-3.0-only License, copied verbatim in the file “LICENSE.md”. SPDX-License-Identifier: LGPL-3.0-only

Macros Documentation

define MEM_PATH

  #define MEM_PATH "/dev/mem"
  

Source code

  
#pragma once

#include <map>
#include <memory>
#include <string>

#include "peary/hal/exceptions.hpp"
#include "peary/hal/Resources.hpp"
#include "peary/interfaces/Memory/memory.hpp"
#include "peary/utils/string_hash_map.hpp"

#define MEM_PATH "/dev/mem"

namespace peary::hal {

    class Board {
    public:
        Board() = default;

        virtual ~Board() = default;

        template <typename T> std::shared_ptr<T> getResource(const std::string& name) {
            const auto& comp = _resources.find(name);
            if(comp != _resources.end()) {
                try {
                    auto ptr = std::dynamic_pointer_cast<T>(comp->second);
                    if(ptr != nullptr) {
                        return ptr;
                    }
                } catch(...) {
                }
                throw InvalidResourceError(name, "Invalid resource type");
            }
            throw InvalidResourceError(name, "Resource not found");
        }

        // FIXME: temporarily putting this here awaiting to resolve how to introduce Platforms
        void writeMemory(utils::memory_map mem, uintptr_t value) { writeMemory(mem, 0, value); }

        void writeMemory(utils::memory_map mem, size_t offset, uintptr_t value) {
            interface::iface_mem& imem = interface::InterfaceManager::getInterface<interface::iface_mem>(
                interface::iface_mem::configuration_type(MEM_PATH, mem));
            imem.write(std::make_pair(offset, value));
        }

        uintptr_t readMemory(utils::memory_map mem) { return readMemory(mem, 0); }

        uintptr_t readMemory(utils::memory_map mem, size_t offset) {
            interface::iface_mem& imem = interface::InterfaceManager::getInterface<interface::iface_mem>(
                interface::iface_mem::configuration_type(MEM_PATH, mem));

            return imem.read(offset, 1).front();
        }

    protected:
        template <typename T, typename... Args> void register_resource(const std::string& name, Args&&... args) {
            if(_resources.find(name) != _resources.end()) {
                throw InvalidResourceError(name, "Resource already exists");
            }
            auto resource = std::make_shared<T>(std::forward<Args>(args)...);
            resource->setName(name);
            _resources.emplace(name, resource);
        }

        // Resource map of this board, to be populated by child classes
        utils::string_hash_map<std::shared_ptr<Resource>> _resources;
    };

} // namespace peary::hal
  

Updated on 2025-11-14 at 11:31:23 +0100