55 * SPDX-License-Identifier: Apache-2.0
66 */
77
8- /**
9- * @brief Zephyr CYW20829 driver.
10- *
11- * This driver uses btstack-integration asset as hosts platform adaptation layer
12- * (porting layer) for CYW20829. btstack-integration layer implements/
13- * invokes the interfaces defined by BTSTACK to enable communication
14- * with the BT controller by using IPC_BTSS (IPC Bluetooth sub-system interface).
15- * Zephyr CYW20829 driver implements wiced_bt_**** functions requreds for
16- * btstack-integration asset and Zephyr Bluetooth driver interface
17- * (defined in struct bt_hci_driver).
18- *
19- * CM33 (application core)
20- * |=========================================|
21- * | |-------------------------| |
22- * | | Zephyr application | |
23- * | |-------------------------| |
24- * | | |
25- * | |------------| |
26- * | | Zephyr | |
27- * | | Bluetooth | |
28- * CM33 (BTSS core) | | Host | |
29- * |=====================| | |------------| |
30- * | | | | |
31- * | |---------------| | | |--------------| | -----------| |
32- * | | Bluetooth | | IPC_BTSS | | btstack- | | Zephyr | |
33- * | | Controller FW | | <--------|-> | integration | ---- | CYW20829 | |
34- * | |---------------| | | | asset | | driver | |
35- * | | | |--------------| |------------| |
36- * |=====================| | |
37- * | |=========================================|
38- * |====================|
39- * | CYW20829 |
40- * | Bluetooth |
41- * |====================|
42- *
43- * NOTE:
44- * cyw920829 requires fetch binary files of Bluetooth controller firmware.
45- * To fetch Binary Blobs: west blobs fetch hal_infineon
46- *
47- */
8+ /**
9+ * @brief Zephyr CYW20829 driver.
10+ *
11+ * This driver uses btstack-integration asset as hosts platform adaptation layer
12+ * (porting layer) for CYW20829. btstack-integration layer implements/
13+ * invokes the interfaces defined by BTSTACK to enable communication
14+ * with the BT controller by using IPC_BTSS (IPC Bluetooth sub-system interface).
15+ * Zephyr CYW20829 driver implements wiced_bt_**** functions requreds for
16+ * btstack-integration asset and Zephyr Bluetooth driver interface
17+ * (defined in struct bt_hci_driver).
18+ *
19+ * CM33 (application core)
20+ * |=========================================|
21+ * | |-------------------------| |
22+ * | | Zephyr application | |
23+ * | |-------------------------| |
24+ * | | |
25+ * | |------------| |
26+ * | | Zephyr | |
27+ * | | Bluetooth | |
28+ * CM33 (BTSS core) | | Host | |
29+ * |=====================| | |------------| |
30+ * | | | | |
31+ * | |---------------| | | |--------------| | -----------| |
32+ * | | Bluetooth | | IPC_BTSS | | btstack- | | Zephyr | |
33+ * | | Controller FW | | <--------|-> | integration | ---- | CYW20829 | |
34+ * | |---------------| | | | asset | | driver | |
35+ * | | | |--------------| |------------| |
36+ * |=====================| | |
37+ * | |=========================================|
38+ * |====================|
39+ * | CYW20829 |
40+ * | Bluetooth |
41+ * |====================|
42+ *
43+ * NOTE:
44+ * cyw920829 requires fetch binary files of Bluetooth controller firmware.
45+ * To fetch Binary Blobs: west blobs fetch hal_infineon
46+ *
47+ */
4848
4949#include <errno.h>
5050#include <stddef.h>
6969#include <cyabs_rtos.h>
7070#include <cybt_result.h>
7171
72- #define LOG_LEVEL CONFIG_BT_HCI_DRIVER_LOG_LEVEL
72+ #include "cyhal_syspm.h"
73+
74+ #define LOG_LEVEL CONFIG_BT_HCI_DRIVER_LOG_LEVEL
7375#include <zephyr/logging/log.h>
7476LOG_MODULE_REGISTER (cyw208xx );
7577
@@ -91,25 +93,34 @@ enum {
9193extern const uint8_t brcm_patchram_buf [];
9294extern const int brcm_patch_ram_length ;
9395
94- #define CYBSP_BT_PLATFORM_CFG_SLEEP_MODE_LP_ENABLED (0 )
95- #define BTM_SET_LOCAL_DEV_ADDR_LENGTH 6
96+ #define CYBSP_BT_PLATFORM_CFG_SLEEP_MODE_LP_ENABLED (1 )
97+ #define BTM_SET_LOCAL_DEV_ADDR_LENGTH 6
9698
9799static K_SEM_DEFINE (hci_sem , 1 , 1 ) ;
98100static K_SEM_DEFINE (cybt_platform_task_init_sem , 0 , 1 ) ;
99101
102+ cy_en_syspm_status_t cyw208xx_syspm_callback (cy_stc_syspm_callback_params_t * callbackParams ,
103+ cy_en_syspm_callback_mode_t mode );
104+
105+ static cy_stc_syspm_callback_params_t cyw208xx_syspm_callback_param = {NULL , NULL };
106+ static cy_stc_syspm_callback_t cyw208xx_syspm_callback_cfg = {
107+ .callback = & cyw208xx_syspm_callback ,
108+ .type = (cy_en_syspm_callback_type_t )CY_SYSPM_DEEPSLEEP | CY_SYSPM_SLEEP ,
109+ .callbackParams = & cyw208xx_syspm_callback_param ,
110+ .order = 253u ,
111+ };
100112
101- /******************************************************************************
102- * Function Declarations
103- ******************************************************************************/
113+ /* Extern btstack integration functions */
104114extern void host_stack_platform_interface_init (void );
105115extern void cybt_platform_hci_wait_for_boot_fully_up (bool is_from_isr );
106116extern uint8_t * host_stack_get_acl_to_lower_buffer (wiced_bt_transport_t transport , uint32_t size );
107- extern wiced_result_t host_stack_send_acl_to_lower (wiced_bt_transport_t transport ,
108- uint8_t * data , uint16_t len );
117+ extern wiced_result_t host_stack_send_acl_to_lower (wiced_bt_transport_t transport , uint8_t * data ,
118+ uint16_t len );
109119extern wiced_result_t host_stack_send_cmd_to_lower (uint8_t * cmd , uint16_t cmd_len );
110120extern wiced_result_t host_stack_send_iso_to_lower (uint8_t * data , uint16_t len );
111121extern cybt_result_t cybt_platform_msg_to_bt_task (const uint16_t msg , bool is_from_isr );
112122extern void cybt_bttask_deinit (void );
123+ uint8_t task_queue_utilization (void );
113124
114125static int cyw208xx_bt_firmware_download (const uint8_t * firmware_image , uint32_t size )
115126{
@@ -129,11 +140,16 @@ static int cyw208xx_bt_firmware_download(const uint8_t *firmware_image, uint32_t
129140 size_t data_length = data [2 ]; /* data length from firmware image block */
130141 uint16_t op_code = * (uint16_t * )data ;
131142
143+ if (op_code == BT_HCI_VND_OP_LAUNCH_RAM ) {
144+ /* set hf0 to 48MHz */
145+ Cy_SysClk_ClkHfSetSource (0U , CY_SYSCLK_CLKHF_IN_CLKPATH1 );
146+ }
147+
132148 /* Allocate buffer for hci_write_ram/hci_launch_ram command. */
133149 buf = bt_hci_cmd_create (op_code , data_length );
134150 if (buf == NULL ) {
135151 LOG_ERR ("Unable to allocate command buffer" );
136- return err ;
152+ return - ENOBUFS ;
137153 }
138154
139155 /* Add data part of packet */
@@ -174,15 +190,20 @@ static int cyw208xx_setup(const struct device *dev, const struct bt_hci_setup_pa
174190 int err ;
175191 struct net_buf * buf ;
176192
193+ /* Avoid sleep while downloading firmware */
194+ cyhal_syspm_lock_deepsleep ();
195+
177196 /* Send HCI_RESET */
178197 err = bt_hci_cmd_send_sync (BT_HCI_OP_RESET , NULL , NULL );
179198 if (err ) {
199+ cyhal_syspm_unlock_deepsleep ();
180200 return err ;
181201 }
182202
183203 /* BT firmware download */
184204 err = cyw208xx_bt_firmware_download (brcm_patchram_buf , (uint32_t )brcm_patch_ram_length );
185205 if (err ) {
206+ cyhal_syspm_unlock_deepsleep ();
186207 return err ;
187208 }
188209
@@ -193,6 +214,7 @@ static int cyw208xx_setup(const struct device *dev, const struct bt_hci_setup_pa
193214 buf = bt_hci_cmd_create (BT_HCI_VND_OP_SET_LOCAL_DEV_ADDR , BTM_SET_LOCAL_DEV_ADDR_LENGTH );
194215 if (buf == NULL ) {
195216 LOG_ERR ("Unable to allocate command buffer" );
217+ cyhal_syspm_unlock_deepsleep ();
196218 return - ENOMEM ;
197219 }
198220
@@ -215,9 +237,12 @@ static int cyw208xx_setup(const struct device *dev, const struct bt_hci_setup_pa
215237 err = bt_hci_cmd_send_sync (BT_HCI_VND_OP_SET_LOCAL_DEV_ADDR , buf , NULL );
216238 if (err ) {
217239 LOG_ERR ("Failed to set public address (%d)" , err );
240+ cyhal_syspm_unlock_deepsleep ();
218241 return err ;
219242 }
220243
244+ cyhal_syspm_unlock_deepsleep ();
245+
221246 return 0 ;
222247}
223248
@@ -317,25 +342,30 @@ static int cyw208xx_hci_init(const struct device *dev)
317342 const cybt_platform_config_t cybsp_bt_platform_cfg = {
318343 .hci_config = {
319344 .hci_transport = CYBT_HCI_IPC ,
320- },
345+ },
321346
322347 .controller_config = {
323348 .sleep_mode = {
324- .sleep_mode_enabled = CYBSP_BT_PLATFORM_CFG_SLEEP_MODE_LP_ENABLED ,
325- } ,
326- }
327- };
349+ .sleep_mode_enabled =
350+ CYBSP_BT_PLATFORM_CFG_SLEEP_MODE_LP_ENABLED ,
351+ },
352+ } };
328353
329354 /* Configure platform specific settings for the BT device */
330355 cybt_platform_config_init (& cybsp_bt_platform_cfg );
331356
357+ if (!Cy_SysPm_RegisterCallback (& cyw208xx_syspm_callback_cfg )) {
358+ LOG_ERR ("Syspm Callback registering failed!" );
359+ CY_ASSERT (0 );
360+ }
332361 return 0 ;
333362}
334363
335364/* Implements wiced_bt_**** functions requreds for the btstack-integration asset */
336365
337- wiced_result_t wiced_bt_dev_vendor_specific_command (uint16_t opcode , uint8_t param_len ,
338- uint8_t * param_buf , wiced_bt_dev_vendor_specific_command_complete_cback_t cback )
366+ wiced_result_t
367+ wiced_bt_dev_vendor_specific_command (uint16_t opcode , uint8_t param_len , uint8_t * param_buf ,
368+ wiced_bt_dev_vendor_specific_command_complete_cback_t cback )
339369
340370{
341371 /*
@@ -396,7 +426,6 @@ void wiced_bt_process_hci(hci_packet_type_t pti, uint8_t *data, uint32_t length)
396426
397427 default :
398428 return ;
399-
400429 }
401430
402431 buf_tailroom = net_buf_tailroom (buf );
@@ -448,10 +477,37 @@ void wiced_bt_process_timer(void)
448477 /* NA for Zephyr */
449478}
450479
451- #define CYW208XX_DEVICE_INIT (inst ) \
452- static struct cyw208xx_data cyw208xx_data_##inst = { \
453- }; \
454- DEVICE_DT_INST_DEFINE(inst, cyw208xx_hci_init, NULL, &cyw208xx_data_##inst, NULL, \
480+ cy_en_syspm_status_t cyw208xx_syspm_callback (cy_stc_syspm_callback_params_t * callbackParams ,
481+ cy_en_syspm_callback_mode_t mode )
482+ {
483+ cy_en_syspm_status_t retVal = CY_SYSPM_FAIL ;
484+
485+ CY_UNUSED_PARAMETER (callbackParams );
486+
487+ switch (mode ) {
488+ case CY_SYSPM_CHECK_READY :
489+ case CY_SYSPM_BEFORE_TRANSITION : {
490+ retVal = (task_queue_utilization () == 0 ) ? CY_SYSPM_SUCCESS : CY_SYSPM_FAIL ;
491+ break ;
492+ }
493+
494+ case CY_SYSPM_CHECK_FAIL :
495+ case CY_SYSPM_AFTER_TRANSITION : {
496+
497+ retVal = CY_SYSPM_SUCCESS ;
498+ break ;
499+ }
500+
501+ default :
502+ break ;
503+ }
504+
505+ return retVal ;
506+ }
507+
508+ #define CYW208XX_DEVICE_INIT (inst ) \
509+ static struct cyw208xx_data cyw208xx_data_##inst = {}; \
510+ DEVICE_DT_INST_DEFINE(inst, cyw208xx_hci_init, NULL, &cyw208xx_data_##inst, NULL, \
455511 POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &drv)
456512
457513/* Only one instance supported */
0 commit comments