alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 2 | * Copyright (c) 2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 17 | #include "hal.h" /* API */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 18 | |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 19 | #include "platform_drivers.h" /* Platform drivers */ |
| 20 | #include "log_macros.h" /* Logging macros */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <assert.h> |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 24 | #include <string.h> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 25 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 26 | int hal_init(hal_platform* platform, data_acq_module* data_acq, |
| 27 | data_psn_module* data_psn, platform_timer* timer) |
| 28 | { |
| 29 | assert(platform && data_acq && data_psn); |
| 30 | |
| 31 | platform->data_acq = data_acq; |
| 32 | platform->data_psn = data_psn; |
| 33 | platform->timer = timer; |
Kshitij Sisodia | a1256e3 | 2022-02-23 14:40:45 +0000 | [diff] [blame] | 34 | platform->platform_init = platform_init; |
| 35 | platform->platform_release = platform_release; |
| 36 | platform_name(platform->plat_name, sizeof(platform->plat_name)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @brief Local helper function to clean the slate for current platform. |
| 43 | **/ |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 44 | static void hal_platform_clear(hal_platform* platform) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 45 | { |
| 46 | assert(platform); |
| 47 | platform->inited = 0; |
| 48 | } |
| 49 | |
| 50 | int hal_platform_init(hal_platform* platform) |
| 51 | { |
| 52 | int state; |
| 53 | assert(platform && platform->platform_init); |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 54 | hal_platform_clear(platform); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 55 | |
| 56 | /* Initialise platform */ |
| 57 | if (0 != (state = platform->platform_init())) { |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 58 | printf_err("Failed to initialise platform %s\n", platform->plat_name); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 59 | return state; |
| 60 | } |
| 61 | |
| 62 | /* Initialise the data acquisition module */ |
| 63 | if (0 != (state = data_acq_channel_init(platform->data_acq))) { |
| 64 | if (!platform->data_acq->inited) { |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 65 | printf_err("Failed to initialise data acq module: %s\n", |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 66 | platform->data_acq->system_name); |
| 67 | } |
| 68 | hal_platform_release(platform); |
| 69 | return state; |
| 70 | } |
| 71 | |
| 72 | /* Initialise the presentation module */ |
| 73 | if (0 != (state = data_psn_system_init(platform->data_psn))) { |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 74 | printf_err("Failed to initialise data psn module: %s\n", |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 75 | platform->data_psn->system_name); |
| 76 | data_acq_channel_release(platform->data_acq); |
| 77 | hal_platform_release(platform); |
| 78 | return state; |
| 79 | } |
| 80 | |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 81 | /* Followed by the timer module */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 82 | init_timer(platform->timer); |
| 83 | |
| 84 | info("%s platform initialised\n", platform->plat_name); |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 85 | debug("Using %s module for data acquisition\n", |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | platform->data_acq->system_name); |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 87 | debug("Using %s module for data presentation\n", |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 88 | platform->data_psn->system_name); |
| 89 | |
| 90 | platform->inited = !state; |
| 91 | |
| 92 | return state; |
| 93 | } |
| 94 | |
| 95 | void hal_platform_release(hal_platform *platform) |
| 96 | { |
| 97 | assert(platform && platform->platform_release); |
| 98 | data_acq_channel_release(platform->data_acq); |
| 99 | data_psn_system_release(platform->data_psn); |
| 100 | |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 101 | hal_platform_clear(platform); |
Isabella Gottardi | ee4920b | 2022-02-25 14:29:32 +0000 | [diff] [blame] | 102 | info("Releasing platform %s\n", platform->plat_name); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 103 | platform->platform_release(); |
| 104 | } |