platformio / platform-atmelavr

Atmel AVR: development platform for PlatformIO
https://registry.platformio.org/platforms/platformio/atmelavr
Apache License 2.0
138 stars 105 forks source link

PlatformIO MiniCore support #138

Closed MCUdude closed 5 years ago

MCUdude commented 5 years ago

Hi!

I'm starting to look into PlatformIO MiniCore support. @ivankravets would you prefer if I issued some PRs, or do you want to add it yourself?

Did I miss anything?

ivankravets commented 5 years ago

We are going to refactor all our bord list and split them into:

We need that to allow developers to start their projects from DEVICES if they have a custom board and would like to use bare programming or SDK.

So, instead of duplicating boards by Arduino cores it is better to have 1 DEVICE and explain in PlatformIO docs for Atmel AVR dev/platform how to switch between Arduino cores and variants.

For example, a few devices:

Related issue https://github.com/platformio/platformio-core/issues/2413

MCUdude commented 5 years ago

So, instead of duplicating boards by Arduino cores it is better to have 1 DEVICE and explain in PlatformIO docs for Atmel AVR dev/platform how to switch between Arduino cores and variants.

This is a very good idea! My Arduino cores follow this exact philosophy and are chip oriented instead of board oriented. The user has a specific microcontroller and wants to use the Arduino framework to develop an application. Clock speed, Bootloaders, upload port, upload speed, brown-out detection, LTO (and so on) is all up to the user.

But even though the user has selected an ATmega328P as the target, the user may want to program it as an Arduino UNO (locked to 16 MHz external oscillator), use it barebone (avrlibc only) or program it through MiniCore.

How do we select what platform or API to use?

MCUdude commented 5 years ago

@ivankravets any hints?

ivankravets commented 5 years ago

Sorry for the delay, I had a small vacation.

Can you make PR with generic board manifests as described above? ATMEGA168PA.json?

MCUdude commented 5 years ago

Sorry, I'm heading to Shenzhen, China for a week on a business trip. I can do it as soon as I get back. I'm not even sure Github is available in chine without the use of VPN.

Do you want all variants to have a dedicated JSON file or just the ones that actually have different device signatures? ATmega168P and ATmega168PA have the same signature. ATmega168/A and ATmega168P/PA does not.

I just did a quick edit of an existing file. Is this how you want the files to look like? If not, please provide an example, and I'll make a PR as soon as I can.

{
  "build": {
    "core": "MiniCore",
    "extra_flags": "-DARDUINO_AVR_ATmega168",
    "f_cpu": "16000000L",
    "mcu": "atmega168p",
    "variant": "miniycore_standard"
  },
  "frameworks": [
    "arduino"
  ],
  "name": "MiniCore ATmega168P/PA",
  "upload": {
    "maximum_ram_size": 1024,
    "maximum_size": 15872,
    "protocol": "arduino",
    "require_upload_port": true,
    "speed": 115200
  },
  "url": "https://github.com/MCUdude/MiniCore",
  "vendor": "MCUdude"
}
ivankravets commented 5 years ago

How about this ATMEGA168P.json?

{
  "version": 2,
  "device": {
    "mcu": "ATmega168P",
    "flash": 16384, 
    "ram": 1024,
    "eeprom": 512,
    "cpu_frequency": 20000000
  },
  "build": {
    "frameworks": {
      "arduino": {
        "core": "MiniCore",
        "extra_flags": "-DARDUINO_AVR_ATMEGA168P",
        "cpu_frequency": "16000000L",
        "variant": "miniycore_standard"
      }      
    },
    "extr_flags": "... common flags for all frameworks"
  },
  "frameworks": [
    "arduino"
  ],
  "name": "ATMEGA168P",
  "upload": {
    "maximum_ram_size": 1024,
    "maximum_size": 15872,
    "protocol": "arduino",
    "require_upload_port": true,
    "speed": 115200
  },
  "url": "https://www.microchip.com/wwwproducts/en/ATmega168P",
  "vendor": "Microchip"
}
MCUdude commented 5 years ago

Looks good to me, but I have a few questions.

First, what does "version": 2 means? Let's say I want to change F_CPU. Would I still add board_build.f_cpu = 8000000L to platformio.ini?

This is the template platformio.ini file I use for my projects. What would it look like with the new JSON file?


; PlatformIO Project Configuration File for MightyCore
; https://github.com/MCUdude/MightyCore/
;
;   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
; https://github.com/MCUdude/MightyCore/PlatformIO.md
; https://docs.platformio.org/page/projectconf.html

; ENVIRONMENT SETTINGS
[env:MightyCore]
platform = atmelavr
;platform = https://github.com/platformio/platform-atmelavr.git
framework = arduino

; TARGET SETTINGS
; PlatformIO requires the board parameter. Must match your actual hardware
board = mightycore32
; Actual target 
board_build.mcu = atmega32
; Clock frequency in [Hz]
board_build.f_cpu = 16000000L

; BUILD OPTIONS
; Current pinout
board_build.variant = mightycore_standard
; Comment out to enable LTO (this line unflags it)
build_unflags = -flto
; Extra build flags
;build_flags = 

; UPLOAD SETTINGS
; Serial port. Use COMx on Windows
;upload_port = /dev/cu.usbserial*
; Upload baud rate
board_upload.speed = 115200

; Upload using programmer
;upload_protocol = usbasp
; Aditional upload flags
;upload_flags = -Pusb

; SERIAL MONITOR OPTIONS
; Monitor and upload port is the same by default
;monitor_port = 
; Serial monitor baud rate
monitor_speed = 9600
ivankravets commented 5 years ago

I've just thought... to avoid any delays you can make a PR with own boards in the old format. Later, we will reformat them.

As you described here => https://github.com/platformio/platform-atmelavr/issues/138#issuecomment-490010934

MCUdude commented 5 years ago

That's fine! Is it OK that I do changes to existing JSON files as well (atmega328pb.json for instance)?

ivankravets commented 5 years ago

I see that atmega328pb.json uses an official Arduino core. I propose to keep it without changes. Maybe, some developers use it in their projects.

As I understand correctly, they can switch to your core later without any issues.

MCUdude commented 5 years ago

I see that atmega328pb.json uses an official Arduino core.

The 328pb isn't officially supported by the official Arduino core. There's no pin mapping for the extra pins, and PWM generated by timer3 and 4 isn't available through analogWrite.

As I understand correctly, they can switch to your core later without any issues.

Yes, this is true. It's 100% compatible with the official core but offers some extended functionality that the 328pb needs. It would be a pity if we can't have proper 328pb support in PlatformIO IMO. Please let me know how we can solve this.

ivankravets commented 5 years ago

Ah, I got you. Yes, please update atmega328pb.json too. Thanks!

MCUdude commented 5 years ago

Do you want the "name" fields to be: "ATmega328P" "Atmel ATmega328P" "MCUdude ATmega328P" "MiniCore ATmega328P"

Personally, I'd prefer the first one if it's possible to choose between the official Arduino framework and MiniCore in a separate menu when you start a new project. Is this option something that should be added to the JSON files?

MCUdude commented 5 years ago

.. And how should I deal with devices that doesn't have a bootloader (and will have to be programmed with a program like USBasp or USBtinyISP). Like this?


{
  "build": {
    "core": "MiniCore",
    "extra_flags": "-DARDUINO_AVR_ATmega48P",
    "f_cpu": "16000000L",
    "mcu": "atmega48p",
    "variant": "minicore_standard"
  },
  "frameworks": [
    "arduino"
  ],
  "name": "ATmega48P/PA",
  "upload": {
    "maximum_ram_size": 512,
    "maximum_size": 4096,
    "protocol": "arduino",
    "require_upload_port": false,
  },
  "url": "https://github.com/MCUdude/MiniCore",
  "vendor": "MCUdude"
}
ivankravets commented 5 years ago

"url": "https://github.com/MCUdude/MiniCore", "vendor": "MCUdude"

What is MCUdude? Do we have physical MCUdude boards?

MCUdude commented 5 years ago

Well, I do for MightyCore, but not for MiniCore.

Should it say Microchip instead? How about the URL to the MiniCore repo? Lots of useful documentation for the user there.

ivankravets commented 5 years ago

Oh, sorry :( Now I understand. MigthyCore this is software part, not hardware. In this case, I recommend using "device" notation for file name and provide documentation on how to switch between cores. We already did this for ST STM32 => http://docs.platformio.org/en/latest/platforms/ststm32.html#switching-between-arduino-cores

They also have a few Arduino cores.

I recommend the next format for ATMEGA168P.json:

{
  "build": {
    "core": "MiniCore",
    "extra_flags": "..",
    "f_cpu": "16000000L",
    "mcu": "...",
    "variant": "minicore_standard"
  },
  "frameworks": [
    "arduino"
  ],
  "name": "ATMEGA168P",
  "upload": {
    "maximum_ram_size": 1024,
    "maximum_size": 15872,
    "protocol": "arduino",
    "require_upload_port": true,
    "speed": 115200
  },
  "url": "https://www.microchip.com/wwwproducts/en/ATmega168P",
  "vendor": "Microchip"
}

Later, it will be easy for us to convert it to 2.0 format. The important thing here is a board identifier. It will be the same, so, end developer will not be affected later.

What do you think?

MCUdude commented 5 years ago

What do you think?

Looks good to me! But my previous question still remains unanswered. How should I handle ATmega48, which doesn't have a bootloader and require a dedicated programmer? Is it OK if I add require_upload_port": false, and remove the speed option?

EDIT: Is it correct that extra_flags have two dots, while mcu have three dots?

ivankravets commented 5 years ago

Is it OK if I add require_upload_port": false, and remove the speed option?

Yes, please update upload section to the working format.

Is it correct that extra_flags have two dots, while mcu have three dots?

You need to replace these ..... with real data. "mcu": "..." => "mcu": "atmega168p", and "extra_flags": ".." to "extra_flags": "-DARDUINO_AVR_ATMEGA168" or similar to that.

MCUdude commented 5 years ago

You need to replace these ..... with real data. "mcu": "..." => "mcu": "atmega168p", and "extra_flags": ".." to "extra_flags": "-DARDUINO_AVR_ATMEGA168" or similar to that.

Thanks! I found it a bit unclear since ATmega168 was already there under "name"

ivankravets commented 5 years ago

Thanks! I found it a bit unclear since ATmega168 was already there under "name"

We don't use name field in the build process and show only in UI and docs.

MCUdude commented 5 years ago

Last question, where should I put the MiniCore repo URL?

ivankravets commented 5 years ago

We will create a new section in our docs similar to STM32. Will it work for you? You can even propose your PR to this section. I'll show later where is this place.

MCUdude commented 5 years ago

That's OK for me! Do you mean something like this? It would be nice if the repo URL was visible to the user for the user when MiniCore is selected as core.

{
  "build": {
    "core": "MiniCore",
    "core_url": "https://github.com/MCUdude/MiniCore",
    "extra_flags": "-DARDUINO_AVR_ATmega168P",
    "f_cpu": "16000000L",
    "mcu": "atmega168p",
    "variant": "minicore_standard"
  },
  "frameworks": [
    "arduino"
  ],
  "name": "ATmega168P/PA",
  "upload": {
    "maximum_ram_size": 1024,
    "maximum_size": 15872,
    "protocol": "arduino",
    "require_upload_port": true,
    "speed": 115200
  },
  "url": "https://www.microchip.com/wwwproducts/en/ATmega168P",
  "vendor": "Microchip"
}
ivankravets commented 5 years ago

This core_url will not be visible for and developer. We will put this link in docs.

MCUdude commented 5 years ago

That's perfectly fine.

Many of the AVR chips have different suffixes but the same device signature. ATmega328P and ATmega328PA share the same signature. Is it OK if these two variants share the same JSON file too?

Filename: atmega48p.json

{
  "build": {
    "core": "MiniCore",
    "extra_flags": "-DARDUINO_AVR_ATmega48P",
    "f_cpu": "16000000L",
    "mcu": "atmega48p",
    "variant": "minicore_standard"
  },
  "frameworks": [
    "arduino"
  ],
  "name": "ATmega48P/PA",
  "upload": {
    "maximum_ram_size": 512,
    "maximum_size": 4096,
    "protocol": "arduino",
    "require_upload_port": false,
  },
  "url": "https://www.microchip.com/wwwproducts/en/ATmega48P",
  "vendor": "Microchip"
}
ivankravets commented 5 years ago

Is it OK if these two variants share the same JSON file too?

Sure!


I like it! Just one thing, to keep non-board profiles (for devices) in the capital case => ATMEGA48P.json

ivankravets commented 5 years ago

Please re-test with upstream version http://docs.platformio.org/en/latest/platforms/atmelavr.html#upstream

MCUdude commented 5 years ago

I'm able to compile for ATmega328P and ATmega328PB when adding platform = https://github.com/platformio/platform-atmelavr.git to platformio.ini.

platform = atmelavr does not work.

However, I'm not able to create a new project with VSCode. See platformio #2606 for details.