blob: 2715a1761539bbc8a999de36921c2b95b6643e0d [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Isabella Gottardiee4920b2022-02-25 14:29:32 +00002 * Copyright (c) 2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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 Sisodiaa1256e32022-02-23 14:40:45 +000017#include "hal.h" /* API */
alexander3c798932021-03-26 21:42:19 +000018
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000019#include "platform_drivers.h" /* Platform drivers */
20#include "log_macros.h" /* Logging macros */
alexander3c798932021-03-26 21:42:19 +000021
22#include <stdio.h>
23#include <assert.h>
Kshitij Sisodiaa1256e32022-02-23 14:40:45 +000024#include <string.h>
alexander3c798932021-03-26 21:42:19 +000025
alexander3c798932021-03-26 21:42:19 +000026int 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 Sisodiaa1256e32022-02-23 14:40:45 +000034 platform->platform_init = platform_init;
35 platform->platform_release = platform_release;
36 platform_name(platform->plat_name, sizeof(platform->plat_name));
alexander3c798932021-03-26 21:42:19 +000037
38 return 0;
39}
40
41/**
42 * @brief Local helper function to clean the slate for current platform.
43 **/
alexanderc350cdc2021-04-29 20:36:09 +010044static void hal_platform_clear(hal_platform* platform)
alexander3c798932021-03-26 21:42:19 +000045{
46 assert(platform);
47 platform->inited = 0;
48}
49
50int hal_platform_init(hal_platform* platform)
51{
52 int state;
53 assert(platform && platform->platform_init);
alexanderc350cdc2021-04-29 20:36:09 +010054 hal_platform_clear(platform);
alexander3c798932021-03-26 21:42:19 +000055
56 /* Initialise platform */
57 if (0 != (state = platform->platform_init())) {
Isabella Gottardiee4920b2022-02-25 14:29:32 +000058 printf_err("Failed to initialise platform %s\n", platform->plat_name);
alexander3c798932021-03-26 21:42:19 +000059 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 Gottardiee4920b2022-02-25 14:29:32 +000065 printf_err("Failed to initialise data acq module: %s\n",
alexander3c798932021-03-26 21:42:19 +000066 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 Gottardiee4920b2022-02-25 14:29:32 +000074 printf_err("Failed to initialise data psn module: %s\n",
alexander3c798932021-03-26 21:42:19 +000075 platform->data_psn->system_name);
76 data_acq_channel_release(platform->data_acq);
77 hal_platform_release(platform);
78 return state;
79 }
80
Isabella Gottardiee4920b2022-02-25 14:29:32 +000081 /* Followed by the timer module */
alexander3c798932021-03-26 21:42:19 +000082 init_timer(platform->timer);
83
84 info("%s platform initialised\n", platform->plat_name);
Isabella Gottardiee4920b2022-02-25 14:29:32 +000085 debug("Using %s module for data acquisition\n",
alexander3c798932021-03-26 21:42:19 +000086 platform->data_acq->system_name);
Isabella Gottardiee4920b2022-02-25 14:29:32 +000087 debug("Using %s module for data presentation\n",
alexander3c798932021-03-26 21:42:19 +000088 platform->data_psn->system_name);
89
90 platform->inited = !state;
91
92 return state;
93}
94
95void 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
alexanderc350cdc2021-04-29 20:36:09 +0100101 hal_platform_clear(platform);
Isabella Gottardiee4920b2022-02-25 14:29:32 +0000102 info("Releasing platform %s\n", platform->plat_name);
alexander3c798932021-03-26 21:42:19 +0000103 platform->platform_release();
104}