microbuilder/LPC810_CodeBase — pull request #3 by cpldcpu (2013-06-23 18:23:11+00:00)
Title: Bugfixes and major code size saving

Upon evaluating the clocking issue I found a number of places where code size could be reduced and also fixed several bugs. Please look at the individual commits to decide what to include and what not.

## Code size savings ##

In total the code size of the example was reduced from 2236 bytes to 1644 bytes without changing functionality or design. A reduction by 592 bytes or 26.5%.

1. Switched off code read protection (saves 432 bytes).
2. Inlined functions for data section initialization as the LPC810 only has one memory bank (saves 16 bytes)
3. Instead of using a global variable to save the clock frequency, a constant define can be transparently used by undefining ```__USE_DYNAMIC_CLOCK```. Doing this avoids to perform timing relevant calculations during run time and leads to significant savings in code space. To allow this, the clock definition had to be moved from the CMSIS program file to the header file. The change should be transparent to the user. In the course of doing this change I found that the clock system was actually never properly initialized since ```SystemInit()``` was never called (see bugs). Fixing this unfortunately negated a lot of the code space savings so that only 144 bytes were saved.
4. Size optimized the delay loops in CMSIS.

Potential for further size optimization:
- Due to the contruction of uart.c the baud rate calculation is still done at runtime when complied with -Os. May be optimized by using a #define.
- Use CPU based wait loops...

## Bug fixes ##

1. ```SystemCoreClockUpdate()``` was called instead of ```SystemInit()```, meaning that the default clock settings were used. Furthermore, the clock setting assumed an external clock source. Updated the clock setting to 30 Mhz based on the internal RC oscillator.
2. The Baud rate calculation incorrectly used the CPU clock instead of the main clock.