## Software Development with NXP LPC17xx and LPC11xx

### Software development environment with Eclipse & GCC

As usual the IDE of choice for all my embedded software development is Eclipse. It is completely free and have a lot of support for many processors. First thing first, follow my Eclipse page to setup the Eclipse IDE and plugins for ARM based development. Next step is to clone the EHAL source tree from github : https://github.com/I-SYST/EHAL. This can be done using git command line tools or use SourceTree from this site http://www.sourcetreeapp.com.

/your_root - Development root directory

|-- external - Contains downloaded SDKs from silicon vendors

| |-- CMSIS - ARM CMSIS SDK for all ARM platform (download from ARM)

| |-- nRF51_SDK - Nordic SDK

| |-- KSDK - Kinetis SDK

| |......

|-- EHAL - Put the EHAL here

| |-- include - Generic include common to all platform

| |-- src - Generic source common to all platform

| |-- ARM

| | |-- include - Common include for all ARM platform

| | |-- src - Common source for all ARM platform

| | |-- NXP - NXP ARM platform

| | | |-- include - Common include for all NXP ARM MCU

| | | |-- src - Common source for all NXP ARM MCU

| | | |-- LPC11xx - LPC11xx processor workspace

| | | | |-- CMSIS

| | | | |-- EHAL - Embedded Hardware Abstraction Library project for LPC11xx

| | | | | |-- include

| | | | | |-- src

| | | | |-- exemples - Example code

| | | |-- LPC17xx - LPC17xx processor workspace

| | | | |-- CMSIS

| | | | |-- EHAL - Embedded Hardware Abstraction Library project for NXP

| | | | | |-- include

| | | | | |-- src

| | | | |-- exemples - Example code

### Optimizing baudrate calculation on NXP LPCxx series UART

The description in the user manual is not easy to understand when you read it the first time. The baudrate generator uses the PCLK for it's time base with this equation baudrate = PCLK / (16 * Divisor). Unfortunately that division is not always integer. Therefore an adjustment called fractional rate is added to compensate. At first glance I though it was the fractional part of the division but it is not. The given formula is

baurate = PCLK / (16 * iDiv * (1 + DivAdd/MulVal))

Looking closely 1 + DivAdd/MulVal is a simple compensation factor. It is like adding a certain % of taxes to your purchase. We have here 3 unknowns with one equation. Therefore a trial & error loop algorithm is explained in the manual. It doesn't have to be that way. As we know it is an adjustment factor so let view DivAdd/MalVal as a % of taxes we need to add and call it xfactor. Let view the analogy here. Let's say we buy a pencil for $1 and we need to pay 5% taxes on it. The total we need to pay is $1.05. This from the fact we pay $1 + .05 which generally calculated as amount * 1.05 yield amount * (1 + %tax). We can now view the real Divisior as :

Divisor = iDiv * (1 + xfactor)

so the baurate formula can be viewed as

baudrate = PCLK / (16 * iDiv * (1 + xfactor))

It is now a lot simpler isn't it? We still have 2 unknowns with one equation. As we all know a factor is a value between 0.0 and 1.0, can't be anything else. So whatever the value we chose for it, the result is still good. So why not chose it as the % of the reminder value of this division

Divisor = PCLK / (16 * baurate)

let say rate16 = 16 * baurate. We don't want to predivise the PCLK/16 in order to preserve precision of the division

iDiv = PCLK / (rate16 * (1 + xfactor))

xfactor = (PCLK % rate16) / rate16

=>

DivAddVal = PCLK % rate16

MulVal = rate16

By choosing the factor of the reminder, if the division is integer the xfactor becomes zero. So we are still good.

In order to reduce DivAddVal & MulVal to the require 4 bits val, let's normalize the xfactor. Which means to make DivAddVal = 1, therefore MulVal = rate16 / (PCLK % rate16)

If after normalization MulVal is still bigger than 4 bits. It's simply means the fractional part is too small. It is negligeable so we set DivAddVal to zero, equivalent to an integer division.

Final algorithm is just one pass of simple calculations, no loops, no trial & error.

Code is available in EHAL library.

## No comments:

## Post a Comment