SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
SDBC_class.hpp
1#pragma once
2#include <stdint.h>
3
4#include "Canbus.h"
5#include "Pins.h"
6
7namespace SDBC {
8
9class MSG;
10struct SIG;
11
12struct GPIO {
13 const uint16_t pin;
14 const SIG *settee;
15 const bool outOrIn;
16 const bool digOrAnl;
17 const bool isVirt = false;
18};
19
20struct VIRT : GPIO {
21};
22
23const struct SIG {
24 const uint64_t bitMask;
25 const MSG *msg;
26 const GPIO *setter;
27 const dt dataType;
28 const uint8_t bitPos;
29 const uint8_t nodeID;
30 // TODO: Format function pointer
31};
32
33class MSG {
34 CAN::Buffer buffer; // FIXME: get live buffer from CAN lib
35
36public:
37 const bool outgoing;
38 volatile union value {
39 volatile uint8_t arr[8];
40 volatile uint64_t raw;
41 } value;
42 const SIG **SIGNALS;
43 const int signalCount;
44 const bool hasValueSetter;
45
46 template <typename T>
47 void setSig(const SIG *signal, T value) { // IMPROVE: add specialization for double and float to ensure value is not mangled
48 uint64_t data = *((uint64_t *)&value) & (~(uint64_t)0 >> (64 - 8 * sizeof(T)));
49 data <<= signal->bitPos;
50 data &= signal->bitMask;
51
52 buffer.lock_wait();
53 value.raw &= ~signal->bitMask;
54 value.raw |= data;
55 buffer.unlock();
56 }
57
58 template <typename T>
59 T getSig(const SIG *signal) {
60 uint64_t data = value.raw;
61 data &= signal.bitMask;
62 data >>= signal.bitPos;
63 return *(T *)&data;
64 }
65
66 MSG(const uint32_t msgAddr, bool outgoing, const SIG **SIGNALS, const int signalCount, const bool hasValueSetter) : buffer(msgAddr, value.arr, outgoing), outgoing(outgoing), SIGNALS(SIGNALS), signalCount(signalCount), hasValueSetter(hasValueSetter) {}
67};
68
69struct LINK : MSG {
70};
71
72} // namespace SDBC
FlexCAN_T4 wrapper.
Update, set, and get predefined pin values.
Definition SDBC.h:11
An incoming canbus message, allows the message data to be interpreted through a simple wrapper class.
Definition CanBuffer.h:23
void lock_wait()
locks this buffer to be used, waits indefinitely for it to unlock if it is locked
void unlock()
Unlocks this buffer if it is locked.