How to add new variant to Atmel SAM platform?

Hello, I’m trying to add support for the Macchina M2. Support was originally added in November, 2017, however it was removed at some point between now and then.

The M2 is based on the Arduino Due but there are a few differences, so even though the dueUSB board does compile and run, I’d like to be able to use some variant-specific defines.

So far the only way I’ve been able to get this to work is the following:

  1. Create boards/macchina_m2.json with the contents:
{
  "build": {
    "arduino": {
      "ldscript": "flash.ld"
    },
    "core": "arduino",
    "cpu": "cortex-m3",
    "extra_flags": "-D__SAM3X8E__ -DARDUINO_SAM_DUE -DARDUINO_M2 -DMACCHINA_M2",
    "f_cpu": "84000000L",
    "hwids": [
      [
        "0x2341",
        "0x003E"
      ],
      [
        "0x2A03",
        "0x003E"
      ]
    ],
    "mcu": "at91sam3x8e",
    "usb_product": "Arduino Due",
    "variant": "m2"
  },
  "connectivity": [
    "can"
  ],
  "debug": {
    "jlink_device": "ATSAM3X8E",
    "openocd_chipname": "at91sam3X8E",
    "openocd_target": "at91sam3ax_8x",
    "svd_path": "ATSAM3X8E.svd"
  },
  "frameworks": [
    "arduino",
    "simba"
  ],
  "name": "Macchina M2",
  "upload": {
    "disable_flushing": true,
    "maximum_ram_size": 98304,
    "maximum_size": 524288,
    "native_usb": true,
    "protocol": "sam-ba",
    "protocols": [
      "sam-ba",
      "jlink",
      "blackmagic",
      "atmel-ice",
      "stlink"
    ],
    "require_upload_port": true,
    "use_1200bps_touch": true,
    "wait_for_upload_port": true
  },
  "url": "https://docs.macchina.cc/m2-docs",
  "vendor": "Macchina"
}
  1. Clone GitHub - macchina/arduino-boards-sam: Arduino IDE support for the M2 and other SAM-based boards. to /tmp
  2. Add board_build.variants_dir = /tmp/arduino-boards-sam/sam/variants to platformio.ini

My questions are as follows:

  1. How can I add this custom variant to the Atmel SAM platform? I see how I can add the board to the existing GitHub repo, but how can I add the variant such that I do not have to locally clone a repo and alter board_build.variants_dir?
  2. How does PlatformIO generate package.json for existing cores? For example, framework-arduino-sam is sourced as from https://github.com/arduino/ArduinoCore-sam, however there’s an additional package.json in my ~/.platformio/packages/framework-arduino-sam directory that does not appear in the open source repo.

I’d still like to find out an answer to this, if anyone knows.

So basically you want the variants/m2 folder included in the Arduino framework? Then you’ll need to have a modified framework for this. Fork the current GitHub - arduino/ArduinoCore-sam, add a package.json to it based on the previous version of it, add your variant folder to it, then you can point to that new repo by using a platform_packages expression that alters the source for the framework-arduino-sam package. Since your board JSON file already declares "variant": "m2" (and if you remove board_build.variants_dir), the build process should automatically use the m2/ folder in the now modified Arduino framework core.

Of course in the big scheme of things you might want to pull-request into the main Arduino core to get the variant folder in there, so that it’s automatically included in the next release.

Another more complicated way would be if you published the modified framewokr-arduino-sam package in the trusted repository (see pio package publish documentation), then you can fork GitHub - platformio/platform-atmelsam: Atmel SAM: development platform for PlatformIO and modify the platform.json to not use PlatformIO’s package anymore but yours in regards to

And to use that then, you’d have to set a different platform = <your forked repo> link the project’s platformio.ini.

Actually if you keep things (the board variant folder) locally in the project that may not be so bad after all. That way you’re decoupled from the Arduino core and receives the updates from it, as opposed to having to update your modified package yourself.

I think package.json files are just added by PlatformIO staff when releasing a new package version if the repository doesn’t contain it by itself. For example, Arduino-ESP8266 already self-provides the package.json in the correct form and versioning themselves, so PIO people don’t need to do this.

1 Like