diff --git a/Inc/comms.h b/Inc/comms.h index f942270..674e98b 100644 --- a/Inc/comms.h +++ b/Inc/comms.h @@ -23,6 +23,7 @@ #include "stm32f1xx_hal.h" +#if defined(DEBUG_SERIAL_PROTOCOL) enum types {UINT8_T,UINT16_T,UINT32_T,INT8_T,INT16_T,INT32_T,INT,FLOAT}; #define typename(x) _Generic((x), \ @@ -32,7 +33,7 @@ enum types {UINT8_T,UINT16_T,UINT32_T,INT8_T,INT16_T,INT32_T,INT,FLOAT}; int8_t: INT8_T, \ int16_t: INT16_T, \ int32_t: INT32_T, \ - int: INT, \ + int: INT, \ float: FLOAT) #define PARAM_SIZE(param) sizeof(param) / sizeof(parameter_entry) @@ -93,20 +94,20 @@ typedef struct parameter_entry_struct parameter_entry; struct parameter_entry_struct { const uint8_t type; const char *name; - const uint8_t datatype; + const uint8_t datatype; void *valueL; void *valueR; const uint16_t addr; - const int32_t init; + const int32_t init; const uint8_t initFormat; const int32_t min; - const int32_t max; + const int32_t max; const uint8_t div; const uint8_t mul; const uint8_t fix; - void (*callback_function)(); - const char *help; + void (*callback_function)(); + const char *help; }; - -#endif \ No newline at end of file +#endif // DEBUG_SERIAL_PROTOCOL +#endif // COMMS_H \ No newline at end of file diff --git a/Inc/config.h b/Inc/config.h index 0a7ace9..28775f3 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -172,6 +172,7 @@ #define ADC_MARGIN 100 // ADC input margin applied on the raw ADC min and max to make sure the MIN and MAX values are reached even in the presence of noise #define ADC_PROTECT_TIMEOUT 100 // ADC Protection: number of wrong / missing input commands before safety state is taken #define ADC_PROTECT_THRESH 200 // ADC Protection threshold below/above the MIN/MAX ADC values +#define AUTO_CALIBRATION_ENA // Enable/Disable input auto-calibration by holding power button pressed. Un-comment this if auto-calibration is not needed. /* FILTER is in fixdt(0,16,16): VAL_fixedPoint = VAL_floatingPoint * 2^16. In this case 6553 = 0.1 * 2^16 * Value of COEFFICIENT is in fixdt(1,16,14) @@ -227,7 +228,7 @@ * enable DEBUG_SERIAL_USART3 or DEBUG_SERIAL_USART2 * * - * DEBUG_SERIAL_ASCII output is: + * DEBUG ASCII output is: * // "in1:345 in2:1337 cmdL:0 cmdR:0 BatADC:0 BatV:0 TempADC:0 Temp:0\r\n" * * in1: (int16_t)input1[inIdx].raw); raw input1: ADC1, UART, PWM, PPM, iBUS @@ -243,6 +244,7 @@ // #define DEBUG_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! // #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! +// #define DEBUG_SERIAL_PROTOCOL // uncomment this to send user commands to the board, change parameters and print specific signals (see comms.c for the user commands) // ########################### END OF DEBUG SERIAL ############################ @@ -291,7 +293,6 @@ #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! #endif - #define DEBUG_SERIAL_PROTOCOL // #define SUPPORT_BUTTONS_LEFT // use left sensor board cable for button inputs. Disable DEBUG_SERIAL_USART2! // #define SUPPORT_BUTTONS_RIGHT // use right sensor board cable for button inputs. Disable DEBUG_SERIAL_USART3! #endif diff --git a/Makefile b/Makefile index 3d4cad7..e8e0f3d 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,7 @@ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_dma.c \ Src/system_stm32f1xx.c \ Src/setup.c \ Src/control.c \ +Src/comms.c \ Src/util.c \ Src/main.c \ Src/bldc.c \ diff --git a/Src/comms.c b/Src/comms.c index 5302c7e..c6f8966 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -17,7 +17,6 @@ * along with this program. If not, see . */ - // Includes #include #include @@ -30,6 +29,7 @@ #include "util.h" #include "comms.h" +#if defined(DEBUG_SERIAL_PROTOCOL) #if defined(DEBUG_SERIAL_PROTOCOL) && (defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3)) #ifdef CONTROL_ADC @@ -635,4 +635,6 @@ void process_debug() } } -#endif \ No newline at end of file +#endif +#endif // DEBUG_SERIAL_PROTOCOL + diff --git a/Src/main.c b/Src/main.c index 9c86c2b..3d190f8 100644 --- a/Src/main.c +++ b/Src/main.c @@ -200,7 +200,7 @@ int main(void) { int16_t board_temp_deg_c; // Loop until button is released - while(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) HAL_Delay(10); + while(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { HAL_Delay(10); } while(1) { HAL_Delay(DELAY_IN_MAIN_LOOP); // delay in ms @@ -424,7 +424,9 @@ int main(void) { // ####### DEBUG SERIAL OUT ####### #if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3) if (main_loop_counter % 25 == 0) { // Send data periodically every 125 ms - #ifndef DEBUG_SERIAL_PROTOCOL + #if defined(DEBUG_SERIAL_PROTOCOL) + process_debug(); + #else printf("in1:%i in2:%i cmdL:%i cmdR:%i BatADC:%i BatV:%i TempADC:%i Temp:%i\r\n", input1[inIdx].raw, // 1: INPUT1 input2[inIdx].raw, // 2: INPUT2 @@ -434,8 +436,6 @@ int main(void) { batVoltage * BAT_CALIB_REAL_VOLTAGE / BAT_CALIB_ADC, // 6: for verifying battery voltage calibration board_temp_adcFilt, // 7: for board temperature calibration board_temp_deg_c); // 8: for verifying board temperature calibration - #else - process_debug(); #endif } #endif diff --git a/Src/util.c b/Src/util.c index 1a0f305..46d5b1e 100644 --- a/Src/util.c +++ b/Src/util.c @@ -483,6 +483,7 @@ void calcAvgSpeed(void) { * The Values will be saved to flash. Values are persistent if you flash with platformio. To erase them, make a full chip erase. */ void adcCalibLim(void) { +#ifdef AUTO_CALIBRATION_ENA calcAvgSpeed(); if (speedAvgAbs > 5) { // do not enter this mode if motors are spinning return; @@ -571,6 +572,7 @@ void adcCalibLim(void) { #endif #endif +#endif // AUTO_CALIBRATION_ENA } /* * Update Maximum Motor Current Limit (via ADC1) and Maximum Speed Limit (via ADC2) @@ -1063,14 +1065,20 @@ void usart2_rx_check(void) #endif #if defined(DEBUG_SERIAL_USART2) + uint8_t *ptr; if (pos != old_pos) { // Check change in received data if (pos > old_pos) { // "Linear" buffer mode: check if current position is over previous one usart_process_debug(&rx_buffer_L[old_pos], pos - old_pos); // Process data } else { // "Overflow" buffer mode - usart_process_debug(&rx_buffer_L[old_pos], rx_buffer_L_len - old_pos); // First Process data from the end of buffer + ptr = (uint8_t *) malloc(sizeof(uint8_t) * (rx_buffer_L_len - old_pos + pos)); + memcpy(ptr, &rx_buffer_L[old_pos], rx_buffer_L_len - old_pos); // First copy data from the end of buffer if (pos > 0) { // Check and continue with beginning of buffer - usart_process_debug(&rx_buffer_L[0], pos); // Process remaining data + ptr += rx_buffer_L_len - old_pos; + memcpy(ptr, &rx_buffer_L[0], pos); // Copy remaining data } + ptr -= rx_buffer_L_len - old_pos; + usart_process_debug(ptr, rx_buffer_L_len - old_pos + pos); // Process data + free(ptr); } } #endif // DEBUG_SERIAL_USART2 @@ -1134,26 +1142,25 @@ void usart3_rx_check(void) #if defined(DEBUG_SERIAL_USART3) uint8_t *ptr; - if (pos != old_pos) { // Check change in received data if (pos > old_pos) { // "Linear" buffer mode: check if current position is over previous one usart_process_debug(&rx_buffer_R[old_pos], pos - old_pos); // Process data } else { // "Overflow" buffer mode - ptr = (uint8_t *) malloc( sizeof(uint8_t) * (rx_buffer_R_len - old_pos + pos) ); - memcpy(ptr,&rx_buffer_R[old_pos],rx_buffer_R_len - old_pos); // First Process data from the end of buffer + ptr = (uint8_t *) malloc(sizeof(uint8_t) * (rx_buffer_R_len - old_pos + pos)); + memcpy(ptr, &rx_buffer_R[old_pos], rx_buffer_R_len - old_pos); // First copy data from the end of buffer if (pos > 0) { // Check and continue with beginning of buffer ptr += rx_buffer_R_len - old_pos; - memcpy(ptr,&rx_buffer_R[0], pos); // Process remaining data + memcpy(ptr, &rx_buffer_R[0], pos); // Copy remaining data } ptr -= rx_buffer_R_len - old_pos; - usart_process_debug(ptr, rx_buffer_R_len - old_pos + pos); - free( ptr ); + usart_process_debug(ptr, rx_buffer_R_len - old_pos + pos); // Process data + free(ptr); } } #endif // DEBUG_SERIAL_USART3 #ifdef CONTROL_SERIAL_USART3 - uint8_t *ptr; + uint8_t *ptr; if (pos != old_pos) { // Check change in received data ptr = (uint8_t *)&commandR_raw; // Initialize the pointer with command_raw address if (pos > old_pos && (pos - old_pos) == commandR_len) { // "Linear" buffer mode: check if current position is over previous one AND data length equals expected length @@ -1202,16 +1209,8 @@ void usart3_rx_check(void) #if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3) void usart_process_debug(uint8_t *userCommand, uint32_t len) { - - #ifndef DEBUG_SERIAL_PROTOCOL - for (; len > 0; len--, userCommand++) { - if (*userCommand != '\n' && *userCommand != '\r') { // Do not accept 'new line' and 'carriage return' commands - printf("Command = %c\r\n", *userCommand); - // handle_input(*userCommand); // -> Create this function to handle the user commands - } - } - #else - handle_input(userCommand,len); + #ifdef DEBUG_SERIAL_PROTOCOL + handle_input(userCommand, len); #endif } @@ -1542,9 +1541,11 @@ void poweroffPressCheck(void) { updateCurSpdLim(); beepShort(5); } else { // Long press: Calibrate ADC Limits + #ifdef AUTO_CALIBRATION_ENA beepLong(16); adcCalibLim(); beepShort(5); + #endif } } else if (cnt_press > 8) { // Short press: power off (80 ms debounce) poweroff();