|
|
|
|
|
|
|
|
#include "kinetis.h" |
|
|
#include "kinetis.h" |
|
|
#include <avr/eeprom.h> |
|
|
#include <avr/eeprom.h> |
|
|
//#include "HardwareSerial.h" |
|
|
//#include "HardwareSerial.h" |
|
|
|
|
|
#if F_CPU > 120000000 && defined(__MK66FX1M0__) |
|
|
|
|
|
#include "core_pins.h" // delayMicroseconds() |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(__MK20DX128__) || defined(__MK20DX256__) |
|
|
#if defined(__MK20DX128__) || defined(__MK20DX256__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if F_CPU > 120000000 && defined(__MK66FX1M0__) |
|
|
#if F_CPU > 120000000 && defined(__MK66FX1M0__) |
|
|
static volatile uint16_t c_intrestore = 0; |
|
|
static volatile uint16_t c_intrestore = 0; |
|
|
|
|
|
|
|
|
void c_enable_irq( void ); |
|
|
void c_enable_irq( void ); |
|
|
void c_disable_irq( void ); |
|
|
void c_disable_irq( void ); |
|
|
static __inline__ uint32_t __get_primask(void) \ |
|
|
static __inline__ uint32_t __get_primask(void) \ |
|
|
|
|
|
|
|
|
if (SMC_PMSTAT == SMC_PMSTAT_HSRUN) { |
|
|
if (SMC_PMSTAT == SMC_PMSTAT_HSRUN) { |
|
|
c_disable_irq( ); // Turn off interrupts for the DURATION !!!! |
|
|
c_disable_irq( ); // Turn off interrupts for the DURATION !!!! |
|
|
SMC_PMCTRL = SMC_PMCTRL_RUNM(0); // exit HSRUN mode |
|
|
SMC_PMCTRL = SMC_PMCTRL_RUNM(0); // exit HSRUN mode |
|
|
while (SMC_PMSTAT == SMC_PMSTAT_HSRUN) ; // wait for !HSRUN |
|
|
|
|
|
|
|
|
while (SMC_PMSTAT == SMC_PMSTAT_HSRUN) delayMicroseconds(2); // wait for !HSRUN |
|
|
|
|
|
delayMicroseconds(100); |
|
|
restore_hsrun = 1; |
|
|
restore_hsrun = 1; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
{ |
|
|
{ |
|
|
if (restore_hsrun) { |
|
|
if (restore_hsrun) { |
|
|
SMC_PMCTRL = SMC_PMCTRL_RUNM(3); // enter HSRUN mode |
|
|
SMC_PMCTRL = SMC_PMCTRL_RUNM(3); // enter HSRUN mode |
|
|
while (SMC_PMSTAT != SMC_PMSTAT_HSRUN); // wait for HSRUN |
|
|
|
|
|
|
|
|
while (SMC_PMSTAT != SMC_PMSTAT_HSRUN) delayMicroseconds(2);; // wait for HSRUN |
|
|
restore_hsrun = 0; |
|
|
restore_hsrun = 0; |
|
|
c_enable_irq( ); // Restore interrupts only when HSRUN restored } |
|
|
c_enable_irq( ); // Restore interrupts only when HSRUN restored } |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
uint32_t offset = (uint32_t)addr; |
|
|
uint32_t offset = (uint32_t)addr; |
|
|
|
|
|
|
|
|
if (offset >= EEPROM_SIZE) return; |
|
|
if (offset >= EEPROM_SIZE) return; |
|
|
hsrun_off(); |
|
|
|
|
|
if (!(FTFL_FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); |
|
|
if (!(FTFL_FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); |
|
|
|
|
|
hsrun_off(); |
|
|
if (FlexRAM[offset] != value) { |
|
|
if (FlexRAM[offset] != value) { |
|
|
uint8_t stat = FTFL_FSTAT & 0x70; |
|
|
uint8_t stat = FTFL_FSTAT & 0x70; |
|
|
if (stat) FTFL_FSTAT = stat; |
|
|
if (stat) FTFL_FSTAT = stat; |
|
|
|
|
|
|
|
|
uint32_t offset = (uint32_t)addr; |
|
|
uint32_t offset = (uint32_t)addr; |
|
|
|
|
|
|
|
|
if (offset >= EEPROM_SIZE-1) return; |
|
|
if (offset >= EEPROM_SIZE-1) return; |
|
|
hsrun_off(); |
|
|
|
|
|
if (!(FTFL_FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); |
|
|
if (!(FTFL_FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); |
|
|
|
|
|
hsrun_off(); |
|
|
#ifdef HANDLE_UNALIGNED_WRITES |
|
|
#ifdef HANDLE_UNALIGNED_WRITES |
|
|
if ((offset & 1) == 0) { |
|
|
if ((offset & 1) == 0) { |
|
|
#endif |
|
|
#endif |
|
|
|
|
|
|
|
|
uint32_t offset = (uint32_t)addr; |
|
|
uint32_t offset = (uint32_t)addr; |
|
|
|
|
|
|
|
|
if (offset >= EEPROM_SIZE-3) return; |
|
|
if (offset >= EEPROM_SIZE-3) return; |
|
|
hsrun_off(); |
|
|
|
|
|
if (!(FTFL_FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); |
|
|
if (!(FTFL_FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); |
|
|
|
|
|
hsrun_off(); |
|
|
#ifdef HANDLE_UNALIGNED_WRITES |
|
|
#ifdef HANDLE_UNALIGNED_WRITES |
|
|
switch (offset & 3) { |
|
|
switch (offset & 3) { |
|
|
case 0: |
|
|
case 0: |
|
|
|
|
|
|
|
|
const uint8_t *src = (const uint8_t *)buf; |
|
|
const uint8_t *src = (const uint8_t *)buf; |
|
|
|
|
|
|
|
|
if (offset >= EEPROM_SIZE) return; |
|
|
if (offset >= EEPROM_SIZE) return; |
|
|
hsrun_off(); |
|
|
|
|
|
if (!(FTFL_FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); |
|
|
if (!(FTFL_FCNFG & FTFL_FCNFG_EEERDY)) eeprom_initialize(); |
|
|
|
|
|
hsrun_off(); |
|
|
if (len >= EEPROM_SIZE) len = EEPROM_SIZE; |
|
|
if (len >= EEPROM_SIZE) len = EEPROM_SIZE; |
|
|
if (offset + len >= EEPROM_SIZE) len = EEPROM_SIZE - offset; |
|
|
if (offset + len >= EEPROM_SIZE) len = EEPROM_SIZE - offset; |
|
|
while (len > 0) { |
|
|
while (len > 0) { |