From 12c95ee9e96e9c5fd3609a6f095cd80fc2a849f3 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 7 Nov 2021 17:51:29 -0600 Subject: [PATCH 1/9] oofatfs: Allow fat32 mkfs to be compiled out Saves 508 bytes --- lib/oofatfs/ff.c | 33 ++++++++++++++++--- lib/oofatfs/ffconf.h | 6 ++++ .../unix/variants/coverage/mpconfigvariant.h | 1 + 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/oofatfs/ff.c b/lib/oofatfs/ff.c index 68c662c7a3..b9b347a7d1 100644 --- a/lib/oofatfs/ff.c +++ b/lib/oofatfs/ff.c @@ -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; @@ -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; } @@ -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 */ @@ -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 */ @@ -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) { @@ -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 { @@ -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] */ @@ -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) */ @@ -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); @@ -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 */ @@ -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); @@ -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) { diff --git a/lib/oofatfs/ffconf.h b/lib/oofatfs/ffconf.h index a4a55a0fa2..248987dda3 100644 --- a/lib/oofatfs/ffconf.h +++ b/lib/oofatfs/ffconf.h @@ -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) */ diff --git a/ports/unix/variants/coverage/mpconfigvariant.h b/ports/unix/variants/coverage/mpconfigvariant.h index 7902f461bb..2ad0d94bc0 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.h +++ b/ports/unix/variants/coverage/mpconfigvariant.h @@ -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) From dfef5014eda2f429a79e11ab1bd8b097cb023ffa Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 12 Nov 2021 16:45:51 -0600 Subject: [PATCH 2/9] enable rainbowio with newly free space --- ports/atmel-samd/boards/arduino_mkr1300/mpconfigboard.mk | 2 -- ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk | 1 - ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk | 2 -- 3 files changed, 5 deletions(-) diff --git a/ports/atmel-samd/boards/arduino_mkr1300/mpconfigboard.mk b/ports/atmel-samd/boards/arduino_mkr1300/mpconfigboard.mk index abbdbe87ef..c3d75202bf 100644 --- a/ports/atmel-samd/boards/arduino_mkr1300/mpconfigboard.mk +++ b/ports/atmel-samd/boards/arduino_mkr1300/mpconfigboard.mk @@ -9,5 +9,3 @@ CHIP_FAMILY = samd21 INTERNAL_FLASH_FILESYSTEM = 1 LONGINT_IMPL = NONE CIRCUITPY_FULL_BUILD = 0 - -CIRCUITPY_RAINBOWIO = 0 diff --git a/ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk b/ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk index c940bc02ef..7fe396d698 100644 --- a/ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk +++ b/ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk @@ -11,4 +11,3 @@ LONGINT_IMPL = NONE CIRCUITPY_FULL_BUILD = 0 CIRCUITPY_ONEWIREIO = 0 -CIRCUITPY_RAINBOWIO = 0 diff --git a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk index 58fd9ca6e4..5ee22c59ad 100644 --- a/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk +++ b/ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk @@ -9,5 +9,3 @@ CHIP_FAMILY = samd21 INTERNAL_FLASH_FILESYSTEM = 1 LONGINT_IMPL = NONE CIRCUITPY_FULL_BUILD = 0 - -CIRCUITPY_RAINBOWIO = 0 From c91901dc37f866fd909812f1f6c4d0e12d1395a3 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 12 Nov 2021 17:06:12 -0600 Subject: [PATCH 3/9] rainbowio: Fix some things the unix build errors about --- shared-bindings/rainbowio/__init__.h | 3 ++- shared-module/rainbowio/__init__.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/shared-bindings/rainbowio/__init__.h b/shared-bindings/rainbowio/__init__.h index 216687518c..3599e3ab3e 100644 --- a/shared-bindings/rainbowio/__init__.h +++ b/shared-bindings/rainbowio/__init__.h @@ -27,7 +27,8 @@ #ifndef CP_SHARED_BINDINGS_RAINBOWIO_INIT_H #define CP_SHARED_BINDINGS_RAINBOWIO_INIT_H #include +#include "py/misc.h" -const int32_t colorwheel(float pos); +int32_t colorwheel(mp_float_t pos); #endif // CP_SHARED_BINDINGS_RAINBOWIO_INIT_H diff --git a/shared-module/rainbowio/__init__.c b/shared-module/rainbowio/__init__.c index ded0c313f7..88ae580dc4 100644 --- a/shared-module/rainbowio/__init__.c +++ b/shared-module/rainbowio/__init__.c @@ -26,7 +26,7 @@ #include "shared-bindings/rainbowio/__init__.h" -const int32_t colorwheel(float pos) { +int32_t colorwheel(mp_float_t pos) { if (pos > 255) { pos = pos - ((uint32_t)(pos / 256) * 256); } From 1c73e2e466dacf60a7ef45cca15bc2b158735431 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 12 Nov 2021 17:06:20 -0600 Subject: [PATCH 4/9] tests: Remove a non-useful test --- tests/circuitpython/blinky.py | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 tests/circuitpython/blinky.py diff --git a/tests/circuitpython/blinky.py b/tests/circuitpython/blinky.py deleted file mode 100644 index a9412b07c6..0000000000 --- a/tests/circuitpython/blinky.py +++ /dev/null @@ -1,2 +0,0 @@ -import digitalio -import board From 010a822b8c54daf004ce8f077d68b94ae10f2c3e Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 12 Nov 2021 17:06:44 -0600 Subject: [PATCH 5/9] tests: run circuitpython tests during `make test` .. there are none that aren't skipped now, but it'll change --- tests/run-tests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/run-tests.py b/tests/run-tests.py index 4315b5eaaf..b795d6ec94 100755 --- a/tests/run-tests.py +++ b/tests/run-tests.py @@ -819,6 +819,7 @@ the last matching regex is used: if args.test_dirs is None: test_dirs = ( "basics", + "circuitpython", "micropython", "misc", "extmod", From 1cea871c709cc87c79d7a88b56e55837ee8de56b Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 12 Nov 2021 17:07:00 -0600 Subject: [PATCH 6/9] enable rainbowio on unix coverage port --- ports/unix/variants/coverage/mpconfigvariant.mk | 11 +++++++---- tests/unix/extra_coverage.py.exp | 10 +++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/ports/unix/variants/coverage/mpconfigvariant.mk b/ports/unix/variants/coverage/mpconfigvariant.mk index 69996b2839..563b5ad860 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.mk +++ b/ports/unix/variants/coverage/mpconfigvariant.mk @@ -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 diff --git a/tests/unix/extra_coverage.py.exp b/tests/unix/extra_coverage.py.exp index 4be6b20d5d..278c6601a3 100644 --- a/tests/unix/extra_coverage.py.exp +++ b/tests/unix/extra_coverage.py.exp @@ -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 From d4dd06904a82481e59b895a1a81155a0a0d2786f Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 12 Nov 2021 17:11:32 -0600 Subject: [PATCH 7/9] add a test of rainbows --- tests/circuitpython/rainbows.py | 6 ++++++ tests/circuitpython/rainbows.py.exp | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/circuitpython/rainbows.py create mode 100644 tests/circuitpython/rainbows.py.exp diff --git a/tests/circuitpython/rainbows.py b/tests/circuitpython/rainbows.py new file mode 100644 index 0000000000..664d15de35 --- /dev/null +++ b/tests/circuitpython/rainbows.py @@ -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))) diff --git a/tests/circuitpython/rainbows.py.exp b/tests/circuitpython/rainbows.py.exp new file mode 100644 index 0000000000..fce94a523a --- /dev/null +++ b/tests/circuitpython/rainbows.py.exp @@ -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 From 9646d3f9631da007927a782e39dacd50e84684a4 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 12 Nov 2021 17:28:57 -0600 Subject: [PATCH 8/9] rainbowio: No need for the ternary here .. mp_obj_get_float handles the case where the arg is an int already. This saves 12 bytes of code size on trinket_m0. --- shared-bindings/rainbowio/__init__.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shared-bindings/rainbowio/__init__.c b/shared-bindings/rainbowio/__init__.c index 12a33b7d0c..0dfdcd3a45 100644 --- a/shared-bindings/rainbowio/__init__.c +++ b/shared-bindings/rainbowio/__init__.c @@ -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); From edac675c4853016eddf09627edf9d85abb2a038a Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 12 Nov 2021 17:29:17 -0600 Subject: [PATCH 9/9] rainbowio: optimize for code size .. this saves 76 bytes on trinket_m0 --- shared-module/rainbowio/__init__.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/shared-module/rainbowio/__init__.c b/shared-module/rainbowio/__init__.c index 88ae580dc4..a6a2e4974e 100644 --- a/shared-module/rainbowio/__init__.c +++ b/shared-module/rainbowio/__init__.c @@ -27,16 +27,20 @@ #include "shared-bindings/rainbowio/__init__.h" int32_t colorwheel(mp_float_t pos) { - if (pos > 255) { - pos = pos - ((uint32_t)(pos / 256) * 256); - } + 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); }