[Solved] Attiny1616: 'PIN_B5' was not declared in this scope, Serial outputs just gibberish

Hi,

I built a custom PCB with a Attiny1616 in a QFN Package on it.
Now I’m start writing the code for it, wich works mostly but I have some issues.

Here’s my platformio.ini

[env]
platform = atmelmegaavr
board = ATtiny1616
framework = arduino
monitor_speed = 115200

[env:udpi]
upload_flags =
    --tool
    uart
    --device
    $BOARD_MCU
    --uart
    $UPLOAD_PORT
    --clk
    $UPLOAD_SPEED
upload_command = pymcuprog write --erase $UPLOAD_FLAGS --filename $SOURCE

I have a led on PA1 which works fine with the following code:

#include <Arduino.h>

#define HEARTBEAT_LED         PIN_A1

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

void loop() {
  digitalWrite(HEARTBEAT_LED, HIGH);
  delay(100);
  digitalWrite(HEARTBEAT_LED, LOW);
  delay(100);
}

I have another led on PB5, but If I simply replace the PIN_A1 in the define with PIN_B5 I get a compile error:

error: 'PIN_B5' was not declared in this scope

Any ideas whats wrong?

Weird?

Really strange!

I use PIO core and not the Vscode IDE but that shouldn’t make difference, right?

Your platformio.ini is the same as the one I posted?

Yes, your exact platformio.ini.

Can you post the output of pio run -v -j1 to pastebin.com and link it here?

Yes but only tomorrow, I don’t have the computer here where I code.

Here we go:

I get the exact ame error when I use the VScode Platformio IDE.

I see this error if I hover over the underlined error:

grafik

Interestingly, if I change it to PIN_PA1 and select “Go to Definition”, it opens up C:\Users\user\.platformio\packages\framework-arduino-megaavr-megatinycore\variants\txy6\pins_arduino.h which is exactly the header file I expected.

If I look through the file I clearly see the #define for PIN_PB5!?

grafik

  1. Delete the .pio folder of the project
  2. Close VSCode
  3. Delete C:\Users\<user>\.platformio\packages\framework-arduino-megaavr-megatinycore\
  4. Delete C:\Users\<user>\.platformio\platforms\atmelmegaavr
  5. Delete C:\Users\<user>\.platformio\.cache
  6. Delete C:\Users\<user>\.platformio\lib if it exists
  7. Restart VSCode, recompile

:exploding_head:

After deleting all the flders you mentioned, it compiles (on the CLI) and the LED on PB5 is working!

But one other issue is still left. If I add a Serial like this:

#include <Arduino.h>

#define HEARTBEAT_LED         PIN_PB5

void setup() {
  pinMode(HEARTBEAT_LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(HEARTBEAT_LED, HIGH);
  delay(100);
  digitalWrite(HEARTBEAT_LED, LOW);
  delay(100);
  Serial.println("Hello");
}

And start the monitor with pio device monitor, all I get is gibberish:

This is the circuit I use:

I got it from here ATtiny3216 Development Board v2 - OSHWLab

Its the first time I use a controller with UPDI, so I have no experiance with it.

In your platformio.ini you wrote

but in your code

IMO both values must be the same to get proper output

Then there must have been either a corrupted download or a local modification to one of the deleted folders.

Tell-tale sign of wrong oscillator / clock frequency. Depending on the Fuse settings burned into your ATTiny1616, it will try to use its internal oscillator or an external crystal, divided by some prescaler. Right now your config compiles the code with the expectation that the ATTiny1616 is set up for a 16MHz clock.

If this assumption is wrong, then the UART frequency will be off.

Try setting the monitor_speed to double, half, an eigtht or times eight the baud rate you set in the code. The actual baud rate will then give you the factor by how much your clock frequency is off.

I read something about a 20MHz and a 16MHz setting inside the ATTiny1616.

https://onlinedocs.microchip.com/oxy/GUID-BD668D46-A7E4-40CA-8B1D-4EB24BAA33C3-en-US-4/GUID-1EB8B46B-8973-46E6-A33B-E20C8EEE92B5.html

https://onlinedocs.microchip.com/oxy/GUID-C541EA24-5EC3-41E5-9648-79068F9853C0-en-US-3/GUID-D7105EF0-DF37-456F-89BA-52BC0E30100A.html

If it’s running at 20MHz, so, faster, than the actual baud rate should be 20/16 * 9600 = 12000 baud. Try setting that as monitor_speed?

If proper text shows up, set

board_build.f_cpu = 20000000L

instead.

You’re a genius!

Thats it, with a baud rate of 12000 it works!

I’ll try setting the fcpu to 20Mhz now

Works like charm! Thank you so much for helping me solve both issues in a blaze :partying_face: