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

Not working for TxHeader.DataLength > FDCAN_DLC_BYTES_2 #1

Open
JoseCSNeto opened this issue Mar 18, 2021 · 4 comments
Open

Not working for TxHeader.DataLength > FDCAN_DLC_BYTES_2 #1

JoseCSNeto opened this issue Mar 18, 2021 · 4 comments

Comments

@JoseCSNeto
Copy link

JoseCSNeto commented Mar 18, 2021

Hi. I managed to make your example work on two NUCLEO-G431KB. Thank you for that.

Now, my problem is that I can't make it work with more than 2 bytes, even after setting TxHeader.DataLength = FDCAN_DLC_BYTES_3, etc..

Sketch:

#include <Arduino.h>
#include <SimpleCan.h>

static void handleCanMessage(FDCAN_RxHeaderTypeDef rxHeader, uint8_t *rxData);
static void init_CAN(void);
static void Button_Down(void);

// pass in optional shutdown and terminator pins that disable transceiver and add 120ohm resistor respectively
SimpleCan can1(-1, -1);
SimpleCan::RxHandler can1RxHandler(64, handleCanMessage);

FDCAN_TxHeaderTypeDef TxHeader;
uint8_t TxData[8];

void setup()
{
	Serial2.begin(9600);
	delay(3000);

	pinMode(LED_BUILTIN, OUTPUT);
	digitalWrite(LED_BUILTIN, HIGH);
	attachInterrupt(PB0, Button_Down, RISING);
	delay(100);
	init_CAN();
}

void loop()
{
}

static void init_CAN()
{
	Serial2.println(can1.init(CanSpeed::Mbit1) == HAL_OK
						? "CAN: initialized."
						: "CAN: error when initializing.");

	//DCAN_FilterTypeDef sFilterConfig;

	// Configure Rx filter
	// sFilterConfig.IdType = FDCAN_STANDARD_ID;
	// sFilterConfig.FilterIndex = 0;
	// sFilterConfig.FilterType = FDCAN_FILTER_MASK;
	// sFilterConfig.FilterConfig = FDCAN_FILTER_TO_RXFIFO0;
	// sFilterConfig.FilterID1 = 0x321;
	// sFilterConfig.FilterID2 = 0x7FF;

	// can1.configFilter(&sFilterConfig);
	// can1.configGlobalFilter(FDCAN_REJECT, FDCAN_REJECT, FDCAN_FILTER_REMOTE, FDCAN_FILTER_REMOTE);
	can1.activateNotification(&can1RxHandler);

	Serial2.println(can1.start() == HAL_OK
						? "CAN: started."
						: "CAN: error when starting.");
}

int dlcToLength(uint32_t dlc)
{
	int length = dlc >> 16;
	if (length >= 13)
	{
		return 32 + (13 - length) * 16;
	}
	else if (length == 12)
	{
		return 24;
	}
	else if (length >= 9)
	{
		return 12 + (9 - length) * 4;
	}
	return length;
}

static void handleCanMessage(FDCAN_RxHeaderTypeDef rxHeader, uint8_t *rxData)
{
	//Serial2.println("Recebi algo");

	int byte_length = dlcToLength(rxHeader.DataLength);

	Serial2.print("Received packet, id=0x");
	Serial2.print(rxHeader.Identifier, HEX);
	Serial2.print(", length=");
	Serial2.print(byte_length);
	for (int byte_index = 0; byte_index < byte_length; byte_index++)
	{
		Serial2.print(" byte[");
		Serial2.print(byte_index);
		Serial2.print("]=");
		Serial2.print(rxData[byte_index]);
		Serial2.print(" ");
	}
	Serial2.println();

	digitalToggle(LED_BUILTIN);
}

static void Button_Down()
{

	static uint8_t press_count = 0;

	press_count++;

	TxHeader.Identifier = 0x321;
	TxHeader.IdType = FDCAN_STANDARD_ID;
	TxHeader.TxFrameType = FDCAN_DATA_FRAME;
	TxHeader.DataLength = FDCAN_DLC_BYTES_3;
	TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
	TxHeader.BitRateSwitch = FDCAN_BRS_OFF;
	TxHeader.FDFormat = FDCAN_FD_CAN; // FDCAN_CLASSIC_CAN
	TxHeader.TxEventFifoControl = FDCAN_NO_TX_EVENTS;
	TxHeader.MessageMarker = 0;

	TxData[0] = press_count;
	TxData[1] = 0xAD;
	TxData[2] = 2;

	Serial2.print("CAN: sending message ");
	Serial2.println(can1.addMessageToTxFifoQ(&TxHeader, TxData) == HAL_OK ? "was ok." : "failed.");
}

Additional info:

@owennewo
Copy link
Owner

What error are you seeing? Is it printing 'CAN: sending message failed"?
It might be worth checking what can1.addMessageToTxFifoQ is returning it might be HAL_BUSY or HAL_ERROR

I've looked at the code. I can see a bit of a weird bit on the receiving end but the sending bit looks ok.

The bit that looks weird is:

public:
		RxHandler(uint16_t dataLength, void(*callback)(FDCAN_RxHeaderTypeDef rxHeader, uint8_t *rxData));
		~RxHandler();

		void notify(FDCAN_HandleTypeDef *hfdcan);
	private:
		FDCAN_RxHeaderTypeDef _rxHeader;
		uint8_t *_rxData;
		void(*_callback)(FDCAN_RxHeaderTypeDef, uint8_t *);
	};

I can't see where rxData is initialized.

The code was originally written by erwin74 and discussed here:
https://community.simplefoc.com/t/can-bus-support/407
The stm part of the code was initially writtern by erwin73

@JoseCSNeto
Copy link
Author

JoseCSNeto commented Mar 18, 2021

It's seems to be actually sending the message: addMessageToTxFifoQ returns HAL_OK -> "CAN: sending message was ok.".

I guess the problem is on the receiving side.

_rxData is initialized here (SimpleCan.cpp), but I'm not so sure about rxData.

SimpleCan::RxHandler::RxHandler(uint16_t dataLength, void (*callback)(FDCAN_RxHeaderTypeDef, uint8_t *))
{
	_rxData = new byte[dataLength];
	_callback = callback;
}

Tomorrow I'll compare this with another example and see if I can come up with the solution.

@owennewo
Copy link
Owner

Can you remove most of the code from handleCanMessage and see if the function is triggered e.g.

static void handleCanMessage(FDCAN_RxHeaderTypeDef rxHeader, uint8_t *rxData)
{
	Serial.println("received");

	digitalToggle(LED_BUILTIN);
}

@JoseCSNeto
Copy link
Author

JoseCSNeto commented Mar 18, 2021

Will try that tomorrow, thanks.

EDIT: It is receiving. Must be blocking somewhere on the handleCanMessage function.

EDIT2: It works if I print it out of the for loop. Serial.print is blocking for some reason?
Example for TxHeader.DataLength = FDCAN_DLC_BYTES_3:

Serial2.println(rxData[0]);
Serial2.print(" ");
Serial2.println(rxData[1]);
Serial2.print(" ");
Serial2.println(rxData[2]);
Serial2.println();

EDIT3: This bit of code inside the for loop blocks everything for some reason:

Serial2.print(" byte[");
Serial2.print(byte_index);
Serial2.print("]=");

When I commented it out it stopped blocking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants