├── images └── nrf5340-pdk.jpg ├── obj └── .gitignore ├── .gitmodules ├── src-net ├── ldscript ├── nrf.h ├── ble.h ├── mdepx.conf ├── board.c ├── main.c ├── start.S └── ble.c ├── src-app ├── ldscript ├── ble.h ├── mdepx.conf ├── board.c ├── main.c ├── start.S └── ble.c ├── Makefile ├── common.h ├── nrf5340_net.dts ├── nrf5340_app.dts └── README.md /images/nrf5340-pdk.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/machdep/nrf5340/HEAD/images/nrf5340-pdk.jpg -------------------------------------------------------------------------------- /obj/.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore everything in this directory 2 | * 3 | # Except this file 4 | !.gitignore 5 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "mdepx"] 2 | path = mdepx 3 | url = https://github.com/machdep/mdepx 4 | [submodule "nrfxlib"] 5 | path = nrfxlib 6 | url = https://github.com/NordicPlayground/nrfxlib 7 | -------------------------------------------------------------------------------- /src-net/ldscript: -------------------------------------------------------------------------------- 1 | MEMORY 2 | { 3 | flash (rx) : ORIGIN = 0x01000000, LENGTH = 256K - 0x4000 4 | sram0 (rwx) : ORIGIN = 0x21000000, LENGTH = 48K 5 | sram1 (rwx) : ORIGIN = 0x2100c000, LENGTH = 16K /* malloc */ 6 | } 7 | 8 | ENTRY(md_init) 9 | SECTIONS 10 | { 11 | . = 0x01000000; 12 | .start . : { 13 | *start.o(.text); 14 | } > flash 15 | 16 | .text : { 17 | *(.exception); 18 | *(.text*); 19 | } > flash 20 | 21 | .sysinit : { 22 | __sysinit_start = ABSOLUTE(.); 23 | *(.sysinit) 24 | __sysinit_end = ABSOLUTE(.); 25 | } > flash 26 | 27 | .rodata : { 28 | *(.rodata*); 29 | } > flash 30 | 31 | /* Ensure _smem is associated with the next section */ 32 | . = .; 33 | _smem = ABSOLUTE(.); 34 | .data : { 35 | _sdata = ABSOLUTE(.); 36 | *(.data*); 37 | _edata = ABSOLUTE(.); 38 | } > sram0 AT > flash 39 | 40 | .bss : { 41 | _sbss = ABSOLUTE(.); 42 | *(.bss*) 43 | *(COMMON) 44 | _ebss = ABSOLUTE(.); 45 | } > sram0 46 | } 47 | -------------------------------------------------------------------------------- /src-app/ldscript: -------------------------------------------------------------------------------- 1 | MEMORY 2 | { 3 | flash (rx) : ORIGIN = 0x00040000, LENGTH = 1M - 0x40000 - 0x4000 4 | sram0 (rwx) : ORIGIN = 0x20000000, LENGTH = 16K /* boot loader */ 5 | sram1 (rwx) : ORIGIN = 0x20010000, LENGTH = 4K /* ring buffer */ 6 | sram2 (rwx) : ORIGIN = 0x20011000, LENGTH = 64K /* this app */ 7 | sram3 (rwx) : ORIGIN = 0x20021000, LENGTH = 0x1f000 /* malloc */ 8 | } 9 | 10 | ENTRY(__start) 11 | SECTIONS 12 | { 13 | . = 0x40000; 14 | .start . : { 15 | *start.o(.text); 16 | } > flash 17 | 18 | .text : { 19 | *(.exception); 20 | *(.text*); 21 | } > flash 22 | 23 | .sysinit : { 24 | __sysinit_start = ABSOLUTE(.); 25 | *(.sysinit) 26 | __sysinit_end = ABSOLUTE(.); 27 | } > flash 28 | 29 | .rodata : { 30 | *(.rodata*); 31 | } > flash 32 | 33 | /* Ensure _smem is associated with the next section */ 34 | . = .; 35 | _smem = ABSOLUTE(.); 36 | .data : { 37 | _sdata = ABSOLUTE(.); 38 | *(.data*); 39 | _edata = ABSOLUTE(.); 40 | } > sram2 AT > flash 41 | 42 | .bss : { 43 | _sbss = ABSOLUTE(.); 44 | *(.bss*) 45 | *(COMMON) 46 | _ebss = ABSOLUTE(.); 47 | } > sram2 48 | } 49 | -------------------------------------------------------------------------------- /src-net/nrf.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2019-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef _NRF_H_ 28 | #define _NRF_H_ 29 | 30 | typedef uint32_t IRQn_Type; 31 | 32 | #endif /* !_NRF_H_ */ 33 | -------------------------------------------------------------------------------- /src-app/ble.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2019-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef _SRC_BLE_H_ 28 | #define _SRC_BLE_H_ 29 | 30 | int ble_test(void); 31 | void ble_ipc_intr(void *arg); 32 | 33 | #endif /* !_SRC_BLE_H_ */ 34 | -------------------------------------------------------------------------------- /src-net/ble.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2019-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef _SRC_BLE_H_ 28 | #define _SRC_BLE_H_ 29 | 30 | int ble_test(void); 31 | void ble_ipc_intr(void *arg); 32 | 33 | #endif /* !_SRC_BLE_H_ */ 34 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | APP = nrf5340 2 | 3 | OSDIR = mdepx 4 | 5 | CMD = python3 -B ${OSDIR}/tools/emitter.py 6 | 7 | all: app net 8 | 9 | app: 10 | @${CMD} -j src-app/mdepx.conf 11 | ${CROSS_COMPILE}objcopy -O ihex obj/${APP}-app.elf obj/${APP}-app.hex 12 | ${CROSS_COMPILE}size obj/${APP}-app.elf 13 | 14 | net: 15 | @${CMD} -j src-net/mdepx.conf 16 | ${CROSS_COMPILE}objcopy -O ihex obj/${APP}-net.elf obj/${APP}-net.hex 17 | ${CROSS_COMPILE}size obj/${APP}-net.elf 18 | 19 | dtb-app: 20 | cpp -nostdinc -Imdepx/dts -Imdepx/dts/arm -Imdepx/dts/common \ 21 | -Imdepx/dts/include -undef, -x assembler-with-cpp \ 22 | nrf5340_app.dts -O obj/nrf5340_app.dts 23 | dtc -I dts -O dtb obj/nrf5340_app.dts -o obj/nrf5340_app.dtb 24 | bin2hex.py --offset=1015808 obj/nrf5340_app.dtb obj/nrf5340_app.dtb.hex 25 | nrfjprog -f NRF53 --erasepage 0xf8000-0xfc000 26 | nrfjprog -f NRF53 --program obj/nrf5340_app.dtb.hex -r 27 | 28 | dtb-net: 29 | cpp -nostdinc -Imdepx/dts -Imdepx/dts/arm -Imdepx/dts/common \ 30 | -Imdepx/dts/include -undef, -x assembler-with-cpp \ 31 | nrf5340_net.dts -O obj/nrf5340_net.dts 32 | dtc -I dts -O dtb obj/nrf5340_net.dts -o obj/nrf5340_net.dtb 33 | bin2hex.py --offset=17022976 obj/nrf5340_net.dtb obj/nrf5340_net.dtb.hex 34 | nrfjprog -f NRF53 --coprocessor CP_NETWORK \ 35 | --erasepage 0x0103c000-0x01040000 36 | nrfjprog -f NRF53 --coprocessor CP_NETWORK \ 37 | --program obj/nrf5340_net.dtb.hex -r 38 | 39 | flash-app: 40 | nrfjprog -f NRF53 --erasepage 0x40000-0x60000 41 | nrfjprog -f NRF53 --program obj/nrf5340-app.hex -r 42 | 43 | flash-net: 44 | nrfjprog -f NRF53 --coprocessor CP_NETWORK \ 45 | --erasepage 0x01000000-0x0103c000 46 | nrfjprog -f NRF53 --coprocessor CP_NETWORK \ 47 | --program obj/nrf5340-net.hex -r 48 | 49 | reset: 50 | nrfjprog -f NRF53 -r 51 | 52 | clean: 53 | @rm -rf obj/* 54 | 55 | include ${OSDIR}/mk/user.mk 56 | -------------------------------------------------------------------------------- /common.h: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2019-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #ifndef _COMMON_H_ 28 | #define _COMMON_H_ 29 | 30 | #define RINGBUF_TX_BASE 0x20010000 31 | #define RINGBUF_TX_BASE_SIZE 0x100 32 | #define RINGBUF_TX_BUF 0x20010100 33 | #define RINGBUF_TX_BUF_SIZE (2048 - 0x100) 34 | 35 | #define RINGBUF_RX_BASE 0x20010800 36 | #define RINGBUF_RX_BASE_SIZE 0x100 37 | #define RINGBUF_RX_BUF 0x20010900 38 | #define RINGBUF_RX_BUF_SIZE (2048 - 0x100) 39 | 40 | #endif /* !_COMMON_H_ */ 41 | -------------------------------------------------------------------------------- /nrf5340_net.dts: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | /dts-v1/; 28 | 29 | #include 30 | 31 | / { 32 | model = "nrf5340"; 33 | compatible = "nordic,nrf5340"; 34 | 35 | chosen { 36 | mdepx,console = &uart0; 37 | }; 38 | }; 39 | 40 | &uart0 { 41 | status = "okay"; 42 | current-speed = <115200>; 43 | tx-pin = <25>; 44 | rx-pin = <26>; 45 | rts-pin = <0>; 46 | cts-pin = <0>; 47 | }; 48 | 49 | &timer1 { 50 | status = "okay"; 51 | }; 52 | -------------------------------------------------------------------------------- /nrf5340_app.dts: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | /dts-v1/; 28 | 29 | #include 30 | 31 | / { 32 | model = "nrf5340"; 33 | compatible = "nordic,nrf5340"; 34 | 35 | chosen { 36 | mdepx,console = &uart0; 37 | }; 38 | }; 39 | 40 | &uart0 { 41 | status = "okay"; 42 | current-speed = <115200>; 43 | tx-pin = <20>; 44 | rx-pin = <22>; 45 | rts-pin = <0>; 46 | cts-pin = <0>; 47 | }; 48 | 49 | &timer0 { 50 | status = "okay"; 51 | }; 52 | 53 | &gpio0 { 54 | status = "okay"; 55 | }; 56 | -------------------------------------------------------------------------------- /src-app/mdepx.conf: -------------------------------------------------------------------------------- 1 | modules src-app; 2 | modules mdepx; 3 | 4 | link ./src-app/ldscript obj/nrf5340-app.elf; 5 | 6 | set-build-flags -mthumb 7 | -mcpu=cortex-m33 8 | -mfpu=fpv5-sp-d16 9 | -mfloat-abi=hard 10 | -g 11 | -nostdlib -nostdinc 12 | -fshort-enums 13 | -fno-builtin-printf 14 | -ffreestanding; 15 | 16 | set-build-flags -Wredundant-decls 17 | -Wnested-externs 18 | -Wstrict-prototypes 19 | -Wmissing-prototypes 20 | -Wpointer-arith 21 | -Winline 22 | -Wcast-qual 23 | -Wundef 24 | -Wmissing-include-dirs 25 | -Wall 26 | -Werror; 27 | 28 | src-app { 29 | append-search-path ../mdepx/arch 30 | ../mdepx/include 31 | ../mdepx/kernel 32 | ../mdepx/lib 33 | ../mdepx/lib/bluetooth 34 | ../mdepx 35 | ../; 36 | objects board.o main.o start.o ble.o; 37 | }; 38 | 39 | mdepx { 40 | modules lib; 41 | modules arch; 42 | modules kernel; 43 | modules dev; 44 | 45 | arch { 46 | modules arm; 47 | 48 | arm { 49 | modules nordicsemi; 50 | 51 | options vfp trustzone; 52 | 53 | nordicsemi { 54 | options nrf9160; 55 | }; 56 | }; 57 | }; 58 | 59 | dev { 60 | modules uart intc gpio; 61 | }; 62 | 63 | kernel { 64 | modules callout 65 | cpu 66 | malloc 67 | of 68 | sched 69 | systm 70 | thread 71 | time; 72 | 73 | callout { 74 | options usec_to_ticks_1mhz; 75 | }; 76 | 77 | malloc { 78 | options fl fl_wrapper; 79 | }; 80 | 81 | systm { 82 | options console ringbuf device; 83 | }; 84 | }; 85 | 86 | lib { 87 | modules aeabi_softfloat 88 | bluetooth 89 | ftoa 90 | libaeabi 91 | libc 92 | libfdt 93 | softfloat; 94 | 95 | libc { 96 | modules stdio string stdlib time; 97 | }; 98 | 99 | softfloat { 100 | modules source; 101 | 102 | source { 103 | modules ARM-VFPv2; 104 | }; 105 | 106 | options armvfpv2; 107 | }; 108 | }; 109 | }; 110 | -------------------------------------------------------------------------------- /src-net/mdepx.conf: -------------------------------------------------------------------------------- 1 | modules mdepx; 2 | modules nrfxlib; 3 | modules src-net; 4 | 5 | link ./src-net/ldscript obj/nrf5340-net.elf; 6 | 7 | set-build-flags -mthumb 8 | -mcpu=cortex-m33 9 | -g 10 | -nostdlib -nostdinc 11 | -fshort-enums 12 | -fno-builtin-printf 13 | -ffreestanding; 14 | 15 | set-build-flags -Wredundant-decls 16 | -Wnested-externs 17 | -Wstrict-prototypes 18 | -Wmissing-prototypes 19 | -Wpointer-arith 20 | -Winline 21 | -Wcast-qual 22 | -Wundef 23 | -Wmissing-include-dirs 24 | -Wall 25 | -Werror; 26 | 27 | src-net { 28 | append-search-path ../mdepx/arch 29 | ../mdepx/include 30 | ../mdepx/kernel 31 | ../mdepx/lib/bluetooth 32 | ../mdepx 33 | ../ ./; 34 | objects board.o main.o start.o ble.o; 35 | }; 36 | 37 | nrfxlib { 38 | modules ble_controller; 39 | modules mpsl; 40 | 41 | ble_controller { 42 | modules lib; 43 | 44 | lib { 45 | modules cortex-m33+nodsp; 46 | 47 | cortex-m33+nodsp { 48 | objects soft-float/libble_controller_s140.a; 49 | }; 50 | }; 51 | }; 52 | 53 | mpsl { 54 | objects lib/cortex-m33+nodsp/soft-float/libmpsl.a; 55 | }; 56 | }; 57 | 58 | mdepx { 59 | modules arch; 60 | modules kernel; 61 | modules dev; 62 | modules lib; 63 | 64 | arch { 65 | modules arm; 66 | 67 | arm { 68 | modules nordicsemi; 69 | 70 | options trustzone; 71 | 72 | nordicsemi { 73 | options nrf9160; 74 | }; 75 | }; 76 | }; 77 | 78 | dev { 79 | modules uart intc; 80 | }; 81 | 82 | kernel { 83 | modules cpu; 84 | modules callout; 85 | modules malloc; 86 | modules of; 87 | modules systm; 88 | modules thread; 89 | modules sched; 90 | modules time; 91 | 92 | callout { 93 | options usec_to_ticks_1mhz; 94 | }; 95 | 96 | malloc { 97 | options fl fl_wrapper; 98 | }; 99 | 100 | systm { 101 | options console ringbuf device; 102 | }; 103 | }; 104 | 105 | lib { 106 | modules libc; 107 | modules libfdt; 108 | modules libaeabi; 109 | 110 | libc { 111 | modules stdio string stdlib; 112 | }; 113 | }; 114 | }; 115 | -------------------------------------------------------------------------------- /src-app/board.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2018-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | #include 35 | 36 | #include 37 | 38 | void 39 | board_init(void) 40 | { 41 | mdx_device_t nvic; 42 | 43 | mdx_fl_init(); 44 | mdx_fl_add_region((void *)0x20021000, 0x1f000); 45 | 46 | mdx_of_install_dtbp((void *)0xf8000); 47 | mdx_of_probe_devices(); 48 | 49 | nvic = mdx_device_lookup_by_name("nvic", 0); 50 | if (nvic == NULL) 51 | panic("could not find nvic device"); 52 | 53 | mdx_intc_set_prio(nvic, ID_IPC, 6); 54 | 55 | printf("mdepx initialized\n"); 56 | } 57 | -------------------------------------------------------------------------------- /src-net/board.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2019-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include 37 | 38 | #include 39 | 40 | void 41 | board_init(void) 42 | { 43 | mdx_device_t nvic; 44 | 45 | mdx_fl_init(); 46 | mdx_fl_add_region((void *)0x2100c000, 16 * 1024); 47 | 48 | mdx_of_install_dtbp((void *)0x0103c000); 49 | mdx_of_probe_devices(); 50 | 51 | nvic = mdx_device_lookup_by_name("nvic", 0); 52 | if (nvic == NULL) 53 | panic("could not find nvic device"); 54 | 55 | mdx_intc_enable(nvic, ID_EGU0); 56 | mdx_intc_enable(nvic, ID_IPC); 57 | 58 | printf("mdepx initialized\n"); 59 | } 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nRF5340 2 | 3 | This is a demo for Bluetooth LE on nRF5340. 4 | Nordic libble controller s140 is used in this demo. 5 | 6 | Current status: advertising, establishing connection and pairing works fine. Re-pair after successful pair/unpair works fine too. 7 | 8 | Bluetooth LE supported only. 9 | Tested with Xiaomi MI9 Lite (android), not tested with iPhones. 10 | 11 | This application requests current time from android using bluetooth CTS (current time service) profile. 12 | 13 | GATT CTS is provided by nRF Connect application for Android 14 | (install it as androids do not have built-in CTS). 15 | 16 | BLE host-layer library is included to the main core app, Nordic link-layer library has set up on the network core. 17 | 18 | Nordicsemi nRF5340 is a dual-core ARM Cortex-M33. 19 | Network core is reserved entirely for real-time (bluetooth link) control. Application core is available for your code. 20 | 21 | Ring-buffer (in shared SRAM memory) has been established between two cores. It is used for transmitting HCI commands between bluetooth HCI library (mdepx/lib/bluetooth) and libble controller s140. 22 | 23 | For nRF5340-DK (application core) connect micro usb cable to J2, for other boards connect UART pins as follows: 24 | 25 | | nRF5340 | UART-to-USB adapter | 26 | | ----------------- | -------------------- | 27 | | P0.22 (TX) | RX | 28 | | P0.20 (RX) | TX | 29 | 30 | Network MCU has separate UART: 31 | 32 | | nRF5340 | UART-to-USB adapter | 33 | | ----------------- | -------------------- | 34 | | P0.25 (TX) | RX | 35 | | P0.26 (RX) | TX | 36 | 37 | UART baud rate: 115200 38 | 39 | Use on-board debugger or plug Segger JLINK adapter to nRF5340-DK 'Debug in' connector. 40 | 41 | This app depends on the [secure bootloader for nRF5340](https://github.com/machdep/nrf-boot). 42 | 43 | ### Under Linux 44 | $ sudo apt install gcc-arm-linux-gnueabi 45 | $ export CROSS_COMPILE=arm-linux-gnueabi- 46 | 47 | ### Under FreeBSD 48 | $ sudo pkg install arm-none-eabi-gcc arm-none-eabi-binutils 49 | $ export CROSS_COMPILE=arm-none-eabi- 50 | 51 | ### Build 52 | $ git clone --recursive https://github.com/machdep/nrf5340 53 | $ cd nrf5340 54 | $ make app net 55 | 56 | ## Program the network core using nrfjprog 57 | 58 | $ nrfjprog -f NRF53 --coprocessor CP_NETWORK --erasepage 0x01000000-0x01040000 59 | $ nrfjprog -f NRF53 --coprocessor CP_NETWORK --program obj/nrf5340-net.hex -r 60 | 61 | ## Program the application core using nrfjprog 62 | $ nrfjprog -f NRF53 --erasepage 0x40000-0x90000 63 | $ nrfjprog -f NRF53 --program obj/nrf5340-app.hex -r 64 | 65 | Note: both cores has to be reset after programming due to ringbuffer mechanism (both master and slave have to be re-initialized). 66 | 67 | ![alt text](https://raw.githubusercontent.com/machdep/nrf5340/master/images/nrf5340-pdk.jpg) 68 | -------------------------------------------------------------------------------- /src-app/main.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2019-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include 37 | 38 | extern struct arm_nvic_softc nvic_sc; 39 | 40 | struct mdx_ringbuf_softc ringbuf_tx_sc; 41 | struct mdx_ringbuf_softc ringbuf_rx_sc; 42 | 43 | #include 44 | #include 45 | 46 | #include "ble.h" 47 | 48 | static void 49 | ipc_config(void) 50 | { 51 | mdx_device_t nvic; 52 | mdx_device_t ipc; 53 | 54 | nvic = mdx_device_lookup_by_name("nvic", 0); 55 | if (nvic == NULL) 56 | panic("could not find nvic device"); 57 | 58 | ipc = mdx_device_lookup_by_name("nrf_ipc", 0); 59 | if (ipc == NULL) 60 | panic("could not find ipc device"); 61 | 62 | /* Receive event 1 on channel 1 */ 63 | nrf_ipc_configure_recv(ipc, 1, (1 << 1), ble_ipc_intr, NULL); 64 | nrf_ipc_inten(ipc, 1, true); 65 | 66 | /* Send event 0 to channel 0 */ 67 | nrf_ipc_configure_send(ipc, 0, (1 << 0)); 68 | } 69 | 70 | int 71 | main(void) 72 | { 73 | 74 | printf("Hello world!\n"); 75 | 76 | #if 0 77 | double a; 78 | double b; 79 | double d; 80 | 81 | a = 0.12; 82 | b = 0.13; 83 | d = a + b; 84 | printf("d is %.2f\n", d); 85 | #endif 86 | 87 | ipc_config(); 88 | 89 | /* Give some time for the NET core to startup. */ 90 | mdx_usleep(500000); 91 | 92 | ble_test(); 93 | 94 | while (1) 95 | mdx_usleep(2000000); 96 | 97 | return (0); 98 | } 99 | -------------------------------------------------------------------------------- /src-net/main.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2019-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include 37 | #include 38 | 39 | #include 40 | 41 | #include 42 | #include 43 | 44 | #include "common.h" 45 | #include "ble.h" 46 | 47 | extern struct arm_nvic_softc nvic_sc; 48 | 49 | struct mdx_ringbuf_softc ringbuf_tx_sc; 50 | struct mdx_ringbuf_softc ringbuf_rx_sc; 51 | 52 | static void 53 | ble_rng_intr(void *arg, int irq) 54 | { 55 | 56 | ble_controller_RNG_IRQHandler(); 57 | } 58 | 59 | static void 60 | ble_timer_intr(void *arg, int irq) 61 | { 62 | 63 | MPSL_IRQ_TIMER0_Handler(); 64 | } 65 | 66 | static void 67 | ble_radio_intr(void *arg, int irq) 68 | { 69 | 70 | MPSL_IRQ_RADIO_Handler(); 71 | } 72 | 73 | static void 74 | ble_rtc_intr(void *arg, int irq) 75 | { 76 | 77 | MPSL_IRQ_RTC0_Handler(); 78 | } 79 | 80 | static void 81 | ble_power_intr(void *arg, int irq) 82 | { 83 | 84 | MPSL_IRQ_CLOCK_Handler(); 85 | } 86 | 87 | static void 88 | nrf_egu0_intr(void *arg, int irq) 89 | { 90 | 91 | mpsl_low_priority_process(); 92 | } 93 | 94 | static void 95 | config_ipc(void) 96 | { 97 | mdx_device_t nvic, ipc; 98 | 99 | nvic = mdx_device_lookup_by_name("nvic", 0); 100 | if (nvic == NULL) 101 | panic("nvic device not found"); 102 | 103 | ipc = mdx_device_lookup_by_name("nrf_ipc", 0); 104 | if (ipc == NULL) 105 | panic("ipc device not found"); 106 | 107 | /* Send event 1 to channel 1 */ 108 | nrf_ipc_configure_send(ipc, 1, (1 << 1)); 109 | 110 | /* Receive event 0 on channel 0 */ 111 | nrf_ipc_configure_recv(ipc, 0, (1 << 0), ble_ipc_intr, NULL); 112 | nrf_ipc_inten(ipc, 0, true); 113 | } 114 | 115 | int 116 | main(void) 117 | { 118 | mdx_device_t nvic; 119 | 120 | printf("Hello world!\n"); 121 | 122 | config_ipc(); 123 | 124 | nvic = mdx_device_lookup_by_name("nvic", 0); 125 | if (nvic == NULL) 126 | panic("nvic device not found"); 127 | 128 | mdx_intc_setup(nvic, ID_EGU0, nrf_egu0_intr, NULL); 129 | mdx_intc_setup(nvic, ID_RNG, ble_rng_intr, NULL); 130 | mdx_intc_setup(nvic, ID_TIMER0, ble_timer_intr, NULL); 131 | mdx_intc_setup(nvic, ID_RADIO, ble_radio_intr, NULL); 132 | mdx_intc_setup(nvic, ID_RTC0, ble_rtc_intr, NULL); 133 | mdx_intc_setup(nvic, ID_POWER, ble_power_intr, NULL); 134 | 135 | mdx_ringbuf_init(&ringbuf_rx_sc, 136 | (void *)RINGBUF_RX_BASE, RINGBUF_RX_BASE_SIZE, 137 | (void *)RINGBUF_RX_BUF, RINGBUF_RX_BUF_SIZE); 138 | mdx_ringbuf_join(&ringbuf_tx_sc, 139 | (void *)RINGBUF_TX_BASE); 140 | 141 | ble_test(); 142 | 143 | while (1) 144 | mdx_usleep(2000000); 145 | 146 | return (0); 147 | } 148 | -------------------------------------------------------------------------------- /src-app/start.S: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2018-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | .cpu cortex-m33 28 | .syntax unified 29 | .thumb 30 | 31 | .word idle_thread_stack + MDX_THREAD_STACK_SIZE /* Stack top address */ 32 | .word __start /* Reset */ 33 | .word exception /* NMI */ 34 | .word exception /* Hard Fault */ 35 | .word exception /* mm */ 36 | .word exception /* Bus Fault */ 37 | .word exception /* Usage Fault */ 38 | .word exception /* Reserved */ 39 | .word exception /* Reserved */ 40 | .word exception /* Reserved */ 41 | .word exception /* Reserved */ 42 | .word exception /* SVCall */ 43 | .word exception /* Reserved */ 44 | .word exception /* Reserved */ 45 | .word exception /* Pend */ 46 | .word exception /* Systick */ 47 | .word exception /* IRQ 0 */ 48 | .word exception /* IRQ 1 */ 49 | .word exception /* IRQ 2 */ 50 | .word exception /* IRQ 3 */ 51 | .word exception /* IRQ 4 */ 52 | .word exception /* IRQ 5 */ 53 | .word exception /* IRQ 6 */ 54 | .word exception /* IRQ 7 */ 55 | .word exception /* IRQ 8 */ 56 | .word exception /* IRQ 9 */ 57 | .word exception /* IRQ 10 */ 58 | .word exception /* IRQ 11 */ 59 | .word exception /* IRQ 12 */ 60 | .word exception /* IRQ 13 */ 61 | .word exception /* IRQ 14 */ 62 | .word exception /* IRQ 15 */ 63 | .word exception /* IRQ 16 */ 64 | .word exception /* IRQ 17 */ 65 | .word exception /* IRQ 18 */ 66 | .word exception /* IRQ 19 */ 67 | .word exception /* IRQ 20 */ 68 | .word exception /* IRQ 21 */ 69 | .word exception /* IRQ 22 */ 70 | .word exception /* IRQ 23 */ 71 | .word exception /* IRQ 24 */ 72 | .word exception /* IRQ 25 */ 73 | .word exception /* IRQ 26 */ 74 | .word exception /* IRQ 27 */ 75 | .word exception /* IRQ 28 */ 76 | .word exception /* IRQ 29 */ 77 | .word exception /* IRQ 30 */ 78 | .word exception /* IRQ 31 */ 79 | .word exception /* IRQ 32 */ 80 | .word exception /* IRQ 33 */ 81 | .word exception /* IRQ 34 */ 82 | .word exception /* IRQ 35 */ 83 | .word exception /* IRQ 36 */ 84 | .word exception /* IRQ 37 */ 85 | .word exception /* IRQ 38 */ 86 | .word exception /* IRQ 39 */ 87 | .word exception /* IRQ 40 */ 88 | .word exception /* IRQ 41 */ 89 | .word exception /* IRQ 42 */ 90 | .word exception /* IRQ 43 */ 91 | .word exception /* IRQ 44 */ 92 | .word exception /* IRQ 45 */ 93 | .word exception /* IRQ 46 */ 94 | .word exception /* IRQ 47 */ 95 | .word exception /* IRQ 48 */ 96 | .word exception /* IRQ 49 */ 97 | .word exception /* IRQ 50 */ 98 | .word exception /* IRQ 51 */ 99 | .word exception /* IRQ 52 */ 100 | .word exception /* IRQ 53 */ 101 | .word exception /* IRQ 54 */ 102 | .word exception /* IRQ 55 */ 103 | .word exception /* IRQ 56 */ 104 | .word exception /* IRQ 57 */ 105 | .word exception /* IRQ 58 */ 106 | .word exception /* IRQ 59 */ 107 | .word exception /* IRQ 60 */ 108 | .word exception /* IRQ 61 */ 109 | .word exception /* IRQ 62 */ 110 | .word exception /* IRQ 63 */ 111 | .word exception /* IRQ 64 */ 112 | .word exception /* IRQ 65 */ 113 | .word exception /* IRQ 66 */ 114 | .word exception /* IRQ 67 */ 115 | .word exception /* IRQ 68 */ 116 | .word exception /* IRQ 69 */ 117 | .word exception /* IRQ 70 */ 118 | .word exception /* IRQ 71 */ 119 | .word exception /* IRQ 72 */ 120 | .word exception /* IRQ 73 */ 121 | .word exception /* IRQ 74 */ 122 | .word exception /* IRQ 75 */ 123 | .word exception /* IRQ 76 */ 124 | .word exception /* IRQ 77 */ 125 | .word exception /* IRQ 78 */ 126 | .word exception /* IRQ 79 */ 127 | .word exception /* IRQ 80 */ 128 | .word exception /* IRQ 81 */ 129 | .word exception /* IRQ 82 */ 130 | .word exception /* IRQ 83 */ 131 | .word exception /* IRQ 84 */ 132 | .word exception /* IRQ 85 */ 133 | .word exception /* IRQ 86 */ 134 | .word exception /* IRQ 87 */ 135 | .word exception /* IRQ 88 */ 136 | .word exception /* IRQ 89 */ 137 | .word exception /* IRQ 90 */ 138 | .word exception /* IRQ 91 */ 139 | .word exception /* IRQ 92 */ 140 | .word exception /* IRQ 93 */ 141 | .word exception /* IRQ 94 */ 142 | .word exception /* IRQ 95 */ 143 | .word exception /* IRQ 96 */ 144 | .word exception /* IRQ 97 */ 145 | .word exception /* IRQ 98 */ 146 | .word exception /* IRQ 99 */ 147 | .word exception /* IRQ 100 */ 148 | .word exception /* IRQ 101 */ 149 | .word exception /* IRQ 102 */ 150 | .word exception /* IRQ 103 */ 151 | .word exception /* IRQ 104 */ 152 | .word exception /* IRQ 105 */ 153 | .word exception /* IRQ 106 */ 154 | .word exception /* IRQ 107 */ 155 | .word exception /* IRQ 108 */ 156 | .word exception /* IRQ 109 */ 157 | .word exception /* IRQ 110 */ 158 | .word exception /* IRQ 111 */ 159 | .word exception /* IRQ 112 */ 160 | .word exception /* IRQ 113 */ 161 | .word exception /* IRQ 114 */ 162 | .word exception /* IRQ 115 */ 163 | .word exception /* IRQ 116 */ 164 | .word exception /* IRQ 117 */ 165 | .word exception /* IRQ 118 */ 166 | .word exception /* IRQ 119 */ 167 | .word exception /* IRQ 120 */ 168 | .word exception /* IRQ 121 */ 169 | .word exception /* IRQ 122 */ 170 | .word exception /* IRQ 123 */ 171 | .word exception /* IRQ 124 */ 172 | .word exception /* IRQ 125 */ 173 | .word exception /* IRQ 126 */ 174 | .word exception /* IRQ 127 */ 175 | -------------------------------------------------------------------------------- /src-net/start.S: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2018-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | .cpu cortex-m33 28 | .syntax unified 29 | .thumb 30 | 31 | .word idle_thread_stack + MDX_THREAD_STACK_SIZE /* Stack top address */ 32 | .word __start /* Reset */ 33 | .word exception /* NMI */ 34 | .word exception /* Hard Fault */ 35 | .word exception /* mm */ 36 | .word exception /* Bus Fault */ 37 | .word exception /* Usage Fault */ 38 | .word exception /* Reserved */ 39 | .word exception /* Reserved */ 40 | .word exception /* Reserved */ 41 | .word exception /* Reserved */ 42 | .word exception /* SVCall */ 43 | .word exception /* Reserved */ 44 | .word exception /* Reserved */ 45 | .word exception /* Pend */ 46 | .word exception /* Systick */ 47 | .word exception /* IRQ 0 */ 48 | .word exception /* IRQ 1 */ 49 | .word exception /* IRQ 2 */ 50 | .word exception /* IRQ 3 */ 51 | .word exception /* IRQ 4 */ 52 | .word exception /* IRQ 5 */ 53 | .word exception /* IRQ 6 */ 54 | .word exception /* IRQ 7 */ 55 | .word exception /* IRQ 8 */ 56 | .word exception /* IRQ 9 */ 57 | .word exception /* IRQ 10 */ 58 | .word exception /* IRQ 11 */ 59 | .word exception /* IRQ 12 */ 60 | .word exception /* IRQ 13 */ 61 | .word exception /* IRQ 14 */ 62 | .word exception /* IRQ 15 */ 63 | .word exception /* IRQ 16 */ 64 | .word exception /* IRQ 17 */ 65 | .word exception /* IRQ 18 */ 66 | .word exception /* IRQ 19 */ 67 | .word exception /* IRQ 20 */ 68 | .word exception /* IRQ 21 */ 69 | .word exception /* IRQ 22 */ 70 | .word exception /* IRQ 23 */ 71 | .word exception /* IRQ 24 */ 72 | .word exception /* IRQ 25 */ 73 | .word exception /* IRQ 26 */ 74 | .word exception /* IRQ 27 */ 75 | .word exception /* IRQ 28 */ 76 | .word exception /* IRQ 29 */ 77 | .word exception /* IRQ 30 */ 78 | .word exception /* IRQ 31 */ 79 | .word exception /* IRQ 32 */ 80 | .word exception /* IRQ 33 */ 81 | .word exception /* IRQ 34 */ 82 | .word exception /* IRQ 35 */ 83 | .word exception /* IRQ 36 */ 84 | .word exception /* IRQ 37 */ 85 | .word exception /* IRQ 38 */ 86 | .word exception /* IRQ 39 */ 87 | .word exception /* IRQ 40 */ 88 | .word exception /* IRQ 41 */ 89 | .word exception /* IRQ 42 */ 90 | .word exception /* IRQ 43 */ 91 | .word exception /* IRQ 44 */ 92 | .word exception /* IRQ 45 */ 93 | .word exception /* IRQ 46 */ 94 | .word exception /* IRQ 47 */ 95 | .word exception /* IRQ 48 */ 96 | .word exception /* IRQ 49 */ 97 | .word exception /* IRQ 50 */ 98 | .word exception /* IRQ 51 */ 99 | .word exception /* IRQ 52 */ 100 | .word exception /* IRQ 53 */ 101 | .word exception /* IRQ 54 */ 102 | .word exception /* IRQ 55 */ 103 | .word exception /* IRQ 56 */ 104 | .word exception /* IRQ 57 */ 105 | .word exception /* IRQ 58 */ 106 | .word exception /* IRQ 59 */ 107 | .word exception /* IRQ 60 */ 108 | .word exception /* IRQ 61 */ 109 | .word exception /* IRQ 62 */ 110 | .word exception /* IRQ 63 */ 111 | .word exception /* IRQ 64 */ 112 | .word exception /* IRQ 65 */ 113 | .word exception /* IRQ 66 */ 114 | .word exception /* IRQ 67 */ 115 | .word exception /* IRQ 68 */ 116 | .word exception /* IRQ 69 */ 117 | .word exception /* IRQ 70 */ 118 | .word exception /* IRQ 71 */ 119 | .word exception /* IRQ 72 */ 120 | .word exception /* IRQ 73 */ 121 | .word exception /* IRQ 74 */ 122 | .word exception /* IRQ 75 */ 123 | .word exception /* IRQ 76 */ 124 | .word exception /* IRQ 77 */ 125 | .word exception /* IRQ 78 */ 126 | .word exception /* IRQ 79 */ 127 | .word exception /* IRQ 80 */ 128 | .word exception /* IRQ 81 */ 129 | .word exception /* IRQ 82 */ 130 | .word exception /* IRQ 83 */ 131 | .word exception /* IRQ 84 */ 132 | .word exception /* IRQ 85 */ 133 | .word exception /* IRQ 86 */ 134 | .word exception /* IRQ 87 */ 135 | .word exception /* IRQ 88 */ 136 | .word exception /* IRQ 89 */ 137 | .word exception /* IRQ 90 */ 138 | .word exception /* IRQ 91 */ 139 | .word exception /* IRQ 92 */ 140 | .word exception /* IRQ 93 */ 141 | .word exception /* IRQ 94 */ 142 | .word exception /* IRQ 95 */ 143 | .word exception /* IRQ 96 */ 144 | .word exception /* IRQ 97 */ 145 | .word exception /* IRQ 98 */ 146 | .word exception /* IRQ 99 */ 147 | .word exception /* IRQ 100 */ 148 | .word exception /* IRQ 101 */ 149 | .word exception /* IRQ 102 */ 150 | .word exception /* IRQ 103 */ 151 | .word exception /* IRQ 104 */ 152 | .word exception /* IRQ 105 */ 153 | .word exception /* IRQ 106 */ 154 | .word exception /* IRQ 107 */ 155 | .word exception /* IRQ 108 */ 156 | .word exception /* IRQ 109 */ 157 | .word exception /* IRQ 110 */ 158 | .word exception /* IRQ 111 */ 159 | .word exception /* IRQ 112 */ 160 | .word exception /* IRQ 113 */ 161 | .word exception /* IRQ 114 */ 162 | .word exception /* IRQ 115 */ 163 | .word exception /* IRQ 116 */ 164 | .word exception /* IRQ 117 */ 165 | .word exception /* IRQ 118 */ 166 | .word exception /* IRQ 119 */ 167 | .word exception /* IRQ 120 */ 168 | .word exception /* IRQ 121 */ 169 | .word exception /* IRQ 122 */ 170 | .word exception /* IRQ 123 */ 171 | .word exception /* IRQ 124 */ 172 | .word exception /* IRQ 125 */ 173 | .word exception /* IRQ 126 */ 174 | .word exception /* IRQ 127 */ 175 | -------------------------------------------------------------------------------- /src-app/ble.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2019-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #include 38 | 39 | #include 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | 47 | #include 48 | #include 49 | 50 | #include "common.h" 51 | #include "ble.h" 52 | 53 | extern struct nrf_ipc_softc ipc_sc; 54 | extern struct mdx_ringbuf_softc ringbuf_tx_sc; 55 | extern struct mdx_ringbuf_softc ringbuf_rx_sc; 56 | 57 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 58 | 59 | static struct bt_conn *g_conn; 60 | static struct mdx_callout c; 61 | static struct bt_gatt_discover_params params; 62 | static struct bt_uuid lt_uuid = { 63 | .type = BT_UUID_16, 64 | .u16 = BT_UUID_CTS_CURRENT_TIME, 65 | }; 66 | 67 | static const struct bt_eir ad[] = { 68 | { 69 | .len = 2, 70 | .type = BT_EIR_FLAGS, 71 | .data = { BT_LE_AD_LIMITED | BT_LE_AD_NO_BREDR }, 72 | }, 73 | { 74 | .len = 7, 75 | .type = BT_EIR_UUID16_ALL, 76 | .data = { 0x0d, 0x18, 0x0f, 0x18, 0x05, 0x18 }, 77 | }, 78 | { 79 | .len = 17, 80 | .type = BT_EIR_UUID128_ALL, 81 | .data = { 0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12, 82 | 0x78, 0x56, 0x34, 0x12, 0x78, 0x56, 0x34, 0x12 }, 83 | }, 84 | { } 85 | }; 86 | 87 | static const struct bt_eir sd[] = { 88 | { 89 | .len = 6, 90 | .type = BT_EIR_NAME_COMPLETE, 91 | .data = "mdepx", 92 | }, 93 | { 94 | .len = 5, 95 | .type = BT_EIR_NAME_SHORTENED, 96 | .data = "mdep", 97 | }, 98 | { } 99 | }; 100 | 101 | static void 102 | ble_recv(void) 103 | { 104 | struct mdx_ringbuf *rb; 105 | struct bt_buf *buf; 106 | int err; 107 | 108 | while (1) { 109 | err = mdx_ringbuf_head(&ringbuf_rx_sc, &rb); 110 | if (err) 111 | break; 112 | 113 | buf = bt_buf_get(rb->user, 0); 114 | if (!buf) 115 | panic("no bufs"); 116 | 117 | memcpy(bt_buf_tail(buf), rb->buf, rb->fill); 118 | buf->len += rb->fill; 119 | 120 | #if 0 121 | struct bt_hci_evt_hdr *evt; 122 | if (rb->user == 1) { 123 | evt = (struct bt_hci_evt_hdr *)buf->data; 124 | printf("%s: evt %x len %d buflen %d\n", 125 | __func__, evt->evt, evt->len, buf->len); 126 | } else 127 | printf("%s: acl buflen %d\n", 128 | __func__, buf->len); 129 | #endif 130 | 131 | bt_recv(buf); 132 | 133 | mdx_ringbuf_submit(&ringbuf_rx_sc); 134 | }; 135 | } 136 | 137 | void 138 | ble_ipc_intr(void *arg) 139 | { 140 | 141 | ble_recv(); 142 | } 143 | 144 | static int 145 | bt_send(struct bt_buf *buf) 146 | { 147 | struct mdx_ringbuf *rb; 148 | int err; 149 | 150 | err = mdx_ringbuf_head(&ringbuf_tx_sc, &rb); 151 | if (err) 152 | panic("could not send bufs: no ring entries available"); 153 | 154 | if (buf->len > rb->bufsize) 155 | panic("could not send bufs: ring entry buf is too small"); 156 | 157 | memcpy(rb->buf, buf->data, buf->len); 158 | rb->fill = buf->len; 159 | 160 | switch(buf->type) { 161 | case BT_ACL_OUT: 162 | rb->user = BT_ACL_OUT; 163 | break; 164 | case BT_CMD: 165 | rb->user = BT_CMD; 166 | break; 167 | default: 168 | panic("unknown type %d", buf->type); 169 | } 170 | 171 | mdx_ringbuf_submit(&ringbuf_tx_sc); 172 | 173 | /* Notify network core. */ 174 | mdx_device_t dev; 175 | 176 | dev = mdx_device_lookup_by_name("nrf_ipc", 0); 177 | if (dev == NULL) 178 | panic("could not find ipc device"); 179 | 180 | nrf_ipc_trigger(dev, 0); 181 | 182 | return (0); 183 | } 184 | 185 | static int 186 | bt_open(void) 187 | { 188 | 189 | return (0); 190 | } 191 | 192 | static struct bt_driver drv = { 193 | .head_reserve = 0, 194 | .open = bt_open, 195 | .send = bt_send, 196 | }; 197 | 198 | static void 199 | read_cts(struct bt_conn *conn, int err, 200 | const void *data, uint16_t length) 201 | { 202 | struct tm tm; 203 | const uint8_t *buf; 204 | struct timespec ts; 205 | uint16_t year; 206 | time_t t; 207 | 208 | buf = (const uint8_t *)data; 209 | 210 | memcpy(&year, buf, 2); /* year */ 211 | tm.tm_year = year - 1900; 212 | tm.tm_mon = buf[2] - 1; 213 | tm.tm_mday = buf[3]; 214 | tm.tm_hour = buf[4]; 215 | tm.tm_min = buf[5]; 216 | tm.tm_sec = buf[6]; 217 | 218 | t = mktime(&tm); 219 | 220 | printf("current time %d\n", t); 221 | 222 | ts.tv_sec = t; 223 | ts.tv_nsec = 0; 224 | 225 | mdx_clock_settime(CLOCK_REALTIME, &ts); 226 | 227 | bzero(&tm, sizeof(struct tm)); 228 | while (1) { 229 | mdx_clock_gettime(CLOCK_REALTIME, &ts); 230 | gmtime_r(&ts.tv_sec, &tm); 231 | 232 | printf("%s: %d/%d/%d %d:%d:%d\n", __func__, 233 | tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900, 234 | tm.tm_hour, tm.tm_min, tm.tm_sec); 235 | 236 | mdx_tsleep(1000000); 237 | } 238 | } 239 | 240 | static uint8_t 241 | discover_func(const struct bt_gatt_attr *attr, void *user_data) 242 | { 243 | uint16_t handle; 244 | uint16_t offset; 245 | int err; 246 | 247 | /* CTS descriptor found */ 248 | 249 | printf("%s: handle 0x%x user_data %p\n", 250 | __func__, attr->handle, user_data); 251 | 252 | handle = attr->handle; 253 | offset = 0; 254 | 255 | err = bt_gatt_read(g_conn, handle, offset, read_cts); 256 | if (err) 257 | printf("%s: err %d\n", __func__, err); 258 | 259 | return (BT_GATT_ITER_STOP); 260 | } 261 | 262 | static void 263 | discover_cts_descr(void *arg) 264 | { 265 | struct bt_conn *conn; 266 | int err; 267 | 268 | conn = arg; 269 | 270 | bzero(¶ms, sizeof(struct bt_gatt_discover_params)); 271 | params.uuid = <_uuid; 272 | params.func = discover_func; 273 | params.start_handle = 1; 274 | params.end_handle = 0xffff; 275 | err = bt_gatt_discover_descriptor(conn, ¶ms); 276 | if (err) 277 | printf("%s: err %d\n", __func__, err); 278 | } 279 | 280 | static void 281 | connected(struct bt_conn *conn) 282 | { 283 | 284 | printf("%s\n", __func__); 285 | 286 | g_conn = conn; 287 | 288 | mdx_callout_init(&c); 289 | mdx_callout_set(&c, 4000000, discover_cts_descr, conn); 290 | } 291 | 292 | static void 293 | disconnected(struct bt_conn *conn) 294 | { 295 | 296 | printf("%s\n", __func__); 297 | } 298 | 299 | static struct bt_conn_cb conn_callbacks = { 300 | .connected = connected, 301 | .disconnected = disconnected, 302 | }; 303 | 304 | static struct bt_uuid gap_uuid = { 305 | .type = BT_UUID_16, 306 | .u16 = BT_UUID_GAP, 307 | }; 308 | 309 | static struct bt_uuid device_name_uuid = { 310 | .type = BT_UUID_16, 311 | .u16 = BT_UUID_GAP_DEVICE_NAME, 312 | }; 313 | 314 | static struct bt_gatt_chrc name_chrc = { 315 | .properties = BT_GATT_CHRC_READ, 316 | .value_handle = 0x0003, 317 | .uuid = &device_name_uuid, 318 | }; 319 | 320 | static const struct bt_gatt_attr attrs[] = { 321 | BT_GATT_PRIMARY_SERVICE(0x0001, &gap_uuid), 322 | BT_GATT_CHARACTERISTIC(0x0002, &name_chrc), 323 | }; 324 | 325 | int 326 | ble_test(void) 327 | { 328 | bt_addr_t addr; 329 | int err; 330 | 331 | /* Setup ring buffer. */ 332 | mdx_ringbuf_init(&ringbuf_tx_sc, 333 | (void *)RINGBUF_TX_BASE, RINGBUF_TX_BASE_SIZE, 334 | (void *)RINGBUF_TX_BUF, RINGBUF_TX_BUF_SIZE); 335 | mdx_ringbuf_join(&ringbuf_rx_sc, 336 | (void *)RINGBUF_RX_BASE); 337 | 338 | bt_driver_register(&drv); 339 | 340 | err = bt_init(); 341 | if (err) 342 | panic("Failed to initialize bluetooth, err %d\n", err); 343 | 344 | printf("Bluetooth initialized\n"); 345 | 346 | addr.val[0] = 0x03; 347 | addr.val[1] = 0x01; 348 | addr.val[2] = 0x03; 349 | addr.val[3] = 0x04; 350 | addr.val[4] = 0x05; 351 | addr.val[5] = 0x03; 352 | 353 | hci_le_set_random_address(&addr); 354 | 355 | bt_gatt_register(attrs, ARRAY_SIZE(attrs)); 356 | 357 | err = bt_start_advertising(BT_LE_ADV_IND, ad, sd); 358 | if (err != 0) { 359 | printf("Could not start advertising. Error %d\n", err); 360 | return (MDX_ERROR); 361 | } 362 | 363 | bt_conn_cb_register(&conn_callbacks); 364 | 365 | return (MDX_OK); 366 | } 367 | -------------------------------------------------------------------------------- /src-net/ble.c: -------------------------------------------------------------------------------- 1 | /*- 2 | * Copyright (c) 2019-2020 Ruslan Bukin 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions 7 | * are met: 8 | * 1. Redistributions of source code must retain the above copyright 9 | * notice, this list of conditions and the following disclaimer. 10 | * 2. Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 | * SUCH DAMAGE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | #include 38 | #include 39 | 40 | #include 41 | #include 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | 48 | #include 49 | #include 50 | #include 51 | #include 52 | 53 | #include "ble.h" 54 | 55 | #define BLE_CONTROLLER_PROCESS_IRQn ID_EGU0 56 | #define BLE_MAX_CONN_EVENT_LEN_DEFAULT 7500 57 | 58 | #define MAX_TX_PKT_SIZE BLE_CONTROLLER_DEFAULT_TX_PACKET_SIZE 59 | #define MAX_RX_PKT_SIZE BLE_CONTROLLER_DEFAULT_RX_PACKET_SIZE 60 | #define BLE_SLAVE_COUNT 1 61 | #define BLE_MASTER_COUNT 1 62 | 63 | #define MASTER_MEM_SIZE (BLE_CONTROLLER_MEM_PER_MASTER_LINK( \ 64 | MAX_TX_PKT_SIZE, \ 65 | MAX_RX_PKT_SIZE, \ 66 | BLE_CONTROLLER_DEFAULT_TX_PACKET_COUNT, \ 67 | BLE_CONTROLLER_DEFAULT_RX_PACKET_COUNT) \ 68 | + BLE_CONTROLLER_MEM_MASTER_LINKS_SHARED) 69 | 70 | #define SLAVE_MEM_SIZE (BLE_CONTROLLER_MEM_PER_SLAVE_LINK( \ 71 | MAX_TX_PKT_SIZE, \ 72 | MAX_RX_PKT_SIZE, \ 73 | BLE_CONTROLLER_DEFAULT_TX_PACKET_COUNT, \ 74 | BLE_CONTROLLER_DEFAULT_RX_PACKET_COUNT) \ 75 | + BLE_CONTROLLER_MEM_SLAVE_LINKS_SHARED) 76 | 77 | #define MEMPOOL_SIZE ((BLE_SLAVE_COUNT * SLAVE_MEM_SIZE) + \ 78 | (BLE_MASTER_COUNT * MASTER_MEM_SIZE)) 79 | 80 | mdx_device_t ipc; 81 | 82 | extern struct mdx_ringbuf_softc ringbuf_tx_sc; 83 | extern struct mdx_ringbuf_softc ringbuf_rx_sc; 84 | 85 | #define BLE_STACK_SIZE 4096 86 | 87 | static struct thread recv_td; 88 | static struct thread send_td; 89 | static uint8_t recv_td_stack[BLE_STACK_SIZE]; 90 | static uint8_t send_td_stack[BLE_STACK_SIZE]; 91 | 92 | static uint8_t ble_controller_mempool[MEMPOOL_SIZE]; 93 | 94 | static mdx_sem_t sem_recv; 95 | static mdx_sem_t sem_send; 96 | static mdx_sem_t sem_thread; 97 | 98 | static void 99 | print_build_rev(void) 100 | { 101 | uint8_t ble_controller_rev[BLE_CONTROLLER_BUILD_REVISION_SIZE]; 102 | int i; 103 | 104 | ble_controller_build_revision_get(ble_controller_rev); 105 | 106 | printf("Build revision: "); 107 | for (i = 0; i < BLE_CONTROLLER_BUILD_REVISION_SIZE; i++) 108 | printf("%x", ble_controller_rev[i]); 109 | 110 | printf("\n"); 111 | } 112 | 113 | /* Send a packet to Bluetooth Controller. */ 114 | 115 | static void 116 | ble_send(void *arg) 117 | { 118 | struct mdx_ringbuf *rb; 119 | int err; 120 | 121 | while (1) { 122 | mdx_sem_wait(&sem_send); 123 | 124 | /* Send a packet to BLE controller */ 125 | 126 | while ((err = mdx_ringbuf_head(&ringbuf_tx_sc, &rb)) == 0) { 127 | #if 0 128 | printf("%s: got buf %x *buf %d bufsize %d\n", 129 | __func__, (int)rb->buf, 130 | *(volatile uint32_t *)rb->buf, rb->fill); 131 | #endif 132 | switch (rb->user) { 133 | case BT_ACL_OUT: 134 | mdx_sem_wait(&sem_thread); 135 | hci_data_put(rb->buf); 136 | mdx_sem_post(&sem_thread); 137 | break; 138 | case BT_CMD: 139 | mdx_sem_wait(&sem_thread); 140 | hci_cmd_put(rb->buf); 141 | mdx_sem_post(&sem_thread); 142 | mdx_sem_post(&sem_recv); 143 | break; 144 | default: 145 | panic("unknown packet type"); 146 | } 147 | mdx_ringbuf_submit(&ringbuf_tx_sc); 148 | } 149 | } 150 | } 151 | 152 | /* Receive data from Bluetooth Controller. */ 153 | 154 | static int 155 | handle_hci_input(void) 156 | { 157 | struct bt_hci_acl_hdr *hdr; 158 | struct mdx_ringbuf *rb; 159 | int err; 160 | 161 | err = mdx_ringbuf_head(&ringbuf_rx_sc, &rb); 162 | if (err) 163 | panic("could not get an entry in the ringbuf"); 164 | 165 | mdx_sem_wait(&sem_thread); 166 | err = hci_data_get((uint8_t *)rb->buf); 167 | mdx_sem_post(&sem_thread); 168 | if (err == 0) { 169 | hdr = (void *)rb->buf; 170 | rb->fill = hdr->len + sizeof(struct bt_hci_acl_hdr); 171 | rb->user = BT_ACL_IN; 172 | mdx_ringbuf_submit(&ringbuf_rx_sc); 173 | nrf_ipc_trigger(ipc, 1); 174 | return (1); 175 | } 176 | 177 | return (0); 178 | } 179 | 180 | static int 181 | handle_evt_input(void) 182 | { 183 | struct mdx_ringbuf *rb; 184 | int err; 185 | 186 | err = mdx_ringbuf_head(&ringbuf_rx_sc, &rb); 187 | if (err) 188 | panic("could not get an entry in the ringbuf"); 189 | 190 | mdx_sem_wait(&sem_thread); 191 | err = hci_evt_get((uint8_t *)rb->buf); 192 | mdx_sem_post(&sem_thread); 193 | if (err == 0) { 194 | #if 0 195 | struct bt_hci_evt_hdr *evt_hdr; 196 | evt_hdr = (void *)rb->buf; 197 | rb->fill = evt_hdr->len + sizeof(struct bt_hci_evt_hdr); 198 | #else 199 | rb->fill = 74; 200 | #endif 201 | rb->user = BT_EVT; 202 | mdx_ringbuf_submit(&ringbuf_rx_sc); 203 | nrf_ipc_trigger(ipc, 1); 204 | return (1); 205 | } 206 | 207 | return (0); 208 | } 209 | 210 | static void 211 | ble_recv(void *arg) 212 | { 213 | int err1, err2; 214 | 215 | while (1) { 216 | mdx_sem_wait(&sem_recv); 217 | 218 | do { 219 | err1 = handle_hci_input(); 220 | err2 = handle_evt_input(); 221 | } while (err1 || err2); 222 | 223 | mdx_thread_yield(); 224 | } 225 | } 226 | 227 | static void 228 | ble_controller_fault_handler(const char * file, const uint32_t line) 229 | { 230 | 231 | printf("%s: %s:%d\n", __func__, file, line); 232 | 233 | while (1); 234 | } 235 | 236 | static void 237 | ble_assertion_handler(const char *const file, const uint32_t line) 238 | { 239 | 240 | printf("%s: %s:%d\n", __func__, file, line); 241 | 242 | while (1); 243 | } 244 | 245 | static void 246 | host_signal(void) 247 | { 248 | 249 | mdx_sem_post(&sem_recv); 250 | } 251 | 252 | void 253 | ble_ipc_intr(void *arg) 254 | { 255 | 256 | mdx_sem_post(&sem_send); 257 | } 258 | 259 | int 260 | ble_test(void) 261 | { 262 | mpsl_clock_lfclk_cfg_t clock_cfg; 263 | struct thread *td; 264 | int err; 265 | 266 | mdx_sem_init(&sem_recv, 0); 267 | mdx_sem_init(&sem_send, 0); 268 | mdx_sem_init(&sem_thread, 1); 269 | 270 | ipc = mdx_device_lookup_by_name("nrf_ipc", 0); 271 | if (ipc == NULL) 272 | panic("could not find ipc device"); 273 | 274 | td = &recv_td; 275 | bzero(td, sizeof(struct thread)); 276 | td->td_stack = (void *)((uint32_t)recv_td_stack); 277 | td->td_stack_size = BLE_STACK_SIZE; 278 | err = mdx_thread_setup(td, "ble_recv", 279 | 1, /* priority */ 280 | 0, /* quantum */ 281 | ble_recv, NULL); 282 | if (err) 283 | panic("Could not setup bt recv thread."); 284 | mdx_sched_add(td); 285 | 286 | td = &send_td; 287 | bzero(td, sizeof(struct thread)); 288 | td->td_stack = (void *)((uint32_t)send_td_stack); 289 | td->td_stack_size = BLE_STACK_SIZE; 290 | err = mdx_thread_setup(td, "ble_send", 291 | 1, /* priority */ 292 | 0, /* quantum */ 293 | ble_send, NULL); 294 | if (err) 295 | panic("Could not create bt send thread."); 296 | mdx_sched_add(td); 297 | 298 | print_build_rev(); 299 | 300 | bzero(&clock_cfg, sizeof(mpsl_clock_lfclk_cfg_t)); 301 | clock_cfg.accuracy_ppm = 500; 302 | 303 | #if 1 304 | clock_cfg.source = MPSL_CLOCK_LF_SRC_XTAL; 305 | #else 306 | clock_cfg.source = MPSL_CLOCK_LF_SRC_RC; 307 | clock_cfg.rc_ctiv = MPSL_RECOMMENDED_RC_CTIV; 308 | clock_cfg.rc_temp_ctiv = MPSL_RECOMMENDED_RC_TEMP_CTIV; 309 | #endif 310 | 311 | printf("%s: Initializing BLE controller\n", __func__); 312 | 313 | mpsl_init(&clock_cfg, BLE_CONTROLLER_PROCESS_IRQn, 314 | ble_assertion_handler); 315 | err = ble_controller_init(ble_controller_fault_handler); 316 | 317 | if (err != 0) { 318 | printf("%s: Failed to initialize BLE controller, error %d\n", 319 | __func__, err); 320 | return (err); 321 | }; 322 | 323 | printf("%s: Enabling BLE controller\n", __func__); 324 | 325 | ble_controller_cfg_t cfg; 326 | 327 | cfg.master_count.count = BLE_MASTER_COUNT; 328 | err = ble_controller_cfg_set(BLE_CONTROLLER_DEFAULT_RESOURCE_CFG_TAG, 329 | BLE_CONTROLLER_CFG_TYPE_MASTER_COUNT, 330 | &cfg); 331 | 332 | cfg.slave_count.count = BLE_SLAVE_COUNT; 333 | err = ble_controller_cfg_set(BLE_CONTROLLER_DEFAULT_RESOURCE_CFG_TAG, 334 | BLE_CONTROLLER_CFG_TYPE_SLAVE_COUNT, 335 | &cfg); 336 | 337 | cfg.buffer_cfg.rx_packet_size = MAX_RX_PKT_SIZE; 338 | cfg.buffer_cfg.tx_packet_size = MAX_TX_PKT_SIZE; 339 | cfg.buffer_cfg.rx_packet_count = BLE_CONTROLLER_DEFAULT_RX_PACKET_COUNT; 340 | cfg.buffer_cfg.tx_packet_count = BLE_CONTROLLER_DEFAULT_TX_PACKET_COUNT; 341 | 342 | err = ble_controller_cfg_set(BLE_CONTROLLER_DEFAULT_RESOURCE_CFG_TAG, 343 | BLE_CONTROLLER_CFG_TYPE_BUFFER_CFG, 344 | &cfg); 345 | 346 | cfg.event_length.event_length_us = 347 | BLE_MAX_CONN_EVENT_LEN_DEFAULT; 348 | err = ble_controller_cfg_set(BLE_CONTROLLER_DEFAULT_RESOURCE_CFG_TAG, 349 | BLE_CONTROLLER_CFG_TYPE_EVENT_LENGTH, 350 | &cfg); 351 | 352 | err = ble_controller_enable(host_signal, ble_controller_mempool); 353 | if (err != 0) { 354 | printf("%s: Failed to enable BLE controller, error %d\n", 355 | __func__, err); 356 | return (err); 357 | } 358 | 359 | printf("%s: BLE controller is ready\n", __func__); 360 | 361 | return (0); 362 | } 363 | --------------------------------------------------------------------------------