From 252aacdddf1aae89e3ebe91ca72a3ad666728e37 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 27 Jun 2018 11:17:13 -0700 Subject: [PATCH 01/15] Analysis fixes and long lived tweaks. --- .gitignore | 1 + py/gc.c | 0 py/gc_long_lived.c | 2 +- tools/analyze_heap_dump.py | 69 +++++++++++++++++++++------------- tools/output_gc_until_repl.txt | 2 +- 5 files changed, 45 insertions(+), 29 deletions(-) mode change 100644 => 100755 py/gc.c mode change 100644 => 100755 py/gc_long_lived.c mode change 100644 => 100755 tools/analyze_heap_dump.py diff --git a/.gitignore b/.gitignore index 09ffb164d1..aa4ff6dab2 100644 --- a/.gitignore +++ b/.gitignore @@ -58,3 +58,4 @@ TAGS ################# *.orig +*.DS_Store diff --git a/py/gc.c b/py/gc.c old mode 100644 new mode 100755 diff --git a/py/gc_long_lived.c b/py/gc_long_lived.c old mode 100644 new mode 100755 index d34fde5d9b..e30b389faf --- a/py/gc_long_lived.c +++ b/py/gc_long_lived.c @@ -121,7 +121,7 @@ mp_obj_t make_obj_long_lived(mp_obj_t obj, uint8_t max_depth){ } else if (MP_OBJ_IS_TYPE(obj, &mp_type_property)) { mp_obj_property_t *prop = MP_OBJ_TO_PTR(obj); return MP_OBJ_FROM_PTR(make_property_long_lived(prop, max_depth)); - } else if (MP_OBJ_IS_TYPE(obj, &mp_type_str)) { + } else if (MP_OBJ_IS_TYPE(obj, &mp_type_str) || MP_OBJ_IS_TYPE(obj, &mp_type_bytes)) { mp_obj_str_t *str = MP_OBJ_TO_PTR(obj); return MP_OBJ_FROM_PTR(make_str_long_lived(str)); } else if (MP_OBJ_IS_TYPE(obj, &mp_type_type)) { diff --git a/tools/analyze_heap_dump.py b/tools/analyze_heap_dump.py old mode 100644 new mode 100755 index 55342ac693..fa3efbe207 --- a/tools/analyze_heap_dump.py +++ b/tools/analyze_heap_dump.py @@ -135,8 +135,11 @@ def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_cont ram_start = symbols["_srelocate"][0] ram_end = symbols["_estack"][0] ram_length = ram_end - ram_start + # print(ram_length, "ram length") + # print(len(ram_dump) // ram_length, "snapshots") if analyze_snapshots == "all": - snapshots = range(len(ram_dump) // ram_length - 1, -1, -1) + #snapshots = range(len(ram_dump) // ram_length - 1, -1, -1) + snapshots = range(4576, -1, -1) elif analyze_snapshots == "last": snapshots = range(len(ram_dump) // ram_length - 1, len(ram_dump) // ram_length - 2, -1) for snapshot_num in snapshots: @@ -167,16 +170,16 @@ def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_cont mp_state_ctx = symbols["mp_state_ctx"][0] manual_symbol_map["mp_state_ctx+20"] = "mp_state_ctx.vm.last_pool" last_pool = load_pointer(mp_state_ctx + 20) # (gdb) p &mp_state_ctx.vm.last_pool - manual_symbol_map["mp_state_ctx+88"] = "mp_state_ctx.vm.dict_main.map.table" - dict_main_table = load_pointer(mp_state_ctx + 88) # (gdb) p &mp_state_ctx.vm.dict_main.map.table - manual_symbol_map["mp_state_ctx+68"] = "mp_state_ctx.vm.mp_loaded_modules_dict.map.table" - imports_table = load_pointer(mp_state_ctx + 68) # (gdb) p &mp_state_ctx.vm.mp_loaded_modules_dict.map.table + manual_symbol_map["mp_state_ctx+104"] = "mp_state_ctx.vm.dict_main.map.table" + dict_main_table = load_pointer(mp_state_ctx + 104) # (gdb) p &mp_state_ctx.vm.dict_main.map.table + manual_symbol_map["mp_state_ctx+84"] = "mp_state_ctx.vm.mp_loaded_modules_dict.map.table" + imports_table = load_pointer(mp_state_ctx + 84) # (gdb) p &mp_state_ctx.vm.mp_loaded_modules_dict.map.table - manual_symbol_map["mp_state_ctx+104"] = "mp_state_ctx.vm.mp_sys_path_obj.items" - manual_symbol_map["mp_state_ctx+120"] = "mp_state_ctx.vm.mp_sys_argv_obj.items" + manual_symbol_map["mp_state_ctx+120"] = "mp_state_ctx.vm.mp_sys_path_obj.items" + manual_symbol_map["mp_state_ctx+136"] = "mp_state_ctx.vm.mp_sys_argv_obj.items" for i in range(READLINE_HIST_SIZE): - manual_symbol_map["mp_state_ctx+{}".format(128 + i * 4)] = "mp_state_ctx.vm.readline_hist[{}]".format(i) + manual_symbol_map["mp_state_ctx+{}".format(144 + i * 4)] = "mp_state_ctx.vm.readline_hist[{}]".format(i) tuple_type = symbols["mp_type_tuple"][0] type_type = symbols["mp_type_type"][0] @@ -214,8 +217,8 @@ def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_cont pool_start = heap_start + total_byte_len - pool_length - pool_shift pool = heap[-pool_length-pool_shift:] - total_height = 65 * 18 - total_width = (pool_length // (64 * 16)) * 90 + total_height = 128 * 18 + total_width = (pool_length // (128 * 16)) * 85 map_element_blocks = [dict_main_table, imports_table] string_blocks = [] @@ -255,7 +258,7 @@ def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_cont block_data[address] = data for k in range(len(data) // 4): word = struct.unpack_from(">= 3 + if qstr_index > total_prev_len + alloc: + return "invalid" while pool_ptr != 0: if pool_ptr > ram_start: if pool_ptr in block_data: @@ -492,7 +500,10 @@ def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_cont offset = len(data) continue offset += 2 + qstr_len + 1 - qstrs_in_chunk += " " + data[offset - qstr_len - 1: offset - 1].decode("utf-8") + try: + qstrs_in_chunk += " " + data[offset - qstr_len - 1: offset - 1].decode("utf-8") + except UnicodeDecodeError: + qstrs_in_chunk += " " + "░"*qstr_len printable_qstrs = "" for i in range(len(qstrs_in_chunk)): c = qstrs_in_chunk[i] @@ -515,20 +526,28 @@ def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_cont # First render the graph of objects on the heap. if draw_heap_ownership: ownership_graph.layout(prog="dot") - fn = os.path.join(output_directory, "heap_ownership{:04d}.png".format(snapshot_num)) + fn = os.path.join(output_directory, "heap_ownership{:04d}.svg".format(snapshot_num)) print(fn) ownership_graph.draw(fn) + # Clear edge positioning from ownership graph layout. + if draw_heap_ownership: + for edge in ownership_graph.iteredges(): + del edge.attr["pos"] + else: + for edge in ownership_graph.edges(): + ownership_graph.delete_edge(edge) + # Second, render the heap layout in memory order. - for node in ownership_graph: + for node in ownership_graph.nodes(): try: address = int(node.name) except ValueError: - ownership_graph.remove_node(node) + ownership_graph.remove_node(node.name) continue block = (address - pool_start) // 16 - x = block // 64 - y = 64 - block % 64 + x = block // 128 + y = 128 - block % 128 try: height = float(node.attr["height"]) except: @@ -538,11 +557,6 @@ def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_cont # print(hex(address), block, len(block_data[address]), x, y, height) node.attr["pos"] = "{},{}".format(x * 80, (y - (height - 0.25) * 2) * 18) # in inches - # Clear edge positioning from ownership graph layout. - if draw_heap_ownership: - for edge in ownership_graph.iteredges(): - del edge.attr["pos"] - # Reformat block nodes so they are the correct size and do not have keys in them. for block in sorted(map_element_blocks): try: @@ -565,9 +579,9 @@ def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_cont else: #print(" {}, {}".format(format(key), format(value))) cells.append((key, "")) - if value in block_data: - edge = ownership_graph.get_edge(block, value) - edge.attr["tailport"] = str(key) + # if value in block_data: + # edge = ownership_graph.get_edge(block, value) + # edge.attr["tailport"] = str(key) rows = "" for i in range(len(cells) // 2): rows += "{}{}".format( @@ -586,6 +600,7 @@ def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_cont if draw_heap_layout: fn = os.path.join(output_directory, "heap_layout{:04d}.png".format(snapshot_num)) print(fn) + #ownership_graph.write(fn+".dot") ownership_graph.draw(fn) if __name__ == "__main__": diff --git a/tools/output_gc_until_repl.txt b/tools/output_gc_until_repl.txt index 0e99626353..81711deeb8 100644 --- a/tools/output_gc_until_repl.txt +++ b/tools/output_gc_until_repl.txt @@ -19,7 +19,7 @@ append binary memory ram.bin &_srelocate &_estack continue end -break main.c:164 +break main.c:179 continue From ec78d3cefdd98b317a01840e172e7453013ada0c Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 28 Jun 2018 10:36:28 -0700 Subject: [PATCH 02/15] Mark pointers in cpu registers as in use. This prevents bugs where gc_collect is called from C code that did a recent allocation. --- main.c | 10 ++++--- ports/atmel-samd/Makefile | 3 ++ ports/atmel-samd/supervisor/samd21_cpu.s | 35 ++++++++++++++++++++++++ ports/atmel-samd/supervisor/samd51_cpu.s | 27 ++++++++++++++++++ supervisor/cpu.h | 34 +++++++++++++++++++++++ 5 files changed, 105 insertions(+), 4 deletions(-) mode change 100644 => 100755 main.c mode change 100644 => 100755 ports/atmel-samd/Makefile create mode 100755 ports/atmel-samd/supervisor/samd21_cpu.s create mode 100755 ports/atmel-samd/supervisor/samd51_cpu.s create mode 100755 supervisor/cpu.h diff --git a/main.c b/main.c old mode 100644 new mode 100755 index cbc0b093c3..91d8200892 --- a/main.c +++ b/main.c @@ -44,6 +44,7 @@ #include "lib/utils/pyexec.h" #include "mpconfigboard.h" +#include "supervisor/cpu.h" #include "supervisor/port.h" #include "supervisor/filesystem.h" // TODO(tannewt): Figure out how to choose language at compile time. @@ -381,16 +382,17 @@ int __attribute__((used)) main(void) { } void gc_collect(void) { - // WARNING: This gc_collect implementation doesn't try to get root - // pointers from CPU registers, and thus may function incorrectly. - void *dummy; gc_collect_start(); + + mp_uint_t regs[10]; + mp_uint_t sp = cpu_get_regs_and_sp(regs); + // This collects root pointers from the VFS mount table. Some of them may // have lost their references in the VM even though they are mounted. gc_collect_root((void**)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t)); // This naively collects all object references from an approximate stack // range. - gc_collect_root(&dummy, ((mp_uint_t)&_estack - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); + gc_collect_root((void**)sp, ((uint32_t)&_estack - sp) / sizeof(uint32_t)); gc_collect_end(); } diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile old mode 100644 new mode 100755 index 731e826776..9a40102700 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -412,6 +412,8 @@ endif SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \ $(addprefix shared-module/, $(SRC_SHARED_MODULE)) +SRC_O = supervisor/$(CHIP_FAMILY)_cpu.o + OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o)) @@ -419,6 +421,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o)) ifeq ($(INTERNAL_LIBM),1) OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o)) endif +OBJ += $(addprefix $(BUILD)/, $(SRC_O)) SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) $(STM_SRC_C) diff --git a/ports/atmel-samd/supervisor/samd21_cpu.s b/ports/atmel-samd/supervisor/samd21_cpu.s new file mode 100755 index 0000000000..741bb21358 --- /dev/null +++ b/ports/atmel-samd/supervisor/samd21_cpu.s @@ -0,0 +1,35 @@ +.syntax unified +.cpu cortex-m0 +.thumb +.text +.align 2 + +@ uint cpu_get_regs_and_sp(r0=uint regs[10]) +.global cpu_get_regs_and_sp +.thumb +.thumb_func +.type cpu_get_regs_and_sp, %function +cpu_get_regs_and_sp: +@ store registers into given array +str r4, [r0, #0] +str r5, [r0, #4] +str r6, [r0, #8] +str r7, [r0, #12] +push {r1} +mov r1, r8 +str r1, [r0, #16] +mov r1, r9 +str r1, [r0, #20] +mov r1, r10 +str r1, [r0, #24] +mov r1, r11 +str r1, [r0, #28] +mov r1, r12 +str r1, [r0, #32] +mov r1, r13 +str r1, [r0, #36] +pop {r1} + +@ return the sp +mov r0, sp +bx lr diff --git a/ports/atmel-samd/supervisor/samd51_cpu.s b/ports/atmel-samd/supervisor/samd51_cpu.s new file mode 100755 index 0000000000..9e6807a5e2 --- /dev/null +++ b/ports/atmel-samd/supervisor/samd51_cpu.s @@ -0,0 +1,27 @@ +.syntax unified +.cpu cortex-m4 +.thumb +.text +.align 2 + +@ uint cpu_get_regs_and_sp(r0=uint regs[10]) +.global cpu_get_regs_and_sp +.thumb +.thumb_func +.type cpu_get_regs_and_sp, %function +cpu_get_regs_and_sp: +@ store registers into given array +str r4, [r0], #4 +str r5, [r0], #4 +str r6, [r0], #4 +str r7, [r0], #4 +str r8, [r0], #4 +str r9, [r0], #4 +str r10, [r0], #4 +str r11, [r0], #4 +str r12, [r0], #4 +str r13, [r0], #4 + +@ return the sp +mov r0, sp +bx lr diff --git a/supervisor/cpu.h b/supervisor/cpu.h new file mode 100755 index 0000000000..c4f81316c7 --- /dev/null +++ b/supervisor/cpu.h @@ -0,0 +1,34 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SUPERVISOR_CPU_H +#define MICROPY_INCLUDED_SUPERVISOR_CPU_H + +// Adds up to 10 pointers from the CPUs registers to regs. This is used to make sure no actively +// used heap memory is freed. Its usually implemented in assembly. +mp_uint_t cpu_get_regs_and_sp(mp_uint_t *regs); + +#endif // MICROPY_INCLUDED_SUPERVISOR_CPU_H From 519287550c184db3c3843f10e27111a006fec4b1 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 28 Jun 2018 10:38:57 -0700 Subject: [PATCH 03/15] Long-live code.py itself. --- lib/utils/pyexec.c | 8 ++++++++ 1 file changed, 8 insertions(+) mode change 100644 => 100755 lib/utils/pyexec.c diff --git a/lib/utils/pyexec.c b/lib/utils/pyexec.c old mode 100644 new mode 100755 index 718c6828cc..929ee5224d --- a/lib/utils/pyexec.c +++ b/lib/utils/pyexec.c @@ -33,6 +33,7 @@ #include "py/runtime.h" #include "py/repl.h" #include "py/gc.h" +#include "py/gc_long_lived.h" #include "py/frozenmod.h" #include "py/mphal.h" #if defined(USE_DEVICE_MODE) @@ -97,6 +98,13 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input #endif } + // If the code was loaded from a file its likely to be running for a while so we'll long + // live it and collect any garbage before running. + if (input_kind == MP_PARSE_FILE_INPUT) { + module_fun = make_obj_long_lived(module_fun, 6); + gc_collect(); + } + // execute code mp_hal_set_interrupt_char(CHAR_CTRL_C); // allow ctrl-C to interrupt us start = mp_hal_ticks_ms(); From 9de611c056e06684dfa9e5fb4c5fd2f4df579046 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 28 Jun 2018 10:39:12 -0700 Subject: [PATCH 04/15] Always collect after an import Imports generate a lot of garbage so cleaning it up immediately reduces the likelihood longer lived data structures don't end up in the middle of the heap. Fixes #856 --- py/builtinimport.c | 5 +++++ 1 file changed, 5 insertions(+) mode change 100644 => 100755 py/builtinimport.c diff --git a/py/builtinimport.c b/py/builtinimport.c old mode 100644 new mode 100755 index 9f7d34dcae..c1437da4c2 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -31,6 +31,7 @@ #include "py/compile.h" #include "py/gc_long_lived.h" +#include "py/gc.h" #include "py/objmodule.h" #include "py/persistentcode.h" #include "py/runtime.h" @@ -468,6 +469,10 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { // (the module that was just loaded) is not a package. This will be caught // on the next iteration because the file will not exist. } + + // Loading a module thrashes the heap significantly so we explicitly clean up + // afterwards. + gc_collect(); } if (outer_module_obj != MP_OBJ_NULL) { qstr s = qstr_from_strn(mod_str + last, i - last); From cced51cbd23aa732f0243fe555b4427ff46267b4 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 28 Jun 2018 10:43:39 -0700 Subject: [PATCH 05/15] Limit qstr pool size to reduce memory waste. --- py/mpconfig.h | 6 ++++++ py/qstr.c | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) mode change 100644 => 100755 py/mpconfig.h mode change 100644 => 100755 py/qstr.c diff --git a/py/mpconfig.h b/py/mpconfig.h old mode 100644 new mode 100755 index 765ae1e8f0..74d7c2c929 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -130,6 +130,12 @@ #define MICROPY_ALLOC_QSTR_CHUNK_INIT (128) #endif +// Max number of entries in newly allocated QSTR pools. Smaller numbers may make QSTR lookups +// slightly slower but reduce the waste of unused spots. +#ifndef MICROPY_QSTR_POOL_MAX_ENTRIES +#define MICROPY_QSTR_POOL_MAX_ENTRIES (64) +#endif + // Initial amount for lexer indentation level #ifndef MICROPY_ALLOC_LEXER_INDENT_INIT #define MICROPY_ALLOC_LEXER_INDENT_INIT (10) diff --git a/py/qstr.c b/py/qstr.c old mode 100644 new mode 100755 index 49b2d9daaa..de4dc6240a --- a/py/qstr.c +++ b/py/qstr.c @@ -144,14 +144,18 @@ STATIC qstr qstr_add(const byte *q_ptr) { // make sure we have room in the pool for a new qstr if (MP_STATE_VM(last_pool)->len >= MP_STATE_VM(last_pool)->alloc) { - qstr_pool_t *pool = m_new_ll_obj_var_maybe(qstr_pool_t, const char*, MP_STATE_VM(last_pool)->alloc * 2); + uint32_t new_pool_length = MP_STATE_VM(last_pool)->alloc * 2; + if (new_pool_length > MICROPY_QSTR_POOL_MAX_ENTRIES) { + new_pool_length = MICROPY_QSTR_POOL_MAX_ENTRIES; + } + qstr_pool_t *pool = m_new_ll_obj_var_maybe(qstr_pool_t, const char*, new_pool_length); if (pool == NULL) { QSTR_EXIT(); - m_malloc_fail(MP_STATE_VM(last_pool)->alloc * 2); + m_malloc_fail(new_pool_length); } pool->prev = MP_STATE_VM(last_pool); pool->total_prev_len = MP_STATE_VM(last_pool)->total_prev_len + MP_STATE_VM(last_pool)->len; - pool->alloc = MP_STATE_VM(last_pool)->alloc * 2; + pool->alloc = new_pool_length; pool->len = 0; MP_STATE_VM(last_pool) = pool; DEBUG_printf("QSTR: allocate new pool of size %d\n", MP_STATE_VM(last_pool)->alloc); From f3f8e13b882f5a4d685ce33831c24efd7077f57c Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 28 Jun 2018 10:44:01 -0700 Subject: [PATCH 06/15] analyze everything --- tools/analyze_heap_dump.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tools/analyze_heap_dump.py b/tools/analyze_heap_dump.py index fa3efbe207..31202c23e8 100755 --- a/tools/analyze_heap_dump.py +++ b/tools/analyze_heap_dump.py @@ -138,8 +138,8 @@ def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_cont # print(ram_length, "ram length") # print(len(ram_dump) // ram_length, "snapshots") if analyze_snapshots == "all": - #snapshots = range(len(ram_dump) // ram_length - 1, -1, -1) - snapshots = range(4576, -1, -1) + snapshots = range(len(ram_dump) // ram_length - 1, -1, -1) + #snapshots = range(4576, -1, -1) elif analyze_snapshots == "last": snapshots = range(len(ram_dump) // ram_length - 1, len(ram_dump) // ram_length - 2, -1) for snapshot_num in snapshots: @@ -262,6 +262,7 @@ def do_all_the_things(ram_filename, bin_filename, map_filename, print_block_cont potential_type = word bgcolor = "gray" if address in qstr_pools: + #print(address, len(data)) bgcolor = "tomato" elif potential_type in function_types: bgcolor = "green" From dbc977485c5004e7e63d5783ac51421e3ba026aa Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 3 Jul 2018 06:31:07 -0700 Subject: [PATCH 07/15] Support gathering register values on nrf. --- ports/nrf/Makefile | 2 +- ports/nrf/supervisor/cpu.s | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) mode change 100644 => 100755 ports/nrf/Makefile create mode 100755 ports/nrf/supervisor/cpu.s diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile old mode 100644 new mode 100755 index 6ba9ef359a..5f3b9b25cc --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -213,7 +213,7 @@ FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py') FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/,$(FROZEN_MPY_PY_FILES:.py=.mpy)) OBJ += $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) -OBJ += $(BUILD)/pins_gen.o +OBJ += $(BUILD)/pins_gen.o supervisor/cpu.o OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_NRFX:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) diff --git a/ports/nrf/supervisor/cpu.s b/ports/nrf/supervisor/cpu.s new file mode 100755 index 0000000000..9e6807a5e2 --- /dev/null +++ b/ports/nrf/supervisor/cpu.s @@ -0,0 +1,27 @@ +.syntax unified +.cpu cortex-m4 +.thumb +.text +.align 2 + +@ uint cpu_get_regs_and_sp(r0=uint regs[10]) +.global cpu_get_regs_and_sp +.thumb +.thumb_func +.type cpu_get_regs_and_sp, %function +cpu_get_regs_and_sp: +@ store registers into given array +str r4, [r0], #4 +str r5, [r0], #4 +str r6, [r0], #4 +str r7, [r0], #4 +str r8, [r0], #4 +str r9, [r0], #4 +str r10, [r0], #4 +str r11, [r0], #4 +str r12, [r0], #4 +str r13, [r0], #4 + +@ return the sp +mov r0, sp +bx lr From 8fb976f0bd6919b858814d51a58dd5f8b8f746f9 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 3 Jul 2018 07:25:54 -0700 Subject: [PATCH 08/15] Split out assembly source files. --- ports/nrf/Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 5f3b9b25cc..912b60ecf6 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -209,16 +209,19 @@ SRC_SHARED_BINDINGS = \ SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_BINDINGS)) \ $(addprefix shared-module/, $(SRC_SHARED_MODULE)) +SRC_S = supervisor/cpu.s + FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py') FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/,$(FROZEN_MPY_PY_FILES:.py=.mpy)) OBJ += $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) -OBJ += $(BUILD)/pins_gen.o supervisor/cpu.o +OBJ += $(BUILD)/pins_gen.o OBJ += $(addprefix $(BUILD)/, $(SRC_HAL:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_NRFX:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(DRIVERS_SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_COMMON_HAL_EXPANDED:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o)) +OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) $(BUILD)/$(FATFS_DIR)/ff.o: COPT += -Os $(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os From 54179a01897ace60099b76690e7793d3241b1556 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 5 Jul 2018 04:44:00 -0700 Subject: [PATCH 09/15] Use .s in samd Makefile too. --- ports/atmel-samd/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index 9a40102700..9c14ac29c3 100755 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -412,7 +412,7 @@ endif SRC_SHARED_MODULE_EXPANDED = $(addprefix shared-bindings/, $(SRC_SHARED_MODULE)) \ $(addprefix shared-module/, $(SRC_SHARED_MODULE)) -SRC_O = supervisor/$(CHIP_FAMILY)_cpu.o +SRC_S = supervisor/$(CHIP_FAMILY)_cpu.s OBJ = $(PY_O) $(SUPERVISOR_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(SRC_ASF:.c=.o)) @@ -421,7 +421,7 @@ OBJ += $(addprefix $(BUILD)/, $(SRC_SHARED_MODULE_EXPANDED:.c=.o)) ifeq ($(INTERNAL_LIBM),1) OBJ += $(addprefix $(BUILD)/, $(SRC_LIBM:.c=.o)) endif -OBJ += $(addprefix $(BUILD)/, $(SRC_O)) +OBJ += $(addprefix $(BUILD)/, $(SRC_S:.s=.o)) SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_COMMON_HAL_EXPANDED) $(SRC_SHARED_MODULE_EXPANDED) $(STM_SRC_C) From ac74edb4e4ee2ac08ca0651c7dc8640286d08ca7 Mon Sep 17 00:00:00 2001 From: arturo182 Date: Wed, 4 Jul 2018 20:24:41 +0200 Subject: [PATCH 10/15] nrf: Fix SPI transfers with data larger than HW allows --- ports/nrf/common-hal/busio/SPI.c | 58 +++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/ports/nrf/common-hal/busio/SPI.c b/ports/nrf/common-hal/busio/SPI.c index d826bc0261..10974c4c5b 100644 --- a/ports/nrf/common-hal/busio/SPI.c +++ b/ports/nrf/common-hal/busio/SPI.c @@ -33,6 +33,8 @@ #define INST_NO 2 #endif +#define MAX_XFER_SIZE ((1U << NRFX_CONCAT_3(SPIM, INST_NO, _EASYDMA_MAXCNT_SIZE)) - 1) + // Convert frequency to clock-speed-dependent value static nrf_spim_frequency_t baudrate_to_spim_frequency(const uint32_t baudrate) { if (baudrate <= 125000) @@ -152,30 +154,68 @@ bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size if (len == 0) return true; - const nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_TX(data, len); - const nrfx_err_t err = nrfx_spim_xfer(&self->spim, &xfer, 0); + const uint32_t parts = len / MAX_XFER_SIZE; + const uint32_t remainder = len % MAX_XFER_SIZE; - return (err == NRFX_SUCCESS); + for (uint32_t i = 0; i < parts; ++i) { + const nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_TX(data + i * MAX_XFER_SIZE, MAX_XFER_SIZE); + if (nrfx_spim_xfer(&self->spim, &xfer, 0) != NRFX_SUCCESS) + return false; + } + + if (remainder > 0) { + const nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_TX(data + parts * MAX_XFER_SIZE, remainder); + if (nrfx_spim_xfer(&self->spim, &xfer, 0) != NRFX_SUCCESS) + return false; + } + + return true; } bool common_hal_busio_spi_read(busio_spi_obj_t *self, uint8_t *data, size_t len, uint8_t write_value) { if (len == 0) return true; - const nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_RX(data, len); - const nrfx_err_t err = nrfx_spim_xfer(&self->spim, &xfer, 0); + const uint32_t parts = len / MAX_XFER_SIZE; + const uint32_t remainder = len % MAX_XFER_SIZE; - return (err == NRFX_SUCCESS); + for (uint32_t i = 0; i < parts; ++i) { + const nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_RX(data + i * MAX_XFER_SIZE, MAX_XFER_SIZE); + if (nrfx_spim_xfer(&self->spim, &xfer, 0) != NRFX_SUCCESS) + return false; + } + + if (remainder > 0) { + const nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_RX(data + parts * MAX_XFER_SIZE, remainder); + if (nrfx_spim_xfer(&self->spim, &xfer, 0) != NRFX_SUCCESS) + return false; + } + + return true; } bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, uint8_t *data_out, uint8_t *data_in, size_t len) { if (len == 0) return true; - const nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_SINGLE_XFER(data_out, len, data_in, len); - const nrfx_err_t err = nrfx_spim_xfer(&self->spim, &xfer, 0); + const uint32_t parts = len / MAX_XFER_SIZE; + const uint32_t remainder = len % MAX_XFER_SIZE; - return (err == NRFX_SUCCESS); + for (uint32_t i = 0; i < parts; ++i) { + const nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_SINGLE_XFER(data_out + i * MAX_XFER_SIZE, MAX_XFER_SIZE, + data_in + i * MAX_XFER_SIZE, MAX_XFER_SIZE); + if (nrfx_spim_xfer(&self->spim, &xfer, 0) != NRFX_SUCCESS) + return false; + } + + if (remainder > 0) { + const nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_SINGLE_XFER(data_out + parts * MAX_XFER_SIZE, remainder, + data_in + parts * MAX_XFER_SIZE, remainder); + if (nrfx_spim_xfer(&self->spim, &xfer, 0) != NRFX_SUCCESS) + return false; + } + + return true; } uint32_t common_hal_busio_spi_get_frequency(busio_spi_obj_t* self) { From da27253693a92d44f194ae58a620f08bd8a30b46 Mon Sep 17 00:00:00 2001 From: arturo182 Date: Wed, 4 Jul 2018 20:33:01 +0200 Subject: [PATCH 11/15] nrf: Fix I2C transfers with data larger than HW allows --- ports/nrf/common-hal/busio/I2C.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/ports/nrf/common-hal/busio/I2C.c b/ports/nrf/common-hal/busio/I2C.c index c84c763f63..f421891627 100644 --- a/ports/nrf/common-hal/busio/I2C.c +++ b/ports/nrf/common-hal/busio/I2C.c @@ -34,6 +34,7 @@ #include "nrf_gpio.h" #define INST_NO 0 +#define MAX_XFER_SIZE ((1U << NRFX_CONCAT_3(TWIM, INST_NO, _EASYDMA_MAXCNT_SIZE)) - 1) static uint8_t twi_error_to_mp(const nrfx_err_t err) { switch (err) { @@ -152,8 +153,21 @@ uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addr, const u if(len == 0) return common_hal_busio_i2c_probe(self, addr) ? 0 : MP_ENODEV; + const uint32_t parts = len / MAX_XFER_SIZE; + const uint32_t remainder = len % MAX_XFER_SIZE; + nrfx_err_t err = NRFX_SUCCESS; + nrfx_twim_enable(&self->twim); - const nrfx_err_t err = nrfx_twim_tx(&self->twim, addr, data, len, !stopBit); + + for (uint32_t i = 0; i < parts; ++i) { + err = nrfx_twim_tx(&self->twim, addr, data + i * MAX_XFER_SIZE, MAX_XFER_SIZE, !stopBit); + if (err != NRFX_SUCCESS) + break; + } + + if ((remainder > 0) && (err == NRFX_SUCCESS)) + err = nrfx_twim_tx(&self->twim, addr, data + parts * MAX_XFER_SIZE, remainder, !stopBit); + nrfx_twim_disable(&self->twim); return twi_error_to_mp(err); @@ -163,8 +177,21 @@ uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t if(len == 0) return 0; + const uint32_t parts = len / MAX_XFER_SIZE; + const uint32_t remainder = len % MAX_XFER_SIZE; + nrfx_err_t err = NRFX_SUCCESS; + nrfx_twim_enable(&self->twim); - const nrfx_err_t err = nrfx_twim_rx(&self->twim, addr, data, len); + + for (uint32_t i = 0; i < parts; ++i) { + err = nrfx_twim_rx(&self->twim, addr, data + i * MAX_XFER_SIZE, MAX_XFER_SIZE); + if (err != NRFX_SUCCESS) + break; + } + + if ((remainder > 0) && (err == NRFX_SUCCESS)) + err = nrfx_twim_rx(&self->twim, addr, data + parts * MAX_XFER_SIZE, remainder); + nrfx_twim_disable(&self->twim); return twi_error_to_mp(err); From 1ea633c484a78a2d42168f03f46c4a28d9da2f65 Mon Sep 17 00:00:00 2001 From: arturo182 Date: Sun, 8 Jul 2018 12:49:39 +0200 Subject: [PATCH 12/15] nrf: mphalport cleanup --- ports/nrf/mphalport.h | 48 +++++++++---------------------------------- 1 file changed, 10 insertions(+), 38 deletions(-) diff --git a/ports/nrf/mphalport.h b/ports/nrf/mphalport.h index 3334fd7fe9..01a37a3b31 100644 --- a/ports/nrf/mphalport.h +++ b/ports/nrf/mphalport.h @@ -27,53 +27,25 @@ #ifndef __NRF52_HAL #define __NRF52_HAL -#include "py/mpconfig.h" +#include +#include + +#include "hal_gpio.h" +#include "lib/utils/interrupt_char.h" +#include "nrf.h" #include NRF5_HAL_H #include "pin.h" -#include "hal_gpio.h" +#include "py/mpconfig.h" -#include "lib/oofatfs/ff.h" +extern volatile uint64_t ticks_ms; -typedef enum -{ - HAL_OK = 0x00, - HAL_ERROR = 0x01, - HAL_BUSY = 0x02, - HAL_TIMEOUT = 0x03 -} HAL_StatusTypeDef; - -extern FIL* boot_output_file; - -static inline uint32_t hal_tick_fake(void) { - return 0; +static inline mp_uint_t mp_hal_ticks_ms(void) { + return ticks_ms; } -#define mp_hal_ticks_ms hal_tick_fake // TODO: implement. Right now, return 0 always - -extern const unsigned char mp_hal_status_to_errno_table[4]; - -NORETURN void mp_hal_raise(HAL_StatusTypeDef status); -void mp_hal_set_interrupt_char(int c); // -1 to disable - int mp_hal_stdin_rx_chr(void); void mp_hal_stdout_tx_str(const char *str); bool mp_hal_stdin_any(void); -#define mp_hal_pin_obj_t const pin_obj_t* -#define mp_hal_get_pin_obj(o) pin_find(o) -#define mp_hal_pin_high(p) hal_gpio_pin_high(p) -#define mp_hal_pin_low(p) hal_gpio_pin_low(p) -#define mp_hal_pin_read(p) hal_gpio_pin_read(p) -#define mp_hal_pin_write(p, v) do { if (v) { mp_hal_pin_high(p); } else { mp_hal_pin_low(p); } } while (0) -#define mp_hal_pin_od_low(p) mp_hal_pin_low(p) -#define mp_hal_pin_od_high(p) mp_hal_pin_high(p) -#define mp_hal_pin_open_drain(p) hal_gpio_cfg_pin(p->port, p->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED) - - -// TODO: empty implementation for now. Used by machine_spi.c:69 -#define mp_hal_delay_us_fast(p) -#define mp_hal_ticks_us() (0) -#define mp_hal_ticks_cpu() (0) - #endif From 887486582e94012061d48d1f5310fd979021b9ac Mon Sep 17 00:00:00 2001 From: arturo182 Date: Sun, 8 Jul 2018 14:52:14 +0200 Subject: [PATCH 13/15] nrf: Remove port-specific help It contains mentions to the pyb module which was removed. The ble-specific help will be re-added later when the API is stabilized. --- ports/nrf/Makefile | 1 - ports/nrf/help.c | 54 ---------------------------------------- ports/nrf/mpconfigport.h | 2 +- 3 files changed, 1 insertion(+), 56 deletions(-) delete mode 100644 ports/nrf/help.c diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 6ba9ef359a..cb579fdc22 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -102,7 +102,6 @@ SRC_NRFX = $(addprefix nrfx/,\ SRC_C += \ mphalport.c \ - help.c \ fatfs_port.c \ fifo.c \ tick.c \ diff --git a/ports/nrf/help.c b/ports/nrf/help.c deleted file mode 100644 index 5cbb0fc911..0000000000 --- a/ports/nrf/help.c +++ /dev/null @@ -1,54 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2013, 2014 Damien P. George - * Copyright (c) 2016 Glenn Ruben Bakke - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include "py/builtin.h" - -#if BLUETOOTH_SD -#include "help_sd.h" -#endif - -const char nrf5_help_text[] = -"Welcome to MicroPython!\n" -"\n" -"For online help please visit http://micropython.org/help/.\n" -"\n" -"Quick overview of commands for the board:\n" -#if MICROPY_HW_HAS_LED -" pyb.LED(n) -- create an LED object for LED n (n=" HELP_TEXT_BOARD_LED ")\n" -"\n" -#endif -#if BLUETOOTH_SD -HELP_TEXT_SD -#endif -"Control commands:\n" -" CTRL-A -- on a blank line, enter raw REPL mode\n" -" CTRL-B -- on a blank line, enter normal REPL mode\n" -" CTRL-D -- on a blank line, do a soft reset of the board\n" -" CTRL-E -- on a blank line, enter paste mode\n" -"\n" -"For further help on a specific object, type help(obj)\n" -; diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 4d57d79f07..f8e717ccc1 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -93,8 +93,8 @@ #define MICROPY_PY_BUILTINS_EXECFILE (0) #define MICROPY_PY_BUILTINS_COMPILE (1) #define MICROPY_PY_BUILTINS_HELP (1) -#define MICROPY_PY_BUILTINS_HELP_TEXT nrf5_help_text #define MICROPY_PY_BUILTINS_HELP_MODULES (1) +#define MICROPY_PY_BUILTINS_HELP_TEXT circuitpython_help_text #define MICROPY_PY_BUILTINS_INPUT (1) #define MICROPY_MODULE_BUILTIN_INIT (1) #define MICROPY_PY_ALL_SPECIAL_METHODS (0) From c6a2a55bc9593469b8547d4ea9a0833894d5bc12 Mon Sep 17 00:00:00 2001 From: arturo182 Date: Sun, 8 Jul 2018 15:04:41 +0200 Subject: [PATCH 14/15] nrf: Cleanup mpconfigboard files All the removed defines were used by legacy modules that are now removed. --- ports/nrf/boards/feather52832/mpconfigboard.h | 41 ++-------------- ports/nrf/boards/feather52840/mpconfigboard.h | 45 ++---------------- ports/nrf/boards/pca10040/mpconfigboard.h | 43 ++--------------- ports/nrf/boards/pca10056/mpconfigboard.h | 47 ++----------------- ports/nrf/mpconfigport.h | 8 ---- 5 files changed, 12 insertions(+), 172 deletions(-) diff --git a/ports/nrf/boards/feather52832/mpconfigboard.h b/ports/nrf/boards/feather52832/mpconfigboard.h index d0da6e3987..cfbf224474 100644 --- a/ports/nrf/boards/feather52832/mpconfigboard.h +++ b/ports/nrf/boards/feather52832/mpconfigboard.h @@ -25,47 +25,12 @@ */ #define MICROPY_HW_BOARD_NAME "Bluefruit nRF52 Feather" -#define MICROPY_HW_MCU_NAME "NRF52832" -#define MICROPY_PY_SYS_PLATFORM "nrf52" +#define MICROPY_HW_MCU_NAME "nRF52832" +#define MICROPY_PY_SYS_PLATFORM "nRF52" -#define MICROPY_HW_HAS_LED (1) -#define MICROPY_HW_HAS_SWITCH (0) -#define MICROPY_HW_HAS_FLASH (0) -#define MICROPY_HW_HAS_SDCARD (0) -#define MICROPY_HW_HAS_MMA7660 (0) -#define MICROPY_HW_HAS_LIS3DSH (0) -#define MICROPY_HW_HAS_LCD (0) -#define MICROPY_HW_ENABLE_RNG (0) -#define MICROPY_HW_ENABLE_RTC (0) -#define MICROPY_HW_ENABLE_TIMER (0) -#define MICROPY_HW_ENABLE_SERVO (0) -#define MICROPY_HW_ENABLE_DAC (0) -#define MICROPY_HW_ENABLE_CAN (0) - -#define MICROPY_HW_LED_COUNT (2) -#define MICROPY_HW_LED_PULLUP (0) - -#define MICROPY_HW_LED1 (17) // LED1 -#define MICROPY_HW_LED2 (19) // LED2 - -// UART config #define MICROPY_HW_UART1_RX (pin_P0_08) #define MICROPY_HW_UART1_TX (pin_P0_06) #define MICROPY_HW_UART1_HWFC (0) -// SPI0 config -#define MICROPY_HW_SPI0_NAME "SPI0" -#define MICROPY_HW_SPI0_SCK (pin_P0_12) // (Arduino D13) -#define MICROPY_HW_SPI0_MOSI (pin_P0_13) // (Arduino D11) -#define MICROPY_HW_SPI0_MISO (pin_P0_14) // (Arduino D12) - -#define MICROPY_HW_PWM0_NAME "PWM0" -#define MICROPY_HW_PWM1_NAME "PWM1" -#define MICROPY_HW_PWM2_NAME "PWM2" - -#define HELP_TEXT_BOARD_LED "1,2" - - - -#define PORT_HEAP_SIZE (32*1024) +#define PORT_HEAP_SIZE (32 * 1024) #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 diff --git a/ports/nrf/boards/feather52840/mpconfigboard.h b/ports/nrf/boards/feather52840/mpconfigboard.h index 252906fca9..aff9177d0b 100644 --- a/ports/nrf/boards/feather52840/mpconfigboard.h +++ b/ports/nrf/boards/feather52840/mpconfigboard.h @@ -27,53 +27,14 @@ #define FEATHER52840 #define MICROPY_HW_BOARD_NAME "Feather52840" -#define MICROPY_HW_MCU_NAME "NRF52840" -#define MICROPY_PY_SYS_PLATFORM "nrf52840-PDK" +#define MICROPY_HW_MCU_NAME "nRF52840" +#define MICROPY_PY_SYS_PLATFORM "nRF52840-PDK" -#define MICROPY_HW_HAS_LED (1) -#define MICROPY_HW_HAS_SWITCH (0) -#define MICROPY_HW_HAS_FLASH (0) -#define MICROPY_HW_HAS_SDCARD (0) -#define MICROPY_HW_HAS_MMA7660 (0) -#define MICROPY_HW_HAS_LIS3DSH (0) -#define MICROPY_HW_HAS_LCD (0) -#define MICROPY_HW_ENABLE_RNG (0) -#define MICROPY_HW_ENABLE_RTC (0) -#define MICROPY_HW_ENABLE_TIMER (0) -#define MICROPY_HW_ENABLE_SERVO (0) -#define MICROPY_HW_ENABLE_DAC (0) -#define MICROPY_HW_ENABLE_CAN (0) - -#define MICROPY_HW_LED_COUNT (4) -#define MICROPY_HW_LED_PULLUP (1) - -#define MICROPY_HW_LED1 (13) // LED1 -#define MICROPY_HW_LED2 (14) // LED2 -#define MICROPY_HW_LED3 (15) // LED3 -#define MICROPY_HW_LED4 (16) // LED4 - -// UART config #define MICROPY_HW_UART1_RX (pin_P0_08) #define MICROPY_HW_UART1_TX (pin_P0_06) #define MICROPY_HW_UART1_CTS (pin_P0_07) #define MICROPY_HW_UART1_RTS (pin_P0_05) #define MICROPY_HW_UART1_HWFC (1) -// SPI0 config -#define MICROPY_HW_SPI0_NAME "SPI0" - -#define MICROPY_HW_SPI0_SCK (pin_P1_15) -#define MICROPY_HW_SPI0_MOSI (pin_P1_13) -#define MICROPY_HW_SPI0_MISO (pin_P1_14) - -#define MICROPY_HW_PWM0_NAME "PWM0" -#define MICROPY_HW_PWM1_NAME "PWM1" -#define MICROPY_HW_PWM2_NAME "PWM2" -#if 0 -#define MICROPY_HW_PWM3_NAME "PWM3" -#endif - -#define HELP_TEXT_BOARD_LED "1,2,3,4" - -#define PORT_HEAP_SIZE (128*1024) +#define PORT_HEAP_SIZE (128 * 1024) #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 diff --git a/ports/nrf/boards/pca10040/mpconfigboard.h b/ports/nrf/boards/pca10040/mpconfigboard.h index ea8000ca5a..f618a39ea0 100644 --- a/ports/nrf/boards/pca10040/mpconfigboard.h +++ b/ports/nrf/boards/pca10040/mpconfigboard.h @@ -24,52 +24,15 @@ * THE SOFTWARE. */ -#define PCA10040 - #define MICROPY_HW_BOARD_NAME "PCA10040" -#define MICROPY_HW_MCU_NAME "NRF52832" -#define MICROPY_PY_SYS_PLATFORM "nrf52-DK" +#define MICROPY_HW_MCU_NAME "nRF52832" +#define MICROPY_PY_SYS_PLATFORM "nRF52-DK" -#define MICROPY_HW_HAS_LED (1) -#define MICROPY_HW_HAS_SWITCH (0) -#define MICROPY_HW_HAS_FLASH (0) -#define MICROPY_HW_HAS_SDCARD (0) -#define MICROPY_HW_HAS_MMA7660 (0) -#define MICROPY_HW_HAS_LIS3DSH (0) -#define MICROPY_HW_HAS_LCD (0) -#define MICROPY_HW_ENABLE_RNG (0) -#define MICROPY_HW_ENABLE_RTC (0) -#define MICROPY_HW_ENABLE_TIMER (0) -#define MICROPY_HW_ENABLE_SERVO (0) -#define MICROPY_HW_ENABLE_DAC (0) -#define MICROPY_HW_ENABLE_CAN (0) - -#define MICROPY_HW_LED_COUNT (4) -#define MICROPY_HW_LED_PULLUP (1) - -#define MICROPY_HW_LED1 (17) // LED1 -#define MICROPY_HW_LED2 (18) // LED2 -#define MICROPY_HW_LED3 (19) // LED3 -#define MICROPY_HW_LED4 (20) // LED4 - -// UART config #define MICROPY_HW_UART1_RX (pin_P0_08) #define MICROPY_HW_UART1_TX (pin_P0_06) #define MICROPY_HW_UART1_CTS (pin_P0_07) #define MICROPY_HW_UART1_RTS (pin_P0_05) #define MICROPY_HW_UART1_HWFC (1) -// SPI0 config -#define MICROPY_HW_SPI0_NAME "SPI0" -#define MICROPY_HW_SPI0_SCK (pin_P0_25) // (Arduino D13) -#define MICROPY_HW_SPI0_MOSI (pin_P0_23) // (Arduino D11) -#define MICROPY_HW_SPI0_MISO (pin_P0_24) // (Arduino D12) - -#define MICROPY_HW_PWM0_NAME "PWM0" -#define MICROPY_HW_PWM1_NAME "PWM1" -#define MICROPY_HW_PWM2_NAME "PWM2" - -#define HELP_TEXT_BOARD_LED "1,2,3,4" - -#define PORT_HEAP_SIZE (32*1024) +#define PORT_HEAP_SIZE (32 * 1024) #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 diff --git a/ports/nrf/boards/pca10056/mpconfigboard.h b/ports/nrf/boards/pca10056/mpconfigboard.h index 2be1156d07..4278572478 100644 --- a/ports/nrf/boards/pca10056/mpconfigboard.h +++ b/ports/nrf/boards/pca10056/mpconfigboard.h @@ -24,56 +24,15 @@ * THE SOFTWARE. */ -#define PCA10056 - #define MICROPY_HW_BOARD_NAME "PCA10056" -#define MICROPY_HW_MCU_NAME "NRF52840" -#define MICROPY_PY_SYS_PLATFORM "nrf52840-PDK" +#define MICROPY_HW_MCU_NAME "nRF52840" +#define MICROPY_PY_SYS_PLATFORM "nRF52840-PDK" -#define MICROPY_HW_HAS_LED (1) -#define MICROPY_HW_HAS_SWITCH (0) -#define MICROPY_HW_HAS_FLASH (0) -#define MICROPY_HW_HAS_SDCARD (0) -#define MICROPY_HW_HAS_MMA7660 (0) -#define MICROPY_HW_HAS_LIS3DSH (0) -#define MICROPY_HW_HAS_LCD (0) -#define MICROPY_HW_ENABLE_RNG (0) -#define MICROPY_HW_ENABLE_RTC (0) -#define MICROPY_HW_ENABLE_TIMER (0) -#define MICROPY_HW_ENABLE_SERVO (0) -#define MICROPY_HW_ENABLE_DAC (0) -#define MICROPY_HW_ENABLE_CAN (0) - -#define MICROPY_HW_LED_COUNT (4) -#define MICROPY_HW_LED_PULLUP (1) - -#define MICROPY_HW_LED1 (13) // LED1 -#define MICROPY_HW_LED2 (14) // LED2 -#define MICROPY_HW_LED3 (15) // LED3 -#define MICROPY_HW_LED4 (16) // LED4 - -// UART config #define MICROPY_HW_UART1_RX (pin_P0_08) #define MICROPY_HW_UART1_TX (pin_P0_06) #define MICROPY_HW_UART1_CTS (pin_P0_07) #define MICROPY_HW_UART1_RTS (pin_P0_05) #define MICROPY_HW_UART1_HWFC (1) -// SPI0 config -#define MICROPY_HW_SPI0_NAME "SPI0" - -#define MICROPY_HW_SPI0_SCK (pin_P1_15) -#define MICROPY_HW_SPI0_MOSI (pin_P1_13) -#define MICROPY_HW_SPI0_MISO (pin_P1_14) - -#define MICROPY_HW_PWM0_NAME "PWM0" -#define MICROPY_HW_PWM1_NAME "PWM1" -#define MICROPY_HW_PWM2_NAME "PWM2" -#if 0 -#define MICROPY_HW_PWM3_NAME "PWM3" -#endif - -#define HELP_TEXT_BOARD_LED "1,2,3,4" - -#define PORT_HEAP_SIZE (128*1024) +#define PORT_HEAP_SIZE (128 * 1024) #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index f8e717ccc1..05f64bb4f2 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -126,14 +126,6 @@ #define MICROPY_KBD_EXCEPTION (1) -#ifndef MICROPY_HW_LED_COUNT -#define MICROPY_HW_LED_COUNT (0) -#endif - -#ifndef MICROPY_HW_LED_PULLUP -#define MICROPY_HW_LED_PULLUP (0) -#endif - #ifndef MICROPY_PY_HW_RNG #define MICROPY_PY_HW_RNG (1) #endif From 25ece8fb3d96969dffb0726ba57d42cf7ed95b4d Mon Sep 17 00:00:00 2001 From: arturo182 Date: Sun, 8 Jul 2018 15:17:06 +0200 Subject: [PATCH 15/15] nrf: Rewrite the UART HAL using nrfx --- ports/nrf/Makefile | 2 +- ports/nrf/boards/feather52832/mpconfigboard.h | 6 +- ports/nrf/boards/feather52840/mpconfigboard.h | 8 +- ports/nrf/boards/pca10040/mpconfigboard.h | 8 +- ports/nrf/boards/pca10056/mpconfigboard.h | 8 +- ports/nrf/hal/hal_uart.c | 181 ------------------ ports/nrf/hal/hal_uart.h | 113 ----------- ports/nrf/mphalport.c | 48 ++--- ports/nrf/mphalport.h | 3 + ports/nrf/nrfx_config.h | 9 + ports/nrf/supervisor/serial.c | 51 +++-- 11 files changed, 75 insertions(+), 362 deletions(-) delete mode 100644 ports/nrf/hal/hal_uart.c delete mode 100644 ports/nrf/hal/hal_uart.h diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index cb579fdc22..1a827ce13e 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -90,7 +90,6 @@ LIBS += -L $(dir $(LIBC_FILE_NAME)) -lc LIBS += -L $(dir $(LIBGCC_FILE_NAME)) -lgcc SRC_HAL = $(addprefix hal/,\ - hal_uart.c \ hal_time.c \ hal_rng.c \ ) @@ -98,6 +97,7 @@ SRC_HAL = $(addprefix hal/,\ SRC_NRFX = $(addprefix nrfx/,\ drivers/src/nrfx_spim.c \ drivers/src/nrfx_twim.c \ + drivers/src/nrfx_uart.c \ ) SRC_C += \ diff --git a/ports/nrf/boards/feather52832/mpconfigboard.h b/ports/nrf/boards/feather52832/mpconfigboard.h index cfbf224474..84afc5e2d7 100644 --- a/ports/nrf/boards/feather52832/mpconfigboard.h +++ b/ports/nrf/boards/feather52832/mpconfigboard.h @@ -28,9 +28,9 @@ #define MICROPY_HW_MCU_NAME "nRF52832" #define MICROPY_PY_SYS_PLATFORM "nRF52" -#define MICROPY_HW_UART1_RX (pin_P0_08) -#define MICROPY_HW_UART1_TX (pin_P0_06) -#define MICROPY_HW_UART1_HWFC (0) +#define MICROPY_HW_UART_RX NRF_GPIO_PIN_MAP(0, 8) +#define MICROPY_HW_UART_TX NRF_GPIO_PIN_MAP(0, 6) +#define MICROPY_HW_UART_HWFC (0) #define PORT_HEAP_SIZE (32 * 1024) #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 diff --git a/ports/nrf/boards/feather52840/mpconfigboard.h b/ports/nrf/boards/feather52840/mpconfigboard.h index aff9177d0b..3b318bdab7 100644 --- a/ports/nrf/boards/feather52840/mpconfigboard.h +++ b/ports/nrf/boards/feather52840/mpconfigboard.h @@ -30,11 +30,9 @@ #define MICROPY_HW_MCU_NAME "nRF52840" #define MICROPY_PY_SYS_PLATFORM "nRF52840-PDK" -#define MICROPY_HW_UART1_RX (pin_P0_08) -#define MICROPY_HW_UART1_TX (pin_P0_06) -#define MICROPY_HW_UART1_CTS (pin_P0_07) -#define MICROPY_HW_UART1_RTS (pin_P0_05) -#define MICROPY_HW_UART1_HWFC (1) +#define MICROPY_HW_UART_RX NRF_GPIO_PIN_MAP(0, 8) +#define MICROPY_HW_UART_TX NRF_GPIO_PIN_MAP(0, 6) +#define MICROPY_HW_UART_HWFC (0) #define PORT_HEAP_SIZE (128 * 1024) #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 diff --git a/ports/nrf/boards/pca10040/mpconfigboard.h b/ports/nrf/boards/pca10040/mpconfigboard.h index f618a39ea0..8fa001dfa1 100644 --- a/ports/nrf/boards/pca10040/mpconfigboard.h +++ b/ports/nrf/boards/pca10040/mpconfigboard.h @@ -28,11 +28,9 @@ #define MICROPY_HW_MCU_NAME "nRF52832" #define MICROPY_PY_SYS_PLATFORM "nRF52-DK" -#define MICROPY_HW_UART1_RX (pin_P0_08) -#define MICROPY_HW_UART1_TX (pin_P0_06) -#define MICROPY_HW_UART1_CTS (pin_P0_07) -#define MICROPY_HW_UART1_RTS (pin_P0_05) -#define MICROPY_HW_UART1_HWFC (1) +#define MICROPY_HW_UART_RX NRF_GPIO_PIN_MAP(0, 8) +#define MICROPY_HW_UART_TX NRF_GPIO_PIN_MAP(0, 6) +#define MICROPY_HW_UART_HWFC (0) #define PORT_HEAP_SIZE (32 * 1024) #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 diff --git a/ports/nrf/boards/pca10056/mpconfigboard.h b/ports/nrf/boards/pca10056/mpconfigboard.h index 4278572478..96e08e5788 100644 --- a/ports/nrf/boards/pca10056/mpconfigboard.h +++ b/ports/nrf/boards/pca10056/mpconfigboard.h @@ -28,11 +28,9 @@ #define MICROPY_HW_MCU_NAME "nRF52840" #define MICROPY_PY_SYS_PLATFORM "nRF52840-PDK" -#define MICROPY_HW_UART1_RX (pin_P0_08) -#define MICROPY_HW_UART1_TX (pin_P0_06) -#define MICROPY_HW_UART1_CTS (pin_P0_07) -#define MICROPY_HW_UART1_RTS (pin_P0_05) -#define MICROPY_HW_UART1_HWFC (1) +#define MICROPY_HW_UART_RX NRF_GPIO_PIN_MAP(0, 8) +#define MICROPY_HW_UART_TX NRF_GPIO_PIN_MAP(0, 6) +#define MICROPY_HW_UART_HWFC (0) #define PORT_HEAP_SIZE (128 * 1024) #define CIRCUITPY_AUTORELOAD_DELAY_MS 500 diff --git a/ports/nrf/hal/hal_uart.c b/ports/nrf/hal/hal_uart.c deleted file mode 100644 index e8d4230816..0000000000 --- a/ports/nrf/hal/hal_uart.c +++ /dev/null @@ -1,181 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Glenn Ruben Bakke - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#include -#include - -#include "nrf.h" -#include "mphalport.h" -#include "hal_uart.h" -#include "fifo.h" - -#include "lib/utils/interrupt_char.h" - -#ifdef HAL_UART_MODULE_ENABLED - -FIFO_DEF(_ff_uart, 128, uint8_t, true, UARTE0_UART0_IRQn); - -uint32_t hal_uart_baudrate_lookup[] = { - UART_BAUDRATE_BAUDRATE_Baud1200, ///< 1200 baud. - UART_BAUDRATE_BAUDRATE_Baud2400, ///< 2400 baud. - UART_BAUDRATE_BAUDRATE_Baud4800, ///< 4800 baud. - UART_BAUDRATE_BAUDRATE_Baud9600, ///< 9600 baud. - UART_BAUDRATE_BAUDRATE_Baud14400, ///< 14400 baud. - UART_BAUDRATE_BAUDRATE_Baud19200, ///< 19200 baud. - UART_BAUDRATE_BAUDRATE_Baud28800, ///< 28800 baud. - UART_BAUDRATE_BAUDRATE_Baud38400, ///< 38400 baud. - UART_BAUDRATE_BAUDRATE_Baud57600, ///< 57600 baud. - UART_BAUDRATE_BAUDRATE_Baud76800, ///< 76800 baud. - UART_BAUDRATE_BAUDRATE_Baud115200, ///< 115200 baud. - UART_BAUDRATE_BAUDRATE_Baud230400, ///< 230400 baud. - UART_BAUDRATE_BAUDRATE_Baud250000, ///< 250000 baud. - UART_BAUDRATE_BAUDRATE_Baud460800, ///< 460800 baud. - UART_BAUDRATE_BAUDRATE_Baud921600, ///< 921600 baud. - UART_BAUDRATE_BAUDRATE_Baud1M, ///< 1000000 baud. -}; - -hal_uart_error_t hal_uart_char_write(NRF_UART_Type * p_instance, uint8_t ch) { - p_instance->ERRORSRC = 0; - p_instance->TXD = (uint8_t)ch; - while (p_instance->EVENTS_TXDRDY != 1) { - // Blocking wait. - } - - // Clear the TX flag. - p_instance->EVENTS_TXDRDY = 0; - - return p_instance->ERRORSRC; -} - -hal_uart_error_t hal_uart_char_read(NRF_UART_Type * p_instance, uint8_t * ch) { - while ( !fifo_read(_ff_uart, ch) ) { - // wait for fifo data - } - - return HAL_UART_ERROR_NONE; -} - -hal_uart_error_t hal_uart_buffer_write(NRF_UART_Type * p_instance, uint8_t * p_buffer, uint32_t num_of_bytes, uart_complete_cb cb) { - int i = 0; - hal_uart_error_t err = 0; - uint8_t ch = p_buffer[i++]; - while (i < num_of_bytes) { - err = hal_uart_char_write(p_instance, ch); - if (err) { - return err; - } - ch = p_buffer[i++]; - } - cb(); - return err; -} - -hal_uart_error_t hal_uart_buffer_read(NRF_UART_Type * p_instance, uint8_t * p_buffer, uint32_t num_of_bytes, uart_complete_cb cb) { - int i = 0; - hal_uart_error_t err = 0; - while (i < num_of_bytes) { - hal_uart_error_t err = hal_uart_char_read(p_instance, &p_buffer[i]); - if (err) { - return err; - } - i++; - } - cb(); - return err; -} - -int hal_uart_available(NRF_UART_Type * p_instance) -{ - return fifo_count(_ff_uart); -} - -void hal_uart_init(NRF_UART_Type * p_instance, hal_uart_init_t const * p_uart_init) { - hal_gpio_cfg_pin(p_uart_init->tx_pin->port, p_uart_init->tx_pin->pin, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_PULL_DISABLED); - hal_gpio_cfg_pin(p_uart_init->tx_pin->port, p_uart_init->rx_pin->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED); - - hal_gpio_pin_clear(p_uart_init->tx_pin->port, p_uart_init->tx_pin->pin); - - p_instance->PSELTXD = p_uart_init->tx_pin->pin; - p_instance->PSELRXD = p_uart_init->rx_pin->pin; - -#if NRF52840_XXAA - p_instance->PSELTXD |= (p_uart_init->tx_pin->port << UARTE_PSEL_TXD_PORT_Pos); - p_instance->PSELRXD |= (p_uart_init->rx_pin->port << UARTE_PSEL_RXD_PORT_Pos); -#endif - - if (p_uart_init->flow_control) { - hal_gpio_cfg_pin(p_uart_init->rts_pin->port, p_uart_init->rts_pin->pin, HAL_GPIO_MODE_OUTPUT, HAL_GPIO_PULL_DISABLED); - hal_gpio_cfg_pin(p_uart_init->cts_pin->port, p_uart_init->cts_pin->pin, HAL_GPIO_MODE_INPUT, HAL_GPIO_PULL_DISABLED); - - p_instance->PSELCTS = p_uart_init->cts_pin->pin; - p_instance->PSELRTS = p_uart_init->rts_pin->pin; - -#if NRF52840_XXAA - p_instance->PSELCTS |= (p_uart_init->cts_pin->port << UARTE_PSEL_CTS_PORT_Pos); - p_instance->PSELRTS |= (p_uart_init->rts_pin->port << UARTE_PSEL_RTS_PORT_Pos); -#endif - - p_instance->CONFIG = (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos); - } - - p_instance->BAUDRATE = (hal_uart_baudrate_lookup[p_uart_init->baud_rate]); - p_instance->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos); - p_instance->EVENTS_TXDRDY = 0; - p_instance->EVENTS_RXDRDY = 0; - p_instance->TASKS_STARTTX = 1; - p_instance->TASKS_STARTRX = 1; - - // Adafruit IRQ + fifo - fifo_clear(_ff_uart); - p_instance->INTENSET = UART_INTENSET_RXDRDY_Msk; - NVIC_ClearPendingIRQ(p_uart_init->irq_num); - NVIC_SetPriority(p_uart_init->irq_num, p_uart_init->irq_priority); - NVIC_EnableIRQ(p_uart_init->irq_num); -} - - -void UARTE0_UART0_IRQHandler(void) -{ - NRF_UART_Type * p_instance = NRF_UART0; - - if (p_instance->EVENTS_RXDRDY) - { - uint8_t ch = (uint8_t) p_instance->RXD; - - // Keyboard interrupt - if (mp_interrupt_char != -1 && ch == mp_interrupt_char) - { - mp_keyboard_interrupt(); - }else - { - fifo_write(_ff_uart, &ch); - } - - p_instance->EVENTS_RXDRDY = 0x0UL; - } -} - -#endif // HAL_UART_MODULE_ENABLED diff --git a/ports/nrf/hal/hal_uart.h b/ports/nrf/hal/hal_uart.h deleted file mode 100644 index dc8d96ee51..0000000000 --- a/ports/nrf/hal/hal_uart.h +++ /dev/null @@ -1,113 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2016 Glenn Ruben Bakke - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef HAL_UART_H__ -#define HAL_UART_H__ - -#include -#include - -#include "nrf.h" - -#define UART_HWCONTROL_NONE ((uint32_t)UARTE_CONFIG_HWFC_Disabled << UARTE_CONFIG_HWFC_Pos) -#define UART_HWCONTROL_RTS_CTS ((uint32_t)(UARTE_CONFIG_HWFC_Enabled << UARTE_CONFIG_HWFC_Pos) -#define IS_UART_HARDWARE_FLOW_CONTROL(CONTROL)\ - (((CONTROL) == UART_HWCONTROL_NONE) || \ - ((CONTROL) == UART_HWCONTROL_RTS_CTS)) -#ifdef HAL_UART_MODULE_ENABLED - #define UART_BASE_POINTERS (const uint32_t[]){NRF_UART0_BASE} - #define UART_IRQ_VALUES (const uint32_t[]){UARTE0_UART0_IRQn} -#else // HAL_UARTE_MODULE_ENABLED - #ifdef NRF52832_XXAA - #define UART_BASE_POINTERS (const uint32_t[]){NRF_UARTE0_BASE} - #define UART_IRQ_VALUES (const uint32_t[]){UARTE0_UART0_IRQn} - #elif NRF52840_XXAA - #define UART_BASE_POINTERS (const uint32_t[]){NRF_UARTE0_BASE, \ - NRF_UARTE1_BASE} - #define UART_IRQ_VALUES (const uint32_t[]){UARTE0_UART0_IRQn, \ - UARTE1_IRQn} - #endif // HAL_UARTE_MODULE_ENABLED -#endif - -#define UART_BASE(x) ((NRF_UART_Type *)UART_BASE_POINTERS[x]) -#define UART_IRQ_NUM(x) (UART_IRQ_VALUES[x]) - -typedef enum -{ - HAL_UART_ERROR_NONE = 0x00, /*!< No error */ - HAL_UART_ERROR_ORE = 0x01, /*!< Overrun error. A start bit is received while the previous data still lies in RXD. (Previous data is lost.) */ - HAL_UART_ERROR_PE = 0x02, /*!< Parity error. A character with bad parity is received, if HW parity check is enabled. */ - HAL_UART_ERROR_FE = 0x04, /*!< Frame error. A valid stop bit is not detected on the serial data input after all bits in a character have been received. */ - HAL_UART_ERROR_BE = 0x08, /*!< Break error. The serial data input is '0' for longer than the length of a data frame. (The data frame length is 10 bits without parity bit, and 11 bits with parity bit.). */ -} hal_uart_error_t; - -typedef enum { - HAL_UART_BAUD_1K2 = 0, /**< 1200 baud */ - HAL_UART_BAUD_2K4, /**< 2400 baud */ - HAL_UART_BAUD_4K8, /**< 4800 baud */ - HAL_UART_BAUD_9K6, /**< 9600 baud */ - HAL_UART_BAUD_14K4, /**< 14.4 kbaud */ - HAL_UART_BAUD_19K2, /**< 19.2 kbaud */ - HAL_UART_BAUD_28K8, /**< 28.8 kbaud */ - HAL_UART_BAUD_38K4, /**< 38.4 kbaud */ - HAL_UART_BAUD_57K6, /**< 57.6 kbaud */ - HAL_UART_BAUD_76K8, /**< 76.8 kbaud */ - HAL_UART_BAUD_115K2, /**< 115.2 kbaud */ - HAL_UART_BAUD_230K4, /**< 230.4 kbaud */ - HAL_UART_BAUD_250K0, /**< 250.0 kbaud */ - HAL_UART_BAUD_500K0, /**< 500.0 kbaud */ - HAL_UART_BAUD_1M0 /**< 1 mbaud */ -} hal_uart_baudrate_t; - -typedef struct { - uint8_t id; /* UART instance id */ - const pin_obj_t * rx_pin; /* RX pin. */ - const pin_obj_t * tx_pin; /* TX pin. */ - const pin_obj_t * rts_pin; /* RTS pin, only used if flow control is enabled. */ - const pin_obj_t * cts_pin; /* CTS pin, only used if flow control is enabled. */ - bool flow_control; /* Flow control setting, if flow control is used, the system will use low power UART mode, based on CTS signal. */ - bool use_parity; /* Even parity if TRUE, no parity if FALSE. */ - uint32_t baud_rate; /* Baud rate configuration. */ - uint32_t irq_priority; /* UARTE IRQ priority. */ - uint32_t irq_num; -} hal_uart_init_t; - -typedef struct -{ - NRF_UART_Type * p_instance; /* UART registers base address */ - hal_uart_init_t init; /* UART communication parameters */ -} UART_HandleTypeDef; - -typedef void (*uart_complete_cb)(void); - -void hal_uart_init(NRF_UART_Type * p_instance, hal_uart_init_t const * p_uart_init); - -hal_uart_error_t hal_uart_char_write(NRF_UART_Type * p_instance, uint8_t ch); - -hal_uart_error_t hal_uart_char_read(NRF_UART_Type * p_instance, uint8_t * ch); -int hal_uart_available(NRF_UART_Type * p_instance); - -#endif // HAL_UART_H__ diff --git a/ports/nrf/mphalport.c b/ports/nrf/mphalport.c index 1d4e795c07..99c0c74436 100644 --- a/ports/nrf/mphalport.c +++ b/ports/nrf/mphalport.c @@ -4,6 +4,7 @@ * The MIT License (MIT) * * Copyright (c) 2015 Glenn Ruben Bakke + * Copyright (c) 2018 Artur Pacholec * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -24,46 +25,47 @@ * THE SOFTWARE. */ -#include #include -#include "py/mpstate.h" -#include "py/mphal.h" -#include "py/mperrno.h" -#include "hal_uart.h" +#include "mphalport.h" -#define UART_INSTANCE UART_BASE(0) +void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len); #if (MICROPY_PY_BLE_NUS == 0) int mp_hal_stdin_rx_chr(void) { - for (;;) { - if ( hal_uart_available(UART_INSTANCE) ) { - uint8_t ch; - hal_uart_char_read(UART_INSTANCE, &ch); - return (int) ch; - } - } + uint8_t data = 0; - return 0; + while (!nrfx_uart_rx_ready(&serial_instance)); + + const nrfx_err_t err = nrfx_uart_rx(&serial_instance, &data, sizeof(data)); + if (err == NRFX_SUCCESS) + NRFX_ASSERT(err); + + return data; } bool mp_hal_stdin_any(void) { - return hal_uart_available(UART_INSTANCE); + return nrfx_uart_rx_ready(&serial_instance); } void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { - while(len--) { - hal_uart_char_write(UART_INSTANCE, *str++); - } + if (len == 0) + return; + + const nrfx_err_t err = nrfx_uart_tx(&serial_instance, (uint8_t*)str, len); + if (err == NRFX_SUCCESS) + NRFX_ASSERT(err); } void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) { - while(len--){ - if (*str == '\n') { - hal_uart_char_write(UART_INSTANCE, '\r'); + const char cr = '\r'; + + while (len--) { + if (*str == '\n') + mp_hal_stdout_tx_strn(&cr, sizeof(cr)); + + mp_hal_stdout_tx_strn(str++, sizeof(char)); } - hal_uart_char_write(UART_INSTANCE, *str++); - } } #endif diff --git a/ports/nrf/mphalport.h b/ports/nrf/mphalport.h index 01a37a3b31..a217d88961 100644 --- a/ports/nrf/mphalport.h +++ b/ports/nrf/mphalport.h @@ -34,9 +34,12 @@ #include "lib/utils/interrupt_char.h" #include "nrf.h" #include NRF5_HAL_H +#include "nrfx_uart.h" #include "pin.h" #include "py/mpconfig.h" +extern nrfx_uart_t serial_instance; + extern volatile uint64_t ticks_ms; static inline mp_uint_t mp_hal_ticks_ms(void) { diff --git a/ports/nrf/nrfx_config.h b/ports/nrf/nrfx_config.h index 818931236a..f09ca04a3e 100644 --- a/ports/nrf/nrfx_config.h +++ b/ports/nrf/nrfx_config.h @@ -21,4 +21,13 @@ #define NRFX_TWIM_DEFAULT_CONFIG_FREQUENCY NRF_TWIM_FREQ_400K #define NRFX_TWIM_DEFAULT_CONFIG_HOLD_BUS_UNINIT 0 +// UART +#define NRFX_UART_ENABLED 1 +#define NRFX_UART0_ENABLED 1 + +#define NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY 7 +#define NRFX_UART_DEFAULT_CONFIG_HWFC NRF_UART_HWFC_DISABLED +#define NRFX_UART_DEFAULT_CONFIG_PARITY NRF_UART_PARITY_EXCLUDED +#define NRFX_UART_DEFAULT_CONFIG_BAUDRATE NRF_UART_BAUDRATE_115200 + #endif diff --git a/ports/nrf/supervisor/serial.c b/ports/nrf/supervisor/serial.c index 51e61bce7c..0ebc2b5777 100644 --- a/ports/nrf/supervisor/serial.c +++ b/ports/nrf/supervisor/serial.c @@ -24,45 +24,45 @@ * THE SOFTWARE. */ -#include "supervisor/serial.h" - -#include "py/obj.h" -#include "py/runtime.h" #include "mphalport.h" -#include "pins.h" -#include "hal_uart.h" -#if (MICROPY_PY_BLE_NUS) +#if MICROPY_PY_BLE_NUS #include "ble_uart.h" +#else +#include "nrf_gpio.h" +#include "pin.h" #endif -void serial_init(void) { +#define INST_NO 0 +nrfx_uart_t serial_instance = NRFX_UART_INSTANCE(INST_NO); + +void serial_init(void) { #if MICROPY_PY_BLE_NUS ble_uart_init0(); while (!ble_uart_enabled()) { ; } #else - hal_uart_init_t param = - { - .id = 0, - .rx_pin = &MICROPY_HW_UART1_RX, - .tx_pin = &MICROPY_HW_UART1_TX, - .rts_pin = NULL, - .cts_pin = NULL, - .flow_control = MICROPY_HW_UART1_HWFC ? true : false, - .use_parity = false, - .baud_rate = HAL_UART_BAUD_115K2, - .irq_priority = 6, - .irq_num = UARTE0_UART0_IRQn - }; + nrfx_uart_config_t config = NRFX_UART_DEFAULT_CONFIG; + config.pseltxd = MICROPY_HW_UART_TX; + config.pselrxd = MICROPY_HW_UART_RX; + config.hwfc = MICROPY_HW_UART_HWFC ? NRF_UART_HWFC_ENABLED : NRF_UART_HWFC_DISABLED; +#ifdef MICROPY_HW_UART_CTS + config.pselcts = MICROPY_HW_UART_CTS; +#endif +#ifdef MICROPY_HW_UART_RTS + config.pselrts = MICROPY_HW_UART_RTS; +#endif - hal_uart_init( UART_BASE(0), ¶m); + const nrfx_err_t err = nrfx_uart_init(&serial_instance, &config, NULL); + if (err == NRFX_SUCCESS) + NRFX_ASSERT(err); + + nrfx_uart_rx_enable(&serial_instance); #endif } - bool serial_connected(void) { return true; } @@ -75,7 +75,6 @@ bool serial_bytes_available(void) { return mp_hal_stdin_any(); } -void serial_write(const char* text) { - mp_hal_stdout_tx_str(text); +void serial_write(const char *text) { + mp_hal_stdout_tx_str(text); } -