Skip to content

Commit

Permalink
FW 3.32: Configurable CAN baud rate
Browse files Browse the repository at this point in the history
  • Loading branch information
vedderb committed Nov 8, 2017
1 parent ab59b41 commit 5348662
Show file tree
Hide file tree
Showing 30 changed files with 50 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
=== FW 3.32 ===
* Added CAN-bus baud rate setting.

=== FW 3.31 ===
* Option to decrease temperature limits during acceleration to still have braking torque left.
* Added PID speed control mode to ADC app.
Expand Down
3 changes: 3 additions & 0 deletions appconf/appconf_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
#ifndef APPCONF_SEND_CAN_STATUS_RATE_HZ
#define APPCONF_SEND_CAN_STATUS_RATE_HZ 500
#endif
#ifndef APPCONF_CAN_BAUD_RATE
#define APPCONF_CAN_BAUD_RATE CAN_BAUD_500K
#endif

// The default app is UART in case the UART port is used for
// firmware updates.
Expand Down
5 changes: 5 additions & 0 deletions applications/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "hw.h"
#include "nrf_driver.h"
#include "rfhelp.h"
#include "comm_can.h"

// Private variables
static app_configuration appconf;
Expand All @@ -49,6 +50,10 @@ void app_set_configuration(app_configuration *conf) {
nrf_driver_stop();
}

#if CAN_ENABLE
comm_can_set_baud(conf->can_baud_rate);
#endif

#ifdef APP_CUSTOM_TO_USE
app_custom_stop();
#endif
Expand Down
Binary file modified build_all/410_o_411_o_412/VESC_0005ohm.bin
Binary file not shown.
Binary file modified build_all/410_o_411_o_412/VESC_default.bin
Binary file not shown.
Binary file modified build_all/410_o_411_o_412/VESC_default_no_hw_limits.bin
Binary file not shown.
Binary file modified build_all/410_o_411_o_412/VESC_servoout.bin
Binary file not shown.
Binary file modified build_all/410_o_411_o_412/VESC_ws2811.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_0005ohm.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_33k.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_default.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_servoout.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_ws2811.bin
Binary file not shown.
Binary file modified build_all/46_o_47/VESC_ws2811_33k.bin
Binary file not shown.
Binary file modified build_all/48/VESC_0005ohm.bin
Binary file not shown.
Binary file modified build_all/48/VESC_default.bin
Binary file not shown.
Binary file modified build_all/48/VESC_servoout.bin
Binary file not shown.
Binary file modified build_all/48/VESC_ws2811.bin
Binary file not shown.
Binary file modified build_all/60/VESC_default.bin
Binary file not shown.
Binary file modified build_all/60/VESC_default_no_hw_limits.bin
Binary file not shown.
Binary file modified build_all/60/VESC_servoout.bin
Binary file not shown.
Binary file modified build_all/60/VESC_ws2811.bin
Binary file not shown.
Binary file modified build_all/DAS_RS/VESC_default.bin
Binary file not shown.
24 changes: 22 additions & 2 deletions comm_can.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ static thread_t *process_tp;
*/
static const CANConfig cancfg = {
CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP,
CAN_BTR_SJW(0) | CAN_BTR_TS2(1) |
CAN_BTR_TS1(8) | CAN_BTR_BRP(6)
CAN_BTR_SJW(3) | CAN_BTR_TS2(2) |
CAN_BTR_TS1(9) | CAN_BTR_BRP(5)
};

// Private functions
static void send_packet_wrapper(unsigned char *data, unsigned int len);
static void set_timing(int brp, int ts1, int ts2);

// Function pointers
static void(*sid_callback)(uint32_t id, uint8_t *data, uint8_t len) = 0;
Expand Down Expand Up @@ -101,6 +102,16 @@ void comm_can_init(void) {
cancom_process_thread, NULL);
}

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;
}
}

static THD_FUNCTION(cancom_read_thread, arg) {
(void)arg;
chRegSetThreadName("CAN");
Expand Down Expand Up @@ -567,3 +578,12 @@ can_status_msg *comm_can_get_status_msg_id(int id) {
static void send_packet_wrapper(unsigned char *data, unsigned int len) {
comm_can_send_buffer(rx_buffer_last_id, data, len, true);
}

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);
}
1 change: 1 addition & 0 deletions comm_can.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

// Functions
void comm_can_init(void);
void comm_can_set_baud(CAN_BAUD baud);
void comm_can_transmit_eid(uint32_t id, uint8_t *data, uint8_t len);
void comm_can_transmit_sid(uint32_t id, uint8_t *data, uint8_t len);
void comm_can_send_buffer(uint8_t controller_id, uint8_t *data, unsigned int len, bool send);
Expand Down
2 changes: 2 additions & 0 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ void commands_process_packet(unsigned char *data, unsigned int len) {
appconf.timeout_brake_current = buffer_get_float32_auto(data, &ind);
appconf.send_can_status = data[ind++];
appconf.send_can_status_rate_hz = buffer_get_uint16(data, &ind);
appconf.can_baud_rate = data[ind++];

appconf.app_to_use = data[ind++];

Expand Down Expand Up @@ -926,6 +927,7 @@ void commands_send_appconf(COMM_PACKET_ID packet_id, app_configuration *appconf)
buffer_append_float32_auto(send_buffer, appconf->timeout_brake_current, &ind);
send_buffer[ind++] = appconf->send_can_status;
buffer_append_uint16(send_buffer, appconf->send_can_status_rate_hz, &ind);
send_buffer[ind++] = appconf->can_baud_rate;

send_buffer[ind++] = appconf->app_to_use;

Expand Down
1 change: 1 addition & 0 deletions conf_general.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void conf_general_get_default_app_configuration(app_configuration *conf) {
conf->timeout_brake_current = APPCONF_TIMEOUT_BRAKE_CURRENT;
conf->send_can_status = APPCONF_SEND_CAN_STATUS;
conf->send_can_status_rate_hz = APPCONF_SEND_CAN_STATUS_RATE_HZ;
conf->can_baud_rate = APPCONF_CAN_BAUD_RATE;

conf->app_to_use = APPCONF_APP_TO_USE;

Expand Down
2 changes: 1 addition & 1 deletion conf_general.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

// Firmware version
#define FW_VERSION_MAJOR 3
#define FW_VERSION_MINOR 31
#define FW_VERSION_MINOR 32

#include "datatypes.h"

Expand Down
8 changes: 8 additions & 0 deletions datatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ typedef enum {
DEBUG_SAMPLING_SEND_LAST_SAMPLES
} debug_sampling_mode;

typedef enum {
CAN_BAUD_125K = 0,
CAN_BAUD_250K,
CAN_BAUD_500K,
CAN_BAUD_1M
} CAN_BAUD;

typedef struct {
// Switching and drive
mc_pwm_mode pwm_mode;
Expand Down Expand Up @@ -414,6 +421,7 @@ typedef struct {
float timeout_brake_current;
bool send_can_status;
uint32_t send_can_status_rate_hz;
CAN_BAUD can_baud_rate;

// Application to use
app_use app_to_use;
Expand Down
8 changes: 4 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ int main(void) {
commands_init();
comm_usb_init();

#if CAN_ENABLE
comm_can_init();
#endif

app_configuration appconf;
conf_general_read_app_configuration(&appconf);
app_set_configuration(&appconf);
Expand All @@ -227,10 +231,6 @@ int main(void) {
timeout_init();
timeout_configure(appconf.timeout_msec, appconf.timeout_brake_current);

#if CAN_ENABLE
comm_can_init();
#endif

#if WS2811_ENABLE
ws2811_init();
#if !WS2811_TEST
Expand Down

0 comments on commit 5348662

Please sign in to comment.