Skip to content

Commit

Permalink
Support both LVGL resorces in littlefs and in custom raw fs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkra committed Oct 8, 2023
1 parent 8a9a1a3 commit 01236a1
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 95 deletions.
14 changes: 9 additions & 5 deletions app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_3.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,10 @@
/ {
fstab {
compatible = "zephyr,fstab";
lvgl_fs: lvgl_fs {
lvgl_lfs: lvgl_lfs {
compatible = "zephyr,fstab,littlefs";
mount-point = "/lvgl_fs";
partition = <&lvgl_fs_partition>;
mount-point = "/lvgl_lfs";
partition = <&lvgl_lfs_partition>;
automount;
read-size = <1024>;
prog-size = <512>;
Expand All @@ -152,9 +152,13 @@
#address-cells = <1>;
#size-cells = <1>;

lvgl_fs_partition: partition@0 {
label = "storagelvgl";
lvgl_lfs_partition: partition@0 {
label = "lvgl_lfs_partition";
reg = <0x00000000 0x00200000>;
};
lvgl_raw_partition: partition@200000 {
label = "lvgl_raw_partition";
reg = <0x00200000 0x00200000>;
};
};
};
1 change: 1 addition & 0 deletions app/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP_LABELS=y

# Filesystem
#CONFIG_FILE_SYSTEM=y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
len:uint32
...
'''
# TODO add total length ijn the beginning?

def create_fs_image(img_filename, source_dir, block_size=4096):
def create_custom_raw_fs_image(img_filename, source_dir, block_size=4096):
table = {}
offset = 0
files_image = bytearray()
Expand Down Expand Up @@ -67,4 +66,4 @@ def create_fs_image(img_filename, source_dir, block_size=4096):
block_size = args.block_size
source_dir = args.source

create_fs_image(img_filename, source_dir, block_size)
create_custom_raw_fs_image(img_filename, source_dir, block_size)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import argparse
from littlefs import LittleFS

def create_fs_image(img_filename, img_size, block_size, read_size, prog_size, name_max, file_max, attr_max, source_dir, disk_version):
def create_littlefs_fs_image(img_filename, img_size, block_size, read_size, prog_size, name_max, file_max, attr_max, source_dir, disk_version):
block_count = img_size // block_size
if block_count * block_size != img_size:
print("image size should be a multiple of block size")
Expand Down Expand Up @@ -76,4 +76,4 @@ def create_fs_image(img_filename, img_size, block_size, read_size, prog_size, na
attr_max = args.attr_max
source_dir = args.source

create_fs_image(img_filename, img_size, block_size, read_size, prog_size, name_max, file_max, attr_max, source_dir, args.disk_version)
create_littlefs_fs_image(img_filename, img_size, block_size, read_size, prog_size, name_max, file_max, attr_max, source_dir, args.disk_version)
21 changes: 12 additions & 9 deletions app/scripts/rtt_flash_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ def read_rtt(jlink):
raise


def dump_flash(jlink, file):
def dump_flash(jlink, file, partition):
print(jlink, file)
with open(file, "wb") as file:
try:
bytes = list(bytearray("DUMP_START", "utf-8"))
bytes = list(bytearray(f"DUMP_START:{partition}", "utf-8"))
print("Start:", bytes)
jlink.rtt_write(2, bytes)
time.sleep(2)

Expand Down Expand Up @@ -78,12 +79,12 @@ def dump_flash(jlink, file):
raise


def load_data(jlink, file):
def load_data(jlink, file, partition):
try:
buffer_size = 4096 * 2
counter = 0
print("FILENAME", file)
bytes = list(bytearray("LOADER_START", "utf-8"))
bytes = list(bytearray(f"LOADER_START:{partition}", "utf-8"))
jlink.rtt_write(2, bytes)
time.sleep(2)

Expand Down Expand Up @@ -129,7 +130,7 @@ def load_data(jlink, file):
raise


def run_loader(target_device, file, read_data_only=False):
def run_loader(target_device, file, partition, read_data_only=False):
"""Creates connection to target via RTT and either writes a file or reads from flash.
Args:
Expand Down Expand Up @@ -191,9 +192,9 @@ def run_loader(target_device, file, read_data_only=False):

work_thread = None
if read_data_only:
work_thread = Thread(target=dump_flash, args=(jlink, file))
work_thread = Thread(target=dump_flash, args=(jlink, file, partition))
else:
work_thread = Thread(target=load_data, args=(jlink, file))
work_thread = Thread(target=load_data, args=(jlink, file, partition))
work_thread.daemon = True
work_thread.start()
work_thread.join()
Expand All @@ -218,14 +219,16 @@ def run_loader(target_device, file, read_data_only=False):
"--file",
help="Binary file to send to target or in case of read the filename to store the data in.",
)
parser.add_argument(
"-p", "--partition", type=int, help="Label of partition in DTS to write to.", default="lvgl_raw_partition"
)
parser.add_argument(
"--read_data", help="Read data from flash block", action="store_true"
)

parser.add_argument(
"-s", "--serial", type=int, help="Serial number of ZSWatch attached debugger"
)

args = parser.parse_args()

sys.exit(run_loader(args.target_cpu, args.file, args.read_data))
sys.exit(run_loader(args.target_cpu, args.file, partition, args.read_data))
31 changes: 23 additions & 8 deletions app/scripts/upload_fs_west_command.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from west.commands import WestCommand # your extension must subclass this
from west import log # use this for user output
#from create_resource_fs import create_fs_image
from west.commands import WestCommand
from west import log
from create_custom_resource_image import create_custom_raw_fs_image
from rtt_flash_loader import run_loader
from create_lvgl_image import create_fs_image
from create_littlefs_resouce_image import create_littlefs_fs_image
import sys
import os
from pathlib import Path

class UploadFsWestCommand(WestCommand):

Expand All @@ -19,7 +21,11 @@ def do_add_parser(self, parser_adder):
help=self.help,
description=self.description)

parser.add_argument('--type', type=str, default='raw', help='raw or fs. fs to load littlefs image, raw to load custom binary')
parser.add_argument('--read_file', type=str, help='If set dump flash to this filename')
parser.add_argument(
"-p", "--partition", type=str, help="Label of partition in DTS to write to. Leave blank to use auto guess name.",
)
return parser

def do_run(self, args, unknown_args):
Expand All @@ -32,12 +38,21 @@ def do_run(self, args, unknown_args):
name_max = 255
file_max = 0
attr_max = 0
source_dir = "../src/images/binaries/"
disk_version = "2.0"
filename = "lvgl_resources"
partition = args.partition
zephyr_base = Path(os.environ.get("ZEPHYR_BASE"))
images_path = f'{zephyr_base.parent.absolute()}/app/src/images/binaries'
print(images_path)
if (args.read_file):
filename = args.read_file
if args.read_file is None:
create_fs_image(img_filename, source_dir, block_size)
#create_fs_image(img_filename, img_size, block_size, read_size, prog_size, name_max, file_max, attr_max, source_dir, disk_version)
sys.exit(run_loader("nRF5340_XXAA", filename, args.read_file))
if args.type == 'raw':
source_dir = f"{images_path}/S"
partition = partition if partition else "lvgl_raw_partition"
create_custom_raw_fs_image(img_filename, source_dir, block_size)
elif args.type == 'lfs':
source_dir = f"{images_path}/lvgl_lfs"
partition = partition if partition else "lvgl_lfs_partition"
create_littlefs_fs_image(img_filename, img_size, block_size, read_size, prog_size, name_max, file_max, attr_max, source_dir, disk_version)
sys.exit(run_loader("nRF5340_XXAA", filename, partition, args.read_file))
2 changes: 1 addition & 1 deletion app/src/applications/qr_code/qr_code_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void qr_code_ui_show(lv_obj_t *root)

lv_obj_t *img = lv_img_create(root_page);
#ifdef CONFIG_LV_Z_USE_FILESYSTEM
lv_img_set_src(img, "S:x_ray.bin");
lv_img_set_src(img, "/lvgl_lfs/qr_code.bin");
#else
LV_IMG_DECLARE(round_qr_full);
lv_img_set_src(img, &round_qr_full);
Expand Down
7 changes: 5 additions & 2 deletions app/src/applications/x_ray/x_ray_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ void x_ray_ui_show(lv_obj_t *root)
// Does not loog very good on the round display.
lv_obj_set_scrollbar_mode(root_page, LV_SCROLLBAR_MODE_OFF);

LV_IMG_DECLARE(x_ray);

lv_obj_t *img = lv_img_create(root_page);
#ifdef CONFIG_LV_Z_USE_FILESYSTEM
lv_img_set_src(img, "S:x_ray.bin");
#else
LV_IMG_DECLARE(x_ray);
lv_img_set_src(img, &x_ray);
#endif
lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
lv_obj_set_size(img, 240, 240);
}
Expand Down
13 changes: 13 additions & 0 deletions app/src/images/binaries/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# External SPI Flash resource storage

## Folder options

- `filename.bin` put into `lvgl_lfs` goes into littlefs filesystem into one partition of external flash.
- Usage: `lv_img_set_src(img, "S:filename.bin");`
- Upload: `west upload_fs --type fs`
- `filename.bin` put into `S` goes into a basic readonly filesystem into one other partition of external flash.
- Usage: `lv_img_set_src(img, "/lvgl_lfs/filename.bin");`
- Upload: `west upload_fs --type fs`

## Which one to use?
For now those options are mostly for experimentation. Using littlefs may be faster due to littlefs caching. However the other custom filesystem allows us to do more optimization for ZSWatch in the future.
2 changes: 1 addition & 1 deletion app/src/zsw_lvgl_spi_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#define SPI_FLASH_SECTOR_SIZE 4096

#define FLASH_PARTITION_NAME lvgl_fs_partition
#define FLASH_PARTITION_NAME lvgl_raw_partition

#define FLASH_PARTITION_ID FIXED_PARTITION_ID(FLASH_PARTITION_NAME)
#define FLASH_PARTITION_DEVICE FIXED_PARTITION_DEVICE(FLASH_PARTITION_NAME)
Expand Down
Loading

0 comments on commit 01236a1

Please sign in to comment.