-
-
Notifications
You must be signed in to change notification settings - Fork 262
Description
Hi,
I am working on a project that requires an easy to implement yet robust communication protocol. I chose CAN because it seemed to fit. The data that must be transmitted is fairly simple. It has a node identifying byte and a data byte. In a prototype I used one the popular dev boards for CAN bus shown below and it worked and is still working without major issues. I has an MCP2515+TJA1050.
Once everything was working well I decided to integrate the communication hardware into the project PCB. I will not get into details here but it is a type of keyboard, so no high power components, no RF components.
For reference I used the module schematic as well as datasheets and design notes for MCP2515 and the TJA1042T (the one I was able to get). The communication part of the circuit is this:
The strange looking component 2WTS1 (2 way toggle switch) is a sliding switch with two sets of contacts to be able to choose between the two 60ohm resistors and the two 1.3k (or nothing). It looks like this:
The oscillator is an integrated one like this:
The test code that I used to see if everything works is the following one:
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include "CAN.h"
#include "PCF8574.h"
void send(uint8_t item, uint8_t value) // as name indicantes, simple funcion to send the data structure
{
uint8_t id;
const uint8_t id_sender = 0xA0; // left KB
id = id_sender + item;
Serial.println("Data ready00");
CAN.beginPacket(id, 1);
Serial.println("Data ready01");
CAN.write(value);
Serial.println("Data ready02");
CAN.endPacket();
Serial.println("Data ready03");
Serial.println("Data sent");
}
void setup()
{
//delay(2000);
Serial.begin(9600);
Serial.println("Puto el que lee");
int i;
pinMode(9, OUTPUT);
digitalWrite(9, HIGH);
Wire.begin();
//i2c_scan();
CAN.setPins(10, -1);
CAN.setClockFrequency(8E6);
if (CAN.begin(500E3))
{
Serial.println("CAN started!!!");
}
else
{
Serial.println("CAN failed!!!");
}
}
void loop()
{
int packetSize = CAN.parsePacket();
send(2, 0xA0);
delay(500);
////------- RECEIVER -------////
if (packetSize)
{
// received a packet
Serial.print("Received ");
Serial.print("packet with id 0x");
Serial.println(CAN.packetId(), HEX);
while (CAN.available())
{
received = CAN.read();
Serial.print(received, BIN);
Serial.print(" - ");
Serial.print(CAN.packetId(), HEX);
Serial.println();
}
}
}
There are too many Serial prints, but i was trying to find the failure point.
So, the sender always freezes right after "Serial.println("Data ready02");", meaning the issue is the "CAN.endPacket();". I was not able to make the custom boards from the schematic above move past it. The dev board from the first image works without issue.
I did a little digging in the library itself and found that the issue is in the "while" loop on line 191 of MCP2515.cpp:
while (readRegister(REG_TXBnCTRL(n)) & 0x08) {
if (readRegister(REG_TXBnCTRL(n)) & 0x10) {
// abort
aborted = true;
modifyRegister(REG_CANCTRL, 0x10, 0x10);
}
//Serial.println(readRegister(REG_TXBnCTRL(n)), BIN);
//Serial.println(aborted);
//Serial.println("Data ready02a");
yield();
}
The result of the "Serial.println(readRegister(REG_TXBnCTRL(n)), BIN);" was 101000 and once 1101000. According to the MCP datasheet, this corresponds to Message transmit Request (logical), Message Lost Arbitration and Message Aborted Flag.
This is as far as I could get. I do not have an osciloscope, so I cannot look at the data between the ICs.
The following was tried without success:
- taking out the switch for changing between resistor sets
- adding a 10k resistor between the STB pin of TJA and ground
- checked continuity multiple times, as well as the welds and presence of shorts
Does anyone have any idea of what might be the issue here? I still have the possibility of using the dev board for communication. It looks bad and needs a lot of space compared to the on-board solution but at least it works. I really want to be able to integrate CAN bus hardware into my own circuits, but this problem is driving me crazy.
Cheers