2014-12-06 19:30:25 -08:00
|
|
|
/*
|
2016-11-04 07:18:34 -07:00
|
|
|
Copyright 2016 Benjamin Vedder benjamin@vedder.se
|
2014-12-06 19:30:25 -08:00
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
This file is part of the VESC firmware.
|
|
|
|
|
|
|
|
The VESC firmware is free software: you can redistribute it and/or modify
|
2014-12-06 19:30:25 -08:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
2016-11-04 07:18:34 -07:00
|
|
|
The VESC firmware is distributed in the hope that it will be useful,
|
2014-12-06 19:30:25 -08:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-11-04 07:18:34 -07:00
|
|
|
*/
|
2014-12-06 19:30:25 -08:00
|
|
|
|
2015-04-26 15:02:32 -07:00
|
|
|
#include <string.h>
|
2017-09-04 12:12:43 -07:00
|
|
|
#include <math.h>
|
2014-12-06 19:30:25 -08:00
|
|
|
#include "comm_can.h"
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
2015-10-08 14:09:39 -07:00
|
|
|
#include "stm32f4xx_conf.h"
|
2014-12-06 19:30:25 -08:00
|
|
|
#include "datatypes.h"
|
|
|
|
#include "buffer.h"
|
2015-12-08 12:01:23 -08:00
|
|
|
#include "mc_interface.h"
|
2014-12-06 19:30:25 -08:00
|
|
|
#include "timeout.h"
|
|
|
|
#include "commands.h"
|
2015-02-19 12:20:07 -08:00
|
|
|
#include "app.h"
|
2015-04-26 15:02:32 -07:00
|
|
|
#include "crc.h"
|
2015-05-20 07:13:48 -07:00
|
|
|
#include "packet.h"
|
2014-12-06 19:30:25 -08:00
|
|
|
|
|
|
|
// Settings
|
2015-02-19 12:20:07 -08:00
|
|
|
#define CANDx CAND1
|
2015-05-20 07:13:48 -07:00
|
|
|
#define RX_FRAMES_SIZE 100
|
|
|
|
#define RX_BUFFER_SIZE PACKET_MAX_PL_LEN
|
2014-12-06 19:30:25 -08:00
|
|
|
|
|
|
|
// Threads
|
2015-10-08 14:09:39 -07:00
|
|
|
static THD_WORKING_AREA(cancom_read_thread_wa, 512);
|
|
|
|
static THD_WORKING_AREA(cancom_process_thread_wa, 4096);
|
|
|
|
static THD_WORKING_AREA(cancom_status_thread_wa, 1024);
|
|
|
|
static THD_FUNCTION(cancom_read_thread, arg);
|
|
|
|
static THD_FUNCTION(cancom_status_thread, arg);
|
|
|
|
static THD_FUNCTION(cancom_process_thread, arg);
|
2015-02-19 12:20:07 -08:00
|
|
|
|
|
|
|
// Variables
|
|
|
|
static can_status_msg stat_msgs[CAN_STATUS_MSGS_TO_STORE];
|
2015-10-08 14:09:39 -07:00
|
|
|
static mutex_t can_mtx;
|
2015-05-20 07:13:48 -07:00
|
|
|
static uint8_t rx_buffer[RX_BUFFER_SIZE];
|
|
|
|
static unsigned int rx_buffer_last_id;
|
2015-05-08 13:53:59 -07:00
|
|
|
static CANRxFrame rx_frames[RX_FRAMES_SIZE];
|
|
|
|
static int rx_frame_read;
|
|
|
|
static int rx_frame_write;
|
2015-10-08 14:09:39 -07:00
|
|
|
static thread_t *process_tp;
|
2014-12-06 19:30:25 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 500KBaud, automatic wakeup, automatic recover
|
|
|
|
* from abort mode.
|
|
|
|
* See section 22.7.7 on the STM32 reference manual.
|
|
|
|
*/
|
|
|
|
static const CANConfig cancfg = {
|
2015-02-19 12:20:07 -08:00
|
|
|
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
|
2017-11-08 05:07:35 -08:00
|
|
|
CAN_BTR_SJW(3) | CAN_BTR_TS2(2) |
|
|
|
|
CAN_BTR_TS1(9) | CAN_BTR_BRP(5)
|
2014-12-06 19:30:25 -08:00
|
|
|
};
|
|
|
|
|
2015-04-26 15:02:32 -07:00
|
|
|
// Private functions
|
2015-05-20 07:13:48 -07:00
|
|
|
static void send_packet_wrapper(unsigned char *data, unsigned int len);
|
2017-11-08 05:07:35 -08:00
|
|
|
static void set_timing(int brp, int ts1, int ts2);
|
2015-04-26 15:02:32 -07:00
|
|
|
|
2017-10-28 02:06:21 -07:00
|
|
|
// Function pointers
|
|
|
|
static void(*sid_callback)(uint32_t id, uint8_t *data, uint8_t len) = 0;
|
|
|
|
|
2014-12-06 19:30:25 -08:00
|
|
|
void comm_can_init(void) {
|
2015-02-19 12:20:07 -08:00
|
|
|
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
|
|
|
|
stat_msgs[i].id = -1;
|
|
|
|
}
|
|
|
|
|
2015-05-08 13:53:59 -07:00
|
|
|
rx_frame_read = 0;
|
|
|
|
rx_frame_write = 0;
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
chMtxObjectInit(&can_mtx);
|
2015-02-19 12:20:07 -08:00
|
|
|
|
2014-12-06 19:30:25 -08:00
|
|
|
palSetPadMode(GPIOB, 8,
|
|
|
|
PAL_MODE_ALTERNATE(GPIO_AF_CAN1) |
|
|
|
|
PAL_STM32_OTYPE_PUSHPULL |
|
|
|
|
PAL_STM32_OSPEED_MID1);
|
|
|
|
palSetPadMode(GPIOB, 9,
|
|
|
|
PAL_MODE_ALTERNATE(GPIO_AF_CAN1) |
|
|
|
|
PAL_STM32_OTYPE_PUSHPULL |
|
|
|
|
PAL_STM32_OSPEED_MID1);
|
|
|
|
|
|
|
|
canStart(&CANDx, &cancfg);
|
|
|
|
|
2015-05-08 13:53:59 -07:00
|
|
|
chThdCreateStatic(cancom_read_thread_wa, sizeof(cancom_read_thread_wa), NORMALPRIO + 1,
|
|
|
|
cancom_read_thread, NULL);
|
2015-02-19 12:20:07 -08:00
|
|
|
chThdCreateStatic(cancom_status_thread_wa, sizeof(cancom_status_thread_wa), NORMALPRIO,
|
|
|
|
cancom_status_thread, NULL);
|
2015-05-08 13:53:59 -07:00
|
|
|
chThdCreateStatic(cancom_process_thread_wa, sizeof(cancom_process_thread_wa), NORMALPRIO,
|
|
|
|
cancom_process_thread, NULL);
|
2014-12-06 19:30:25 -08:00
|
|
|
}
|
|
|
|
|
2017-11-08 05:07:35 -08:00
|
|
|
void comm_can_set_baud(CAN_BAUD baud) {
|
|
|
|
switch (baud) {
|
|
|
|
case CAN_BAUD_125K: set_timing(15, 14, 4); break;
|
|
|
|
case CAN_BAUD_250K: set_timing(7, 14, 4); break;
|
|
|
|
case CAN_BAUD_500K: set_timing(5, 9, 2); break;
|
|
|
|
case CAN_BAUD_1M: set_timing(2, 9, 2); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
static THD_FUNCTION(cancom_read_thread, arg) {
|
2014-12-06 19:30:25 -08:00
|
|
|
(void)arg;
|
|
|
|
chRegSetThreadName("CAN");
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
event_listener_t el;
|
2014-12-06 19:30:25 -08:00
|
|
|
CANRxFrame rxmsg;
|
|
|
|
|
|
|
|
chEvtRegister(&CANDx.rxfull_event, &el, 0);
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
while(!chThdShouldTerminateX()) {
|
2015-02-19 12:20:07 -08:00
|
|
|
if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(10)) == 0) {
|
2014-12-06 19:30:25 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-02-19 12:20:07 -08:00
|
|
|
msg_t result = canReceive(&CANDx, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE);
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
while (result == MSG_OK) {
|
2015-05-08 13:53:59 -07:00
|
|
|
rx_frames[rx_frame_write++] = rxmsg;
|
|
|
|
if (rx_frame_write == RX_FRAMES_SIZE) {
|
|
|
|
rx_frame_write = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
chEvtSignal(process_tp, (eventmask_t) 1);
|
|
|
|
|
|
|
|
result = canReceive(&CANDx, CAN_ANY_MAILBOX, &rxmsg, TIME_IMMEDIATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chEvtUnregister(&CANDx.rxfull_event, &el);
|
|
|
|
}
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
static THD_FUNCTION(cancom_process_thread, arg) {
|
2015-05-08 13:53:59 -07:00
|
|
|
(void)arg;
|
|
|
|
|
|
|
|
chRegSetThreadName("Cancom process");
|
2015-10-08 14:09:39 -07:00
|
|
|
process_tp = chThdGetSelfX();
|
2015-05-08 13:53:59 -07:00
|
|
|
|
|
|
|
int32_t ind = 0;
|
2015-05-20 07:13:48 -07:00
|
|
|
unsigned int rxbuf_len;
|
|
|
|
unsigned int rxbuf_ind;
|
2015-05-08 13:53:59 -07:00
|
|
|
uint8_t crc_low;
|
|
|
|
uint8_t crc_high;
|
|
|
|
bool commands_send;
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
chEvtWaitAny((eventmask_t) 1);
|
|
|
|
|
|
|
|
while (rx_frame_read != rx_frame_write) {
|
|
|
|
CANRxFrame rxmsg = rx_frames[rx_frame_read++];
|
|
|
|
|
2014-12-06 19:30:25 -08:00
|
|
|
if (rxmsg.IDE == CAN_IDE_EXT) {
|
2015-02-19 12:20:07 -08:00
|
|
|
uint8_t id = rxmsg.EID & 0xFF;
|
2014-12-06 19:30:25 -08:00
|
|
|
CAN_PACKET_ID cmd = rxmsg.EID >> 8;
|
2015-02-19 12:20:07 -08:00
|
|
|
can_status_msg *stat_tmp;
|
2014-12-06 19:30:25 -08:00
|
|
|
|
2015-02-19 12:20:07 -08:00
|
|
|
if (id == 255 || id == app_get_configuration()->controller_id) {
|
|
|
|
switch (cmd) {
|
|
|
|
case CAN_PACKET_SET_DUTY:
|
|
|
|
ind = 0;
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_duty((float)buffer_get_int32(rxmsg.data8, &ind) / 100000.0);
|
2015-02-19 12:20:07 -08:00
|
|
|
timeout_reset();
|
|
|
|
break;
|
2014-12-06 19:30:25 -08:00
|
|
|
|
2015-02-19 12:20:07 -08:00
|
|
|
case CAN_PACKET_SET_CURRENT:
|
|
|
|
ind = 0;
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_current((float)buffer_get_int32(rxmsg.data8, &ind) / 1000.0);
|
2015-02-19 12:20:07 -08:00
|
|
|
timeout_reset();
|
|
|
|
break;
|
2014-12-06 19:30:25 -08:00
|
|
|
|
2015-02-19 12:20:07 -08:00
|
|
|
case CAN_PACKET_SET_CURRENT_BRAKE:
|
|
|
|
ind = 0;
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_brake_current((float)buffer_get_int32(rxmsg.data8, &ind) / 1000.0);
|
2015-02-19 12:20:07 -08:00
|
|
|
timeout_reset();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CAN_PACKET_SET_RPM:
|
|
|
|
ind = 0;
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_pid_speed((float)buffer_get_int32(rxmsg.data8, &ind));
|
2015-02-19 12:20:07 -08:00
|
|
|
timeout_reset();
|
|
|
|
break;
|
2014-12-06 19:30:25 -08:00
|
|
|
|
2015-04-26 15:02:32 -07:00
|
|
|
case CAN_PACKET_SET_POS:
|
|
|
|
ind = 0;
|
2015-12-08 12:01:23 -08:00
|
|
|
mc_interface_set_pid_pos((float)buffer_get_int32(rxmsg.data8, &ind) / 1000000.0);
|
2015-04-26 15:02:32 -07:00
|
|
|
timeout_reset();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CAN_PACKET_FILL_RX_BUFFER:
|
|
|
|
memcpy(rx_buffer + rxmsg.data8[0], rxmsg.data8 + 1, rxmsg.DLC - 1);
|
|
|
|
break;
|
|
|
|
|
2015-05-20 07:13:48 -07:00
|
|
|
case CAN_PACKET_FILL_RX_BUFFER_LONG:
|
|
|
|
rxbuf_ind = (unsigned int)rxmsg.data8[0] << 8;
|
|
|
|
rxbuf_ind |= rxmsg.data8[1];
|
|
|
|
if (rxbuf_ind < RX_BUFFER_SIZE) {
|
|
|
|
memcpy(rx_buffer + rxbuf_ind, rxmsg.data8 + 2, rxmsg.DLC - 2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-04-26 15:02:32 -07:00
|
|
|
case CAN_PACKET_PROCESS_RX_BUFFER:
|
|
|
|
ind = 0;
|
|
|
|
rx_buffer_last_id = rxmsg.data8[ind++];
|
|
|
|
commands_send = rxmsg.data8[ind++];
|
2015-05-20 07:13:48 -07:00
|
|
|
rxbuf_len = (unsigned int)rxmsg.data8[ind++] << 8;
|
|
|
|
rxbuf_len |= (unsigned int)rxmsg.data8[ind++];
|
|
|
|
|
|
|
|
if (rxbuf_len > RX_BUFFER_SIZE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:02:32 -07:00
|
|
|
crc_high = rxmsg.data8[ind++];
|
|
|
|
crc_low = rxmsg.data8[ind++];
|
|
|
|
|
|
|
|
if (crc16(rx_buffer, rxbuf_len)
|
|
|
|
== ((unsigned short) crc_high << 8
|
|
|
|
| (unsigned short) crc_low)) {
|
|
|
|
|
|
|
|
if (commands_send) {
|
|
|
|
commands_send_packet(rx_buffer, rxbuf_len);
|
|
|
|
} else {
|
|
|
|
commands_set_send_func(send_packet_wrapper);
|
|
|
|
commands_process_packet(rx_buffer, rxbuf_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-04-27 11:43:02 -07:00
|
|
|
case CAN_PACKET_PROCESS_SHORT_BUFFER:
|
|
|
|
ind = 0;
|
|
|
|
rx_buffer_last_id = rxmsg.data8[ind++];
|
|
|
|
commands_send = rxmsg.data8[ind++];
|
|
|
|
|
|
|
|
if (commands_send) {
|
|
|
|
commands_send_packet(rxmsg.data8 + ind, rxmsg.DLC - ind);
|
|
|
|
} else {
|
|
|
|
commands_set_send_func(send_packet_wrapper);
|
|
|
|
commands_process_packet(rxmsg.data8 + ind, rxmsg.DLC - ind);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-09-04 12:12:43 -07:00
|
|
|
case CAN_PACKET_SET_CURRENT_REL:
|
|
|
|
ind = 0;
|
|
|
|
mc_interface_set_current_rel(buffer_get_float32(rxmsg.data8, 1e5, &ind));
|
|
|
|
timeout_reset();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CAN_PACKET_SET_CURRENT_BRAKE_REL:
|
|
|
|
ind = 0;
|
|
|
|
mc_interface_set_brake_current_rel(buffer_get_float32(rxmsg.data8, 1e5, &ind));
|
|
|
|
timeout_reset();
|
|
|
|
break;
|
|
|
|
|
2015-02-19 12:20:07 -08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case CAN_PACKET_STATUS:
|
|
|
|
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
|
|
|
|
stat_tmp = &stat_msgs[i];
|
|
|
|
if (stat_tmp->id == id || stat_tmp->id == -1) {
|
|
|
|
ind = 0;
|
|
|
|
stat_tmp->id = id;
|
2015-10-08 14:09:39 -07:00
|
|
|
stat_tmp->rx_time = chVTGetSystemTime();
|
2015-02-19 12:20:07 -08:00
|
|
|
stat_tmp->rpm = (float)buffer_get_int32(rxmsg.data8, &ind);
|
|
|
|
stat_tmp->current = (float)buffer_get_int16(rxmsg.data8, &ind) / 10.0;
|
|
|
|
stat_tmp->duty = (float)buffer_get_int16(rxmsg.data8, &ind) / 1000.0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-12-06 19:30:25 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-10-28 02:06:21 -07:00
|
|
|
} else {
|
|
|
|
if (sid_callback) {
|
|
|
|
sid_callback(rxmsg.SID, rxmsg.data8, rxmsg.DLC);
|
|
|
|
}
|
2014-12-06 19:30:25 -08:00
|
|
|
}
|
2015-02-19 12:20:07 -08:00
|
|
|
|
2015-05-08 13:53:59 -07:00
|
|
|
if (rx_frame_read == RX_FRAMES_SIZE) {
|
|
|
|
rx_frame_read = 0;
|
|
|
|
}
|
2014-12-06 19:30:25 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
static THD_FUNCTION(cancom_status_thread, arg) {
|
2015-02-19 12:20:07 -08:00
|
|
|
(void)arg;
|
|
|
|
chRegSetThreadName("CAN status");
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if (app_get_configuration()->send_can_status) {
|
|
|
|
// Send status message
|
|
|
|
int32_t send_index = 0;
|
|
|
|
uint8_t buffer[8];
|
2015-12-08 12:01:23 -08:00
|
|
|
buffer_append_int32(buffer, (int32_t)mc_interface_get_rpm(), &send_index);
|
|
|
|
buffer_append_int16(buffer, (int16_t)(mc_interface_get_tot_current() * 10.0), &send_index);
|
|
|
|
buffer_append_int16(buffer, (int16_t)(mc_interface_get_duty_cycle_now() * 1000.0), &send_index);
|
2017-09-04 12:12:43 -07:00
|
|
|
comm_can_transmit_eid(app_get_configuration()->controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_STATUS << 8), buffer, send_index);
|
2015-02-19 12:20:07 -08:00
|
|
|
}
|
|
|
|
|
2015-10-08 14:09:39 -07:00
|
|
|
systime_t sleep_time = CH_CFG_ST_FREQUENCY / app_get_configuration()->send_can_status_rate_hz;
|
2015-05-04 14:25:43 -07:00
|
|
|
if (sleep_time == 0) {
|
|
|
|
sleep_time = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
chThdSleep(sleep_time);
|
2015-02-19 12:20:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:12:43 -07:00
|
|
|
void comm_can_transmit_eid(uint32_t id, uint8_t *data, uint8_t len) {
|
2017-10-28 02:06:21 -07:00
|
|
|
if (len > 8) {
|
|
|
|
len = 8;
|
|
|
|
}
|
|
|
|
|
2015-04-27 11:43:02 -07:00
|
|
|
#if CAN_ENABLE
|
2015-05-08 13:53:59 -07:00
|
|
|
CANTxFrame txmsg;
|
|
|
|
txmsg.IDE = CAN_IDE_EXT;
|
|
|
|
txmsg.EID = id;
|
|
|
|
txmsg.RTR = CAN_RTR_DATA;
|
|
|
|
txmsg.DLC = len;
|
|
|
|
memcpy(txmsg.data8, data, len);
|
2014-12-06 19:30:25 -08:00
|
|
|
|
2015-05-08 13:53:59 -07:00
|
|
|
chMtxLock(&can_mtx);
|
|
|
|
canTransmit(&CANDx, CAN_ANY_MAILBOX, &txmsg, MS2ST(20));
|
2015-10-08 14:09:39 -07:00
|
|
|
chMtxUnlock(&can_mtx);
|
2015-05-08 13:53:59 -07:00
|
|
|
|
2015-04-27 11:43:02 -07:00
|
|
|
#else
|
|
|
|
(void)id;
|
|
|
|
(void)data;
|
|
|
|
(void)len;
|
|
|
|
#endif
|
2014-12-06 19:30:25 -08:00
|
|
|
}
|
|
|
|
|
2017-10-28 02:06:21 -07:00
|
|
|
void comm_can_transmit_sid(uint32_t id, uint8_t *data, uint8_t len) {
|
|
|
|
if (len > 8) {
|
|
|
|
len = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CAN_ENABLE
|
|
|
|
CANTxFrame txmsg;
|
|
|
|
txmsg.IDE = CAN_IDE_STD;
|
|
|
|
txmsg.SID = id;
|
|
|
|
txmsg.RTR = CAN_RTR_DATA;
|
|
|
|
txmsg.DLC = len;
|
|
|
|
memcpy(txmsg.data8, data, len);
|
|
|
|
|
|
|
|
chMtxLock(&can_mtx);
|
|
|
|
canTransmit(&CANDx, CAN_ANY_MAILBOX, &txmsg, MS2ST(20));
|
|
|
|
chMtxUnlock(&can_mtx);
|
|
|
|
|
|
|
|
#else
|
|
|
|
(void)id;
|
|
|
|
(void)data;
|
|
|
|
(void)len;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set function to be called when standard CAN frames are received.
|
|
|
|
*
|
|
|
|
* @param p_func
|
|
|
|
* Pointer to the function.
|
|
|
|
*/
|
|
|
|
void comm_can_set_sid_rx_callback(void (*p_func)(uint32_t id, uint8_t *data, uint8_t len)) {
|
|
|
|
sid_callback = p_func;
|
|
|
|
}
|
|
|
|
|
2015-04-26 15:02:32 -07:00
|
|
|
/**
|
2015-05-20 07:13:48 -07:00
|
|
|
* Send a buffer up to RX_BUFFER_SIZE bytes as fragments. If the buffer is 6 bytes or less
|
2015-04-27 11:43:02 -07:00
|
|
|
* it will be sent in a single CAN frame, otherwise it will be split into
|
|
|
|
* several frames.
|
2015-04-26 15:02:32 -07:00
|
|
|
*
|
|
|
|
* @param controller_id
|
|
|
|
* The controller id to send to.
|
|
|
|
*
|
|
|
|
* @param data
|
|
|
|
* The payload.
|
|
|
|
*
|
|
|
|
* @param len
|
|
|
|
* The payload length.
|
|
|
|
*
|
|
|
|
* @param send
|
|
|
|
* If true, this packet will be passed to the send function of commands.
|
|
|
|
* Otherwise, it will be passed to the process function.
|
|
|
|
*/
|
2015-05-20 07:13:48 -07:00
|
|
|
void comm_can_send_buffer(uint8_t controller_id, uint8_t *data, unsigned int len, bool send) {
|
2015-04-26 15:02:32 -07:00
|
|
|
uint8_t send_buffer[8];
|
|
|
|
|
2015-04-27 11:43:02 -07:00
|
|
|
if (len <= 6) {
|
|
|
|
uint32_t ind = 0;
|
|
|
|
send_buffer[ind++] = app_get_configuration()->controller_id;
|
|
|
|
send_buffer[ind++] = send;
|
|
|
|
memcpy(send_buffer + ind, data, len);
|
|
|
|
ind += len;
|
2017-09-04 12:12:43 -07:00
|
|
|
comm_can_transmit_eid(controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_PROCESS_SHORT_BUFFER << 8), send_buffer, ind);
|
2015-04-27 11:43:02 -07:00
|
|
|
} else {
|
2015-05-20 07:13:48 -07:00
|
|
|
unsigned int end_a = 0;
|
|
|
|
for (unsigned int i = 0;i < len;i += 7) {
|
2015-12-27 16:50:23 -08:00
|
|
|
if (i > 255) {
|
2015-05-20 07:13:48 -07:00
|
|
|
break;
|
|
|
|
}
|
2015-04-27 11:43:02 -07:00
|
|
|
|
2016-01-24 04:44:51 -08:00
|
|
|
end_a = i + 7;
|
|
|
|
|
2015-05-20 07:13:48 -07:00
|
|
|
uint8_t send_len = 7;
|
2015-04-27 11:43:02 -07:00
|
|
|
send_buffer[0] = i;
|
|
|
|
|
|
|
|
if ((i + 7) <= len) {
|
|
|
|
memcpy(send_buffer + 1, data + i, send_len);
|
|
|
|
} else {
|
|
|
|
send_len = len - i;
|
|
|
|
memcpy(send_buffer + 1, data + i, send_len);
|
|
|
|
}
|
2015-04-26 15:02:32 -07:00
|
|
|
|
2017-09-04 12:12:43 -07:00
|
|
|
comm_can_transmit_eid(controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_FILL_RX_BUFFER << 8), send_buffer, send_len + 1);
|
2015-04-26 15:02:32 -07:00
|
|
|
}
|
|
|
|
|
2015-05-20 07:13:48 -07:00
|
|
|
for (unsigned int i = end_a;i < len;i += 6) {
|
|
|
|
uint8_t send_len = 6;
|
|
|
|
send_buffer[0] = i >> 8;
|
|
|
|
send_buffer[1] = i & 0xFF;
|
|
|
|
|
|
|
|
if ((i + 6) <= len) {
|
|
|
|
memcpy(send_buffer + 2, data + i, send_len);
|
|
|
|
} else {
|
|
|
|
send_len = len - i;
|
|
|
|
memcpy(send_buffer + 2, data + i, send_len);
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:12:43 -07:00
|
|
|
comm_can_transmit_eid(controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_FILL_RX_BUFFER_LONG << 8), send_buffer, send_len + 2);
|
2015-05-20 07:13:48 -07:00
|
|
|
}
|
|
|
|
|
2015-04-27 11:43:02 -07:00
|
|
|
uint32_t ind = 0;
|
|
|
|
send_buffer[ind++] = app_get_configuration()->controller_id;
|
|
|
|
send_buffer[ind++] = send;
|
2015-05-20 07:13:48 -07:00
|
|
|
send_buffer[ind++] = len >> 8;
|
|
|
|
send_buffer[ind++] = len & 0xFF;
|
2015-04-27 11:43:02 -07:00
|
|
|
unsigned short crc = crc16(data, len);
|
|
|
|
send_buffer[ind++] = (uint8_t)(crc >> 8);
|
|
|
|
send_buffer[ind++] = (uint8_t)(crc & 0xFF);
|
2015-04-26 15:02:32 -07:00
|
|
|
|
2017-09-04 12:12:43 -07:00
|
|
|
comm_can_transmit_eid(controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_PROCESS_RX_BUFFER << 8), send_buffer, ind++);
|
2015-04-27 11:43:02 -07:00
|
|
|
}
|
2015-04-26 15:02:32 -07:00
|
|
|
}
|
|
|
|
|
2014-12-06 19:30:25 -08:00
|
|
|
void comm_can_set_duty(uint8_t controller_id, float duty) {
|
|
|
|
int32_t send_index = 0;
|
|
|
|
uint8_t buffer[4];
|
|
|
|
buffer_append_int32(buffer, (int32_t)(duty * 100000.0), &send_index);
|
2017-09-04 12:12:43 -07:00
|
|
|
comm_can_transmit_eid(controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_SET_DUTY << 8), buffer, send_index);
|
2014-12-06 19:30:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void comm_can_set_current(uint8_t controller_id, float current) {
|
|
|
|
int32_t send_index = 0;
|
|
|
|
uint8_t buffer[4];
|
|
|
|
buffer_append_int32(buffer, (int32_t)(current * 1000.0), &send_index);
|
2017-09-04 12:12:43 -07:00
|
|
|
comm_can_transmit_eid(controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_SET_CURRENT << 8), buffer, send_index);
|
2014-12-06 19:30:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void comm_can_set_current_brake(uint8_t controller_id, float current) {
|
|
|
|
int32_t send_index = 0;
|
|
|
|
uint8_t buffer[4];
|
|
|
|
buffer_append_int32(buffer, (int32_t)(current * 1000.0), &send_index);
|
2017-09-04 12:12:43 -07:00
|
|
|
comm_can_transmit_eid(controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_SET_CURRENT_BRAKE << 8), buffer, send_index);
|
2014-12-06 19:30:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void comm_can_set_rpm(uint8_t controller_id, float rpm) {
|
|
|
|
int32_t send_index = 0;
|
|
|
|
uint8_t buffer[4];
|
|
|
|
buffer_append_int32(buffer, (int32_t)rpm, &send_index);
|
2017-09-04 12:12:43 -07:00
|
|
|
comm_can_transmit_eid(controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_SET_RPM << 8), buffer, send_index);
|
2014-12-06 19:30:25 -08:00
|
|
|
}
|
2015-02-19 12:20:07 -08:00
|
|
|
|
2015-04-26 15:02:32 -07:00
|
|
|
void comm_can_set_pos(uint8_t controller_id, float pos) {
|
|
|
|
int32_t send_index = 0;
|
|
|
|
uint8_t buffer[4];
|
|
|
|
buffer_append_int32(buffer, (int32_t)(pos * 1000000.0), &send_index);
|
2017-09-04 12:12:43 -07:00
|
|
|
comm_can_transmit_eid(controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_SET_POS << 8), buffer, send_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set current relative to the minimum and maximum current limits.
|
|
|
|
*
|
|
|
|
* @param controller_id
|
|
|
|
* The ID of the VESC to set the current on.
|
|
|
|
*
|
|
|
|
* @param current_rel
|
|
|
|
* The relative current value, range [-1.0 1.0]
|
|
|
|
*/
|
|
|
|
void comm_can_set_current_rel(uint8_t controller_id, float current_rel) {
|
|
|
|
int32_t send_index = 0;
|
|
|
|
uint8_t buffer[4];
|
|
|
|
buffer_append_float32(buffer, current_rel, 1e5, &send_index);
|
|
|
|
comm_can_transmit_eid(controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_SET_CURRENT_REL << 8), buffer, send_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set brake current relative to the minimum current limit.
|
|
|
|
*
|
|
|
|
* @param controller_id
|
|
|
|
* The ID of the VESC to set the current on.
|
|
|
|
*
|
|
|
|
* @param current_rel
|
|
|
|
* The relative current value, range [0.0 1.0]
|
|
|
|
*/
|
|
|
|
void comm_can_set_current_brake_rel(uint8_t controller_id, float current_rel) {
|
|
|
|
int32_t send_index = 0;
|
|
|
|
uint8_t buffer[4];
|
|
|
|
buffer_append_float32(buffer, current_rel, 1e5, &send_index);
|
|
|
|
comm_can_transmit_eid(controller_id |
|
|
|
|
((uint32_t)CAN_PACKET_SET_CURRENT_BRAKE_REL << 8), buffer, send_index);
|
2015-04-26 15:02:32 -07:00
|
|
|
}
|
|
|
|
|
2015-02-19 12:20:07 -08:00
|
|
|
/**
|
|
|
|
* Get status message by index.
|
|
|
|
*
|
|
|
|
* @param index
|
|
|
|
* Index in the array
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The message or 0 for an invalid index.
|
|
|
|
*/
|
|
|
|
can_status_msg *comm_can_get_status_msg_index(int index) {
|
|
|
|
if (index < CAN_STATUS_MSGS_TO_STORE) {
|
|
|
|
return &stat_msgs[index];
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get status message by id.
|
|
|
|
*
|
|
|
|
* @param id
|
|
|
|
* Id of the controller that sent the status message.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* The message or 0 for an invalid id.
|
|
|
|
*/
|
|
|
|
can_status_msg *comm_can_get_status_msg_id(int id) {
|
|
|
|
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
|
|
|
|
if (stat_msgs[i].id == id) {
|
|
|
|
return &stat_msgs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-04-26 15:02:32 -07:00
|
|
|
|
2015-05-20 07:13:48 -07:00
|
|
|
static void send_packet_wrapper(unsigned char *data, unsigned int len) {
|
2015-04-26 15:02:32 -07:00
|
|
|
comm_can_send_buffer(rx_buffer_last_id, data, len, true);
|
|
|
|
}
|
2017-11-08 05:07:35 -08:00
|
|
|
|
|
|
|
static void set_timing(int brp, int ts1, int ts2) {
|
|
|
|
brp &= 0b1111111111;
|
|
|
|
ts1 &= 0b1111;
|
|
|
|
ts2 &= 0b111;
|
|
|
|
|
|
|
|
CANDx.can->BTR = CAN_BTR_SJW(3) | CAN_BTR_TS2(ts2) |
|
|
|
|
CAN_BTR_TS1(ts1) | CAN_BTR_BRP(brp);
|
|
|
|
}
|