Skip to content

Commit 8d7688b

Browse files
author
nulllab
committed
Fix extern osc eerom write fail
1 parent 95e0e5d commit 8d7688b

File tree

2 files changed

+49
-52
lines changed

2 files changed

+49
-52
lines changed

cores/lgt8f/main.cpp

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
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-
5149
void 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

121114
void 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

libraries/E2PROM/EEPROM.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@
3131
#include <inttypes.h>
3232
#include <Arduino.h>
3333

34+
#ifndef EEROM_SIZE
35+
#define EEROM_SIZE 1
36+
#endif
37+
3438
#define eeprom_SWM_ON() do { ECCR = 0x80; ECCR |= 0x10; } while(0);
3539
#define eeprom_SWM_OFF() do { ECCR = 0x80; ECCR &= 0xEF; } while(0);
3640
#define eeprom_reset() do { ECCR |= 0x20; } while(0)
3741

38-
#define EEROM_1KB_PAGE_FREE_SIZE (uint16_t)1020
42+
#define EEROM_1KB_PAGE_FREE_SIZE (uint16_t)(1024-2)
3943

4044
void eeprom_init(uint8_t _eerom_size = 1);
4145
int eeprom_size(bool theoretical = false);
@@ -175,7 +179,7 @@ struct EEPROMClass
175179
//STL and C++11 iteration capability.
176180
EEPtr begin() { return 0x00; }
177181
EEPtr end() { return length(); } //Standards requires this to be the item after the last valid entry. The returned pointer is invalid.
178-
uint16_t length() { return (eeprom_size(false) - 720); }
182+
uint16_t length() { return (eeprom_size(false)); }
179183

180184
//Functionality to 'get' and 'put' objects to and from EEPROM.
181185
template< typename T > T &get( int idx, T &t )

0 commit comments

Comments
 (0)