gitignore
[rapper.git] / lpc176x / sysinit.c
1 void sysinit() {
2     // xtal_freq = 12000000;
3
4     volatile int *scs; 
5     scs = (int *)0x400fc1a0;
6     // enable main oscillator
7     *scs = 0x20;
8
9     // wait for oscillator to get ready
10     while ((*scs & (1<<6)) == 0);
11
12     volatile int *cclkcfg;  
13     cclkcfg = (int *)0x400FC104;
14     // set clockdivider to 1/4x
15     *cclkcfg = 0x03;
16
17     // peripheral clocks (all set to 1/4x cclk)
18     volatile int *pclksel0, *pclksel1;
19     pclksel0=(int *)0x400FC1A8;
20     *pclksel0 = 0;
21     pclksel1=(int *)0x400FC1AC;
22     *pclksel1 = 0;
23
24     // main clock set (pll0)
25     volatile int *clksrcsel;
26     clksrcsel = (int *)0x400FC10C;
27     *clksrcsel = 0x01;
28
29     volatile int *pll0con, *pll0stat, *pll0feed, *pll0cfg;
30     pll0con=(int*)0x400FC080;
31     pll0stat=(int *)0x400FC088;
32     // pll0feed : feed 0xAA followed by 0x55 to feed 
33     pll0feed=(int *)0x400FC08C;
34     pll0cfg=(int *)0x400FC084;
35
36     // FORMULA = FCCO = (2 * M * FIN) / N
37     // as Xtal (FIN) is 12MHz, N = 6 , M = 25 gives 100 MHz
38     // as both msel and nsel stores "N-1" store 24 and 5
39     *pll0cfg = 24 | (5 << 16);
40
41     // feed to set clock
42     *pll0feed = 0xaa;
43     *pll0feed = 0x55;
44
45
46     // enable pll0
47     *pll0con = 0x01;
48     *pll0feed = 0xaa;
49     *pll0feed = 0x55;
50
51     // wait for enable
52     while (!(*pll0stat & (1<<26)));
53
54     // lock pll0
55     *pll0con = 0x03;
56     *pll0feed = 0xaa;
57     *pll0feed = 0x55;
58
59     // wait for lock
60     while (!(*pll0stat & ((1<<25) | (1<<24))));
61
62     volatile int *pll1con, *pll1cfg, *pll1stat, *pll1feed;
63     pll1con=(int *)0x400FC0A0;
64     pll1cfg=(int *)0x400FC0A4;
65     pll1stat=(int *)0x400FC0A8;
66     pll1feed=(int *)0x400FC0AC;
67
68     // setup pll1 (multiplier 4, divider 6)
69     *pll1cfg = 3 | (0x01 << 5);
70
71     // enable pll1
72     *pll1con = 0x01;
73     *pll1feed = 0xaa;
74     *pll1feed = 0x55;
75     while (!(*pll1stat & (1<<10)));
76
77     // connect pll1
78     *pll1con = 0x03;
79     *pll1feed = 0xaa;
80     *pll1feed = 0x55;
81     while (!(*pll1stat & ((1<< 9) | (1<< 8))));
82 }