1 /*
2  * Copyright (c) 2023 STMicroelectronics
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/init.h>
9 
10 #include "app_conf.h"
11 #include "blestack.h"
12 #include "bpka.h"
13 #include "ll_intf.h"
14 
15 K_MUTEX_DEFINE(ble_ctlr_stack_mutex);
16 struct k_work_q ble_ctlr_work_q, ll_work_q;
17 struct k_work ble_ctlr_stack_work, bpka_work;
18 
19 uint8_t ll_state_busy;
20 
21 #define BLE_CTLR_TASK_STACK_SIZE (256 * 7)
22 #define LL_TASK_STACK_SIZE (256 * 7)
23 #define BLE_CTLR_TASK_PRIO	 (14)
24 #define LL_TASK_PRIO (14)
25 
26 K_THREAD_STACK_DEFINE(ble_ctlr_work_area, BLE_CTLR_TASK_STACK_SIZE);
27 K_THREAD_STACK_DEFINE(ll_work_area, LL_TASK_STACK_SIZE);
28 
HostStack_Process(void)29 void HostStack_Process(void)
30 {
31 	k_work_submit_to_queue(&ble_ctlr_work_q, &ble_ctlr_stack_work);
32 }
33 
ble_ctlr_stack_handler(struct k_work * work)34 static void ble_ctlr_stack_handler(struct k_work *work)
35 {
36 	uint8_t running = 0x0;
37 	change_state_options_t options;
38 
39 	k_mutex_lock(&ble_ctlr_stack_mutex, K_FOREVER);
40 	running = BleStack_Process();
41 	k_mutex_unlock(&ble_ctlr_stack_mutex);
42 
43 	if (ll_state_busy == 1) {
44 		options.combined_value = 0x0F;
45 		ll_intf_chng_evnt_hndlr_state(options);
46 		ll_state_busy = 0;
47 	}
48 
49 	if (running == BLE_SLEEPMODE_RUNNING) {
50 		HostStack_Process();
51 	}
52 }
53 
BPKACB_Process(void)54 void BPKACB_Process(void)
55 {
56 	k_work_submit_to_queue(&ble_ctlr_work_q, &bpka_work);
57 }
58 
bpka_work_handler(struct k_work * work)59 static void bpka_work_handler(struct k_work *work)
60 {
61 	BPKA_BG_Process();
62 }
63 
stm32wba_ble_ctlr_init(void)64 static int stm32wba_ble_ctlr_init(void)
65 {
66 	k_work_queue_init(&ble_ctlr_work_q);
67 	k_work_queue_start(&ble_ctlr_work_q, ble_ctlr_work_area,
68 				K_THREAD_STACK_SIZEOF(ble_ctlr_work_area),
69 				BLE_CTLR_TASK_PRIO, NULL);
70 
71 	k_work_queue_init(&ll_work_q);
72 	k_work_queue_start(&ll_work_q, ll_work_area,
73 				K_THREAD_STACK_SIZEOF(ll_work_area),
74 				LL_TASK_PRIO, NULL);
75 
76 	k_work_init(&ble_ctlr_stack_work, &ble_ctlr_stack_handler);
77 	k_work_init(&bpka_work, &bpka_work_handler);
78 
79 	return 0;
80 }
81 
82 SYS_INIT(stm32wba_ble_ctlr_init, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
83