Strange PWM behavior difference between PlatformIO and ArduinoIDE

Hi All,

I have trouble with PWM behavior not consistent between platformio and arduino,
even with the same framework version installed.

PWM (analogWrite) on ArduinoIDE gives me a dimmer LED brightness, which is expected, and on PlatformIO, the light simply turns on and off.

I used the value of 0xAA to make sure the level of pwm output is above zero.

platform: Attiny1616,

in arduino, I changed the PWM pins by using the IDE tools->pwm selection for PB3~5
I also set the PORTMUX configuration in code.

using the same code, in platformIO, I noticed that the PWM only worked like a digitalWrite.
here I include two projects (toned down version)

Help is needed, if anyone can shed some light… thank you. Regards,
Rob

Please show the content of your platformio.ini

thanks for replying so quickly:


[env:ATtiny1616]
platform = atmelmegaavr
board = ATtiny1616
framework = arduino
platform_packages = framework-arduino-megaavr-megatinycore @ https://github.com/SpenceKonde/megaTinyCore/

Thanks!
I’ll setup a Test project an see what I can do.

What is the reason for this line?
Are you 100% sure about this?

First I tried to simply replace the entire package from SpenceKonde’s repo, and then found another post that suggested using the platform_packages directive. I have created a new project using just this ini and the package.json from the framework is now “version”: “2.6.10”,. so I think it’s legitimate. I tried to do some compare between the directories but couldn’t see anything meaningful… I looked into the preproc of arduino’s build, it only replaced the register line :slight_smile:

...
void show_RGB(long val);
void setup() {
  pinMode((6), 1 /* used for pinMode() */);
  pinMode((5), 1 /* used for pinMode() */);
  pinMode((4), 1 /* used for pinMode() */);
  
# 9 "/home/qq/Documents/github.com/toneDownPWMProblem/toneDownPWMProblem.ino" 3
 (*(volatile uint8_t *)(0x0202)) 
# 9 "/home/qq/Documents/github.com/toneDownPWMProblem/toneDownPWMProblem.ino"
               |= 
# 9 "/home/qq/Documents/github.com/toneDownPWMProblem/toneDownPWMProblem.ino" 3
                  0x01 /* Port Multiplexer TCA0 Output 0 bit mask. */ 
# 9 "/home/qq/Documents/github.com/toneDownPWMProblem/toneDownPWMProblem.ino"
                                   | 
# 9 "/home/qq/Documents/github.com/toneDownPWMProblem/toneDownPWMProblem.ino" 3
                                     0x02 /* Port Multiplexer TCA0 Output 1 bit mask. */ 
# 9 "/home/qq/Documents/github.com/toneDownPWMProblem/toneDownPWMProblem.ino"
                                                      | 
# 9 "/home/qq/Documents/github.com/toneDownPWMProblem/toneDownPWMProblem.ino" 3
                                                        0x04 /* Port Multiplexer TCA0 Output 2 bit mask. */
# 9 "/home/qq/Documents/github.com/toneDownPWMProblem/toneDownPWMProblem.ino"
                                                                        ;
  show_RGB(0xFFFF00);
...

I couldn’t find anything like this in platformIO .pio folder though, just binaries.
I am trying to reverse the binaries and trying to compare the asm and see if there is anything different.

I’m not familiar with ATtiny1616 but I’ll try to find the correct settings for the PWM issue you have by looking at the boards.txt file.

Make sure platform_packages settings is correct.
Maybe use a simple and small test project and see if it compiles correctly.

Meanwhile I continue to find the correct PWM settings…

Please post a screenshot of your ArduinoIDE settings which shows the settings you need to make regarding the PWM issue.

Thanks!

atxy6.menu.PWMmux.I6_A=PB3-5 PA3-5, 1-series: PC0/1 results in atxy6.menu.PWMmux.I6_A.build.pwmflags=-DTCA_PORTMUX=0x07 -DUSE_TIMERD0_PWM in the ArduinoIDE.

The equivalent in the platformio.ini should be

[env:ATtiny1616]
platform = atmelmegaavr
board = ATtiny1616
framework = arduino
board_build.pwmflags=-DTCA_PORTMUX=0x07 -DUSE_TIMERD0_PWM

Thank you Boris, however it didn’t work for my case…
I found a discusson from SpenceKonde earlier and noticed that the mapping between SplitMode and HIGH/LOW comparator is not explicitly mentioned in the datasheet…
I had to manually do the following to make the alternative pins work (note that analogWrite does not work in this case…)

#include <Arduino.h>
void setup() {
    pinMode(PIN_PA3, OUTPUT);
    pinMode(PIN_PB4, OUTPUT);
    pinMode(PIN_PB5, OUTPUT);
    PORTMUX_CTRLC = PORTMUX_TCA00_bm | PORTMUX_TCA01_bm | PORTMUX_TCA02_bm;
    PORTMUX_CTRLD = PORTMUX_TCB0_bm | PORTMUX_TCB1_bm;
    PORTB.DIRSET = PIN3_bm | PIN4_bm | PIN5_bm; // Set PB3, PB4, PB5 as output
    PORTA.DIRSET = PIN3_bm; // Set PA3 as output
    TCA0.SPLIT.CTRLD = TCA_SPLIT_SPLITM_bm; // Enable split mode
    TCA0.SPLIT.LPER = 0xFF; // Set period for lower byte
    TCA0.SPLIT.HPER = 0xFF; // Set period for high byte
    TCA0.SPLIT.CTRLA = TCA_SPLIT_CLKSEL_DIV1_gc | TCA_SPLIT_ENABLE_bm; // Clock select and enable
    TCA0.SPLIT.CTRLB = TCA_SPLIT_HCMP0EN_bm |TCA_SPLIT_LCMP2EN_bm | TCA_SPLIT_LCMP1EN_bm; 
    TCA0.SPLIT.LCMP1 = 0xFE; 
    TCA0.SPLIT.LCMP2 = 0xFE; 
    TCA0.SPLIT.HCMP0 = 0xFE;
    /**
     * 
      * LCMP0 - PB0 / 3
        LCMP1 - PB1 / 4
        LCMP2 - PB2 / 5 - PB5 - 
        HCMP0 - PA3 
        HCMP1 - PA4
        HCMP2 - PA5

     * https://github.com/SpenceKonde/megaTinyCore/discussions/384
     * 
    */
}
char data=0;
void loop() {
    if(data>0xFD)data=0; 
    data+=10;
    for (int i=0;i<3;i++){
        TCA0.SPLIT.LCMP1 = data; //Green
        TCA0.SPLIT.LCMP2 = 0xFE; //blue
        TCA0.SPLIT.HCMP0 = 0XFE; //RED!!!
        delay(10);
        TCA0.SPLIT.LCMP1 = 0xFE; //Green
        TCA0.SPLIT.LCMP2 = data; //blue
        TCA0.SPLIT.HCMP0 = 0XFE; //RED!!!
        delay(10);
        TCA0.SPLIT.LCMP1 = 0xFE; //Green
        TCA0.SPLIT.LCMP2 = 0xFE; //blue
        TCA0.SPLIT.HCMP0 = data; //RED!!!
        delay(10);
    }
}

I am still not sure how did Arduino managed to do this.

Here I also attach the arduino code that worked with the analogWrite function:

#include <Arduino.h>
void show_RGB(long val);
void setup() {
  pinMode(PIN_PA3, OUTPUT);
  pinMode(PIN_PB4, OUTPUT);
  pinMode(PIN_PB5, OUTPUT);
  show_RGB(0xFFFFFF); 
}
int data=0;
void loop() {  
    if(data>0xFE)data=0;
    data+=20;
    show_RGB(data<<16 | 0xFFFF); 
    delay(10);
    show_RGB(data<<8 | 0xFF00FF); 
    delay(10);
    show_RGB(data | 0xFFFF00); 
    delay(10);
}
void show_RGB(long val){
    analogWrite(PIN_PA3,(val>>16) & 0xFF);
    analogWrite(PIN_PB4,(val>>8)  & 0xFF);
    analogWrite(PIN_PB5,val       & 0xFF);
}

EDIT: after a bit of reading and fiddling the build_flags are found to be :

build_flags = -DTCA_PORTMUX=0x07 -DUSE_TIMERD0_PWM

and the PWM worked like a charm!

my entire platformio.ini now reads like this:

[env:ATtiny1616]
platform = atmelmegaavr
board = ATtiny1616
framework = arduino
platform_packages = framework-arduino-megaavr-megatinycore @ https://github.com/SpenceKonde/megaTinyCore/
build_flags = -DTCA_PORTMUX=0x07 -DUSE_TIMERD0_PWM