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) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy