Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PlatformIO problem with setting #329

Closed
mikrocoder opened this issue Aug 19, 2022 · 17 comments
Closed

PlatformIO problem with setting #329

mikrocoder opened this issue Aug 19, 2022 · 17 comments

Comments

@mikrocoder
Copy link
Contributor

Hello,

do you know anything about PlatformIO? Somehow there is still one setting wrong.

I can compile and transfer a blinksketch but the led does not blink. What is wrong?

This is an AVR128DB64 with bootloader to which a FTDI232RL is connected and works with the Arduino IDE and your DxCore. It is flashed via USB > ComPort.

What is wrong set in PlatformIO?

#include <Arduino.h>

void setup() {
  pinMode(PIN_PB4, OUTPUT);
}

void loop() {
  digitalWrite(PIN_PB4, HIGH);
  delay(200);
  digitalWrite(PIN_PB4, LOW);
  delay(800);
}

platformio.ini

[platformio]
default_envs = Upload_USB

[env]
platform = atmelmegaavr
framework = arduino
board = AVR128DB64
board_build.variant = 64pin-standard
board_build.f_cpu = 16000000L

[env:Upload_USB]
upload_protocol = arduino
upload_port = COM[5]
upload_speed = 115200
upload_flags = 

Output:

Processing Upload_USB (platform: atmelmegaavr; framework: arduino; board: AVR128DB64)
-------------------------------------------------------------------------------------------------------Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelmegaavr/AVR128DB64.html
PLATFORM: Atmel megaAVR (1.6.0) > AVR128DB64     
HARDWARE: AVR128DB64 16MHz, 16KB RAM, 128KB Flash
PACKAGES:
 - framework-arduino-megaavr-dxcore @ 1.4.10     
 - tool-avrdude-megaavr @ 3.60300.220118 (6.3.0) 
 - toolchain-atmelavr @ 3.70300.220127 (7.3.0)   
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 17 compatible libraries
Scanning dependencies...
Dependency Graph
|-- SPI @ 1.0.2
|-- Wire @ 2.0.6        
Building in release mode
Checking size .pio\build\Upload_USB\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [          ]   0.0% (used 4 bytes from 16384 bytes)   
Flash: [          ]   0.7% (used 958 bytes from 131072 bytes)
Configuring upload protocol...
AVAILABLE: arduino
CURRENT: upload_protocol = arduino
Looking for upload port...
Auto-detected: COM5
Uploading .pio\build\Upload_USB\firmware.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e970b (probably avr128db64)
avrdude: reading input file ".pio\build\Upload_USB\firmware.hex"
avrdude: writing flash (1470 bytes):

Writing | ################################################## | 100% 0.09s

avrdude: 1470 bytes of flash written
avrdude: verifying flash memory against .pio\build\Upload_USB\firmware.hex:
avrdude: load data flash data from input file .pio\build\Upload_USB\firmware.hex:
avrdude: input file .pio\build\Upload_USB\firmware.hex contains 1470 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.07s

avrdude: verifying ...
avrdude: 1470 bytes of flash verified

avrdude: safemode: Fuses OK (E:FF, H:FF, L:FF)

avrdude done.  Thank you.
``````
@MCUdude
Copy link
Contributor

MCUdude commented Aug 19, 2022

Workout looking into it, it's probably macro that has not been set in the PlatformIO build.

DxCore's boards.txt adds various macros as build flags (-DSOMEVAL=1), and this has to be replicated in the PlatformIO build configuration. @brunob45 was the one that added DxCore support to PlatformIO, but the implementation has to be kept up to date for it to work with newer DxCore releases.

@mikrocoder
Copy link
Contributor Author

Thanks for your info. Is brunob45 here on GitHub or on the PlatformIO forum? Basically I understand you that I can't use the DxCore with PlatformIO yet, because nothing works yet.

Is there a way to flash using Atmel-ICE and still use the DxCore Package for programming? Then I also have to do without the bootloader.

@SpenceKonde
Copy link
Owner

SpenceKonde commented Aug 19, 2022 via email

@MCUdude
Copy link
Contributor

MCUdude commented Aug 19, 2022

But it's totally possible
and somewhere I habe an open issue requesting help from people with the
necessary knowledge to write guides for alternative ides because I get tons
of questions about them that I can't answer. The only ide I know is arduino

My time is quite limited, but I was pretty deep into the PlatformIO build system a while ago. You don't have to learn how to use it, but you should take into account how it gathers all the necessary bits and pieces from the platformio.ini project file before it starts the build. I also have various macros (-DSOMETHING) in the MegaCoreX boards.txt file, but I always try to have a fallback state in the core, in case macros aren't explicitly defined at compile time.

@brunob45
Copy link
Contributor

brunob45 commented Aug 20, 2022

@MCUdude Thanks for the credits, but the Platformio team has taken over the porting of DxCore to Platformio IDE.

As of right now, the DxCore version 1.4.10 is officially released: https://registry.platformio.org/tools/platformio/framework-arduino-megaavr-dxcore

I have made a blink sketch for the AVR128DA48 here. It should work the same for a DB-series, but I can't test it as I only have DA-series chips.

No need to set board_build.variant and f_cpu, the Platformio default values should be fine.

@mikrocoder
Copy link
Contributor Author

Hello,

Maybe you can help me.

PlatformIO has only been freshly installed for a few days. The DxCore package is also up to date on 1.4.10 according to the output.

Now I had to find out how to install 'pymcuprog'. Okay, it works. Error message about this is gone.

However, it still does not work. A socket.py is supposedly not found. But it is definitely present in the path. What do I have to do?

Uploading .pio\build\Upload_USB\firmware.hex
Traceback (most recent call last):
  File "C:\Users\Worker\.platformio\python3\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  file "C:\Users\Worker\.platformio\python3\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\Worker\AppData\Local\Programs\Python\Python310\Scripts\pymcuprog.exe\__main__.py", line 4, in <module>
  File "C:\Users\Worker\AppData\Local\Programs\Python\Python310\lib\site-packages\pymcuprog\pymcuprog.py", line 12, in <module>
    from logging.config import dictConfig
  File "C:\Users\Worker\.platformio\python3\lib\logging\config.py", line 30, in <module>
    import logging.handlers
  File "C:\Users\Worker\.platformio\python3\lib\logging\handlers.py", line 26, in <module>
    import logging, socket, os, pickle, struct, time, re
  File "C:\Users\Worker\.platformio\python3\lib\socket.py", line 51, in <module>
    import _socket
ImportError: DLL load failed while importing _socket: The specified module was not found.
*** [upload] Error 1

Also, is this true for me at all? I want to flash via USB > FTDI232RL > USART bootloader > µC. Is the upload protocol the right one for me? In the Arduino IDE it all works.

[platformio]
default_envs = Upload_USB

[env]
platform = atmelmegaavr
framework = arduino
board = AVR128DB64
;board_build.variant = 64pin-standard
;board_build.f_cpu = 160000L

[env:Upload_USB]
upload_protocol = custom
upload_command = pymcuprog erase && pymcuprog write -f $SOURCE

Because of f_cpu. 16MHz are default. okay. But how does PlatformIO know how and with what I clock the controller? For me this is too much default and too much hidden settings.

@mikrocoder
Copy link
Contributor Author

mikrocoder commented Aug 20, 2022

The solution is here: platformio/platform-atmelmegaavr#41
This setting works.
#319 (comment)

@SpenceKonde
Copy link
Owner

SpenceKonde commented Aug 21, 2022

I will note that this is in essence a duplicate of #319. A lot of our users are on platformio, about which I know about as much as my cat knows about AVR assembly (which is to say, not very much - I mean, she can't even name a single instruction that got timing enhancements in AVRxt versus AVRe+.

They deserve better docs. But right now the head doesn't compile blink so... priorities are priorities. What good would a document be it if just got them to the same compile errors as the Arduino IDE?

@mikrocoder
Copy link
Contributor Author

Hallo brunob45,

can you do me a favor? What settings do I need so that I can flash like in the Arduino IDE. What is hidden somewhere default and what you have to change yourself? I hope this is not too much to ask.

Arduino IDE DxCore Setting

@mikrocoder
Copy link
Contributor Author

mikrocoder commented Aug 21, 2022

@ Spence:
I think we have different views.
At the moment the knowledge about PlatformIO is only wildly scattered on the internet for me, because I can't cope with the syntax and the for me incompletely explained options.

Everything I can gather and test for myself, I can somehow merge into a documentation and publish in #319. Isn't that what you're asking everyone?
Exactly for this it needs further Thread where I and others may ask stupid questions and hopefully receive good answers, so that all this can be bundled somehow. That's why you can't close every thread right away. That doesn't make any sense. Who should find the thread to ask other questions. Closing the threads is not purposeful. That harms "your" plan to have a good doc someday.

@brunob45
Copy link
Contributor

@mikrocoder I will answer to the best of my knowledge. I did some work to port DxCore to PlatformIO a while back, but I haven't touch this since.

The config you have can be translated as

[env:avrdb]
platform = atmelmegaavr
board = AVR128DB64
framework = arduino

; Set clock speed & source - default is 24MHz internal
board_build.f_cpu = 16000000L
board_hardware.oscillator = crystal

; Set millis timer source - default is B2
board_hardware.millistimer = B2

; Set BOD level (fixed 32Hz) - default is Disabled
board_hardware.bod = 2.85v

; Set reset pin function - default is reset
board_hardware.rstpin = reset

; Cannot change startup time - default is 32ms

; Set bootloader serial port - default is no_bootloader
board_hardware.uart = ser2_alt

; Set MVIO - default is no
board_hardware.mvio_enable = no

build_unflags =
    -DTWI_MORS_SINGLE ; Unset 1x Wire (enabled by default)
    ; -DCORE_ATTACH_ALL ; Uncomment to select another attach method

build_flags = 
    -DTWI_MORS_BOTH ; Set 2x Wire
    ; -DCORE_ATTACH_NONE ; Uncomment to select this attach method
    ; -DCORE_ATTACH_OLD ; Uncomment to select this attach method

upload_protocol = pkobn_updi
; Or try the other protocols: https://github.com/MCUdude/MegaCoreX/blob/master/PlatformIO.md#upload_protocol
; If none of the above work, use
; upload_protocol = custom
; upload_command = pymcuprog erase && pymcuprog write -f $SOURCE

; run the following command to set fuses
; pio run -t fuses
; run the following command to set fuses + burn bootloader
; pio run -t bootloader

I've made a pull request (platformio/platform-atmelmegaavr#48) to improve DxCore support (added bootloader selection and millis timer parameter in the ini file), you can try by using the platform

platform = https://github.com/brunob45/platform-atmelavrdx#dev/avrea

@mikrocoder
Copy link
Contributor Author

mikrocoder commented Aug 22, 2022

Hi,

Thanks for the help. I just had to change the upload setting. I flash with USB to USART (FTDI232RL) connector onboard.

build_flags = 
    -DTWI_MORS_BOTH ; Set 2x Wire
    -DUSING_OPTIBOOT
    ; -DCORE_ATTACH_NONE ; Uncomment to select this attach method
    ; -DCORE_ATTACH_OLD ; Uncomment to select this attach method

upload_protocol = arduino
upload_speed = 115200

Therefore I continue with USB connection for now.

But what I already noticed and now still not working is the 16MHz crystal. When I remove the crystal, the led continues to blink. This means that the internal clock is still active. If I switch to 'external' nothing flashes. So far okay. With 'internal' and 'crystal' there must be an error in the detail.

The PlatformIO universe is very large. 😉 In which file are all these settings? Where are they defined, what is default and what is optional? For example, the clock source must somehow lead to a register setting in the end so that it works. Then I could look once myself.

PS: I can not this thread reopen.

@brunob45
Copy link
Contributor

brunob45 commented Aug 22, 2022

@mikrocoder
The board_hardware.oscillator setting actually set the value of the CLOCK_SOURCE define in the background. The values are:

board_hardware.oscillator -DCLOCK_SOURCE=
internal 0
crystal 1
external 2

You can use build_unflags = -DCLOCK_SOURCE and build_flags = -DCLOCK_SOURCE=1 to select the crystal.

Otherwise, I made the correction in my branch so board_hardware.oscillator = crystal should just work

platform = https://github.com/brunob45/platform-atmelavrdx#dev/avrea

Also, the information is scattered into different files. The most interesting for you would be https://github.com/SpenceKonde/DxCore/blob/master/megaavr/boards.txt. It defines the output behavior for every menu in the Arduino IDE.

The platformio part is handled by https://github.com/platformio/platform-atmelmegaavr/blob/develop/builder/frameworks/arduino.py . It's a Python script that parse the platformio.ini file and generate the compiler arguments.

@mikrocoder
Copy link
Contributor Author

Hello,

okay, thanks, helps a little.
But with this it does not compile.

C:\Users\Worker\.platformio\packages\framework-arduino-megaavr-dxcore\cores\dxcore\wiring.c:37:4: error: #error "CLOCK_SOURCE not defined. Must be 0 for internal, 1 for crystal, or 2 for external clock"
   #error "CLOCK_SOURCE not defined. Must be 0 for internal, 1 for crystal, or 2 for external clock"
    ^~~~~
*** [.pio\build\AVR128DB\FrameworkArduino\wiring.c.o] Error 1

@brunob45
Copy link
Contributor

brunob45 commented Aug 22, 2022

Edit: my bad, the build_unflags should have been -DCLOCK_SOURCE=0.
When using this, the project builds fine:

[env:avrdb]
platform = atmelmegaavr
board = AVR128DB64
framework = arduino

; Set clock speed & source - default is 24MHz internal
board_build.f_cpu = 16000000L

; Set millis timer source - default is B2
board_hardware.millistimer = B2

; Set BOD level (fixed 32Hz) - default is Disabled
board_hardware.bod = 2.85v

; Set reset pin function - default is reset
board_hardware.rstpin = reset

; Cannot change startup time - default is 32ms

; Set bootloader serial port - default is no_bootloader
board_hardware.uart = ser2_alt

; Set MVIO - default is no
board_hardware.mvio_enable = no

build_unflags =
    -DCLOCK_SOURCE=0
    -DTWI_MORS_SINGLE ; Unset 1x Wire (enabled by DEFAULT) 

build_flags =
    -DUSING_OPTIBOOT
    -DCLOCK_SOURCE=1 ; crystal
    -DTWI_MORS_BOTH ; Set 2x Wire

upload_protocol = arduino
upload_speed = 115200

@mikrocoder
Copy link
Contributor Author

Hello,

thank you very much. Now it works. 👍 😀
One must always invalidate the exact parameter before rewriting it?

@brunob45
Copy link
Contributor

I didn't think so, but apparently yes 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants