Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 7 | * not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 14 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #ifndef MAILBOX_HPP |
| 20 | #define MAILBOX_HPP |
| 21 | |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 22 | #include <cstddef> |
Kristofer Jonsson | 73ebc48 | 2020-10-12 11:17:04 +0200 | [diff] [blame] | 23 | #include <cstdint> |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 24 | #include <list> |
| 25 | |
| 26 | namespace Mailbox { |
| 27 | |
Jonny Svärd | 9fc527b | 2020-11-16 16:18:07 +0100 | [diff] [blame^] | 28 | /** |
| 29 | * The Mailbox parent class |
| 30 | * |
| 31 | * Intended to be implemented by a driver subclass, see MHU_v2 driver as example. |
| 32 | */ |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 33 | class Mailbox { |
| 34 | public: |
Jonny Svärd | 9fc527b | 2020-11-16 16:18:07 +0100 | [diff] [blame^] | 35 | /** |
| 36 | * Constructor/Destructor |
| 37 | */ |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 38 | Mailbox(); |
| 39 | virtual ~Mailbox(); |
Jonny Svärd | 9fc527b | 2020-11-16 16:18:07 +0100 | [diff] [blame^] | 40 | |
| 41 | /** |
| 42 | * Intended to trigger an interrupt to an external block |
| 43 | * MUST be implemented by subclass. |
| 44 | */ |
| 45 | virtual bool sendMessage() = 0; |
| 46 | |
| 47 | /** |
| 48 | * Intended to be called when Cortex M has received an |
| 49 | * interrupt/event from mailbox/mhu. If an interrupt needs to be cleared, |
| 50 | * this is a good place to do that. MUST call notify(). |
| 51 | * MUST be implemented by subclass. |
| 52 | */ |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 53 | virtual void handleMessage() = 0; |
Jonny Svärd | 9fc527b | 2020-11-16 16:18:07 +0100 | [diff] [blame^] | 54 | |
| 55 | /** |
| 56 | * Can be used to verify that hardware versions match expected versions |
| 57 | * CAN be implemented by subclass, optional. Default impl returns true. |
| 58 | */ |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 59 | virtual bool verifyHardware(); |
Jonny Svärd | 9fc527b | 2020-11-16 16:18:07 +0100 | [diff] [blame^] | 60 | |
| 61 | /** |
| 62 | * Function signature for callbacks |
| 63 | */ |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 64 | typedef void (*CallbackFptr)(void *userArg); |
Jonny Svärd | 9fc527b | 2020-11-16 16:18:07 +0100 | [diff] [blame^] | 65 | |
| 66 | /** |
| 67 | * Register a callback to be called when a message is received. |
| 68 | */ |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 69 | void registerCallback(CallbackFptr callback, void *userArg); |
Jonny Svärd | 9fc527b | 2020-11-16 16:18:07 +0100 | [diff] [blame^] | 70 | |
| 71 | /** |
| 72 | * Remove a specific callback from the callback list. |
| 73 | */ |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 74 | void deregisterCallback(CallbackFptr callback, void *userArg); |
| 75 | |
| 76 | protected: |
Jonny Svärd | 9fc527b | 2020-11-16 16:18:07 +0100 | [diff] [blame^] | 77 | /** |
| 78 | * Calls every registered callback when a message has been received. |
| 79 | */ |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 80 | void notify(); |
Jonny Svärd | 9fc527b | 2020-11-16 16:18:07 +0100 | [diff] [blame^] | 81 | |
| 82 | /** |
| 83 | * Helper functions |
| 84 | */ |
Jonny Svärd | 44398c8 | 2020-10-06 14:18:28 +0200 | [diff] [blame] | 85 | uint32_t read32(volatile uint32_t *baseAddr, const uint32_t offset); |
| 86 | void write32(volatile uint32_t *baseAddr, const uint32_t offset, const uint32_t value); |
| 87 | |
| 88 | private: |
| 89 | struct Callback { |
| 90 | bool operator==(const Callback &b) const; |
| 91 | CallbackFptr callback; |
| 92 | void *userArg; |
| 93 | }; |
| 94 | |
| 95 | std::list<Callback> callbacks; |
| 96 | }; |
| 97 | |
| 98 | } // namespace Mailbox |
| 99 | |
| 100 | #endif |