# lpc-clk
[LPC2xxx related tools running on the host]

LPC2xxx PLL and UART calculator tool. More...

LPC2xxx PLL and UART calculator tool.

This program can calculate the PLL settings for a given clock source and desired CPU clock frequency. In addition, it can calculate the UART's division parameters (integer and fractional) for a given set of baud rates.

## Usage

`lpc-clk options list_of_baud_rates`
The output of the program is in the format of a valid C header file, so you can incorporate it in your sources and use the generated values without actually typing them from the screen. If you start the program without any command line switches, it will print a usage message that details all options. If you supply the required parameters on the command line, then the program calculates the PLL and UART settings, formats them so that they can be included in your C source and prints the result on the standard output, which you can redirect to a file.
The following will be `#define` -d (not including the UART related definitions):

    CLOCK_XTAL       is the crystal frequency, in Hz
    CLOCK_CCLK       is the (achieved) CPU frequency, in Hz
    CLOCK_PLL_M      is the 'M' value setting for the PLL
    CLOCK_PLL_N      is the 'N' value setting for the PLL
    CLOCK_CPU_DIV    is the CPU clock division value
    CLOCK_USB_DIV    is the USB clock division value
    CLOCK_MAM_TIM    is the minimum MAM time value
    UART_CDIV        is the ratio between CCLK and the UART's PCLK

In case of an error, an error message is sent to `stderr`.

## Options

- `-x xtal`
  The reference clock frequency (float)
  If the number is less than 100, it is taken as MHz, otherwise as Hz. Values between 32.768kHz and 40MHz are accepted. This option is mandatory.

- `-c cclk`
  The required CPU clock (float).
  If the number is less than 100, it is taken as MHz, otherwise as Hz. The minimum is 1.075MHz, the maximum is 72MHz. This option is mandatory.

- `-h max_clock`
  Maximum CPU clock (float).
  If the number is less than 100, it is taken as MHz, otherwise as Hz. The default is 60MHz or the specified clock (whichever is larger). If you specify a value that is less than the requested clock frequency the the requested clock frequency will be used instead. This value is only used if the required CPU clock can not be exactly achieved. A value close to the requested frequency will be accepted if it is not higher than this value.

- `-l min_clock`
  Minimum CPU clock (float).
  If the number is less than 100, it is taken as MHz, otherwise as Hz. The default is 0. If you specify a value that is more than the requested clock frequency then the requested clock frequency will be used instead. This value is only used if the required CPU clock can not be exactly achieved. A value close to the requested frequency will be accepted if it is not lower than this value.

- `-u max_error`
  Maximum USB error (float).
  Indicates that USB is needed. In that case the VCO frequency must be set to an exact multiple of 96MHz. The parameter indicates the maximum frequency error, in ppm. Specifying 0 or a negative value disables the USB calculations. The default is 0. Suggested value is 500ppm.

- `-p n`
  The peripheral clock divider for the UART's clock (1, 2, 4 or 8).
  The default value is 4.

- `-i string`
  The identifier to be used for the #ifndef directive protecting the header file from multiple inclusions. It must be a valid C identifier. The program does *not* check the validity of the string, it uses it verbatim. The default is `_LPC_CLOCK_TOOL`.

- `-a`
  Selects the alternate format for UART calculations.
  The default is to create three definitions for each baudrate:
  `UART_I_XXXX` is the UART's integer divider for baud rate XXXX
  `UART_M_XXXX` is the UART's fractional divider's 'M' value
  `UART_A_XXXX` is the UART's fractional divider's 'A' value
  The alternative format is that an initialiser is defined for an array of structures. The structure has to have 4 members, the first one is the baud rate, the next one is the integer divider value, then the fractional divider's M value and finally the fractional divider's A value. The suggested member sizes are int, short, char and char, all unsigned. The initialiser is called `UART_CLOCK_TABLE_INIT` and can be used like this:

          // Structure to store the baud rate parameters

          typedef struct {
              unsigned int    b;  // Baud rate
              unsigned short  i;  // Integer divisor
              unsigned char   m;  // Fractional divider M value
              unsigned char   a;  // Fractional divider A value
          } BAUDS;

          // Table holding the baud rates

          static const BAUDS brate_table[] = { UART_CLOCK_TABLE_INIT };

## List of baud rates

It is an optional list of integers, separated by space. Each number is a baud rate for which the divisor values should be calculated. For example, the list
`9600 38400 115200`
will make the tool calculate the divisor values for three common baud rates.

## Exit code

0 : success
1 : command line format error or illegal parameter value
2 : the requirements could not be satisfied