uisp-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Uisp-dev] STK 500 oscillator speed setting


From: Terran Melconian
Subject: [Uisp-dev] STK 500 oscillator speed setting
Date: Sat, 12 Jul 2003 01:04:54 -0400

The following patch should add the ability to read and write the
software oscillator on the STK500.  It has been tested with firmware
1.7; I have only the one STK500 board and I'm not at all clear on
what's changed beyond the fact that "something changed in 1.14", so I
suppose it might not work with that.

It's based on the documentation provided by Atmel about the STK500
protocol, assisted by my own notes from a previous attempt at
reverse-engineering the communications protocol before I realized that
other people had already made much more progress than I had and gave
up.

If this doesn't meet your coding standards, explain what it needs and
I'll rework it.


Index: Main.C
===================================================================
RCS file: /cvsroot/uisp/uisp/src/Main.C,v
retrieving revision 1.15
diff -u -r1.15 Main.C
--- Main.C      12 May 2003 17:25:19 -0000      1.15
+++ Main.C      12 Jul 2003 04:45:12 -0000
@@ -143,6 +143,8 @@
 "  --wr_vtg     Set the VTarget Voltage. Valid values are 0.0 to 6.0 volts 
in\n"
 "               0.1 volt increments. Value can not be smaller than the\n"
 "               ARef value.\n"
+"  --rd_osc     Read the oscillator frequency in Hertz.\n"
+"  --wr_osc     Set the oscillator frequency in Hertz, from 14.06 to 
3686400.\n"
 "\n"
 "Functions:\n"
 "  --upload     Upload \"input_file\" to the AVR memory.\n"
Index: Stk500.C
===================================================================
RCS file: /cvsroot/uisp/uisp/src/Stk500.C,v
retrieving revision 1.22
diff -u -r1.22 Stk500.C
--- Stk500.C    9 May 2003 06:21:00 -0000       1.22
+++ Stk500.C    12 Jul 2003 04:45:12 -0000
@@ -32,6 +32,9 @@
 #include "Stk500.h"
 #include "Serial.h"
 
+int clockbase=3686400;
+int clockscale[]={0,1,8,32,64,128,256,1024}; /* from page 19 of STK500 docs */
+
 const TByte TStk500::pSTK500[] = { 0x30, 0x20 };
 const TByte TStk500::pSTK500_Reply[] = { 0x14, 0x10 };
 
@@ -833,6 +836,71 @@
       TByte val = ReadParam(0x85);
       printf("ARef = %d.%d V\n", val/10, val%10);
   }
+
+  if (GetCmdParam("--rd_osc", false))
+  {
+      TByte p = ReadParam(0x86);
+      TByte n = ReadParam(0x87);
+      TByte sc = ReadParam(0x89);
+
+      printf("Oscillator: p=%x, n=%x, sc=%x, ",p,n,sc);
+
+      if (p==0)
+       printf ("0 Hz (stopped)\n");
+      else
+      {
+       double freq=clockbase/clockscale[p] / (n+1.0);
+        printf ("frequency=%.8g Hz\n", freq);
+      }
+  }
+
+  if ((val=GetCmdParam("--wr_osc", true)))
+  {
+      /* See page 19 of the STK500 protocol documentation from Atmel */
+      TByte p,n,sc;
+      double freq=atof(val);
+      double setfreq;
+
+      if (val == 0)
+      {
+       /* Special case for stopped */
+       p=0;
+       n=0;
+       sc=0xff;
+      }
+      else
+      {
+        /* Select lowest usable prescaler value */
+        for (p=1; p<=7; p++)
+       {
+           if (clockbase / clockscale[p] / freq <= 256)
+               break;
+       }
+       if (p > 7)
+       {
+           p=7;
+           n=0xff;
+       }
+       else
+           n=(int)(clockbase/clockscale[p]/freq+.5) - 1;
+
+       setfreq=clockbase/clockscale[p]/(n+1.0);
+
+       if (p > 1)
+           sc=0xff;
+       else
+           sc=(int)(8000000/setfreq) - 1;
+
+       printf ("Setting oscillator frequency to %.8g (p=%d,n=%d,sc=%d)\n", 
setfreq,
+               p,n,sc);
+       WriteParam(0x86,p);
+       WriteParam(0x87,n);
+       WriteParam(0x89,sc);
+      }
+
+      
+  }
+
 
   EnterProgrammingMode();
   ReadSignature();




reply via email to

[Prev in Thread] Current Thread [Next in Thread]