From 889e7bc976cdf3510d0b4676b1bcbeb279033ad2 Mon Sep 17 00:00:00 2001 From: Bartosz Wieczorek Date: Fri, 13 Jun 2025 08:15:48 +0200 Subject: [PATCH] remove exceptions from 'zephyr' helpers --- rims_app/src/error.hpp | 10 +++++++--- rims_app/src/uart.cpp | 4 ---- rims_app/src/uart.hpp | 1 - rims_app/src/zephyr.cpp | 23 ++++++++++++++--------- rims_app/src/zephyr.hpp | 24 ++++-------------------- 5 files changed, 25 insertions(+), 37 deletions(-) diff --git a/rims_app/src/error.hpp b/rims_app/src/error.hpp index 66279bb13c0..14f19b30600 100644 --- a/rims_app/src/error.hpp +++ b/rims_app/src/error.hpp @@ -28,14 +28,18 @@ struct Error { } namespace zephyr::gpio { - ERR_CATEGORY(zephyr_gpio, 16); - ERR(ioerror, EIO); ERR(wouldblock, EWOULDBLOCK); - +ERR(pin_configure_error, __ELASTERROR + 1); } // namespace zephyr::gpio +namespace zephyr::adc{ +ERR_CATEGORY(zephyr_adc, 17); +ERR(configuration_error, 1); +} // namespace zephyr::gpio + + namespace rims::gpio { ERR_CATEGORY(rims_gpio, gpio_receiver_tag); ERR(bad_id, gpio_Error_BadId); diff --git a/rims_app/src/uart.cpp b/rims_app/src/uart.cpp index 13de524ab46..8612a8dd02d 100644 --- a/rims_app/src/uart.cpp +++ b/rims_app/src/uart.cpp @@ -201,10 +201,6 @@ void AsyncUART::handleRxBufferOverflowError()noexcept { _faultFlag = true; } -int UARTThread::do_hardwarenInit() { - return 0; -} - void UARTThread::threadMain() { k_work_init(&uart_log_work.work, log_worker); /// runs in context of new thread, on new thread stack etc. diff --git a/rims_app/src/uart.hpp b/rims_app/src/uart.hpp index 4839853bab4..3830206316d 100644 --- a/rims_app/src/uart.hpp +++ b/rims_app/src/uart.hpp @@ -87,7 +87,6 @@ class UARTThread : public ZephyrThread { // ZephyrThread interface protected: - int do_hardwarenInit() override; void threadMain() override; }; diff --git a/rims_app/src/zephyr.cpp b/rims_app/src/zephyr.cpp index 80a22be1f35..3f78119357c 100644 --- a/rims_app/src/zephyr.cpp +++ b/rims_app/src/zephyr.cpp @@ -1,45 +1,50 @@ #include "zephyr.hpp" +#include "error.hpp" #include "log.hpp" -#include "zephyr/sys/__assert.h" +#include #include -void zephyr::adc::channel_setup(const device *adc, const adc_channel_cfg &channel_cfg, std::source_location sl) { +std::expected zephyr::adc::channel_setup(const device *adc, const adc_channel_cfg &channel_cfg, std::source_location sl) { __ASSERT(adc, "adc needs to work"); auto status = adc_channel_setup(adc, &channel_cfg); if (status < 0) { rims::Log{sl}.error("%s for %s:%d failed with status %d", "adc_channel_setup", adc->name, channel_cfg.channel_id, status); - throw adc_configure_error{}; + return std::unexpected{adc::configuration_error{}}; } rims::Log{sl}.info("Channel onfiguration done"); + return {}; } -void zephyr::gpio::pin_configure(const gpio_dt_spec &spec, gpio_flags_t flags, std::source_location sl) { +std::expected zephyr::gpio::pin_configure(const gpio_dt_spec &spec, gpio_flags_t flags, std::source_location sl) { auto status = gpio_pin_configure_dt(&spec, flags); if (status < 0) { rims::Log{sl}.error("%s for %s:%d failed with status %d", "gpio_pin_configure_dt", spec.port->name, spec.pin, status); - throw pin_configure_error{}; + return std::unexpected{pin_configure_error{}}; } else { rims::Log{sl}.info("%s for %s:%d checked and ready", "gpio_pin_configure_dt", spec.port->name, spec.pin); } + return {}; } -void zephyr::gpio::pin_interrupt_configure(const gpio_dt_spec &spec, gpio_flags_t flags, std::source_location sl) { +std::expected zephyr::gpio::pin_interrupt_configure(const gpio_dt_spec &spec, gpio_flags_t flags, std::source_location sl) { auto status = gpio_pin_interrupt_configure_dt(&spec, GPIO_INT_EDGE_BOTH); if (status < 0) { rims::Log{sl}.error("%s pin %d@%s failed with status %d", "gpio_pin_interrupt_configure_dt", spec.pin, spec.port->name, status); - throw pin_configure_error{}; + return std::unexpected{pin_configure_error{}}; } else { rims::Log{sl}.info("%s pin %d@%s ok", "gpio_pin_interrupt_configure_dt", spec.pin, spec.port->name); } + return {}; } -void zephyr::gpio::add_callback(const gpio_dt_spec &spec, callback &callback, std::source_location sl) { +std::expected zephyr::gpio::add_callback(const gpio_dt_spec &spec, callback &callback, std::source_location sl) { auto status = gpio_add_callback(spec.port, &callback._cb); if (status < 0) { rims::Log{sl}.error("%s pin %d@%s failed with status %d", "gpio_add_callback", spec.pin, spec.port->name, status); - throw pin_configure_error{}; + return std::unexpected{pin_configure_error{}}; } else { rims::Log{sl}.info("%s pin %d@%s ok", "gpio_add_callback", spec.pin, spec.port->name); } + return {}; } diff --git a/rims_app/src/zephyr.hpp b/rims_app/src/zephyr.hpp index cb657139e66..d07e09e202d 100644 --- a/rims_app/src/zephyr.hpp +++ b/rims_app/src/zephyr.hpp @@ -189,27 +189,11 @@ inline int k_sem_take(sem &sem, std::chrono::microseconds us) { } // namespace zephyr::semaphore namespace zephyr::adc { -class adc_configure_error : public std::exception { - // exception interface - public: - const char *what() const noexcept override { - return "adc_channel_setup error"; - } -}; - -void channel_setup(const struct device *adc, const adc_channel_cfg &channel_cfg, std::source_location sl = std::source_location::current()); +std::expected channel_setup(const struct device *adc, const adc_channel_cfg &channel_cfg, std::source_location sl = std::source_location::current()); } // namespace zephyr::adc namespace zephyr::gpio { -class pin_configure_error : public std::exception { - // exception interface - public: - const char *what() const noexcept override { - return "gpio_pin_configure_dt error"; - } -}; - struct callback { gpio_callback _cb; callback(gpio_callback_handler_t handler, gpio_port_pins_t pin_mask) { @@ -217,7 +201,7 @@ struct callback { } }; -void pin_configure(const gpio_dt_spec &spec, gpio_flags_t flags, std::source_location sl = std::source_location::current()); -void pin_interrupt_configure(const struct gpio_dt_spec &spec, gpio_flags_t flags, std::source_location sl = std::source_location::current()); -void add_callback(const gpio_dt_spec &spec, callback &callback, std::source_location sl = std::source_location::current()); +std::expected pin_configure(const gpio_dt_spec &spec, gpio_flags_t flags, std::source_location sl = std::source_location::current()); +std::expected pin_interrupt_configure(const struct gpio_dt_spec &spec, gpio_flags_t flags, std::source_location sl = std::source_location::current()); +std::expected add_callback(const gpio_dt_spec &spec, callback &callback, std::source_location sl = std::source_location::current()); } // namespace zephyr::gpio