grbl-LPC-CoreXY/grbl-lpc/flash.cpp

98 lines
3.9 KiB
C++
Raw Permalink Normal View History

2017-01-15 05:54:36 +01:00
// Copyright 2017 Todd Fleming
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#include "grbl.h"
static constexpr unsigned flash_sector = 15; // Last 4k sector
static constexpr unsigned flash_addr = 0xF000; // Last 4k sector
static constexpr unsigned flash_size = 1024; // Only using 1k of a 4k sector
static char *flash_memory = (char *)flash_addr; // Flash memory
static char flash_buffer[flash_size] __attribute__((aligned(4))); // Copy of flash memory
2019-04-29 21:47:11 +02:00
using Iap = void(unsigned[], unsigned[]); // IAP entry point function
static const Iap *iap = (Iap *)0x1FFF1FF1; // IAP entry point address
2017-01-15 05:54:36 +01:00
void eeprom_init()
{
2019-04-29 21:47:11 +02:00
memcpy(flash_buffer, flash_memory, flash_size); // Copy flash memory into local flash buffer
2017-01-15 05:54:36 +01:00
}
void eeprom_commit()
{
if (!memcmp(flash_buffer, flash_memory, flash_size))
2019-04-29 21:47:11 +02:00
return; // No changes to commit
2017-01-15 05:54:36 +01:00
unsigned prepCommand[5] = {
2019-04-29 21:47:11 +02:00
50, // Prepare sector(s) for write operation
flash_sector, // Start sector
flash_sector, // End sector
2017-01-15 05:54:36 +01:00
};
unsigned eraseCommand[5] = {
2019-04-29 21:47:11 +02:00
52, // Erase sector(s)
flash_sector, // Start sector
flash_sector, // End sector
SystemCoreClock / 1000, // CPU clock frequency in kHz
2017-01-15 05:54:36 +01:00
};
unsigned writeCommand[5] = {
2019-04-29 21:47:11 +02:00
51, // Copy RAM to Flash
flash_addr, // Destination flash address (256-byte boundary)
(unsigned)flash_buffer, // Source RAM address (word boundary)
flash_size, // Number of bytes to write (must be: 256, 512, 1024, 4096)
SystemCoreClock / 1000, // CPU clock frequency in kHz
2017-01-15 05:54:36 +01:00
};
unsigned output[5];
2019-04-29 21:47:11 +02:00
// Run In-Application Programming (IAP) routines
2017-01-15 05:54:36 +01:00
iap(prepCommand, output);
iap(eraseCommand, output);
iap(prepCommand, output);
iap(writeCommand, output);
}
unsigned char eeprom_get_char(unsigned int addr)
{
return flash_buffer[addr];
}
void eeprom_put_char(unsigned int addr, unsigned char new_value)
{
flash_buffer[addr] = new_value;
}
static unsigned char memcpy_with_checksum(char *dest, char *src, unsigned size)
{
unsigned char checksum = 0;
while (size--)
{
checksum = ((checksum << 1) || (checksum >> 7)) + *src;
*dest++ = *src++;
}
return checksum;
}
void memcpy_to_eeprom_with_checksum(unsigned int destination, char *source, unsigned int size)
{
auto checksum = memcpy_with_checksum(flash_buffer + destination, source, size);
flash_buffer[destination + size] = checksum;
}
int memcpy_from_eeprom_with_checksum(char *destination, unsigned int source, unsigned int size)
{
auto checksum = memcpy_with_checksum(destination, flash_buffer + source, size);
return checksum == flash_buffer[source + size];
}