Skip to content

oofatfs: Allow fat32 mkfs to be compiled out. optimize rainbowio & enable everywhere #5578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions lib/oofatfs/ff.c
Original file line number Diff line number Diff line change
Expand Up @@ -5392,7 +5392,9 @@ FRESULT f_mkfs (
const UINT n_fats = 1; /* Number of FATs for FAT/FAT32 volume (1 or 2) */
const UINT n_rootdir = 512; /* Number of root directory entries for FAT volume */
static const WORD cst[] = {1, 4, 16, 64, 256, 512, 0}; /* Cluster size boundary for FAT volume (4Ks unit) */
#if FF_MKFS_FAT32
static const WORD cst32[] = {1, 2, 4, 8, 16, 32, 0}; /* Cluster size boundary for FAT32 volume (128Ks unit) */
#endif
BYTE fmt, sys, *buf, *pte, part; void *pdrv;
WORD ss; /* Sector size */
DWORD szb_buf, sz_buf, sz_blk, n_clst, pau, sect, nsect, n;
Expand Down Expand Up @@ -5464,7 +5466,7 @@ FRESULT f_mkfs (
}
}
if (au > 128) LEAVE_MKFS(FR_INVALID_PARAMETER); /* Too large au for FAT/FAT32 */
if (opt & FM_FAT32) { /* FAT32 possible? */
if (FF_MKFS_FAT32 && (opt & FM_FAT32)) { /* FAT32 possible? */
if ((opt & FM_ANY) == FM_FAT32 || !(opt & FM_FAT)) { /* FAT32 only or no-FAT? */
fmt = FS_FAT32; break;
}
Expand Down Expand Up @@ -5641,6 +5643,7 @@ FRESULT f_mkfs (
do {
pau = au;
/* Pre-determine number of clusters and FAT sub-type */
#if FF_MKFS_FAT32
if (fmt == FS_FAT32) { /* FAT32 volume */
if (pau == 0) { /* au auto-selection */
n = sz_vol / 0x20000; /* Volume size in unit of 128KS */
Expand All @@ -5651,7 +5654,9 @@ FRESULT f_mkfs (
sz_rsv = 32; /* Number of reserved sectors */
sz_dir = 0; /* No static directory */
if (n_clst <= MAX_FAT16 || n_clst > MAX_FAT32) LEAVE_MKFS(FR_MKFS_ABORTED);
} else { /* FAT volume */
} else
#endif
{ /* FAT volume */
if (pau == 0) { /* au auto-selection */
n = sz_vol / 0x1000; /* Volume size in unit of 4KS */
for (i = 0, pau = 1; cst[i] && cst[i] <= n; i++, pau <<= 1) ; /* Get from table */
Expand Down Expand Up @@ -5681,12 +5686,14 @@ FRESULT f_mkfs (
/* Determine number of clusters and final check of validity of the FAT sub-type */
if (sz_vol < b_data + pau * 16 - b_vol) LEAVE_MKFS(FR_MKFS_ABORTED); /* Too small volume */
n_clst = (sz_vol - sz_rsv - sz_fat * n_fats - sz_dir) / pau;
#if FF_MKFS_FAT32
if (fmt == FS_FAT32) {
if (n_clst <= MAX_FAT16) { /* Too few clusters for FAT32 */
if (au == 0 && (au = pau / 2) != 0) continue; /* Adjust cluster size and retry */
LEAVE_MKFS(FR_MKFS_ABORTED);
}
}
#endif
if (fmt == FS_FAT16) {
if (n_clst > MAX_FAT16) { /* Too many clusters for FAT16 */
if (au == 0 && (pau * 2) <= 64) {
Expand Down Expand Up @@ -5720,7 +5727,11 @@ FRESULT f_mkfs (
buf[BPB_SecPerClus] = (BYTE)pau; /* Cluster size [sector] */
st_word(buf + BPB_RsvdSecCnt, (WORD)sz_rsv); /* Size of reserved area */
buf[BPB_NumFATs] = (BYTE)n_fats; /* Number of FATs */
#if FF_MKFS_FAT32
st_word(buf + BPB_RootEntCnt, (WORD)((fmt == FS_FAT32) ? 0 : n_rootdir)); /* Number of root directory entries */
#else
st_word(buf + BPB_RootEntCnt, (WORD) n_rootdir); /* Number of root directory entries */
#endif
if (sz_vol < 0x10000) {
st_word(buf + BPB_TotSec16, (WORD)sz_vol); /* Volume size in 16-bit LBA */
} else {
Expand All @@ -5730,6 +5741,7 @@ FRESULT f_mkfs (
st_word(buf + BPB_SecPerTrk, 63); /* Number of sectors per track (for int13) */
st_word(buf + BPB_NumHeads, 255); /* Number of heads (for int13) */
st_dword(buf + BPB_HiddSec, b_vol); /* Volume offset in the physical drive [sector] */
#if FF_MKFS_FAT32
if (fmt == FS_FAT32) {
st_dword(buf + BS_VolID32, GET_FATTIME()); /* VSN */
st_dword(buf + BPB_FATSz32, sz_fat); /* FAT size [sector] */
Expand All @@ -5739,7 +5751,9 @@ FRESULT f_mkfs (
buf[BS_DrvNum32] = 0x80; /* Drive number (for int13) */
buf[BS_BootSig32] = 0x29; /* Extended boot signature */
mem_cpy(buf + BS_VolLab32, "NO NAME " "FAT32 ", 19); /* Volume label, FAT signature */
} else {
} else
#endif
{
st_dword(buf + BS_VolID, GET_FATTIME()); /* VSN */
st_word(buf + BPB_FATSz16, (WORD)sz_fat); /* FAT size [sector] */
buf[BS_DrvNum] = 0x80; /* Drive number (for int13) */
Expand All @@ -5750,6 +5764,7 @@ FRESULT f_mkfs (
if (disk_write(pdrv, buf, b_vol, 1) != RES_OK) LEAVE_MKFS(FR_DISK_ERR); /* Write it to the VBR sector */

/* Create FSINFO record if needed */
#if FF_MKFS_FAT32
if (fmt == FS_FAT32) {
disk_write(pdrv, buf, b_vol + 6, 1); /* Write backup VBR (VBR + 6) */
mem_set(buf, 0, ss);
Expand All @@ -5761,16 +5776,20 @@ FRESULT f_mkfs (
disk_write(pdrv, buf, b_vol + 7, 1); /* Write backup FSINFO (VBR + 7) */
disk_write(pdrv, buf, b_vol + 1, 1); /* Write original FSINFO (VBR + 1) */
}
#endif

/* Initialize FAT area */
mem_set(buf, 0, (UINT)szb_buf);
sect = b_fat; /* FAT start sector */
for (i = 0; i < n_fats; i++) { /* Initialize FATs each */
#if FF_MKFS_FAT32
if (fmt == FS_FAT32) {
st_dword(buf + 0, 0xFFFFFFF8); /* Entry 0 */
st_dword(buf + 4, 0xFFFFFFFF); /* Entry 1 */
st_dword(buf + 8, 0x0FFFFFFF); /* Entry 2 (root directory) */
} else {
} else
#endif
{
st_dword(buf + 0, (fmt == FS_FAT12) ? 0xFFFFF8 : 0xFFFFFFF8); /* Entry 0 and 1 */
}
nsect = sz_fat; /* Number of FAT sectors */
Expand All @@ -5783,7 +5802,11 @@ FRESULT f_mkfs (
}

/* Initialize root directory (fill with zero) */
#if FF_MKFS_FAT32
nsect = (fmt == FS_FAT32) ? pau : sz_dir; /* Number of root directory sectors */
#else
nsect = sz_dir; /* Number of root directory sectors */
#endif
do {
n = (nsect > sz_buf) ? sz_buf : nsect;
if (disk_write(pdrv, buf, sect, (UINT)n) != RES_OK) LEAVE_MKFS(FR_DISK_ERR);
Expand All @@ -5795,7 +5818,7 @@ FRESULT f_mkfs (
if (FF_FS_EXFAT && fmt == FS_EXFAT) {
sys = 0x07; /* HPFS/NTFS/exFAT */
} else {
if (fmt == FS_FAT32) {
if (FF_MKFS_FAT32 && fmt == FS_FAT32) {
sys = 0x0C; /* FAT32X */
} else {
if (sz_vol >= 0x10000) {
Expand Down
6 changes: 6 additions & 0 deletions lib/oofatfs/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
#define FF_USE_MKFS 1
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */

#ifdef MICROPY_FF_MKFS_FAT32
#define FF_MKFS_FAT32 MICROPY_FF_MKFS_FAT32
#else
#define FF_MKFS_FAT32 0
#endif
/* This option switches off FAT32 support in f_mkfs() */

#define FF_USE_FASTSEEK 1
/* This option switches fast seek function. (0:Disable or 1:Enable) */
Expand Down
2 changes: 0 additions & 2 deletions ports/atmel-samd/boards/arduino_mkr1300/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ CHIP_FAMILY = samd21
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

CIRCUITPY_RAINBOWIO = 0
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

CIRCUITPY_ONEWIREIO = 0
CIRCUITPY_RAINBOWIO = 0
2 changes: 0 additions & 2 deletions ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,3 @@ CHIP_FAMILY = samd21
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

CIRCUITPY_RAINBOWIO = 0
1 change: 1 addition & 0 deletions ports/unix/variants/coverage/mpconfigvariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#define MICROPY_PY_URE_SUB (1)
#define MICROPY_VFS_POSIX (1)
#define MICROPY_FATFS_USE_LABEL (1)
#define MICROPY_FF_MKFS_FAT32 (1)
#define MICROPY_PY_FRAMEBUF (1)
#define MICROPY_PY_COLLECTIONS_NAMEDTUPLE__ASDICT (1)
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1)
Expand Down
11 changes: 7 additions & 4 deletions ports/unix/variants/coverage/mpconfigvariant.mk
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ SRC_BITMAP := \
$(patsubst ../../%,%,$(wildcard ../../shared-bindings/gifio/*.c ../../shared-module/gifio/*.c)) \
shared/runtime/context_manager_helpers.c \
displayio_min.c \
shared-bindings/bitmaptools/__init__.c \
shared-bindings/displayio/Bitmap.c \
shared-bindings/rainbowio/__init__.c \
shared-bindings/util.c \
shared-module/bitmaptools/__init__.c \
shared-module/displayio/area.c \
shared-module/displayio/Bitmap.c \
shared-module/displayio/ColorConverter.c \
shared-bindings/bitmaptools/__init__.c \
shared-module/bitmaptools/__init__.c \
shared-bindings/util.c \
shared-module/displayio/ColorConverter.c \
shared-module/rainbowio/__init__.c \

$(info $(SRC_BITMAP))
SRC_C += $(SRC_BITMAP)

CFLAGS += -DCIRCUITPY_GIFIO=1 -DCIRCUITPY_DISPLAYIO_UNIX=1 -DCIRCUITPY_BITMAPTOOLS=1
CFLAGS += -DCIRCUITPY_GIFIO=1 -DCIRCUITPY_DISPLAYIO_UNIX=1 -DCIRCUITPY_BITMAPTOOLS=1 -DCIRCUITPY_RAINBOWIO=1

SRC_C += coverage.c
SRC_CXX += coveragecpp.cpp
3 changes: 2 additions & 1 deletion shared-bindings/rainbowio/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
//| ...
//|
STATIC mp_obj_t rainbowio_colorwheel(mp_obj_t n) {
return MP_OBJ_NEW_SMALL_INT(colorwheel(mp_obj_is_small_int(n) ? MP_OBJ_SMALL_INT_VALUE(n) : mp_obj_get_float(n)));
mp_float_t f = mp_obj_get_float(n);
return MP_OBJ_NEW_SMALL_INT(colorwheel(f));
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(rainbowio_colorwheel_obj, rainbowio_colorwheel);

Expand Down
3 changes: 2 additions & 1 deletion shared-bindings/rainbowio/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
#ifndef CP_SHARED_BINDINGS_RAINBOWIO_INIT_H
#define CP_SHARED_BINDINGS_RAINBOWIO_INIT_H
#include <stdint.h>
#include "py/misc.h"

const int32_t colorwheel(float pos);
int32_t colorwheel(mp_float_t pos);

#endif // CP_SHARED_BINDINGS_RAINBOWIO_INIT_H
18 changes: 11 additions & 7 deletions shared-module/rainbowio/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@

#include "shared-bindings/rainbowio/__init__.h"

const int32_t colorwheel(float pos) {
if (pos > 255) {
pos = pos - ((uint32_t)(pos / 256) * 256);
}
int32_t colorwheel(mp_float_t pos) {
pos = pos - ((uint32_t)(pos / 256) * 256);
int shift1, shift2;
if (pos < 85) {
return (uint8_t)(255 - (pos * 3)) << 16 | (uint8_t)(pos * 3) << 8;
shift1 = 8;
shift2 = 16;
} else if (pos < 170) {
pos -= 85;
return (uint8_t)(255 - (pos * 3)) << 8 | (uint8_t)(pos * 3);
shift1 = 0;
shift2 = 8;
} else {
pos -= 170;
return (uint8_t)(pos * 3) << 16 | (uint8_t)(255 - (pos * 3));
shift1 = 16;
shift2 = 0;
}
int p = (int)(pos * 3);
return (p << shift1) | ((255 - p) << shift2);
}
2 changes: 0 additions & 2 deletions tests/circuitpython/blinky.py

This file was deleted.

6 changes: 6 additions & 0 deletions tests/circuitpython/rainbows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import rainbowio

for i in range(0, 256, 15):
print("{:3} {:06x} {:06x}".format(i, rainbowio.colorwheel(i), rainbowio.colorwheel(float(i))))
for i in range(256, 1024, 128):
print("{:3} {:06x}".format(i, rainbowio.colorwheel(i)))
24 changes: 24 additions & 0 deletions tests/circuitpython/rainbows.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
0 ff0000 ff0000
15 d22d00 d22d00
30 a55a00 a55a00
45 788700 788700
60 4bb400 4bb400
75 1ee100 1ee100
90 00f00f 00f00f
105 00c33c 00c33c
120 009669 009669
135 006996 006996
150 003cc3 003cc3
165 000ff0 000ff0
180 1e00e1 1e00e1
195 4b00b4 4b00b4
210 780087 780087
225 a5005a a5005a
240 d2002d d2002d
255 ff0000 ff0000
256 ff0000
384 007e81
512 ff0000
640 007e81
768 ff0000
896 007e81
1 change: 1 addition & 0 deletions tests/run-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ def main():
if args.test_dirs is None:
test_dirs = (
"basics",
"circuitpython",
"micropython",
"misc",
"extmod",
Expand Down
10 changes: 5 additions & 5 deletions tests/unix/extra_coverage.py.exp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ binascii bitmaptools btree cexample
cmath collections cppexample displayio
errno ffi framebuf gc
gifio hashlib json math
qrio re sys termios
ubinascii uctypes uerrno uheapq
uio ujson ulab uos
urandom ure uselect ustruct
utime utimeq uzlib
qrio rainbowio re sys
termios ubinascii uctypes uerrno
uheapq uio ujson ulab
uos urandom ure uselect
ustruct utime utimeq uzlib
ime

utime utimeq
Expand Down