-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare sensor hub HAL functions for BNO085 driver.
Note: RESET/IRQ/BOOTN GPIOs will need to be controlled separately.
- Loading branch information
Showing
6 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "sh2"] | ||
path = sh2 | ||
url = https://github.com/pika-spark/pika-spark-sh2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* This software is distributed under the terms of the MIT License. | ||
* Copyright (c) 2023 LXRobotics. | ||
* Author: Alexander Entinger <alexander.entinger@lxrobotics.com> | ||
* Contributors: https://github.com/107-systems/pika-spark-bno085-driver/graphs/contributors. | ||
*/ | ||
|
||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include "bno085.h" | ||
|
||
#include <cstring> | ||
|
||
/************************************************************************************** | ||
* PRIVATE FUNCTION DECLARATIONS | ||
**************************************************************************************/ | ||
|
||
static int priv_sh2_hal_open (sh2_Hal_t * self); | ||
static void priv_sh2_hal_close (sh2_Hal_t * self); | ||
static int priv_sh2_hal_read (sh2_Hal_t * self, uint8_t * pBuffer, unsigned len, uint32_t * t_us); | ||
static int priv_sh2_hal_write (sh2_Hal_t * self, uint8_t * pBuffer, unsigned len); | ||
static uint32_t priv_sh2_hal_getTimeUs(sh2_Hal_t * self); | ||
|
||
/************************************************************************************** | ||
* CTOR/DTOR | ||
**************************************************************************************/ | ||
|
||
BNO085::BNO085(std::shared_ptr<SPI> const spi) | ||
: _spi{spi} | ||
, _start{std::chrono::steady_clock::now()} | ||
{ | ||
_sh2_hal.user_reference = reinterpret_cast<void *>(this); | ||
|
||
reset(); | ||
init(); | ||
} | ||
|
||
/************************************************************************************** | ||
* PUBLIC MEMBER FUNCTIONS | ||
**************************************************************************************/ | ||
|
||
int BNO085::sh2_hal_read(uint8_t * pBuffer, unsigned len, uint32_t * /* t_us */) | ||
{ | ||
/* Read the sensor hub header. */ | ||
uint8_t sh_hdr_rx_buf[4] = {0}; | ||
uint8_t const sh_hdr_tx_buf[4] = {0}; | ||
|
||
if (!_spi->transfer(sh_hdr_tx_buf, sh_hdr_rx_buf, sizeof(sh_hdr_tx_buf))) | ||
return 0; | ||
|
||
/* Determine packet size (mask out continuous bit). */ | ||
uint16_t const packet_size = | ||
(static_cast<uint16_t>(sh_hdr_rx_buf[0]) | static_cast<uint16_t>(sh_hdr_rx_buf[1]) << 8) & 0x7FFF; | ||
|
||
size_t const bytes_to_read = std::min(static_cast<size_t>(packet_size), static_cast<size_t>(len)); | ||
|
||
/* Ensure that we only transmit zero's. */ | ||
memset(pBuffer, 0, bytes_to_read); | ||
|
||
/* Transmit zero's and read from the device. */ | ||
if (!_spi->transfer(pBuffer, pBuffer, bytes_to_read)) | ||
return 0; | ||
|
||
return bytes_to_read; | ||
} | ||
|
||
int BNO085::sh2_hal_write(uint8_t * pBuffer, unsigned len) | ||
{ | ||
if (!_spi->transfer(pBuffer, pBuffer, len)) | ||
return 0; | ||
|
||
return len; | ||
} | ||
|
||
uint32_t BNO085::sh2_hal_getTimeUs() | ||
{ | ||
auto const now = std::chrono::steady_clock::now(); | ||
auto const uptime = (now - _start); | ||
return std::chrono::duration_cast<std::chrono::microseconds>(uptime).count(); | ||
} | ||
|
||
/************************************************************************************** | ||
* PRIVATE MEMBER FUNCTIONS | ||
**************************************************************************************/ | ||
|
||
void BNO085::reset() | ||
{ | ||
/* TODO. */ | ||
} | ||
|
||
void BNO085::init() | ||
{ | ||
_sh2_hal.open = priv_sh2_hal_open; | ||
_sh2_hal.close = priv_sh2_hal_close; | ||
_sh2_hal.read = priv_sh2_hal_read; | ||
_sh2_hal.write = priv_sh2_hal_write; | ||
_sh2_hal.getTimeUs = priv_sh2_hal_getTimeUs; | ||
} | ||
|
||
/************************************************************************************** | ||
* PRIVATE FUNCTION DEFINITIONS | ||
**************************************************************************************/ | ||
|
||
int priv_sh2_hal_open(sh2_Hal_t * /* self */) | ||
{ | ||
return 0; | ||
} | ||
|
||
void priv_sh2_hal_close(sh2_Hal_t * /* self */) | ||
{ | ||
/* Do nothing. */ | ||
} | ||
|
||
int priv_sh2_hal_read(sh2_Hal_t * self, uint8_t * pBuffer, unsigned len, uint32_t * t_us) | ||
{ | ||
BNO085 * this_ptr = reinterpret_cast<BNO085 *>(self->user_reference); | ||
return this_ptr->sh2_hal_read(pBuffer, len, t_us); | ||
} | ||
|
||
int priv_sh2_hal_write(sh2_Hal_t * self, uint8_t * pBuffer, unsigned len) | ||
{ | ||
BNO085 * this_ptr = reinterpret_cast<BNO085 *>(self->user_reference); | ||
return this_ptr->sh2_hal_write(pBuffer, len); | ||
} | ||
|
||
uint32_t priv_sh2_hal_getTimeUs(sh2_Hal_t * self) | ||
{ | ||
BNO085 * this_ptr = reinterpret_cast<BNO085 *>(self->user_reference); | ||
return this_ptr->sh2_hal_getTimeUs(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* This software is distributed under the terms of the MIT License. | ||
* Copyright (c) 2023 LXRobotics. | ||
* Author: Alexander Entinger <alexander.entinger@lxrobotics.com> | ||
* Contributors: https://github.com/107-systems/pika-spark-bno085-driver/graphs/contributors. | ||
*/ | ||
|
||
#pragma once | ||
|
||
/************************************************************************************** | ||
* INCLUDE | ||
**************************************************************************************/ | ||
|
||
#include <chrono> | ||
#include <memory> | ||
|
||
#include <sh2.h> | ||
|
||
#include "spi.h" | ||
|
||
/************************************************************************************** | ||
* CLASS DECLARATION | ||
**************************************************************************************/ | ||
|
||
class BNO085 | ||
{ | ||
public: | ||
BNO085(std::shared_ptr<SPI> const spi); | ||
|
||
/* Do not publicly use those functions. | ||
* They are used for the sensor hub HAL. | ||
*/ | ||
int sh2_hal_read (uint8_t * pBuffer, unsigned len, uint32_t * t_us); | ||
int sh2_hal_write (uint8_t * pBuffer, unsigned len); | ||
uint32_t sh2_hal_getTimeUs(); | ||
|
||
private: | ||
std::shared_ptr<SPI> const _spi; | ||
std::chrono::steady_clock::time_point const _start; | ||
sh2_Hal_t _sh2_hal; | ||
|
||
void reset(); | ||
void init(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters