Arduino MKRZERO dissapears from port selection after code upload from platformio

Hello,
I’m trying to get my Arduino MKRZERO to send CAN messages using this library.

When I use the library and run code with the normal Arduino IDE everything works perfectly fine. But I am trying to build a larger system that has multiple classes and files so just using the Arduino IDE is not good enough, so I’ve been trying to use PlatformIO.

However, everytime I upload code through the IDE the arduino disappears from the port selection and I cannot view anything from the serial monitor to see print statements. The only way I can get the board to show up is by double clicking the reset button which resets the board into a state where no code is running.

I also created another project built for an Arduino Uno to see if something was wrong with my MKRZERO board, and although the port problem doesn’t occur with the Uno it simply just doesn’t run the code that I upload (I’ve tried the simplest blink code and print statements).

I was wondering if platformio may have corrupted my board or what the problem could be? I’m using the most recent version of the software on VScode and the most recent Arduino IDE.

If anyone could help I’d really appreciate it!

Best,
Arya

Can you test a minimal SerialUSB sketch? What is the exact platformio.ini you’re working with?

I tested the following code:

#include <Arduino.h>

void setup() {
// put your setup code here, to run once:
SerialUSB.begin(9600); 
while(!SerialUSB);
SerialUSB.print("Hello World");
}
void loop() {
// put your main code here, to run repeatedly:
}

and I got the same problem of the port disapearing after I uploaded the code

For the version of platformio on the homepage it says the followign:
Core: 5.1.1
Home: 3.3.4

No I mean, the project folder has a platformio.ini file. What’s its content?

oh sorry this is what it says:
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; Redirecting...

[env:mkrzero]
platform = atmelsam
board = mkrzero
framework = arduino
monitor_speed = 115200

This all looks good. Can you do the project task “Clean” and then “Upload”, and copy-paste the entire output that appears here? (Code blocks can be formatted in Markdown with three backticks)

This is the output from the cleaning

Processing mkrzero (platform: atmelsam; board: mkrzero; framework: arduino)
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Removed .pio/build/mkrzero/firmware.elf
Removed .pio/build/mkrzero/firmware.bin
Removed .pio/build/mkrzero/libFrameworkArduino.a
Removed .pio/build/mkrzero/libFrameworkArduinoVariant.a
Removed .pio/build/mkrzero/.sconsign39.dblite
Removed .pio/build/mkrzero/libc73/libmcp_can.a
Removed .pio/build/mkrzero/libc73/mcp_can/mcp_can.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/WInterrupts.c.o
Removed .pio/build/mkrzero/FrameworkArduino/main.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/pulse_asm.S.o
Removed .pio/build/mkrzero/FrameworkArduino/abi.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/cortex_handlers.c.o
Removed .pio/build/mkrzero/FrameworkArduino/startup.c.o
Removed .pio/build/mkrzero/FrameworkArduino/new.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/wiring_private.c.o
Removed .pio/build/mkrzero/FrameworkArduino/pulse.c.o
Removed .pio/build/mkrzero/FrameworkArduino/Tone.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/delay.c.o
Removed .pio/build/mkrzero/FrameworkArduino/wiring_analog.c.o
Removed .pio/build/mkrzero/FrameworkArduino/wiring_digital.c.o
Removed .pio/build/mkrzero/FrameworkArduino/Uart.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/wiring.c.o
Removed .pio/build/mkrzero/FrameworkArduino/Reset.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/SERCOM.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/WMath.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/hooks.c.o
Removed .pio/build/mkrzero/FrameworkArduino/itoa.c.o
Removed .pio/build/mkrzero/FrameworkArduino/wiring_shift.c.o
Removed .pio/build/mkrzero/FrameworkArduino/compact/dtostrf.c.o
Removed .pio/build/mkrzero/FrameworkArduino/USB/USBCore.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/USB/samd21_host.c.o
Removed .pio/build/mkrzero/FrameworkArduino/USB/CDC.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/api/String.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/api/IPAddress.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/api/PluggableUSB.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/api/Common.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/api/Print.cpp.o
Removed .pio/build/mkrzero/FrameworkArduino/api/Stream.cpp.o
Removed .pio/build/mkrzero/FrameworkArduinoVariant/variant.cpp.o
Removed .pio/build/mkrzero/libd35/libSPI.a
Removed .pio/build/mkrzero/libd35/SPI/SPI.cpp.o
Removed .pio/build/mkrzero/src/Teleop.cpp.o
Removed .pio/build/mkrzero/src/main.cpp.o
Removed .pio/build/mkrzero/src/Off.cpp.o
Removed .pio/build/mkrzero/src/Button.cpp.o
Removed .pio/build/mkrzero/src/ILAS.cpp.o
Removed .pio/build/mkrzero/src/Ignition.cpp.o
Removed .pio/build/mkrzero/src/MotorController.cpp.o
Removed .pio/build/mkrzero/src/Throttle.cpp.o
Removed .pio/build/mkrzero/src/Regen.cpp.o
Removed .pio/build/mkrzero/src/Kattara.cpp.o
Removed .pio/build/mkrzero/src/A_CAN.cpp.o
Done cleaning
=============================================================== [SUCCESS] Took 0.49 seconds ===============================================================

Terminal will be reused by tasks, press any key to close it

This is the output of running the code after the clean

> Executing task in folder Katara3: platformio run --target upload <

Processing mkrzero (platform: atmelsam; board: mkrzero; framework: arduino)
-----------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelsam/mkrzero.html
PLATFORM: Atmel SAM (6.2.0) > Arduino MKRZERO
HARDWARE: SAMD21G18A 48MHz, 32KB RAM, 256KB Flash
DEBUG: Current (atmel-ice) External (atmel-ice, blackmagic, jlink)
PACKAGES: 
 - framework-arduino-samd 1.8.11 
 - framework-cmsis 1.40500.0 (4.5.0) 
 - framework-cmsis-atmel 1.2.2 
 - tool-bossac 1.10700.190624 (1.7.0) 
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 13 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <mcp_can>
|   |-- <SPI> 1.0
Building in release mode
Compiling .pio/build/mkrzero/src/A_CAN.cpp.o
Compiling .pio/build/mkrzero/src/Button.cpp.o
Compiling .pio/build/mkrzero/src/ILAS.cpp.o
Compiling .pio/build/mkrzero/src/Ignition.cpp.o
Compiling .pio/build/mkrzero/src/Kattara.cpp.o
Compiling .pio/build/mkrzero/src/MotorController.cpp.o
Compiling .pio/build/mkrzero/src/Off.cpp.o
Compiling .pio/build/mkrzero/src/Regen.cpp.o
Compiling .pio/build/mkrzero/src/Teleop.cpp.o
Compiling .pio/build/mkrzero/src/Throttle.cpp.o
Compiling .pio/build/mkrzero/src/main.cpp.o
Compiling .pio/build/mkrzero/libd35/SPI/SPI.cpp.o
Compiling .pio/build/mkrzero/libc73/mcp_can/mcp_can.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduinoVariant/variant.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/Reset.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/SERCOM.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/Tone.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/USB/CDC.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/USB/USBCore.cpp.o
Archiving .pio/build/mkrzero/libd35/libSPI.a
Archiving .pio/build/mkrzero/libFrameworkArduinoVariant.a
Indexing .pio/build/mkrzero/libd35/libSPI.a
Indexing .pio/build/mkrzero/libFrameworkArduinoVariant.a
Compiling .pio/build/mkrzero/FrameworkArduino/USB/samd21_host.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/Uart.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/WInterrupts.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/WMath.cpp.o
In file included from /Users/aryabastani/.platformio/packages/framework-arduino-samd/cores/arduino/Uart.h:23:0,
                 from /Users/aryabastani/.platformio/packages/framework-arduino-samd/variants/mkrzero/variant.h:162,
                 from /Users/aryabastani/.platformio/packages/framework-arduino-samd/cores/arduino/Arduino.h:48,
                 from /Users/aryabastani/.platformio/packages/framework-arduino-samd/cores/arduino/Uart.cpp:19:
/Users/aryabastani/.platformio/packages/framework-arduino-samd/cores/arduino/SafeRingBuffer.h: In member function 'int arduino::SafeRingBufferN<N>::read_char() [with int N = 64]':
/Users/aryabastani/.platformio/packages/framework-arduino-samd/cores/arduino/SafeRingBuffer.h:44:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
Archiving .pio/build/mkrzero/libc73/libmcp_can.a
Indexing .pio/build/mkrzero/libc73/libmcp_can.a
Compiling .pio/build/mkrzero/FrameworkArduino/abi.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/api/Common.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/api/IPAddress.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/api/PluggableUSB.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/api/Print.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/api/Stream.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/api/String.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/compact/dtostrf.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/cortex_handlers.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/delay.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/hooks.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/itoa.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/main.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/new.cpp.o
Compiling .pio/build/mkrzero/FrameworkArduino/pulse.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/pulse_asm.S.o
Compiling .pio/build/mkrzero/FrameworkArduino/startup.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/wiring.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/wiring_analog.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/wiring_digital.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/wiring_private.c.o
Compiling .pio/build/mkrzero/FrameworkArduino/wiring_shift.c.o
Archiving .pio/build/mkrzero/libFrameworkArduino.a
Indexing .pio/build/mkrzero/libFrameworkArduino.a
Linking .pio/build/mkrzero/firmware.elf
Checking size .pio/build/mkrzero/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   6.4% (used 2084 bytes from 32768 bytes)
Flash: [=         ]   5.6% (used 14596 bytes from 262144 bytes)
Building .pio/build/mkrzero/firmware.bin
Configuring upload protocol...
AVAILABLE: atmel-ice, blackmagic, jlink, sam-ba
CURRENT: upload_protocol = sam-ba
Looking for upload port...
Auto-detected: /dev/cu.usbmodem141101
Forcing reset using 1200bps open/close on port /dev/cu.usbmodem141101
Waiting for the new upload port...
Uploading .pio/build/mkrzero/firmware.bin
Atmel SMART device 0x10010005 found
Erase flash
done in 0.865 seconds

Write 14596 bytes to flash (229 pages)

[========                      ] 27% (64/229 pages)
[================              ] 55% (128/229 pages)
[=========================     ] 83% (192/229 pages)
[==============================] 100% (229/229 pages)
done in 0.089 seconds

Verify 14596 bytes of flash with checksum.
Verify successful
done in 0.012 seconds
CPU reset.
====================================================== [SUCCESS] Took 6.91 seconds ======================================================

Terminal will be reused by tasks, press any key to close it.

this is also what the side bar looks like too after I uploaded the code (I’m not sure if it’s significant that after I ran the code - which is just a built_in_led blink code right now to test which doesn’t even work - there’s a red x on the side bar):

Also it gives this problem:

There’s of code files there, have you tried this in a new blank project with just the code above?

When I try that code in a blank project the system works fine

I’ve done some trouble shooting and I have realized that my problem is that when I instantiate my main class called “kattara”, that intianiates a bunch of other classes.

One of those is a class that is built in order to send CAN messages through an mcp2515.

But as a part of instaniating that class there is a function call that is made, and when I comment out that function call I don’t get the problem of losing the arduino on the port selection.

This is the body of the function that is causing the problem:

Does anyone have an ideas of why it may be causing the problem I’ve been having?

When I run the instaniation of the class in the normal Arduino IDE I don’t have the problem with this function and everything runns normally.

Are you using the exact same library version in PlatformIO as you do in the Arduino IDE?

What version of the Arduino SAMD core are you using in the Arduino IDE board manager?

Yea I’m using the same library version in both.

I’m using the MKRZERO SAMD board in the Arduino IDE

Yes but what does the board manager say about the Arduino SAMD core version?

The board manager says that the SAMD core version is 1.8.11

Okay, that’s also the exact same version that PlatformIO uses.

So if both the core and the Arduino library are of the same version, the reason for it not working must be somewhere else…

Without a debugger or the exact code this is extremely hard to say though.

I’ve narrowed it down to this method.
When I comment out the body of this method and have it return a dummy value the problem goes away.

I’ve looked through it multiple times and can’t see what’s wrong and commenting out things hasn’t really given any concrete answers. If you could see if there are any blarring problems that would be really appreciated.

** Function name:           mcp2515_init
** Descriptions:            Initialize the controller
*********************************************************************************************************/
INT8U MCP_CAN::mcp2515_init(const INT8U canIDMode, const INT8U canSpeed, const INT8U canClock)
{

  INT8U res;

    mcp2515_reset();
    
    mcpMode = MCP_LOOPBACK;

    res = mcp2515_setCANCTRL_Mode(MODE_CONFIG);
    if(res > 0)
    {
#if DEBUG_MODE
      Serial.println(F("Entering Configuration Mode Failure...")); 
#endif
      return res;
    }
#if DEBUG_MODE
    Serial.println(F("Entering Configuration Mode Successful!"));
#endif

    // Set Baudrate
    if(mcp2515_configRate(canSpeed, canClock))
    {
#if DEBUG_MODE
      Serial.println(F("Setting Baudrate Failure..."));
#endif
      return res;
    }
#if DEBUG_MODE
    Serial.println(F("Setting Baudrate Successful!"));
#endif

    if ( res == MCP2515_OK ) {

                                                                        // init canbuffers
        mcp2515_initCANBuffers();

                                                                        //interrupt mode
        mcp2515_setRegister(MCP_CANINTE, MCP_RX0IF | MCP_RX1IF);

	//Sets BF pins as GPO
	mcp2515_setRegister(MCP_BFPCTRL,MCP_BxBFS_MASK | MCP_BxBFE_MASK);
	//Sets RTS pins as GPI
	mcp2515_setRegister(MCP_TXRTSCTRL,0x00);

        switch(canIDMode)
        {
            case (MCP_ANY):
            mcp2515_modifyRegister(MCP_RXB0CTRL,
            MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK,
            MCP_RXB_RX_ANY | MCP_RXB_BUKT_MASK);
            mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK,
            MCP_RXB_RX_ANY);
            break;
            

//KEEP COMMENTED
/*          The followingn two functions of the MCP2515 do not work, there is a bug in the silicon.
            case (MCP_STD): 
            mcp2515_modifyRegister(MCP_RXB0CTRL,
            MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK,
            MCP_RXB_RX_STD | MCP_RXB_BUKT_MASK );
            mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK,
            MCP_RXB_RX_STD);
            break;

            case (MCP_EXT): 
            mcp2515_modifyRegister(MCP_RXB0CTRL,
            MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK,
            MCP_RXB_RX_EXT | MCP_RXB_BUKT_MASK );
            mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK,
            MCP_RXB_RX_EXT);
            break;
*/

            case (MCP_STDEXT): 
            mcp2515_modifyRegister(MCP_RXB0CTRL,
            MCP_RXB_RX_MASK | MCP_RXB_BUKT_MASK,
            MCP_RXB_RX_STDEXT | MCP_RXB_BUKT_MASK );
            mcp2515_modifyRegister(MCP_RXB1CTRL, MCP_RXB_RX_MASK,
            MCP_RXB_RX_STDEXT);
            break;
    
            default:
#if DEBUG_MODE        
            Serial.println(F("`Setting ID Mode Failure..."));
#endif           
            return MCP2515_FAIL;
            break;
}    


        res = mcp2515_setCANCTRL_Mode(mcpMode);                                                                
        if(res)
        {
#if DEBUG_MODE        
          Serial.println(F("Returning to Previous Mode Failure..."));
#endif           
          return res;
        }

    }
    return res;
}

Update:
I’ve narrowed it down even more.

From what I can tell the first method that is called in the above code named mcp2515_reset() is causing the problem.

Here is the code:

/*********************************************************************************************************
** Function name:           mcp2515_reset
** Descriptions:            Performs a software reset
*********************************************************************************************************/
void MCP_CAN::mcp2515_reset(void)                                      
{   
    //The line below is causing the problem!!
    SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0));
   
    MCP2515_SELECT();
    spi_readwrite(MCP_RESET);
    MCP2515_UNSELECT();
    SPI.endTransaction();
    delay(5); // If the MCP2515 was in sleep mode when the reset command was issued then we need to wait a while for it to reset properly
 
}

Within this code I believe that the method call: SPI.beginTransaction(SPISettings(10000000, MSBFIRST, SPI_MODE0)); is what is causing the problem. But I don’t know why or how to fix it, if anyone has any ideas of how to troubleshoot this they would be greatly appreciated.