Skip to content

Commit 7e9a404

Browse files
committed
Added minimum delay from stepper enable to first step pulse.
Added low level driver support for WIZnet SPI based ethernet breakout boards, updated SPI interface for DMA transfer. NOTE: Build support for ethernet is not yet ready!
1 parent 078836e commit 7e9a404

File tree

9 files changed

+819
-333
lines changed

9 files changed

+819
-333
lines changed

bluetooth.c

+62-13
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ typedef struct {
6363

6464
const int RFCOMM_SERVER_CHANNEL = 1;
6565

66+
static const io_stream_t *claim_stream (uint32_t baud_rate);
67+
6668
static bool is_up = false;
6769
static bluetooth_settings_t bluetooth;
6870
static stream_rx_buffer_t rxbuffer = {0};
@@ -71,6 +73,16 @@ static nvs_address_t nvs_address;
7173
static enqueue_realtime_command_ptr enqueue_realtime_command = protocol_enqueue_realtime_command;
7274
static bt_session_t session;
7375
static uint8_t spp_service_buffer[150];
76+
static io_stream_properties_t bt_stream = {
77+
.type = StreamType_Bluetooth,
78+
.instance = 20,
79+
.flags.claimable = On,
80+
.flags.claimed = Off,
81+
.flags.connected = Off,
82+
.flags.can_set_baud = On,
83+
.flags.modbus_ready = Off,
84+
.claim = claim_stream
85+
};
7486
static on_report_options_ptr on_report_options;
7587
static on_execute_realtime_ptr on_execute_realtime;
7688

@@ -235,6 +247,8 @@ static void msg_bt_open_failed (sys_state_t state)
235247
hal.stream.write_all("]" ASCII_EOL);
236248
}
237249

250+
#if PICO_CYW43_ARCH_POLL && !WIFI_ENABLE
251+
238252
static void bt_poll (sys_state_t state)
239253
{
240254
static uint32_t last_ms0;
@@ -249,11 +263,18 @@ static void bt_poll (sys_state_t state)
249263
on_execute_realtime(state);
250264
}
251265

252-
static void packetHandler (uint8_t type, uint16_t channel, uint8_t *packet, uint16_t size)
266+
#endif
267+
268+
static bool is_connected (void)
253269
{
254-
static const io_stream_t bluetooth_stream = {
270+
return bt_stream.flags.connected;
271+
}
272+
273+
static const io_stream_t *claim_stream (uint32_t baud_rate)
274+
{
275+
static const io_stream_t stream = {
255276
.type = StreamType_Bluetooth,
256-
.state.connected = true,
277+
.is_connected = is_connected,
257278
.read = BTStreamGetC,
258279
.write = BTStreamWriteS,
259280
.write_char = BTStreamPutC,
@@ -263,8 +284,20 @@ static void packetHandler (uint8_t type, uint16_t channel, uint8_t *packet, uint
263284
.set_enqueue_rt_handler = BTSetRtHandler
264285
};
265286

287+
if(bt_stream.flags.claimed || bt_stream.flags.connected)
288+
return NULL;
289+
290+
if(baud_rate != 0)
291+
bt_stream.flags.claimed = On;
292+
293+
return &stream;
294+
}
295+
296+
static void packetHandler (uint8_t type, uint16_t channel, uint8_t *packet, uint16_t size)
297+
{
266298
static uint16_t mtu;
267299
static uint8_t tx_buf[TX_BUFFER_SIZE];
300+
static const io_stream_t *stream = NULL;
268301

269302
UNUSED(channel);
270303

@@ -292,15 +325,22 @@ static void packetHandler (uint8_t type, uint16_t channel, uint8_t *packet, uint
292325
if((session.status = rfcomm_event_channel_opened_get_status(packet)))
293326
protocol_enqueue_rt_command(msg_bt_open_failed);
294327
else {
295-
296-
mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
297-
session.channel = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
298-
session.connected = true;
299-
300-
rxbuffer.tail = rxbuffer.head; // Flush rx & tx
301-
txbuffer.tail = txbuffer.head; // buffers.
328+
if(!bt_stream.flags.connected) {
329+
330+
rxbuffer.tail = rxbuffer.head; // Flush rx & tx
331+
txbuffer.tail = txbuffer.head; // buffers.
302332

303-
stream_connect(&bluetooth_stream);
333+
if(bt_stream.flags.claimed)
334+
bt_stream.flags.connected = On;
335+
else if(!bt_stream.flags.connected && (stream = claim_stream(0)))
336+
bt_stream.flags.connected = stream_connect(stream);
337+
338+
mtu = rfcomm_event_channel_opened_get_max_frame_size(packet);
339+
session.channel = rfcomm_event_channel_opened_get_rfcomm_cid(packet);
340+
session.connected = bt_stream.flags.connected;
341+
} // else deny connection...
342+
343+
// if(!bt_stream.flags.connected) deny connection...
304344
}
305345
break;
306346

@@ -327,7 +367,11 @@ static void packetHandler (uint8_t type, uint16_t channel, uint8_t *packet, uint
327367
session.channel = 0;
328368
session.connected = false;
329369
*session.client_mac = '\0';
330-
stream_disconnect(&bluetooth_stream);
370+
if(stream) {
371+
stream_disconnect(stream);
372+
stream = NULL;
373+
}
374+
bt_stream.flags.connected = Off;
331375
break;
332376

333377
default:
@@ -367,6 +411,10 @@ bool bluetooth_start_local (void)
367411
{
368412
static char device_name[52];
369413
static btstack_packet_callback_registration_t hci_event_callback;
414+
static io_stream_details_t streams = {
415+
.n_streams = 1,
416+
.streams = &bt_stream,
417+
};
370418

371419
if(*bluetooth.device_name == '\0')
372420
return false;
@@ -400,6 +448,8 @@ bool bluetooth_start_local (void)
400448

401449
hci_power_control(HCI_POWER_ON);
402450

451+
stream_register_streams(&streams);
452+
403453
is_up = true;
404454

405455
#if PICO_CYW43_ARCH_POLL && !WIFI_ENABLE
@@ -417,7 +467,6 @@ bool bluetooth_disable (void)
417467
return true;
418468
}
419469

420-
421470
static const setting_group_detail_t bluetooth_groups [] = {
422471
{ Group_Root, Group_Bluetooth, "Bluetooth"},
423472
};

0 commit comments

Comments
 (0)