CVS: python README-msp430-dco.txt,NONE,1.1 msp430-dco.py,1.7,1.8

Chris Liechti <[email protected]>
Newsgroups gmane.comp.hardware.texas-instruments.msp430.gcc.cvs
Message-ID <[email protected]>
Update of /cvsroot/mspgcc/python
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17880/python

Modified Files:
	msp430-dco.py 
Added Files:
	README-msp430-dco.txt 
Log Message:
- ability to write selected values to the flash memory of the target
- add documentation

--- NEW FILE: README-msp430-dco.txt ---
msp430-dco
===========

MSP430 clock calibration utility.

(C) 2005-2006 Chris Liechti <[email protected]>


Features
--------

- can handle F1xx, F2xx and F4xx devices, with or without external
  Rsel resisitor
- measure calibration values for a given frequency
- restore calibration values of F2xx devices
- selectable clock tolerance
- can write measured values to the target flash, output C code or #defines


Requirements
------------
- Linux, BSD, Un*x or Windows PC
- Python 2.3 or newer
- Parallel JTAG hardware with an MSP430 device connected
  (currently only the parallel port adapter with the MSP430mspgcc library
  is supported)


Installation
------------
Binaries for Windows are included in the installer.

Linux users should refer to the next section.


Building from source
--------------------
Please refer to the msp430-jtag documentation. The steps are identical, just
the python frontend has a different name.


Short introduction
------------------
This software uses the JTAG hardware that comes with the FET kits. It is
connected to the parallel port.

The program can be started by typing ``msp430-dco`` when installed correctly
If it's used from the source directory use ``python msp430-dco.py``.


usage: msp430-dco [options] frequency

MSP430 clock calibration utility.

This tool can measure the internal oscillator of F1xx, F2xx and F4xx devices,
display the supported frequencies, or run a software FLL to find the settings
for a specified frequency.

The target device has to be connected to the JTAG interface.

Examples:
    See min and max clock speeds:
        msp430-dco --measure

    Get clock settings for 2.0MHz +/-1%:
        msp430-dco --tolerance=0.01 2.0e6

    Write clock calibration for 1.5MHz to the information memory at 0x1000:
        msp430-dco 1.5e6 BCSCTL1@0x1000 DCOCTL@0x1000

Use it at your own risk. No guarantee that the values are correct.

options:
  -h, --help            show this help message and exit
  -o FILE, --output=FILE
                        write result to given file
  --dcor                use external resistor
  -d, --debug           print debug messages
  -l LPT, --lpt=LPT     set the parallel port
  -m, --measure         measure min and max clock settings and exit
  -c, --calibrate       Restore calibration values on F2xx devices
  -t TOLERANCE, --tolerance=TOLERANCE
                        set the clock tolerance as factor. e.g. 0.01 means 1%
                        (default=0.005)
  --define              output #defines instead of assignments
  --erase=ERASE         erase flash page at given address. Use with care!

Notes:
- the --output option can be used to redirect the output. Once could also use
  the usual stdout redirection from the shell (``>filename``)
- --tolerance specifies a window in which the clock has to be to complete the
  calibration.


Variables
---------

Arguments in the form ``variable@address`` are used to write the coresponding
values to the target device.
Variable names are case insensitive, addresses can be specified in decimal,
octal or hexadecimal format.

The available variables depend on the target type and executed operation.
All variables that are written all caps in the table below are in
``unsigned char`` format, others in ``unsigned short`` format. The later
should be written to even addresses only, as the code reading these values
could have problems otherwise.

Frequencies are in kHz.

    +-------------+------+-------------------------------------------------+
    | *Operation* | *MCU*| *Variables*                                     |
    +-------------+------+-------------------------------------------------+
    | frequency   | F1xx | BCSCTL1 BCSCTL2 DCOCTL freq                     |
    +             +------+-------------------------------------------------+
    |             | F2xx | BCSCTL1 BCSCTL2 DCOCTL freq                     |
    +             +------+-------------------------------------------------+
    |             | F4xx | SCFI0 SCFI1 SCFQCTL FLL_CTL0 FLL_CTL1 freq      |
    +-------------+------+-------------------------------------------------+
    | --measure   | F1xx | fmax fmin                                       |
    |             |      | rsel0_fmax rsel0_fmin rsel1_fmax rsel1_fmin     |
    |             |      | rsel2_fmax rsel2_fmin rsel3_fmax rsel3_fmin     |
    |             |      | rsel4_fmax rsel4_fmin rsel5_fmax rsel5_fmin     |
    |             |      | rsel6_fmax rsel6_fmin rsel7_fmax rsel7_fmin     |
    +             +------+-------------------------------------------------+
    |             | F2xx | fmax fmin                                       |
    |             |      | rsel0_fmax rsel0_fmin rsel1_fmax rsel1_fmin     |
    |             |      | rsel2_fmax rsel2_fmin rsel3_fmax rsel3_fmin     |
    |             |      | rsel4_fmax rsel4_fmin rsel5_fmax rsel5_fmin     |
    |             |      | rsel6_fmax rsel6_fmin rsel7_fmax rsel7_fmin     |
    |             |      | rsel8_fmax rsel8_fmin rsel9_fmax rsel9_fmin     |
    |             |      | rsel10_fmax rsel10_fmin rsel11_fmax rsel11_fmin |
    |             |      | rsel12_fmax rsel12_fmin rsel13_fmax rsel13_fmin |
    |             |      | rsel14_fmax rsel14_fmin rsel15_fmax rsel15_fmin |
    +             +------+-------------------------------------------------+
    |             | F4xx | fmax fmin                                       |
    +-------------+------+-------------------------------------------------+
    | --calibrate | F1xx | *not supported*                                 |
    +             +------+-------------------------------------------------+
    |             | F2xx | f16MHz_dcoctl f16MHz_bcsctl1                    |
    |             |      | f12MHz_dcoctl f12MHz_bcsctl1                    |
    |             |      | f8MHz_dcoctl f8MHz_bcsctl1                      |
    |             |      | f1MHz_dcoctl f1MHz_bcsctl1                      |
    +             +------+-------------------------------------------------+
    |             | F4xx | *not supported*                                 |
    +-------------+------+-------------------------------------------------+


When the ``msp430-dco`` tool is run with the ``--debug`` option it provides
an output with all the possible variables and their values.


Examples
--------
``msp430-dco 2.5e6``
    Print the calibration values for 2.5MHz

``msp430-dco 2.5e6 --define``
    Same as above, but format the output as defines usable for C include files.

``msp430-dco 1e6 --erase 0x1000 BCSCTL1@0x1000 DCOCTL@0x1001``
    Measure calibration values for 1MHz, then erase the information memory
    flash page at 0x1000. These values are then written to the flash at
    0x1000 and 0x1001.
    
    This can be useful in combination with firmware downloads. For example
    make a mass erase, write firmware, then write clock calibration for this
    device::
    
        msp430-jtag -e my_firmware.elf
        msp430-dco 1e6 BCSCTL1@0x1000 DCOCTL@0x1001

    The firmware can then read the values form the flash and configure the
    Basic Clock System using these values.

``msp430-dco --measure``
    Print frequency ranges of all DCO settings as well as minimal and maximal
    values. (Note: restricted functionality on F4xx devices)

``msp430-dco --calibrate``
    Recalculate the calibration values for 16MHz, 12MHz, 8MHz and 1MHz
    that are available in the information memory at 0x10f8-0x10ff.
    This is only possible for F2xx devices.




History
-------
V1.0
    Public release.

V1.1
    Can write values to target flash


References
----------
- Python: http://www.python.org

- Texas Instruments MSP430 Homepage, links to Datasheets and Application
  Notes: http://www.ti.com/msp430


Index: msp430-dco.py
===================================================================
RCS file: /cvsroot/mspgcc/python/msp430-dco.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -w -d -r1.7 -r1.8
--- msp430-dco.py	11 Apr 2006 18:35:23 -0000	1.7
+++ msp430-dco.py	20 Apr 2006 23:06:47 -0000	1.8
@@ -6,7 +6,7 @@
 # that are connected to the JTAG. It can  display the supported frequencies,
 # or run a software FLL to find the settings for a specified frequency.
 #
-# (C) 2005 Chris Liechti <[email protected]>
+# (C) 2005-2006 Chris Liechti <[email protected]>
 # this is distributed under a free software license, see license.txt
 #
 # $Id$
@@ -36,6 +36,11 @@
     return "%.2fGHz" % (frequency/1e9)
 
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
+# variable types (with type code for the struct module)
+TYPE_8BIT = '<B'
+TYPE_16BIT = '<H'
+
+# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
 def get_msp430_type():
     """return the MSP430 type id that is stored in the ROM"""
@@ -53,6 +58,7 @@
     if tolerance < 0.005 or tolerance > 50:
         raise ValueError('tolerance out of range %f' % (tolerance,))
     device = get_msp430_type() >> 8
+    variables = {}
     
     if device == 0xf1:
         measured_frequency, dco, bcs1 = clock.setDCO(
@@ -61,6 +67,10 @@
             maxrsel=7,
             dcor=dcor
         )
+        variables['freq'] = TYPE_16BIT, measured_frequency/1e3
+        variables['dcoctl'] = TYPE_8BIT, dco
+        variables['bcsctl1'] = TYPE_8BIT, bcs1
+        variables['bcsctl2'] = TYPE_8BIT, dcor and 1 or 0
         out.write('// BCS settings for %s\n' % (nice_frequency(measured_frequency), ))
         if define:
             suffix = '_%s' % nice_frequency(frequency).replace('.','_')
@@ -84,6 +94,10 @@
             maxrsel=15,
             dcor=dcor
         )
+        variables['freq'] = TYPE_16BIT, measured_frequency/1e3
+        variables['dcoctl'] = TYPE_8BIT, dco
+        variables['bcsctl1'] = TYPE_8BIT, bcs1
+        variables['bcsctl2'] = TYPE_8BIT, dcor and 1 or 0
         out.write('// BCS+ settings for %s\n' % (nice_frequency(measured_frequency), ))
         if define:
             suffix = '_%s' % nice_frequency(frequency).replace('.','_')
@@ -107,6 +121,12 @@
             frequency*(1-tolerance),
             frequency*(1+tolerance)
         )
+        variables['freq'] = TYPE_16BIT, measured_frequency/1e3
+        variables['scfi0'] = TYPE_8BIT, scfi0
+        variables['scfi1'] = TYPE_8BIT, scfi1
+        variables['scfqctl'] = TYPE_8BIT, scfqctl
+        variables['fll_ctl0'] = TYPE_8BIT, fll_ctl0
+        variables['fll_ctl1'] = TYPE_8BIT, fll_ctl1
         out.write('// FLL+ settings for %s\n' % (nice_frequency(measured_frequency), ))
         if define:
             suffix = '_%s' % nice_frequency(frequency).replace('.','_')
@@ -122,16 +142,20 @@
             ))
     else:
         raise IOError("unknown MSP430 type %02x" % device)
+    return variables
 
 def measure_clock(out):
     """measure fmin and fmax"""
     device = get_msp430_type() >> 8
+    variables = {}
     f_all_max = 0
     f_all_min = 1e99
     if device == 0xf1:
         for rsel in range(8):
             fmin = clock.getDCOFreq(0x00, rsel)
             fmax = clock.getDCOFreq(0xff, rsel)
+            variables['rsel%d_fmin' % rsel] = TYPE_16BIT, fmin/1e3
+            variables['rsel%d_fmax' % rsel] = TYPE_16BIT, fmax/1e3
             out.write('%s <= f(rsel_%d) <= %s\n' % (
                 nice_frequency(fmin),
                 rsel,
@@ -143,6 +167,8 @@
         for rsel in range(16):
             fmin = clock.getDCOFreq(0x00, rsel)
             fmax = clock.getDCOFreq(0xff, rsel)
+            variables['rsel%d_fmin' % rsel] = TYPE_16BIT, fmin/1e3
+            variables['rsel%d_fmax' % rsel] = TYPE_16BIT, fmax/1e3
             out.write('%s <= f(rsel_%d) <= %s\n' % (
                 nice_frequency(fmin),
                 rsel,
@@ -160,6 +186,9 @@
         raise IOError("unknown MSP430 type %02x" % device)
     out.write('fmin = %8dHz (%s)\n' % (fmin, nice_frequency(f_all_min)))
     out.write('fmax = %8dHz (%s)\n' % (fmax, nice_frequency(f_all_max)))
+    variables['fmin'] = TYPE_16BIT, f_all_min/1e3
+    variables['fmax'] = TYPE_16BIT, f_all_max/1e3
+    return variables
 
 
 calibvalues_memory_map = {
@@ -173,6 +202,7 @@
     """curently for F2xx only:
        recalculate the clock calibration values and write them to the flash."""
     device = get_msp430_type() >> 8
+    variables = {}
     if device == 0xf2:
         #first read the segment form the device, so that only the calibration values
         #are updated. any other data in SegmentA is not changed.
@@ -186,6 +216,8 @@
                 maxrsel=15,
                 dcor=dcor
             )
+            variables['f%dMHz_dcoctl' % (frequency/1e6)] = TYPE_8BIT, dco
+            variables['f%dMHz_bcsctl1' % (frequency/1e6)] = TYPE_8BIT, bcs1
             out.write('BCS settings for %s: DCOCTL=0x%02x BCSCTL1=0x%02x\n' % (
                 nice_frequency(measured_frequency), dco, bcs1)
             )
@@ -196,7 +228,7 @@
         jtag._parjtag.memwrite(segment_a[0].startaddress, segment_a[0].data)
     else:
         raise NotImplementedError("--calibrate is not supported on %Xxx" % device)
-
+    return variables
 
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
@@ -204,9 +236,9 @@
     from optparse import OptionParser
 
     parser = OptionParser(usage="""\
-    usage: %prog [options] frequency
+%prog [options] frequency
 
-MSP430 clock callibration utility.
+MSP430 clock calibration utility V1.1
 
 This tool can measure the internal oscillator of F1xx, F2xx and F4xx devices,
 display the supported frequencies, or run a software FLL to find the settings
@@ -221,6 +253,9 @@
     Get clock settings for 2.0MHz +/-1%:
         %prog --tolerance=0.01 2.0e6
     
+    Write clock calibration for 1.5MHz to the information memory at 0x1000:
+        %prog 1.5e6 BCSCTL1@0x1000 DCOCTL@0x1000
+
 Use it at your own risk. No guarantee that the values are correct.""")
     parser.add_option("-o", "--output", dest="output",
                       help="write result to given file", metavar="FILE",
@@ -252,16 +287,36 @@
                       help="output #defines instead of assignments",
                       default=False, action='store_true')
 
+    parser.add_option("", "--erase", dest="erase",
+                      help="erase flash page at given address. Use with care!",
+                      default=None)
+
     (options, args) = parser.parse_args()
 
     global debug
     debug = options.debug
     clock.debug = options.debug
 
-    if not (options.measure or options.calibrate):
-        if len(args) != 1:
-            parser.error('exactly one argument expected: the target frequency')
+    # check arguments, filter out variables
+    frequency = None
+    variable_addresses = {}
+    for arg in args:
+        if '@' in arg:
+            try:
+                name, address_text = arg.split('@')
+                address = int(address_text, 0)
+            except ValueError:
+                parser.error('illegal variable expression: %r' % arg)
+            variable_addresses[name.lower()] = address
+        else:
+            try:
         frequency = float(args[0])
+            except ValueError, e:
+                parser.error('bad frequency: %r (%s)' % (arg, e))
+    if frequency is None and not (options.measure or options.calibrate):
+        if len(args) != 1:
+            parser.error('the target frequency expected')
+        
     
     #prepare output
     if options.output is None or options.output == '-':
@@ -269,22 +324,22 @@
     else:
         out = file(options.output, 'w')
     
-    #
+    # connect to th etarget and do the work
     jtag.init_backend(jtag.CTYPES_MSPGCC)   #doesn't currently work with 3'rd party libs
     jtagobj = jtag.JTAG()
     jtagobj.open(options.lpt)               #try to open port
     jtagobj.connect()                       #try to connect to target
     try:
         if options.measure:
-            measure_clock(out)
+            variables = measure_clock(out)
         elif options.calibrate:
-            calibrate_clock(
+            variables = calibrate_clock(
                 out,
                 options.tolerance,
                 options.dcor,
             )
         else:
-            adjust_clock(
+            variables = adjust_clock(
                 out,
                 frequency,
                 options.tolerance,
@@ -292,6 +347,35 @@
                 options.define
             )
         #~ print "%.2f kHz" % (getDCOFreq(0, 0)/1e3)
+        # log valiable names and values
+        if options.debug:
+            sys.stderr.write('Variables:\n')
+            sorted_items = variables.items()
+            sorted_items.sort()
+            for key, (vartype, value) in sorted_items:
+                sys.stderr.write('  %s = %d (0x%02x)\n' % (key, value, value))
+        # now write the variables to the targets memory, optionally erase segemnt before write
+        if options.erase is not None:
+            try:
+                address = int(options.erase, 0)
+            except ValueError, e:
+                parser.error('bad --erase address: %r' % (options.erase))
+            else:
+                jtagobj.makeActionSegmentErase(address)()
+        for variable, address in variable_addresses.items():
+            if variable in variables:
+                vartype, value = variables[variable]
+                sys.stderr.write('writing 0x%02x(%s) to address 0x%04x \n' % (
+                    value, variable.upper(), address
+                ))
+                jtag._parjtag.memwrite(address, struct.pack(vartype, value))
+                written_value = ord(jtag._parjtag.memread(address, 1))
+                if written_value != value:
+                    raise IOError('Value was not written correctly (%s@0x%04x) 0x%02x (expected 0x%02x)' % (
+                        variable.upper(), address, written_value, value
+                    ))
+            else:
+                raise NameError('No such variable: %r' % variable)
     finally:
         if sys.exc_info()[:1]:              #if there is an exception pending
             jtagobj.verbose = 0             #do not write any more messages



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.