feat: improve error handling for radio operations and streamline logging
This commit is contained in:
@ -63,6 +63,17 @@ constexpr auto MAX_RX_BUFFER_SIZE = 0xff - DEFAULT_RX_BUFFER_ADDRESS;
|
||||
} \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief return if STATEVAR has value that is NOT RADIO_TRANS_CMD_PROC_ERR
|
||||
*/
|
||||
#define APP_RADIO_RETURN_ERR_IGNORE_PROC_ERR(STATEVAR) \
|
||||
{ \
|
||||
if (not(STATEVAR.has_value())) { \
|
||||
if (not(STATEVAR.error() == error::RADIO_TRANS_CMD_PROC_ERR)) { \
|
||||
return ue_t{STATEVAR.error()}; \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief return if STATEVAR has value, otherwise return the error
|
||||
@ -208,13 +219,7 @@ inline Result<irq_status_t, error_t>
|
||||
get_irq_status() {
|
||||
uint8_t data[] = {0x00, 0x00};
|
||||
const auto res = spi::read_stream(RADIOLIB_SX126X_CMD_GET_IRQ_STATUS, data);
|
||||
// get_irq_status would sometimes return CMD_PROC_ERR
|
||||
// however, the result is still valid
|
||||
if (not res) {
|
||||
if (not(res.error() == error::RADIO_TRANS_CMD_PROC_ERR)) {
|
||||
return ue_t{res.error()};
|
||||
}
|
||||
}
|
||||
APP_RADIO_RETURN_ERR_IGNORE_PROC_ERR(res);
|
||||
return irq_status_t{.raw = static_cast<uint16_t>(static_cast<uint16_t>(data[0] << 8) | static_cast<uint16_t>(data[1]))};
|
||||
}
|
||||
|
||||
@ -253,7 +258,7 @@ inline Result<status_t, error_t>
|
||||
get_status() {
|
||||
uint8_t data = 0;
|
||||
const auto res = spi::read_stream(RADIOLIB_SX126X_CMD_GET_STATUS, std::span<uint8_t>{&data, 1});
|
||||
APP_RADIO_RETURN_ERR(res);
|
||||
APP_RADIO_RETURN_ERR_IGNORE_PROC_ERR(res);
|
||||
return *reinterpret_cast<status_t *>(&data);
|
||||
}
|
||||
|
||||
@ -261,7 +266,7 @@ inline Result<packet_status_t, error_t>
|
||||
get_packet_status() {
|
||||
uint8_t data[3] = {0, 0, 0};
|
||||
const auto res = spi::read_stream(RADIOLIB_SX126X_CMD_GET_PACKET_STATUS, data, 3);
|
||||
APP_RADIO_RETURN_ERR(res);
|
||||
APP_RADIO_RETURN_ERR_IGNORE_PROC_ERR(res);
|
||||
return packet_status_t{
|
||||
.raw = {data[0], data[1], data[2]}};
|
||||
}
|
||||
@ -965,22 +970,13 @@ inline Result<std::tuple<uint8_t, uint8_t>, error_t>
|
||||
get_rx_buffer_status() {
|
||||
uint8_t rx_buf_status[] = {0, 0};
|
||||
auto res = spi::read_stream(RADIOLIB_SX126X_CMD_GET_RX_BUFFER_STATUS, rx_buf_status);
|
||||
APP_RADIO_RETURN_ERR(res);
|
||||
APP_RADIO_RETURN_ERR_IGNORE_PROC_ERR(res);
|
||||
return std::make_tuple(rx_buf_status[0], rx_buf_status[1]);
|
||||
}
|
||||
|
||||
struct read_result_t {
|
||||
irq_status_t irq_status;
|
||||
std::span<const uint8_t> data;
|
||||
|
||||
bool is_crc_err() const {
|
||||
return (irq_status.bits.crc_err) != 0;
|
||||
}
|
||||
|
||||
bool is_header_err() const {
|
||||
return (irq_status.bits.header_err) != 0;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief user should call this function after reading data (since it will override the internal buffer)
|
||||
*/
|
||||
@ -996,16 +992,6 @@ struct read_result_t {
|
||||
|
||||
inline Result<read_result_t, error_t>
|
||||
read_data_internal() {
|
||||
const auto irq_ = get_irq_status();
|
||||
APP_RADIO_RETURN_ERR(irq_);
|
||||
const auto st_ = get_status();
|
||||
APP_RADIO_RETURN_ERR(st_);
|
||||
const auto irq = *irq_;
|
||||
const auto st = *st_;
|
||||
if (irq.bits.timeout || st.command_status == CommandStatus::COMMAND_TIMEOUT) {
|
||||
return ue_t{error_t{error::RADIO_RX_TIMEOUT}};
|
||||
}
|
||||
|
||||
const auto tuple_ = get_rx_buffer_status();
|
||||
APP_RADIO_RETURN_ERR(tuple_);
|
||||
|
||||
@ -1032,7 +1018,7 @@ read_data_internal() {
|
||||
|
||||
auto res = hal::spi::read_buffer(r_ptr, sz);
|
||||
APP_RADIO_RETURN_ERR(res);
|
||||
return read_result_t{irq, *res};
|
||||
return read_result_t{*res};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user