Skip to content

Commit 05c1384

Browse files
committed
nrf: Split the ble module into a shared part and the port implementation
This allows other ports to implement these shared bindings.
1 parent 2f877bd commit 05c1384

File tree

12 files changed

+379
-155
lines changed

12 files changed

+379
-155
lines changed

ports/nrf/Makefile

+6-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\
146146
ubluepy/ubluepy_descriptor.c \
147147
ubluepy/ubluepy_scanner.c \
148148
ubluepy/ubluepy_scan_entry.c \
149-
ble/modble.c \
150149
)
151150

152151
SRC_COMMON_HAL += \
@@ -173,6 +172,12 @@ SRC_COMMON_HAL += \
173172
supervisor/__init__.c \
174173
supervisor/Runtime.c \
175174

175+
ifneq ($(SD), )
176+
SRC_COMMON_HAL += \
177+
bleio/__init__.c \
178+
bleio/Adapter.c
179+
endif
180+
176181
# These don't have corresponding files in each port but are still located in
177182
# shared-bindings to make it clear what the contents of the modules are.
178183
SRC_BINDINGS_ENUMS = \

ports/nrf/bluetooth_conf.h

-11
This file was deleted.

ports/nrf/common-hal/bleio/Adapter.c

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Glenn Ruben Bakke
7+
* Copyright (c) 2018 Artur Pacholec
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include <stdio.h>
29+
30+
#include "ble_drv.h"
31+
#include "nrfx.h"
32+
#include "nrf_error.h"
33+
#include "py/misc.h"
34+
35+
void common_hal_bleio_adapter_set_enabled(bool enabled) {
36+
if (enabled) {
37+
const uint32_t err = ble_drv_stack_enable();
38+
if (err != NRF_SUCCESS) {
39+
NRFX_ASSERT(err);
40+
}
41+
42+
printf("SoftDevice enabled\n");
43+
} else {
44+
ble_drv_stack_disable();
45+
}
46+
}
47+
48+
bool common_hal_bleio_adapter_get_enabled(void) {
49+
return ble_drv_stack_enabled();
50+
}
51+
52+
void common_hal_bleio_adapter_get_address(vstr_t *vstr) {
53+
ble_drv_addr_t address;
54+
ble_drv_address_get(&address);
55+
56+
vstr_printf(vstr, ""HEX2_FMT":"HEX2_FMT":"HEX2_FMT":" \
57+
HEX2_FMT":"HEX2_FMT":"HEX2_FMT"",
58+
address.addr[5], address.addr[4], address.addr[3],
59+
address.addr[2], address.addr[1], address.addr[0]);
60+
}

ports/nrf/modules/ble/help_sd.h renamed to ports/nrf/common-hal/bleio/Adapter.h

+8-18
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* The MIT License (MIT)
55
*
66
* Copyright (c) 2016 Glenn Ruben Bakke
7+
* Copyright (c) 2018 Artur Pacholec
78
*
89
* Permission is hereby granted, free of charge, to any person obtaining a copy
910
* of this software and associated documentation files (the "Software"), to deal
@@ -24,24 +25,13 @@
2425
* THE SOFTWARE.
2526
*/
2627

27-
#ifndef HELP_SD_H__
28-
#define HELP_SD_H__
28+
#ifndef MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ADAPTER_H
29+
#define MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ADAPTER_H
2930

30-
#include "bluetooth_conf.h"
31+
#include "py/obj.h"
3132

32-
#if MICROPY_PY_BLE
33+
typedef struct {
34+
mp_obj_base_t base;
35+
} super_adapter_obj_t;
3336

34-
#define HELP_TEXT_SD \
35-
"If compiled with SD=<softdevice> the additional commands are\n" \
36-
"available:\n" \
37-
" ble.enable() -- enable bluetooth stack\n" \
38-
" ble.disable() -- disable bluetooth stack\n" \
39-
" ble.enabled() -- check whether bluetooth stack is enabled\n" \
40-
" ble.address() -- return device address as text string\n" \
41-
"\n"
42-
43-
#else
44-
#define HELP_TEXT_SD
45-
#endif // MICROPY_PY_BLE
46-
47-
#endif
37+
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_BLEIO_ADAPTER_H

ports/nrf/common-hal/bleio/__init__.c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Glenn Ruben Bakke
7+
* Copyright (c) 2018 Artur Pacholec
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "shared-bindings/bleio/__init__.h"
29+
#include "shared-bindings/bleio/Adapter.h"
30+
31+
// The singleton bleio.Adapter object, bound to bleio.adapter
32+
// It currently only has properties and no state
33+
const super_adapter_obj_t common_hal_bleio_adapter_obj = {
34+
.base = {
35+
.type = &bleio_adapter_type,
36+
},
37+
};

ports/nrf/modules/ble/modble.c

-104
This file was deleted.

ports/nrf/mpconfigport.h

+15-21
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,27 @@
126126

127127
#define MICROPY_KBD_EXCEPTION (1)
128128

129-
#ifndef MICROPY_PY_HW_RNG
130-
#define MICROPY_PY_HW_RNG (1)
131-
#endif
132-
133129
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
134130
#define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0)
135131

136132
// Scan gamepad every 32ms
137133
#define CIRCUITPY_GAMEPAD_TICKS 0x1f
138134

139-
// if sdk is in use, import configuration
140135
#if BLUETOOTH_SD
141-
#include "bluetooth_conf.h"
136+
#define MICROPY_PY_BLEIO (1)
137+
#define MICROPY_PY_BLE_NUS (0)
138+
#define MICROPY_PY_UBLUEPY (1)
139+
#define MICROPY_PY_UBLUEPY_PERIPHERAL (1)
140+
#define MICROPY_PY_UBLUEPY_CENTRAL (1)
141+
#define BLUETOOTH_WEBBLUETOOTH_REPL (0)
142142
#endif
143143

144-
#ifndef MICROPY_PY_UBLUEPY
145-
#define MICROPY_PY_UBLUEPY (0)
144+
#ifndef MICROPY_PY_BLEIO
145+
#define MICROPY_PY_BLEIO (0)
146146
#endif
147147

148-
#ifndef MICROPY_PY_BLE_NUS
149-
#define MICROPY_PY_BLE_NUS (0)
148+
#ifndef MICROPY_PY_UBLUEPY
149+
#define MICROPY_PY_UBLUEPY (0)
150150
#endif
151151

152152
// type definitions for the specific machine
@@ -181,22 +181,20 @@ extern const struct _mp_obj_module_t struct_module;
181181
extern const struct _mp_obj_module_t time_module;
182182
extern const struct _mp_obj_module_t supervisor_module;
183183
extern const struct _mp_obj_module_t gamepad_module;
184+
extern const struct _mp_obj_module_t bleio_module;
184185

185186
extern const struct _mp_obj_module_t mp_module_ubluepy;
186187

187-
extern const struct _mp_obj_module_t ble_module;
188-
189188
#if MICROPY_PY_UBLUEPY
190189
#define UBLUEPY_MODULE { MP_ROM_QSTR(MP_QSTR_ubluepy), MP_ROM_PTR(&mp_module_ubluepy) },
191190
#else
192191
#define UBLUEPY_MODULE
193192
#endif
194193

195-
#if MICROPY_PY_BLE
196-
extern const struct _mp_obj_module_t ble_module;
197-
#define BLE_MODULE { MP_ROM_QSTR(MP_QSTR_ble), MP_ROM_PTR(&ble_module) },
194+
#if MICROPY_PY_BLEIO
195+
#define BLEIO_MODULE { MP_ROM_QSTR(MP_QSTR_bleio), MP_ROM_PTR(&bleio_module) },
198196
#else
199-
#define BLE_MODULE
197+
#define BLEIO_MODULE
200198
#endif
201199

202200
#define MICROPY_PORT_BUILTIN_MODULES \
@@ -214,7 +212,7 @@ extern const struct _mp_obj_module_t ble_module;
214212
{ MP_OBJ_NEW_QSTR (MP_QSTR_supervisor ), (mp_obj_t)&supervisor_module }, \
215213
{ MP_OBJ_NEW_QSTR (MP_QSTR_gamepad ), (mp_obj_t)&gamepad_module }, \
216214
{ MP_OBJ_NEW_QSTR (MP_QSTR_time ), (mp_obj_t)&time_module }, \
217-
BLE_MODULE \
215+
BLEIO_MODULE \
218216
UBLUEPY_MODULE \
219217

220218
// extra built in names to add to the global namespace
@@ -223,10 +221,6 @@ extern const struct _mp_obj_module_t ble_module;
223221
{ MP_OBJ_NEW_QSTR (MP_QSTR_input), (mp_obj_t)&mp_builtin_input_obj }, \
224222
{ MP_ROM_QSTR (MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, \
225223

226-
// extra constants
227-
#define MICROPY_PORT_CONSTANTS \
228-
BLE_MODULE \
229-
230224
#define MP_STATE_PORT MP_STATE_VM
231225

232226
#define MICROPY_PORT_ROOT_POINTERS \

0 commit comments

Comments
 (0)