@@ -319,6 +319,17 @@ uint32_t GetClockFreq (uint32_t clk_src);
319319uint32_t SystemCoreClock = 120000000U ; /* System Clock Frequency (Core Clock) */
320320
321321
322+ /*----------------------------------------------------------------------------
323+ Approximate delay function (must be used after SystemCoreClockUpdate() call)
324+ *----------------------------------------------------------------------------*/
325+ static void WaitUs (uint32_t us , uint32_t clock_hz )
326+ {
327+ us *= (clock_hz / 1000000 ) / 3 ;
328+
329+ while (us -- );
330+ }
331+
332+
322333#define PLL0_NSEL_MAX (1<<8)
323334/* pre-divider: compute ndec from nsel */
324335static unsigned ndec_new (unsigned nsel )
@@ -417,7 +428,7 @@ static void SetClock (void) {
417428 (0 << 2 ) ; /* Low-frequency mode */
418429
419430 /* Wait ~250us @ 12MHz */
420- for ( i = 1500 ; i ; i -- );
431+ WaitUs ( 250 , CLK_XTAL );
421432
422433#ifdef USE_SPIFI
423434/* configure SPIFI clk to IRC via IDIVA (later IDIVA is configured to PLL1/3) */
@@ -456,8 +467,8 @@ static void SetClock (void) {
456467 LPC_CGU -> BASE_M4_CLK = (0x01 << 11 ) | /* Autoblock En */
457468 (0x09 << 24 ) ; /* Clock source: PLL1 */
458469
459- /* Max. BASE_M4_CLK frequency here is 102MHz, wait at least 20us */
460- for ( i = 1050 ; i ; i -- ); /* Wait minimum 2100 cycles */
470+ /* Wait 20us */
471+ WaitUs ( 20 , ( CLK_XTAL * ( PLL1_MSEL + 1 )) / (( PLL1_NSEL + 1 ) * 2 ));
461472#endif
462473 /* Configure PLL1 */
463474 LPC_CGU -> PLL1_CTRL = (0 << 0 ) | /* PLL1 Enabled */
@@ -547,17 +558,6 @@ static void SetClock (void) {
547558}
548559
549560
550- /*----------------------------------------------------------------------------
551- Approximate delay function (must be used after SystemCoreClockUpdate() call)
552- *----------------------------------------------------------------------------*/
553- #define CPU_NANOSEC (x ) (((uint64_t)(x) * SystemCoreClock)/1000000000)
554-
555- static void WaitUs (uint32_t us ) {
556- uint32_t cyc = us * CPU_NANOSEC (1000 )/4 ;
557- while (cyc -- );
558- }
559-
560-
561561/*----------------------------------------------------------------------------
562562 Measure frequency using frequency monitor
563563 *----------------------------------------------------------------------------*/
0 commit comments