MCUdude / MegaCoreX

An Arduino hardware package for ATmega4809, ATmega4808, ATmega3209, ATmega3208, ATmega1609, ATmega1608, ATmega809 and ATmega808
GNU Lesser General Public License v2.1
247 stars 52 forks source link

Setting frequency works in arduino, but not platformio #169

Closed aarobc closed 1 year ago

aarobc commented 1 year ago

I've recently worked to switch over to platformio, and found that while you can easily set the clockspeed via arduino, it doesn't work if set via platformio.

my config file:

 [platformio]                                                                                  

 [env]                                                                                         
 platform = atmelmegaavr                                                                       
 framework = arduino                                                                           
 board = ATmega4809                                                                            
 ; board_build.f_cpu = 16000000L                                                               
 board_build.f_cpu = 20000000L                                                                 
 board_hardware.oscillator = internal                                                          
 board_build.variant = 48pin-standard                                                          
 build_unflags =                                                                               
 build_flags =                                                                                 
 monitor_speed = 115200                                                                        

 [env:pyprog]                                                                                  
 platform = atmelmegaavr                                                                       
 framework = arduino                                                                           
 upload_speed = 115200                                                                         
 upload_flags =                                                                                
   --tool                                                                                      
   uart                                                                                        
   --device                                                                                    
   $BOARD_MCU                                                                                  
   --uart                                                                                      
   $UPLOAD_PORT                                                                                
   --clk                                                                                       
   $UPLOAD_SPEED                                                                               
 upload_command = pymcuprog write --erase $UPLOAD_FLAGS --filename $SOURCE  

If I upload a simple serial script using 20mhz that outputs "hello world" the output is garbled. This issue does not occur with arduino 20mhz upload.

MCUdude commented 1 year ago

That's because the switch between the 16 and 20 MHz oscillator requires the SYSCFG0 fuse to be changed. Arduino does this automatically on upload, but currently, PlatformIO requires you to manually change the fuses. If you use the PlatformIO template I've provided, you can run pio run -t fuses.