summaryrefslogtreecommitdiff
path: root/ports
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2018-07-13 10:23:37 -0400
committerGitHub <noreply@github.com>2018-07-13 10:23:37 -0400
commitbfe14ff8244f90bf8eadd40f5286b7c5754f2dbd (patch)
treec21e584604ab9fbb8a0a24bdb2efa0f98948dbd8 /ports
parent414267388d37fdffc28f82195a02b165c1724299 (diff)
parent05c1384b677c64ea39d25b4bcd5dc5e38a4b134d (diff)
Merge pull request #1014 from arturo182/nrf_shared_ble
nrf: Split the ble module into a shared part and the port implementation
Diffstat (limited to 'ports')
-rwxr-xr-xports/nrf/Makefile7
-rw-r--r--ports/nrf/bluetooth_conf.h11
-rw-r--r--ports/nrf/common-hal/bleio/Adapter.c60
-rw-r--r--ports/nrf/common-hal/bleio/Adapter.h (renamed from ports/nrf/modules/ble/help_sd.h)26
-rw-r--r--ports/nrf/common-hal/bleio/__init__.c37
-rw-r--r--ports/nrf/modules/ble/modble.c104
-rw-r--r--ports/nrf/mpconfigport.h36
7 files changed, 126 insertions, 155 deletions
diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile
index 586b0cbbd..74b6ec2ad 100755
--- a/ports/nrf/Makefile
+++ b/ports/nrf/Makefile
@@ -146,7 +146,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\
ubluepy/ubluepy_descriptor.c \
ubluepy/ubluepy_scanner.c \
ubluepy/ubluepy_scan_entry.c \
- ble/modble.c \
)
SRC_COMMON_HAL += \
@@ -173,6 +172,12 @@ SRC_COMMON_HAL += \
supervisor/__init__.c \
supervisor/Runtime.c \
+ifneq ($(SD), )
+SRC_COMMON_HAL += \
+ bleio/__init__.c \
+ bleio/Adapter.c
+endif
+
# These don't have corresponding files in each port but are still located in
# shared-bindings to make it clear what the contents of the modules are.
SRC_BINDINGS_ENUMS = \
diff --git a/ports/nrf/bluetooth_conf.h b/ports/nrf/bluetooth_conf.h
deleted file mode 100644
index b86e514f0..000000000
--- a/ports/nrf/bluetooth_conf.h
+++ /dev/null
@@ -1,11 +0,0 @@
-#ifndef BLUETOOTH_CONF_H__
-#define BLUETOOTH_CONF_H__
-
-#define MICROPY_PY_BLE (1)
-#define MICROPY_PY_BLE_NUS (0)
-#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
-#define MICROPY_PY_UBLUEPY (1)
-#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
-#define MICROPY_PY_UBLUEPY_CENTRAL (1)
-
-#endif
diff --git a/ports/nrf/common-hal/bleio/Adapter.c b/ports/nrf/common-hal/bleio/Adapter.c
new file mode 100644
index 000000000..52069e305
--- /dev/null
+++ b/ports/nrf/common-hal/bleio/Adapter.c
@@ -0,0 +1,60 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 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
+ * 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 <stdio.h>
+
+#include "ble_drv.h"
+#include "nrfx.h"
+#include "nrf_error.h"
+#include "py/misc.h"
+
+void common_hal_bleio_adapter_set_enabled(bool enabled) {
+ if (enabled) {
+ const uint32_t err = ble_drv_stack_enable();
+ if (err != NRF_SUCCESS) {
+ NRFX_ASSERT(err);
+ }
+
+ printf("SoftDevice enabled\n");
+ } else {
+ ble_drv_stack_disable();
+ }
+}
+
+bool common_hal_bleio_adapter_get_enabled(void) {
+ return ble_drv_stack_enabled();
+}
+
+void common_hal_bleio_adapter_get_address(vstr_t *vstr) {
+ ble_drv_addr_t address;
+ ble_drv_address_get(&address);
+
+ vstr_printf(vstr, ""HEX2_FMT":"HEX2_FMT":"HEX2_FMT":" \
+ HEX2_FMT":"HEX2_FMT":"HEX2_FMT"",
+ address.addr[5], address.addr[4], address.addr[3],
+ address.addr[2], address.addr[1], address.addr[0]);
+}
diff --git a/ports/nrf/modules/ble/help_sd.h b/ports/nrf/common-hal/bleio/Adapter.h
index 027bbdd51..0497f9ac9 100644
--- a/ports/nrf/modules/ble/help_sd.h
+++ b/ports/nrf/common-hal/bleio/Adapter.h
@@ -4,6 +4,7 @@
* The MIT License (MIT)
*
* Copyright (c) 2016 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,24 +25,13 @@
* THE SOFTWARE.
*/
-#ifndef HELP_SD_H__
-#define HELP_SD_H__
+#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ADAPTER_H
+#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ADAPTER_H
-#include "bluetooth_conf.h"
+#include "py/obj.h"
-#if MICROPY_PY_BLE
+typedef struct {
+ mp_obj_base_t base;
+} super_adapter_obj_t;
-#define HELP_TEXT_SD \
-"If compiled with SD=<softdevice> the additional commands are\n" \
-"available:\n" \
-" ble.enable() -- enable bluetooth stack\n" \
-" ble.disable() -- disable bluetooth stack\n" \
-" ble.enabled() -- check whether bluetooth stack is enabled\n" \
-" ble.address() -- return device address as text string\n" \
-"\n"
-
-#else
-#define HELP_TEXT_SD
-#endif // MICROPY_PY_BLE
-
-#endif
+#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ADAPTER_H
diff --git a/ports/nrf/common-hal/bleio/__init__.c b/ports/nrf/common-hal/bleio/__init__.c
new file mode 100644
index 000000000..0cc1b7152
--- /dev/null
+++ b/ports/nrf/common-hal/bleio/__init__.c
@@ -0,0 +1,37 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2016 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
+ * 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 "shared-bindings/bleio/__init__.h"
+#include "shared-bindings/bleio/Adapter.h"
+
+// The singleton bleio.Adapter object, bound to bleio.adapter
+// It currently only has properties and no state
+const super_adapter_obj_t common_hal_bleio_adapter_obj = {
+ .base = {
+ .type = &bleio_adapter_type,
+ },
+};
diff --git a/ports/nrf/modules/ble/modble.c b/ports/nrf/modules/ble/modble.c
deleted file mode 100644
index 5b75025d1..000000000
--- a/ports/nrf/modules/ble/modble.c
+++ /dev/null
@@ -1,104 +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 <stdio.h>
-#include <stdint.h>
-#include "py/runtime.h"
-
-#if MICROPY_PY_BLE
-
-#include "mpconfigboard.h"
-#include "ble_drv.h"
-
-/// \method enable()
-/// Enable BLE softdevice.
-mp_obj_t ble_obj_enable(void) {
- printf("SoftDevice enabled\n");
- uint32_t err_code = ble_drv_stack_enable();
- if (err_code < 0) {
- // TODO: raise exception.
- }
- return mp_const_none;
-}
-
-/// \method disable()
-/// Disable BLE softdevice.
-mp_obj_t ble_obj_disable(void) {
- ble_drv_stack_disable();
- return mp_const_none;
-}
-
-/// \method enabled()
-/// Get state of whether the softdevice is enabled or not.
-mp_obj_t ble_obj_enabled(void) {
- uint8_t is_enabled = ble_drv_stack_enabled();
- mp_int_t enabled = is_enabled;
- return MP_OBJ_NEW_SMALL_INT(enabled);
-}
-
-/// \method address()
-/// Return device address as text string.
-mp_obj_t ble_obj_address(void) {
- ble_drv_addr_t local_addr;
- ble_drv_address_get(&local_addr);
-
- vstr_t vstr;
- vstr_init(&vstr, 17);
-
- vstr_printf(&vstr, ""HEX2_FMT":"HEX2_FMT":"HEX2_FMT":" \
- HEX2_FMT":"HEX2_FMT":"HEX2_FMT"",
- local_addr.addr[5], local_addr.addr[4], local_addr.addr[3],
- local_addr.addr[2], local_addr.addr[1], local_addr.addr[0]);
-
- mp_obj_t mac_str = mp_obj_new_str(vstr.buf, vstr.len, false);
-
- vstr_clear(&vstr);
-
- return mac_str;
-}
-
-STATIC MP_DEFINE_CONST_FUN_OBJ_0(ble_obj_enable_obj, ble_obj_enable);
-STATIC MP_DEFINE_CONST_FUN_OBJ_0(ble_obj_disable_obj, ble_obj_disable);
-STATIC MP_DEFINE_CONST_FUN_OBJ_0(ble_obj_enabled_obj, ble_obj_enabled);
-STATIC MP_DEFINE_CONST_FUN_OBJ_0(ble_obj_address_obj, ble_obj_address);
-
-STATIC const mp_rom_map_elem_t ble_module_globals_table[] = {
- { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ble) },
- { MP_ROM_QSTR(MP_QSTR_enable), MP_ROM_PTR(&ble_obj_enable_obj) },
- { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&ble_obj_disable_obj) },
- { MP_ROM_QSTR(MP_QSTR_enabled), MP_ROM_PTR(&ble_obj_enabled_obj) },
- { MP_ROM_QSTR(MP_QSTR_address), MP_ROM_PTR(&ble_obj_address_obj) },
-};
-
-
-STATIC MP_DEFINE_CONST_DICT(ble_module_globals, ble_module_globals_table);
-
-const mp_obj_module_t ble_module = {
- .base = { &mp_type_module },
- .globals = (mp_obj_dict_t*)&ble_module_globals,
-};
-
-#endif // MICROPY_PY_BLE
diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h
index dc319a440..85396e5dd 100644
--- a/ports/nrf/mpconfigport.h
+++ b/ports/nrf/mpconfigport.h
@@ -126,27 +126,27 @@
#define MICROPY_KBD_EXCEPTION (1)
-#ifndef MICROPY_PY_HW_RNG
-#define MICROPY_PY_HW_RNG (1)
-#endif
-
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0)
// Scan gamepad every 32ms
#define CIRCUITPY_GAMEPAD_TICKS 0x1f
-// if sdk is in use, import configuration
#if BLUETOOTH_SD
-#include "bluetooth_conf.h"
+#define MICROPY_PY_BLEIO (1)
+#define MICROPY_PY_BLE_NUS (0)
+#define MICROPY_PY_UBLUEPY (1)
+#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
+#define MICROPY_PY_UBLUEPY_CENTRAL (1)
+#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
#endif
-#ifndef MICROPY_PY_UBLUEPY
-#define MICROPY_PY_UBLUEPY (0)
+#ifndef MICROPY_PY_BLEIO
+#define MICROPY_PY_BLEIO (0)
#endif
-#ifndef MICROPY_PY_BLE_NUS
-#define MICROPY_PY_BLE_NUS (0)
+#ifndef MICROPY_PY_UBLUEPY
+#define MICROPY_PY_UBLUEPY (0)
#endif
// type definitions for the specific machine
@@ -181,22 +181,20 @@ extern const struct _mp_obj_module_t struct_module;
extern const struct _mp_obj_module_t time_module;
extern const struct _mp_obj_module_t supervisor_module;
extern const struct _mp_obj_module_t gamepad_module;
+extern const struct _mp_obj_module_t bleio_module;
extern const struct _mp_obj_module_t mp_module_ubluepy;
-extern const struct _mp_obj_module_t ble_module;
-
#if MICROPY_PY_UBLUEPY
#define UBLUEPY_MODULE { MP_ROM_QSTR(MP_QSTR_ubluepy), MP_ROM_PTR(&mp_module_ubluepy) },
#else
#define UBLUEPY_MODULE
#endif
-#if MICROPY_PY_BLE
-extern const struct _mp_obj_module_t ble_module;
-#define BLE_MODULE { MP_ROM_QSTR(MP_QSTR_ble), MP_ROM_PTR(&ble_module) },
+#if MICROPY_PY_BLEIO
+#define BLEIO_MODULE { MP_ROM_QSTR(MP_QSTR_bleio), MP_ROM_PTR(&bleio_module) },
#else
-#define BLE_MODULE
+#define BLEIO_MODULE
#endif
#define MICROPY_PORT_BUILTIN_MODULES \
@@ -214,7 +212,7 @@ extern const struct _mp_obj_module_t ble_module;
{ MP_OBJ_NEW_QSTR (MP_QSTR_supervisor ), (mp_obj_t)&supervisor_module }, \
{ MP_OBJ_NEW_QSTR (MP_QSTR_gamepad ), (mp_obj_t)&gamepad_module }, \
{ MP_OBJ_NEW_QSTR (MP_QSTR_time ), (mp_obj_t)&time_module }, \
- BLE_MODULE \
+ BLEIO_MODULE \
UBLUEPY_MODULE \
// extra built in names to add to the global namespace
@@ -223,10 +221,6 @@ extern const struct _mp_obj_module_t ble_module;
{ MP_OBJ_NEW_QSTR (MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \
{ MP_ROM_QSTR (MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, \
-// extra constants
-#define MICROPY_PORT_CONSTANTS \
- BLE_MODULE \
-
#define MP_STATE_PORT MP_STATE_VM
#define MICROPY_PORT_ROOT_POINTERS \