Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions extmod/zephyr_ble/gatt_pragma.h

This file was deleted.

12 changes: 0 additions & 12 deletions extmod/zephyr_ble/hal/zephyr_ble_fifo.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ static inline sys_snode_t *item_to_snode(void *item) {

// --- k_queue API Implementation (underlying FIFO implementation) ---

// Enable debug output (called after boot to avoid interfering with initialization)
void mp_bluetooth_zephyr_fifo_enable_debug(void) {
debug_enabled = 1;
mp_printf(&mp_plat_print, "[FIFO] Debug output enabled\n");
}

void k_queue_init(struct k_queue *queue) {
DEBUG_FIFO("k_queue_init(%p)", queue);
queue->data_q.head = NULL;
queue->data_q.tail = NULL;
queue->lock.unused = 0; // Initialize spinlock for completeness
}

void k_queue_append(struct k_queue *queue, void *data) {
sys_snode_t *node = item_to_snode(data);
Expand Down
35 changes: 0 additions & 35 deletions extmod/zephyr_ble/hal/zephyr_ble_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,9 @@ static inline void k_yield(void) {
// No-op in MicroPython scheduler
}

// Busy wait for short delays (microseconds)
static inline void k_busy_wait(uint32_t usec_to_wait) {
mp_hal_delay_us(usec_to_wait);
}

// --- Uptime and Timing ---

// Get system uptime in milliseconds
static inline int64_t k_uptime_get(void) {
return (int64_t)mp_hal_ticks_ms();
}

// Get system uptime in 32-bit milliseconds (wraps at ~49 days)
static inline uint32_t k_uptime_get_32(void) {
return mp_hal_ticks_ms();
}

// Convert uptime to ticks (1:1 mapping in MicroPython, both are milliseconds)
static inline int64_t k_uptime_ticks(void) {
return (int64_t)mp_hal_ticks_ms();
}

// Get current cycle count (if available, otherwise return uptime in ms)
static inline uint32_t k_cycle_get_32(void) {
#ifdef mp_hal_ticks_cpu
return mp_hal_ticks_cpu();
#else
return mp_hal_ticks_ms();
#endif
}

// --- Thread Info (No-op in MicroPython) ---

Expand Down Expand Up @@ -105,10 +78,6 @@ static inline bool k_is_in_isr(void) {
return false;
}

// Check if pre-emptible (always returns false, no preemption in scheduler)
static inline bool k_is_preempt_thread(void) {
return false;
}

// --- System State ---

Expand All @@ -117,10 +86,6 @@ static inline bool k_is_preempt_thread(void) {
#define KERNEL_VERSION_MINOR 7
#define KERNEL_VERSION_PATCHLEVEL 0

// System state (always running in MicroPython)
static inline bool k_is_pre_kernel(void) {
return false;
}

// --- Fatal Error Handlers ---

Expand Down
5 changes: 0 additions & 5 deletions extmod/zephyr_ble/hal/zephyr_ble_mem_slab.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ static volatile int debug_enabled = 0;
} \
} while (0)

// Enable debug output
void mp_bluetooth_zephyr_mem_slab_enable_debug(void) {
debug_enabled = 1;
mp_printf(&mp_plat_print, "[SLAB] Debug output enabled\n");
}

// Initialize a memory slab (called by K_MEM_SLAB_DEFINE_STATIC macro)
void k_mem_slab_init(struct k_mem_slab *slab, void *buffer, size_t block_size, uint32_t num_blocks) {
Expand Down
9 changes: 0 additions & 9 deletions extmod/zephyr_ble/hal/zephyr_ble_poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,6 @@ void mp_bluetooth_zephyr_hci_uart_wfi(void) {

// --- Polling Functions ---

void mp_bluetooth_zephyr_poll_init(void) {
// Currently nothing to initialize
// Timers and work queues are initialized on first use
}

void mp_bluetooth_zephyr_poll_deinit(void) {
// Currently nothing to deinitialize
// In the future, this might clean up global state
}

void mp_bluetooth_zephyr_poll(void) {
// Process HCI UART first (if implemented)
Expand Down
111 changes: 0 additions & 111 deletions extmod/zephyr_ble/hal/zephyr_ble_psa_crypto.c

This file was deleted.

4 changes: 0 additions & 4 deletions extmod/zephyr_ble/hal/zephyr_ble_sem.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,3 @@ unsigned int k_sem_count_get(struct k_sem *sem) {
return sem->count;
}

void k_sem_reset(struct k_sem *sem) {
DEBUG_SEM_printf("k_sem_reset(%p)\n", sem);
sem->count = 0;
}
124 changes: 1 addition & 123 deletions extmod/zephyr_ble/hal/zephyr_ble_work.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@

#include <stddef.h>

// Debug counter for tracking execution (avoids printf blocking issues)
volatile int ble_work_debug_step = 0;

// Flag to indicate we're in the bt_enable() init phase
// During this phase, mp_bluetooth_zephyr_init_work_get() will get work from k_sys_work_q
Expand Down Expand Up @@ -222,11 +220,6 @@ int k_work_cancel(struct k_work *work) {
return 1;
}

int k_work_cancel_sync(struct k_work *work, void *sync) {
// sync parameter is for synchronization with other threads, not needed in MicroPython
(void)sync;
return k_work_cancel(work);
}

bool k_work_is_pending(const struct k_work *work) {
return work->pending;
Expand Down Expand Up @@ -286,19 +279,13 @@ int k_work_schedule(struct k_work_delayable *dwork, k_timeout_t delay) {
return k_work_schedule_for_queue(&k_sys_work_q, dwork, delay);
}

int k_work_reschedule_for_queue(struct k_work_q *queue, struct k_work_delayable *dwork, k_timeout_t delay) {
DEBUG_WORK_printf("k_work_reschedule_for_queue(%p, %p, %u)\n", queue, dwork, delay.ticks);

// Reschedule is the same as schedule in our implementation
return k_work_schedule_for_queue(queue, dwork, delay);
}

int k_work_reschedule(struct k_work_delayable *dwork, k_timeout_t delay) {
if (!k_sys_work_q.head && !k_sys_work_q.nextq) {
k_work_queue_init(&k_sys_work_q);
k_sys_work_q.name = "SYS WQ";
}
return k_work_reschedule_for_queue(&k_sys_work_q, dwork, delay);
return k_work_schedule_for_queue(&k_sys_work_q, dwork, delay);
}

int k_work_cancel_delayable(struct k_work_delayable *dwork) {
Expand Down Expand Up @@ -441,48 +428,6 @@ void mp_bluetooth_zephyr_work_process(void) {
work_process_depth--;
}

// Called by mp_bluetooth_init() wait loop to process initialization work synchronously
void mp_bluetooth_zephyr_work_process_init(void) {
// Prevent recursion (though unlikely since init work should be synchronous)
if (init_work_processor_running) {
DEBUG_WORK_printf("init_work_process: already running, skipping\n");
return;
}

init_work_processor_running = true;

// Process ONLY the initialization work queue
struct k_work_q *q = &k_init_work_q;

while (q->head != NULL) {
// Dequeue work item
struct k_work *work = q->head;
q->head = work->next;

if (q->head) {
q->head->prev = NULL;
}

work->next = NULL;
work->prev = NULL;
work->pending = false;

// Execute work handler
DEBUG_WORK_printf("init_work_execute(%p, handler=%p)\n", work, work->handler);
if (work->handler) {
work->handler(work);
}
DEBUG_WORK_printf("init_work_execute(%p) done\n", work);

// Check if work was re-enqueued during execution
if (work->pending) {
DEBUG_WORK_printf(" --> init work re-enqueued, stopping queue processing\n");
break;
}
}

init_work_processor_running = false;
}

// --- Init Work Helper Functions ---

Expand Down Expand Up @@ -543,74 +488,7 @@ struct k_work *mp_bluetooth_zephyr_init_work_get(void) {
return work;
}

// Debug function to report work processing statistics
void mp_bluetooth_zephyr_work_debug_stats(void) {
mp_printf(&mp_plat_print, "WORK STATS: process() called %d times, %d items processed\n",
work_process_call_count, work_items_processed);
}

// Process all pending work in all queues (except during init phase).
// Used by the FreeRTOS work queue thread (added in zephyr_ble_freertos branch).
#if MICROPY_BLUETOOTH_ZEPHYR_USE_FREERTOS
static void ble_work_process_all(void) {
ble_work_debug_step = 10; // Entered ble_work_process_all

// During init phase, main loop handles all work processing
// to ensure proper sequencing of bt_enable() initialization
if (mp_bluetooth_zephyr_in_init_phase()) {
ble_work_debug_step = 11; // Skip due to init phase
return;
}
ble_work_debug_step = 12; // Processing work

for (struct k_work_q *q = global_work_q; q != NULL; q = q->nextq) {
// Skip initialization work queue (processed separately)
if (q == &k_init_work_q) {
continue;
}

// Process all work items in this queue
// Note: Must check q->head inside critical section to avoid race
for (;;) {
// Dequeue work item atomically
MICROPY_PY_BLUETOOTH_ENTER
struct k_work *work = q->head;
if (work == NULL) {
MICROPY_PY_BLUETOOTH_EXIT
break; // Queue empty
}
q->head = work->next;
if (q->head) {
q->head->prev = NULL;
}
work->next = NULL;
work->prev = NULL;
work->pending = false;
MICROPY_PY_BLUETOOTH_EXIT

// Execute work handler outside critical section
work_items_processed++;
ble_work_debug_step = 20; // About to execute work handler

if (work->handler) {
// Set context flag if processing system work queue
bool is_sys_wq = (q == &k_sys_work_q);
if (is_sys_wq) {
in_sys_work_q_context = true;
}
ble_work_debug_step = 21; // Calling handler
work->handler(work);
ble_work_debug_step = 22; // Handler returned
if (is_sys_wq) {
in_sys_work_q_context = false;
}
}

ble_work_debug_step = 23; // Work done
}
}
}
#endif // MICROPY_BLUETOOTH_ZEPHYR_USE_FREERTOS

// Stub implementations — work processed via cooperative polling, no dedicated work thread.
void mp_bluetooth_zephyr_work_thread_start(void) {
Expand Down
Loading
Loading