SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
Echo.cpp
Go to the documentation of this file.
1
11//@cond
12#include "Echo.h"
13#include "Canbus.h"
14
15namespace Echo {
16
17static IntervalTimer currentEcho;
18static uint32_t address;
19static uint32_t delay;
20static uint8_t buf_curr[8];
21static int count = 0;
22
23void echo(uint32_t delay, const uint32_t address, uint8_t buf[8]) {
24 echo(delay, address, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
25}
26
27void echo(uint32_t delay, 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) {
28 Canbus::sendData(ADD_ECHO_DATA, buf_0, buf_1, buf_2, buf_3, buf_4, buf_5, buf_6, buf_7);
29 uint64_t block = delay;
30 block <<= 32;
31 block |= address;
32 Canbus::sendData(ADD_ECHO_DELAY, (uint8_t *)block);
33}
34
35static void send(void) {
36 Canbus::sendData(address, buf_curr);
37 currentEcho.end();
38}
39
40static void startEcho() {
41 currentEcho.end();
42 currentEcho.begin(send, delay);
43}
44
45static void checkStart() {
46 count++;
47 if (count == 2) {
48 startEcho();
49 count = 0;
50 }
51}
52
53static void receiveBlock(uint32_t _, volatile uint8_t *buf) {
54 volatile uint64_t block = *((volatile uint64_t *)buf);
55 address = block & 0xFFFFFFFF;
56 delay = block >> 32;
57 checkStart();
58}
59
60static void receiveBuffer(uint32_t _, volatile uint8_t buf[8]) {
61 buf_curr[0] = buf[0];
62 buf_curr[1] = buf[1];
63 buf_curr[2] = buf[2];
64 buf_curr[3] = buf[3];
65 buf_curr[4] = buf[4];
66 buf_curr[5] = buf[5];
67 buf_curr[6] = buf[6];
68 buf_curr[7] = buf[7];
69 checkStart();
70}
71
72void setup() {
73 Canbus::addCallback(ADD_ECHO_DATA, receiveBlock);
74 Canbus::addCallback(ADD_ECHO_DELAY, receiveBuffer);
75}
76
77} // namespace Echo
78 //@endcond
FlexCAN_T4 wrapper.
Echo can messages with a delay.
This Module can be used to echo a CAN message over a network of two ECUs.
Definition Echo.h:28
void setup()
Initialize on receiving ECU to echo messages.
void echo(uint32_t delay, const uint32_t address, uint8_t buf[8])
Send a can message to the back ECU from the front to then echo the same message at a set delay.