Remove CAN msg queue and update ThingSet lib

This commit is contained in:
Martin Jäger
2020-07-23 17:34:41 +02:00
parent dc4576c8b4
commit ed374e8e99
5 changed files with 11 additions and 169 deletions
-1
View File
@@ -5,5 +5,4 @@ target_sources(app PRIVATE
oled.cpp
serial.cpp
can.cpp
can_msg_queue.cpp
)
+10 -61
View File
@@ -16,9 +16,11 @@
#include <canbus/isotp.h>
#endif
#include <logging/log.h>
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 */
-69
View File
@@ -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 */
-37
View File
@@ -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 <zephyr.h>
#include <drivers/can.h>
#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 */