diff --git a/lib/thingset b/lib/thingset index 9e8eec4..d02766f 160000 --- a/lib/thingset +++ b/lib/thingset @@ -1 +1 @@ -Subproject commit 9e8eec4d5f46b080b85b78b3705c1f5642dfca7f +Subproject commit d02766f1877bb57943bc2a96e62ae713f5fd8942 diff --git a/src/ext/CMakeLists.txt b/src/ext/CMakeLists.txt index 60ab8d5..5d94512 100644 --- a/src/ext/CMakeLists.txt +++ b/src/ext/CMakeLists.txt @@ -5,5 +5,4 @@ target_sources(app PRIVATE oled.cpp serial.cpp can.cpp - can_msg_queue.cpp ) diff --git a/src/ext/can.cpp b/src/ext/can.cpp index e7b1bfb..9822634 100644 --- a/src/ext/can.cpp +++ b/src/ext/can.cpp @@ -16,9 +16,11 @@ #include #endif +#include +LOG_MODULE_REGISTER(ext_can, CONFIG_LOG_DEFAULT_LEVEL); + #include "thingset.h" #include "data_nodes.h" -#include "can_msg_queue.h" #ifndef CAN_NODE_ID #define CAN_NODE_ID 20 @@ -121,30 +123,11 @@ class ThingSetCAN: public ExtInterface public: ThingSetCAN(uint8_t can_node_id, const unsigned int c); - void process_asap(); void process_1s(); void enable(); private: - /** - * Generate CAN frame for data object and put it into TX queue - */ - bool pub_object(const DataNode& data_obj); - - /** - * Retrieves all data objects of configured channel and calls pub_object to enqueue them - * - * \returns number of can objects added to queue - */ - int pub(); - - /** - * Try to send out all data in TX queue - */ - void process_outbox(); - - CanMsgQueue tx_queue; uint8_t node_id; const uint16_t channel; @@ -153,13 +136,6 @@ private: ThingSetCAN ts_can(CAN_NODE_ID, PUB_CAN); -//---------------------------------------------------------------------------- -// preliminary simple CAN functions to send data to the bus for logging -// Data format based on CBOR specification (except for first byte, which uses -// only 6 bit to specify type and transport protocol) -// -// Protocol details: https://libre.solar/thingset/ - ThingSetCAN::ThingSetCAN(uint8_t can_node_id, const unsigned int c): node_id(can_node_id), channel(c) @@ -186,15 +162,13 @@ void ThingSetCAN::enable() #endif /* CONFIG_ISOTP */ } -void ThingSetCAN::process_1s() +void can_pub_isr(uint32_t err_flags, void *arg) { - pub(); - process_asap(); + // Do nothing. Publication messages are fire and forget. } -int ThingSetCAN::pub() +void ThingSetCAN::process_1s() { - int retval = 0; unsigned int can_id; uint8_t can_data[8]; @@ -211,38 +185,13 @@ int ThingSetCAN::pub() if (data_len >= 0) { frame.dlc = data_len; - tx_queue.enqueue(frame); + + if (can_send(can_dev, &frame, K_MSEC(10), can_pub_isr, NULL) != CAN_TX_OK) { + LOG_DBG("Error sending CAN frame\n"); + } } - retval++; } } - return retval; -} - -void ThingSetCAN::process_asap() -{ - process_outbox(); -} - -void can_pub_isr(uint32_t err_flags, void *arg) -{ - // Do nothing. Publication messages are fire and forget. -} - -void ThingSetCAN::process_outbox() -{ - int max_attempts = 15; - while (!tx_queue.empty() && max_attempts > 0) { - CanFrame msg; - tx_queue.first(msg); - if (can_send(can_dev, &msg, K_MSEC(10), can_pub_isr, NULL) == CAN_TX_OK) { - tx_queue.dequeue(); - } - else { - //printk("Sending CAN message failed"); - } - max_attempts--; - } } #endif /* CONFIG_EXT_THINGSET_CAN */ diff --git a/src/ext/can_msg_queue.cpp b/src/ext/can_msg_queue.cpp deleted file mode 100644 index fddcfe8..0000000 --- a/src/ext/can_msg_queue.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2017 Martin Jäger / Libre Solar - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef UNIT_TEST - -#if CONFIG_EXT_THINGSET_CAN - -#include "ext/can_msg_queue.h" - -bool CanMsgQueue::full() -{ - return _length == CAN_QUEUE_SIZE; -} - -bool CanMsgQueue::empty() -{ - return _length == 0; -} - -void CanMsgQueue::enqueue(CanFrame frame) -{ - if (!full()) { - _queue[(_first + _length) % CAN_QUEUE_SIZE] = frame; - _length++; - } -} - -int CanMsgQueue::dequeue() -{ - if (!empty()) { - _first = (_first + 1) % CAN_QUEUE_SIZE; - _length--; - return 1; - } - else { - return 0; - } -} - -int CanMsgQueue::dequeue(CanFrame &frame) -{ - if (!empty()) { - frame = _queue[_first]; - _first = (_first + 1) % CAN_QUEUE_SIZE; - _length--; - return 1; - } - else { - return 0; - } -} - -int CanMsgQueue::first(CanFrame &frame) -{ - if (!empty()) { - frame = _queue[_first]; - return 1; - } - else { - return 0; - } -} - -#endif /* CONFIG_EXT_THINGSET_CAN */ - -#endif /* UNIT_TEST */ diff --git a/src/ext/can_msg_queue.h b/src/ext/can_msg_queue.h deleted file mode 100644 index d7f3de1..0000000 --- a/src/ext/can_msg_queue.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2017 Martin Jäger / Libre Solar - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#ifndef CAN_MSG_QUEUE_H -#define CAN_MSG_QUEUE_H - -#ifdef __MBED__ -#include "mbed.h" -#define CanFrame CANMessage -#elif defined(__ZEPHYR__) -#include -#include -#define CanFrame struct zcan_frame -#endif - -#define CAN_QUEUE_SIZE 30 - -class CanMsgQueue -{ -public: - bool full(); - bool empty(); - void enqueue(CanFrame frame); - int dequeue(CanFrame &frame); - int dequeue(); - int first(CanFrame& frame); - -private: - CanFrame _queue[CAN_QUEUE_SIZE]; - int _first; - int _length; -}; - -#endif /* CAN_MSG_QUEUE_H */