Working CAN (#35)

* Minor fix in documentation

* Make CAN compile again

* Now able to disable uart_serial

* Boots and runs CAN

Requires matching thingset-cpp library
The placement of the ts_can.process_asap/process_1s is a little hack,
but I did want to add even more ifdefs in the main method.
Renaming the functions could do the trick.
BTW, serial and CAN thingset can be run at the same time, no problems.

* Some cleanups in the CAN interface
This commit is contained in:
hogthrob
2019-06-23 00:41:01 +02:00
committed by Martin Jäger
parent 7d02c92c7d
commit 14faa1f0e8
8 changed files with 201 additions and 115 deletions
+3
View File
@@ -1,4 +1,6 @@
#ifndef UNIT_TEST
#include "mbed.h"
#ifdef STM32F0 // STM32L0 does not have CAN
#include "can_msg_queue.h"
@@ -49,3 +51,4 @@ int CANMsgQueue::first(CANMessage& msg) {
}
#endif /* STM32F0 */
#endif
+1 -1
View File
@@ -27,7 +27,7 @@
//#define DCDC_MODE_INIT MODE_NANOGRID
// basic battery configuration
#define BATTERY_TYPE BAT_TYPE_GEL ///< GEL most suitable for general batteries (see battery.h for other types)
#define BATTERY_TYPE BAT_TYPE_GEL ///< GEL most suitable for general batteries (see bat_charger.h for other types)
#define BATTERY_NUM_CELLS 6 ///< For lead-acid batteries: 6 for 12V system, 12 for 24V system
#define BATTERY_CAPACITY 40 ///< Cell capacity or sum of parallel cells capacity (Ah)
+18 -2
View File
@@ -225,14 +225,30 @@ const uint16_t pub_emoncms_ids[] = {
0x06, 0xA5, 0xA6 // SOC, SOH, DayCount
};
// stores object-ids of values to be published via CAN
const uint16_t pub_can_ids[] = {
// 0x01, // internal time stamp
0x73, 0x72, 0x71, 0x70, 0x7A, // voltage + current
0x74, 0x76, 0x77, // temperatures
0x04, 0x78, 0x79, // LoadState, ChgState, DCDCState
0xA0, 0xA1, 0xA2, 0xA3, // daily energy throughput
0x0F, 0x10, // SolarMaxDay_W, LoadMaxDay_W
0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, // V, I, T max
0x06, 0xA4, // SOC, coulomb counter
0x7D, 0x7E, 0x7F,
};
ts_pub_channel_t pub_channels[] = {
{ "Serial_1s", pub_serial_ids, sizeof(pub_serial_ids)/sizeof(uint16_t), false },
{ "EmonCMS_10s", pub_emoncms_ids, sizeof(pub_emoncms_ids)/sizeof(uint16_t), false }
{ "Serial_1s", pub_serial_ids, sizeof(pub_serial_ids)/sizeof(uint16_t) },
{ "EmonCMS_10s", pub_emoncms_ids, sizeof(pub_emoncms_ids)/sizeof(uint16_t) },
{ "CAN_1s", pub_can_ids, sizeof(pub_can_ids)/sizeof(uint16_t) },
};
// TODO: find better solution than manual configuration of channel number
volatile const int pub_channel_serial = 0;
volatile const int pub_channel_emoncms = 1;
volatile const int PUB_CHANNEL_CAN = 2;
ThingSet ts(
data_objects, sizeof(data_objects)/sizeof(data_object_t),
+99 -108
View File
@@ -19,23 +19,38 @@
#include "config.h"
#ifdef CAN_ENABLED
#include "pcb.h"
#ifndef HAS_CAN
#error The hardware does not support CAN, please undefine CAN_ENABLED
#endif
#include "mbed.h"
#include "config.h"
#include "output.h"
#include "output_can.h"
#include "can_msg_queue.h"
//#include "can_iso_tp.h"
#include "data_objects.h"
#include "half_bridge.h"
#include "charger.h"
#include "thingset.h"
#include "thingset_can.h"
extern Serial serial;
extern CAN can;
CANMsgQueue can_tx_queue;
CANMsgQueue can_rx_queue;
#ifndef CAN_SPEED
#define CAN_SPEED 250000 // 250 kHz
#endif
#ifndef CAN_NODE_ID
#define CAN_NODE_ID 10
#endif
class ThingSetCAN_Device
{
CAN can;
DigitalOut can_disable;
ThingSetCAN_Device(PinName rd, PinName td, int hz);
};
CAN can(PIN_CAN_RX, PIN_CAN_TX, CAN_SPEED);
DigitalOut can_disable(PIN_CAN_STB);
//----------------------------------------------------------------------------
// preliminary simple CAN functions to send data to the bus for logging
@@ -45,118 +60,90 @@ CANMsgQueue can_rx_queue;
// Protocol details:
// https://github.com/LibreSolar/ThingSet/blob/master/can.md
void can_pub_msg(data_object_t data_obj)
extern ThingSet ts;
ThingSetCAN ts_can(CAN_NODE_ID);
ThingSetCAN::ThingSetCAN(uint8_t can_node_id): node_id(can_node_id)
{
union float2bytes { float f; char b[4]; } f2b; // for conversion of float to single bytes
can_disable = 1; // we disable the transceiver
can.mode(CAN::Normal);
// TXFP: Transmit FIFO priority driven by request order (chronologically)
// NART: No automatic retransmission
CAN1->MCR |= CAN_MCR_TXFP | CAN_MCR_NART;
}
int msg_priority = 6;
void ThingSetCAN::enable()
{
can_disable = 0; // we enable the transceiver
//can.attach(&can_receive);
}
void ThingSetCAN::process_1s()
{
ts_can.pub();
ts_can.process_asap();
}
bool ThingSetCAN::pub_object(const data_object_t& data_obj)
{
CANMessage msg;
msg.format = CANExtended;
msg.type = CANData;
msg.id = msg_priority << 26
| (1U << 24) | (1U << 25) // identify as publication message
| data_obj.id << 8
| can_node_id;
int encode_len = ts.encode_msg_can(data_obj, node_id, msg.id, msg.data);
// first byte: TinyTP-header or data type for single frame message
// currently: no multi-frame and no timestamp
msg.data[0] = 0x00;
int32_t value;
uint32_t value_abs;
// CBOR byte order: most significant byte first
switch (data_obj.type) {
case T_FLOAT32:
msg.len = 5;
msg.data[0] |= TS_T_FLOAT32;
f2b.f = *((float*)data_obj.data);
msg.data[1] = f2b.b[3];
msg.data[2] = f2b.b[2];
msg.data[3] = f2b.b[1];
msg.data[4] = f2b.b[0];
break;
case T_INT32:
if (data_obj.exponent == 0) {
msg.len = 5;
value = *((int*)data_obj.data);
if (value >= 0) {
msg.data[0] |= TS_T_POS_INT32;
value_abs = abs(value);
}
else {
msg.data[0] |= TS_T_NEG_INT32;
value_abs = abs(value - 1); // 0 is always pos integer
}
msg.data[1] = value_abs >> 24;
msg.data[2] = value_abs >> 16;
msg.data[3] = value_abs >> 8;
msg.data[4] = value_abs;
}
else {
msg.len = 8;
msg.data[0] |= TS_T_DECIMAL_FRAC;
msg.data[1] = 0x82; // array of length 2
// exponent in single byte value, valid: -24 ... 23
uint8_t exponent_abs = abs(data_obj.exponent);
if (data_obj.exponent >= 0 && data_obj.exponent <= 23) {
msg.data[2] = exponent_abs;
}
else if (data_obj.exponent < 0 && data_obj.exponent > -24) {
msg.data[2] = (exponent_abs - 1) | 0x20; // negative uint8
}
// value as positive or negative uint32
value = *((int*)data_obj.data);
if (value >= 0) {
msg.data[3] = 0x1a; // positive int32
value_abs = abs(value);
}
else {
msg.data[3] = 0x3a; // negative int32
value_abs = abs(value - 1); // 0 is always pos integer
}
msg.data[4] = value_abs >> 24;
msg.data[5] = value_abs >> 16;
msg.data[6] = value_abs >> 8;
msg.data[7] = value_abs;
}
break;
case T_BOOL:
msg.len = 1;
if (*((bool*)data_obj.data) == true) {
msg.data[0] = TS_T_TRUE; // simple type: true
}
else {
msg.data[0] = TS_T_FALSE; // simple type: false
}
break;
case T_STRING:
break;
if (encode_len >= 0)
{
msg.len = encode_len;
tx_queue.enqueue(msg);
}
can_tx_queue.enqueue(msg);
return (encode_len >= 0);
}
void can_send_data()
/**
* returns number of can objects added to queue
*/
extern const int PUB_CHANNEL_CAN;
int ThingSetCAN::pub()
{
for (unsigned int i = 0; i < dataObjectsCount; ++i) {
if (dataObjects[i].category == TS_C_OUTPUT) {
can_pub_msg(dataObjects[i]);
int retval = 0;
ts_pub_channel_t* can_chan = ts.get_pub_channel(PUB_CHANNEL_CAN);
if (can_chan != NULL)
{
for (unsigned int element = 0; element < can_chan->num; element++)
{
const data_object_t *data_obj = ts.get_data_object(can_chan->object_ids[element]);
if (data_obj != NULL && data_obj->access & TS_ACCESS_READ)
{
if (ts_can.pub_object(*data_obj))
{
retval++;
}
}
}
}
return retval;
}
void can_process_outbox()
void ThingSetCAN::process_asap()
{
process_outbox();
}
void ThingSetCAN::process_outbox()
{
int max_attempts = 15;
while (!can_tx_queue.empty() && max_attempts > 0) {
while (!tx_queue.empty() && max_attempts > 0) {
CANMessage msg;
can_tx_queue.first(msg);
tx_queue.first(msg);
if(can.write(msg)) {
can_tx_queue.dequeue();
tx_queue.dequeue();
}
else {
//serial.printf("Sending CAN message failed, err: %u, MCR: %u, MSR: %u, TSR: %u, id: %u\n", can.tderror(), (uint32_t)CAN1->MCR, (uint32_t)CAN1->MSR, (uint32_t)CAN1->TSR, msg.id);
@@ -165,6 +152,8 @@ void can_process_outbox()
}
}
#if 0
void can_list_object_ids(int category) {
}
@@ -188,7 +177,7 @@ void can_send_object_name(int data_obj_id, uint8_t can_dest_id)
if (arr_id >= 0) {
if (dataObjects[arr_id].access & ACCESS_READ) {
msg.data[2] = T_STRING;
msg.data[2] = TS_T_STRING;
int len = strlen(dataObjects[arr_id].name);
for (int i = 0; i < len && i < (8-3); i++) {
msg.data[i+3] = *(dataObjects[arr_id].name + i);
@@ -206,7 +195,8 @@ void can_send_object_name(int data_obj_id, uint8_t can_dest_id)
can_tx_queue.enqueue(msg);
}
#endif
#if 0
void can_process_inbox()
{
int max_attempts = 15;
@@ -231,12 +221,12 @@ void can_process_inbox()
int value;
switch (function_id) {
case TS_WRITE:
case TS_INPUT:
data_obj_id = msg.data[1] + (msg.data[2] << 8);
value = msg.data[6] + (msg.data[7] << 8);
for (unsigned int i = 0; i < dataObjectsCount; ++i) {
if (dataObjects[i].id == data_obj_id) {
if (dataObjects[i].access & ACCESS_WRITE) {
if (dataObjects[i].access & TS_ACCESS_WRITE) {
// TODO: write data
serial.printf("ThingSet Write: %d to %s (id = %d)\n", value, dataObjects[i].name, data_obj_id);
} else {
@@ -246,7 +236,7 @@ void can_process_inbox()
}
}
break;
case TS_OBJ_NAME:
case TS_NAME:
data_obj_id = msg.data[1] + (msg.data[2] << 8);
can_send_object_name(data_obj_id, can_dest_id);
serial.printf("Get Data Object Name: %d\n", data_obj_id);
@@ -262,7 +252,8 @@ void can_process_inbox()
max_attempts--;
}
}
#endif
#if 0
void can_receive() {
CANMessage msg;
while (can.read(msg)) {
@@ -274,7 +265,7 @@ void can_receive() {
}
}
}
#endif
#endif /* CAN_ENABLED */
#endif /* UNIT_TEST */
+10 -3
View File
@@ -40,6 +40,7 @@
#include "log.h" // log data (error memory, min/max measurements, etc.)
#include "data_objects.h" // for access to internal data via ThingSet
#include "thingset_serial.h" // UART or USB serial communication
#include "thingset_can.h" // CAN bus communication
Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial");
@@ -116,7 +117,7 @@ int main()
#else // MPPT
dcdc_init(&dcdc);
half_bridge_init(PWM_FREQUENCY, 300, 12 / dcdc.hs_voltage_max, 0.97); // lower duty limit might have to be adjusted dynamically depending on LS voltage
#endif
#endif
// Configuration from EEPROM
data_objects_read_eeprom();
@@ -133,9 +134,15 @@ int main()
calibrate_current_sensors();
// Communication interfaces
uart_serial_init(&serial);
uext_init();
#ifdef UART_SERIAL_ENABLED
thingset_serial_init(&serial);
#endif
#ifdef CAN_ENABLED
ts_can.enable();
#endif
uext_init();
init_watchdog(10); // 10s should be enough for communication ports
// Setup of DC/DC power stage
+6
View File
@@ -120,4 +120,10 @@ enum {
ADC_CHSELR_CHSEL17 \
)
// defines for CAN BUS SUPPORT
#define HAS_CAN 1
#define PIN_CAN_RX PB_8
#define PIN_CAN_TX PB_9
#define PIN_CAN_STB PA_15
#endif
+46
View File
@@ -0,0 +1,46 @@
/* LibreSolar charge controller firmware
* Copyright (c) 2016-2019 Martin Jäger (www.libre.solar)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _THINGSET_CAN_H_
#define _THINGSET_CAN_H_
#ifdef CAN_ENABLED
#include "data_objects.h"
#include "can_msg_queue.h"
class ThingSetCAN
{
public:
ThingSetCAN(uint8_t can_node_id);
void process_asap();
void process_1s();
void enable();
private:
bool pub_object(const data_object_t& data_obj);
int pub();
void process_outbox();
CANMsgQueue tx_queue;
// CANMsgQueue rx_queue;
uint8_t node_id;
};
extern ThingSetCAN ts_can;
#endif /* CAN_ENABLED */
#endif
+18 -1
View File
@@ -41,6 +41,11 @@ extern ThingSet ts;
extern ts_pub_channel_t pub_channels[];
extern const int pub_channel_serial;
#ifdef CAN_ENABLED
#include "thingset_can.h"
#endif
void thingset_serial_init(Serial* s)
{
#ifdef UART_SERIAL_ENABLED
@@ -59,8 +64,12 @@ void thingset_serial_process_asap()
#ifdef USB_SERIAL_ENABLED
usb_serial_process();
#endif
#ifdef CAN_ENABLED
ts_can.process_asap();
#endif
}
void thingset_serial_process_1s()
{
#ifdef UART_SERIAL_ENABLED
@@ -70,6 +79,10 @@ void thingset_serial_process_1s()
usb_serial_pub();
#endif
#ifdef CAN_ENABLED
ts_can.process_1s();
#endif
fflush(stdout);
}
@@ -101,12 +114,16 @@ void uart_serial_isr()
}
}
}
#endif
#ifdef UART_SERIAL_ENABLED
void uart_serial_init(Serial* s)
{
ser_uart = s;
s->attach(uart_serial_isr);
}
#endif
#ifdef UART_SERIAL_ENABLED
extern const int PUB_CHANNEL_SERIAL;
void uart_serial_pub()
{