186 static_assert(!detail::DeclaresSetReset<Port> || detail::HasSetReset<Port>,
187 "bno08x: the optional SetReset hook must be `bool SetReset(bool level) noexcept`");
188 static_assert(!detail::DeclaresSetBootn<Port> || detail::HasSetBootn<Port>,
189 "bno08x: the optional SetBootn hook must be `bool SetBootn(bool level) noexcept`");
190 static_assert(!detail::DeclaresTakeIntTimestamp<Port> || detail::HasTakeIntTimestamp<Port>,
191 "bno08x: the optional TakeIntTimestamp hook must be `bool "
192 "TakeIntTimestamp(uint32_t&) noexcept`");
193 static_assert(!detail::DeclaresIntAsserted<Port> || detail::HasIntAsserted<Port>,
194 "bno08x: the IntAsserted hook must be `bool IntAsserted() noexcept`");
195 static_assert(!detail::DeclaresLog<Port> || detail::HasLog<Port>,
196 "bno08x: the optional Log hook must be `void Log(LogLevel, const char*) noexcept`");
197 static_assert(!detail::DeclaresUartWriteByte<Port> || detail::HasUartWriteByte<Port>,
198 "bno08x: the UartWriteByte hook must be `bool UartWriteByte(uint8_t) noexcept`");
199 static_assert((kTransportOf<Port> != Transport::UartShtp) || detail::HasUartWriteByte<Port> ||
200 detail::PacesUartTx<Port>,
201 "bno08x: a UART-SHTP port without UartWriteByte must set `static constexpr bool "
202 "kPortPacesUartTx = true`");
204 !detail::DeclaresUartWrite<Port> || detail::HasUartWrite<Port>,
205 "bno08x: the UartWrite hook must be `bool UartWrite(const uint8_t*, std::size_t) noexcept`");
209 static constexpr Transport kTransport = Port::kTransport;
216 : port_(port), rx_(stats_, cfg_), uart_(stats_), rvc_(stats_, cfg_),
217 adv_(detail::DefaultAdvert()) {}
233 void Poll(uint32_t now_us)
noexcept;
240 [[nodiscard]]
bool Ready() const noexcept {
241 return state_ == State::Ready;
295 [[nodiscard]] const
ProductId& Product() const noexcept {
304 return std::span<const ProductId>{product_entries_}.first(product_count_);
434 Result<
void> RvcSend(std::span<const uint8_t> text) noexcept;
443 [[nodiscard]] const
Stats& GetStats() const noexcept {
458 [[nodiscard]] uint32_t
AgeUs(
const Event& ev)
const noexcept {
459 return now_us_ - ev.t_us;
462 static constexpr std::size_t kTraceBytes = 32;
471 const std::size_t n = std::min({out.size(), trace_len_, kTraceBytes});
472 static_cast<void>(std::copy_n(trace_.begin(), n, out.begin()));
481 [[nodiscard]]
const detail::Advert&
Advertised() const noexcept {
486 static constexpr std::size_t kFrameBuf =
487 (kTransport == Transport::UartShtp) ? ((2U * Limits::kRxCargo) + 8U) : 8U;
488 static constexpr std::size_t kUartTxBuf =
489 (kTransport == Transport::UartShtp) ? ((2U * Limits::kTxCargo) + 8U) : 8U;
490 static constexpr std::size_t kReadBuf = Limits::kRxCargo + reg::kShtpHeaderLen;
491 static constexpr std::size_t kZeroBuf = (kTransport == Transport::Spi) ? kReadBuf : 1U;
492 static constexpr std::size_t kMaxReadsPerPoll = 8;
493 static constexpr uint32_t kHubUnknownCause = 0xFF;
494 static constexpr uint32_t kRestoreTries = 3;
498 void stepReset() noexcept;
499 void beginWaitAdvert() noexcept;
500 void checkStartup() noexcept;
501 void checkOpTimeout() noexcept;
502 void becomeReady() noexcept;
503 [[nodiscard]]
bool inStartupWindow() const noexcept;
504 void hubRestarted(uint8_t trigger) noexcept;
505 void noteViolation() noexcept;
506 void expireWindows() noexcept;
507 void recover() noexcept;
508 void serviceRestore() noexcept;
509 void restoreFailed(uint8_t sensor) noexcept;
510 void maybeReady() noexcept;
511 void serviceFaulted() noexcept;
512 void releaseWake() noexcept;
513 void resetTxPath() noexcept;
514 void abortUartTx() noexcept;
516 void pumpRx() noexcept;
517 void pumpRxBus() noexcept;
518 void pumpRxUart() noexcept;
519 void pumpRxRvc() noexcept;
520 bool intActive() noexcept;
521 uint32_t takeIntTime() noexcept;
522 bool readTransfer() noexcept;
523 void onTransfer(std::span<const uint8_t> transfer) noexcept;
524 void onUartFrame(std::span<const uint8_t> frame) noexcept;
525 void dispatch(uint8_t channel, std::span<const uint8_t> cargo) noexcept;
526 void onCommandChannel(std::span<const uint8_t> cargo) noexcept;
527 void onExecutable(std::span<const uint8_t> cargo) noexcept;
528 void onControl(std::span<const uint8_t> cargo) noexcept;
529 void onControlReport(std::span<const uint8_t> rep) noexcept;
530 void onInput(std::span<const uint8_t> cargo,
bool wake) noexcept;
531 void onGyroRv(std::span<const uint8_t> cargo) noexcept;
532 void emitSensor(std::span<const uint8_t> rep,
bool wake, int64_t ref_ticks) noexcept;
533 void onFeatureResponse(std::span<const uint8_t> rep) noexcept;
534 void onProductId(std::span<const uint8_t> rep) noexcept;
535 void onFrsWriteResponse(std::span<const uint8_t> rep) noexcept;
536 void onFrsReadResponse(std::span<const uint8_t> rep) noexcept;
537 void onCommandResponse(std::span<const uint8_t> rep) noexcept;
538 void onFlushDone(std::span<const uint8_t> rep) noexcept;
539 void onSh2Initialize(std::span<const uint8_t> rep) noexcept;
540 void fillCommandResult(const detail::Op& op, std::span<const uint8_t> rep) noexcept;
541 [[nodiscard]] static
bool commandIsFinal(const detail::Op& op,
542 std::span<const uint8_t> rep) noexcept;
543 [[nodiscard]] static
Error commandError(const detail::Op& op, std::span<const uint8_t> rep,
544 uint32_t& detail_word) noexcept;
545 void parseMetadata(uint16_t record, uint8_t sensor) noexcept;
546 void clearSession() noexcept;
547 [[nodiscard]]
bool frsBusy() const noexcept;
549 void pushEvent(const
Event& ev) noexcept;
550 void emitOpDone(const detail::Op& op,
Error err, uint32_t detail_word,
551 const std::array<int32_t, kEventValues>* vals) noexcept;
553 Result<
OpId> enqueue(const detail::Op& op) noexcept;
554 [[nodiscard]]
OpId queuedStop(const detail::Op& op) const noexcept;
555 bool evictForStop() noexcept;
556 void removeAt(std::
size_t index,
Error err) noexcept;
557 [[nodiscard]]
Result<
void> checkStop() const noexcept;
558 Result<
OpId> enqueueStopExec(uint8_t code) noexcept;
559 Result<
OpId> enqueueCommand(uint8_t cmd, const std::array<uint8_t, 9>& p,
560 bool no_response) noexcept;
561 Result<
OpId> enqueueExec(uint8_t code,
bool silent = false) noexcept;
562 Result<
void> checkReady(
bool needs_shtp) const noexcept;
565 [[nodiscard]] uint32_t wantedInterval(std::
size_t sensor) const noexcept;
566 [[nodiscard]]
bool userFeatureQueued(std::
size_t sensor) const noexcept;
570 bool off) const noexcept;
571 [[nodiscard]]
bool headWaits(detail::OpKind kind) const noexcept;
572 void startHead() noexcept;
573 void completeHead(
Error err, uint32_t detail_word) noexcept;
574 void completeHeadWith(
Error err, uint32_t detail_word,
575 const std::array<int32_t, kEventValues>* vals) noexcept;
576 void failAll(
Error err,
bool keep_stops = false) noexcept;
577 void afterComplete(const detail::Op& op,
Error err) noexcept;
578 void enterFaulted(
Error err) noexcept;
579 void toFront() noexcept;
580 void restartFailed() noexcept;
581 void afterSent() noexcept;
582 void frsReadFinished(
Error err, uint32_t detail_word) noexcept;
583 void frsWriteStep() noexcept;
584 void frsStartVerify() noexcept;
586 Result<
void> queueTx(uint8_t channel, std::span<const uint8_t> payload) noexcept;
587 void pumpTx() noexcept;
588 Error txBus() noexcept;
589 Error txSpi() noexcept;
590 Error txUart() noexcept;
591 void uartPump() noexcept;
592 bool uartTokenOk(std::
size_t total_len) noexcept;
593 void uartSendBsq() noexcept;
594 void txFailed(
Error err) noexcept;
595 void log(
LogLevel level, const
char* msg) noexcept {
596 detail::Log(port_, level, msg);
598 [[nodiscard]] std::size_t maxWrite() const noexcept;
599 [[nodiscard]] std::
size_t maxRead() const noexcept;
603 std::array<uint8_t, kTraceBytes> trace_{};
604 std::size_t trace_len_{0};
605 std::size_t trace_total_{0};
607 detail::RxEngine<Limits::kRxCargo> rx_;
608 detail::UartFramer<kFrameBuf> uart_;
609 detail::RvcParser rvc_;
611 State state_{State::Idle};
613 uint32_t since_us_{0};
614 uint32_t t_int_us_{0};
615 uint32_t last_poll_us_{0};
616 bool reset_low_{
false};
617 bool pin_reset_{
false};
618 bool advert_seen_{
false};
619 bool reset_seen_{
false};
620 bool init_seen_{
false};
621 bool restarting_{
false};
622 bool advert_asked_{
false};
623 uint32_t advert_at_us_{0};
624 bool reset_heard_{
false};
625 bool init_heard_{
false};
626 uint32_t ready_us_{0};
627 bool restart_event_pending_{
false};
628 std::array<uint8_t, reg::kChannelCount> tx_seq_{};
630 std::array<uint8_t, Limits::kTxCargo> tx_buf_{};
631 std::size_t tx_len_{0};
632 std::size_t duplex_len_{0};
633 bool tx_pending_{
false};
634 bool wake_active_{
false};
635 uint32_t wake_start_us_{0};
636 std::array<uint8_t, kUartTxBuf> uart_tx_{};
637 std::size_t uart_tx_len_{0};
638 bool uart_frame_open_{
false};
639 std::size_t uart_tx_pos_{0};
640 bool uart_tx_cargo_{
false};
641 uint32_t uart_last_byte_us_{0};
642 bool token_valid_{
false};
643 bool bsq_out_{
false};
645 uint16_t token_bytes_{0};
646 uint32_t token_us_{0};
648 std::array<uint8_t, kReadBuf> rd_buf_{};
649 std::array<uint8_t, kZeroBuf> zeros_{};
650 static constexpr std::size_t kRvcChunk = 32;
653 std::array<uint8_t, 64> uart_rx_chunk_{};
655 uint32_t viol_start_us_{0};
656 uint32_t viol_count_{0};
658 std::array<detail::Op, Limits::kOpQueue> ops_{};
659 std::size_t op_head_{0};
660 std::size_t op_count_{0};
664 std::array<uint32_t, kFrsMaxWords> frs_wr_{};
665 std::size_t frs_wr_len_{0};
666 std::size_t frs_wr_pos_{0};
667 FrsBuffer scratch_{};
668 FrsBuffer* frs_dst_{
nullptr};
670 std::array<FeatureConfig, kSensorSlots> features_{};
671 std::array<FeatureConfig, kSensorSlots> desired_{};
672 std::array<int32_t, kEventValues> op_v_{};
674 uint32_t restore_tries_{0};
675 std::array<Event, kSensorSlots> latest_{};
676 std::array<bool, kSensorSlots> have_latest_{};
677 std::array<uint8_t, kSensorSlots> last_report_seq_{};
678 std::array<Metadata, kSensorSlots> metadata_{};
679 ProductId product_{};
680 std::array<ProductId, 5> product_entries_{};
681 std::size_t product_count_{0};
683 std::array<Event, Limits::kEventQueue> events_{};
684 std::size_t ev_head_{0};
685 std::size_t ev_count_{0};
686 uint32_t pending_dropped_{0};