@@ -515,7 +515,11 @@ static void bt_spi_rx_thread(void *p1, void *p2, void *p3)
515515 while (true) {
516516
517517 /* Wait for interrupt pin to be active */
518- k_sem_take (& sem_request , K_FOREVER );
518+ ret = k_sem_take (& sem_request , K_FOREVER );
519+ if (ret ) {
520+ LOG_DBG ("Failed to take the semaphore. The RX thread exits." );
521+ break ;
522+ }
519523
520524 LOG_DBG ("" );
521525
@@ -685,12 +689,39 @@ static int bt_spi_open(const struct device *dev, bt_hci_recv_t recv)
685689 return 0 ;
686690}
687691
692+ static int bt_spi_close (const struct device * dev )
693+ {
694+ struct bt_spi_data * hci = dev -> data ;
695+ int ret ;
696+
697+ gpio_pin_interrupt_configure_dt (& irq_gpio , GPIO_INT_DISABLE );
698+
699+ /* To exit the thread safely */
700+ k_sem_reset (& sem_request );
701+ ret = k_thread_join (& spi_rx_thread_data , K_MSEC (100 ));
702+ if (ret ) {
703+ LOG_DBG ("bt_spi_rx_thread is unable to exit" );
704+ return ret ;
705+ }
706+
707+ /* Reset the BLE controller */
708+ gpio_pin_set_dt (& rst_gpio , 1 );
709+ k_sleep (K_MSEC (DT_INST_PROP_OR (0 , reset_assert_duration_ms , 0 )));
710+ gpio_pin_set_dt (& rst_gpio , 0 );
711+
712+ hci -> recv = NULL ;
713+ LOG_DBG ("Bluetooth disabled" );
714+
715+ return 0 ;
716+ }
717+
688718static DEVICE_API (bt_hci , drv ) = {
689719#if defined(CONFIG_BT_BLUENRG_ACI ) && !defined(CONFIG_BT_HCI_RAW )
690720 .setup = bt_spi_bluenrg_setup ,
691721#endif /* CONFIG_BT_BLUENRG_ACI && !CONFIG_BT_HCI_RAW */
692722 .open = bt_spi_open ,
693723 .send = bt_spi_send ,
724+ .close = bt_spi_close ,
694725};
695726
696727static int bt_spi_init (const struct device * dev )
0 commit comments