main.c


1
/****************************************************************************
2
 *
3
 * Project: LPC-E2468 blinking led demo
4
 *
5
 * Copyright: Ivan Vasilev, Olimex Ltd. All rights reserved.
6
 *
7
 * File: $File main.c $
8
 * Description: Board-specific USB initialization
9
 * Developer: Ivan Vasilev, <ivan at l123.org>
10
 *
11
 * Last change: $Date: 2008-04-08 09:23:26 +0300 (???????, 08 ????? 2008) $
12
 * Revision: $Revision: 4 $
13
 * Id: $Id: main.c 4 2008-04-08 06:23:26Z Ivan $
14
 * Author: $Author: Ivan $
15
 *
16
 *  This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License as
18
 * published by the Free Software Foundation; either version 2 of
19
 * the License, or (at your option) any later version.
20
 *
21
 ****************************************************************************/
22
23
#define PLL_MValue    11
24
#define PLL_NValue    0
25
#define CCLKDivValue  4
26
#define USBCLKDivValue  5
27
28
#include "lpc2468_registers.h"
29
#include "uart.h"
30
#include <stdio.h>
31
#include "led.h"
32
33
#define TRUE  1
34
#define  FALSE  0
35
36
void InitPLL(void);
37
void feed(void);
38
39
void IRQ_Routine (void)   __attribute__ ((interrupt("IRQ")));
40
void FIQ_Routine (void)   __attribute__ ((interrupt("FIQ")));
41
void SWI_Routine (void)   __attribute__ ((interrupt("SWI")));
42
void UNDEF_Routine (void) __attribute__ ((interrupt("UNDEF")));
43
44
/* return TRUE if the button is pressed */
45
int But1Pressed(void)
46
{
47
  if (FIO2PIN & (1<<13)) return FALSE;
48
  else return TRUE;
49
}
50
int But2Pressed(void)
51
{
52
  if (FIO2PIN & (1<<21)) return FALSE;
53
  else return TRUE;
54
}
55
56
void Delay(void)
57
{
58
  int j;
59
  for (j = 0; j < 10000; j++ );
60
}
61
62
/* return TRUE if the RTC is OK */
63
int TestRTC(void)
64
{
65
  int timeout = 1000000;
66
  RTC_CCR = 0;
67
  RTC_SEC = 0x00;
68
  RTC_CCR = 1<<1; /* clear the counter */
69
  RTC_CCR = 1<<0 | 1<<4; /* enable the clock using the external xtal */
70
71
  while (timeout--);
72
  timeout = RTC_CTC;
73
74
  if ((1550 < timeout) & (timeout < 1600)) return TRUE;
75
76
  return FALSE;
77
}
78
79
int  main (void) {
80
81
  int  j;                    // loop counter (stack variable)
82
  char chr = 'h';
83
  InitPLL();
84
85
  SCS |= 1<<0; /* enable fast IO on ports 0&1 */
86
87
  InitLeds();
88
  InitUART0();
89
  InitUART0Interrupt();
90
91
  SendString("=======================\n");
92
  SendString("Hello World...\n");
93
  SendString("=======================\n");
94
95
  while (1)
96
  {
97
  }
98
}
99
100
void InitPLL(void)
101
{
102
103
  volatile unsigned long MValue;
104
  volatile unsigned long NValue;
105
106
  if ( PLLSTAT & (1 << 25) )
107
  {
108
    PLLCON = 1;      /* Enable PLL, disconnected */
109
    PLLFEED = 0xaa;
110
    PLLFEED = 0x55;
111
  }
112
113
  PLLCON = 0;        /* Disable PLL, disconnected */
114
  PLLFEED = 0xaa;
115
  PLLFEED = 0x55;
116
117
  SCS |= 0x20;      /* Enable main OSC */
118
  while( !(SCS & 0x40) );  /* Wait until main OSC is usable */
119
120
  CLKSRCSEL = 0x1;    /* select main OSC, 12MHz, as the PLL clock source */
121
122
  PLLCFG = PLL_MValue | (PLL_NValue << 16);
123
  PLLFEED = 0xaa;
124
  PLLFEED = 0x55;
125
126
  PLLCON = 1;        /* Enable PLL, disconnected */
127
  PLLFEED = 0xaa;
128
  PLLFEED = 0x55;
129
130
  CCLKCFG = CCLKDivValue;  /* Set clock divider */
131
132
  while ( ((PLLSTAT & (1 << 26)) == 0) );  /* Check lock bit status */
133
134
  MValue = PLLSTAT & 0x00007FFF;
135
  NValue = (PLLSTAT & 0x00FF0000) >> 16;
136
  while ((MValue != PLL_MValue) && ( NValue != PLL_NValue) );
137
138
  PLLCON = 3;        /* enable and connect */
139
  PLLFEED = 0xaa;
140
  PLLFEED = 0x55;
141
  while ( ((PLLSTAT & (1 << 25)) == 0) );  /* Check connect bit status */
142
}
143
144
/*  Stubs for various interrupts (may be replaced later)  */
145
/*  ----------------------------------------------------  */
146
147
void IRQ_Routine (void) {
148
  while (1) ;
149
}
150
151
void FIQ_Routine (void)  {
152
  while (1) ;
153
}
154
155
156
void SWI_Routine (void)  {
157
  while (1) ;
158
}
159
160
161
void UNDEF_Routine (void) {
162
  while (1) ;
163
}