Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pure virtual bus #18

Merged
merged 4 commits into from Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions code/headers/can.hpp
Expand Up @@ -588,5 +588,17 @@ namespace r2d2::can_bus {

return tick != can_timeout;
}

/**
* Guess whether we are connected to the outside world,
* by checking if we are in the Error Passive Mode. When
* no physical cable is attached, the error mode will activate.
*
* This will also return true on a high/abnormal rate of errors on the bus.
* @return
*/
static bool has_physical_connection() {
return (port<Bus>->CAN_SR >> 18) & 1;
}
};
}
22 changes: 17 additions & 5 deletions code/headers/channel.hpp
Expand Up @@ -126,7 +126,19 @@ namespace r2d2::can_bus {
}

// Wait for space, space is created in the interrupt
while (!space_in_tx_queue);
while (!space_in_tx_queue) {
// ERRP = Error Passive Mode
bool errp = (port<Bus>->CAN_SR >> 18) & 1;

// Are we in error mode?
if (errp) {
// We are in error mode, so we'll want to update the tx_queue
// with more recent information. In the event that we are connected
// again (e.g. this is a temporary problem), we'll send recent information.
tx_queue.pop();
break;
}
}

tx_queue.push(frame);
}
Expand Down Expand Up @@ -188,7 +200,7 @@ namespace r2d2::can_bus {
if (length <= 8) {
detail::_can_frame_s frame{};

for(size_t i = 0; i < length; i++){
for (size_t i = 0; i < length; i++) {
frame.data.bytes[i] = data[i];
}

Expand All @@ -205,7 +217,7 @@ namespace r2d2::can_bus {
detail::_can_frame_s frame{};

// Has to be 8 bytes; frame.length is copied in a lower layer
for(uint_fast8_t j = 0; j < 8; j++){
for (uint_fast8_t j = 0; j < 8; j++) {
frame.data.bytes[j] = data[j + i];
}

Expand All @@ -221,7 +233,7 @@ namespace r2d2::can_bus {
if (remainder > 0) {
detail::_can_frame_s frame{};

for(uint_fast8_t i = 0; i < remainder; i++){
for (uint_fast8_t i = 0; i < remainder; i++) {
frame.data.bytes[i] = data[i + total];
}

Expand Down Expand Up @@ -282,7 +294,7 @@ namespace r2d2::can_bus {
frame.type = static_cast<frame_type>(can_frame.frame_type);
frame.request = can_frame.mode == detail::_can_frame_mode::READ;

for(uint_fast8_t i = 0; i < 8; i++){
for (uint_fast8_t i = 0; i < 8; i++) {
frame.bytes[i] = can_frame.data.bytes[i];
}

Expand Down
45 changes: 10 additions & 35 deletions code/main.cpp
Expand Up @@ -8,48 +8,23 @@ int main() {
// kill the watchdog
WDT->WDT_MR = WDT_MR_WDDIS;

hwlib::wait_ms(10);
hwlib::wait_ms(1000);

r2d2::comm_c comm;
r2d2::comm_c comm2;

std::array<frame_id, 8> frames = {
r2d2::frame_type::BUTTON_STATE
};
comm2.listen_for_frames({r2d2::frame_type::BUTTON_STATE});

comm.listen_for_frames(frames);
for (;;) {
frame_button_state_s state;
state.pressed = true;

auto led = hwlib::target::pin_out(hwlib::target::pins::d6);
comm.send(state);

uint32_t start = hwlib::now_us();
uint32_t counter = 0;
uint32_t ticks = 0;
while (comm2.has_data()) {
const auto frame = comm2.get_data();

for (;;) {
comm.request(frame_type::BUTTON_STATE);

while (comm.has_data()) {
auto frame = comm.get_data();

// This module doesn't handle requests
if (frame.request) {
continue;
}

if (frame.type != frame_type::BUTTON_STATE) {
continue;
}

counter += 1;
const auto pressed = frame.as_frame_type<frame_type::BUTTON_STATE>().pressed;
led.write(pressed);

ticks = hwlib::now_us() - start;
if (ticks >= 1'000'000) {
hwlib::cout << counter << "\r\n";
start = hwlib::now_us();
ticks = 0;
counter = 0;
}
hwlib::cout << "rec fr\n";
}
}
}