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

CAN: Loopback() #286

Merged
merged 5 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big topic this header about the licensing :)
We can talk about it.

// 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);
}
117 changes: 92 additions & 25 deletions libraries/CAN/src/CANXMC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ XMC_CAN_MO_t CAN_msg_tx = {
{0xFF, XMC_CAN_FRAME_TYPE_STANDARD_11BITS, // {can_identifier, can_id_mode
XMC_CAN_ARBITRATION_MODE_ORDER_BASED_PRIO_1}, // can_priority}
{0x7FFU, 1U}, // {can_id_mask, can_ide_mask}
.can_data_length = 1U,
.can_data_length = 0U,
.can_mo_type = XMC_CAN_MO_TYPE_TRANSMSGOBJ,
};

Expand Down Expand Up @@ -106,31 +106,49 @@ CANXMC::~CANXMC() {}
return 0;
}

/* setup message object */
if (_txExtended) {
XMC_CAN_MO_SetExtendedID(&CAN_msg_tx);
} else {
XMC_CAN_MO_SetStandardID(&CAN_msg_tx);
}
XMC_CAN_MO_SetIdentifier(&CAN_msg_tx, _txId);
if (!_txRtr) {
memcpy(CAN_msg_tx.can_data_byte, _txData, _txLength);
}

XMC_CAN_MO_SetDataLengthCode(&CAN_msg_tx, _txLength);
if (_txRtr) {
// TODO: LJ: how to receive remote frame? and response...
/* Configure remote frame to be transmitted and data length code */
if (_txExtended) {
XMC_CAN_MO_SetExtendedID(&CAN_msg_rx);
} else {
XMC_CAN_MO_SetStandardID(&CAN_msg_rx);
}
XMC_CAN_MO_SetIdentifier(&CAN_msg_rx, _txId);
XMC_CAN_MO_SetDataLengthCode(&CAN_msg_rx, _txLength);
XMC_CAN_MO_UpdateData(&CAN_msg_rx);

/* Send remote frame */
XMC_CAN_STATUS_t send_status = XMC_CAN_MO_Transmit(&CAN_msg_rx);

if (send_status == XMC_CAN_STATUS_SUCCESS) {
return 1;
} else {
return 0;
}

} else {
/* Configure data frame to be transmitted and data length code */
if (_txExtended) {
XMC_CAN_MO_SetExtendedID(&CAN_msg_tx);
} else {
XMC_CAN_MO_SetStandardID(&CAN_msg_tx);
}
XMC_CAN_MO_SetIdentifier(&CAN_msg_tx, _txId);
memcpy(CAN_msg_tx.can_data_byte, _txData, _txLength);
XMC_CAN_MO_SetDataLengthCode(&CAN_msg_tx, _txLength);

/* Configure data to be transmitted and data length code */
XMC_CAN_MO_UpdateData(&CAN_msg_tx);

XMC_CAN_MO_UpdateData(&CAN_msg_tx);

/* Send data in CAN_NODE_LMO_0 */
XMC_CAN_STATUS_t send_status = XMC_CAN_MO_Transmit(&CAN_msg_tx);
/* Send data frame */
XMC_CAN_STATUS_t send_status = XMC_CAN_MO_Transmit(&CAN_msg_tx);

if (send_status == XMC_CAN_STATUS_SUCCESS) {
return 1;
} else {
return 0;
if (send_status == XMC_CAN_STATUS_SUCCESS) {
return 1;
} else {
return 0;
}
}
};

Expand Down Expand Up @@ -202,14 +220,63 @@ CANXMC::~CANXMC() {}

int CANXMC::observe()
{
XMC_CAN_NODE_SetInitBit(_XMC_CAN_config->can_node);
// TOOD: LJ: no idea what this is
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tood? xD

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol I will fix it

return 0;
};

int CANXMC::loopback()
{
XMC_CAN_NODE_SetInitBit(_XMC_CAN_config->can_node);
return 0;
// TODO: LJ: It is actually only some application level... it is not library...
/* reset CAN nodes*/
XMC_CAN_Disable(CAN_xmc);
/* CAN bit time configuration*/
XMC_CAN_NODE_NOMINAL_BIT_TIME_CONFIG_t CAN_NODE_bit_time_config =
{
.can_frequency = _XMC_CAN_config->can_frequency,
.baudrate = (uint32_t)50000, // TODO: LJ: need to be changed
.sample_point = (uint16_t)(80 * 100),
.sjw = (uint16_t)1,
};

XMC_CAN_Enable(CAN_xmc);
/* Configuration of CAN Node and enable the clock */
XMC_CAN_InitEx(CAN_xmc, _XMC_CAN_config->can_clock , _XMC_CAN_config->can_frequency);
if (XMC_CAN_STATUS_SUCCESS == XMC_CAN_NODE_NominalBitTimeConfigureEx( CAN_NODE1, &CAN_NODE_bit_time_config)){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general, keep that in mind for the whole file. We don´t have a formating tool yet, but at least within a file we can keep some consistancy.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I formatted locally using clang style.
I can also add this: https://github.com/marketplace/actions/clang-format-code-formatter for style checking. Is it ok?
That way we don't get PRs with a lot of formatting changes... like the opened one...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

topic close.


/* Enable CAN node 1 for Loop-back mode */
XMC_CAN_NODE_EnableConfigurationChange(CAN_NODE1);
XMC_CAN_NODE_EnableLoopBack(CAN_NODE1);
XMC_CAN_NODE_DisableConfigurationChange(CAN_NODE1);
XMC_CAN_NODE_ResetInitBit(CAN_NODE1);

/* Initializes CAN tx Message Object for loopback */
XMC_CAN_MO_Config(&CAN_msg_tx);
/* Allocate tx Message object to Node 1 */
XMC_CAN_AllocateMOtoNodeList(CAN_xmc, 1, 1);
} else {
return 0;
}

if (XMC_CAN_STATUS_SUCCESS == XMC_CAN_NODE_NominalBitTimeConfigureEx(CAN_NODE0, &CAN_NODE_bit_time_config)){
/* Enable CAN node 0 for Loop-back mode */
XMC_CAN_NODE_EnableConfigurationChange(CAN_NODE0);
XMC_CAN_NODE_EnableLoopBack(CAN_NODE0);
XMC_CAN_NODE_DisableConfigurationChange(CAN_NODE0);
XMC_CAN_NODE_ResetInitBit(CAN_NODE0);

/* Initializes CAN rx Message Object for loopback */
XMC_CAN_MO_Config(&CAN_msg_rx);
/*Allocate rx Message object to Node 0 */
XMC_CAN_AllocateMOtoNodeList(CAN_xmc, 0, 0);
XMC_CAN_MO_AcceptStandardAndExtendedID(&CAN_msg_rx);
/* Enable receive event */
XMC_CAN_MO_SetEventNodePointer(&CAN_msg_rx, XMC_CAN_MO_POINTER_EVENT_RECEIVE, _XMC_CAN_config->irq_service_request);
XMC_CAN_MO_EnableEvent(&CAN_msg_rx, XMC_CAN_MO_EVENT_RECEIVE);

return 1;
} else {
return 0;
}
};

int CANXMC::sleep()
Expand All @@ -230,7 +297,7 @@ CANXMC::~CANXMC() {}
}
};

/* Interrupt Handler*/
/* Interrupt Handler */
extern "C" {
#if(UC_FAMILY == XMC4)
void CAN0_7_IRQHandler() {
Expand Down
Loading