CAN Controller with Integrated Transceiver set with SPI - Page 1
Forum section: electronics / general

author: lorenrus
author_rank: Regular Contributor
timestamp: May 25, 2020, 06:21:54 pm
content: Hello everyone and good SundayI am checking the MCP25625 device with Arduino M0 Pro and in particular with its SPI.I have to enable loopback mode by manipulating three bits of the CANCTRL register. to manipulate them you have to first send the statement, address, then a mask and then the data.What happens to me is that as soon as I turn on I perform the reset and then I expect to see, in steps 44 and 45, the value of those registers to reset.If I press for example 44 I actually have its reset value but if then I press immediately 45 I get a value that does not match the reset value; then if I reset again and I do the vice versa I see the correct reset value of point 45 but if then I push 44 I do not see it anymore.And of course this does not allow me to understand whether I write or not and whether or not I write the bit for activating the loopback function correctly.Someone can tell me where I'm wrong.Thank you very muchCode:/******* TEST CAN ***************/#define RESET_CAN_NEG 1#define CS_CAN_NEG 2#define CAN_INT_NEG 3#define FPGA_RX_CAN 8#define FPGA_TX_CAN 9#define CAN_STAND_BY A3SPISettings SPI_Settings(20000000, MSBFIRST, SPI_MODE0);unsigned int SPI_Received_Value = 0;unsigned int SPI_Received_Value_1 = 0;void setup() {// put your setup code here, to run once:Serial.begin(9600);delay(2000);SPI.begin();pinMode(CS_CAN_NEG, OUTPUT);pinMode(RESET_CAN_NEG, OUTPUT);pinMode(CAN_STAND_BY, OUTPUT);digitalWrite(CS_CAN_NEG, HIGH);digitalWrite(RESET_CAN_NEG, HIGH);digitalWrite(CAN_STAND_BY, LOW);}void loop() {if(Serial.available()) {String Stringa = Serial.readString();int Dato = atoi(Stringa.c_str());delay(500);/**** SPI ****/if(Dato == 42) {digitalWrite(CAN_STAND_BY, HIGH);Serial.println("Chip Reset");digitalWrite(RESET_CAN_NEG, LOW);delay(50);digitalWrite(RESET_CAN_NEG, HIGH);digitalWrite(CAN_STAND_BY, LOW);//digitalWrite(CS_CAN_NEG, LOW);//SPI.transfer(0b11000000);//digitalWrite(CS_CAN_NEG, HIGH);}if(Dato == 43) {Serial.println("Active LoopBack Mode");//digitalWrite(CAN_STAND_BY, HIGH);SPI.beginTransaction(SPI_Settings);digitalWrite(CS_CAN_NEG, LOW);SPI.transfer(0b00000101); // IstructionSPI.transfer(0xF); // AddressSPI.transfer(0b11111111); // MaskSPI.transfer(0b01000000); // DatadigitalWrite(CS_CAN_NEG, HIGH);SPI.endTransaction();//digitalWrite(CAN_STAND_BY, LOW);//SPI.end();}if(Dato == 44) {//SPI.begin();Serial.println("Read Register CAN Control Register");//digitalWrite(CAN_STAND_BY, HIGH);SPI.beginTransaction(SPI_Settings);digitalWrite(CS_CAN_NEG, LOW);SPI.transfer(0b00000011); // IstructionSPI.transfer(0xF); // AddressSPI_Received_Value = SPI.transfer(0);digitalWrite(CS_CAN_NEG, HIGH);SPI.endTransaction();//digitalWrite(CAN_STAND_BY, LOW);Serial.println(SPI_Received_Value);//SPI.end();}if(Dato == 45) {//SPI.begin();Serial.println("Read Register CAN Status Register");//digitalWrite(CAN_STAND_BY, HIGH);SPI.beginTransaction(SPI_Settings);digitalWrite(CS_CAN_NEG, LOW);SPI.transfer(0b00000011); // IstructionSPI.transfer(0xE); // AddressSPI_Received_Value_1 = SPI.transfer(0);digitalWrite(CS_CAN_NEG, HIGH);SPI.endTransaction();//digitalWrite(CAN_STAND_BY, LOW);Serial.println(SPI_Received_Value_1);//SPI.end();}}}

author: lorenrus
author_rank: Regular Contributor
timestamp: May 28, 2020, 06:26:06 am
content: Can you help me ?