Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/firmware/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,18 @@ void app_set_lights(bool on)
}
}

void app_set_speed_limit_operation_mode(uint16_t display_speed_limit_rpm)
{
if (display_speed_limit_rpm == convert_wheel_speed_kph_to_rpm(SPEED_LIMIT_SPORT_SWITCH_KPH))
{
app_set_operation_mode(OPERATION_MODE_SPORT);
}
else
{
app_set_operation_mode(OPERATION_MODE_DEFAULT);
}
}

void app_set_operation_mode(uint8_t mode)
{
if (operation_mode != mode)
Expand Down Expand Up @@ -546,7 +558,7 @@ bool apply_speed_limit(uint8_t* target_current, uint8_t throttle_percent, bool p

// global throttle speed limit applies if enabled in configuration, PAS is not engaged and throttle is used
bool global_throttle_limit_active =
!pas_engaged &&
!pas_engaged &&
throttle_percent > 0 &&
g_config.throttle_global_spd_lim_percent > 0 &&
(
Expand Down
1 change: 1 addition & 0 deletions src/firmware/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void app_set_lights(bool on);

void app_set_operation_mode(uint8_t mode);
void app_set_wheel_max_speed_rpm(uint16_t value);
void app_set_speed_limit_operation_mode(uint16_t display_speed_limit_rpm);

uint8_t app_get_assist_level();
uint8_t app_get_lights();
Expand Down
175 changes: 91 additions & 84 deletions src/firmware/extcom.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "app.h"
#include "util.h"
#include "version.h"
#include "intellisense.h"
#include "intellisense.h"
#include "fwconfig.h"

#include <stdint.h>
Expand All @@ -28,7 +28,7 @@
#define DISCARD -1


#define BUFFER_SIZE 192
#define BUFFER_SIZE 192
#define DISCARD_TIMEOUT_MS 50

#define REQUEST_TYPE_READ 0x01
Expand Down Expand Up @@ -122,7 +122,7 @@ static int16_t process_bafang_display_write_speed_limit();
void extcom_init()
{
msg_len = 0;
last_recv_ms = 0;
last_recv_ms = 0;
discard_until_ms = 0;

// Bafang standard baud rate
Expand Down Expand Up @@ -155,9 +155,9 @@ void extcom_process()
else
{
msgbuf[msg_len++] = uart_read();
last_recv_ms = now;
last_recv_ms = now;
discard_until_ms = 0;
}
}
}

if (msg_len > 0 && now - last_recv_ms > 100)
Expand All @@ -170,10 +170,10 @@ void extcom_process()
if (res == DISCARD)
{
msg_len = 0;
last_recv_ms = 0;
// Discard received data for the next DISCARD_TIMEOUT_MS milliseconds
discard_until_ms = now + DISCARD_TIMEOUT_MS;
last_recv_ms = 0;
// Discard received data for the next DISCARD_TIMEOUT_MS milliseconds
discard_until_ms = now + DISCARD_TIMEOUT_MS;

eventlog_write(EVT_ERROR_EXTCOM_DISCARD);
}
else if (res > 0)
Expand Down Expand Up @@ -354,7 +354,7 @@ static int16_t process_read_fw_version()
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
}

Expand All @@ -378,7 +378,7 @@ static int16_t process_read_evtlog_enable()
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
}

Expand Down Expand Up @@ -410,7 +410,7 @@ static int16_t process_read_config()
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
}

Expand Down Expand Up @@ -442,7 +442,7 @@ static int16_t process_write_evtlog_enable()
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
}

Expand Down Expand Up @@ -481,7 +481,7 @@ static int16_t process_write_config()
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
}

Expand All @@ -508,7 +508,7 @@ static int16_t process_write_reset_config()
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
}

Expand Down Expand Up @@ -541,7 +541,7 @@ static int16_t process_write_adc_voltage_calibration()
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
}

Expand Down Expand Up @@ -662,10 +662,10 @@ static int16_t process_bafang_display_read_range()
{
return KEEP;
}
uint16_t value = 0;
#if DISPLAY_RANGE_FIELD_DATA == DISPLAY_RANGE_FIELD_TEMPERATURE

uint16_t value = 0;

#if DISPLAY_RANGE_FIELD_DATA == DISPLAY_RANGE_FIELD_TEMPERATURE
value = app_get_temperature();
if (g_config.use_freedom_units)
{
Expand All @@ -675,24 +675,24 @@ static int16_t process_bafang_display_read_range()
// F_miles = 2.9C + 50.5

value = ((290u * value) + 5050u) / 100u;
}
#elif DISPLAY_RANGE_FIELD_DATA == DISPLAY_RANGE_FIELD_POWER
if (app_get_lights())
{
value = motor_get_battery_current_x10();
}
else
{
uint16_t max_current_amp_x10 = g_config.max_current_amps * 10;
value = MAP32(motor_get_target_current(), 0, 100, 0, max_current_amp_x10);
}
if (g_config.use_freedom_units)
{
// compensate for km -> miles conversion the display will do
value = (value * 161u) / 100u;
}
#endif
}
#elif DISPLAY_RANGE_FIELD_DATA == DISPLAY_RANGE_FIELD_POWER
if (app_get_lights())
{
value = motor_get_battery_current_x10();
}
else
{
uint16_t max_current_amp_x10 = g_config.max_current_amps * 10;
value = MAP32(motor_get_target_current(), 0, 100, 0, max_current_amp_x10);
}

if (g_config.use_freedom_units)
{
// compensate for km -> miles conversion the display will do
value = (value * 161u) / 100u;
}
#endif

uint8_t checksum = 0;

Expand Down Expand Up @@ -758,13 +758,13 @@ static int16_t process_bafang_display_write_pas()
if (msg_len < 4)
{
return KEEP;
}
if (compute_checksum(msgbuf, 3) == msgbuf[3])
{
}

if (compute_checksum(msgbuf, 3) == msgbuf[3])
{
switch (msgbuf[2])
{
case 0x00:
case 0x00:
app_set_assist_level(ASSIST_0);
break;
case 0x01:
Expand Down Expand Up @@ -796,16 +796,16 @@ static int16_t process_bafang_display_write_pas()
break;
case 0x06:
app_set_assist_level(ASSIST_PUSH);
break;
default:
// Unsupported level, ignore
break;
}
default:
// Unsupported level, ignore
break;
}
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
}

return 4;
Expand All @@ -817,26 +817,26 @@ static int16_t process_bafang_display_write_mode()
{
return KEEP;
}
if (compute_checksum(msgbuf, 3) == msgbuf[3])
{

if (compute_checksum(msgbuf, 3) == msgbuf[3])
{
switch (msgbuf[2])
{
case 0x02:
case 0x02:
app_set_operation_mode(OPERATION_MODE_DEFAULT);
break;
case 0x04:
app_set_operation_mode(OPERATION_MODE_SPORT);
break;
default:
// Unsupported mode, ignore
break;
}
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
default:
// Unsupported mode, ignore
break;
}
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
}

return 4;
Expand All @@ -848,8 +848,8 @@ static int16_t process_bafang_display_write_lights()
{
return KEEP;
}
// No checksum

// No checksum

switch (msgbuf[2])
{
Expand All @@ -858,8 +858,8 @@ static int16_t process_bafang_display_write_lights()
break;
case 0xf1:
app_set_lights(true);
break;
default:
break;
default:
return DISCARD; // unsupported state, assume communication error
}

Expand All @@ -872,22 +872,29 @@ static int16_t process_bafang_display_write_speed_limit()
{
return KEEP;
}

/*
if (compute_checksum(msgbuf, 4) == msgbuf[4])
{
// Ignoring speed limit requested by display,
// Global speed limit is configured in firmware config tool.

uint16_t value = ((msgbuf[2] << 8) | msgbuf[3]);
app_set_wheel_max_speed_rpm(value);
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
}
*/

#if (SPEED_LIMIT_SPORT_SWITCH_KPH > 0)
if (compute_checksum(msgbuf, 4) == msgbuf[4])
{
uint16_t value = ((msgbuf[2] << 8) | msgbuf[3]);

// Enable sport mode if the display sets the speed limit to
// the specified value

app_set_speed_limit_operation_mode(value);


// Ignoring speed limit requested by display,
// Global speed limit is configured in firmware config tool.

// app_set_wheel_max_speed_rpm(value);
}
else
{
eventlog_write(EVT_ERROR_EXTCOM_CHEKSUM);
return DISCARD;
}
#endif

return 5;
}
1 change: 0 additions & 1 deletion src/firmware/extcom.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ void extcom_init();
void extcom_process();

#endif

Loading