|
1 | 1 | use core::{cell::RefCell, ptr::addr_of}; |
2 | 2 |
|
3 | 3 | use critical_section::Mutex; |
| 4 | +use portable_atomic::{AtomicBool, Ordering}; |
4 | 5 |
|
5 | 6 | use crate::ble::btdm::ble_os_adapter_chip_specific::{osi_funcs_s, G_OSI_FUNCS}; |
6 | 7 | use crate::ble::HciOutCollector; |
@@ -31,6 +32,8 @@ pub struct ReceivedPacket { |
31 | 32 | static BT_INTERNAL_QUEUE: Mutex<RefCell<SimpleQueue<[u8; 8], 10>>> = |
32 | 33 | Mutex::new(RefCell::new(SimpleQueue::new())); |
33 | 34 |
|
| 35 | +static PACKET_SENT: AtomicBool = AtomicBool::new(true); |
| 36 | + |
34 | 37 | #[repr(C)] |
35 | 38 | struct vhci_host_callback_s { |
36 | 39 | notify_host_send_available: extern "C" fn(), /* callback used to notify that the host can send packet to controller */ |
@@ -64,6 +67,8 @@ static VHCI_HOST_CALLBACK: vhci_host_callback_s = vhci_host_callback_s { |
64 | 67 |
|
65 | 68 | extern "C" fn notify_host_send_available() { |
66 | 69 | trace!("notify_host_send_available"); |
| 70 | + |
| 71 | + PACKET_SENT.store(true, Ordering::Relaxed); |
67 | 72 | } |
68 | 73 |
|
69 | 74 | extern "C" fn notify_host_recv(data: *mut u8, len: u16) -> i32 { |
@@ -581,13 +586,17 @@ pub fn send_hci(data: &[u8]) { |
581 | 586 | continue; |
582 | 587 | } |
583 | 588 |
|
| 589 | + PACKET_SENT.store(false, Ordering::Relaxed); |
584 | 590 | API_vhci_host_send_packet(packet.as_ptr() as *const u8, packet.len() as u16); |
585 | 591 | trace!("sent vhci host packet"); |
586 | 592 |
|
587 | 593 | dump_packet_info(packet); |
588 | 594 |
|
589 | 595 | break; |
590 | 596 | } |
| 597 | + |
| 598 | + // make sure the packet buffer doesn't get touched until sent |
| 599 | + while !PACKET_SENT.load(Ordering::Relaxed) {} |
591 | 600 | } |
592 | 601 |
|
593 | 602 | hci_out.reset(); |
|
0 commit comments