1818*/
1919#include < avr/wdt.h>
2020#include < Arduino.h>
21- #include < wiring_private.h>
2221
2322#define OSC_DELAY () do {\
2423 _NOP (); _NOP (); _NOP (); _NOP (); _NOP (); _NOP (); _NOP (); _NOP (); _NOP (); _NOP ();\
@@ -47,31 +46,30 @@ void __patch_wdt(void)
4746}
4847#endif
4948
50-
5149void sysClock (uint8_t mode)
5250{
53- if (mode == INT_OSC_32M) {
54- // switch to internal crystal
55- GPIOR0 = PMCR & 0x9f ;
56- PMCR = 0x80 ;
57- PMCR = GPIOR0;
58-
59- // disable external crystal
60- GPIOR0 = PMCR & 0xf3 ;
61- PMCR = 0x80 ;
62- PMCR = GPIOR0;
63-
64- } else if (mode == INT_OSC_32K) {
65- // switch to internal 32K crystal
66- GPIOR0 = (PMCR & 0x9f ) | 0x40 ;
67- PMCR = 0x80 ;
68- PMCR = GPIOR0;
69-
70- // disable external crystal
71- GPIOR0 = (PMCR & 0xf2 ) | 0x02 ;
72- PMCR = 0x80 ;
73- PMCR = GPIOR0;
74- } else if (mode == EXT_OSC_32K) {
51+ if (mode == INT_OSC_32M) {
52+ // switch to internal crystal
53+ GPIOR0 = PMCR & 0x9f ;
54+ PMCR = 0x80 ;
55+ PMCR = GPIOR0;
56+
57+ // disable external crystal
58+ GPIOR0 = PMCR & 0xf3 ;
59+ PMCR = 0x80 ;
60+ PMCR = GPIOR0;
61+
62+ } else if (mode == INT_OSC_32K) {
63+ // switch to internal 32K crystal
64+ GPIOR0 = (PMCR & 0x9f ) | 0x40 ;
65+ PMCR = 0x80 ;
66+ PMCR = GPIOR0;
67+
68+ // disable external crystal
69+ GPIOR0 = (PMCR & 0xf2 ) | 0x02 ;
70+ PMCR = 0x80 ;
71+ PMCR = GPIOR0;
72+ } else if (mode == EXT_OSC_32K) {
7573 // enable external 32K OSC crystal
7674 GPIOR0 = (PMCR & 0xf0 ) | 0x08 ;
7775 PMCR = 0x80 ;
@@ -84,38 +82,33 @@ void sysClock(uint8_t mode)
8482 GPIOR0 = (PMCR & 0x9f ) | 0x60 ;
8583 PMCR = 0x80 ;
8684 PMCR = GPIOR0;
87- } else {
85+ } else { // extern OSC
8886
89- // set to right prescale first
90- CLKPR = 0x80 ;
91- CLKPR = 0x01 ;
87+ // set to right prescale first
88+ CLKPR = 0x80 ;
89+ CLKPR = 0x01 ;
9290
93- asm volatile (" nop" );
94- asm volatile (" nop" );
91+ asm volatile (" nop" );
92+ asm volatile (" nop" );
9593
96- // enable external 400~32MHz OSC crystal
97- GPIOR0 = PMX2 | 0x04 ;
98- PMX2 = 0x80 ;
99- PMX2 = GPIOR0;
94+ // enable external 400~32MHz OSC crystal
95+ GPIOR0 = PMX2 | 0x04 ;
96+ PMX2 = 0x80 ;
97+ PMX2 = GPIOR0; // enable extern osc input
10098
101- GPIOR0 = (PMCR & 0xf3 ) | 0x04 ;
102- PMCR = 0x80 ;
103- PMCR = GPIOR0;
104-
105- // waiting for crystal stable
106- OSC_DELAY ();
99+ GPIOR0 = (PMCR & 0xf3 ) | 0x04 ;
100+ PMCR = 0x80 ;
101+ PMCR = GPIOR0;
107102
108- // switch to external 400~32MHz crystal
109- PMCR = 0x80 ;
110- PMCR = 0xb7 ;
111- OSC_DELAY ();
103+ // waiting for crystal stable
104+ OSC_DELAY ();
112105
113- // disable internal 32MHz crystal
114- PMCR = 0x80 ;
115- PMCR = 0xb6 ;
116- OSC_DELAY ();
106+ // switch to external 400~ 32MHz crystal
107+ PMCR = 0x80 ;
108+ PMCR = 0xb7 ;
109+ OSC_DELAY ();
117110 }
118- clock_set = 1 ;
111+ clock_set = 1 ;
119112}
120113
121114void sysClockPrescale (uint8_t divn)
@@ -289,7 +282,7 @@ int main(void)
289282 lgt8fx8x_init ();
290283
291284#if defined(CLOCK_SOURCE)
292- if (clock_set == 0 )
285+ if (clock_set == 0 )
293286 lgt8fx8x_clk_src ();
294287#endif
295288
0 commit comments