blob: 0b04019b1d3b154bcfdd928dbfe1c3e58ca32d7d [file] [log] [blame]
James Kuszmaulcf324122023-01-14 14:07:17 -08001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#include "FPGACalls.h"
6
7#include <cerrno>
8
9#include "dlfcn.h"
10#include "hal/Errors.h"
11
12static void* NiFpgaLibrary = nullptr;
13
14namespace hal {
15HAL_NiFpga_ReserveIrqContextFunc HAL_NiFpga_ReserveIrqContext;
16HAL_NiFpga_UnreserveIrqContextFunc HAL_NiFpga_UnreserveIrqContext;
17HAL_NiFpga_WaitOnIrqsFunc HAL_NiFpga_WaitOnIrqs;
18HAL_NiFpga_AcknowledgeIrqsFunc HAL_NiFpga_AcknowledgeIrqs;
19HAL_NiFpga_OpenHmbFunc HAL_NiFpga_OpenHmb;
20HAL_NiFpga_CloseHmbFunc HAL_NiFpga_CloseHmb;
21
22namespace init {
23int InitializeFPGA() {
24 NiFpgaLibrary = dlopen("libNiFpga.so", RTLD_LAZY);
25 if (!NiFpgaLibrary) {
26 return errno;
27 }
28
29#pragma GCC diagnostic push
30#pragma GCC diagnostic ignored "-Wpedantic"
31 HAL_NiFpga_ReserveIrqContext =
32 reinterpret_cast<HAL_NiFpga_ReserveIrqContextFunc>(
33 dlsym(NiFpgaLibrary, "NiFpgaDll_ReserveIrqContext"));
34 HAL_NiFpga_UnreserveIrqContext =
35 reinterpret_cast<HAL_NiFpga_UnreserveIrqContextFunc>(
36 dlsym(NiFpgaLibrary, "NiFpgaDll_UnreserveIrqContext"));
37 HAL_NiFpga_WaitOnIrqs = reinterpret_cast<HAL_NiFpga_WaitOnIrqsFunc>(
38 dlsym(NiFpgaLibrary, "NiFpgaDll_WaitOnIrqs"));
39 HAL_NiFpga_AcknowledgeIrqs = reinterpret_cast<HAL_NiFpga_AcknowledgeIrqsFunc>(
40 dlsym(NiFpgaLibrary, "NiFpgaDll_AcknowledgeIrqs"));
41 HAL_NiFpga_OpenHmb = reinterpret_cast<HAL_NiFpga_OpenHmbFunc>(
42 dlsym(NiFpgaLibrary, "NiFpgaDll_OpenHmb"));
43 HAL_NiFpga_CloseHmb = reinterpret_cast<HAL_NiFpga_CloseHmbFunc>(
44 dlsym(NiFpgaLibrary, "NiFpgaDll_CloseHmb"));
45#pragma GCC diagnostic pop
46
47 if (HAL_NiFpga_ReserveIrqContext == nullptr ||
48 HAL_NiFpga_UnreserveIrqContext == nullptr ||
49 HAL_NiFpga_WaitOnIrqs == nullptr ||
50 HAL_NiFpga_AcknowledgeIrqs == nullptr || HAL_NiFpga_OpenHmb == nullptr ||
51 HAL_NiFpga_CloseHmb == nullptr) {
52 HAL_NiFpga_ReserveIrqContext = nullptr;
53 HAL_NiFpga_UnreserveIrqContext = nullptr;
54 HAL_NiFpga_WaitOnIrqs = nullptr;
55 HAL_NiFpga_AcknowledgeIrqs = nullptr;
56 HAL_NiFpga_OpenHmb = nullptr;
57 HAL_NiFpga_CloseHmb = nullptr;
58 dlclose(NiFpgaLibrary);
59 NiFpgaLibrary = nullptr;
60 return NO_AVAILABLE_RESOURCES;
61 }
62
63 return HAL_SUCCESS;
64}
65} // namespace init
66} // namespace hal