Skip to content

Commit 9c167af

Browse files
committed
wip; redid flash writing to be compatible with SD
1 parent 390337b commit 9c167af

File tree

11 files changed

+185
-23
lines changed

11 files changed

+185
-23
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,5 +670,4 @@ void bleio_adapter_reset(bleio_adapter_obj_t* adapter) {
670670
bleio_connection_internal_t *connection = &connections[i];
671671
connection->connection_obj = mp_const_none;
672672
}
673-
bonding_reset();
674673
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self,
133133
common_hal_bleio_characteristic_set_value(self, initial_value_bufinfo);
134134
}
135135

136-
ble_drv_add_event_handler(characteristic_on_ble_evt, self);
136+
self->handler_entry.next = NULL;
137+
//////////////// ble_drv_add_event_handler_entry(&self->handler_entry, characteristic_on_ble_evt, self);
137138
}
138139

139140
bleio_descriptor_obj_t *common_hal_bleio_characteristic_get_descriptor_list(bleio_characteristic_obj_t *self) {
@@ -287,3 +288,8 @@ void common_hal_bleio_characteristic_set_cccd(bleio_characteristic_obj_t *self,
287288
}
288289

289290
}
291+
292+
void common_hal_bleio_characteristic_del(bleio_characteristic_obj_t *self) {
293+
// Remove from event handler list, since the evt handler entry is built-in and not a heap object.
294+
ble_drv_remove_event_handler(characteristic_on_ble_evt, self);
295+
}

ports/nrf/common-hal/_bleio/Characteristic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct _bleio_characteristic_obj {
4747
bleio_attribute_security_mode_t read_perm;
4848
bleio_attribute_security_mode_t write_perm;
4949
bleio_descriptor_obj_t *descriptor_list;
50+
ble_drv_evt_handler_entry_t handler_entry;
5051
uint16_t user_desc_handle;
5152
uint16_t cccd_handle;
5253
uint16_t sccd_handle;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ void bleio_reset() {
9494
common_hal_bleio_adapter_set_enabled(&common_hal_bleio_adapter_obj, false);
9595
}
9696
supervisor_start_bluetooth();
97+
bonding_reset();
9798
}
9899

99100
// The singleton _bleio.Adapter object, bound to _bleio.adapter

ports/nrf/common-hal/_bleio/bonding.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "shared-bindings/_bleio/Adapter.h"
3535
#include "shared-bindings/nvm/ByteArray.h"
3636

37-
#include "nrf_nvmc.h"
37+
#include "nrf_soc.h"
3838
#include "sd_mutex.h"
3939

4040
#include "bonding.h"
@@ -68,15 +68,17 @@ STATIC inline size_t compute_block_size(uint16_t data_length) {
6868

6969
STATIC void erase_bonding_storage(void) {
7070
// Erase all pages in the bonding area.
71-
BONDING_DEBUG_PRINTF("erase_bonding_storage()\n");
7271
for(uint32_t page_address = BONDING_PAGES_START_ADDR;
7372
page_address < BONDING_PAGES_END_ADDR;
7473
page_address += FLASH_PAGE_SIZE) {
75-
nrf_nvmc_page_erase(page_address);
74+
// Argument is page number, not address.
75+
sd_flash_page_erase_sync(page_address / FLASH_PAGE_SIZE);
7676
}
7777
// Write marker words at the beginning and the end of the bonding area.
78-
nrf_nvmc_write_word(BONDING_DATA_START_ADDR, BONDING_START_FLAG_ADDR);
79-
nrf_nvmc_write_word(BONDING_DATA_END_ADDR, BONDING_END_FLAG_ADDR);
78+
uint32_t flag = BONDING_START_FLAG;
79+
sd_flash_write_sync((uint32_t *) BONDING_DATA_START_ADDR, &flag, 1);
80+
flag = BONDING_END_FLAG;
81+
sd_flash_write_sync((uint32_t *) BONDING_DATA_END_ADDR, &flag, 1);
8082
// First unused block is at the beginning.
8183
bonding_unused_block = (bonding_block_t *) BONDING_DATA_START_ADDR;
8284
}
@@ -135,7 +137,8 @@ STATIC bonding_block_t *find_block(bool is_central, bonding_block_type_t type, u
135137
// We don't change data_length, so we can still skip over this block.
136138
STATIC void invalidate_block(bonding_block_t *block) {
137139
BONDING_DEBUG_PRINTF("invalidate_block()\n");
138-
nrf_nvmc_write_word((uint32_t) block, 0x00000000);
140+
uint32_t zero = 0;
141+
sd_flash_write_sync((uint32_t *) block, &zero, 1);
139142
}
140143

141144
STATIC void queue_write_block(bool is_central, bonding_block_type_t type, uint16_t ediv, uint16_t conn_handle, uint8_t *data, uint16_t data_length) {
@@ -178,7 +181,7 @@ STATIC void write_block_header(bonding_block_t *block) {
178181
erase_bonding_storage();
179182
}
180183

181-
nrf_nvmc_write_words((uint32_t) bonding_unused_block, (uint32_t *) block, sizeof(bonding_block_t) / 4);
184+
sd_flash_write_sync((uint32_t *) bonding_unused_block, (uint32_t *) block, sizeof(bonding_block_t) / 4);
182185
}
183186

184187
// Write variable-length data at end of bonding block.
@@ -190,7 +193,7 @@ STATIC void write_block_data(uint8_t *data, uint16_t data_length) {
190193
while (1) {
191194
uint32_t word = 0xffffffff;
192195
memcpy(&word, data, data_length >= 4 ? 4 : data_length);
193-
nrf_nvmc_write_word((uint32_t) flash_word_p, word);
196+
sd_flash_write_sync(flash_word_p, &word, 1);
194197
if (data_length <= 4) {
195198
break;
196199
}
@@ -242,8 +245,9 @@ void bonding_clear_keys(bonding_keys_t *bonding_keys) {
242245
memset((uint8_t*) bonding_keys, 0, sizeof(bonding_keys_t));
243246
}
244247

248+
// Call only when SD is enabled.
245249
void bonding_reset(void) {
246-
BONDING_DEBUG_PRINTF("bonding_reset()\n");
250+
MP_STATE_VM(queued_bonding_block_list) = NULL;
247251
sd_mutex_new(&queued_bonding_block_list_mutex);
248252
if (BONDING_START_FLAG != *((uint32_t *) BONDING_START_FLAG_ADDR) ||
249253
BONDING_END_FLAG != *((uint32_t *) BONDING_END_FLAG_ADDR)) {
@@ -256,18 +260,20 @@ void bonding_reset(void) {
256260
// Write bonding blocks to flash. These have been queued during event handlers.
257261
// We do one at a time, on each background call.
258262
void bonding_background(void) {
259-
263+
uint8_t sd_en = 0;
264+
(void) sd_softdevice_is_enabled(&sd_en);
265+
if (!sd_en) {
266+
return;
267+
}
260268
// Get block at front of list.
261269
sd_mutex_acquire_wait(&queued_bonding_block_list_mutex);
262-
bonding_block_t *block = &(MP_STATE_VM(queued_bonding_block_list)->bonding_block);
270+
bonding_block_t *block = (bonding_block_t *) MP_STATE_VM(queued_bonding_block_list);
263271
if (block) {
264-
// Remove written block from front of list.
272+
// Remove block from list.
265273
MP_STATE_VM(queued_bonding_block_list) = MP_STATE_VM(queued_bonding_block_list)->next_queued_block;
266274
}
267275
sd_mutex_release(&queued_bonding_block_list_mutex);
268-
269276
if (!block) {
270-
// No blocks in queue.
271277
return;
272278
}
273279

ports/nrf/peripherals/nrf/nvm.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,62 @@
3838
#include "ble_drv.h"
3939
#include "nrf_sdm.h"
4040

41+
STATIC bool sd_is_enabled(void) {
42+
uint8_t sd_en = 0;
43+
(void) sd_softdevice_is_enabled(&sd_en);
44+
return sd_en;
45+
}
46+
4147
STATIC void sd_flash_operation_start(void) {
4248
sd_flash_operation_status = SD_FLASH_OPERATION_IN_PROGRESS;
4349
}
4450

4551
STATIC sd_flash_operation_status_t sd_flash_operation_wait_until_done(void) {
46-
while (sd_flash_operation_status == SD_FLASH_OPERATION_IN_PROGRESS) {
47-
sd_app_evt_wait();
52+
// If the SD is not enabled, no events are generated, so just return immediately.
53+
if (sd_is_enabled()) {
54+
while (sd_flash_operation_status == SD_FLASH_OPERATION_IN_PROGRESS) {
55+
sd_app_evt_wait();
56+
}
57+
} else {
58+
sd_flash_operation_status = SD_FLASH_OPERATION_DONE;
4859
}
4960
return sd_flash_operation_status;
61+
5062
}
63+
64+
bool sd_flash_page_erase_sync(uint32_t page_number) {
65+
sd_flash_operation_start();
66+
if (sd_flash_page_erase(page_number) != NRF_SUCCESS) {
67+
return false;
68+
}
69+
if (sd_flash_operation_wait_until_done() == SD_FLASH_OPERATION_ERROR) {
70+
return false;
71+
}
72+
return true;
73+
}
74+
75+
bool sd_flash_write_sync(uint32_t *dest_words, uint32_t* src_words, uint32_t num_words) {
76+
sd_flash_operation_start();
77+
if (sd_flash_write(dest_words, src_words, num_words) != NRF_SUCCESS) {
78+
return false;
79+
}
80+
if (sd_flash_operation_wait_until_done() == SD_FLASH_OPERATION_ERROR) {
81+
return false;
82+
}
83+
return true;
84+
}
85+
5186
#endif
5287

5388
// The nRF52840 datasheet specifies a maximum of two writes to a flash
5489
// location before an erase is necessary, even if the write is all
5590
// ones (erased state). So we can't avoid erases even if the page
5691
// appears to be already erased (all ones), unless we keep track of
57-
// writes to a page.
92+
// writes to a page.g
5893

5994
bool nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data) {
6095
#ifdef BLUETOOTH_SD
61-
uint8_t sd_en = 0;
62-
(void) sd_softdevice_is_enabled(&sd_en);
63-
if (sd_en) {
96+
if (sd_is_enabled()) {
6497
uint32_t err_code;
6598
sd_flash_operation_status_t status;
6699

ports/nrf/peripherals/nrf/nvm.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@
2727

2828
#define FLASH_PAGE_SIZE (4096)
2929

30+
#if BLUETOOTH_SD
31+
bool sd_flash_page_erase_sync(uint32_t page_number);
32+
bool sd_flash_write_sync(uint32_t *dest_words, uint32_t* src_words, uint32_t num_words);
33+
#endif
34+
3035
bool nrf_nvm_safe_flash_page_write(uint32_t page_addr, uint8_t *data);

ports/nrf/sd.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/mpconfig.h"
28+
#include "py/runtime.h"
29+
#include "nrf_soc.h"
30+
31+
void sd_mutex_acquire_check(nrf_mutex_t* p_mutex) {
32+
uint32_t err_code = sd_mutex_acquire(p_mutex);
33+
if (err_code != NRF_SUCCESS) {
34+
mp_raise_OSError_msg_varg(translate("Failed to acquire mutex, err 0x%04x"), err_code);
35+
}
36+
}
37+
38+
void sd_mutex_acquire_wait(nrf_mutex_t* p_mutex) {
39+
while (sd_mutex_acquire(p_mutex) == NRF_ERROR_SOC_MUTEX_ALREADY_TAKEN) {
40+
RUN_BACKGROUND_TASKS;
41+
}
42+
}
43+
44+
void sd_mutex_acquire_wait_no_vm(nrf_mutex_t* p_mutex) {
45+
while (sd_mutex_acquire(p_mutex) == NRF_ERROR_SOC_MUTEX_ALREADY_TAKEN) {
46+
}
47+
}
48+
49+
void sd_mutex_release_check(nrf_mutex_t* p_mutex) {
50+
uint32_t err_code = sd_mutex_release(p_mutex);
51+
if (err_code != NRF_SUCCESS) {
52+
mp_raise_OSError_msg_varg(translate("Failed to release mutex, err 0x%04x"), err_code);
53+
}
54+
}

ports/nrf/sd.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2019 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_NRF_SD_MUTEX_H
28+
#define MICROPY_INCLUDED_NRF_SD_MUTEX_H
29+
30+
#include "nrf_soc.h"
31+
32+
// Helpers for common usage of nrf_mutex.
33+
34+
// Try to acquire a mutex right now. Raise exception if we can't get it.
35+
void sd_mutex_acquire_check(nrf_mutex_t* p_mutex);
36+
37+
// Wait for a mutex to become available. Run VM background tasks while waiting.
38+
void sd_mutex_acquire_wait(nrf_mutex_t* p_mutex);
39+
40+
// Wait for a mutex to become available.. Block VM while waiting.
41+
void sd_mutex_acquire_wait_no_vm(nrf_mutex_t* p_mutex);
42+
43+
// Release a mutex, and raise exception on error.
44+
void sd_mutex_release_check(nrf_mutex_t* p_mutex);
45+
46+
#endif // MICROPY_INCLUDED_NRF_SD_MUTEX_H

shared-bindings/_bleio/Characteristic.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ STATIC mp_obj_t bleio_characteristic_add_to_service(size_t n_args, const mp_obj_
128128
}
129129
mp_get_buffer_raise(initial_value, &initial_value_bufinfo, MP_BUFFER_READ);
130130

131-
bleio_characteristic_obj_t *characteristic = m_new_obj(bleio_characteristic_obj_t);
131+
// There may be some cleanup needed when a characteristic is gc'd, so enable finaliser.
132+
bleio_characteristic_obj_t *characteristic = m_new_obj_with_finaliser(bleio_characteristic_obj_t);
132133
characteristic->base.type = &bleio_characteristic_type;
133134

134135
// Range checking on max_length arg is done by the common_hal layer, because
@@ -292,8 +293,17 @@ STATIC mp_obj_t bleio_characteristic_set_cccd(mp_uint_t n_args, const mp_obj_t *
292293
}
293294
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(bleio_characteristic_set_cccd_obj, 1, bleio_characteristic_set_cccd);
294295

296+
// Cleanup on gc.
297+
STATIC mp_obj_t bleio_characteristic_del(mp_obj_t self_in) {
298+
bleio_characteristic_obj_t *self = MP_OBJ_TO_PTR(self_in);
299+
common_hal_bleio_characteristic_del(self);
300+
return mp_const_none;
301+
}
302+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(bleio_characteristic_del_obj, bleio_characteristic_del);
303+
295304

296305
STATIC const mp_rom_map_elem_t bleio_characteristic_locals_dict_table[] = {
306+
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&bleio_characteristic_del_obj) },
297307
{ MP_ROM_QSTR(MP_QSTR_add_to_service), MP_ROM_PTR(&bleio_characteristic_add_to_service_obj) },
298308
{ MP_ROM_QSTR(MP_QSTR_properties), MP_ROM_PTR(&bleio_characteristic_properties_obj) },
299309
{ MP_ROM_QSTR(MP_QSTR_uuid), MP_ROM_PTR(&bleio_characteristic_uuid_obj) },

0 commit comments

Comments
 (0)