Robot-Will / Stino

A Sublime Text Plugin for Arduino
Other
1.58k stars 250 forks source link

Improper compilation causing endless rebooting #498

Open CptAwe opened 6 years ago

CptAwe commented 6 years ago

Edit: this issue is solved. See solution at the bottom.


If the following code is compiled through Stino then the arduino will reset itself endlessly. On the other hand, if the code is compiled through the arduino IDE then everything is fine.

The problem seems to appear when the three functions (WakeUp(), ShowInfo(), Select_level_1()) in loop() are executed in that order.

I suspect it has something to do with the MFRC522 or SPI libraries or the way serial communication is compiled and handled through Stino.

Sorry for the endless code, but I don't know exactly where the problem is.

Environment:

 /*   RFID-RC522 (SPI connection)
 *   
 *    RC522           Arduino (UNO)
 *     SDA  -----------  9 (See SDA_PIN) [Slave Select pin]
 *     SCK  -----------  13
 *     MOSI -----------  11
 *     MISO -----------  12
 *     IRQ  ----------- 
 *     GND  -----------  GND
 *     RST  -----------  8 (See RST_PIN)
 *     3.3V -----------  3.3V
 *     
 */

#include <MFRC522.h>
#include <SPI.h>

#define RST_PIN 8
#define SDA_PIN 9

byte Card[16][4];

MFRC522 NFC(SDA_PIN, RST_PIN);

void WakeUp(bool debug=false);
void ShowInfo(bool debug=false);
void Select_level_1(bool debug=false);

void setup() {

    Serial.begin(19200);// Initialize serial communications with the PC

    Serial.println("\n~~~~~~~~~~");
    Serial.print("Starting SPI...");
    SPI.begin();                                    // Init SPI bus
    Serial.println(" Done!");
    Serial.print("Initializing the RC522...");
    NFC.PCD_Init();                                 // Init MFRC522
    Serial.println(" Done!");
    //  NFC.PCD_DumpVersionToSerial();              // Show details of PCD - MFRC522 Card Reader details
    Serial.println("Ready...");
    Serial.println("~~~~~~~~~~");

}

void loop() {

    // if( !NFC.PICC_IsNewCardPresent()) {
    //  return;
    // }

    // if ( ! NFC.PICC_ReadCardSerial()) {
    //  return;
    // }

    WakeUp();
    ShowInfo(true);

    Select_level_1(true);

    Serial.println("Done...");
    Serial.println("~~~~~~~~~~");
    while(true){}

}

// Wakes up the card
void WakeUp(bool debug=false){
    Serial.println("Waking Up the card...");

    MFRC522::StatusCode status;

    byte response[2];
    status=NFC.PICC_REQA_or_WUPA(NFC.PICC_CMD_WUPA, response, 2);

    Serial.print("   PICC_REQA_or_WUPA(): ");
    Serial.println(NFC.GetStatusCodeName(status));

    if(debug){
        Serial.println("   Expected: 44 0");
        Serial.print("   Response: ");
        for (byte i=0; i<2; i++){
            Serial.print(response[i],HEX);
            Serial.print(" ");
        }
    Serial.println();
    }
}

// fills the "Card[16][4]" with 0s
void ResetInfo(){
    for (int i=0; i<=15; i++){
        for (int j=0; j<=4; j++){
            Card[i][j]=0;
        }
    }
}

// fills the "Card[16][4]" with data
void ReadInfo(bool debug=false) {

    ResetInfo();

    MFRC522::StatusCode status;

    byte buffer[18]; // 16 (data) + 2 (CRC)

    for (byte page=0; page<=15; page+=4){

        byte byteCount = sizeof(buffer);
        status=NFC.MIFARE_Read(page,buffer,&byteCount);
        if (status != NFC.STATUS_OK) {
          Serial.print("MIFARE_Read(): ");
          Serial.println(NFC.GetStatusCodeName(status));
          return;
        }

        //     [page][index]
        //      0-15   0-3
        // Card [16]   [4]

        //      0-3   page 0
        //      4-7   page 1
        //      8-11  page 2
        //     12-15  page 3
        // response[16+2]

        int i_=0;
        for (int i=page; i<=page+3; i++){
          for (int j=0; j<=3; j++){
            Card[i][j]=buffer[4*i_ + j];
          }
          i_++;
        }

    }

    //  This is to stop the card from sending the info over and over again
    // NFC.PICC_HaltA();
}

// Nice way to show the card's data
void ShowInfo(bool debug=false){
    ReadInfo(debug);

    Serial.print("--------------------");
    for (byte i=0; i<=15; i++){
        Serial.print("\n ");
        for (byte j=0; j<=3; j++){
            Serial.print(Card[i][j],HEX);
            Serial.print(" ");
        }
    }
    Serial.print("\n--------------------\n");
}

void Select_level_1(bool debug){

    MFRC522::StatusCode status;

    byte command[9];
    command[0]=0x93; // Select command
    command[1]=0x70; // Select argument
    command[2]=0x88; // Cascade Tag

    command[3]=Card[0][0]; // ID0
    command[4]=Card[0][1]; // ID1
    command[5]=Card[0][2]; // ID2
    command[6]=0x88^Card[0][0]^Card[0][1]^Card[0][2];// BCC1

    status=NFC.PCD_CalculateCRC(command,2,&command[7]);
    if (debug){
        Serial.print("PCD_CalculateCRC(): ");
        Serial.println(NFC.GetStatusCodeName(status));
    }

    // This part causes the endless reseting
    for (int i = 0; i < 9; i++)
    {
        Serial.print(command[i],HEX);
        Serial.print(" ");
    }

    // byte command_size=sizeof(command);

    // byte response[3];
    // byte response_size=sizeof(response);
    // status=NFC.PCD_TransceiveData(   command,
    //                              command_size,
    //                              response,
    //                              response_size,
    //                              NULL,
    //                              0,
    //                              true);
    // if (debug){
    //  Serial.print("PCD_TransceiveData(): ");
    //     Serial.println(NFC.GetStatusCodeName(status));
    // }

}
CptAwe commented 6 years ago

Well, after some sleep I solved the issue.

For future reference: The Arduino will reset itself if you try to access a value of an array outside of it's original size (see: ResetInfo() > "for (int j=0; j<=4; j++)")

This issue is solved and has nothing to do with the plugin!