diff --git a/lispBM/README.md b/lispBM/README.md index 423d30a7..9878ca24 100644 --- a/lispBM/README.md +++ b/lispBM/README.md @@ -382,6 +382,14 @@ Get statistics about the selected motor since boot (or since stats-reset). The f 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 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. diff --git a/lispBM/lispif_vesc_extensions.c b/lispBM/lispif_vesc_extensions.c index d9f35671..629d0b22 100644 --- a/lispBM/lispif_vesc_extensions.c +++ b/lispBM/lispif_vesc_extensions.c @@ -49,6 +49,7 @@ #include "firmware_metadata.h" #include "log.h" #include "buffer.h" +#include "crc.h" #include #include @@ -3645,6 +3646,31 @@ static lbm_value ext_icu_period(lbm_value *args, lbm_uint argn) { 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) { 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-width", ext_icu_width); lbm_add_extension("icu-period", ext_icu_period); + lbm_add_extension("crc16", ext_crc16); // APP commands lbm_add_extension("app-adc-detach", ext_app_adc_detach);