mirror of https://github.com/Desuuuu/klipper.git
spi_flash: support for firmware upgrades via SD Card
This module connects directly to MCU's previously flashed with Klipper, uploads Klipper firmware to an attached SD Card, and performs a device reset to intiate the bootloader's update process. Signed-off-by: Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
parent
7699834a61
commit
44c1caf2b9
|
@ -0,0 +1,93 @@
|
|||
# SPI Flash board definitions
|
||||
#
|
||||
# Copyright (C) 2021 Eric Callahan <arksine.code@gmail.com>
|
||||
#
|
||||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
###########################################################
|
||||
#
|
||||
# Board Definitions
|
||||
#
|
||||
###########################################################
|
||||
|
||||
BOARD_DEFS = {
|
||||
'generic-lpc1768': {
|
||||
'mcu': "lpc1768",
|
||||
'spi_bus': "ssp1",
|
||||
"cs_pin": "P0.6"
|
||||
},
|
||||
'generic-lpc1769': {
|
||||
'mcu': "lpc1769",
|
||||
'spi_bus': "ssp1",
|
||||
"cs_pin": "P0.6"
|
||||
},
|
||||
'btt-skr-mini': {
|
||||
'mcu': "stm32f103xe",
|
||||
'spi_bus': "spi1",
|
||||
"cs_pin": "PA4"
|
||||
},
|
||||
'flyboard-mini': {
|
||||
'mcu': "stm32f103xe",
|
||||
'spi_bus': "spi2",
|
||||
"cs_pin": "PB12",
|
||||
"current_firmware_path": "FLY.CUR"
|
||||
},
|
||||
'mks-robin-e3': {
|
||||
'mcu': "stm32f103xe",
|
||||
'spi_bus': "spi2",
|
||||
"cs_pin": "PA15",
|
||||
"firmware_path": "Robin_e3.bin",
|
||||
"current_firmware_path": "Robin_e3.cur"
|
||||
},
|
||||
'btt-skr-pro': {
|
||||
'mcu': "stm32f407xx",
|
||||
'spi_bus': "swspi",
|
||||
'spi_pins': "PA6,PB5,PA5",
|
||||
"cs_pin": "PA4"
|
||||
},
|
||||
'btt-gtr': {
|
||||
'mcu': "stm32f407xx",
|
||||
'spi_bus': "spi1",
|
||||
"cs_pin": "PA4"
|
||||
}
|
||||
}
|
||||
|
||||
###########################################################
|
||||
#
|
||||
# Board Definition Aliases
|
||||
#
|
||||
###########################################################
|
||||
|
||||
BOARD_ALIASES = {
|
||||
'btt-skr-v1.1': BOARD_DEFS['generic-lpc1768'],
|
||||
'btt-skr-v1.3': BOARD_DEFS['generic-lpc1768'],
|
||||
'btt-skr-v1.4': BOARD_DEFS['generic-lpc1768'],
|
||||
'mks-sgenl-v1': BOARD_DEFS['generic-lpc1768'],
|
||||
'mks-sbase': BOARD_DEFS['generic-lpc1768'],
|
||||
'smoothieboard-v1': BOARD_DEFS['generic-lpc1769'],
|
||||
'btt-skr-turbo-v1.4': BOARD_DEFS['generic-lpc1769'],
|
||||
'btt-skr-e3-turbo': BOARD_DEFS['generic-lpc1769'],
|
||||
'mks-sgenl-v2': BOARD_DEFS['generic-lpc1769'],
|
||||
'btt-skr-mini-v1.1': BOARD_DEFS['btt-skr-mini'],
|
||||
'btt-skr-mini-e3-v1': BOARD_DEFS['btt-skr-mini'],
|
||||
'btt-skr-mini-e3-v1.2': BOARD_DEFS['btt-skr-mini'],
|
||||
'btt-skr-mini-e3-v2': BOARD_DEFS['btt-skr-mini'],
|
||||
'btt-skr-mini-mz': BOARD_DEFS['btt-skr-mini'],
|
||||
'btt-skr-e3-dip': BOARD_DEFS['btt-skr-mini'],
|
||||
'btt002-v1': BOARD_DEFS['btt-skr-mini'],
|
||||
'creality-v4.2.7': BOARD_DEFS['btt-skr-mini'],
|
||||
'btt-skr-pro-v1.1': BOARD_DEFS['btt-skr-pro'],
|
||||
'btt-skr-pro-v1.2': BOARD_DEFS['btt-skr-pro'],
|
||||
'btt-gtr-v1': BOARD_DEFS['btt-gtr'],
|
||||
'mks-robin-e3d': BOARD_DEFS['mks-robin-e3'],
|
||||
}
|
||||
|
||||
def list_boards():
|
||||
return sorted(list(BOARD_DEFS.keys()) + list(BOARD_ALIASES.keys()))
|
||||
|
||||
def lookup_board(name):
|
||||
name = name.lower()
|
||||
bdef = BOARD_ALIASES.get(name, BOARD_DEFS.get(name, None))
|
||||
if bdef is not None:
|
||||
return dict(bdef)
|
||||
return None
|
|
@ -0,0 +1,232 @@
|
|||
// Python CFFI bindings for fatfs
|
||||
//
|
||||
// Copyright (C) 2021 Eric Callahan <arksine.code@gmail.com>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include <string.h> // memset
|
||||
#include <stdlib.h> // malloc
|
||||
#include "fatfs_api.h" // python-fatfs prototypes
|
||||
#include "../../lib/fatfs/ff.h" // fatfs APIs
|
||||
#include "../../lib/fatfs/diskio.h" // fatfs media APIs (callbacks)
|
||||
|
||||
/* Callbacks */
|
||||
static uint8_t (*python_disk_status)(void) = 0;
|
||||
static uint8_t (*python_disk_initialize)(void) = 0;
|
||||
static uint8_t (*python_disk_read)(uint8_t* buff, uint32_t sector,
|
||||
unsigned int count) = 0;
|
||||
static uint8_t (*python_disk_write)(const uint8_t* buff, uint32_t sector,
|
||||
unsigned int count) = 0;
|
||||
static uint8_t (*python_disk_ioctl)(uint8_t cmd, void* buff) = 0;
|
||||
static uint32_t (*python_fattime)(void) = 0;
|
||||
|
||||
void __visible
|
||||
fatfs_set_callbacks(
|
||||
uint8_t (*status_callback)(void),
|
||||
uint8_t (*init_callback)(void),
|
||||
uint8_t (*read_callback)(uint8_t*, uint32_t, unsigned int),
|
||||
uint8_t (*write_callback)(const uint8_t*, uint32_t, unsigned int),
|
||||
uint8_t (*ioctl_callback)(uint8_t, void*),
|
||||
uint32_t (*fattime_callback)(void)
|
||||
)
|
||||
{
|
||||
python_disk_status = status_callback;
|
||||
python_disk_initialize = init_callback;
|
||||
python_disk_read = read_callback;
|
||||
python_disk_write = write_callback;
|
||||
python_disk_ioctl = ioctl_callback;
|
||||
python_fattime = fattime_callback;
|
||||
}
|
||||
|
||||
void __visible
|
||||
fatfs_clear_callbacks(void)
|
||||
{
|
||||
python_disk_status = 0;
|
||||
python_disk_initialize = 0;
|
||||
python_disk_read = 0;
|
||||
python_disk_write = 0;
|
||||
python_disk_ioctl = 0;
|
||||
python_fattime = 0;
|
||||
}
|
||||
|
||||
/* Callbacks from fatfs to python. These methods are used to */
|
||||
/* Access access the SD Card APIs */
|
||||
|
||||
/* Get FAT Time */
|
||||
DWORD
|
||||
get_fattime(void)
|
||||
{
|
||||
if (python_fattime == 0)
|
||||
{
|
||||
// Return a default FATTIME of 1/1/2021
|
||||
return 41 << 25 | 1 << 21 | 1 << 16;
|
||||
}
|
||||
return python_fattime();
|
||||
}
|
||||
|
||||
DSTATUS
|
||||
disk_status(BYTE pdrv)
|
||||
{
|
||||
if (python_disk_status != 0)
|
||||
return python_disk_status();
|
||||
|
||||
return STA_NOINIT;
|
||||
}
|
||||
|
||||
DSTATUS
|
||||
disk_initialize(BYTE pdrv)
|
||||
{
|
||||
if (python_disk_initialize != 0)
|
||||
return python_disk_initialize();
|
||||
return STA_NOINIT;
|
||||
}
|
||||
|
||||
|
||||
DRESULT
|
||||
disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count)
|
||||
{
|
||||
if (python_disk_read != 0)
|
||||
return python_disk_read(buff, sector, count);
|
||||
return RES_NOTRDY;
|
||||
}
|
||||
|
||||
|
||||
DRESULT
|
||||
disk_write(BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count)
|
||||
{
|
||||
if (python_disk_write != 0)
|
||||
return python_disk_write(buff, sector, count);
|
||||
return RES_NOTRDY;
|
||||
}
|
||||
|
||||
DRESULT
|
||||
disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
|
||||
{
|
||||
if (python_disk_ioctl != 0)
|
||||
return python_disk_ioctl(cmd, buff);
|
||||
return RES_NOTRDY;
|
||||
}
|
||||
|
||||
/* Python-FatFS Interface Functions */
|
||||
|
||||
// Wrapper around the FatFS FIL struct. FIL is type defined
|
||||
// anonymously, thus can't be forward declared and poses
|
||||
// challenges with CFFI.
|
||||
struct ff_file {
|
||||
FIL fobj;
|
||||
};
|
||||
static FATFS fs;
|
||||
|
||||
uint8_t __visible
|
||||
fatfs_mount(void)
|
||||
{
|
||||
FRESULT res;
|
||||
memset(&fs, 0, sizeof(fs));
|
||||
res = f_mount(&fs, "", 1);
|
||||
return res;
|
||||
}
|
||||
|
||||
uint8_t __visible
|
||||
fatfs_unmount(void)
|
||||
{
|
||||
FRESULT res;
|
||||
res = f_unmount("");
|
||||
memset(&fs, 0, sizeof(fs));
|
||||
return res;
|
||||
}
|
||||
|
||||
struct ff_file* __visible
|
||||
fatfs_open(const char* path, uint8_t mode)
|
||||
{
|
||||
FRESULT res;
|
||||
struct ff_file* fhdl = malloc(sizeof(*fhdl));
|
||||
memset(fhdl, 0, sizeof(*fhdl));
|
||||
res = f_open(&(fhdl->fobj), path, mode);
|
||||
if (res != FR_OK) {
|
||||
free(fhdl);
|
||||
fhdl = NULL;
|
||||
}
|
||||
return fhdl;
|
||||
}
|
||||
|
||||
uint8_t __visible
|
||||
fatfs_close(struct ff_file* fhdl)
|
||||
{
|
||||
FRESULT res;
|
||||
res = f_close(&(fhdl->fobj));
|
||||
free(fhdl);
|
||||
return res;
|
||||
}
|
||||
|
||||
int __visible
|
||||
fatfs_read(struct ff_file* fhdl, void* rbuf, uint16_t btr)
|
||||
{
|
||||
FRESULT res;
|
||||
UINT bytes_read;
|
||||
res = f_read(&(fhdl->fobj), rbuf, btr, &bytes_read);
|
||||
if (res != FR_OK) {
|
||||
return -res;
|
||||
}
|
||||
return bytes_read;
|
||||
}
|
||||
|
||||
int __visible
|
||||
fatfs_write(struct ff_file* fhdl, const void* wbuf, uint16_t btw)
|
||||
{
|
||||
FRESULT res;
|
||||
UINT bytes_written;
|
||||
res = f_write(&(fhdl->fobj), wbuf, btw, &bytes_written);
|
||||
if (bytes_written < btw) {
|
||||
return -res;
|
||||
}
|
||||
return bytes_written;
|
||||
}
|
||||
|
||||
// Remove a file or director
|
||||
uint8_t __visible
|
||||
fatfs_remove(const char* path)
|
||||
{
|
||||
FRESULT res;
|
||||
res = f_unlink(path);
|
||||
return res;
|
||||
}
|
||||
|
||||
uint8_t __visible
|
||||
fatfs_get_fstats(struct ff_file_info* finfo, const char* path)
|
||||
{
|
||||
FRESULT res;
|
||||
FILINFO nfo;
|
||||
res = f_stat(path, &nfo);
|
||||
if (res == FR_OK)
|
||||
memcpy(finfo, &nfo, sizeof(nfo));
|
||||
return res;
|
||||
}
|
||||
|
||||
uint8_t __visible
|
||||
fatfs_get_disk_info(struct ff_disk_info* dinfo)
|
||||
{
|
||||
FRESULT res;
|
||||
res = f_getlabel("", dinfo->label, &(dinfo->serial_number));
|
||||
if (res != FR_OK)
|
||||
return res;
|
||||
dinfo->fs_type = fs.fs_type;
|
||||
return res;
|
||||
}
|
||||
|
||||
uint8_t __visible
|
||||
fatfs_list_dir(struct ff_file_info* flist, uint8_t max_size, char* path)
|
||||
{
|
||||
FRESULT res;
|
||||
DIR dir;
|
||||
FILINFO nfo;
|
||||
res = f_opendir(&dir, path);
|
||||
if (res == FR_OK) {
|
||||
for (uint8_t i=0; i < max_size; i++) {
|
||||
res = f_readdir(&dir, &nfo);
|
||||
if (res != FR_OK ||nfo.fname[0] == 0)
|
||||
break;
|
||||
memcpy(&(flist[i]), &nfo, sizeof(nfo));
|
||||
}
|
||||
}
|
||||
return FR_OK;
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
// Python CFFI definitions for fatfs
|
||||
//
|
||||
// Copyright (C) 2021 Eric Callahan <arksine.code@gmail.com>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
#ifndef FATFS_API_H
|
||||
#define FATFS_API_H
|
||||
|
||||
#include <stdint.h> // uint8_t
|
||||
|
||||
#define __visible __attribute__((externally_visible))
|
||||
|
||||
void fatfs_set_callbacks(
|
||||
uint8_t (*status_callback)(void),
|
||||
uint8_t (*init_callback)(void),
|
||||
uint8_t (*read_callback)(uint8_t*, uint32_t, unsigned int),
|
||||
uint8_t (*write_callback)(const uint8_t*, uint32_t, unsigned int),
|
||||
uint8_t (*ioctl_callback)(uint8_t, void*),
|
||||
uint32_t (*fattime_callback)(void));
|
||||
void fatfs_clear_callbacks(void);
|
||||
|
||||
struct ff_file;
|
||||
|
||||
struct ff_file_info {
|
||||
uint32_t size;
|
||||
uint16_t modified_date;
|
||||
uint16_t modified_time;
|
||||
uint8_t attrs;
|
||||
char name[13];
|
||||
};
|
||||
|
||||
struct ff_disk_info {
|
||||
char label[12];
|
||||
uint32_t serial_number;
|
||||
uint8_t fs_type;
|
||||
};
|
||||
|
||||
uint8_t fatfs_mount(void);
|
||||
uint8_t fatfs_unmount(void);
|
||||
struct ff_file* fatfs_open(const char* path, uint8_t mode);
|
||||
uint8_t fatfs_close(struct ff_file* fhdl);
|
||||
int fatfs_read(struct ff_file* fhdl, void* rbuf, uint16_t btr);
|
||||
int fatfs_write(struct ff_file* fhdl, const void* wbuf, uint16_t btw);
|
||||
uint8_t fatfs_remove(const char* path);
|
||||
uint8_t fatfs_get_fstats(struct ff_file_info* finfo, const char* path);
|
||||
uint8_t fatfs_get_disk_info(struct ff_disk_info* dinfo);
|
||||
uint8_t fatfs_list_dir(struct ff_file_info* flist, uint8_t max_size,
|
||||
char* path);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,87 @@
|
|||
# FatFS CFFI support
|
||||
#
|
||||
# Copyright (C) 2021 Eric Callahan <arksine.code@gmail.com>
|
||||
#
|
||||
# This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
import os
|
||||
import sys
|
||||
KLIPPER_DIR = os.path.abspath(os.path.join(
|
||||
os.path.dirname(__file__), "../../"))
|
||||
sys.path.append(os.path.join(KLIPPER_DIR, "klippy"))
|
||||
import chelper
|
||||
|
||||
DEST_LIB = "fatfs.so"
|
||||
FATFS_DIR = os.path.join(KLIPPER_DIR, "lib/fatfs")
|
||||
FATFS_SRC = ["ff.c", "ffsystem.c", "ffunicode.c"]
|
||||
SPI_FLASH_SRC = ["fatfs_api.c"]
|
||||
FATFS_HEADERS = ["diskio.h", "ff.h", "ffconf.h"]
|
||||
SPI_FLASH_HEADERS = ["fatfs_api.h"]
|
||||
|
||||
FATFS_CDEFS = """
|
||||
void fatfs_set_callbacks(
|
||||
uint8_t (*status_callback)(void),
|
||||
uint8_t (*init_callback)(void),
|
||||
uint8_t (*read_callback)(uint8_t*, uint32_t, unsigned int),
|
||||
uint8_t (*write_callback)(const uint8_t*, uint32_t, unsigned int),
|
||||
uint8_t (*ioctl_callback)(uint8_t, void*),
|
||||
uint32_t (*fattime_callback)(void));
|
||||
void fatfs_clear_callbacks(void);
|
||||
|
||||
struct ff_file_info {
|
||||
uint32_t size;
|
||||
uint16_t modified_date;
|
||||
uint16_t modified_time;
|
||||
uint8_t attrs;
|
||||
char name[13];
|
||||
};
|
||||
|
||||
struct ff_disk_info {
|
||||
char label[12];
|
||||
uint32_t serial_number;
|
||||
uint8_t fs_type;
|
||||
};
|
||||
|
||||
uint8_t fatfs_mount(void);
|
||||
uint8_t fatfs_unmount(void);
|
||||
struct ff_file* fatfs_open(const char* path, uint8_t mode);
|
||||
uint8_t fatfs_close(struct ff_file* fhdl);
|
||||
int fatfs_read(struct ff_file* fhdl, void* rbuf, uint16_t btr);
|
||||
int fatfs_write(struct ff_file* fhdl, const void* wbuf, uint16_t btw);
|
||||
uint8_t fatfs_remove(const char* path);
|
||||
uint8_t fatfs_get_fstats(struct ff_file_info* finfo, const char* path);
|
||||
uint8_t fatfs_get_disk_info(struct ff_disk_info* dinfo);
|
||||
uint8_t fatfs_list_dir(struct ff_file_info* flist, uint8_t max_size,
|
||||
char* path);
|
||||
"""
|
||||
|
||||
fatfs_ffi_main = None
|
||||
fatfs_ffi_lib = None
|
||||
|
||||
def check_fatfs_build(printfunc=lambda o: o):
|
||||
printfunc("Checking FatFS CFFI Build...\n")
|
||||
ffi_main, ffi_lib = chelper.get_ffi()
|
||||
srcdir = os.path.dirname(os.path.realpath(__file__))
|
||||
srcfiles = chelper.get_abs_files(FATFS_DIR, FATFS_SRC)
|
||||
srcfiles.extend(chelper.get_abs_files(srcdir, SPI_FLASH_SRC))
|
||||
ofiles = chelper.get_abs_files(FATFS_DIR, FATFS_HEADERS)
|
||||
ofiles.extend(chelper.get_abs_files(srcdir, SPI_FLASH_HEADERS))
|
||||
destlib = os.path.join(srcdir, DEST_LIB)
|
||||
if chelper.check_build_code(srcfiles+ofiles+[__file__], destlib):
|
||||
if chelper.check_gcc_option(chelper.SSE_FLAGS):
|
||||
cmd = "%s %s %s" % (chelper.GCC_CMD, chelper.SSE_FLAGS,
|
||||
chelper.COMPILE_ARGS)
|
||||
else:
|
||||
cmd = "%s %s" % (chelper.GCC_CMD, chelper.COMPILE_ARGS)
|
||||
printfunc("Building FatFS shared library...")
|
||||
os.system(cmd % (destlib, ' '.join(srcfiles)))
|
||||
printfunc("Done\n")
|
||||
global fatfs_ffi_main, fatfs_ffi_lib
|
||||
ffi_main.cdef(FATFS_CDEFS)
|
||||
fatfs_ffi_lib = ffi_main.dlopen(destlib)
|
||||
fatfs_ffi_main = ffi_main
|
||||
|
||||
def get_fatfs_ffi():
|
||||
global fatfs_ffi_main, fatfs_ffi_lib
|
||||
if fatfs_ffi_main is None:
|
||||
check_fatfs_build()
|
||||
return fatfs_ffi_main, fatfs_ffi_lib
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue