00001 /* 00002 * File : delay.h 00003 * Title : Delay Routines. 00004 * 00005 * Revised : 03/Apr/2007 00006 * Created : 25/Jan/2006 00007 * Version : 1.0.1 00008 * Editor Tabs : 3 00009 * Tool-Chain : arm-elf-gcc 4.0.1 00010 * 00011 * MCU : Philips LPC2xxx (32-Bits ARM7TDMI-S Core) 00012 * 00013 * Author : Boris Estudiez <stk@freeshell.org> 00014 * Website : http://stk.freeshell.org 00015 * Licence : GPL 00016 * 00017 * Description : Inline functions and macros for accurate delays. 00018 * 00019 * Note[1] : No timers or interruptions used. 00020 * Note[2] : MAM should be Fully Enabled for CCLK > 20 MHz. 00021 * 00022 * History : Originally relased for my LPC-STXLIB library. 00023 * 00024 * (C) Copyright by slicetex electronics, 2001-2007. 00025 */ 00026 00027 #ifndef __DELAY_H__ 00028 #define __DELAY_H__ 00029 00030 // --------------------------------------------------------------------------- 00031 // HEADERS 00032 // --------------------------------------------------------------------------- 00033 00034 #include "inttypes.h" // C99 Standard Integer Data Types 00035 00036 // --------------------------------------------------------------------------- 00037 // CONFIGURATION 00038 // 00039 // Setup FOSC and PLL_MUL defines according to your system. 00040 // 00041 // For example, LPC2138, XTAL = 12000000 Hz and PLL_multiplier = 5. 00042 // CPU clock will be 60 MHz. 00043 // --------------------------------------------------------------------------- 00044 00045 #define FOSC (12000000) // Specify Oscilator Output Frequency. 00046 #define PLL_MUL (5) // Specify PLL Multiplier. 00047 00048 #define CCLK (FOSC * PLL_MUL) // CPU Clock Frequency. 00049 00050 // --------------------------------------------------------------------------- 00051 // CPU CYCLES DEFINES 00052 // 00053 // ARM7TDMI-S cycles per micro-second, mili-second, second, etc 00054 // Remember: 1 clock = 1 cpu_cycle on this architecture. 00055 // --------------------------------------------------------------------------- 00056 00057 #define CPU_CLOCKS_PER_CYCLE (1) 00058 #define CPU_CYCLES_PER_SEC (CCLK/CPU_CLOCKS_PER_CYCLE) 00059 #define CPU_CYCLES_PER_US (CPU_CYCLES_PER_SEC/1000000) 00060 #define CPU_CYCLES_PER_MS (CPU_CYCLES_PER_SEC/1000) 00061 00062 // --------------------------------------------------------------------------- 00063 // CONSTANTS AND COMPUTATIONS 00064 // --------------------------------------------------------------------------- 00065 00066 // Define cycles per loop for each delay function: 00067 00068 #define DELAY4_CYCLES_PER_LOOP (4) 00069 00070 // Compute how many CPU clycles per US, MS and Second fix into one 00071 // delay loop for each delay function. 00072 00073 #define DELAY4_LOOPS_PER_US (CPU_CYCLES_PER_US/DELAY4_CYCLES_PER_LOOP) 00074 #define DELAY4_LOOPS_PER_MS (CPU_CYCLES_PER_MS/DELAY4_CYCLES_PER_LOOP) 00075 #define DELAY4_LOOPS_PER_SEC (CPU_CYCLES_PER_SEC/DELAY4_CYCLES_PER_LOOP) 00076 00077 // --------------------------------------------------------------------------- 00078 // MACROS 00079 // --------------------------------------------------------------------------- 00080 00081 // --------------------------------------------------------------------------- 00082 // DelayUS() : Delay in Micro-Seconds. 00083 // Max Delay : MAX_UINT32_T/DELAY4_LOOPS_PER_US [micro-seconds] 00084 // Error : 2 cycles (at least) + MAMTIM cycles (if CCLK > 20 Mhz). 00085 // --------------------------------------------------------------------------- 00086 00087 #define DelayUS(us) Delay4( ((uint32_t) ( (us) * (DELAY4_LOOPS_PER_US) )) ) 00088 00089 // --------------------------------------------------------------------------- 00090 // DelayMS() : Delay in Mili-Seconds. 00091 // Max Delay : MAX_UINT32_T/DELAY4_LOOPS_PER_MS [mili-seconds] 00092 // Error : 2 cycles (at least) + MAMTIM cycles (if CCLK > 20 Mhz). 00093 // --------------------------------------------------------------------------- 00094 00095 #define DelayMS(ms) Delay4( ((uint32_t) ( (ms) * (DELAY4_LOOPS_PER_MS) )) ) 00096 00097 // --------------------------------------------------------------------------- 00098 // DelayS() : Delay in Seconds. 00099 // Max Delay : MAX_UINT32_T/(DELAY4_LOOPS_PER_SEC) [seconds] 00100 // Error : 2 cycles (at least) + MAMTIM cycles (if CCLK > 20 Mhz). 00101 // --------------------------------------------------------------------------- 00102 00103 #define DelayS(s) Delay4( ((uint32_t) ( (s) * (DELAY4_LOOPS_PER_SEC) )) ) 00104 00105 // --------------------------------------------------------------------------- 00106 // INLINE FUNCTIONS 00107 // --------------------------------------------------------------------------- 00108 00109 // *************************************************************************** 00110 // Delay4(): Delay 4 cycles per loop. 00111 // 00112 // INPUT: loops = number of loops. 00113 // 00114 // Total Delay Cycles = 4 * loops - 2. 00115 // Max Delay Cycles = 4 * (2^32 - 1) - 2 = 17,179,869,179 (wow!!!) 00116 // 00117 // Example: 00118 // If CCLK = 60 Mhz, then 1 cycle (clock) = ~ 16.666 nS. 00119 // Max Delay Time = (Max Delay Cycles) * 16.666 nS = 286.319 Sec. = 4.77 Min. 00120 // *************************************************************************** 00121 00122 static __inline__ void Delay4(uint32_t loops) 00123 { 00124 asm volatile 00125 ( \ 00126 "\n\t" \ 00127 "LOOP_%=: \n\t" \ 00128 "subs %0, %0, #1 @ 1 cycle \n\t" \ 00129 "bne LOOP_%= @ 3 cycles (true), 1 cycle (false) \n\t" \ 00130 "\n\t" \ 00131 : /* NO OUTPUTS */ \ 00132 : "r" (loops) \ 00133 ); 00134 } 00135 00136 00137 // --------------------------------------------------------------------------- 00138 00139 #endif 00140 00141 // --------------------------------------------------------------------------- 00142 // END OF FILE 00143 // --------------------------------------------------------------------------- 00144
1.4.7