Skip to content

Commit 18b8657

Browse files
committed
psoc6/modbluetooth: Add scan API's - WIP.
Signed-off-by: NikhitaR-IFX <nikhita.rajasekhar@infineon.com>
1 parent 73082ee commit 18b8657

1 file changed

Lines changed: 166 additions & 1 deletion

File tree

ports/psoc6/modbluetooth.c

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,19 @@ wiced_result_t bt_management_callback(wiced_bt_management_evt_t event,
367367
}
368368
break;
369369

370+
case BTM_BLE_SCAN_STATE_CHANGED_EVT:
371+
372+
if (p_event_data->ble_scan_state_changed == BTM_BLE_SCAN_TYPE_HIGH_DUTY) {
373+
printf("Scan State Change: BTM_BLE_SCAN_TYPE_HIGH_DUTY\n");
374+
} else if (p_event_data->ble_scan_state_changed == BTM_BLE_SCAN_TYPE_LOW_DUTY) {
375+
printf("Scan State Change: BTM_BLE_SCAN_TYPE_LOW_DUTY\n");
376+
} else if (p_event_data->ble_scan_state_changed == BTM_BLE_SCAN_TYPE_NONE) {
377+
printf("Scan stopped\n");
378+
} else {
379+
printf("Invalid scan state\n");
380+
}
381+
break;
382+
370383
default:
371384
break;
372385
}
@@ -590,13 +603,165 @@ void mp_bluetooth_gap_advertise_stop(void) {
590603

591604
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
592605

606+
static void isr_timer(void *callback_arg, cyhal_timer_event_t event) {
607+
// printf("Tim int\r\n");
608+
mp_bluetooth_gap_scan_stop();
609+
}
610+
611+
static cyhal_timer_t mp_bluetooth_gap_scan_timer_obj;
612+
613+
void mp_bluetooth_gap_scan_timer_start(int32_t duration_ms) {
614+
uint32_t period_hal; // Period/count input for the PSoC6 HAL timer configuration
615+
uint32_t fz_hal = 1000000; // Frequency for the PSoC timer clock is fixed as 1 MHz
616+
period_hal = (uint32_t)(duration_ms * fz_hal) - 1; // Overflow Period = (Period + 1)/ frequency ;period = (overflow period * frequency)-1
617+
618+
// Adjust the frequency & recalculate the period if period/count is greater than the maximum overflow value for a 32 bit timer ie; 2^32
619+
while (period_hal > 4294967296) {
620+
fz_hal = fz_hal / 10; // Reduce the fz_hal value by 10%
621+
period_hal = (uint32_t)(duration_ms * fz_hal) - 1; // Recalculate Period input for the PSoC6 HAL timer configuration
622+
}
623+
624+
// Timer initialisation of port
625+
cy_rslt_t rslt;
626+
period_hal = 13000; // For testing purpose only, remove later//9999
627+
628+
const cyhal_timer_cfg_t timer_cfg =
629+
{
630+
.compare_value = 0, /* Timer compare value, not used */
631+
.period = period_hal, /* Defines the timer period */
632+
.direction = CYHAL_TIMER_DIR_UP, /* Timer counts up */
633+
.is_compare = false, /* Don't use compare mode */
634+
.is_continuous = 0, /* Run the timer */
635+
.value = 0 /* Initial value of counter */
636+
};
637+
638+
/* Initialize the timer object. Does not use pin output ('pin' is NC) and
639+
* does not use a pre-configured clock source ('clk' is NULL). */
640+
641+
rslt = cyhal_timer_init(&mp_bluetooth_gap_scan_timer_obj, NC, NULL);
642+
CY_ASSERT(CY_RSLT_SUCCESS == rslt);
643+
644+
/* Apply timer configuration such as period, count direction, run mode, etc. */
645+
rslt = cyhal_timer_configure(&mp_bluetooth_gap_scan_timer_obj, &timer_cfg);
646+
647+
/* Set the frequency of timer to Defined frequency */
648+
rslt = cyhal_timer_set_frequency(&mp_bluetooth_gap_scan_timer_obj, fz_hal);
649+
650+
/* Assign the ISR to execute on timer interrupt */
651+
cyhal_timer_register_callback(&mp_bluetooth_gap_scan_timer_obj, isr_timer, NULL);
652+
653+
/* Set the event on which timer interrupt occurs and enable it */
654+
cyhal_timer_enable_event(&mp_bluetooth_gap_scan_timer_obj, CYHAL_TIMER_IRQ_TERMINAL_COUNT, 3, true);
655+
656+
/* Start the timer with the configured settings */
657+
rslt = cyhal_timer_start(&mp_bluetooth_gap_scan_timer_obj);
658+
659+
CY_ASSERT(CY_RSLT_SUCCESS == rslt);
660+
661+
}
662+
663+
void print_bd_address(wiced_bt_device_address_t bdadr) {
664+
printf("%02X:%02X:%02X:%02X:%02X:%02X \n", bdadr[0], bdadr[1], bdadr[2], bdadr[3], bdadr[4], bdadr[5]);
665+
}
666+
667+
void ctss_scan_result_cback(wiced_bt_ble_scan_results_t *p_scan_result,
668+
uint8_t *p_adv_data) {
669+
printf("Yayy\r\n");
670+
}
671+
672+
// void ctss_scan_result_cback(wiced_bt_ble_scan_results_t *p_scan_result,
673+
// uint8_t *p_adv_data )
674+
// {
675+
// wiced_result_t result = WICED_BT_SUCCESS;
676+
// uint8_t length = 0u;
677+
// uint8_t *adv_name;
678+
// uint8_t client_device_name[15] = {'C','T','S',' ','C','l',
679+
// 'i','e','n','t','\0'};
680+
//
681+
// if (p_scan_result)
682+
// {
683+
// mp_bluetooth_gap_on_scan_result(p_scan_result->ble_addr_type, p_scan_result->remote_bd_addr, p_scan_result->flag, p_scan_result->rssi, p_adv_data, sizeof(p_adv_data));
684+
// adv_name = wiced_bt_ble_check_advertising_data(p_adv_data,
685+
// BTM_BLE_ADVERT_TYPE_NAME_COMPLETE,
686+
// &length);
687+
// if(NULL == adv_name)
688+
// {
689+
// return;
690+
// }
691+
// /* Check if the peer device's name is "BLE CTS Client" */
692+
// if(0 == memcmp(adv_name, client_device_name, strlen((const char *)client_device_name)))
693+
// {
694+
// printf("\nFound the peer device! BD Addr: ");
695+
// print_bd_address(p_scan_result->remote_bd_addr);
696+
//
697+
// /* Device found. Stop scan. */
698+
// if((result = wiced_bt_ble_scan(BTM_BLE_SCAN_TYPE_NONE, WICED_TRUE,
699+
// ctss_scan_result_cback))!= WICED_BT_SUCCESS)
700+
// {
701+
// printf("\r\nscan off status %d\n", result);
702+
// }
703+
// else
704+
// {
705+
// printf("Scan completed\n\n");
706+
// }
707+
//
708+
// printf("Initiating connection\n");
709+
// /* Initiate the connection */
710+
// if(wiced_bt_gatt_le_connect(p_scan_result->remote_bd_addr,
711+
// p_scan_result->ble_addr_type,
712+
// BLE_CONN_MODE_HIGH_DUTY,
713+
// WICED_TRUE)!= WICED_TRUE)
714+
// {
715+
// printf("\rwiced_bt_gatt_connect failed\n");
716+
// }
717+
// }
718+
// else
719+
// {
720+
// printf("BD Addr: ");
721+
// print_bd_address(p_scan_result->remote_bd_addr);
722+
// return; //Skip - This is not the device we are looking for.
723+
// }
724+
// }
725+
// }
726+
593727
int mp_bluetooth_gap_scan_start(int32_t duration_ms, int32_t interval_us, int32_t window_us, bool active_scan) {
728+
// Stop any ongoing GAP scan.
729+
wiced_result_t result;
730+
/*int ret = mp_bluetooth_gap_scan_stop();
731+
if (ret) {
732+
return ret;
733+
}*/
734+
mp_bluetooth_gap_scan_timer_start(duration_ms);
735+
736+
for (;;) {
737+
result = wiced_bt_ble_scan(BTM_BLE_SCAN_TYPE_HIGH_DUTY, WICED_TRUE, ctss_scan_result_cback);
738+
if ((WICED_BT_PENDING == result) || (WICED_BT_BUSY == result)) {
739+
printf("Scanning...\r\n");
740+
// vTaskDelay(10);
741+
// cyhal_system_delay_ms(1);
742+
} else {
743+
printf("\rError: Starting scan failed. Error code: %d\n", result);
744+
return -1;
745+
}
746+
}
747+
594748
return 0;
595749
}
596750

597751
// Stop discovery
598752
int mp_bluetooth_gap_scan_stop(void) {
599-
return 0;
753+
printf("Scan stopping\r\n");
754+
wiced_result_t result;
755+
result = wiced_bt_ble_scan(BTM_BLE_SCAN_TYPE_NONE, WICED_TRUE, ctss_scan_result_cback);
756+
if (result != WICED_BT_SUCCESS) {
757+
mp_printf(&mp_plat_print, "Stopping scan failed with error: %d\n", result);
758+
return -1;
759+
}
760+
cyhal_timer_stop(&mp_bluetooth_gap_scan_timer_obj);
761+
cyhal_timer_free(&mp_bluetooth_gap_scan_timer_obj);
762+
763+
mp_bluetooth_gap_on_scan_complete();
764+
return result;
600765
}
601766

602767
// Connect to a found peripheral.

0 commit comments

Comments
 (0)