Skip to content

Commit

Permalink
[SensorBHI260AP] Added method
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Sep 27, 2023
1 parent e94feac commit bbd9807
Showing 1 changed file with 170 additions and 4 deletions.
174 changes: 170 additions & 4 deletions src/SensorBHI260AP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "bosch/common/common.h"

#define BHI260AP_SLAVE_ADDRESS 0x28
#define BHY_PROCESS_BUFFER_SZIE 512

class SensorBHI260AP
{
Expand Down Expand Up @@ -106,6 +107,10 @@ class SensorBHI260AP

void deinit()
{
if (processBuffer) {
free(processBuffer);
}
processBuffer = NULL;
// end();
}

Expand All @@ -116,9 +121,11 @@ class SensorBHI260AP

void update()
{
if (!processBuffer)
return;
if (__handler.irq != -1) {
if ( digitalRead(__handler.irq) == HIGH) {
bhy2_get_and_process_fifo(work_buffer, BHY2_WORK_BUFFER_SIZE, bhy2);
bhy2_get_and_process_fifo(processBuffer, BHY2_WORK_BUFFER_SIZE, bhy2);
}
}
}
Expand All @@ -142,7 +149,7 @@ class SensorBHI260AP
{
}

const bhy2_dev *getHandler()
bhy2_dev *getHandler()
{
return &__handler.bhy2;
}
Expand All @@ -169,6 +176,153 @@ class SensorBHI260AP
}


bool setInterruptCtrl(uint8_t data)
{
__error_code = bhy2_set_host_interrupt_ctrl(data, bhy2);
if (__error_code != BHY2_OK) {
return false;
}
}

uint8_t getInterruptCtrl()
{
uint8_t data;
__error_code = bhy2_get_host_interrupt_ctrl(&data, bhy2);
if (__error_code != BHY2_OK) {
return 0;
}
return data;
}

void printInterruptCtrl(Stream &steram)
{
uint8_t data;
__error_code = bhy2_get_host_interrupt_ctrl(&data, bhy2);
if (__error_code != BHY2_OK) {
return ;
}
steram.printf("Host interrupt control\r\n");
steram.printf("-- Wake up FIFO %s.\r\n", (data & BHY2_ICTL_DISABLE_FIFO_W) ? "disabled" : "enabled");
steram.printf("-- Non wake up FIFO %s.\r\n", (data & BHY2_ICTL_DISABLE_FIFO_NW) ? "disabled" : "enabled");
steram.printf("-- Status FIFO %s.\r\n", (data & BHY2_ICTL_DISABLE_STATUS_FIFO) ? "disabled" : "enabled");
steram.printf("-- Debugging %s.\r\n", (data & BHY2_ICTL_DISABLE_DEBUG) ? "disabled" : "enabled");
steram.printf("-- Fault %s.\r\n", (data & BHY2_ICTL_DISABLE_FAULT) ? "disabled" : "enabled");
steram.printf("-- Interrupt is %s.\r\n", (data & BHY2_ICTL_ACTIVE_LOW) ? "active low" : "active high");
steram.printf("-- Interrupt is %s triggered.\r\n", (data & BHY2_ICTL_EDGE) ? "pulse" : "level");
steram.printf("-- Interrupt pin drive is %s.\r\n", (data & BHY2_ICTL_OPEN_DRAIN) ? "open drain" : "push-pull");
}

bool isReady()
{
uint8_t boot_status;
__error_code = bhy2_get_boot_status(&boot_status, bhy2);
if (__error_code != BHY2_OK) {
return false;
}
return boot_status & BHY2_BST_HOST_INTERFACE_READY;
}

uint16_t getKernelVersion()
{
uint16_t version = 0;
__error_code = bhy2_get_kernel_version(&version, bhy2);
if ((__error_code != BHY2_OK) && (version == 0)) {
return 0;
}
log_i("Boot successful. Kernel version %u.\r\n", version);
return version;
}

enum EventID {
EVENT_ID_PADDING = (0),
EVENT_ID_TS_SMALL_DELTA = (251),
EVENT_ID_TS_LARGE_DELTA = (252),
EVENT_ID_TS_FULL = (253),
EVENT_ID_META_EVENT = (254),
EVENT_ID_TS_SMALL_DELTA_WU = (245),
EVENT_ID_TS_LARGE_DELTA_WU = (246),
EVENT_ID_TS_FULL_WU = (247),
EVENT_ID_META_EVENT_WU = (248),
EVENT_ID_FILLER = (255),
EVENT_ID_DEBUG_MSG = (250),
EVENT_ID_BHY2_LOG_UPDATE_SUB = (243),
EVENT_ID_BHY2_LOG_DOSTEP = (244),
};

bool registerEventCallback(EventID event_id, bhy2_fifo_parse_callback_t callback, void *private_data)
{
__error_code = bhy2_register_fifo_parse_callback(event_id, callback, private_data, bhy2);
return __error_code == BHY2_OK;
}


void setProcessBufferSize(uint32_t size)
{
processBufferSize = size;
}


bool uploadFirmware(const uint8_t *firmware, uint32_t length, bool write2Flash = false)
{
uint8_t sensor_error;
uint8_t boot_status;
__error_code = bhy2_get_boot_status(&boot_status, bhy2);
if (__error_code != BHY2_OK) {
return false;
}
if (write2Flash) {
if (boot_status & BHY2_BST_FLASH_DETECTED) {
uint32_t start_addr = BHY2_FLASH_SECTOR_START_ADDR;
uint32_t end_addr = start_addr + length;
log_i("Flash detected. Erasing flash to upload firmware\r\n");
__error_code = bhy2_erase_flash(start_addr, end_addr, bhy2);
if (__error_code != BHY2_OK) {
return false;
}
} else {
log_e("Flash not detected\r\n");
return false;
}
printf("Loading firmware into FLASH.\r\n");
__error_code = bhy2_upload_firmware_to_flash(firmware, length, bhy2);
if (__error_code != BHY2_OK) {
return false;
}
} else {
log_i("Loading firmware into RAM.\r\n");
__error_code = bhy2_upload_firmware_to_ram(firmware, length, bhy2);
}

log_i("Loading firmware into RAM Done\r\n");
__error_code = bhy2_get_error_value(&sensor_error, bhy2);
if (__error_code != BHY2_OK) {
return false;
}
if (sensor_error != BHY2_OK) {
__error_code = bhy2_get_error_value(&sensor_error, bhy2);
log_e("%s\r\n", get_sensor_error_text(sensor_error));
return false;
}


if (write2Flash) {
log_i("Booting from FLASH.\r\n");
__error_code = bhy2_boot_from_flash(bhy2);
} else {
log_i("Booting from RAM.\r\n");
__error_code = bhy2_boot_from_ram(bhy2);
}

__error_code = bhy2_get_error_value(&sensor_error, bhy2);
if (sensor_error) {
log_e("%s\r\n", get_sensor_error_text(sensor_error));
return false;
}
return sensor_error == BHY2_OK;
}



private:

bool initImpl()
Expand All @@ -187,6 +341,8 @@ class SensorBHI260AP
setup_interfaces(true, __handler); /* Perform a power on reset */

rslt = bhy2_init(BHY2_I2C_INTERFACE, bhy2_i2c_read, bhy2_i2c_write, bhy2_delay_us, BHY2_RD_WR_LEN, &__handler, &__handler.bhy2);

rslt |= bhy2_set_host_intf_ctrl(BHY2_I2C_INTERFACE, &__handler.bhy2);
break;

case BHY2_SPI_INTERFACE:
Expand All @@ -199,6 +355,8 @@ class SensorBHI260AP
setup_interfaces(true, __handler); /* Perform a power on reset */

rslt = bhy2_init(BHY2_SPI_INTERFACE, bhy2_spi_read, bhy2_spi_write, bhy2_delay_us, BHY2_RD_WR_LEN, &__handler, &__handler.bhy2);

rslt |= bhy2_set_host_intf_ctrl(BHY2_SPI_INTERFACE, &__handler.bhy2);
break;
default:
return false;
Expand Down Expand Up @@ -233,13 +391,21 @@ class SensorBHI260AP

bhy2_get_virt_sensor_list(bhy2);

return true;
#if defined(ESP32) && defined(BOARD_HAS_PSRAM)
processBuffer = (uint8_t *)ps_malloc(processBufferSize);
#else
processBuffer = (uint8_t *)malloc(processBufferSize);
#endif

return processBuffer == NULL;
}

protected:
struct bhy2_dev *bhy2 = NULL;
bhy_config_t __handler;
uint8_t work_buffer[BHY2_WORK_BUFFER_SIZE];
uint8_t __error_code;
uint8_t *processBuffer = NULL;
size_t processBufferSize = BHY_PROCESS_BUFFER_SZIE;
};


Expand Down

0 comments on commit bbd9807

Please sign in to comment.