26FlexCAN_T4<CONFIG_FLEXCAN_CAN_SELECT, RX_SIZE_256, TX_SIZE_16> F_Can;
28volatile uint8_t buffer[CAN_MESSAGE_COUNT + 1][8];
30#define __BUF_INTERNAL true
31#define __BUF_EXTERNAL false
32static Buffer buffers[CAN_MESSAGE_COUNT + 1] = {
33#define MSG(c, addr, name, sig_c, sig_def, ie_t, contained) Buffer(addr, (volatile uint8_t *)buffer[c], CONCAT(__BUF_, ie_t)),
36 Buffer(0, (
volatile uint8_t *)buffer[CAN_MESSAGE_COUNT]),
39static Buffer *invalidBuf = buffers + CAN_MESSAGE_COUNT;
42static CAN_message_t receive;
43static Thread::Mutex mux_send;
44static CAN_message_t send;
45static bool started =
false;
48static void _receiveCan(
const CAN_message_t &msg) {
51 if (buf == invalidBuf || !buf->lock()) {
53 Log.
w(Canbus_t::ID,
"Discarding can msg", msg.id);
60 buf->callback(msg.id, buf->buffer);
66#define MSG(c, addr, name, sig_c, sig_def, ie_t, contained) \
78 if (buf == invalidBuf) {
79 Log.
e(ID,
"Address has not been allocated: ", address);
84 buf->callback = callback;
89 F_Can.enableMBInterrupts(enable);
93 Log.
d(ID,
"Interrupts enabled");
95 Log.
d(ID,
"Interrupts disabled");
103 send.id = buf.address;
113 memcpy(send.buf, buf, 8);
118void Canbus_t::sendData(
const uint32_t address,
const uint8_t buf_0,
const uint8_t buf_1,
const uint8_t buf_2,
const uint8_t buf_3,
const uint8_t buf_4,
const uint8_t buf_5,
const uint8_t buf_6,
const uint8_t buf_7) {
133static void _canSniff(
const CAN_message_t &msg) {
135 Serial.print(
" ID: ");
136 Serial.print(msg.id, HEX);
137 Serial.print(
" Buffer: ");
138 for (uint8_t i = 0; i < msg.len; i++) {
139 Serial.print(msg.buf[i], HEX);
146#ifndef CONF_LOGGING_ASCII_DEBUG
147 Log.
w(ID,
"Canbus sniffer will now be outputting raw ascii strings");
150 Serial.println(
"Enabling canbus sniffer");
151 F_Can.onReceive(_canSniff);
153 Serial.println(
"Disabling canbus sniffer");
154 F_Can.onReceive(_receiveCan);
160 Log.
d(ID,
"Starting");
163 Log.
d(ID,
"Setting MB RX");
164 for (
int i = TX_MAILBOXES; i < 16; i++) {
165 F_Can.setMB((FLEXCAN_MAILBOX)i, RX, NONE);
167 Log.
d(ID,
"Setting MB TX");
168 for (
int i = 0; i < TX_MAILBOXES; i++) {
169 F_Can.setMB((FLEXCAN_MAILBOX)i, TX, NONE);
172 Log.
d(ID,
"Setting Callback");
173 F_Can.onReceive(_receiveCan);
174 Log.
d(ID,
"Enabling Interrupts");
176 F_Can.enableMBInterrupts();
177#ifdef CONF_LOGGING_ASCII_DEBUG
178 F_Can.mailboxStatus();
185 for (Buffer &buf : buffers) {
186 if (buf.outgoing && buf.modified) {
Define incoming CAN line addresses.
Configure canbus functionality.
#define CONFIG_FLEXCAN_TX_MAILBOXES
Set the number of outgoing mailboxes.
#define CONFIG_FLEXCAN_BAUD_RATE
Set the baud rate at which the CAN line should run.
Configure global build properties.
Special logging functionality.
Logging::Log_t Log
The global logging object.
static void enableCanbusSniffer(bool enable)
continuously prints out strings of any message that is received through canbus. As such,...
static void sendData(Buffer &buf)
Send data given a buffer object.
void setup(void)
Setup the Canbus line.
static void setCallback(const uint32_t address, canCallback callback)
Set a callback to an incoming address. If an incoming address buffer is updated it will call the give...
void run(void)
Run the teensy Canbus line.
static constexpr Buffer * getBuffer(const uint32_t address)
Get the buffer of an any address. If it is outgoing, use pushData to push the data after modifying th...
static void enableInterrupts(bool enable)
Enable mailbox interrupts, allowing values to automaticaly update. Enabled by default.
Canbus functionality. Refer to Canbus.h for more info.
void(* canCallback)(uint32_t, volatile uint8_t *)
The function type to pass to addCallback.
void d(LOG_TAG TAG, LOG_MSG message)
Log a string using a debug tag.
void w(LOG_TAG TAG, LOG_MSG message)
Log a string using a warning tag.
void e(LOG_TAG TAG, LOG_MSG message)
Log a string using an error tag.