Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af17448539 | ||
|
|
7643ae0a56 | ||
|
|
9b5f2bd70a | ||
|
|
e2410cd3d0 | ||
|
|
cedfa1a95f |
No files matched your search
+20
-3
@@ -18,6 +18,8 @@ source "src/stm32/Kconfig"
|
|||||||
source "src/lpc176x/Kconfig"
|
source "src/lpc176x/Kconfig"
|
||||||
|
|
||||||
# Generic configuration options for serial ports
|
# Generic configuration options for serial ports
|
||||||
|
config SERIAL
|
||||||
|
bool
|
||||||
config SERIAL_BAUD
|
config SERIAL_BAUD
|
||||||
depends on SERIAL
|
depends on SERIAL
|
||||||
int "Baud rate for serial port" if LOW_LEVEL_OPTIONS
|
int "Baud rate for serial port" if LOW_LEVEL_OPTIONS
|
||||||
@@ -27,12 +29,14 @@ config SERIAL_BAUD
|
|||||||
to 250000. Read the FAQ before changing this value.
|
to 250000. Read the FAQ before changing this value.
|
||||||
|
|
||||||
# Generic configuration options for USB
|
# Generic configuration options for USB
|
||||||
|
config USBSERIAL
|
||||||
|
bool
|
||||||
config USB_VENDOR_ID
|
config USB_VENDOR_ID
|
||||||
default 0x1d50
|
default 0x1d50
|
||||||
config USB_DEVICE_ID
|
config USB_DEVICE_ID
|
||||||
default 0x6177
|
default 0x6177
|
||||||
config USB_SERIAL_NUMBER_CHIPID
|
config USB_SERIAL_NUMBER_CHIPID
|
||||||
depends on HAVE_CHIPID
|
depends on HAVE_CHIPID && USBSERIAL
|
||||||
default y
|
default y
|
||||||
config USB_SERIAL_NUMBER
|
config USB_SERIAL_NUMBER
|
||||||
default "12345"
|
default "12345"
|
||||||
@@ -40,15 +44,28 @@ config USB_SERIAL_NUMBER
|
|||||||
menu "USB ids"
|
menu "USB ids"
|
||||||
depends on USBSERIAL && LOW_LEVEL_OPTIONS
|
depends on USBSERIAL && LOW_LEVEL_OPTIONS
|
||||||
config USB_VENDOR_ID
|
config USB_VENDOR_ID
|
||||||
hex "USB vendor ID"
|
hex "USB vendor ID" if USBSERIAL
|
||||||
config USB_DEVICE_ID
|
config USB_DEVICE_ID
|
||||||
hex "USB device ID"
|
hex "USB device ID" if USBSERIAL
|
||||||
config USB_SERIAL_NUMBER_CHIPID
|
config USB_SERIAL_NUMBER_CHIPID
|
||||||
bool "USB serial number from CHIPID" if HAVE_CHIPID
|
bool "USB serial number from CHIPID" if HAVE_CHIPID
|
||||||
config USB_SERIAL_NUMBER
|
config USB_SERIAL_NUMBER
|
||||||
string "USB serial number" if !USB_SERIAL_NUMBER_CHIPID
|
string "USB serial number" if !USB_SERIAL_NUMBER_CHIPID
|
||||||
endmenu
|
endmenu
|
||||||
|
|
||||||
|
# Generic configuration options for CANbus
|
||||||
|
config CANSERIAL
|
||||||
|
bool
|
||||||
|
config CANBUS
|
||||||
|
bool
|
||||||
|
default y if CANSERIAL
|
||||||
|
config CANBUS_FREQUENCY
|
||||||
|
int "CAN bus speed" if CANBUS
|
||||||
|
default 500000
|
||||||
|
config CANBUS_FILTER
|
||||||
|
bool
|
||||||
|
default y if CANSERIAL
|
||||||
|
|
||||||
config INITIAL_PINS
|
config INITIAL_PINS
|
||||||
string "GPIO pins to set on bootloader entry"
|
string "GPIO pins to set on bootloader entry"
|
||||||
depends on LOW_LEVEL_OPTIONS
|
depends on LOW_LEVEL_OPTIONS
|
||||||
|
|||||||
+17
-276
@@ -1,291 +1,32 @@
|
|||||||
// Generic handling of serial over CAN support
|
// Wrapper functions connecting canserial.c to low-level can hardware
|
||||||
//
|
//
|
||||||
// Copyright (C) 2019 Eug Krashtan <eug.krashtan@gmail.com>
|
// Copyright (C) 2022 Kevin O'Connor <kevin@koconnor.net>
|
||||||
// Copyright (C) 2020 Pontus Borg <glpontus@gmail.com>
|
|
||||||
// Copyright (C) 2021 Kevin O'Connor <kevin@koconnor.net>
|
|
||||||
//
|
//
|
||||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||||
|
|
||||||
#include <string.h> // memcpy
|
#include "canbus.h" // canbus_send
|
||||||
#include "canbus.h" // canbus_set_uuid
|
#include "canserial.h" // canserial_send
|
||||||
#include "command.h" // DECL_CONSTANT
|
|
||||||
#include "generic/io.h" // readb
|
|
||||||
#include "generic/irq.h" // irq_disable
|
|
||||||
#include "sched.h" // sched_wake_task
|
|
||||||
|
|
||||||
static uint32_t canbus_assigned_id;
|
int
|
||||||
static uint8_t canbus_uuid[CANBUS_UUID_LEN];
|
canserial_send(struct canbus_msg *msg)
|
||||||
|
{
|
||||||
|
return canbus_send(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
/****************************************************************
|
canserial_set_filter(uint32_t id)
|
||||||
* Data transmission over CAN
|
{
|
||||||
****************************************************************/
|
canbus_set_filter(id);
|
||||||
|
}
|
||||||
static struct task_wake canbus_tx_wake;
|
|
||||||
static uint8_t transmit_buf[96], transmit_pos, transmit_max;
|
|
||||||
|
|
||||||
void
|
void
|
||||||
canbus_notify_tx(void)
|
canbus_notify_tx(void)
|
||||||
{
|
{
|
||||||
sched_wake_task(&canbus_tx_wake);
|
canserial_notify_tx();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
canbus_tx_task(void)
|
canbus_process_data(struct canbus_msg *msg)
|
||||||
{
|
{
|
||||||
if (!sched_check_wake(&canbus_tx_wake))
|
canserial_process_data(msg);
|
||||||
return;
|
|
||||||
uint32_t id = canbus_assigned_id;
|
|
||||||
if (!id) {
|
|
||||||
transmit_pos = transmit_max = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
uint32_t tpos = transmit_pos, tmax = transmit_max;
|
|
||||||
for (;;) {
|
|
||||||
int avail = tmax - tpos, now = avail > 8 ? 8 : avail;
|
|
||||||
if (avail <= 0)
|
|
||||||
break;
|
|
||||||
int ret = canbus_send(id + 1, now, &transmit_buf[tpos]);
|
|
||||||
if (ret <= 0)
|
|
||||||
break;
|
|
||||||
tpos += now;
|
|
||||||
}
|
|
||||||
transmit_pos = tpos;
|
|
||||||
}
|
}
|
||||||
DECL_TASK(canbus_tx_task);
|
|
||||||
|
|
||||||
// Encode and transmit a "response" message
|
|
||||||
void
|
|
||||||
console_sendf(const struct command_encoder *ce, va_list args)
|
|
||||||
{
|
|
||||||
// Verify space for message
|
|
||||||
uint32_t tpos = transmit_pos, tmax = transmit_max;
|
|
||||||
if (tpos >= tmax)
|
|
||||||
transmit_pos = transmit_max = tpos = tmax = 0;
|
|
||||||
uint32_t max_size = ce->max_size;
|
|
||||||
if (tmax + max_size > sizeof(transmit_buf)) {
|
|
||||||
if (tmax + max_size - tpos > sizeof(transmit_buf))
|
|
||||||
// Not enough space for message
|
|
||||||
return;
|
|
||||||
// Move buffer
|
|
||||||
tmax -= tpos;
|
|
||||||
memmove(&transmit_buf[0], &transmit_buf[tpos], tmax);
|
|
||||||
transmit_pos = tpos = 0;
|
|
||||||
transmit_max = tmax;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate message
|
|
||||||
uint32_t msglen = command_encode_and_frame(&transmit_buf[tmax], ce, args);
|
|
||||||
|
|
||||||
// Start message transmit
|
|
||||||
transmit_max = tmax + msglen;
|
|
||||||
canbus_notify_tx();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/****************************************************************
|
|
||||||
* CAN "admin" command handling
|
|
||||||
****************************************************************/
|
|
||||||
|
|
||||||
// Available commands and responses
|
|
||||||
#define CANBUS_CMD_QUERY_UNASSIGNED 0x00
|
|
||||||
#define CANBUS_CMD_SET_CANBOOT_NODEID 0x11
|
|
||||||
#define CANBUS_CMD_CLEAR_CANBOOT_NODEID 0x12
|
|
||||||
#define CANBUS_RESP_NEED_NODEID 0x20
|
|
||||||
|
|
||||||
// Helper to verify a UUID in a command matches this chip's UUID
|
|
||||||
static int
|
|
||||||
can_check_uuid(uint32_t id, uint32_t len, uint8_t *data)
|
|
||||||
{
|
|
||||||
return len >= 7 && memcmp(&data[1], canbus_uuid, sizeof(canbus_uuid)) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint32_t
|
|
||||||
can_decode_nodeid(int nodeid)
|
|
||||||
{
|
|
||||||
return (nodeid << 1) + 0x100;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
can_process_query_unassigned(uint32_t id, uint32_t len, uint8_t *data)
|
|
||||||
{
|
|
||||||
if (canbus_assigned_id)
|
|
||||||
return;
|
|
||||||
uint8_t send[8];
|
|
||||||
send[0] = CANBUS_RESP_NEED_NODEID;
|
|
||||||
memcpy(&send[1], canbus_uuid, sizeof(canbus_uuid));
|
|
||||||
send[7] = CANBUS_CMD_SET_CANBOOT_NODEID;
|
|
||||||
// Send with retry
|
|
||||||
for (;;) {
|
|
||||||
int ret = canbus_send(CANBUS_ID_ADMIN_RESP, 8, send);
|
|
||||||
if (ret >= 0)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
can_process_clear_canboot_nodeid(void)
|
|
||||||
{
|
|
||||||
canbus_assigned_id = 0;
|
|
||||||
canbus_set_filter(canbus_assigned_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
can_id_conflict(void)
|
|
||||||
{
|
|
||||||
canbus_assigned_id = 0;
|
|
||||||
canbus_set_filter(canbus_assigned_id);
|
|
||||||
// TODO: We should likely do something here, such as report back
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
can_process_set_canboot_nodeid(uint32_t id, uint32_t len, uint8_t *data)
|
|
||||||
{
|
|
||||||
if (len < 8)
|
|
||||||
return;
|
|
||||||
uint32_t newid = can_decode_nodeid(data[7]);
|
|
||||||
if (can_check_uuid(id, len, data)) {
|
|
||||||
if (newid != canbus_assigned_id) {
|
|
||||||
canbus_assigned_id = newid;
|
|
||||||
canbus_set_filter(canbus_assigned_id);
|
|
||||||
}
|
|
||||||
} else if (newid == canbus_assigned_id) {
|
|
||||||
can_id_conflict();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle an "admin" command
|
|
||||||
static void
|
|
||||||
can_process(uint32_t id, uint32_t len, uint8_t *data)
|
|
||||||
{
|
|
||||||
if (!len)
|
|
||||||
return;
|
|
||||||
switch (data[0]) {
|
|
||||||
case CANBUS_CMD_QUERY_UNASSIGNED:
|
|
||||||
can_process_query_unassigned(id, len, data);
|
|
||||||
break;
|
|
||||||
case CANBUS_CMD_SET_CANBOOT_NODEID:
|
|
||||||
can_process_set_canboot_nodeid(id, len, data);
|
|
||||||
break;
|
|
||||||
case CANBUS_CMD_CLEAR_CANBOOT_NODEID:
|
|
||||||
can_process_clear_canboot_nodeid();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/****************************************************************
|
|
||||||
* CAN packet reading
|
|
||||||
****************************************************************/
|
|
||||||
|
|
||||||
static struct task_wake canbus_rx_wake;
|
|
||||||
|
|
||||||
void
|
|
||||||
canbus_notify_rx(void)
|
|
||||||
{
|
|
||||||
sched_wake_task(&canbus_rx_wake);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint8_t receive_buf[192], receive_pos;
|
|
||||||
DECL_CONSTANT("RECEIVE_WINDOW", ARRAY_SIZE(receive_buf));
|
|
||||||
|
|
||||||
// Handle incoming data (called from IRQ handler)
|
|
||||||
void
|
|
||||||
canbus_process_data(uint32_t id, uint32_t len, uint8_t *data)
|
|
||||||
{
|
|
||||||
if (!id || id != canbus_assigned_id)
|
|
||||||
return;
|
|
||||||
int rpos = receive_pos;
|
|
||||||
if (len > sizeof(receive_buf) - rpos)
|
|
||||||
len = sizeof(receive_buf) - rpos;
|
|
||||||
memcpy(&receive_buf[rpos], data, len);
|
|
||||||
receive_pos = rpos + len;
|
|
||||||
canbus_notify_rx();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove from the receive buffer the given number of bytes
|
|
||||||
static void
|
|
||||||
console_pop_input(int len)
|
|
||||||
{
|
|
||||||
int copied = 0;
|
|
||||||
for (;;) {
|
|
||||||
int rpos = readb(&receive_pos);
|
|
||||||
int needcopy = rpos - len;
|
|
||||||
if (needcopy) {
|
|
||||||
memmove(&receive_buf[copied], &receive_buf[copied + len]
|
|
||||||
, needcopy - copied);
|
|
||||||
copied = needcopy;
|
|
||||||
canbus_notify_rx();
|
|
||||||
}
|
|
||||||
irqstatus_t flag = irq_save();
|
|
||||||
if (rpos != readb(&receive_pos)) {
|
|
||||||
// Raced with irq handler - retry
|
|
||||||
irq_restore(flag);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
receive_pos = needcopy;
|
|
||||||
irq_restore(flag);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Task to process incoming commands and admin messages
|
|
||||||
void
|
|
||||||
canbus_rx_task(void)
|
|
||||||
{
|
|
||||||
if (!sched_check_wake(&canbus_rx_wake))
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Read any pending CAN packets
|
|
||||||
for (;;) {
|
|
||||||
uint8_t data[8];
|
|
||||||
uint32_t id;
|
|
||||||
int ret = canbus_read(&id, data);
|
|
||||||
if (ret < 0)
|
|
||||||
break;
|
|
||||||
if (id && id == canbus_assigned_id + 1)
|
|
||||||
can_id_conflict();
|
|
||||||
else if (id == CANBUS_ID_ADMIN)
|
|
||||||
can_process(id, ret, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check for a complete message block and process it
|
|
||||||
uint_fast8_t rpos = readb(&receive_pos), pop_count;
|
|
||||||
int ret = command_find_block(receive_buf, rpos, &pop_count);
|
|
||||||
if (ret > 0)
|
|
||||||
command_dispatch(receive_buf, pop_count);
|
|
||||||
if (ret) {
|
|
||||||
console_pop_input(pop_count);
|
|
||||||
if (ret > 0)
|
|
||||||
command_send_ack();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
DECL_TASK(canbus_rx_task);
|
|
||||||
|
|
||||||
|
|
||||||
/****************************************************************
|
|
||||||
* Setup and shutdown
|
|
||||||
****************************************************************/
|
|
||||||
|
|
||||||
void
|
|
||||||
command_get_canbus_id(uint32_t *data)
|
|
||||||
{
|
|
||||||
uint32_t out[5] = {};
|
|
||||||
memcpy(&out[2], canbus_uuid, 6);
|
|
||||||
command_respond_ack(CMD_GET_CANBUS_ID, out, ARRAY_SIZE(out));
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
canbus_set_uuid(void *uuid)
|
|
||||||
{
|
|
||||||
memcpy(canbus_uuid, uuid, sizeof(canbus_uuid));
|
|
||||||
canbus_notify_rx();
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
canbus_shutdown(void)
|
|
||||||
{
|
|
||||||
canbus_notify_tx();
|
|
||||||
canbus_notify_rx();
|
|
||||||
}
|
|
||||||
DECL_SHUTDOWN(canbus_shutdown);
|
|
||||||
+15
-8
@@ -3,19 +3,26 @@
|
|||||||
|
|
||||||
#include <stdint.h> // uint32_t
|
#include <stdint.h> // uint32_t
|
||||||
|
|
||||||
#define CANBUS_ID_ADMIN 0x3f0
|
struct canbus_msg {
|
||||||
#define CANBUS_ID_ADMIN_RESP 0x3f1
|
uint32_t id;
|
||||||
#define CANBUS_UUID_LEN 6
|
uint32_t dlc;
|
||||||
|
union {
|
||||||
|
uint8_t data[8];
|
||||||
|
uint32_t data32[2];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
#define CANMSG_ID_RTR (1<<30)
|
||||||
|
#define CANMSG_ID_EFF (1<<31)
|
||||||
|
|
||||||
|
#define CANMSG_DATA_LEN(msg) ((msg)->dlc > 8 ? 8 : (msg)->dlc)
|
||||||
|
|
||||||
// callbacks provided by board specific code
|
// callbacks provided by board specific code
|
||||||
int canbus_read(uint32_t *id, uint8_t *data);
|
int canbus_send(struct canbus_msg *msg);
|
||||||
int canbus_send(uint32_t id, uint32_t len, uint8_t *data);
|
|
||||||
void canbus_set_filter(uint32_t id);
|
void canbus_set_filter(uint32_t id);
|
||||||
|
|
||||||
// canbus.c
|
// canbus.c
|
||||||
void canbus_notify_tx(void);
|
void canbus_notify_tx(void);
|
||||||
void canbus_notify_rx(void);
|
void canbus_process_data(struct canbus_msg *msg);
|
||||||
void canbus_process_data(uint32_t id, uint32_t len, uint8_t *data);
|
|
||||||
void canbus_set_uuid(void *data);
|
|
||||||
|
|
||||||
#endif // canbus.h
|
#endif // canbus.h
|
||||||
@@ -0,0 +1,342 @@
|
|||||||
|
// Generic handling of serial over CAN support
|
||||||
|
//
|
||||||
|
// Copyright (C) 2019 Eug Krashtan <eug.krashtan@gmail.com>
|
||||||
|
// Copyright (C) 2020 Pontus Borg <glpontus@gmail.com>
|
||||||
|
// Copyright (C) 2021 Kevin O'Connor <kevin@koconnor.net>
|
||||||
|
//
|
||||||
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||||
|
|
||||||
|
#include <string.h> // memcpy
|
||||||
|
#include "board/io.h" // readb
|
||||||
|
#include "board/irq.h" // irq_save
|
||||||
|
#include "board/misc.h" // console_sendf
|
||||||
|
#include "canbus.h" // canbus_set_uuid
|
||||||
|
#include "canserial.h" // canserial_notify_tx
|
||||||
|
#include "command.h" // DECL_CONSTANT
|
||||||
|
#include "fasthash.h" // fasthash64
|
||||||
|
#include "sched.h" // sched_wake_task
|
||||||
|
|
||||||
|
#define CANBUS_UUID_LEN 6
|
||||||
|
|
||||||
|
// Global storage
|
||||||
|
static struct canbus_data {
|
||||||
|
uint32_t assigned_id;
|
||||||
|
uint8_t uuid[CANBUS_UUID_LEN];
|
||||||
|
|
||||||
|
// Tx data
|
||||||
|
struct task_wake tx_wake;
|
||||||
|
uint8_t transmit_pos, transmit_max;
|
||||||
|
|
||||||
|
// Rx data
|
||||||
|
struct task_wake rx_wake;
|
||||||
|
uint8_t receive_pos;
|
||||||
|
uint32_t admin_pull_pos, admin_push_pos;
|
||||||
|
|
||||||
|
// Transfer buffers
|
||||||
|
struct canbus_msg admin_queue[8];
|
||||||
|
uint8_t transmit_buf[96];
|
||||||
|
uint8_t receive_buf[192];
|
||||||
|
} CanData;
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************
|
||||||
|
* Data transmission over CAN
|
||||||
|
****************************************************************/
|
||||||
|
|
||||||
|
void
|
||||||
|
canserial_notify_tx(void)
|
||||||
|
{
|
||||||
|
sched_wake_task(&CanData.tx_wake);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
canserial_tx_task(void)
|
||||||
|
{
|
||||||
|
if (!sched_check_wake(&CanData.tx_wake))
|
||||||
|
return;
|
||||||
|
uint32_t id = CanData.assigned_id;
|
||||||
|
if (!id) {
|
||||||
|
CanData.transmit_pos = CanData.transmit_max = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
struct canbus_msg msg;
|
||||||
|
msg.id = id + 1;
|
||||||
|
uint32_t tpos = CanData.transmit_pos, tmax = CanData.transmit_max;
|
||||||
|
for (;;) {
|
||||||
|
int avail = tmax - tpos, now = avail > 8 ? 8 : avail;
|
||||||
|
if (avail <= 0)
|
||||||
|
break;
|
||||||
|
msg.dlc = now;
|
||||||
|
memcpy(msg.data, &CanData.transmit_buf[tpos], now);
|
||||||
|
int ret = canserial_send(&msg);
|
||||||
|
if (ret <= 0)
|
||||||
|
break;
|
||||||
|
tpos += now;
|
||||||
|
}
|
||||||
|
CanData.transmit_pos = tpos;
|
||||||
|
}
|
||||||
|
DECL_TASK(canserial_tx_task);
|
||||||
|
|
||||||
|
// Encode and transmit a "response" message
|
||||||
|
void
|
||||||
|
console_sendf(const struct command_encoder *ce, va_list args)
|
||||||
|
{
|
||||||
|
// Verify space for message
|
||||||
|
uint32_t tpos = CanData.transmit_pos, tmax = CanData.transmit_max;
|
||||||
|
if (tpos >= tmax)
|
||||||
|
CanData.transmit_pos = CanData.transmit_max = tpos = tmax = 0;
|
||||||
|
uint32_t max_size = ce->max_size;
|
||||||
|
if (tmax + max_size > sizeof(CanData.transmit_buf)) {
|
||||||
|
if (tmax + max_size - tpos > sizeof(CanData.transmit_buf))
|
||||||
|
// Not enough space for message
|
||||||
|
return;
|
||||||
|
// Move buffer
|
||||||
|
tmax -= tpos;
|
||||||
|
memmove(&CanData.transmit_buf[0], &CanData.transmit_buf[tpos], tmax);
|
||||||
|
CanData.transmit_pos = tpos = 0;
|
||||||
|
CanData.transmit_max = tmax;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate message
|
||||||
|
uint32_t msglen = command_encode_and_frame(&CanData.transmit_buf[tmax]
|
||||||
|
, ce, args);
|
||||||
|
|
||||||
|
// Start message transmit
|
||||||
|
CanData.transmit_max = tmax + msglen;
|
||||||
|
canserial_notify_tx();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************
|
||||||
|
* CAN "admin" command handling
|
||||||
|
****************************************************************/
|
||||||
|
|
||||||
|
// Available commands and responses
|
||||||
|
#define CANBUS_CMD_QUERY_UNASSIGNED 0x00
|
||||||
|
#define CANBUS_CMD_SET_CANBOOT_NODEID 0x11
|
||||||
|
#define CANBUS_CMD_CLEAR_CANBOOT_NODEID 0x12
|
||||||
|
#define CANBUS_RESP_NEED_NODEID 0x20
|
||||||
|
|
||||||
|
// Helper to verify a UUID in a command matches this chip's UUID
|
||||||
|
static int
|
||||||
|
can_check_uuid(struct canbus_msg *msg)
|
||||||
|
{
|
||||||
|
return (msg->dlc >= 7
|
||||||
|
&& memcmp(&msg->data[1], CanData.uuid, sizeof(CanData.uuid)) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helpers to encode/decode a CAN identifier to a 1-byte "nodeid"
|
||||||
|
/*static int
|
||||||
|
can_get_nodeid(void)
|
||||||
|
{
|
||||||
|
if (!CanData.assigned_id)
|
||||||
|
return 0;
|
||||||
|
return (CanData.assigned_id - 0x100) >> 1;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
static uint32_t
|
||||||
|
can_decode_nodeid(int nodeid)
|
||||||
|
{
|
||||||
|
return (nodeid << 1) + 0x100;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
can_process_query_unassigned(struct canbus_msg *msg)
|
||||||
|
{
|
||||||
|
if (CanData.assigned_id)
|
||||||
|
return;
|
||||||
|
struct canbus_msg send;
|
||||||
|
send.id = CANBUS_ID_ADMIN_RESP;
|
||||||
|
send.dlc = 8;
|
||||||
|
send.data[0] = CANBUS_RESP_NEED_NODEID;
|
||||||
|
memcpy(&send.data[1], CanData.uuid, sizeof(CanData.uuid));
|
||||||
|
send.data[7] = CANBUS_CMD_SET_CANBOOT_NODEID;
|
||||||
|
// Send with retry
|
||||||
|
for (;;) {
|
||||||
|
int ret = canserial_send(&send);
|
||||||
|
if (ret >= 0)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
can_process_clear_canboot_nodeid(void)
|
||||||
|
{
|
||||||
|
CanData.assigned_id = 0;
|
||||||
|
canbus_set_filter(CanData.assigned_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
can_id_conflict(void)
|
||||||
|
{
|
||||||
|
CanData.assigned_id = 0;
|
||||||
|
canserial_set_filter(CanData.assigned_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
can_process_set_canboot_nodeid(struct canbus_msg *msg)
|
||||||
|
{
|
||||||
|
if (msg->dlc < 8)
|
||||||
|
return;
|
||||||
|
uint32_t newid = can_decode_nodeid(msg->data[7]);
|
||||||
|
if (can_check_uuid(msg)) {
|
||||||
|
if (newid != CanData.assigned_id) {
|
||||||
|
CanData.assigned_id = newid;
|
||||||
|
canserial_set_filter(CanData.assigned_id);
|
||||||
|
}
|
||||||
|
} else if (newid == CanData.assigned_id) {
|
||||||
|
can_id_conflict();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle an "admin" command
|
||||||
|
static void
|
||||||
|
can_process_admin(struct canbus_msg *msg)
|
||||||
|
{
|
||||||
|
if (!msg->dlc)
|
||||||
|
return;
|
||||||
|
switch (msg->data[0]) {
|
||||||
|
case CANBUS_CMD_QUERY_UNASSIGNED:
|
||||||
|
can_process_query_unassigned(msg);
|
||||||
|
break;
|
||||||
|
case CANBUS_CMD_SET_CANBOOT_NODEID:
|
||||||
|
can_process_set_canboot_nodeid(msg);
|
||||||
|
break;
|
||||||
|
case CANBUS_CMD_CLEAR_CANBOOT_NODEID:
|
||||||
|
can_process_clear_canboot_nodeid();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************
|
||||||
|
* CAN packet reading
|
||||||
|
****************************************************************/
|
||||||
|
|
||||||
|
static void
|
||||||
|
canserial_notify_rx(void)
|
||||||
|
{
|
||||||
|
sched_wake_task(&CanData.rx_wake);
|
||||||
|
}
|
||||||
|
|
||||||
|
DECL_CONSTANT("RECEIVE_WINDOW", ARRAY_SIZE(CanData.receive_buf));
|
||||||
|
|
||||||
|
// Handle incoming data (called from IRQ handler)
|
||||||
|
int
|
||||||
|
canserial_process_data(struct canbus_msg *msg)
|
||||||
|
{
|
||||||
|
uint32_t id = msg->id;
|
||||||
|
if (CanData.assigned_id && id == CanData.assigned_id) {
|
||||||
|
// Add to incoming data buffer
|
||||||
|
int rpos = CanData.receive_pos;
|
||||||
|
uint32_t len = CANMSG_DATA_LEN(msg);
|
||||||
|
if (len > sizeof(CanData.receive_buf) - rpos)
|
||||||
|
return -1;
|
||||||
|
memcpy(&CanData.receive_buf[rpos], msg->data, len);
|
||||||
|
CanData.receive_pos = rpos + len;
|
||||||
|
canserial_notify_rx();
|
||||||
|
} else if (id == CANBUS_ID_ADMIN
|
||||||
|
|| (CanData.assigned_id && id == CanData.assigned_id + 1)) {
|
||||||
|
// Add to admin command queue
|
||||||
|
uint32_t pushp = CanData.admin_push_pos;
|
||||||
|
if (pushp >= CanData.admin_pull_pos + ARRAY_SIZE(CanData.admin_queue))
|
||||||
|
// No space - drop message
|
||||||
|
return -1;
|
||||||
|
uint32_t pos = pushp % ARRAY_SIZE(CanData.admin_queue);
|
||||||
|
memcpy(&CanData.admin_queue[pos], msg, sizeof(*msg));
|
||||||
|
CanData.admin_push_pos = pushp + 1;
|
||||||
|
canserial_notify_rx();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove from the receive buffer the given number of bytes
|
||||||
|
static void
|
||||||
|
console_pop_input(int len)
|
||||||
|
{
|
||||||
|
int copied = 0;
|
||||||
|
for (;;) {
|
||||||
|
int rpos = readb(&CanData.receive_pos);
|
||||||
|
int needcopy = rpos - len;
|
||||||
|
if (needcopy) {
|
||||||
|
memmove(&CanData.receive_buf[copied]
|
||||||
|
, &CanData.receive_buf[copied + len], needcopy - copied);
|
||||||
|
copied = needcopy;
|
||||||
|
canserial_notify_rx();
|
||||||
|
}
|
||||||
|
irqstatus_t flag = irq_save();
|
||||||
|
if (rpos != readb(&CanData.receive_pos)) {
|
||||||
|
// Raced with irq handler - retry
|
||||||
|
irq_restore(flag);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
CanData.receive_pos = needcopy;
|
||||||
|
irq_restore(flag);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Task to process incoming commands and admin messages
|
||||||
|
void
|
||||||
|
canserial_rx_task(void)
|
||||||
|
{
|
||||||
|
if (!sched_check_wake(&CanData.rx_wake))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Process pending admin messages
|
||||||
|
for (;;) {
|
||||||
|
uint32_t pushp = readl(&CanData.admin_push_pos);
|
||||||
|
uint32_t pullp = CanData.admin_pull_pos;
|
||||||
|
if (pushp == pullp)
|
||||||
|
break;
|
||||||
|
uint32_t pos = pullp % ARRAY_SIZE(CanData.admin_queue);
|
||||||
|
struct canbus_msg *msg = &CanData.admin_queue[pos];
|
||||||
|
uint32_t id = msg->id;
|
||||||
|
if (CanData.assigned_id && id == CanData.assigned_id + 1)
|
||||||
|
can_id_conflict();
|
||||||
|
else if (id == CANBUS_ID_ADMIN)
|
||||||
|
can_process_admin(msg);
|
||||||
|
CanData.admin_pull_pos = pullp + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for a complete message block and process it
|
||||||
|
uint_fast8_t rpos = readb(&CanData.receive_pos), pop_count;
|
||||||
|
int ret = command_find_block(CanData.receive_buf, rpos, &pop_count);
|
||||||
|
if (ret > 0)
|
||||||
|
command_dispatch(CanData.receive_buf, pop_count);
|
||||||
|
if (ret) {
|
||||||
|
console_pop_input(pop_count);
|
||||||
|
if (ret > 0)
|
||||||
|
command_send_ack();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DECL_TASK(canserial_rx_task);
|
||||||
|
|
||||||
|
|
||||||
|
/****************************************************************
|
||||||
|
* Setup and shutdown
|
||||||
|
****************************************************************/
|
||||||
|
|
||||||
|
void
|
||||||
|
command_get_canbus_id(uint32_t *args)
|
||||||
|
{
|
||||||
|
uint32_t out[5] = {};
|
||||||
|
memcpy(&out[2], CanData.uuid, 6);
|
||||||
|
command_respond_ack(CMD_GET_CANBUS_ID, out, ARRAY_SIZE(out));
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
canserial_set_uuid(uint8_t *raw_uuid, uint32_t raw_uuid_len)
|
||||||
|
{
|
||||||
|
uint64_t hash = fasthash64(raw_uuid, raw_uuid_len, 0xA16231A7);
|
||||||
|
memcpy(CanData.uuid, &hash, sizeof(CanData.uuid));
|
||||||
|
canserial_notify_rx();
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
canserial_shutdown(void)
|
||||||
|
{
|
||||||
|
canserial_notify_tx();
|
||||||
|
canserial_notify_rx();
|
||||||
|
}
|
||||||
|
DECL_SHUTDOWN(canserial_shutdown);
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef __CANSERIAL_H__
|
||||||
|
#define __CANSERIAL_H__
|
||||||
|
|
||||||
|
#include <stdint.h> // uint32_t
|
||||||
|
|
||||||
|
#define CANBUS_ID_ADMIN 0x3f0
|
||||||
|
#define CANBUS_ID_ADMIN_RESP 0x3f1
|
||||||
|
|
||||||
|
// callbacks provided by board specific code
|
||||||
|
struct canbus_msg;
|
||||||
|
int canserial_send(struct canbus_msg *msg);
|
||||||
|
void canserial_set_filter(uint32_t id);
|
||||||
|
|
||||||
|
// canserial.c
|
||||||
|
void canserial_notify_tx(void);
|
||||||
|
int canserial_process_data(struct canbus_msg *msg);
|
||||||
|
void canserial_set_uuid(uint8_t *raw_uuid, uint32_t raw_uuid_len);
|
||||||
|
|
||||||
|
#endif // canserial.h
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <string.h> // memmove
|
#include <string.h> // memmove
|
||||||
#include "autoconf.h" // CONFIG_USB_VENDOR_ID
|
#include "autoconf.h" // CONFIG_USB_VENDOR_ID
|
||||||
|
#include "board/misc.h" // console_sendf
|
||||||
#include "board/pgm.h" // PROGMEM
|
#include "board/pgm.h" // PROGMEM
|
||||||
#include "board/usb_cdc_ep.h" // USB_CDC_EP_BULK_IN
|
#include "board/usb_cdc_ep.h" // USB_CDC_EP_BULK_IN
|
||||||
#include "byteorder.h" // cpu_to_le16
|
#include "byteorder.h" // cpu_to_le16
|
||||||
|
|||||||
@@ -57,10 +57,6 @@ config FLASH_START
|
|||||||
hex
|
hex
|
||||||
default 0x0000
|
default 0x0000
|
||||||
|
|
||||||
config USBSERIAL
|
|
||||||
bool
|
|
||||||
config SERIAL
|
|
||||||
bool
|
|
||||||
choice
|
choice
|
||||||
prompt "Communication interface"
|
prompt "Communication interface"
|
||||||
config LPC_USB
|
config LPC_USB
|
||||||
|
|||||||
+17
-15
@@ -75,6 +75,10 @@ choice
|
|||||||
select MACH_STM32H7
|
select MACH_STM32H7
|
||||||
endchoice
|
endchoice
|
||||||
|
|
||||||
|
config MACH_STM32F103x6
|
||||||
|
depends on LOW_LEVEL_OPTIONS && MACH_STM32F103
|
||||||
|
bool "Only 10KiB of RAM (for rare stm32f103x6 variant)"
|
||||||
|
|
||||||
config MACH_STM32F0
|
config MACH_STM32F0
|
||||||
bool
|
bool
|
||||||
config MACH_STM32F1
|
config MACH_STM32F1
|
||||||
@@ -93,13 +97,17 @@ config MACH_STM32F4x5 # F405, F407, F429 series
|
|||||||
bool
|
bool
|
||||||
config HAVE_STM32_USBFS
|
config HAVE_STM32_USBFS
|
||||||
bool
|
bool
|
||||||
default y if MACH_STM32F103 || MACH_STM32F0x2 || MACH_STM32F070 || MACH_STM32G0
|
default y if MACH_STM32F0x2 || MACH_STM32G0
|
||||||
|
default y if (MACH_STM32F103 || MACH_STM32F070) && !STM32_CLOCK_REF_INTERNAL
|
||||||
config HAVE_STM32_USBOTG
|
config HAVE_STM32_USBOTG
|
||||||
bool
|
bool
|
||||||
default y if MACH_STM32F2 || MACH_STM32F4 || MACH_STM32H7
|
default y if MACH_STM32F2 || MACH_STM32F4 || MACH_STM32H7
|
||||||
config HAVE_STM32_CANBUS
|
config HAVE_STM32_CANBUS
|
||||||
bool
|
bool
|
||||||
default y if MACH_STM32F1 || MACH_STM32F2 || MACH_STM32F4x5 || MACH_STM32F446 || MACH_STM32F0x2
|
default y if MACH_STM32F1 || MACH_STM32F2 || MACH_STM32F4x5 || MACH_STM32F446 || MACH_STM32F0x2
|
||||||
|
config HAVE_STM32_FDCANBUS
|
||||||
|
bool
|
||||||
|
default y if MACH_STM32G0
|
||||||
|
|
||||||
config MCU
|
config MCU
|
||||||
string
|
string
|
||||||
@@ -152,7 +160,8 @@ config RAM_SIZE
|
|||||||
default 0x1000 if MACH_STM32F031
|
default 0x1000 if MACH_STM32F031
|
||||||
default 0x1800 if MACH_STM32F042
|
default 0x1800 if MACH_STM32F042
|
||||||
default 0x4000 if MACH_STM32F070 || MACH_STM32F072
|
default 0x4000 if MACH_STM32F070 || MACH_STM32F072
|
||||||
default 0x5000 if MACH_STM32F103 # Ram size of stm32f103x8 (20KiB)
|
default 0x2800 if MACH_STM32F103x6
|
||||||
|
default 0x5000 if MACH_STM32F103 && !MACH_STM32F103x6 # Ram size of stm32f103x8
|
||||||
default 0x20000 if MACH_STM32F207
|
default 0x20000 if MACH_STM32F207
|
||||||
default 0x10000 if MACH_STM32F401
|
default 0x10000 if MACH_STM32F401
|
||||||
default 0x20000 if MACH_STM32F4x5 || MACH_STM32F446
|
default 0x20000 if MACH_STM32F4x5 || MACH_STM32F446
|
||||||
@@ -257,12 +266,6 @@ config STM32F0_TRIM
|
|||||||
# Communication inteface
|
# Communication inteface
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
config USBSERIAL
|
|
||||||
bool
|
|
||||||
config SERIAL
|
|
||||||
bool
|
|
||||||
config CANSERIAL
|
|
||||||
bool
|
|
||||||
choice
|
choice
|
||||||
prompt "Communication interface"
|
prompt "Communication interface"
|
||||||
config STM32_USB_PA11_PA12
|
config STM32_USB_PA11_PA12
|
||||||
@@ -304,7 +307,7 @@ choice
|
|||||||
select SERIAL
|
select SERIAL
|
||||||
config STM32_CANBUS_PA11_PA12
|
config STM32_CANBUS_PA11_PA12
|
||||||
bool "CAN bus (on PA11/PA12)"
|
bool "CAN bus (on PA11/PA12)"
|
||||||
depends on HAVE_STM32_CANBUS
|
depends on HAVE_STM32_CANBUS || HAVE_STM32_FDCANBUS
|
||||||
select CANSERIAL
|
select CANSERIAL
|
||||||
config STM32_CANBUS_PA11_PA12_REMAP
|
config STM32_CANBUS_PA11_PA12_REMAP
|
||||||
bool "CAN bus (on PA9/PA10)" if LOW_LEVEL_OPTIONS
|
bool "CAN bus (on PA9/PA10)" if LOW_LEVEL_OPTIONS
|
||||||
@@ -312,7 +315,7 @@ choice
|
|||||||
select CANSERIAL
|
select CANSERIAL
|
||||||
config STM32_CANBUS_PB8_PB9
|
config STM32_CANBUS_PB8_PB9
|
||||||
bool "CAN bus (on PB8/PB9)" if LOW_LEVEL_OPTIONS
|
bool "CAN bus (on PB8/PB9)" if LOW_LEVEL_OPTIONS
|
||||||
depends on HAVE_STM32_CANBUS
|
depends on HAVE_STM32_CANBUS || HAVE_STM32_FDCANBUS
|
||||||
select CANSERIAL
|
select CANSERIAL
|
||||||
config STM32_CANBUS_PI9_PH13
|
config STM32_CANBUS_PI9_PH13
|
||||||
bool "CAN bus (on PI9/PH13)" if LOW_LEVEL_OPTIONS
|
bool "CAN bus (on PI9/PH13)" if LOW_LEVEL_OPTIONS
|
||||||
@@ -330,13 +333,12 @@ choice
|
|||||||
bool "CAN bus (on PD0/PD1)" if LOW_LEVEL_OPTIONS
|
bool "CAN bus (on PD0/PD1)" if LOW_LEVEL_OPTIONS
|
||||||
depends on HAVE_STM32_CANBUS
|
depends on HAVE_STM32_CANBUS
|
||||||
select CANSERIAL
|
select CANSERIAL
|
||||||
|
config STM32_CANBUS_PB0_PB1
|
||||||
|
bool "CAN bus (on PB0/PB1)"
|
||||||
|
depends on HAVE_STM32_FDCANBUS
|
||||||
|
select CANSERIAL
|
||||||
endchoice
|
endchoice
|
||||||
|
|
||||||
config CANBUS_FREQUENCY
|
|
||||||
int "CAN bus speed" if LOW_LEVEL_OPTIONS && CANSERIAL
|
|
||||||
default 500000
|
|
||||||
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# Flash settings
|
# Flash settings
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|||||||
+5
-5
@@ -3,7 +3,7 @@
|
|||||||
# Setup the toolchain
|
# Setup the toolchain
|
||||||
CROSS_PREFIX=arm-none-eabi-
|
CROSS_PREFIX=arm-none-eabi-
|
||||||
|
|
||||||
dirs-y += src/stm32 src/generic
|
dirs-y += src/stm32 src/generic lib/fast-hash
|
||||||
dirs-$(CONFIG_MACH_STM32F0) += lib/stm32f0
|
dirs-$(CONFIG_MACH_STM32F0) += lib/stm32f0
|
||||||
dirs-$(CONFIG_MACH_STM32F1) += lib/stm32f1
|
dirs-$(CONFIG_MACH_STM32F1) += lib/stm32f1
|
||||||
dirs-$(CONFIG_MACH_STM32F2) += lib/stm32f2
|
dirs-$(CONFIG_MACH_STM32F2) += lib/stm32f2
|
||||||
@@ -53,10 +53,10 @@ serial-src-$(CONFIG_MACH_STM32F0) := stm32/stm32f0_serial.c
|
|||||||
serial-src-$(CONFIG_MACH_STM32G0) := stm32/stm32f0_serial.c
|
serial-src-$(CONFIG_MACH_STM32G0) := stm32/stm32f0_serial.c
|
||||||
serial-src-$(CONFIG_MACH_STM32H7) := stm32/stm32h7_serial.c
|
serial-src-$(CONFIG_MACH_STM32H7) := stm32/stm32h7_serial.c
|
||||||
src-$(CONFIG_SERIAL) += $(serial-src-y) generic/serial_irq.c
|
src-$(CONFIG_SERIAL) += $(serial-src-y) generic/serial_irq.c
|
||||||
src-$(CONFIG_CANSERIAL) += stm32/can.c ../lib/fast-hash/fasthash.c
|
canbus-src-y := generic/canserial.c ../lib/fast-hash/fasthash.c
|
||||||
src-$(CONFIG_CANSERIAL) += generic/canbus.c
|
canbus-src-$(CONFIG_HAVE_STM32_CANBUS) += stm32/can.c
|
||||||
|
canbus-src-$(CONFIG_HAVE_STM32_FDCANBUS) += stm32/fdcan.c
|
||||||
dirs-$(CONFIG_CANSERIAL) += lib/fast-hash
|
src-$(CONFIG_CANSERIAL) += $(canbus-src-y) generic/canbus.c stm32/chipid.c
|
||||||
|
|
||||||
# Binary output file rules
|
# Binary output file rules
|
||||||
target-y += $(OUT)canboot.bin
|
target-y += $(OUT)canboot.bin
|
||||||
|
|||||||
+42
-80
@@ -10,9 +10,9 @@
|
|||||||
#include "autoconf.h" // CONFIG_MACH_STM32F1
|
#include "autoconf.h" // CONFIG_MACH_STM32F1
|
||||||
#include "board/irq.h" // irq_disable
|
#include "board/irq.h" // irq_disable
|
||||||
#include "command.h" // DECL_CONSTANT_STR
|
#include "command.h" // DECL_CONSTANT_STR
|
||||||
#include "fasthash.h" // fasthash64
|
|
||||||
#include "generic/armcm_boot.h" // armcm_enable_irq
|
#include "generic/armcm_boot.h" // armcm_enable_irq
|
||||||
#include "generic/canbus.h" // canbus_notify_tx
|
#include "generic/canbus.h" // canbus_notify_tx
|
||||||
|
#include "generic/canserial.h" // CANBUS_ID_ADMIN
|
||||||
#include "internal.h" // enable_pclock
|
#include "internal.h" // enable_pclock
|
||||||
#include "sched.h" // DECL_INIT
|
#include "sched.h" // DECL_INIT
|
||||||
|
|
||||||
@@ -66,7 +66,6 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if CONFIG_MACH_STM32F4
|
#if CONFIG_MACH_STM32F4
|
||||||
#warning CAN on STM32F4 is untested
|
|
||||||
#if (CONFIG_STM32_CANBUS_PA11_PA12 || CONFIG_STM32_CANBUS_PB8_PB9 \
|
#if (CONFIG_STM32_CANBUS_PA11_PA12 || CONFIG_STM32_CANBUS_PB8_PB9 \
|
||||||
|| CONFIG_STM32_CANBUS_PD0_PD1 || CONFIG_STM32_CANBUS_PI9_PH13)
|
|| CONFIG_STM32_CANBUS_PD0_PD1 || CONFIG_STM32_CANBUS_PI9_PH13)
|
||||||
#define SOC_CAN CAN1
|
#define SOC_CAN CAN1
|
||||||
@@ -91,41 +90,9 @@
|
|||||||
#error No known CAN device for configured MCU
|
#error No known CAN device for configured MCU
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Read the next CAN packet
|
|
||||||
int
|
|
||||||
canbus_read(uint32_t *id, uint8_t *data)
|
|
||||||
{
|
|
||||||
if (!(SOC_CAN->RF0R & CAN_RF0R_FMP0)) {
|
|
||||||
// All rx mboxes empty, enable wake on rx IRQ
|
|
||||||
irq_disable();
|
|
||||||
SOC_CAN->IER |= CAN_IER_FMPIE0;
|
|
||||||
irq_enable();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read and ack packet
|
|
||||||
CAN_FIFOMailBox_TypeDef *mb = &SOC_CAN->sFIFOMailBox[0];
|
|
||||||
uint32_t rir_id = (mb->RIR >> CAN_RI0R_STID_Pos) & 0x7FF;
|
|
||||||
uint32_t dlc = mb->RDTR & CAN_RDT0R_DLC;
|
|
||||||
uint32_t rdlr = mb->RDLR, rdhr = mb->RDHR;
|
|
||||||
SOC_CAN->RF0R = CAN_RF0R_RFOM0;
|
|
||||||
|
|
||||||
// Return packet
|
|
||||||
*id = rir_id;
|
|
||||||
data[0] = (rdlr >> 0) & 0xff;
|
|
||||||
data[1] = (rdlr >> 8) & 0xff;
|
|
||||||
data[2] = (rdlr >> 16) & 0xff;
|
|
||||||
data[3] = (rdlr >> 24) & 0xff;
|
|
||||||
data[4] = (rdhr >> 0) & 0xff;
|
|
||||||
data[5] = (rdhr >> 8) & 0xff;
|
|
||||||
data[6] = (rdhr >> 16) & 0xff;
|
|
||||||
data[7] = (rdhr >> 24) & 0xff;
|
|
||||||
return dlc;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Transmit a packet
|
// Transmit a packet
|
||||||
int
|
int
|
||||||
canbus_send(uint32_t id, uint32_t len, uint8_t *data)
|
canbus_send(struct canbus_msg *msg)
|
||||||
{
|
{
|
||||||
uint32_t tsr = SOC_CAN->TSR;
|
uint32_t tsr = SOC_CAN->TSR;
|
||||||
if (!(tsr & (CAN_TSR_TME0|CAN_TSR_TME1|CAN_TSR_TME2))) {
|
if (!(tsr & (CAN_TSR_TME0|CAN_TSR_TME1|CAN_TSR_TME2))) {
|
||||||
@@ -143,23 +110,21 @@ canbus_send(uint32_t id, uint32_t len, uint8_t *data)
|
|||||||
CAN_TxMailBox_TypeDef *mb = &SOC_CAN->sTxMailBox[mbox];
|
CAN_TxMailBox_TypeDef *mb = &SOC_CAN->sTxMailBox[mbox];
|
||||||
|
|
||||||
/* Set up the DLC */
|
/* Set up the DLC */
|
||||||
mb->TDTR = (mb->TDTR & 0xFFFFFFF0) | (len & 0x0F);
|
mb->TDTR = (mb->TDTR & 0xFFFFFFF0) | (msg->dlc & 0x0F);
|
||||||
|
|
||||||
/* Set up the data field */
|
/* Set up the data field */
|
||||||
if (len) {
|
mb->TDLR = msg->data32[0];
|
||||||
mb->TDLR = (((uint32_t)data[3] << 24)
|
mb->TDHR = msg->data32[1];
|
||||||
| ((uint32_t)data[2] << 16)
|
|
||||||
| ((uint32_t)data[1] << 8)
|
|
||||||
| ((uint32_t)data[0] << 0));
|
|
||||||
mb->TDHR = (((uint32_t)data[7] << 24)
|
|
||||||
| ((uint32_t)data[6] << 16)
|
|
||||||
| ((uint32_t)data[5] << 8)
|
|
||||||
| ((uint32_t)data[4] << 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Request transmission */
|
/* Request transmission */
|
||||||
mb->TIR = (id << CAN_TI0R_STID_Pos) | CAN_TI0R_TXRQ;
|
uint32_t tir;
|
||||||
return len;
|
if (msg->id & CANMSG_ID_EFF)
|
||||||
|
tir = ((msg->id & 0x1fffffff) << CAN_TI0R_EXID_Pos) | CAN_TI0R_IDE;
|
||||||
|
else
|
||||||
|
tir = (msg->id & 0x7ff) << CAN_TI0R_STID_Pos;
|
||||||
|
tir |= msg->id & CANMSG_ID_RTR ? CAN_TI0R_RTR : 0;
|
||||||
|
mb->TIR = (msg->id << CAN_TI0R_STID_Pos) | CAN_TI0R_TXRQ;
|
||||||
|
return CANMSG_DATA_LEN(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup the receive packet filter
|
// Setup the receive packet filter
|
||||||
@@ -171,20 +136,23 @@ canbus_set_filter(uint32_t id)
|
|||||||
/* Initialisation mode for the filter */
|
/* Initialisation mode for the filter */
|
||||||
SOC_CAN->FA1R = 0;
|
SOC_CAN->FA1R = 0;
|
||||||
|
|
||||||
uint32_t mask = CAN_RI0R_STID | CAN_TI0R_IDE | CAN_TI0R_RTR;
|
if (CONFIG_CANBUS_FILTER) {
|
||||||
SOC_CAN->sFilterRegister[0].FR1 = CANBUS_ID_ADMIN << CAN_RI0R_STID_Pos;
|
uint32_t mask = CAN_TI0R_STID | CAN_TI0R_IDE | CAN_TI0R_RTR;
|
||||||
SOC_CAN->sFilterRegister[0].FR2 = mask;
|
SOC_CAN->sFilterRegister[0].FR1 = CANBUS_ID_ADMIN << CAN_RI0R_STID_Pos;
|
||||||
SOC_CAN->sFilterRegister[1].FR1 = (id + 1) << CAN_RI0R_STID_Pos;
|
SOC_CAN->sFilterRegister[0].FR2 = mask;
|
||||||
SOC_CAN->sFilterRegister[1].FR2 = mask;
|
SOC_CAN->sFilterRegister[1].FR1 = (id + 1) << CAN_RI0R_STID_Pos;
|
||||||
SOC_CAN->sFilterRegister[2].FR1 = id << CAN_RI0R_STID_Pos;
|
SOC_CAN->sFilterRegister[1].FR2 = mask;
|
||||||
SOC_CAN->sFilterRegister[2].FR2 = mask;
|
SOC_CAN->sFilterRegister[2].FR1 = id << CAN_RI0R_STID_Pos;
|
||||||
|
SOC_CAN->sFilterRegister[2].FR2 = mask;
|
||||||
|
} else {
|
||||||
|
SOC_CAN->sFilterRegister[0].FR1 = 0;
|
||||||
|
SOC_CAN->sFilterRegister[0].FR2 = 0;
|
||||||
|
id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* 32-bit scale for the filter */
|
/* 32-bit scale for the filter */
|
||||||
SOC_CAN->FS1R = (1<<0) | (1<<1) | (1<<2);
|
SOC_CAN->FS1R = (1<<0) | (1<<1) | (1<<2);
|
||||||
|
|
||||||
/* FIFO 1 assigned to 'id' */
|
|
||||||
SOC_CAN->FFA1R = (1<<2);
|
|
||||||
|
|
||||||
/* Filter activation */
|
/* Filter activation */
|
||||||
SOC_CAN->FA1R = (1<<0) | (id ? (1<<1) | (1<<2) : 0);
|
SOC_CAN->FA1R = (1<<0) | (id ? (1<<1) | (1<<2) : 0);
|
||||||
/* Leave the initialisation mode for the filter */
|
/* Leave the initialisation mode for the filter */
|
||||||
@@ -195,27 +163,25 @@ canbus_set_filter(uint32_t id)
|
|||||||
void
|
void
|
||||||
CAN_IRQHandler(void)
|
CAN_IRQHandler(void)
|
||||||
{
|
{
|
||||||
if (SOC_CAN->RF1R & CAN_RF1R_FMP1) {
|
if (SOC_CAN->RF0R & CAN_RF0R_FMP0) {
|
||||||
// Read and ack data packet
|
// Read and ack data packet
|
||||||
CAN_FIFOMailBox_TypeDef *mb = &SOC_CAN->sFIFOMailBox[1];
|
CAN_FIFOMailBox_TypeDef *mb = &SOC_CAN->sFIFOMailBox[0];
|
||||||
uint32_t rir_id = (mb->RIR >> CAN_RI0R_STID_Pos) & 0x7FF;
|
uint32_t rir = mb->RIR;
|
||||||
uint32_t dlc = mb->RDTR & CAN_RDT0R_DLC;
|
struct canbus_msg msg;
|
||||||
uint32_t rdlr = mb->RDLR, rdhr = mb->RDHR;
|
if (rir & CAN_RI0R_IDE)
|
||||||
SOC_CAN->RF1R = CAN_RF1R_RFOM1;
|
msg.id = ((rir >> CAN_RI0R_EXID_Pos) & 0x1fffffff) | CANMSG_ID_EFF;
|
||||||
|
else
|
||||||
|
msg.id = (rir >> CAN_RI0R_STID_Pos) & 0x7ff;
|
||||||
|
msg.id |= rir & CAN_RI0R_RTR ? CANMSG_ID_RTR : 0;
|
||||||
|
msg.dlc = mb->RDTR & CAN_RDT0R_DLC;
|
||||||
|
msg.data32[0] = mb->RDLR;
|
||||||
|
msg.data32[1] = mb->RDHR;
|
||||||
|
SOC_CAN->RF0R = CAN_RF0R_RFOM0;
|
||||||
|
|
||||||
// Process packet
|
// Process packet
|
||||||
union {
|
canbus_process_data(&msg);
|
||||||
struct { uint32_t rdlr, rdhr; };
|
|
||||||
uint8_t data[8];
|
|
||||||
} rdata = { .rdlr = rdlr, .rdhr = rdhr };
|
|
||||||
canbus_process_data(rir_id, dlc, rdata.data);
|
|
||||||
}
|
}
|
||||||
uint32_t ier = SOC_CAN->IER;
|
uint32_t ier = SOC_CAN->IER;
|
||||||
if (ier & CAN_IER_FMPIE0 && SOC_CAN->RF0R & CAN_RF0R_FMP0) {
|
|
||||||
// Admin Rx
|
|
||||||
SOC_CAN->IER = ier = ier & ~CAN_IER_FMPIE0;
|
|
||||||
canbus_notify_rx();
|
|
||||||
}
|
|
||||||
if (ier & CAN_IER_TMEIE
|
if (ier & CAN_IER_TMEIE
|
||||||
&& SOC_CAN->TSR & (CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2)) {
|
&& SOC_CAN->TSR & (CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2)) {
|
||||||
// Tx
|
// Tx
|
||||||
@@ -256,7 +222,7 @@ compute_btr(uint32_t pclock, uint32_t bitrate)
|
|||||||
|
|
||||||
uint32_t bit_clocks = pclock / bitrate; // clock ticks per bit
|
uint32_t bit_clocks = pclock / bitrate; // clock ticks per bit
|
||||||
|
|
||||||
uint32_t sjw = 2;
|
uint32_t sjw = 2;
|
||||||
uint32_t qs;
|
uint32_t qs;
|
||||||
// Find number of time quantas that gives us the exact wanted bit time
|
// Find number of time quantas that gives us the exact wanted bit time
|
||||||
for (qs = 18; qs > 9; qs--) {
|
for (qs = 18; qs > 9; qs--) {
|
||||||
@@ -310,10 +276,6 @@ can_init(void)
|
|||||||
armcm_enable_irq(CAN_IRQHandler, CAN_RX1_IRQn, 0);
|
armcm_enable_irq(CAN_IRQHandler, CAN_RX1_IRQn, 0);
|
||||||
if (CAN_RX0_IRQn != CAN_TX_IRQn)
|
if (CAN_RX0_IRQn != CAN_TX_IRQn)
|
||||||
armcm_enable_irq(CAN_IRQHandler, CAN_TX_IRQn, 0);
|
armcm_enable_irq(CAN_IRQHandler, CAN_TX_IRQn, 0);
|
||||||
SOC_CAN->IER = CAN_IER_FMPIE1;
|
SOC_CAN->IER = CAN_IER_FMPIE0;
|
||||||
|
|
||||||
// Convert unique 96-bit chip id into 48 bit representation
|
|
||||||
uint64_t hash = fasthash64((uint8_t*)UID_BASE, 12, 0xA16231A7);
|
|
||||||
canbus_set_uuid(&hash);
|
|
||||||
}
|
}
|
||||||
DECL_INIT(can_init);
|
DECL_INIT(can_init);
|
||||||
+6
-4
@@ -4,6 +4,7 @@
|
|||||||
//
|
//
|
||||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||||
|
|
||||||
|
#include "generic/canserial.h" // canserial_set_uuid
|
||||||
#include "generic/usb_cdc.h" // usb_fill_serial
|
#include "generic/usb_cdc.h" // usb_fill_serial
|
||||||
#include "generic/usbstd.h" // usb_string_descriptor
|
#include "generic/usbstd.h" // usb_string_descriptor
|
||||||
#include "internal.h" // UID_BASE
|
#include "internal.h" // UID_BASE
|
||||||
@@ -25,9 +26,10 @@ usbserial_get_serialid(void)
|
|||||||
void
|
void
|
||||||
chipid_init(void)
|
chipid_init(void)
|
||||||
{
|
{
|
||||||
if (!CONFIG_USB_SERIAL_NUMBER_CHIPID)
|
if (CONFIG_USB_SERIAL_NUMBER_CHIPID)
|
||||||
return;
|
usb_fill_serial(&cdc_chipid.desc, ARRAY_SIZE(cdc_chipid.data)
|
||||||
usb_fill_serial(&cdc_chipid.desc, ARRAY_SIZE(cdc_chipid.data)
|
, (void*)UID_BASE);
|
||||||
, (void*)UID_BASE);
|
if (CONFIG_CANBUS)
|
||||||
|
canserial_set_uuid((void*)UID_BASE, CHIP_UID_LEN);
|
||||||
}
|
}
|
||||||
DECL_INIT(chipid_init);
|
DECL_INIT(chipid_init);
|
||||||
@@ -0,0 +1,266 @@
|
|||||||
|
// Serial over CAN emulation for STM32 boards.
|
||||||
|
//
|
||||||
|
// Copyright (C) 2019 Eug Krashtan <eug.krashtan@gmail.com>
|
||||||
|
// Copyright (C) 2020 Pontus Borg <glpontus@gmail.com>
|
||||||
|
// Copyright (C) 2021 Kevin O'Connor <kevin@koconnor.net>
|
||||||
|
//
|
||||||
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||||
|
|
||||||
|
#include <string.h> // memcpy
|
||||||
|
#include "autoconf.h" // CONFIG_MACH_STM32F1
|
||||||
|
#include "board/irq.h" // irq_disable
|
||||||
|
#include "command.h" // DECL_CONSTANT_STR
|
||||||
|
#include "generic/armcm_boot.h" // armcm_enable_irq
|
||||||
|
#include "generic/canbus.h" // canbus_notify_tx
|
||||||
|
#include "generic/canserial.h" // CANBUS_ID_ADMIN
|
||||||
|
#include "generic/serial_irq.h" // serial_rx_byte
|
||||||
|
#include "internal.h" // enable_pclock
|
||||||
|
#include "sched.h" // DECL_INIT
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
__IO uint32_t id_section;
|
||||||
|
__IO uint32_t dlc_section;
|
||||||
|
__IO uint32_t data[64 / 4];
|
||||||
|
}FDCAN_FIFO_TypeDef;
|
||||||
|
|
||||||
|
#define FDCAN_XTD (1<<30)
|
||||||
|
#define FDCAN_RTR (1<<29)
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
__IO uint32_t FLS[28]; // Filter list standard
|
||||||
|
__IO uint32_t FLE[16]; // Filter list extended
|
||||||
|
FDCAN_FIFO_TypeDef RXF0[3];
|
||||||
|
FDCAN_FIFO_TypeDef RXF1[3];
|
||||||
|
__IO uint32_t TEF[6]; // Tx event FIFO
|
||||||
|
FDCAN_FIFO_TypeDef TXFIFO[3];
|
||||||
|
}FDCAN_MSG_RAM_TypeDef;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
FDCAN_MSG_RAM_TypeDef fdcan1;
|
||||||
|
FDCAN_MSG_RAM_TypeDef fdcan2;
|
||||||
|
}FDCAN_RAM_TypeDef;
|
||||||
|
|
||||||
|
FDCAN_RAM_TypeDef *fdcan_ram = (FDCAN_RAM_TypeDef *)(SRAMCAN_BASE);
|
||||||
|
|
||||||
|
#define FDCAN_IE_TC (FDCAN_IE_TCE | FDCAN_IE_TCFE | FDCAN_IE_TFEE)
|
||||||
|
|
||||||
|
#if CONFIG_STM32_CANBUS_PA11_PA12
|
||||||
|
DECL_CONSTANT_STR("RESERVE_PINS_CAN", "PA11,PA12");
|
||||||
|
#define GPIO_Rx GPIO('A', 11)
|
||||||
|
#define GPIO_Tx GPIO('A', 12)
|
||||||
|
#elif CONFIG_STM32_CANBUS_PB8_PB9
|
||||||
|
DECL_CONSTANT_STR("RESERVE_PINS_CAN", "PB8,PB9");
|
||||||
|
#define GPIO_Rx GPIO('B', 8)
|
||||||
|
#define GPIO_Tx GPIO('B', 9)
|
||||||
|
#elif CONFIG_STM32_CANBUS_PB0_PB1
|
||||||
|
DECL_CONSTANT_STR("RESERVE_PINS_CAN", "PB0,PB1");
|
||||||
|
#define GPIO_Rx GPIO('B', 0)
|
||||||
|
#define GPIO_Tx GPIO('B', 1)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !CONFIG_STM32_CANBUS_PB0_PB1
|
||||||
|
#define SOC_CAN FDCAN1
|
||||||
|
#define MSG_RAM fdcan_ram->fdcan1
|
||||||
|
#else
|
||||||
|
#define SOC_CAN FDCAN2
|
||||||
|
#define MSG_RAM fdcan_ram->fdcan2
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define CAN_IT0_IRQn TIM16_FDCAN_IT0_IRQn
|
||||||
|
#define CAN_FUNCTION GPIO_FUNCTION(3) // Alternative function mapping number
|
||||||
|
|
||||||
|
#ifndef SOC_CAN
|
||||||
|
#error No known CAN device for configured MCU
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Transmit a packet
|
||||||
|
int
|
||||||
|
canbus_send(struct canbus_msg *msg)
|
||||||
|
{
|
||||||
|
uint32_t txfqs = SOC_CAN->TXFQS;
|
||||||
|
if (txfqs & FDCAN_TXFQS_TFQF)
|
||||||
|
// No space in transmit fifo - wait for irq
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
uint32_t w_index = ((txfqs & FDCAN_TXFQS_TFQPI) >> FDCAN_TXFQS_TFQPI_Pos);
|
||||||
|
FDCAN_FIFO_TypeDef *txfifo = &MSG_RAM.TXFIFO[w_index];
|
||||||
|
uint32_t ids;
|
||||||
|
if (msg->id & CANMSG_ID_EFF)
|
||||||
|
ids = (msg->id & 0x1fffffff) | FDCAN_XTD;
|
||||||
|
else
|
||||||
|
ids = (msg->id & 0x7ff) << 18;
|
||||||
|
ids |= msg->id & CANMSG_ID_RTR ? FDCAN_RTR : 0;
|
||||||
|
txfifo->id_section = ids;
|
||||||
|
txfifo->dlc_section = (msg->dlc & 0x0f) << 16;
|
||||||
|
txfifo->data[0] = msg->data32[0];
|
||||||
|
txfifo->data[1] = msg->data32[1];
|
||||||
|
SOC_CAN->TXBAR = ((uint32_t)1 << w_index);
|
||||||
|
return CANMSG_DATA_LEN(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
can_filter(uint32_t index, uint32_t id)
|
||||||
|
{
|
||||||
|
MSG_RAM.FLS[index] = ((0x2 << 30) // Classic filter
|
||||||
|
| (0x1 << 27) // Store in Rx FIFO 0 if filter matches
|
||||||
|
| (id << 16)
|
||||||
|
| 0x7FF); // mask all enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup the receive packet filter
|
||||||
|
void
|
||||||
|
canbus_set_filter(uint32_t id)
|
||||||
|
{
|
||||||
|
if (!CONFIG_CANBUS_FILTER)
|
||||||
|
return;
|
||||||
|
/* Request initialisation */
|
||||||
|
SOC_CAN->CCCR |= FDCAN_CCCR_INIT;
|
||||||
|
/* Wait the acknowledge */
|
||||||
|
while (!(SOC_CAN->CCCR & FDCAN_CCCR_INIT))
|
||||||
|
;
|
||||||
|
/* Enable configuration change */
|
||||||
|
SOC_CAN->CCCR |= FDCAN_CCCR_CCE;
|
||||||
|
|
||||||
|
// Load filter
|
||||||
|
can_filter(0, CANBUS_ID_ADMIN);
|
||||||
|
can_filter(1, id);
|
||||||
|
can_filter(2, id + 1);
|
||||||
|
SOC_CAN->RXGFC = ((id ? 3 : 1) << FDCAN_RXGFC_LSS_Pos
|
||||||
|
| 0x02 << FDCAN_RXGFC_ANFS_Pos);
|
||||||
|
|
||||||
|
/* Leave the initialisation mode for the filter */
|
||||||
|
SOC_CAN->CCCR &= ~FDCAN_CCCR_CCE;
|
||||||
|
SOC_CAN->CCCR &= ~FDCAN_CCCR_INIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function handles CAN global interrupts
|
||||||
|
void
|
||||||
|
CAN_IRQHandler(void)
|
||||||
|
{
|
||||||
|
uint32_t ir = SOC_CAN->IR;
|
||||||
|
|
||||||
|
if (ir & FDCAN_IE_RF0NE) {
|
||||||
|
SOC_CAN->IR = FDCAN_IE_RF0NE;
|
||||||
|
|
||||||
|
uint32_t rxf0s = SOC_CAN->RXF0S;
|
||||||
|
if (rxf0s & FDCAN_RXF0S_F0FL) {
|
||||||
|
// Read and ack data packet
|
||||||
|
uint32_t idx = (rxf0s & FDCAN_RXF0S_F0GI) >> FDCAN_RXF0S_F0GI_Pos;
|
||||||
|
FDCAN_FIFO_TypeDef *rxf0 = &MSG_RAM.RXF0[idx];
|
||||||
|
uint32_t ids = rxf0->id_section;
|
||||||
|
struct canbus_msg msg;
|
||||||
|
if (ids & FDCAN_XTD)
|
||||||
|
msg.id = (ids & 0x1fffffff) | CANMSG_ID_EFF;
|
||||||
|
else
|
||||||
|
msg.id = (ids >> 18) & 0x7ff;
|
||||||
|
msg.id |= ids & FDCAN_RTR ? CANMSG_ID_RTR : 0;
|
||||||
|
msg.dlc = (rxf0->dlc_section >> 16) & 0x0f;
|
||||||
|
msg.data32[0] = rxf0->data[0];
|
||||||
|
msg.data32[1] = rxf0->data[1];
|
||||||
|
SOC_CAN->RXF0A = idx;
|
||||||
|
|
||||||
|
// Process packet
|
||||||
|
canbus_process_data(&msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ir & FDCAN_IE_TC) {
|
||||||
|
// Tx
|
||||||
|
SOC_CAN->IR = FDCAN_IE_TC;
|
||||||
|
canbus_notify_tx();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline const uint32_t
|
||||||
|
make_btr(uint32_t sjw, // Sync jump width, ... hmm
|
||||||
|
uint32_t time_seg1, // time segment before sample point, 1 .. 16
|
||||||
|
uint32_t time_seg2, // time segment after sample point, 1 .. 8
|
||||||
|
uint32_t brp) // Baud rate prescaler, 1 .. 1024
|
||||||
|
{
|
||||||
|
return (((uint32_t)(sjw-1)) << FDCAN_NBTP_NSJW_Pos
|
||||||
|
| ((uint32_t)(time_seg1-1)) << FDCAN_NBTP_NTSEG1_Pos
|
||||||
|
| ((uint32_t)(time_seg2-1)) << FDCAN_NBTP_NTSEG2_Pos
|
||||||
|
| ((uint32_t)(brp - 1)) << FDCAN_NBTP_NBRP_Pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline const uint32_t
|
||||||
|
compute_btr(uint32_t pclock, uint32_t bitrate)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
Some equations:
|
||||||
|
Tpclock = 1 / pclock
|
||||||
|
Tq = brp * Tpclock
|
||||||
|
Tbs1 = Tq * TS1
|
||||||
|
Tbs2 = Tq * TS2
|
||||||
|
NominalBitTime = Tq + Tbs1 + Tbs2
|
||||||
|
BaudRate = 1/NominalBitTime
|
||||||
|
Bit value sample point is after Tq+Tbs1. Ideal sample point
|
||||||
|
is at 87.5% of NominalBitTime
|
||||||
|
Use the lowest brp where ts1 and ts2 are in valid range
|
||||||
|
*/
|
||||||
|
|
||||||
|
uint32_t bit_clocks = pclock / bitrate; // clock ticks per bit
|
||||||
|
|
||||||
|
uint32_t sjw = 2;
|
||||||
|
uint32_t qs;
|
||||||
|
// Find number of time quantas that gives us the exact wanted bit time
|
||||||
|
for (qs = 18; qs > 9; qs--) {
|
||||||
|
// check that bit_clocks / quantas is an integer
|
||||||
|
uint32_t brp_rem = bit_clocks % qs;
|
||||||
|
if (brp_rem == 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
uint32_t brp = bit_clocks / qs;
|
||||||
|
uint32_t time_seg2 = qs / 8; // sample at ~87.5%
|
||||||
|
uint32_t time_seg1 = qs - (1 + time_seg2);
|
||||||
|
|
||||||
|
return make_btr(sjw, time_seg1, time_seg2, brp);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
can_init(void)
|
||||||
|
{
|
||||||
|
enable_pclock((uint32_t)SOC_CAN);
|
||||||
|
|
||||||
|
gpio_peripheral(GPIO_Rx, CAN_FUNCTION, 1);
|
||||||
|
gpio_peripheral(GPIO_Tx, CAN_FUNCTION, 0);
|
||||||
|
|
||||||
|
uint32_t pclock = get_pclock_frequency((uint32_t)SOC_CAN);
|
||||||
|
|
||||||
|
uint32_t btr = compute_btr(pclock, CONFIG_CANBUS_FREQUENCY);
|
||||||
|
|
||||||
|
/*##-1- Configure the CAN #######################################*/
|
||||||
|
|
||||||
|
/* Exit from sleep mode */
|
||||||
|
SOC_CAN->CCCR &= ~FDCAN_CCCR_CSR;
|
||||||
|
/* Wait the acknowledge */
|
||||||
|
while (SOC_CAN->CCCR & FDCAN_CCCR_CSA)
|
||||||
|
;
|
||||||
|
/* Request initialisation */
|
||||||
|
SOC_CAN->CCCR |= FDCAN_CCCR_INIT;
|
||||||
|
/* Wait the acknowledge */
|
||||||
|
while (!(SOC_CAN->CCCR & FDCAN_CCCR_INIT))
|
||||||
|
;
|
||||||
|
/* Enable configuration change */
|
||||||
|
SOC_CAN->CCCR |= FDCAN_CCCR_CCE;
|
||||||
|
|
||||||
|
/* Disable protocol exception handling */
|
||||||
|
SOC_CAN->CCCR |= FDCAN_CCCR_PXHD;
|
||||||
|
|
||||||
|
SOC_CAN->NBTP = btr;
|
||||||
|
|
||||||
|
/* Leave the initialisation mode */
|
||||||
|
SOC_CAN->CCCR &= ~FDCAN_CCCR_CCE;
|
||||||
|
SOC_CAN->CCCR &= ~FDCAN_CCCR_INIT;
|
||||||
|
|
||||||
|
/*##-2- Configure the CAN Filter #######################################*/
|
||||||
|
canbus_set_filter(0);
|
||||||
|
|
||||||
|
/*##-3- Configure Interrupts #################################*/
|
||||||
|
armcm_enable_irq(CAN_IRQHandler, CAN_IT0_IRQn, 0);
|
||||||
|
SOC_CAN->ILE = FDCAN_ILE_EINT0;
|
||||||
|
SOC_CAN->IE = FDCAN_IE_RF0NE | FDCAN_IE_TC;
|
||||||
|
}
|
||||||
|
DECL_INIT(can_init);
|
||||||
@@ -30,6 +30,8 @@ lookup_clock_line(uint32_t periph_base)
|
|||||||
uint32_t bit = 1 << ((periph_base - AHBPERIPH_BASE) / 0x400);
|
uint32_t bit = 1 << ((periph_base - AHBPERIPH_BASE) / 0x400);
|
||||||
return (struct cline){.en=&RCC->AHBENR, .rst=&RCC->AHBRSTR, .bit=bit};
|
return (struct cline){.en=&RCC->AHBENR, .rst=&RCC->AHBRSTR, .bit=bit};
|
||||||
}
|
}
|
||||||
|
if ((periph_base == FDCAN1_BASE) || (periph_base == FDCAN2_BASE))
|
||||||
|
return (struct cline){.en=&RCC->APBENR1,.rst=&RCC->APBRSTR1,.bit=1<<12};
|
||||||
if (periph_base == USB_BASE)
|
if (periph_base == USB_BASE)
|
||||||
return (struct cline){.en=&RCC->APBENR1,.rst=&RCC->APBRSTR1,.bit=1<<13};
|
return (struct cline){.en=&RCC->APBENR1,.rst=&RCC->APBRSTR1,.bit=1<<13};
|
||||||
if (periph_base == CRS_BASE)
|
if (periph_base == CRS_BASE)
|
||||||
|
|||||||
Reference in new issue
Block a user