Improve title comment. Update for debian
[cluck.git] / clock.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          cluck.lisp
6 ;;;; Purpose:       Common Lisp uControler Clock Calculator
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  March 2007
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; Copyright (c) 2007 Kevin M. Rosenberg
13 ;;;;
14 ;;;; Redistribution and use in source and binary forms, with or without
15 ;;;; modification, are permitted provided that the following conditions
16 ;;;; are met:
17 ;;;; 1. Redistributions of source code must retain the above copyright
18 ;;;;    notice, this list of conditions and the following disclaimer.
19 ;;;; 2. Redistributions in binary form must reproduce the above copyright
20 ;;;;    notice, this list of conditions and the following disclaimer in the
21 ;;;;    documentation and/or other materials provided with the distribution.
22 ;;;; 3. Neither the name of the author nor the names of the contributors
23 ;;;;    may be used to endorse or promote products derived from this software
24 ;;;;    without specific prior written permission.
25 ;;;;
26 ;;;; THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
27 ;;;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 ;;;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ;;;; ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
30 ;;;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 ;;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 ;;;; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 ;;;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 ;;;; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 ;;;; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 ;;;; SUCH DAMAGE.
37 ;;;; *************************************************************************
38
39 (in-package #:cluck)
40
41 (defvar *f-cpu* 16000000)
42
43 (defvar *8-bit-prescalars* '(1 8 64 256))
44 (defvar *10-bit-prescalars* '(1 8 64 256 1024))
45
46 (defvar *base-error-zero-baud-clk* (* 9 25 8192)
47 "Base multiple for multi-megahertz clock frequencies to have
48 0% error at common UART baud rates. Value of this base is 1.8432 million.
49 Common multiples of this are 2 (3.6864Mhz), 4 (7.3728Mhz), 8 (14745600),
50 and 10 (18.432MHz)")
51
52 (defun show-timers (f-cpu prescalers width)
53   (let ((max-count (expt 2 width)))
54     (format t "~&Prescalar MaxRate     MinUS     MinRate        MaxMS~%")
55     (dolist (prescale prescalers)
56       (let ((base (/ f-cpu prescale)))
57         (format t "~4D ~12,1F ~9,3F ~10,4F ~13,3F~%"
58                 prescale
59                 (coerce base 'float)
60                 (coerce (/ 1000000 base) 'float)
61                 (coerce (/ base max-count) 'float)
62                 (coerce (/ 1000 (/ base max-count)) 'float))))))
63
64 (defun show-8-bit-timers (&optional (f-cpu *f-cpu*))
65   (show-timers f-cpu *10-bit-prescalars* 8))
66
67 (defun show-16-bit-timers (&optional (f-cpu *f-cpu*))
68   (show-timers f-cpu *10-bit-prescalars* 16))
69
70 (defun show-32-bit-timers (&optional (f-cpu *f-cpu*))
71   "Show max/min periods for 32-bit timers. For 16-bit PIC
72 controllers, 32-bit timers use 8-bit prescalers"
73   (show-timers f-cpu *8-bit-prescalars* 32))
74
75 (defun ms-timer-width (ms f-cpu prescalars width)
76   "Returns the prescalar and compare count for both 8 and 16 bit timers."
77   (labels ((nearest-count (prescale)
78              (let ((count (round (* ms (/ f-cpu 1000 prescale))))
79                    (max-count (expt 2 width)))
80                (cond
81                  ((< count 1)
82                    1)
83                  ((<= count max-count)
84                    count)
85                  ((> max-count)
86                    max-count))))
87            (clk-ms (prescale count)
88              (unless (zerop count)
89                (/ count (/ f-cpu 1000 prescale))))
90            (percent-error (actual desired)
91              (* 100 (- (/ actual desired) 1))))
92
93     (dolist (prescale prescalars)
94       (let* ((count (nearest-count prescale))
95              (clk-ms (clk-ms prescale count))
96              (err (percent-error clk-ms ms))
97              (fmt-err (if (> err 1000)
98                         "   >1000%"
99                         (format nil "~8,3F%" err))))
100         (format t "~2D  ~4D  ~5D ~10,4F ~A~%"
101                 width prescale count clk-ms fmt-err)))))
102
103 (defun ms-timer (ms &optional (f-cpu *f-cpu*))
104   "Returns the prescalar and compare count for both 8 and 16 bit timers."
105   (dolist (width '(8 16))
106     (ms-timer-width ms f-cpu *10-bit-prescalars* width)))
107
108 (defconstant* +baud-rates+ '(300 600 1200 2400 4800 9600 14400 19200 28800
109                              38400 56000 57600 76800 115200 128000 250000
110                              256000 500000))
111
112 (defun avr-uart-divisors (&optional (f-cpu *f-cpu*) (view-below-percent nil))
113   "Displays the divisor UBRR and error percent for various baud
114 rates for F_CPU. UBBR is limited to 12 bits."
115   (dolist (baud +baud-rates+)
116     (let* ((ubrr (min 4096
117                       (max 0 (round (- (/ f-cpu 16 baud) 1)))))
118            (ubrr2 (min 4096
119                        (max 0 (round (- (/ f-cpu 8 baud) 1)))))
120            (actual-baud (/ f-cpu 16 (1+ ubrr)))
121            (actual-baud2 (/ f-cpu 8 (1+ ubrr2)))
122            (err (* 100 (- (/ actual-baud baud) 1)))
123            (err2 (* 100 (- (/ actual-baud2 baud) 1))))
124       (when (or (not view-below-percent)
125                 (or (< (abs err) view-below-percent)
126                     (< (abs err2) view-below-percent)))
127         (format t "~6D ~4D ~5,1F% ~4D ~5,1F%~%"
128                 baud ubrr err ubrr2 err2)))))
129
130 (defun pic-uart-divisors (&optional (fcy *f-cpu*) (view-below-percent nil))
131   "Displays the divisor BRG and error percent for various baud
132 rates for Fcy. BRG is limited to 16 bits."
133   (dolist (baud +baud-rates+)
134     (let* ((brg (min 65536
135                      (max 0 (round (- (/ fcy 16 baud) 1)))))
136            (actual-baud (/ fcy 16 (1+ brg)))
137            (err (* 100 (- (/ actual-baud baud) 1))))
138       (when (or (not view-below-percent)
139                 (< (abs err) view-below-percent))
140         (format t "~6D ~4D ~5,1F%~%" baud brg err)))))
141
142 (defun zero-error-uart-clocks ()
143   (dolist (mult '(1 2 4 6 8 10 12))
144     (format t "~&~8,4F MHz~%" (* mult  *base-error-zero-baud-clk* 1e-6))))
145