blob: d22fcda7211f2cc8b5fa9b01c0fa9e3c36a47e2f [file] [log] [blame]
James Kuszmaulb13e13f2023-11-22 20:44:04 -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 "hal/roborio/HMB.h"
6
7#include <memory>
8
9#include "FPGACalls.h"
10#include "hal/ChipObject.h"
11
12using namespace hal;
13
14// 16 classes of data, each takes up 16 uint16_t's
15static_assert(sizeof(HAL_HMBData) == 0x10 * 16 * sizeof(uint32_t));
16// Timestamp is the last class, and should be offset correctly
17static_assert(offsetof(HAL_HMBData, Timestamp) == 0x10 * 15 * sizeof(uint32_t));
18
19static volatile HAL_HMBData* hmbBuffer;
20static size_t hmbBufferSize;
21static constexpr const char hmbName[] = "HMB_0_RAM";
22static std::unique_ptr<tHMB> hmb;
23
24namespace {
25struct HMBHolder {
26 ~HMBHolder() {
27 if (hmbBuffer) {
28 hal::HAL_NiFpga_CloseHmb(hmb->getSystemInterface()->getHandle(), hmbName);
29 }
30 }
31};
32} // namespace
33
34extern "C" {
35
36void HAL_InitializeHMB(int32_t* status) {
37 static HMBHolder holder;
38
39 hmb.reset(tHMB::create(status));
40 if (*status != 0) {
41 return;
42 }
43
44 *status = hal::HAL_NiFpga_OpenHmb(
45 hmb->getSystemInterface()->getHandle(), hmbName, &hmbBufferSize,
46 reinterpret_cast<void**>(const_cast<HAL_HMBData**>(&hmbBuffer)));
47
48 if (*status != 0) {
49 return;
50 }
51
52 auto cfg = hmb->readConfig(status);
53 cfg.Enables_AI0_Low = 1;
54 cfg.Enables_AI0_High = 1;
55 cfg.Enables_AIAveraged0_Low = 1;
56 cfg.Enables_AIAveraged0_High = 1;
57 cfg.Enables_Accumulator0 = 1;
58 cfg.Enables_Accumulator1 = 1;
59 cfg.Enables_DI = 1;
60 cfg.Enables_AnalogTriggers = 1;
61 cfg.Enables_Counters_Low = 1;
62 cfg.Enables_Counters_High = 1;
63 cfg.Enables_CounterTimers_Low = 1;
64 cfg.Enables_CounterTimers_High = 1;
65 cfg.Enables_Encoders_Low = 1;
66 cfg.Enables_Encoders_High = 1;
67 cfg.Enables_EncoderTimers_Low = 1;
68 cfg.Enables_EncoderTimers_High = 1;
69 cfg.Enables_DutyCycle_Low = 1;
70 cfg.Enables_DutyCycle_High = 1;
71 cfg.Enables_Interrupts = 1;
72 cfg.Enables_PWM = 1;
73 cfg.Enables_PWM_MXP = 1;
74 cfg.Enables_Relay_DO_AO = 1;
75 cfg.Enables_Timestamp = 1;
76 hmb->writeConfig(cfg, status);
77}
78
79const volatile HAL_HMBData* HAL_GetHMBBuffer(void) {
80 return hmbBuffer;
81}
82} // extern "C"