Zephyr: CAN support added

So far, only publishing messages are supported (no RX yet).

Logging is switched on again, but with minimal output only (suitable
for small F0 MCUs). CAN is activated by default for MPPT 2420 LC.
This commit is contained in:
Martin Jaeger
2020-01-30 16:54:58 +01:00
parent e078b87553
commit 1b6a7f82d7
11 changed files with 161 additions and 63 deletions
@@ -40,6 +40,10 @@
gpios = <&gpioc 14 GPIO_DIR_IN>;
label = "UEXT disable";
};
can-dis {
gpios = <&gpioa 15 GPIO_DIR_IN>;
label = "CAN disable / standby";
};
};
aliases {
@@ -76,3 +80,8 @@
&adc1 {
status = "okay";
};
&can1 {
bus-speed = <250000>;
status = "okay";
};
@@ -34,3 +34,7 @@ CONFIG_CLOCK_STM32_PLL_SRC_HSE=y
CONFIG_CLOCK_STM32_PLL_MULTIPLIER=6
CONFIG_CLOCK_STM32_AHB_PRESCALER=1
CONFIG_CLOCK_STM32_APB1_PRESCALER=1
CONFIG_CAN=y
CONFIG_CAN_1=y
CONFIG_EXT_THINGSET_CAN=y
+4
View File
@@ -46,6 +46,10 @@ static const struct pin_config pinconf[] = {
#ifdef CONFIG_DAC_1
{STM32_PIN_PA4, STM32F0_PINMUX_FUNC_PA4_DAC_OUT1},
#endif /* CONFIG_ADC_1 */
#ifdef CONFIG_CAN_1
{STM32_PIN_PB8, STM32F0_PINMUX_FUNC_PB8_CAN_RX},
{STM32_PIN_PB9, STM32F0_PINMUX_FUNC_PB9_CAN_TX},
#endif /* CONFIG_CAN_1 */
};
static int pinmux_stm32_init(struct device *port)
+1 -1
View File
@@ -53,7 +53,7 @@
#define CONFIG_EXT_THINGSET_SERIAL 1
// CAN bus drivers (not supported by all charge controllers)
//#define CAN_ENABLED
#define CONFIG_EXT_THINGSET_CAN 0
// LoRa board RFM9x connected to UEXT port
// https://github.com/LibreSolar/UEXT_LoRa
+107 -32
View File
@@ -4,23 +4,31 @@
* SPDX-License-Identifier: Apache-2.0
*/
#ifdef __MBED__
#ifndef UNIT_TEST
#include "config.h"
#ifdef CAN_ENABLED
#if CONFIG_EXT_THINGSET_CAN
#include "ext.h"
#if defined(__ZEPHYR__)
#include <zephyr.h>
#include <device.h>
#include <drivers/gpio.h>
#include <drivers/can.h>
#include <stm32f0xx_ll_system.h> // needed for debug printing of register contents
#elif defined(__MBED__)
#include "mbed.h"
#endif
#include "pcb.h"
#include "thingset.h"
#include "data_objects.h"
#include "can_msg_queue.h"
#ifndef HAS_CAN
#error The hardware does not support CAN, please undefine CAN_ENABLED
#ifndef CONFIG_CAN
#error The hardware does not support CAN, please disable CONFIG_EXT_THINGSET_CAN
#endif
#ifndef CAN_SPEED
@@ -38,41 +46,51 @@ public:
void enable();
private:
/**
* Generate CAN frame for data object and put it into TX queue
*/
bool pub_object(const data_object_t& data_obj);
int pub();
/**
* 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();
#if defined(CAN_RECEIVE)
#if defined(CAN_RECEIVE) && defined(__MBED__)
void process_inbox();
void process_input();
void send_object_name(int data_obj_id, uint8_t can_dest_id);
CANMsgQueue rx_queue;
CanMsgQueue rx_queue;
#endif
CANMsgQueue tx_queue;
CanMsgQueue tx_queue;
uint8_t node_id;
const unsigned int channel;
#ifdef __MBED__
CAN can;
DigitalOut can_disable;
#elif defined(__ZEPHYR__)
struct device *can_dev;
struct device *gpio_stb_dev;
#endif
};
extern const unsigned int PUB_CHANNEL_CAN;
#ifndef CAN_NODE_ID
#define CAN_NODE_ID 10
#define CAN_NODE_ID 10
#endif
ThingSetCAN ts_can(CAN_NODE_ID, PUB_CHANNEL_CAN);
class ThingSetCAN_Device
{
CAN can;
DigitalOut can_disable;
ThingSetCAN_Device(PinName rd, PinName td, int hz);
};
//----------------------------------------------------------------------------
// preliminary simple CAN functions to send data to the bus for logging
// Data format based on CBOR specification (except for first byte, which uses
@@ -83,6 +101,8 @@ class ThingSetCAN_Device
extern ThingSet ts;
#ifdef __MBED__
ThingSetCAN::ThingSetCAN(uint8_t can_node_id, const unsigned int c):
node_id(can_node_id),
channel(c),
@@ -104,12 +124,35 @@ void ThingSetCAN::enable()
#endif
}
#elif defined(__ZEPHYR__)
ThingSetCAN::ThingSetCAN(uint8_t can_node_id, const unsigned int c):
node_id(can_node_id),
channel(c)
{
gpio_stb_dev = device_get_binding(DT_SWITCH_CAN_DIS_GPIOS_CONTROLLER);
gpio_pin_configure(gpio_stb_dev, DT_SWITCH_CAN_DIS_GPIOS_PIN, GPIO_DIR_OUT);
gpio_pin_write(gpio_stb_dev, DT_SWITCH_CAN_DIS_GPIOS_PIN, 1);
can_dev = device_get_binding("CAN_1");
}
void ThingSetCAN::enable()
{
// enable the transceiver (set STB pin low)
gpio_pin_write(gpio_stb_dev, DT_SWITCH_CAN_DIS_GPIOS_PIN, 0);
}
#endif
void ThingSetCAN::process_1s()
{
pub();
process_asap();
}
#ifdef __MBED__
bool ThingSetCAN::pub_object(const data_object_t& data_obj)
{
CANMessage msg;
@@ -126,10 +169,32 @@ bool ThingSetCAN::pub_object(const data_object_t& data_obj)
return (encode_len >= 0);
}
#elif defined(__ZEPHYR__)
bool ThingSetCAN::pub_object(const data_object_t& data_obj)
{
unsigned int can_id;
uint8_t data[8];
struct zcan_frame frame = {
.id_type = CAN_EXTENDED_IDENTIFIER,
.rtr = CAN_DATAFRAME
};
int encode_len = ts.encode_msg_can(data_obj, node_id, can_id, data);
frame.ext_id = can_id;
memcpy(frame.data, data, 8);
if (encode_len >= 0)
{
frame.dlc = encode_len;
tx_queue.enqueue(frame);
}
return (encode_len >= 0);
}
#endif
/**
* returns number of can objects added to queue
*/
int ThingSetCAN::pub()
{
int retval = 0;
@@ -155,34 +220,43 @@ int ThingSetCAN::pub()
void ThingSetCAN::process_asap()
{
process_outbox();
#if defined(CAN_RECEIVE)
#if defined(CAN_RECEIVE)
process_inbox();
#endif
#endif
}
void ThingSetCAN::process_outbox()
{
int max_attempts = 15;
while (!tx_queue.empty() && max_attempts > 0) {
CANMessage msg;
CanFrame msg;
tx_queue.first(msg);
if(can.write(msg)) {
#ifdef __MBED__
if (can.write(msg)) {
#elif defined(__ZEPHYR__)
if (can_send(can_dev, &msg, K_MSEC(100), NULL, NULL) == CAN_TX_OK) {
#endif
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);
//printk("Sending CAN message failed, MCR: %x, MSR: %x, TSR: %x\n", (uint32_t)CAN->MCR, (uint32_t)CAN->MSR, (uint32_t)CAN->TSR);
//printf("Sending CAN message failed, MCR: %x, MSR: %x, TSR: %x\n", (uint32_t)CAN1->MCR, (uint32_t)CAN1->MSR, (uint32_t)CAN1->TSR);
}
max_attempts--;
}
}
#if defined(CAN_RECEIVE)
/**
* CAN receive currently only working with mbed
*/
#if defined(CAN_RECEIVE) && defined(__MBED__)
// TODO: Move encoding to ThingSet class
void ThingSetCAN::send_object_name(int data_obj_id, uint8_t can_dest_id)
{
uint8_t msg_priority = 7; // low priority service message
uint8_t function_id = 0x84;
CANMessage msg;
CanFrame msg;
msg.format = CANExtended;
msg.type = CANData;
msg.id = msg_priority << 26 | function_id << 16 |(can_dest_id << 8)| node_id; // TODO: add destination node ID
@@ -214,7 +288,7 @@ void ThingSetCAN::process_inbox()
{
int max_attempts = 15;
while (!rx_queue.empty() && max_attempts >0) {
CANMessage msg;
CanFrame msg;
rx_queue.dequeue(msg);
if (!(msg.id & (0x1U<<25))) {
@@ -278,7 +352,7 @@ void ThingSetCAN::process_inbox()
void ThingSetCAN::process_input()
{
CANMessage msg;
CanFrame msg;
while (can.read(msg)) {
if (!rx_queue.full()) {
rx_queue.enqueue(msg);
@@ -288,8 +362,9 @@ void ThingSetCAN::process_input()
}
}
}
#endif /* CAN_RECEIVE */
#endif /* CAN_ENABLED */
#endif /* CONFIG_EXT_THINGSET_CAN */
#endif /* __MBED__ */
#endif /* UNIT_TEST */
+16 -16
View File
@@ -4,33 +4,33 @@
* SPDX-License-Identifier: Apache-2.0
*/
#if defined(__MBED__) && !defined(UNIT_TEST)
#ifndef UNIT_TEST
#include "config.h"
#if CONFIG_EXT_THINGSET_CAN
#include "ext/can_msg_queue.h"
#include "mbed.h"
#ifdef STM32F0 // STM32L0 does not have CAN
bool CANMsgQueue::full()
bool CanMsgQueue::full()
{
return _length == CAN_QUEUE_SIZE;
}
bool CANMsgQueue::empty()
bool CanMsgQueue::empty()
{
return _length == 0;
}
void CANMsgQueue::enqueue(CANMessage msg)
void CanMsgQueue::enqueue(CanFrame frame)
{
if (!full()) {
_queue[(_first + _length) % CAN_QUEUE_SIZE] = msg;
_queue[(_first + _length) % CAN_QUEUE_SIZE] = frame;
_length++;
}
}
int CANMsgQueue::dequeue()
int CanMsgQueue::dequeue()
{
if (!empty()) {
_first = (_first + 1) % CAN_QUEUE_SIZE;
@@ -42,10 +42,10 @@ int CANMsgQueue::dequeue()
}
}
int CANMsgQueue::dequeue(CANMessage& msg)
int CanMsgQueue::dequeue(CanFrame &frame)
{
if (!empty()) {
msg = _queue[_first];
frame = _queue[_first];
_first = (_first + 1) % CAN_QUEUE_SIZE;
_length--;
return 1;
@@ -55,10 +55,10 @@ int CANMsgQueue::dequeue(CANMessage& msg)
}
}
int CANMsgQueue::first(CANMessage& msg)
int CanMsgQueue::first(CanFrame &frame)
{
if (!empty()) {
msg = _queue[_first];
frame = _queue[_first];
return 1;
}
else {
@@ -66,6 +66,6 @@ int CANMsgQueue::first(CANMessage& msg)
}
}
#endif /* STM32F0 */
#endif /* CONFIG_EXT_THINGSET_CAN */
#endif
#endif /* UNIT_TEST */
+12 -9
View File
@@ -7,28 +7,31 @@
#ifndef CAN_MSG_QUEUE_H
#define CAN_MSG_QUEUE_H
#ifdef __MBED__
#include "mbed.h"
#ifdef STM32F0 // STM32L0 does not have CAN
#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
class CanMsgQueue
{
public:
bool full();
bool empty();
void enqueue(CANMessage msg);
int dequeue(CANMessage& msg);
void enqueue(CanFrame frame);
int dequeue(CanFrame &frame);
int dequeue();
int first(CANMessage& msg);
int first(CanFrame& frame);
private:
CANMessage _queue[CAN_QUEUE_SIZE];
CanFrame _queue[CAN_QUEUE_SIZE];
int _first;
int _length;
};
#endif /* STM32F0 */
#endif /* CAN_MSG_QUEUE_H */
+1 -1
View File
@@ -63,7 +63,7 @@
#define PIN_EEPROM_SDA PB_11
#define EEPROM_24AA32
#define HAS_CAN 1
#define CONFIG_CAN 1
enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT };
+2
View File
@@ -31,6 +31,8 @@ target_sources(app PRIVATE ../src/pwm_switch.cpp)
target_sources(app PRIVATE ../src/ext/ext.cpp)
target_sources(app PRIVATE ../src/ext/oled.cpp)
target_sources(app PRIVATE ../src/ext/serial.cpp)
target_sources(app PRIVATE ../src/ext/can.cpp)
target_sources(app PRIVATE ../src/ext/can_msg_queue.cpp)
add_subdirectory(../lib/thingset build/thingset)
add_subdirectory(../lib/oled-display build/oled-display)
+4 -3
View File
@@ -1,8 +1,8 @@
# direct printf calls to console
CONFIG_STDOUT_CONSOLE=y
# default is 1024, but we need just a few bytes for ExtManager
CONFIG_HEAP_MEM_POOL_SIZE=256
# default is 1024
CONFIG_HEAP_MEM_POOL_SIZE=1024
CONFIG_GPIO=y
CONFIG_SERIAL=y
@@ -13,7 +13,8 @@ CONFIG_ADC=y
CONFIG_ADC_1=y
# disable log by default to save memory for STM32F0 with little RAM
CONFIG_LOG=n
CONFIG_LOG=y
CONFIG_LOG_MINIMAL=y
#CONFIG_NEWLIB_LIBC_NANO=y
CONFIG_NEWLIB_LIBC=y