Author Topic: Trying to increase PWM frequency - successful but now digispark is bricked!?  (Read 2830 times)

TnF

  • Newbie
  • *
  • Posts: 3
Hello guys. I'm new to digispark and atmel attiny products so please forgive my ignorance (if any):p

So i got a couple of digisparks due to the small factor to play with.
I was playing around everything was fine. I could upload sketches normally without any issues.
So then i decided to port a sketch of mine that uses PWM in 2 outputs. I noticed only one of the 3 hardware PWM pins had 1khz frequency which is still too low for my application so i looked to find how to increase this and ended up finding this guide here:
https://digistump.com/wiki/digispark/tricks#how_to_increase_hardware_pwm_frequency

However this seemed to be an old guide since the locations don't match with the latest arduino and digistump code. I ended up fining the files here:
Code: [Select]
C:\Users\MyUserName\AppData\Local\Arduino15\packages\digistump\hardware\avr\1.6.7\cores\tiny
MS_TIMER_TICK_EVERY_X_CYCLES can be found in wiring.c as the guide says, but FAVOR_PHASE_CORRECT_PWM is no longer in that file and can be found in core_build_options.c
From my testing changing FAVOR_PHASE_CORRECT_PWM from 1 to 0 works, it gives you a second fast pwm pin as it should. However changing MS_TIMER_TICK_EVERY_X_CYCLES seems to not work as it should. Here is the code from the file:

Code: [Select]
#define MS_TIMER_TICK_EVERY_X_CYCLES  64 /* Shall be a within 1, 8, 64, 256 or 1024. (default = 64) If set to 1, HW PWM is around 64.5KHz@16.5MHz with Digispark */

#if F_CPU >= 3000000L
#if !defined(MS_TIMER_TICK_EVERY_X_CYCLES)
  #define MillisTimer_Prescale_Index  MillisTimer_(Prescale_Value_64)
  #define MillisTimer_Prescale_Value  (64)
  #define ToneTimer_Prescale_Index    ToneTimer_(Prescale_Value_64)
  #define ToneTimer_Prescale_Value    (64)
#else
  #define Prescaler_Value(Val)        PRESCALER_VALUE(Val)
  #define PRESCALER_VALUE(Val)        Prescale_Value_##Val
  #define MillisTimer_Prescale_Index  MillisTimer_(Prescaler_Value(MS_TIMER_TICK_EVERY_X_CYCLES))
  #define MillisTimer_Prescale_Value  (MS_TIMER_TICK_EVERY_X_CYCLES)
  #define ToneTimer_Prescale_Index    ToneTimer_(Prescaler_Value(MS_TIMER_TICK_EVERY_X_CYCLES))
  #define ToneTimer_Prescale_Value    (MS_TIMER_TICK_EVERY_X_CYCLES)
#endif
#else
  #define MillisTimer_Prescale_Index  MillisTimer_(Prescale_Value_8)
  #define MillisTimer_Prescale_Value  (8)
  #define ToneTimer_Prescale_Index    ToneTimer_(Prescale_Value_8)
  #define ToneTimer_Prescale_Value    (8)
#endif

Normally changing the value of MS_TIMER_TICK_EVERY_X_CYCLES should change the prescaler. However i tried several value in the first line, but it always stays to the 64 prescaler.
So then i decided to change the values below:

Code: [Select]
#define MS_TIMER_TICK_EVERY_X_CYCLES  1 /* Shall be a within 1, 8, 64, 256 or 1024. (default = 64) If set to 1, HW PWM is around 64.5KHz@16.5MHz with Digispark */

#if F_CPU >= 3000000L
#if !defined(MS_TIMER_TICK_EVERY_X_CYCLES)
  #define MillisTimer_Prescale_Index  MillisTimer_(Prescale_Value_1)
  #define MillisTimer_Prescale_Value  (1)
  #define ToneTimer_Prescale_Index    ToneTimer_(Prescale_Value_1)
  #define ToneTimer_Prescale_Value    (1)
#else
  #define Prescaler_Value(Val)        PRESCALER_VALUE(Val)
  #define PRESCALER_VALUE(Val)        Prescale_Value_##Val
  #define MillisTimer_Prescale_Index  MillisTimer_(Prescaler_Value(MS_TIMER_TICK_EVERY_X_CYCLES))
  #define MillisTimer_Prescale_Value  (MS_TIMER_TICK_EVERY_X_CYCLES)
  #define ToneTimer_Prescale_Index    ToneTimer_(Prescaler_Value(MS_TIMER_TICK_EVERY_X_CYCLES))
  #define ToneTimer_Prescale_Value    (MS_TIMER_TICK_EVERY_X_CYCLES)
#endif
#else
  #define MillisTimer_Prescale_Index  MillisTimer_(Prescale_Value_8)
  #define MillisTimer_Prescale_Value  (8)
  #define ToneTimer_Prescale_Index    ToneTimer_(Prescale_Value_8)
  #define ToneTimer_Prescale_Value    (8)
#endif

and i uploaded the sketch normally and it worked! 64.5Khz pwm frequency. However then i wanted to test 8 prescaler value so i changed it to this:

Code: [Select]
#define MS_TIMER_TICK_EVERY_X_CYCLES  1 /* Shall be a within 1, 8, 64, 256 or 1024. (default = 64) If set to 1, HW PWM is around 64.5KHz@16.5MHz with Digispark */

#if F_CPU >= 3000000L
#if !defined(MS_TIMER_TICK_EVERY_X_CYCLES)
  #define MillisTimer_Prescale_Index  MillisTimer_(Prescale_Value_8)
  #define MillisTimer_Prescale_Value  (8)
  #define ToneTimer_Prescale_Index    ToneTimer_(Prescale_Value_8)
  #define ToneTimer_Prescale_Value    (8)
#else
  #define Prescaler_Value(Val)        PRESCALER_VALUE(Val)
  #define PRESCALER_VALUE(Val)        Prescale_Value_##Val
  #define MillisTimer_Prescale_Index  MillisTimer_(Prescaler_Value(MS_TIMER_TICK_EVERY_X_CYCLES))
  #define MillisTimer_Prescale_Value  (MS_TIMER_TICK_EVERY_X_CYCLES)
  #define ToneTimer_Prescale_Index    ToneTimer_(Prescaler_Value(MS_TIMER_TICK_EVERY_X_CYCLES))
  #define ToneTimer_Prescale_Value    (MS_TIMER_TICK_EVERY_X_CYCLES)
#endif
#else
  #define MillisTimer_Prescale_Index  MillisTimer_(Prescale_Value_8)
  #define MillisTimer_Prescale_Value  (8)
  #define ToneTimer_Prescale_Index    ToneTimer_(Prescale_Value_8)
  #define ToneTimer_Prescale_Value    (8)
#endif

I hit upload, and it couldn't upload it:/ I tried everything, even disconnecting everything and restarting the PC. When i connect it to the pc it seems to get recognized by windows as i can see it as a digispark bootloader device but it keeps disconnecting and connecting without removing it. Also the previous sketch doesn't work. I think the new one might partially uploaded? Can the modification to the prescaler affected the bootloader? 

Btw while trying to upload now if i try enough times connecting and disconnecting after the timeout i get these in arduino ide:

Code: [Select]
Sketch uses 718 bytes (11%) of program storage space. Maximum is 6,012 bytes.
Global variables use 9 bytes of dynamic memory.
Running Digispark Uploader...
Plug in device now... (will timeout in 60 seconds)
> Please plug in the device ...
> Press CTRL+C to terminate the program.
> Device is found!
connecting: 16% complete
connecting: 22% complete
connecting: 28% complete
connecting: 33% complete
> Device has firmware version 1.11
> Available space for user applications: 65535 bytes
> Suggested sleep time between sending pages: 127ms
> Whole page count: 257  page size: 255
> Erase function sleep duration: 32639ms
parsing: 50% complete
> Erasing the memory ...
erasing: 55% complete
erasing: 60% complete
erasing: 65% complete
> Starting to upload ...
writing: 70% complete
writing: 75% complete
writing: 80% complete
> Starting the user app ...
>> Run error -1 has occured ...
>> Please unplug the device and restart the program.
> Please plug in the device ...
> Press CTRL+C to terminate the program.
> Device is found!
connecting: 16% complete
connecting: 22% complete
connecting: 28% complete
connecting: 33% complete
> Device has firmware version 1.11
> Available space for user applications: 65535 bytes
> Suggested sleep time between sending pages: 127ms
> Whole page count: 517  page size: 127
> Erase function sleep duration: 65659ms
parsing: 50% complete
> Erasing the memory ...
erasing: 55% complete
erasing: 60% complete
erasing: 65% complete
> Starting to upload ...
writing: 70% complete
writing: 75% complete
writing: 80% complete
> Starting the user app ...
>> Run error -1 has occured ...
>> Please unplug the device and restart the program.
> Please plug in the device ...
> Press CTRL+C to terminate the program.
> Device is found!
connecting: 16% complete
connecting: 22% complete
connecting: 28% complete
connecting: 33% complete
> Device has firmware version 1.11
> Available space for user applications: 65535 bytes
> Suggested sleep time between sending pages: 127ms
> Whole page count: 257  page size: 255
> Erase function sleep duration: 32639ms
parsing: 50% complete
> Erasing the memory ...
erasing: 55% complete
erasing: 60% complete
erasing: 65% complete
> Starting to upload ...
writing: 70% complete
writing: 75% complete
writing: 80% complete
> Starting the user app ...
>> Run error -1 has occured ...
>> Please unplug the device and restart the program.

Can someone help?
Kind regards,
TnF
« Last Edit: June 18, 2016, 07:20:43 am by TnF »

TnF

  • Newbie
  • *
  • Posts: 3
Ok FINALLY de-bricked it. I reseted the fuses and then reflashed the bootloader via ISP. So now how do i get the prescaler to work without messing everything up?

TnF

  • Newbie
  • *
  • Posts: 3
I'm not sure what is going on but now that i got the digispark to work again i set #define MS_TIMER_TICK_EVERY_X_CYCLES  to 1 and it works normally without any modification to the rest of the code.