Author Topic: CAN Controller with Integrated Transceiver set with SPI  (Read 1054 times)

0 Members and 1 Guest are viewing this topic.

Offline lorenrusTopic starter

  • Regular Contributor
  • *
  • Posts: 64
  • Country: it
CAN Controller with Integrated Transceiver set with SPI
« on: May 25, 2020, 06:21:54 pm »
Hello everyone and good Sunday
I 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 much

Code: [Select]

/******* 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 A3


  SPISettings 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); // Istruction

        SPI.transfer(0xF); // Address

        SPI.transfer(0b11111111); // Mask

        SPI.transfer(0b01000000); // Data

        digitalWrite(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); // Istruction

        SPI.transfer(0xF); // Address

        SPI_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); // Istruction

        SPI.transfer(0xE); // Address

        SPI_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();
    }
 }
}


 

Offline lorenrusTopic starter

  • Regular Contributor
  • *
  • Posts: 64
  • Country: it
Re: CAN Controller with Integrated Transceiver set with SPI
« Reply #1 on: May 28, 2020, 06:26:06 am »
Can you help me ?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf