remove exceptions from 'zephyr' helpers

This commit is contained in:
Bartosz Wieczorek 2025-06-13 08:15:48 +02:00
parent 28c6899e6c
commit 889e7bc976
5 changed files with 25 additions and 37 deletions

View File

@ -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);

View File

@ -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.

View File

@ -87,7 +87,6 @@ class UARTThread : public ZephyrThread {
// ZephyrThread interface
protected:
int do_hardwarenInit() override;
void threadMain() override;
};

View File

@ -1,45 +1,50 @@
#include "zephyr.hpp"
#include "error.hpp"
#include "log.hpp"
#include "zephyr/sys/__assert.h"
#include <expected>
#include <source_location>
void zephyr::adc::channel_setup(const device *adc, const adc_channel_cfg &channel_cfg, std::source_location sl) {
std::expected<void, zephyr::adc::error> 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<void, zephyr::gpio::error> 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<void, zephyr::gpio::error> 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<void, zephyr::gpio::error> 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 {};
}

View File

@ -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<void, error> 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<void, error> pin_configure(const gpio_dt_spec &spec, gpio_flags_t flags, std::source_location sl = std::source_location::current());
std::expected<void, error> pin_interrupt_configure(const struct gpio_dt_spec &spec, gpio_flags_t flags, std::source_location sl = std::source_location::current());
std::expected<void, error> add_callback(const gpio_dt_spec &spec, callback &callback, std::source_location sl = std::source_location::current());
} // namespace zephyr::gpio