Skip to content

Commit

Permalink
feat: Add USB error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
apage224 committed Aug 10, 2023
1 parent 15bde7c commit abd53fa
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 29 deletions.
2 changes: 1 addition & 1 deletion evb/src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
#define SEG_FRAME_LEN (512)
#define SEG_OVERLAP_LEN (20)
#define SEG_STEP_SIZE (SEG_FRAME_LEN - 2 * SEG_OVERLAP_LEN)
#define SEG_THRESHOLD (0.75)
#define SEG_THRESHOLD (0.50)

// Beat block
#define BEAT_ENABLE 1
Expand Down
39 changes: 24 additions & 15 deletions evb/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fetch_samples_from_stimulus(float32_t *samples, uint32_t numSamples) {
}

uint32_t
fetch_samples_from_pc(float32_t *samples, uint32_t numSamples) {
fetch_samples_from_pc(float32_t *samples, uint32_t numSamples, uint32_t *reqSamples) {
/**
* @brief Fetch samples from PC over RPC
* @param samples Buffer to store samples
Expand All @@ -197,14 +197,14 @@ fetch_samples_from_pc(float32_t *samples, uint32_t numSamples) {
return 0;
}

uint32_t reqSamples = MIN(numSamples, RPC_BUF_LEN);
dataBlock resultBlock = {.length = reqSamples,
*reqSamples = MIN(numSamples, RPC_BUF_LEN);
dataBlock resultBlock = {.length = *reqSamples,
.dType = float32_e,
.description = rpcFetchSamplesDesc,
.cmd = generic_cmd,
.buffer = {
.data = (uint8_t *)(samples),
.dataLength = reqSamples * sizeof(float32_t),
.dataLength = *reqSamples * sizeof(float32_t),
}};

err = ns_rpc_data_computeOnPC(&resultBlock, &resultBlock);
Expand All @@ -216,10 +216,12 @@ fetch_samples_from_pc(float32_t *samples, uint32_t numSamples) {
}
if (err) {
ns_printf("Failed fetching from PC w/ error: %x\n", err);
return 0;
*reqSamples = 0;
return 1;
}
memcpy(samples, resultBlock.buffer.data, resultBlock.buffer.dataLength);
return resultBlock.buffer.dataLength / sizeof(float32_t);
*reqSamples = resultBlock.buffer.dataLength / sizeof(float32_t);
return 0;
}

void
Expand Down Expand Up @@ -319,11 +321,12 @@ start_collecting(void) {
* @brief Setup sensor for collecting
*
*/
uint32_t newSamples = 0;
if (hkStore.collectMode == SENSOR_DATA_COLLECT) {
start_sensor(&sensorCtx);
// Discard first second for sensor warm up time
for (size_t i = 0; i < 100; i++) {
capture_sensor_data(&sensorCtx, hkStore.rawData, NULL, NULL, NULL, SENSOR_LEN);
capture_sensor_data(&sensorCtx, hkStore.rawData, NULL, NULL, NULL, SENSOR_LEN, &newSamples);
ns_delay_us(10000);
}
}
Expand All @@ -337,6 +340,7 @@ collect_samples() {
* @brief Collect samples from sensor or PC
* @return # new samples collected
*/
uint32_t err = 0;
uint32_t newSamples = 0;
uint32_t reqSamples = HK_DATA_LEN - hkStore.numSamples;
float32_t *data = &hkStore.rawData[hkStore.numSamples];
Expand All @@ -345,14 +349,14 @@ collect_samples() {
}
if (hkStore.collectMode == STIMULUS_DATA_COLLECT) {
// newSamples = fetch_samples_from_stimulus(data, reqSamples);
newSamples = fetch_samples_from_pc(data, reqSamples);
err = fetch_samples_from_pc(data, reqSamples, &newSamples);
ns_delay_us(200);
} else if (hkStore.collectMode == SENSOR_DATA_COLLECT) {
newSamples = capture_sensor_data(&sensorCtx, data, NULL, NULL, NULL, reqSamples);
err = capture_sensor_data(&sensorCtx, data, NULL, NULL, NULL, reqSamples, &newSamples);
}
hkStore.numSamples += newSamples;
ns_delay_us(5000);
return newSamples;
return err;
}

void
Expand Down Expand Up @@ -402,13 +406,16 @@ wakeup() {
*/
am_bsp_itm_printf_enable();
am_bsp_debug_printf_enable();
ns_delay_us(5000);
usb_update_state();
}

void
deepsleep() {
/**
* @brief Put SoC into deep sleep
*/
usbCfg.available = false;
am_bsp_itm_printf_disable();
am_bsp_debug_printf_disable();
am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP);
Expand All @@ -422,10 +429,10 @@ setup() {
// Power configuration (mem, cache, peripherals, clock)
NS_TRY(ns_core_init(&nsCoreCfg), "NS Core init failed");
NS_TRY(ns_power_config(&nsPwrCfg), "NS Power config failed");
NS_TRY(init_usb_handler(&usbCfg), "USB init failed");
am_hal_interrupt_master_enable();
wakeup();
ns_i2c_interface_init(&nsI2cCfg, AM_HAL_IOM_400KHZ);
NS_TRY(init_usb_handler(&usbCfg), "USB init failed");
ns_rpc_genericDataOperations_init(&nsRpcCfg);
NS_TRY(ns_peripheral_button_init(&nsBtnCfg), "NS Button init failed");
NS_TRY(init_sensor(&sensorCtx), "Sensor init failed");
Expand Down Expand Up @@ -460,15 +467,17 @@ loop() {
break;

case COLLECT_STATE:
collect_samples();
if (hkStore.numSamples >= HK_DATA_LEN) {
hkStore.errorCode = collect_samples();
if (hkStore.errorCode != 0) {
hkStore.state = STOP_COLLECT_STATE;
} else if (hkStore.numSamples >= HK_DATA_LEN) {
hkStore.state = STOP_COLLECT_STATE;
}
break;

case STOP_COLLECT_STATE:
stop_collecting();
hkStore.state = PREPROCESS_STATE;
hkStore.state = hkStore.errorCode != 0 ? FAIL_STATE : PREPROCESS_STATE;
break;

case PREPROCESS_STATE:
Expand All @@ -482,7 +491,7 @@ loop() {
print_to_pc("INFERENCE_STATE\n");
hkStore.errorCode = inference();
am_hal_pwrctrl_mcu_mode_select(AM_HAL_PWRCTRL_MCU_MODE_LOW_POWER);
hkStore.state = hkStore.errorCode == 1 ? FAIL_STATE : DISPLAY_STATE;
hkStore.state = hkStore.errorCode != 0 ? FAIL_STATE : DISPLAY_STATE;
break;

case DISPLAY_STATE:
Expand Down
12 changes: 6 additions & 6 deletions evb/src/sensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ stop_sensor(hk_sensor_t *ctx) {
}

uint32_t
capture_sensor_data(hk_sensor_t *ctx, float32_t *slot0, float32_t *slot1, float32_t *slot2, float32_t *slot3, uint32_t maxSamples) {
uint32_t numSamples;
capture_sensor_data(hk_sensor_t *ctx, float32_t *slot0, float32_t *slot1, float32_t *slot2, float32_t *slot3, uint32_t maxSamples,
uint32_t *numSamples) {
int32_t val;
float32_t *slots[MAX86150_NUM_SLOTS] = {slot0, slot1, slot2, slot3};
static uint32_t maxFifoBuffer[MAX86150_FIFO_DEPTH * MAX86150_NUM_SLOTS];
numSamples = max86150_read_fifo_samples(ctx->maxCtx, maxFifoBuffer, ctx->maxCfg->fifoSlotConfigs, ctx->maxCfg->numSlots);
numSamples = numSamples < maxSamples ? numSamples : maxSamples;
for (size_t i = 0; i < numSamples; i++) {
*numSamples = max86150_read_fifo_samples(ctx->maxCtx, maxFifoBuffer, ctx->maxCfg->fifoSlotConfigs, ctx->maxCfg->numSlots);
*numSamples = *numSamples < maxSamples ? *numSamples : maxSamples;
for (size_t i = 0; i < *numSamples; i++) {
for (size_t j = 0; j < ctx->maxCfg->numSlots; j++) {
val = maxFifoBuffer[ctx->maxCfg->numSlots * i + j];
if (ctx->maxCfg->fifoSlotConfigs[j] == Max86150SlotEcg) {
Expand All @@ -91,5 +91,5 @@ capture_sensor_data(hk_sensor_t *ctx, float32_t *slot0, float32_t *slot1, float3
}
}
}
return numSamples;
return 0;
}
3 changes: 2 additions & 1 deletion evb/src/sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ init_sensor(hk_sensor_t *ctx);
void
start_sensor(hk_sensor_t *ctx);
uint32_t
capture_sensor_data(hk_sensor_t *ctx, float32_t *slot0, float32_t *slot1, float32_t *slot2, float32_t *slot3, uint32_t maxSamples);
capture_sensor_data(hk_sensor_t *ctx, float32_t *slot0, float32_t *slot1, float32_t *slot2, float32_t *slot3, uint32_t maxSamples,
uint32_t *numSamples);
void
stop_sensor(hk_sensor_t *ctx);

Expand Down
28 changes: 23 additions & 5 deletions evb/src/usb_handler.cc
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
// Handle TinyUSB events

#include "usb_handler.h"
#include "ns_ambiqsuite_harness.h"
#include "ns_usb.h"

bool volatile *g_usb_available;
bool volatile *g_usb_available = nullptr;

void
tud_mount_cb(void) {
*g_usb_available = true;
if (g_usb_available != nullptr) {
*g_usb_available = true;
}
}
void
tud_resume_cb(void) {
*g_usb_available = true;
if (g_usb_available != nullptr) {
*g_usb_available = true;
}
}
void
tud_umount_cb(void) {
*g_usb_available = false;
if (g_usb_available != nullptr) {
*g_usb_available = false;
}
}
void
tud_suspend_cb(bool remote_wakeup_en) {
*g_usb_available = false;
if (g_usb_available != nullptr) {
*g_usb_available = false;
}
}

void
usb_update_state() {
if (g_usb_available != nullptr) {
*g_usb_available = tud_mounted();
}
}

uint32_t
init_usb_handler(usb_config_t *ctx) {
g_usb_available = &ctx->available;
usb_update_state();
return 0;
}
3 changes: 2 additions & 1 deletion evb/src/usb_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ typedef struct {

uint32_t
init_usb_handler(usb_config_t *ctx);

void
usb_update_state();
#endif

0 comments on commit abd53fa

Please sign in to comment.