Skip to content

Commit

Permalink
CAN: Loopback() (#286)
Browse files Browse the repository at this point in the history
* Add: loopback()
Todo: comments some part can be improved...

* Add: Loopback example sketch

* Docu: change for loopback() and update version

* Style: format theCAN lib with Clang formatter

* Fix: PWM pin defines of XMC1400 2go
  • Loading branch information
LinjingZhang committed Jul 11, 2024
1 parent 644093d commit 86c623f
Show file tree
Hide file tree
Showing 5 changed files with 331 additions and 200 deletions.
6 changes: 2 additions & 4 deletions libraries/CAN/README_CAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ if ((packetId & mask) == id) {

Returns `1` on success, `0` on failure.

<strike>

## Other modes

### Loopback mode
Expand All @@ -216,6 +216,7 @@ Put the CAN controller in loopback mode, any outgoing packets will also be recei
CAN.loopback();
```

<strike>
### Sleep mode

Put the CAN contoller in sleep mode.
Expand All @@ -233,8 +234,5 @@ CAN.wakeup();

# Develop notes

## XMCLibs Version
For CAN development, we use v4.3.0 of XMClib. But other source code stay 2.1.16. Need to be fixed and updated in the future!!

## CAN macro
Because XMClibs use CAN macro, conflicts with Arduino default CAN class name, so we manually changed XMClib (CAN -> CAN_xmc). Automatic patch might be needed in the future.
61 changes: 61 additions & 0 deletions libraries/CAN/examples/CANLoopBack/CANLoopBack.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) Linjing Zhang. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include <CAN.h>
using namespace ifx;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial)
;

Serial.println("CAN Loopback");

// start the CAN bus at 500 kbps
if (!CAN.begin(500E3)) {
Serial.println("Starting CAN failed!");
while (1)
;
}

// set the can in loopback mode
CAN.loopback();
}

void loop() {

//
Serial.print("Sending packet");
CAN.beginPacket(0x12);
CAN.write('L');
CAN.write('O');
CAN.write('O');
CAN.write('P');
CAN.endPacket();


int packetSize = CAN.parsePacket();

if (packetSize) {
// received a packet
Serial.print("Received ");

if (CAN.packetExtended()) {
Serial.print("extended ");
}

Serial.print("packet with id 0x");
Serial.print(CAN.packetId(), HEX);

Serial.print(" and length ");
Serial.println(packetSize);

while (CAN.available()) {
Serial.print((char)CAN.read());
}
Serial.println();
}

delay(1000);
}
Loading

0 comments on commit 86c623f

Please sign in to comment.