Skip to content

Commit f66f55b

Browse files
committed
add CharacteristicBuffer; UART seems to work!
1 parent a77b236 commit f66f55b

17 files changed

Lines changed: 395 additions & 79 deletions

ports/nrf/.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Old Nordic soft devices that don't allow redistribution
22
#########################################################
3-
bluetooth/s132_nrf52_2.0.1/
3+
drivers/bluetooth/s132_nrf52_2.0.1/
44

5-
!bluetooth/*/*.hex
5+
!drivers/bluetooth/*/*.hex
66

77
# Build files
88
#####################

ports/nrf/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ SRC_COMMON_HAL += \
174174
bleio/Adapter.c \
175175
bleio/Broadcaster.c \
176176
bleio/Characteristic.c \
177+
bleio/CharacteristicBuffer.c \
177178
bleio/Descriptor.c \
178179
bleio/Peripheral.c \
179180
bleio/Scanner.c \

ports/nrf/bluetooth/ble_drv.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ void ble_drv_add_event_handler(ble_drv_evt_handler_t func, void *param) {
7171
m_event_handlers = handler;
7272
}
7373

74+
void ble_drv_remove_event_handler(ble_drv_evt_handler_t func, void *param) {
75+
event_handler_t *it = m_event_handlers;
76+
event_handler_t **prev = &m_event_handlers;
77+
while (it != NULL) {
78+
if ((it->func == func) && (it->param == param)) {
79+
// Splice out the matching handler.
80+
*prev = it->next;
81+
return;
82+
}
83+
prev = &(it->next);
84+
it = it->next;
85+
}
86+
}
87+
7488
void SD_EVT_IRQHandler(void) {
7589
uint32_t evt_id;
7690
while (sd_evt_get(&evt_id) != NRF_ERROR_NOT_FOUND) {

ports/nrf/bluetooth/ble_drv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ typedef void (*ble_drv_evt_handler_t)(ble_evt_t*, void*);
5252

5353
void ble_drv_reset();
5454
void ble_drv_add_event_handler(ble_drv_evt_handler_t func, void *param);
55+
void ble_drv_remove_event_handler(ble_drv_evt_handler_t func, void *param);
5556

5657
#endif // MICROPY_INCLUDED_NRF_BLUETOOTH_BLE_DRV_H

ports/nrf/bluetooth/download_ble_stack.sh

Lines changed: 0 additions & 32 deletions
This file was deleted.

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

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include "py/runtime.h"
3535
#include "common-hal/bleio/__init__.h"
36+
#include "common-hal/bleio/Characteristic.h"
3637
#include "shared-module/bleio/Characteristic.h"
3738

3839
// TODO - should these be per object?? *****
@@ -190,28 +191,41 @@ STATIC void gattc_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in
190191

191192
STATIC void characteristic_on_ble_evt(ble_evt_t *ble_evt, void *param) {
192193
switch (ble_evt->header.evt_id) {
193-
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
194-
m_tx_in_progress -= ble_evt->evt.gatts_evt.params.hvn_tx_complete.count;
195-
break;
196-
197-
case BLE_GATTC_EVT_READ_RSP:
198-
{
199-
ble_gattc_evt_read_rsp_t *response = &ble_evt->evt.gattc_evt.params.read_rsp;
200-
m_read_characteristic->value_data = mp_obj_new_bytearray(response->len, response->data);
201-
// Flag to busy-wait loop that we've read the characteristic.
202-
m_read_characteristic = NULL;
203-
break;
204-
}
194+
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
195+
m_tx_in_progress -= ble_evt->evt.gatts_evt.params.hvn_tx_complete.count;
196+
break;
197+
198+
case BLE_GATTC_EVT_READ_RSP:
199+
{
200+
ble_gattc_evt_read_rsp_t *response = &ble_evt->evt.gattc_evt.params.read_rsp;
201+
m_read_characteristic->value_data = mp_obj_new_bytearray(response->len, response->data);
202+
// Flag to busy-wait loop that we've read the characteristic.
203+
m_read_characteristic = NULL;
204+
break;
205+
}
206+
207+
case BLE_GATTC_EVT_WRITE_RSP:
208+
// Someone else can write now.
209+
sd_mutex_release(m_write_mutex);
210+
break;
205211

206-
case BLE_GATTC_EVT_WRITE_RSP:
207-
// Someone else can write now.
208-
sd_mutex_release(m_write_mutex);
209-
break;
212+
// For debugging.
213+
default:
214+
mp_printf(&mp_plat_print, "Unhandled characteristic event: 0x%04x\n", ble_evt->header.evt_id);
215+
break;
210216
}
217+
211218
}
212219

213-
void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self) {
214-
ble_drv_add_event_handler(characteristic_on_ble_evt, NULL);
220+
void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self, bleio_uuid_obj_t *uuid, bleio_characteristic_properties_t props) {
221+
self->service = NULL;
222+
self->uuid = uuid;
223+
self->value_data = NULL;
224+
self->props = props;
225+
self->handle = BLE_GATT_HANDLE_INVALID;
226+
227+
ble_drv_add_event_handler(characteristic_on_ble_evt, self);
228+
215229
}
216230

217231
void common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self) {
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) 2018 Artur Pacholec
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_COMMON_HAL_BLEIO_CHARACTERISTIC_H
28+
#define MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTIC_H
29+
30+
#include "shared-module/bleio/Characteristic.h"
31+
#include "shared-module/bleio/Service.h"
32+
#include "common-hal/bleio/UUID.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
bleio_service_obj_t *service;
37+
bleio_uuid_obj_t *uuid;
38+
mp_obj_t value_data;
39+
uint16_t handle;
40+
bleio_characteristic_properties_t props;
41+
uint16_t user_desc_handle;
42+
uint16_t cccd_handle;
43+
uint16_t sccd_handle;
44+
} bleio_characteristic_obj_t;
45+
46+
#endif // MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTIC_H
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Artur Pacholec
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 <string.h>
28+
#include <stdio.h>
29+
30+
#include "ble_drv.h"
31+
#include "ble_gatts.h"
32+
#include "nrf_soc.h"
33+
34+
#include "py/runtime.h"
35+
36+
#include "common-hal/bleio/__init__.h"
37+
#include "common-hal/bleio/CharacteristicBuffer.h"
38+
39+
STATIC void characteristic_buffer_on_ble_evt(ble_evt_t *ble_evt, void *param) {
40+
bleio_characteristic_buffer_obj_t *self = (bleio_characteristic_buffer_obj_t *) param;
41+
switch (ble_evt->header.evt_id) {
42+
case BLE_GATTS_EVT_WRITE: {
43+
ble_gatts_evt_write_t *evt_write = &ble_evt->evt.gatts_evt.params.write;
44+
// Event handle must match the handle for my characteristic.
45+
if (evt_write->handle == self->characteristic->handle) {
46+
// Push all the data onto the ring buffer.
47+
for (size_t i = 0; i < evt_write->len; i++) {
48+
ringbuf_put(&self->ringbuf, evt_write->data[i]);
49+
}
50+
break;
51+
}
52+
}
53+
}
54+
55+
}
56+
57+
// Assumes that buffer_size has been validated before call.
58+
void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffer_obj_t *self, bleio_characteristic_obj_t *characteristic, size_t buffer_size) {
59+
60+
self->characteristic = characteristic;
61+
// This is a macro.
62+
ringbuf_alloc(&self->ringbuf, buffer_size);
63+
64+
ble_drv_add_event_handler(characteristic_buffer_on_ble_evt, self);
65+
66+
}
67+
68+
// Returns a uint8_t byte value, or -1 if no data is available.
69+
int common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_t *self) {
70+
return ringbuf_get(&self->ringbuf);
71+
}
72+
73+
void common_hal_bleio_characteristic_buffer_deinit(bleio_characteristic_buffer_obj_t *self) {
74+
ble_drv_remove_event_handler(characteristic_buffer_on_ble_evt, self);
75+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Artur Pacholec
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_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H
28+
#define MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H
29+
30+
#include "py/ringbuf.h"
31+
32+
#include "shared-bindings/bleio/Characteristic.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
bleio_characteristic_obj_t *characteristic;
37+
// Ring buffer storing consecutive incoming values.
38+
ringbuf_t ringbuf;
39+
} bleio_characteristic_buffer_obj_t;
40+
41+
#endif // MICROPY_INCLUDED_COMMON_HAL_BLEIO_CHARACTERISTICBUFFER_H

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
268268
}
269269

270270
default:
271-
mp_printf(&mp_plat_print, "Unhandled event: 0x%04x\n", ble_evt->header.evt_id);
271+
mp_printf(&mp_plat_print, "Unhandled peripheral event: 0x%04x\n", ble_evt->header.evt_id);
272272
break;
273273
}
274274
}

0 commit comments

Comments
 (0)