Skip to content

Commit

Permalink
Merge pull request #271 from LinjingZhang/xmc1400_can
Browse files Browse the repository at this point in the history
XMC CAN Sending & Unity starting point
  • Loading branch information
LinjingZhang authored Apr 8, 2024
2 parents 7e82af6 + c31deda commit 2d23291
Show file tree
Hide file tree
Showing 26 changed files with 5,826 additions and 68 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/compile-platform-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ jobs:
- libraries/RTC/examples/AlarmRTC
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Compile examples
uses: arduino/compile-sketches@v1
uses: arduino/compile-sketches@v1.1.0
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
fqbn: ${{ matrix.board.fqbn }}
Expand Down Expand Up @@ -182,7 +182,7 @@ jobs:
sketches-report-path: ${{ env.SKETCHES_REPORTS_PATH }}

- name: Save sketches report as workflow artifact
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
if-no-files-found: error
path: ${{ env.SKETCHES_REPORTS_PATH }}
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.history
.vscode
pkg_build
pkg_build
build
115 changes: 115 additions & 0 deletions libraries/CAN/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
FQBN ?=
PORT ?=
TESTS ?=

$(info FQBN : $(FQBN))
$(info PORT : $(PORT))


TESTS_CONNECTED=-DTEST_CAN_CONNECTED

TESTS_NOT_CONNECTED=-DTEST_CAN


CAN_connected: TESTS=-DTEST_CAN -DTEST_CAN_CONNECTED
CAN: TESTS=-DTEST_CAN

CAN_connected CAN: unity flash


# master_baudrate: TESTS=-DMASTER_BAUDRATE
# master_baudrate: PORT=COM18

# slave_baudrate: TESTS=-DSLAVE_BAUDRATE
# master_baudrate: PORT=COM17

# master_baudrate slave_baudrate: unity flash


test_all: TESTS=$(TESTS_CONNECTED) $(TESTS_NOT_CONNECTED)
test_connected: TESTS=$(TESTS_CONNECTED)
test: TESTS=$(TESTS_NOT_CONNECTED)

test_all \
test_connected \
test: unity flash
#test: unity compile


EXAMPLES = CANReceiver CANReceiverCallback CANSender


clean:
-rm -rf build/*


arduino: clean
mkdir -p build
# copy library file
# cp -r libraries/CAN/* build
# find src -name '*.[hc]*' -a \( \! -name 'main*' \) -print -exec cp {} build \;


CANReceiver: arduino
cp examples/CANReceiver/CANReceiver.ino build/build.ino

CANReceiverCallback: arduino
cp examples/CANReceiverCallback/CANReceiverCallback.ino build/build.ino

CANSender: arduino
cp examples/CANSender/CANSender.ino build/build.ino

# master1:
# ...

# slave1:
# ...


unity: arduino
cp -r test/unit/Unity/*.[hc] build
cp test/unit/src/framework/arduino/Test_*.[hc]* build
cp test/unit/src/framework/arduino/unity_ifx.cpp build
cp test/unit/src/framework/arduino/Test_main.ino build/build.ino


# For WSL and Windows :
# download arduino-cli.exe from : https://downloads.arduino.cc/arduino-cli/arduino-cli_latest_Windows_64bit.zip
compile:
ifeq ($(FQBN),)
$(error "Must set variable FQBN in order to be able to compile Arduino sketches !")
else
# CAUTION : only use '=' when assigning values to vars, not '+='
arduino-cli.exe compile --clean --log --warnings all --fqbn $(FQBN) --build-property "compiler.c.extra_flags=\"-DUNITY_INCLUDE_CONFIG_H=1\"" \
--build-property compiler.cpp.extra_flags="$(TESTS)" \
--build-property compiler.ar.cmd=arm-none-eabi-gcc-ar \
--build-property compiler.libraries.ldflags=-lstdc++ \
--build-property compiler.arm.cmsis.path="-isystem{compiler.xmclib_include.path}/XMCLib_CAN_v4_3_0/inc -isystem{compiler.xmclib_include.path}/XMCLib/inc -isystem{compiler.dsp_include.path} -isystem{compiler.nn_include.path} -isystem{compiler.cmsis_include.path} -isystem{compiler.xmclib_include.path}/LIBS -isystem{build.variant.path} -isystem{build.variant.config_path}" \
--build-property compiler.usb.path="-isystem{runtime.platform.path}/cores/usblib -isystem{runtime.platform.path}/cores/usblib/Common -isystem{runtime.platform.path}/cores/usblib/Class -isystem{runtime.platform.path}/cores/usblib/Class/Common -isystem{runtime.platform.path}/cores/usblib/Class/Device -isystem{runtime.platform.path}/cores/usblib/Core -isystem{runtime.platform.path}/cores/usblib/Core/XMC4000" \
build
endif


upload:
ifeq ($(PORT),)
$(error "Must set variable PORT (Windows port naming convention, ie COM16) in order to be able to flash Arduino sketches !")
endif
ifeq ($(FQBN),)
$(error "Must set variable FQBN in order to be able to flash Arduino sketches !")
else
arduino-cli.exe upload -p $(PORT) --fqbn $(FQBN) build
endif


flash: compile upload


monitor:
ifeq ($(PORT),)
$(error "Must set variable PORT (Windows port naming convention, ie COM16) in order to be able to flash Arduino sketches !")
endif
ifeq ($(FQBN),)
$(error "Must set variable FQBN in order to be able to flash Arduino sketches !")
else
arduino-cli.exe monitor -c baudrate=9600 -p $(PORT) --fqbn $(FQBN)
endif
16 changes: 16 additions & 0 deletions libraries/CAN/Makefile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### Build commands


### Example program
make FQBN=Infineon:xmc:XMC4700_Relax_Kit PORT=COM20 CANReceiver flash monitor

make FQBN=Infineon:xmc:XMC4700_Relax_Kit PORT=COM20 CANReceiverCallback flash monitor

make FQBN=Infineon:xmc:XMC4700_Relax_Kit PORT=COM24 CANSender flash monitor



### Unit tests
make FQBN=Infineon:xmc:XMC4700_Relax_Kit PORT=COM20 CAN monitor

make FQBN=Infineon:xmc:XMC1100_XMC2GO PORT=COM21 CAN_connected monitor
27 changes: 16 additions & 11 deletions libraries/CAN/README_CAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Include Library and using namespace

```arduino
#include <CANXMC.h>
#include <CAN.h>
using namespace ifx;
```

Expand All @@ -14,17 +14,24 @@ using namespace ifx;
Initialize the library with the specified bit rate.

```arduino
CAN.begin(bitrate);
CAN.begin(bitrate, id);
```
* `bitrate` - bit rate in bits per seconds (bps) (default 500E3. `1000E3`, `500E3`, `250E3`, `200E3`, `125E3`, `100E3`, `80E3`, `50E3`, `40E3`, `20E3`, `10E3`, `5E3`)
* `bitrate` - bit rate in bits per seconds (bps) (`1000E3`, `500E3`(default), `250E3`, `200E3`, `125E3`, `100E3`, `80E3`, `50E3`, `40E3`, `20E3`, `10E3`, `5E3`)

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

### Set pins

The RX and TX pins are determined.

<strike>
### Set identifier

```arduino
CAN.setIdentifier(id);
```
* `id` - 11 bits standard identifier. Reflecting the contents and priority of the message. (default: `0x12`)


### End

Stop the library
Expand All @@ -33,6 +40,7 @@ Stop the library
CAN.end()
```


## Sending data

### Begin packet
Expand All @@ -43,13 +51,9 @@ Start the sequence of sending a packet.
CAN.beginPacket(id);
CAN.beginPacket(id, dlc);
CAN.beginPacket(id, dlc, rtr);
CAN.beginExtendedPacket(id);
CAN.beginExtendedPacket(id, dlc);
CAN.beginExtendedPacket(id, dlc, rtr);
```

* `id` - 11-bit id (standard packet) or 29-bit packet id (extended packet)
* `id` - 11-bit id (standard packet) <strike> or 29-bit packet id (extended packet)</strike>
* `dlc` - (optional) value of Data Length Code (DLC) field of packet, default is size of data written in packet
* `rtr` - (optional) value of Remote Transmission Request (RTR) field of packet (`false` or `true`), defaults to `false`. RTR packets contain no data, the DLC field of the packet represents the requested length.

Expand Down Expand Up @@ -84,7 +88,7 @@ CAN.endPacket()
```

Returns `1` on success, `0` on failure.
</strike>

## Receiving data

### Parsing packet
Expand Down Expand Up @@ -152,7 +156,7 @@ int availableBytes = CAN.available()
```

Returns number of bytes available for reading.
<strike>

### Peeking

Peek at the next byte in the packet.
Expand All @@ -175,6 +179,7 @@ Returns the next byte in the packet or `-1` if no bytes are available.

**Note:** Other Arduino [`Stream` API's](https://www.arduino.cc/en/Reference/Stream) can also be used to read data from the packet

<strike>
### Filtering

Filter packets that meet the desired criteria.
Expand Down
7 changes: 4 additions & 3 deletions libraries/CAN/examples/CANReceiver/CANReceiver.ino
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include <CANXMC.h>
#include <CAN.h>
using namespace ifx;

void setup() {
Serial.begin(9600);
while (!Serial);
delay(5000);
delay(1000);
Serial.println("CAN Receiver");

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

void loop() {
Expand Down Expand Up @@ -48,7 +49,7 @@ void loop() {

// only print packet data for non-RTR packets
while (CAN.available()) {
Serial.print(CAN.read());
Serial.print((char)CAN.read());
}
Serial.println();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (c) Sandeep Mistry. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include <CANXMC.h>
#include <CAN.h>
using namespace ifx;

void setup() {
Serial.begin(9600);
while (!Serial);

delay(1000);
Serial.println("CAN Receiver Callback");

// start the CAN bus at 500 kbps
Expand All @@ -18,6 +18,7 @@ void setup() {

// register the receive callback
CAN.onReceive(onReceive);
CAN.setIdentifier(0x12);
}

void loop() {
Expand Down Expand Up @@ -49,7 +50,7 @@ void onReceive(int packetSize) {

// only print packet data for non-RTR packets
while (CAN.available()) {
Serial.print(CAN.read());
Serial.print((char)CAN.read());
}
Serial.println();
}
Expand Down
6 changes: 2 additions & 4 deletions libraries/CAN/examples/CANSender/CANSender.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#include <CAN.h>
using namespace ifx;

void setup() {
Serial.begin(9600);
Expand Down Expand Up @@ -32,10 +33,7 @@ void loop() {

delay(1000);

// send extended packet: id is 29 bits, packet can contain up to 8 bytes of data
Serial.print("Sending extended packet ... ");

CAN.beginExtendedPacket(0xabcdef);
CAN.beginPacket(0x12);
CAN.write('w');
CAN.write('o');
CAN.write('r');
Expand Down
8 changes: 8 additions & 0 deletions libraries/CAN/src/CAN.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef CAN_H
#define CAN_H

#if defined(CAN_xmc)
#include "CANXMC.h"
#endif

#endif
Loading

0 comments on commit 2d23291

Please sign in to comment.