Skip to content
Merged
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
45 changes: 45 additions & 0 deletions lib/pbio/drv/bluetooth/bluetooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,52 @@ pbio_error_t pbdrv_bluetooth_process_thread(pbio_os_state_t *state, void *contex
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
}

pbio_error_t pbdrv_bluetooth_close_user_tasks(pbio_os_state_t *state, pbio_os_timer_t *timer) {

static pbio_os_state_t sub;

if (pbio_os_timer_is_expired(timer)) {
return PBIO_ERROR_TIMEDOUT;
}

PBIO_OS_ASYNC_BEGIN(state);

pbdrv_bluetooth_cancel_operation_request();

// Let ongoing user tasks finish first.
PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_peripheral_command(&sub, NULL));
PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_advertise_or_scan_command(&sub, NULL));

// Disconnect peripheral.
pbdrv_bluetooth_peripheral_disconnect();
PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_peripheral_command(&sub, NULL));

// Stop scanning.
pbdrv_bluetooth_start_observing(NULL);
PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_advertise_or_scan_command(&sub, NULL));

// Stop advertising.
pbdrv_bluetooth_start_broadcasting(NULL, 0);
PBIO_OS_AWAIT(state, &sub, pbdrv_bluetooth_await_advertise_or_scan_command(&sub, NULL));

PBIO_OS_ASYNC_END(PBIO_SUCCESS);
}

void pbdrv_bluetooth_deinit(void) {

// Under normal operation ::pbdrv_bluetooth_close_user_tasks completes
// normally and there should be no user activity at this point. If there
// is, a task got stuck, so exit forcefully.
pbio_os_state_t unused;
if (pbdrv_bluetooth_await_advertise_or_scan_command(&unused, NULL) != PBIO_SUCCESS ||
pbdrv_bluetooth_await_peripheral_command(&unused, NULL) != PBIO_SUCCESS) {

// Hard reset without waitng on completion of any process.
pbdrv_bluetooth_controller_reset_hard();
return;
}

// Gracefully disconnect from host and power down.
pbio_busy_count_up();
pbdrv_bluetooth_cancel_operation_request();
shutting_down = true;
Expand Down
1 change: 1 addition & 0 deletions lib/pbio/drv/bluetooth/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define PBDRV_BLUETOOTH_MAX_ADV_SIZE 31

void pbdrv_bluetooth_init_hci(void);
void pbdrv_bluetooth_controller_reset_hard(void);
pbio_error_t pbdrv_bluetooth_controller_reset(pbio_os_state_t *state, pbio_os_timer_t *timer);
pbio_error_t pbdrv_bluetooth_controller_initialize(pbio_os_state_t *state, pbio_os_timer_t *timer);

Expand Down
6 changes: 5 additions & 1 deletion lib/pbio/drv/bluetooth/bluetooth_btstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,10 @@ const char *pbdrv_bluetooth_get_fw_version(void) {
return "v1.4";
}

void pbdrv_bluetooth_controller_reset_hard(void) {
hci_power_control(HCI_POWER_OFF);
}

pbio_error_t pbdrv_bluetooth_controller_reset(pbio_os_state_t *state, pbio_os_timer_t *timer) {

// The event handler also pushes the bluetooth process along, but shouldn't
Expand All @@ -864,7 +868,7 @@ pbio_error_t pbdrv_bluetooth_controller_reset(pbio_os_state_t *state, pbio_os_ti
PBIO_OS_AWAIT_UNTIL(state, le_con_handle == HCI_CON_HANDLE_INVALID);
}

hci_power_control(HCI_POWER_OFF);
pbdrv_bluetooth_controller_reset_hard();
PBIO_OS_AWAIT_UNTIL(state, hci_get_state() == HCI_STATE_OFF);

PBIO_OS_ASYNC_END(PBIO_SUCCESS);
Expand Down
12 changes: 8 additions & 4 deletions lib/pbio/drv/bluetooth/bluetooth_stm32_bluenrg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,13 @@ static pbio_error_t init_uart_service(pbio_os_state_t *state, void *context) {
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
}

void pbdrv_bluetooth_controller_reset_hard(void) {
pybricks_notify_en = uart_tx_notify_en = false;
conn_handle = peripheral_singleton.con_handle = 0;
spi_disable_cs();
bluetooth_reset(true);
}

pbio_error_t pbdrv_bluetooth_controller_reset(pbio_os_state_t *state, pbio_os_timer_t *timer) {
PBIO_OS_ASYNC_BEGIN(state);

Expand All @@ -1249,11 +1256,8 @@ pbio_error_t pbdrv_bluetooth_controller_reset(pbio_os_state_t *state, pbio_os_ti
PBIO_OS_AWAIT_UNTIL(state, conn_handle == 0);
}

pybricks_notify_en = uart_tx_notify_en = false;
conn_handle = peripheral_singleton.con_handle = 0;
pbdrv_bluetooth_controller_reset_hard();

spi_disable_cs();
bluetooth_reset(true);
PBIO_OS_AWAIT_MS(state, timer, 50);

PBIO_OS_ASYNC_END(PBIO_SUCCESS);
Expand Down
13 changes: 9 additions & 4 deletions lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c
Original file line number Diff line number Diff line change
Expand Up @@ -1837,6 +1837,14 @@ static pbio_error_t init_uart_service(pbio_os_state_t *state, void *context) {
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
}

void pbdrv_bluetooth_controller_reset_hard(void) {
pybricks_notify_en = uart_tx_notify_en = false;
conn_handle = peripheral_singleton.con_handle = NO_CONNECTION;

spi_set_mrdy(false);
bluetooth_reset(RESET_STATE_OUT_LOW);
}

pbio_error_t pbdrv_bluetooth_controller_reset(pbio_os_state_t *state, pbio_os_timer_t *timer) {
PBIO_OS_ASYNC_BEGIN(state);

Expand All @@ -1847,11 +1855,8 @@ pbio_error_t pbdrv_bluetooth_controller_reset(pbio_os_state_t *state, pbio_os_ti
PBIO_OS_AWAIT_UNTIL(state, conn_handle == NO_CONNECTION);
}

pybricks_notify_en = uart_tx_notify_en = false;
conn_handle = peripheral_singleton.con_handle = NO_CONNECTION;
pbdrv_bluetooth_controller_reset_hard();

spi_set_mrdy(false);
bluetooth_reset(RESET_STATE_OUT_LOW);
PBIO_OS_AWAIT_MS(state, timer, 150);

PBIO_OS_ASYNC_END(PBIO_SUCCESS);
Expand Down
18 changes: 18 additions & 0 deletions lib/pbio/include/pbdrv/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,19 @@ void pbdrv_bluetooth_restart_observing_request(void);
*/
pbio_error_t pbdrv_bluetooth_await_advertise_or_scan_command(pbio_os_state_t *state, void *context);

/**
* Awaits user activity to complete, usually called during cleanup after running
* a user program. This will disconnect from the peripheral and stop scanning
* and advertising.
*
* @param [in] state Protothread state.
* @param [in] timer Timer used to give up if this takes too long.
* @return ::PBIO_SUCCESS on completion.
* ::PBIO_ERROR_AGAIN while awaiting.
* ::PBIO_ERROR_TIMEDOUT if the timer expired.
*/
pbio_error_t pbdrv_bluetooth_close_user_tasks(pbio_os_state_t *state, pbio_os_timer_t *timer);

#else // PBDRV_CONFIG_BLUETOOTH

static inline void pbdrv_bluetooth_init(void) {
Expand Down Expand Up @@ -602,6 +615,11 @@ static inline pbio_error_t pbdrv_bluetooth_await_advertise_or_scan_command(pbio_
return PBIO_ERROR_NOT_SUPPORTED;
}

static inline pbio_error_t pbdrv_bluetooth_close_user_tasks(pbio_os_state_t *state, pbio_os_timer_t *timer) {
return PBIO_ERROR_NOT_SUPPORTED;
}


#endif // PBDRV_CONFIG_BLUETOOTH

#endif // _PBDRV_BLUETOOTH_H_
Expand Down
4 changes: 3 additions & 1 deletion lib/pbio/include/pbio/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

#include "pbio/config.h"

#include <pbio/error.h>

void pbio_init(bool start_processes);
void pbio_deinit(void);
void pbio_main_stop_application_resources();
pbio_error_t pbio_main_stop_application_resources(void);
void pbio_main_soft_stop(void);

#endif // _PBIO_MAIN_H_
39 changes: 17 additions & 22 deletions lib/pbio/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,37 +69,28 @@ void pbio_main_soft_stop(void) {
pbdrv_bluetooth_cancel_operation_request();
}

static void wait_for_bluetooth(void) {
pbio_os_state_t unused;
while (pbdrv_bluetooth_await_advertise_or_scan_command(&unused, NULL) == PBIO_ERROR_AGAIN ||
pbdrv_bluetooth_await_peripheral_command(&unused, NULL) == PBIO_ERROR_AGAIN) {

// Run event loop until Bluetooth is idle.
pbio_os_run_processes_and_wait_for_event();
}
}

/**
* Stops all application-level background processes. Called when the user
* application completes to get these modules back into their default state.
* Drivers and OS-level processes continue running.
*
* @return ::PBIO_SUCCESS when completed
* ::PBIO_ERROR_TIMEDOUT if it could not stop processes in a reasonable
* amount of time.
*/
void pbio_main_stop_application_resources() {
pbio_error_t pbio_main_stop_application_resources(void) {

pbio_main_soft_stop();

// Let ongoing task finish first.
wait_for_bluetooth();
pbio_error_t err;
pbio_os_state_t state = 0;
pbio_os_timer_t timer;
pbio_os_timer_set(&timer, 5000);

// Stop broadcasting, observing and disconnect peripheral.
pbdrv_bluetooth_start_broadcasting(NULL, 0);
wait_for_bluetooth();

pbdrv_bluetooth_start_observing(NULL);
wait_for_bluetooth();

pbdrv_bluetooth_peripheral_disconnect();
wait_for_bluetooth();
// Run event loop until Bluetooth is idle or times out.
while ((err = pbdrv_bluetooth_close_user_tasks(&state, &timer)) == PBIO_ERROR_AGAIN) {
pbio_os_run_processes_and_wait_for_event();
}

#if PBIO_CONFIG_LIGHT
pbio_light_animation_stop_all();
Expand All @@ -109,6 +100,10 @@ void pbio_main_stop_application_resources() {
pbio_image_fill(pbdrv_display_get_image(), 0);
pbdrv_display_update();
#endif

pbio_os_run_processes_and_wait_for_event();

return err;
}

/** @} */
7 changes: 6 additions & 1 deletion lib/pbio/sys/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ int main(int argc, char **argv) {
pbsys_main_run_program(&program);

// Stop motors, user animations, user bluetooth activity, etc.
pbio_main_stop_application_resources();
err = pbio_main_stop_application_resources();
if (err != PBIO_SUCCESS) {
// If we couldn't get the system back in a normal state, proceed
// towards shutdown.
break;
}

// Get system back in idle state.
pbsys_status_clear(PBIO_PYBRICKS_STATUS_USER_PROGRAM_RUNNING);
Expand Down