Skip to content

nrf: Split the ble module into a shared part and the port implementation #1014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ports/nrf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 += \
Expand All @@ -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 = \
Expand Down
11 changes: 0 additions & 11 deletions ports/nrf/bluetooth_conf.h

This file was deleted.

60 changes: 60 additions & 0 deletions ports/nrf/common-hal/bleio/Adapter.c
Original file line number Diff line number Diff line change
@@ -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]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
37 changes: 37 additions & 0 deletions ports/nrf/common-hal/bleio/__init__.c
Original file line number Diff line number Diff line change
@@ -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,
},
};
104 changes: 0 additions & 104 deletions ports/nrf/modules/ble/modble.c

This file was deleted.

36 changes: 15 additions & 21 deletions ports/nrf/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: are we going to get rid of ubluepy at some point or does it serve some purpose in other nrf ports?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, next step is port ubluepy classes to bleio, I just like to keep things separated and do them one step at a time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, sounds good!

#endif

// type definitions for the specific machine
Expand Down Expand Up @@ -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 \
Expand All @@ -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
Expand All @@ -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 \
Expand Down
Loading