2929
3030#include "ble_drv.h"
3131#include "ble_gatts.h"
32- #include "sd_mutex .h"
32+ #include "nrf_nvic .h"
3333
3434#include "lib/utils/interrupt_char.h"
3535#include "py/runtime.h"
@@ -47,14 +47,14 @@ STATIC void characteristic_buffer_on_ble_evt(ble_evt_t *ble_evt, void *param) {
4747 ble_gatts_evt_write_t * evt_write = & ble_evt -> evt .gatts_evt .params .write ;
4848 // Event handle must match the handle for my characteristic.
4949 if (evt_write -> handle == self -> characteristic -> handle ) {
50- // Push all the data onto the ring buffer, but wait for any reads to finish.
51- sd_mutex_acquire_wait_no_vm (& self -> ringbuf_mutex );
50+ // Push all the data onto the ring buffer.
51+ uint8_t is_nested_critical_region ;
52+ sd_nvic_critical_region_enter (& is_nested_critical_region );
5253 for (size_t i = 0 ; i < evt_write -> len ; i ++ ) {
5354 ringbuf_put (& self -> ringbuf , evt_write -> data [i ]);
5455 }
55- // Don't check for errors: we're in an event handler.
56- sd_mutex_release (& self -> ringbuf_mutex );
57- break ;
56+ sd_nvic_critical_region_exit (is_nested_critical_region );
57+ break ;
5858 }
5959 }
6060 }
@@ -72,7 +72,6 @@ void common_hal_bleio_characteristic_buffer_construct(bleio_characteristic_buffe
7272 // This is a macro.
7373 // true means long-lived, so it won't be moved.
7474 ringbuf_alloc (& self -> ringbuf , buffer_size , true);
75- sd_mutex_new (& self -> ringbuf_mutex );
7675
7776 ble_drv_add_event_handler (characteristic_buffer_on_ble_evt , self );
7877
@@ -92,29 +91,35 @@ int common_hal_bleio_characteristic_buffer_read(bleio_characteristic_buffer_obj_
9291#endif
9392 }
9493
95- // Copy received data. Lock out writes while copying.
96- sd_mutex_acquire_wait (& self -> ringbuf_mutex );
94+ // Copy received data. Lock out write interrupt handler while copying.
95+ uint8_t is_nested_critical_region ;
96+ sd_nvic_critical_region_enter (& is_nested_critical_region );
9797
9898 size_t rx_bytes = MIN (ringbuf_count (& self -> ringbuf ), len );
9999 for ( size_t i = 0 ; i < rx_bytes ; i ++ ) {
100100 data [i ] = ringbuf_get (& self -> ringbuf );
101101 }
102102
103103 // Writes now OK.
104- sd_mutex_release_check ( & self -> ringbuf_mutex );
104+ sd_nvic_critical_region_exit ( is_nested_critical_region );
105105
106106 return rx_bytes ;
107107}
108108
109109uint32_t common_hal_bleio_characteristic_buffer_rx_characters_available (bleio_characteristic_buffer_obj_t * self ) {
110- return ringbuf_count (& self -> ringbuf );
110+ uint8_t is_nested_critical_region ;
111+ sd_nvic_critical_region_enter (& is_nested_critical_region );
112+ uint16_t count = ringbuf_count (& self -> ringbuf );
113+ sd_nvic_critical_region_exit (is_nested_critical_region );
114+ return count ;
111115}
112116
113117void common_hal_bleio_characteristic_buffer_clear_rx_buffer (bleio_characteristic_buffer_obj_t * self ) {
114118 // prevent conflict with uart irq
115- sd_mutex_acquire_wait (& self -> ringbuf_mutex );
119+ uint8_t is_nested_critical_region ;
120+ sd_nvic_critical_region_enter (& is_nested_critical_region );
116121 ringbuf_clear (& self -> ringbuf );
117- sd_mutex_release_check ( & self -> ringbuf_mutex );
122+ sd_nvic_critical_region_exit ( is_nested_critical_region );
118123}
119124
120125bool common_hal_bleio_characteristic_buffer_deinited (bleio_characteristic_buffer_obj_t * self ) {
0 commit comments