Yahoo Groups archive

Lpc2000

Index last updated: 2026-04-28 23:31 UTC

Message

Strange behaviour of the VPBDIV register

2005-06-10 by gustavo_sagasti

Hi:
I tried the following code:

#include <LPC21XX.h>
#include <stdio.h>

#define OSC_CLOCK_FREQ 14745600
#define CR     0x0D

void
UARTInitialize(unsigned int baud)
{
  U0LCR = 0x83; /* 8 bit, 1 stop bit, no parity, enable DLAB */
  U0DLL = 192;	/* 9600 bps @ Peripheral Clock = 2 * 14745600 MHz */
	
  U0DLM = 0;		
  U0LCR &= ~0x80; /* Disable DLAB */
  PINSEL0 = (PINSEL0 & ~0xF) | 0x5;
  U0FCR = 1;
}

unsigned int
cClkFreq(void)
{
  return (OSC_CLOCK_FREQ * (PLLCON & 1 ? (PLLCFG & 0x1F) + 1 : 1));
}

unsigned int
pClkFreq(void)
{ 
  unsigned int divider=0;
  switch (VPBDIV & 3)
    {
      case 0:
        divider = 4;
        break;
      case 1:
        divider = 1;
        break;
      case 2:
        divider = 2;
        break;	  
    } 
  return cClkFreq() / divider;
}

int putchar(int ch)  
{                  
  if (ch == '\n')  {
    while (!(U0LSR & 0x20));
    U0THR = CR;                      
  }
  while (!(U0LSR & 0x20));
  return (U0THR = ch);
}

int main (void) {
   UARTInitialize(9600);

   printf("Processor  Clock %d\n", cClkFreq());
   printf("Peripheral Clock %d\n", pClkFreq());  
   printf("Peripheral Clock %d\n", pClkFreq());
   printf("Peripheral Clock %d\n", pClkFreq());

  while (1) ;
  return 0;
}

It is compiled with Keil CARM, and the VPB Clock = CPU Clock / 2
The first printf the peripheral clock prints a wrong value, while the 
rest are correct.
Here is the result;
Processor  Clock 58982400
Peripheral Clock 14745600   ????
Peripheral Clock 29491200
Peripheral Clock 29491200

Apparently, the problem is when the VPBDIV register is read. The 
first time it reads as zero, while the rest it reads the correct 
value.

Someone have any idea about this?
Thanks
Gus

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.