Skip to content

Commit 64cb762

Browse files
authored
Merge pull request #9619 from tannewt/merge_in_9.1.x
Merge in 9.1.x fixes for C6 and nRF UF2 reset
2 parents fe7e872 + 7fcaa1e commit 64cb762

File tree

19 files changed

+130
-42
lines changed

19 files changed

+130
-42
lines changed

devices/ble_hci/common-hal/_bleio/Characteristic.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
5050
}
5151
}
5252

53+
bool common_hal_bleio_characteristic_deinited(bleio_characteristic_obj_t *self) {
54+
return self->handle == BLE_GATT_HANDLE_INVALID;
55+
}
56+
57+
void common_hal_bleio_characteristic_deinit(bleio_characteristic_obj_t *self) {
58+
if (common_hal_bleio_characteristic_deinited(self)) {
59+
return;
60+
}
61+
self->handle = BLE_GATT_HANDLE_INVALID;
62+
}
63+
5364
mp_obj_tuple_t *common_hal_bleio_characteristic_get_descriptors(bleio_characteristic_obj_t *self) {
5465
return mp_obj_new_tuple(self->descriptor_list->len, self->descriptor_list->items);
5566
}

ports/atmel-samd/boards/trellis_m4_express/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ LONGINT_IMPL = MPZ
1212

1313
CIRCUITPY_FLOPPYIO = 0
1414
CIRCUITPY_FRAMEBUFFERIO = 0
15+
CIRCUITPY_BLEIO_HCI = 0
1516

1617
CIRCUITPY_BITBANG_APA102 = 1

ports/espressif/common-hal/_bleio/Characteristic.c

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "shared-bindings/_bleio/PacketBuffer.h"
1717
#include "shared-bindings/_bleio/Service.h"
1818
#include "shared-bindings/time/__init__.h"
19+
#include "supervisor/shared/safe_mode.h"
1920

2021
#include "common-hal/_bleio/Adapter.h"
2122
#include "common-hal/_bleio/Service.h"
@@ -74,27 +75,20 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
7475
self->flags |= BLE_GATT_CHR_F_WRITE_AUTHEN;
7576
}
7677

77-
if (initial_value_bufinfo != NULL) {
78-
// Copy the initial value if it's on the heap. Otherwise it's internal and we may not be able
79-
// to allocate.
80-
self->current_value_len = initial_value_bufinfo->len;
81-
if (gc_alloc_possible()) {
82-
self->current_value = m_malloc(max_length);
83-
self->current_value_alloc = max_length;
84-
if (gc_nbytes(initial_value_bufinfo->buf) > 0) {
85-
memcpy(self->current_value, initial_value_bufinfo->buf, self->current_value_len);
86-
}
87-
} else {
88-
self->current_value = initial_value_bufinfo->buf;
89-
assert(self->current_value_len == max_length);
90-
}
78+
if (gc_alloc_possible()) {
79+
self->current_value = m_malloc(max_length);
9180
} else {
9281
self->current_value = port_malloc(max_length, false);
93-
if (self->current_value != NULL) {
94-
self->current_value_alloc = max_length;
95-
self->current_value_len = 0;
82+
if (self->current_value == NULL) {
83+
reset_into_safe_mode(SAFE_MODE_NO_HEAP);
9684
}
9785
}
86+
self->current_value_alloc = max_length;
87+
self->current_value_len = 0;
88+
89+
if (initial_value_bufinfo != NULL) {
90+
common_hal_bleio_characteristic_set_value(self, initial_value_bufinfo);
91+
}
9892

9993
if (gc_alloc_possible()) {
10094
self->descriptor_list = mp_obj_new_list(0, NULL);
@@ -114,6 +108,26 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
114108
}
115109
}
116110

111+
bool common_hal_bleio_characteristic_deinited(bleio_characteristic_obj_t *self) {
112+
return self->current_value == NULL;
113+
}
114+
115+
void common_hal_bleio_characteristic_deinit(bleio_characteristic_obj_t *self) {
116+
if (common_hal_bleio_characteristic_deinited(self)) {
117+
return;
118+
}
119+
if (self->current_value == NULL) {
120+
return;
121+
}
122+
123+
if (gc_nbytes(self->current_value) > 0) {
124+
m_free(self->current_value);
125+
} else {
126+
port_free(self->current_value);
127+
}
128+
self->current_value = NULL;
129+
}
130+
117131
mp_obj_tuple_t *common_hal_bleio_characteristic_get_descriptors(bleio_characteristic_obj_t *self) {
118132
if (self->descriptor_list == NULL) {
119133
return mp_const_empty_tuple;
@@ -173,21 +187,7 @@ void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self,
173187
}
174188

175189
self->current_value_len = bufinfo->len;
176-
// If we've already allocated an internal buffer or the provided buffer
177-
// is on the heap, then copy into the internal buffer.
178-
if (self->current_value_alloc > 0 || gc_nbytes(bufinfo->buf) > 0) {
179-
if (self->current_value_alloc < bufinfo->len) {
180-
self->current_value = m_realloc(self->current_value, bufinfo->len);
181-
// Get the number of bytes from the heap because it may be more
182-
// than the len due to gc block size.
183-
self->current_value_alloc = gc_nbytes(self->current_value);
184-
}
185-
memcpy(self->current_value, bufinfo->buf, bufinfo->len);
186-
} else {
187-
// Otherwise, use the provided buffer to delay any heap allocation.
188-
self->current_value = bufinfo->buf;
189-
self->current_value_alloc = 0;
190-
}
190+
memcpy(self->current_value, bufinfo->buf, self->current_value_len);
191191

192192
ble_gatts_chr_updated(self->handle);
193193
}

ports/espressif/common-hal/_bleio/CharacteristicBuffer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void bleio_characteristic_buffer_extend(bleio_characteristic_buffer_obj_t *self,
2626
for (uint16_t i = 0; i < len; i++) {
2727
if (data[i] == mp_interrupt_char) {
2828
mp_sched_keyboard_interrupt();
29+
ringbuf_clear(&self->ringbuf);
2930
} else {
3031
ringbuf_put(&self->ringbuf, data[i]);
3132
}

ports/espressif/common-hal/_bleio/PacketBuffer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ void common_hal_bleio_packet_buffer_deinit(bleio_packet_buffer_obj_t *self) {
436436
return;
437437
}
438438
bleio_characteristic_clear_observer(self->characteristic);
439+
self->characteristic = NULL;
439440
ble_event_remove_handler(packet_buffer_on_ble_client_evt, self);
440441
ringbuf_deinit(&self->ringbuf);
441442
}

ports/espressif/supervisor/usb_serial_jtag.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static void _copy_out_of_fifo(void) {
4646
for (size_t i = 0; i < len; ++i) {
4747
if (rx_buf[i] == mp_interrupt_char) {
4848
mp_sched_keyboard_interrupt();
49+
ringbuf_clear(&ringbuf);
4950
} else {
5051
ringbuf_put(&ringbuf, rx_buf[i]);
5152
}

ports/nordic/common-hal/_bleio/Characteristic.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
109109
}
110110
}
111111

112+
bool common_hal_bleio_characteristic_deinited(bleio_characteristic_obj_t *self) {
113+
return self->handle == BLE_GATT_HANDLE_INVALID;
114+
}
115+
116+
void common_hal_bleio_characteristic_deinit(bleio_characteristic_obj_t *self) {
117+
if (common_hal_bleio_characteristic_deinited(self)) {
118+
return;
119+
}
120+
self->handle = BLE_GATT_HANDLE_INVALID;
121+
// TODO: Can we remove this from the soft device? Right now we assume the
122+
// reset clears things.
123+
}
124+
112125
mp_obj_tuple_t *common_hal_bleio_characteristic_get_descriptors(bleio_characteristic_obj_t *self) {
113126
if (self->descriptor_list == NULL) {
114127
return mp_const_empty_tuple;

ports/nordic/common-hal/_bleio/CharacteristicBuffer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static void write_to_ringbuf(bleio_characteristic_buffer_obj_t *self, uint8_t *d
2929
for (uint16_t i = 0; i < len; i++) {
3030
if (data[i] == mp_interrupt_char) {
3131
mp_sched_keyboard_interrupt();
32+
ringbuf_clear(&self->ringbuf);
3233
} else {
3334
ringbuf_put(&self->ringbuf, data[i]);
3435
}

ports/nordic/common-hal/microcontroller/__init__.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "supervisor/shared/safe_mode.h"
2222
#include "nrfx_glue.h"
2323
#include "nrf_nvic.h"
24+
#include "nrf_power.h"
2425

2526
// This routine should work even when interrupts are disabled. Used by OneWire
2627
// for precise timing.
@@ -61,10 +62,14 @@ void common_hal_mcu_enable_interrupts() {
6162

6263
void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
6364
enum { DFU_MAGIC_UF2_RESET = 0x57 };
65+
uint8_t new_value = 0;
6466
if (runmode == RUNMODE_BOOTLOADER || runmode == RUNMODE_UF2) {
65-
sd_power_gpregret_set(0, DFU_MAGIC_UF2_RESET);
66-
} else {
67-
sd_power_gpregret_set(0, 0);
67+
new_value = DFU_MAGIC_UF2_RESET;
68+
}
69+
int err_code = sd_power_gpregret_set(0, DFU_MAGIC_UF2_RESET);
70+
if (err_code != NRF_SUCCESS) {
71+
// Set it without the soft device if the SD failed. (It may be off.)
72+
nrf_power_gpregret_set(NRF_POWER, new_value);
6873
}
6974
if (runmode == RUNMODE_SAFE_MODE) {
7075
safe_mode_on_next_reset(SAFE_MODE_PROGRAMMATIC);

ports/silabs/common-hal/_bleio/Characteristic.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ void common_hal_bleio_characteristic_construct(
155155
}
156156
}
157157

158+
bool common_hal_bleio_characteristic_deinited(bleio_characteristic_obj_t *self) {
159+
return false;
160+
}
161+
162+
void common_hal_bleio_characteristic_deinit(bleio_characteristic_obj_t *self) {
163+
}
164+
158165
// A tuple of Descriptor that describe this characteristic
159166
mp_obj_tuple_t *common_hal_bleio_characteristic_get_descriptors(
160167
bleio_characteristic_obj_t *self) {

py/gc.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -773,13 +773,10 @@ void gc_info(gc_info_t *info) {
773773
GC_EXIT();
774774
}
775775

776-
// CIRCUITPY-CHANGE
776+
// CIRCUITPY-CHANGE: C code may be used when the VM heap isn't active. This
777+
// allows that code to test if it is. It can use the outer pool if needed.
777778
bool gc_alloc_possible(void) {
778-
#if MICROPY_GC_SPLIT_HEAP
779-
return MP_STATE_MEM(gc_last_free_area) != 0;
780-
#else
781779
return MP_STATE_MEM(area).gc_pool_start != 0;
782-
#endif
783780
}
784781

785782
void *gc_alloc(size_t n_bytes, unsigned int alloc_flags) {

shared-bindings/_bleio/Characteristic.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include "shared-bindings/_bleio/Characteristic.h"
1313
#include "shared-bindings/_bleio/Service.h"
1414
#include "shared-bindings/_bleio/UUID.h"
15+
#include "shared-bindings/util.h"
16+
1517

1618
//| class Characteristic:
1719
//| """Stores information about a BLE service characteristic and allows reading
@@ -138,14 +140,29 @@ static mp_obj_t bleio_characteristic_add_to_service(size_t n_args, const mp_obj_
138140
static MP_DEFINE_CONST_FUN_OBJ_KW(bleio_characteristic_add_to_service_fun_obj, 1, bleio_characteristic_add_to_service);
139141
static MP_DEFINE_CONST_CLASSMETHOD_OBJ(bleio_characteristic_add_to_service_obj, MP_ROM_PTR(&bleio_characteristic_add_to_service_fun_obj));
140142

143+
//| def deinit(self) -> None:
144+
//| """Deinitialises the Characteristic and releases any hardware resources for reuse."""
145+
//| ...
146+
static mp_obj_t bleio_characteristic_deinit(mp_obj_t self_in) {
147+
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
148+
common_hal_bleio_characteristic_deinit(self);
149+
return mp_const_none;
150+
}
151+
static MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_deinit_obj, bleio_characteristic_deinit);
141152

153+
static void check_for_deinit(bleio_characteristic_obj_t *self) {
154+
if (common_hal_bleio_characteristic_deinited(self)) {
155+
raise_deinited_error();
156+
}
157+
}
142158

143159
//| properties: int
144160
//| """An int bitmask representing which properties are set, specified as bitwise or'ing of
145161
//| of these possible values.
146162
//| `BROADCAST`, `INDICATE`, `NOTIFY`, `READ`, `WRITE`, `WRITE_NO_RESPONSE`."""
147163
static mp_obj_t bleio_characteristic_get_properties(mp_obj_t self_in) {
148164
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
165+
check_for_deinit(self);
149166

150167
return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_characteristic_get_properties(self));
151168
}
@@ -160,6 +177,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_properties_obj,
160177
//| Will be ``None`` if the 128-bit UUID for this characteristic is not known."""
161178
static mp_obj_t bleio_characteristic_get_uuid(mp_obj_t self_in) {
162179
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
180+
check_for_deinit(self);
163181

164182
bleio_uuid_obj_t *uuid = common_hal_bleio_characteristic_get_uuid(self);
165183
return uuid ? MP_OBJ_FROM_PTR(uuid) : mp_const_none;
@@ -173,6 +191,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_uuid_obj,
173191
//| """The value of this characteristic."""
174192
static mp_obj_t bleio_characteristic_get_value(mp_obj_t self_in) {
175193
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
194+
check_for_deinit(self);
176195

177196
uint8_t temp[512];
178197
size_t actual_len = common_hal_bleio_characteristic_get_value(self, temp, sizeof(temp));
@@ -182,6 +201,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_get_value_obj, bleio_chara
182201

183202
static mp_obj_t bleio_characteristic_set_value(mp_obj_t self_in, mp_obj_t value_in) {
184203
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
204+
check_for_deinit(self);
185205

186206
mp_buffer_info_t bufinfo;
187207
mp_get_buffer_raise(value_in, &bufinfo, MP_BUFFER_READ);
@@ -200,6 +220,7 @@ MP_PROPERTY_GETSET(bleio_characteristic_value_obj,
200220
//| """The max length of this characteristic."""
201221
static mp_obj_t bleio_characteristic_get_max_length(mp_obj_t self_in) {
202222
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
223+
check_for_deinit(self);
203224

204225
return MP_OBJ_NEW_SMALL_INT(common_hal_bleio_characteristic_get_max_length(self));
205226
}
@@ -212,6 +233,8 @@ MP_PROPERTY_GETTER(bleio_characteristic_max_length_obj,
212233
//| """A tuple of :py:class:`Descriptor` objects related to this characteristic. (read-only)"""
213234
static mp_obj_t bleio_characteristic_get_descriptors(mp_obj_t self_in) {
214235
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
236+
check_for_deinit(self);
237+
215238
// Return list as a tuple so user won't be able to change it.
216239
return MP_OBJ_FROM_PTR(common_hal_bleio_characteristic_get_descriptors(self));
217240
}
@@ -225,6 +248,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_descriptors_obj,
225248
//| """The Service this Characteristic is a part of."""
226249
static mp_obj_t bleio_characteristic_get_service(mp_obj_t self_in) {
227250
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
251+
check_for_deinit(self);
228252

229253
return common_hal_bleio_characteristic_get_service(self);
230254
}
@@ -242,6 +266,7 @@ MP_PROPERTY_GETTER(bleio_characteristic_service_obj,
242266
//| ...
243267
static mp_obj_t bleio_characteristic_set_cccd(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
244268
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
269+
check_for_deinit(self);
245270

246271
enum { ARG_notify, ARG_indicate };
247272
static const mp_arg_t allowed_args[] = {
@@ -259,6 +284,9 @@ static mp_obj_t bleio_characteristic_set_cccd(mp_uint_t n_args, const mp_obj_t *
259284
static MP_DEFINE_CONST_FUN_OBJ_KW(bleio_characteristic_set_cccd_obj, 1, bleio_characteristic_set_cccd);
260285

261286
static const mp_rom_map_elem_t bleio_characteristic_locals_dict_table[] = {
287+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&bleio_characteristic_deinit_obj) },
288+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&bleio_characteristic_deinit_obj) },
289+
262290
{ MP_ROM_QSTR(MP_QSTR_add_to_service), MP_ROM_PTR(&bleio_characteristic_add_to_service_obj) },
263291
{ MP_ROM_QSTR(MP_QSTR_descriptors), MP_ROM_PTR(&bleio_characteristic_descriptors_obj) },
264292
{ MP_ROM_QSTR(MP_QSTR_properties), MP_ROM_PTR(&bleio_characteristic_properties_obj) },

shared-bindings/_bleio/Characteristic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ extern size_t common_hal_bleio_characteristic_get_max_length(bleio_characteristi
2424
extern size_t common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self, uint8_t *buf, size_t len);
2525
extern void common_hal_bleio_characteristic_add_descriptor(bleio_characteristic_obj_t *self, bleio_descriptor_obj_t *descriptor);
2626
extern void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self, bleio_service_obj_t *service, uint16_t handle, bleio_uuid_obj_t *uuid, bleio_characteristic_properties_t props, bleio_attribute_security_mode_t read_perm, bleio_attribute_security_mode_t write_perm, mp_int_t max_length, bool fixed_length, mp_buffer_info_t *initial_value_bufinfo, const char *user_description);
27+
extern bool common_hal_bleio_characteristic_deinited(bleio_characteristic_obj_t *self);
28+
extern void common_hal_bleio_characteristic_deinit(bleio_characteristic_obj_t *self);
2729
extern void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self, bool notify, bool indicate);
2830
extern void common_hal_bleio_characteristic_set_value(bleio_characteristic_obj_t *self, mp_buffer_info_t *bufinfo);

supervisor/shared/bluetooth/bluetooth.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ void supervisor_stop_bluetooth(void) {
339339

340340
ble_started = false;
341341

342+
#if CIRCUITPY_BLE_FILE_SERVICE
343+
supervisor_stop_bluetooth_file_transfer();
344+
#endif
345+
342346
#if CIRCUITPY_SERIAL_BLE
343347
supervisor_stop_bluetooth_serial();
344348
#endif

supervisor/shared/bluetooth/file_transfer.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ void supervisor_start_bluetooth_file_transfer(void) {
101101
&static_handler_entry);
102102
}
103103

104+
void supervisor_stop_bluetooth_file_transfer(void) {
105+
common_hal_bleio_packet_buffer_deinit(&_transfer_packet_buffer);
106+
common_hal_bleio_characteristic_deinit(&supervisor_ble_transfer_characteristic);
107+
common_hal_bleio_characteristic_deinit(&supervisor_ble_version_characteristic);
108+
common_hal_bleio_service_deinit(&supervisor_ble_service);
109+
}
110+
104111
#define COMMAND_SIZE 1024
105112

106113
#define ANY_COMMAND 0x00

supervisor/shared/bluetooth/file_transfer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <stdbool.h>
1010

11-
void supervisor_bluetooth_file_transfer_background(void);
1211
void supervisor_start_bluetooth_file_transfer(void);
12+
void supervisor_stop_bluetooth_file_transfer(void);
13+
14+
void supervisor_bluetooth_file_transfer_background(void);
1315
void supervisor_bluetooth_file_transfer_disconnected(void);

supervisor/shared/bluetooth/serial.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ void supervisor_stop_bluetooth_serial(void) {
148148
return;
149149
}
150150
common_hal_bleio_packet_buffer_flush(&_tx_packet_buffer);
151+
common_hal_bleio_packet_buffer_deinit(&_tx_packet_buffer);
152+
common_hal_bleio_characteristic_deinit(&supervisor_ble_circuitpython_rx_characteristic);
153+
common_hal_bleio_characteristic_deinit(&supervisor_ble_circuitpython_tx_characteristic);
154+
common_hal_bleio_characteristic_deinit(&supervisor_ble_circuitpython_version_characteristic);
155+
common_hal_bleio_service_deinit(&supervisor_ble_circuitpython_service);
151156
}
152157

153158
bool ble_serial_connected(void) {

0 commit comments

Comments
 (0)