Added crc16 extension

This commit is contained in:
Benjamin Vedder 2023-02-20 10:04:14 +01:00
parent 8c9b1ebf4e
commit be0bc7dc18
2 changed files with 35 additions and 0 deletions

View File

@ -382,6 +382,14 @@ Get statistics about the selected motor since boot (or since stats-reset). The f
Reset stat counters to 0. Reset stat counters to 0.
#### crc16
```clj
(crc16 array optLen)
```
Calculate the 16-bit crc of array. optLen is an optional argument for how many elements to include, if it is left out the entire array will be used. The crc uses the polynomial 0x11021 and initial value 0, which is the same as the VESC packets use. Added in FW 6.02.
### App Override Commands ### App Override Commands
Several app-inputs can be detached from the external interfaces and overridden from lisp. This is useful to take advantage of existing throttle curves and control modes from the apps while providing a custom input source. Several app-inputs can be detached from the external interfaces and overridden from lisp. This is useful to take advantage of existing throttle curves and control modes from the apps while providing a custom input source.

View File

@ -49,6 +49,7 @@
#include "firmware_metadata.h" #include "firmware_metadata.h"
#include "log.h" #include "log.h"
#include "buffer.h" #include "buffer.h"
#include "crc.h"
#include <math.h> #include <math.h>
#include <ctype.h> #include <ctype.h>
@ -3645,6 +3646,31 @@ static lbm_value ext_icu_period(lbm_value *args, lbm_uint argn) {
return lbm_enc_i(icu_last_period); return lbm_enc_i(icu_last_period);
} }
static lbm_value ext_crc16(lbm_value *args, lbm_uint argn) {
if ((argn != 1 && argn != 2) || !lbm_is_array(args[0])) {
return ENC_SYM_TERROR;
}
lbm_array_header_t *array = (lbm_array_header_t *)lbm_car(args[0]);
if (array->elt_type != LBM_TYPE_BYTE) {
return ENC_SYM_TERROR;
}
unsigned int len = array->size;
if (argn == 2) {
if (!lbm_is_number(args[1])) {
return ENC_SYM_TERROR;
}
len = lbm_dec_as_u32(args[1]);
if (len > array->size) {
len = array->size;
}
}
return lbm_enc_i(crc16((uint8_t*)array->data, len));
}
void lispif_load_vesc_extensions(void) { void lispif_load_vesc_extensions(void) {
lispif_stop_lib(); lispif_stop_lib();
@ -3715,6 +3741,7 @@ void lispif_load_vesc_extensions(void) {
lbm_add_extension("icu-start", ext_icu_start); lbm_add_extension("icu-start", ext_icu_start);
lbm_add_extension("icu-width", ext_icu_width); lbm_add_extension("icu-width", ext_icu_width);
lbm_add_extension("icu-period", ext_icu_period); lbm_add_extension("icu-period", ext_icu_period);
lbm_add_extension("crc16", ext_crc16);
// APP commands // APP commands
lbm_add_extension("app-adc-detach", ext_app_adc_detach); lbm_add_extension("app-adc-detach", ext_app_adc_detach);