platformio / platform-atmelavr

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

Fixed attiny4313 board definition. #203

Closed svenwiltink closed 4 years ago

svenwiltink commented 4 years ago

Use tiny instead of tinycore and fixed the f_cpu flag

When using tinymodern we get the following error:

Building in release mode
Compiling .pio\build\attiny4313\src\main.cpp.o
Compiling .pio\build\attiny4313\FrameworkArduino\HardwareSerial.cpp.o
Compiling .pio\build\attiny4313\FrameworkArduino\Print.cpp.o
Compiling .pio\build\attiny4313\FrameworkArduino\Stream.cpp.o
Compiling .pio\build\attiny4313\FrameworkArduino\Tone.cpp.o
Compiling .pio\build\attiny4313\FrameworkArduino\WInterrupts.c.o
Compiling .pio\build\attiny4313\FrameworkArduino\WMath.cpp.o
Compiling .pio\build\attiny4313\FrameworkArduino\WString.cpp.o
Compiling .pio\build\attiny4313\FrameworkArduino\abi.cpp.o
Compiling .pio\build\attiny4313\FrameworkArduino\main.cpp.o
Compiling .pio\build\attiny4313\FrameworkArduino\new.cpp.o
Compiling .pio\build\attiny4313\FrameworkArduino\wiring.c.o
Compiling .pio\build\attiny4313\FrameworkArduino\wiring_analog.c.o
Compiling .pio\build\attiny4313\FrameworkArduino\wiring_digital.c.o
Compiling .pio\build\attiny4313\FrameworkArduino\wiring_pulse.c.o
Compiling .pio\build\attiny4313\FrameworkArduino\wiring_shift.c.o
C:\Users\Sven\.platformio\packages\framework-arduino-avr-attiny\cores\tinymodern\Tone.cpp:45:2: error: #error Only five or fifteen prescalers are supported. Update the code to support the number of actual prescalers.
 #error Only five or fifteen prescalers are supported.  Update the code to support the number of actual prescalers.
  ^
C:\Users\Sven\.platformio\packages\framework-arduino-avr-attiny\cores\tinymodern\Tone.cpp:56:59: error: 'TONETIMER_MAXIMUM_DIVISOR' was not declared in this scope
 const unsigned int Tone_Lowest_Frequency = (F_CPU + (2L * TONETIMER_MAXIMUM_DIVISOR - 1L)) / (2L * TONETIMER_MAXIMUM_DIVISOR);
                                                           ^
C:\Users\Sven\.platformio\packages\framework-arduino-avr-attiny\cores\tinymodern\Tone.cpp:56:100: error: 'TONETIMER_MAXIMUM_DIVISOR' was not declared in this scope
<snip>

All the compile errors go away when using the tiny core.

The f_cpu flag also appears to be wrong. The attiny4313 has an internal oscillator that runs at +-8Mhz. When using an exteral clock it can run up to 20Mhz. See the datascheet: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-2586-AVR-8-bit-Microcontroller-ATtiny25-ATtiny45-ATtiny85_Datasheet.pdf

valeros commented 4 years ago

Hi @svenwiltink ! Thanks for the PR. The only question is f_cpu, you specified 800000L which means 800 kHz, is that true?

svenwiltink commented 4 years ago

I was mistaken:

6.2.1Default Clock SourceThe device is shipped with CKSEL = “0100”, SUT = “10”, and CKDIV8 programmed. The defaultclock source setting is the Internal RC Oscillator with longest start-up time and an initial systemclock prescaling of 8, resulting in 1.0 MHz system clock. This default setting ensures that allusers can make their desired clock source setting using an In-System or Parallel programmer.For low-voltage devices it should be noted that unprogramming the CKDIV8 fuse may result inoverclocking. At low voltages (below 2.7V) the devices are rated for maximum 4 MHz operation(see Section 22.3 on page 200), but routing the clock signal from the internal oscillator directly tothe system clock line will run the device at 8 MHz.

By default the chip is shipped at 1Mhz, but can be clocked all the way to 8Mhz using the interal clock. When using an external clock source it can run at 20Mhz.

After setting the fuses to the correct value it runs at 8Mhz correctly

valeros commented 4 years ago

It looks like 8 MHz is the default choice for this board https://github.com/SpenceKonde/ATTinyCore/blob/master/avr/boards.txt#L2197

svenwiltink commented 4 years ago

With the lfuse set to 0xE4 that is the case. It ships with the divide by 8 flag on though, which results in a 1Mhz clock. I am not sure what the sensible value would be for platform.io. You can change the fuses by editing the platform.io settings, but it doesn't do that by default

svenwiltink commented 4 years ago

I can change it to either 8Mhz or 1Mhz, whichever makes more sense from the pio perspective

valeros commented 4 years ago

We tend to use the same settings as in the boards.txt for this board. Please change it to 8MHz as it was before.

svenwiltink commented 4 years ago

Updated

valeros commented 4 years ago

Thanks, merged.