Skip to content

Commit

Permalink
WIP: RTT transfer of filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkra committed Sep 25, 2023
1 parent 2fb8642 commit 8b2298d
Show file tree
Hide file tree
Showing 10 changed files with 478 additions and 336 deletions.
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ target_sources(app PRIVATE src/zsw_pressure_sensor.c)

target_sources(app PRIVATE src/zsw_light_sensor.c)
target_sources(app PRIVATE src/zsw_flash.c)
target_sources(app PRIVATE src/zsw_rtt_transfer.c)

if (DFU_BUILD)
target_sources(app PRIVATE src/dfu.c)
Expand Down
15 changes: 3 additions & 12 deletions app/boards/arm/zswatch_nrf5340/zswatch_nrf5340_cpuapp_3.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,9 @@ CONFIG_SPI_NOR_IDLE_IN_DPD=y
CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
CONFIG_MPU_ALLOW_FLASH_WRITE=y

CONFIG_FILE_SYSTEM=y
CONFIG_FILE_SYSTEM_LITTLEFS=y
#CONFIG_FILE_SYSTEM=y
#CONFIG_FILE_SYSTEM_LITTLEFS=y
CONFIG_LOG=n

CONFIG_LV_Z_USE_FILESYSTEM=y
CONFIG_MCUMGR_GRP_FS=y
CONFIG_MCUMGR=y
CONFIG_FILE_SYSTEM_SHELL=y
CONFIG_SHELL_BACKEND_SERIAL=n
CONFIG_SHELL_BACKEND_RTT=y
CONFIG_SHELL=y
#CONFIG_LV_Z_USE_FILESYSTEM=y
CONFIG_USE_SEGGER_RTT=y
CONFIG_SHELL_BACKEND_RTT_BUFFER=0
CONFIG_LOG_BACKEND_RTT_BUFFER=1
CONFIG_ZCBOR=y
4 changes: 2 additions & 2 deletions app/prj.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CONFIG_LV_Z_MEM_POOL_NUMBER_BLOCKS=8
CONFIG_MAIN_STACK_SIZE=1024
CONFIG_MAIN_STACK_SIZE=2048
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=25000
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
Expand Down Expand Up @@ -184,7 +184,7 @@ CONFIG_DEMO_BUILD=n

# Choose one or many of below
CONFIG_WATCHFACE_ANALOG=n
CONFIG_WATCHFACE_DIGITAL=n
CONFIG_WATCHFACE_DIGITAL=y
CONFIG_WATCHFACE_MINIMAL=y

# Choose one of below
Expand Down
196 changes: 196 additions & 0 deletions app/scripts/rtt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2017 Square, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
# Example RTT terminal.
#
# This module creates an interactive terminal with the target using RTT.
#
# Usage: rtt target_device
# Author: Charles Nicholson
# Date: October 11, 2017
# Copyright: 2017 Square, Inc.

import pylink
import argparse
import sys
import time
import os
from six.moves import input

try:
import thread
except ImportError:
import _thread as thread


def read_rtt(jlink):
"""Reads the JLink RTT buffer #0 at 10Hz and prints to stdout.
This method is a polling loop against the connected JLink unit. If
the JLink is disconnected, it will exit. Additionally, if any exceptions
are raised, they will be caught and re-raised after interrupting the
main thread.
sys.stdout.write and sys.stdout.flush are used since target terminals
are expected to transmit newlines, which may or may not line up with the
arbitrarily-chosen 1024-byte buffer that this loop uses to read.
Args:
jlink (pylink.JLink): The JLink to read.
Raises:
Exception on error.
"""
try:
while jlink.connected():
terminal_bytes = jlink.rtt_read(0, 1024)
if terminal_bytes:
sys.stdout.write("".join(map(chr, terminal_bytes)))
sys.stdout.flush()
time.sleep(0.1)
except Exception:
print("IO read thread exception, exiting...")
thread.interrupt_main()
raise


def write_rtt(jlink):
"""Writes kayboard input to JLink RTT buffer #0.
This method is a loop that blocks waiting on stdin. When enter is pressed,
LF and NUL bytes are added to the input and transmitted as a byte list.
If the JLink is disconnected, it will exit gracefully. If any other
exceptions are raised, they will be caught and re-raised after interrupting
the main thread.
Args:
jlink (pylink.JLink): The JLink to write to.
Raises:
Exception on error.
"""
try:
buffer_size = 4096
counter = 0
with open('lvgl_resources.bin', mode='rb') as f:
num_sent = 0
file_size = os.fstat(f.fileno()).st_size
print('Filesize:', num_sent)
chunk = f.read(buffer_size)
while chunk and jlink.connected():
time.sleep(1)
bytes_written = 0
while bytes_written < len(chunk):
to_send = chunk[bytes_written:]
print("Sening", len(to_send))
sent = jlink.rtt_write(2, chunk[bytes_written:])
print('Wanted', len(chunk) - bytes_written, 'Sent', sent)
bytes_written = bytes_written + sent
time.sleep(1)

num_sent = num_sent + len(chunk)
print(counter, num_sent, '/', file_size, 'len:', len(chunk))
chunk = f.read(buffer_size)
counter = counter + 1
#bytes = list(bytearray(input(), "utf-8") + b"\x0A\x00")
#print("Bytes:", bytes)
#break
print('File send done')
time.sleep(2)
bytes = list(b"\x0A\x00")
bytes_written = jlink.rtt_write(2, bytes)
print("Bytes:", bytes, 'Sent:', bytes_written)

except Exception:
print("IO write thread exception, exiting...")
thread.interrupt_main()
raise


def main(target_device, block_address=None):
"""Creates an interactive terminal to the target via RTT.
The main loop opens a connection to the JLink, and then connects
to the target device. RTT is started, the number of buffers is presented,
and then two worker threads are spawned: one for read, and one for write.
The main loops sleeps until the JLink is either disconnected or the
user hits ctrl-c.
Args:
target_device (string): The target CPU to connect to.
block_address (int): optional address pointing to start of RTT block.
Returns:
Always returns ``0`` or a JLinkException.
Raises:
JLinkException on error.
"""
jlink = pylink.JLink()
print("connecting to JLink...")
jlink.open()
print("connecting to %s..." % target_device)
jlink.set_tif(pylink.enums.JLinkInterfaces.SWD)
jlink.connect(target_device)
print("connected, starting RTT...")
jlink.rtt_start(block_address)

while True:
try:
num_up = jlink.rtt_get_num_up_buffers()
num_down = jlink.rtt_get_num_down_buffers()
print("RTT started, %d up bufs, %d down bufs." % (num_up, num_down))
break
except pylink.errors.JLinkRTTException:
time.sleep(0.1)

print("up channels:")
for buf_index in range(jlink.rtt_get_num_up_buffers()):
buf = jlink.rtt_get_buf_descriptor(buf_index, True)
print(" %d: name = %r, size = %d bytes, flags = %d" % (buf.BufferIndex, buf.name,
buf.SizeOfBuffer, buf.Flags))

print("down channels:")
for buf_index in range(jlink.rtt_get_num_down_buffers()):
buf = jlink.rtt_get_buf_descriptor(buf_index, False)
print(" %d: name = %r, size = %d bytes, flags = %d" % (buf.BufferIndex, buf.name,
buf.SizeOfBuffer, buf.Flags))

try:
thread.start_new_thread(read_rtt, (jlink,))
thread.start_new_thread(write_rtt, (jlink,))
while jlink.connected():
time.sleep(1)
print("JLink disconnected, exiting...")
except KeyboardInterrupt:
print("ctrl-c detected, exiting...")
pass


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Open RTT console.")
parser.add_argument(
"target_cpu",
help="Device Name (see https://www.segger.com/supported-devices/jlink/)")
parser.add_argument(
"rtt_block_address", help="RTT block address in hex",
type=lambda x: int(x, 16), nargs="?")

args = parser.parse_args()

sys.exit(main(args.target_cpu, args.rtt_block_address))
72 changes: 69 additions & 3 deletions app/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
#include <zephyr/logging/log_ctrl.h>
#include <zephyr/fatal.h>
#include "dfu.h"
#include <zsw_rtt_transfer.h>

LOG_MODULE_REGISTER(main, LOG_LEVEL_WRN);
LOG_MODULE_REGISTER(main, LOG_LEVEL_DBG);

#define TASK_WDT_FEED_INTERVAL_MS 3000

Expand Down Expand Up @@ -95,9 +96,8 @@ ZBUS_LISTENER_DEFINE(main_ble_comm_lis, zbus_ble_comm_data_callback);
static void run_init_work(struct k_work *item)
{
lv_indev_t *touch_indev;
k_msleep(2000);
zsw_flash_test();

//zsw_flash_test();
load_retention_ram();
notification_manager_init();
enable_bluetoth();
Expand Down Expand Up @@ -145,6 +145,8 @@ void run_wdt_work(struct k_work *item)
k_work_schedule(&wdt_work, K_MSEC(TASK_WDT_FEED_INTERVAL_MS));
}

static uint8_t buf[4096];
extern int asdf(void);
int main(void)
{
#if defined(CONFIG_TASK_WDT) && !defined(CONFIG_BOARD_NATIVE_POSIX)
Expand All @@ -165,6 +167,70 @@ int main(void)
// this RAM forever, instead re-use the system workqueue for init
// it has the required amount of stack.
k_work_submit(&init_work);
int counter = 0;
int buffer_index = 0;
int bytes_flashed = 0;

while (1) {
int len_to_read = sizeof(buf) - buffer_index;
int len = zsw_rtt_transfer_read(&buf[buffer_index], len_to_read);
if (len != 0) {
LOG_DBG("Want: %d, Got: %d", len_to_read, len);
}

if (len == 0) {

} else if (len == 2) {
LOG_WRN("RTT Transfer done: %d bytes flashed", bytes_flashed);
break;
} else if (len == len_to_read) {
asdf();
bytes_flashed += sizeof(buf);
LOG_DBG("Write: %d, %d", buffer_index, bytes_flashed);
//asdf();
//k_msleep(100);
//int ret = zsw_flash_write_sector(counter, buf, sizeof(buf));
//if (ret != 0) {
// TODO?
//}
//LOG_DBG("zsw_flash_write_sector: %d", ret);

counter++;
buffer_index = 0;
if (counter % 10 == 0) {
LOG_WRN("RTT: Received %d (%d)", bytes_flashed, counter);
}
} else {
buffer_index += len;
if (buffer_index > sizeof(buf)) {
__ASSERT(false, "Something wrong got size %d", buffer_index);
}
LOG_DBG("Fill: %d", buffer_index);
}
k_msleep(100);
/*
if (len > 0) {
if (len == 2) {
LOG_WRN("RTT Transfer done: %d bytes received", total_bytes);
break;
}
total_bytes += len;
counter++;
int ret = zsw_flash_write_sector(counter, buf, len);
if (ret != 0) {
// ?
}
if (counter % 10 == 0) {
LOG_WRN("RTT: Received %d (%d)", total_bytes, counter);
}
k_msleep(50);
} else {
k_msleep(50);
//zsw_rtt_transfer_write(buf, len - 1);
}
*/
}

return 0;
}
Expand Down
Loading

0 comments on commit 8b2298d

Please sign in to comment.