Skip to content

Commit

Permalink
Fix extern osc eerom write fail
Browse files Browse the repository at this point in the history
  • Loading branch information
nulllab committed Mar 6, 2022
1 parent 95e0e5d commit 8d7688b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 52 deletions.
93 changes: 43 additions & 50 deletions cores/lgt8f/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/
#include <avr/wdt.h>
#include <Arduino.h>
#include <wiring_private.h>

#define OSC_DELAY() do {\
_NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP(); _NOP();\
Expand Down Expand Up @@ -47,31 +46,30 @@ void __patch_wdt(void)
}
#endif


void sysClock(uint8_t mode)
{
if(mode == INT_OSC_32M) {
// switch to internal crystal
GPIOR0 = PMCR & 0x9f;
PMCR = 0x80;
PMCR = GPIOR0;

// disable external crystal
GPIOR0 = PMCR & 0xf3;
PMCR = 0x80;
PMCR = GPIOR0;

} else if(mode == INT_OSC_32K) {
// switch to internal 32K crystal
GPIOR0 = (PMCR & 0x9f) | 0x40;
PMCR = 0x80;
PMCR = GPIOR0;

// disable external crystal
GPIOR0 = (PMCR & 0xf2) | 0x02;
PMCR = 0x80;
PMCR = GPIOR0;
} else if(mode == EXT_OSC_32K) {
if(mode == INT_OSC_32M) {
// switch to internal crystal
GPIOR0 = PMCR & 0x9f;
PMCR = 0x80;
PMCR = GPIOR0;

// disable external crystal
GPIOR0 = PMCR & 0xf3;
PMCR = 0x80;
PMCR = GPIOR0;

} else if(mode == INT_OSC_32K) {
// switch to internal 32K crystal
GPIOR0 = (PMCR & 0x9f) | 0x40;
PMCR = 0x80;
PMCR = GPIOR0;

// disable external crystal
GPIOR0 = (PMCR & 0xf2) | 0x02;
PMCR = 0x80;
PMCR = GPIOR0;
} else if(mode == EXT_OSC_32K) {
// enable external 32K OSC crystal
GPIOR0 = (PMCR & 0xf0) | 0x08;
PMCR = 0x80;
Expand All @@ -84,38 +82,33 @@ void sysClock(uint8_t mode)
GPIOR0 = (PMCR & 0x9f) | 0x60;
PMCR = 0x80;
PMCR = GPIOR0;
} else {
} else { // extern OSC

// set to right prescale first
CLKPR = 0x80;
CLKPR = 0x01;
// set to right prescale first
CLKPR = 0x80;
CLKPR = 0x01;

asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");

// enable external 400~32MHz OSC crystal
GPIOR0 = PMX2 | 0x04;
PMX2 = 0x80;
PMX2 = GPIOR0;
// enable external 400~32MHz OSC crystal
GPIOR0 = PMX2 | 0x04;
PMX2 = 0x80;
PMX2 = GPIOR0; //enable extern osc input

GPIOR0 = (PMCR & 0xf3) | 0x04;
PMCR = 0x80;
PMCR = GPIOR0;

// waiting for crystal stable
OSC_DELAY();
GPIOR0 = (PMCR & 0xf3) | 0x04;
PMCR = 0x80;
PMCR = GPIOR0;

// switch to external 400~32MHz crystal
PMCR = 0x80;
PMCR = 0xb7;
OSC_DELAY();
// waiting for crystal stable
OSC_DELAY();

// disable internal 32MHz crystal
PMCR = 0x80;
PMCR = 0xb6;
OSC_DELAY();
// switch to external 400~32MHz crystal
PMCR = 0x80;
PMCR = 0xb7;
OSC_DELAY();
}
clock_set = 1;
clock_set = 1;
}

void sysClockPrescale(uint8_t divn)
Expand Down Expand Up @@ -289,7 +282,7 @@ int main(void)
lgt8fx8x_init();

#if defined(CLOCK_SOURCE)
if(clock_set == 0)
if (clock_set == 0)
lgt8fx8x_clk_src();
#endif

Expand Down
8 changes: 6 additions & 2 deletions libraries/E2PROM/EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
#include <inttypes.h>
#include <Arduino.h>

#ifndef EEROM_SIZE
#define EEROM_SIZE 1
#endif

#define eeprom_SWM_ON() do { ECCR = 0x80; ECCR |= 0x10; } while(0);
#define eeprom_SWM_OFF() do { ECCR = 0x80; ECCR &= 0xEF; } while(0);
#define eeprom_reset() do { ECCR |= 0x20; } while(0)

#define EEROM_1KB_PAGE_FREE_SIZE (uint16_t)1020
#define EEROM_1KB_PAGE_FREE_SIZE (uint16_t)(1024-2)

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

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

0 comments on commit 8d7688b

Please sign in to comment.