From 9c6ca7ec7be585357adaa8e0062e1f0ac684edc3 Mon Sep 17 00:00:00 2001 From: phail Date: Sun, 14 Jun 2020 22:49:56 +0200 Subject: [PATCH 01/16] initial add of bipropellant protocol --- .gitmodules | 3 + Inc/config.h | 3 +- Inc/protocolfunctions.h | 12 + Src/hbprotocol | 1 + Src/main.c | 21 ++ Src/protocolfunctions.c | 549 ++++++++++++++++++++++++++++++++++++++++ platformio.ini | 27 +- 7 files changed, 614 insertions(+), 2 deletions(-) create mode 100644 .gitmodules create mode 100644 Inc/protocolfunctions.h create mode 160000 Src/hbprotocol create mode 100644 Src/protocolfunctions.c diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..64933deb2 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Src/hbprotocol"] + path = Src/hbprotocol + url = https://github.com/bipropellant/hbprotocol.git diff --git a/Inc/config.h b/Inc/config.h index d3271144b..ccddf5835 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -19,6 +19,7 @@ //#define VARIANT_HOVERCAR // Variant for HOVERCAR build //#define VARIANT_HOVERBOARD // Variant for HOVERBOARD build //#define VARIANT_TRANSPOTTER // Variant for TRANSPOTTER build https://github.com/NiklasFauth/hoverboard-firmware-hack/wiki/Build-Instruction:-TranspOtter https://hackaday.io/project/161891-transpotter-ng + //#define VARIANT_BIPROPELLANT// Variant for Bipropellant Protocol #endif // ########################### END OF VARIANT SELECTION ############################ @@ -452,7 +453,7 @@ // ############################### VALIDATE SETTINGS ############################### #if !defined(VARIANT_ADC) && !defined(VARIANT_USART) && !defined(VARIANT_NUNCHUK) && !defined(VARIANT_PPM) && !defined(VARIANT_PWM) && \ - !defined(VARIANT_IBUS) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_HOVERBOARD) && !defined(VARIANT_TRANSPOTTER) + !defined(VARIANT_IBUS) && !defined(VARIANT_HOVERCAR) && !defined(VARIANT_HOVERBOARD) && !defined(VARIANT_TRANSPOTTER) && !defined(VARIANT_BIPROPELLANT) #error Variant not defined! Please check platformio.ini or Inc/config.h for available variants. #endif diff --git a/Inc/protocolfunctions.h b/Inc/protocolfunctions.h new file mode 100644 index 000000000..5ab5aece8 --- /dev/null +++ b/Inc/protocolfunctions.h @@ -0,0 +1,12 @@ +#pragma once + +#include "protocol.h" + +int setup_protocol(PROTOCOL_STAT *s); + + +extern PROTOCOL_STAT sSoftwareSerial; +extern PROTOCOL_STAT sUSART2; +extern PROTOCOL_STAT sUSART3; + +void consoleLog(char *message); \ No newline at end of file diff --git a/Src/hbprotocol b/Src/hbprotocol new file mode 160000 index 000000000..9459e90a7 --- /dev/null +++ b/Src/hbprotocol @@ -0,0 +1 @@ +Subproject commit 9459e90a7831ed41d2671b5b5363590741ff5dde diff --git a/Src/main.c b/Src/main.c index bd9ad01d6..35f594ad1 100644 --- a/Src/main.c +++ b/Src/main.c @@ -29,6 +29,7 @@ #include "comms.h" #include "BLDC_controller.h" /* BLDC's header file */ #include "rtwtypes.h" +#include "protocolfunctions.h" #if defined(DEBUG_I2C_LCD) || defined(SUPPORT_LCD) #include "hd44780.h" @@ -196,6 +197,13 @@ int main(void) { int16_t board_temp_adcFilt = adc_buffer.temp; int16_t board_temp_deg_c; + #if defined(CONTROL_SERIAL_USART2) && defined(VARIANT_BIPROPELLANT) + setup_protocol(&sUSART2); + #endif + #if defined(CONTROL_SERIAL_USART3) && defined(VARIANT_BIPROPELLANT) + setup_protocol(&sUSART3); + #endif + while(1) { HAL_Delay(DELAY_IN_MAIN_LOOP); //delay in ms @@ -203,6 +211,19 @@ int main(void) { readCommand(); // Read Command: cmd1, cmd2 calcAvgSpeed(); // Calculate average measured speed: speedAvg, speedAvgAbs + #if defined(CONTROL_SERIAL_USART2) && defined(VARIANT_BIPROPELLANT) + while ( serial_usart_buffer_count(&usart2_it_RXbuffer) > 0 ) { + protocol_byte( &sUSART2, (unsigned char) serial_usart_buffer_pop(&usart2_it_RXbuffer) ); + } + protocol_tick( &sUSART2 ); + #endif + #if defined(CONTROL_SERIAL_USART3) && defined(VARIANT_BIPROPELLANT) + while ( serial_usart_buffer_count(&usart3_it_RXbuffer) > 0 ) { + protocol_byte( &sUSART3, (unsigned char) serial_usart_buffer_pop(&usart3_it_RXbuffer) ); + } + protocol_tick( &sUSART3 ); + #endif + #ifndef VARIANT_TRANSPOTTER // ####### MOTOR ENABLING: Only if the initial input is very small (for SAFETY) ####### if (enable == 0 && (!rtY_Left.z_errCode && !rtY_Right.z_errCode) && (cmd1 > -50 && cmd1 < 50) && (cmd2 > -50 && cmd2 < 50)){ diff --git a/Src/protocolfunctions.c b/Src/protocolfunctions.c new file mode 100644 index 000000000..df8de7d1f --- /dev/null +++ b/Src/protocolfunctions.c @@ -0,0 +1,549 @@ +#include "defines.h" +#include "config.h" +#include "protocolfunctions.h" +#include "bldc.h" +#include "softwareserial.h" +#include "flashcontent.h" +#include "flashaccess.h" +#include "comms.h" + +#include "stm32f1xx_hal.h" +#ifdef CONTROL_SENSOR + #include "sensorcoms.h" +#endif +#include "hallinterrupts.h" +#include "deadreckoner.h" + +#include +#include + +#include "control_structures.h" + +#ifdef SOFTWARE_SERIAL + PROTOCOL_STAT sSoftwareSerial; +#endif +#if defined(SERIAL_USART2_IT) + PROTOCOL_STAT sUSART2; +#endif +#if defined(SERIAL_USART3_IT) && !defined(CONTROL_SENSOR) + PROTOCOL_STAT sUSART3; +#endif + +extern volatile uint32_t input_timeout_counter; // global variable for input timeout + + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x09 enable + +extern uint8_t enable; // global variable for motor enable +extern void init_PID_control(); // from main +extern int main_ascii_init(PROTOCOL_STAT *s); // from ascii_proto_funcs.c + +////////////////////////////////////////////// +// make values safe before we change enable... + +char protocol_enable = 0; +void fn_enable ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + switch (cmd) { + case PROTOCOL_CMD_READVAL: + case PROTOCOL_CMD_SILENTREAD: + protocol_enable = enable; + break; + + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + if (!protocol_enable) { + // assume we will enable, + // set wanted posn to current posn, else we may rush into a wall + PosnData.wanted_posn_mm[0] = HallData[0].HallPosn_mm; + PosnData.wanted_posn_mm[1] = HallData[1].HallPosn_mm; + + // clear speeds to zero + SpeedData.wanted_speed_mm_per_sec[0] = 0; + SpeedData.wanted_speed_mm_per_sec[1] = 0; + PWMData.pwm[0] = 0; + PWMData.pwm[1] = 0; + init_PID_control(); + } + enable = protocol_enable; + break; + } + fn_defaultProcessing(s, param, cmd, msg); +} + + +#ifdef CONTROL_SENSOR +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x01 sensor_data + +extern SENSOR_DATA sensor_data[2]; + +// used to send only pertinent data, not the whole structure +PROTOCOL_SENSOR_FRAME sensor_copy[2]; + +void fn_SensorData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + switch (cmd) { + case PROTOCOL_CMD_READVAL: + case PROTOCOL_CMD_SILENTREAD: + // copy just sensor input data + memcpy(&sensor_copy[0], &sensor_data[0].complete, sizeof(sensor_copy[0])); + memcpy(&sensor_copy[1], &sensor_data[1].complete, sizeof(sensor_copy[1])); + break; + } + fn_defaultProcessingReadOnly(s, param, cmd, msg); +} + +#endif + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x02 HallData + +/* see hallinterrupts.h */ + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x03 SpeedData + +PROTOCOL_SPEED_DATA SpeedData = { + {0, 0}, + + 600, // max power (PWM) + -600, // min power + 40 // minimum mm/s which we can ask for +}; + + +void fn_SpeedData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + switch (cmd) { + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + control_type = CONTROL_TYPE_SPEED; + input_timeout_counter = 0; + break; + } + fn_defaultProcessing(s, param, cmd, msg); +} + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x04 Position + +POSN Position; + +void fn_Position ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + switch (cmd) { + case PROTOCOL_CMD_READVAL: + case PROTOCOL_CMD_SILENTREAD: + ((POSN*) (param->ptr))->LeftAbsolute = HallData[0].HallPosn_mm; + ((POSN*) (param->ptr))->LeftOffset = HallData[0].HallPosn_mm - HallData[0].HallPosn_mm_lastread; + ((POSN*) (param->ptr))->RightAbsolute = HallData[1].HallPosn_mm; + ((POSN*) (param->ptr))->RightOffset = HallData[1].HallPosn_mm - HallData[1].HallPosn_mm_lastread; + break; + } + + fn_defaultProcessing(s, param, cmd, msg); + + switch (cmd) { + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + HallData[0].HallPosn_mm_lastread = ((POSN*) (param->ptr))->LeftAbsolute; + HallData[1].HallPosn_mm_lastread = ((POSN*) (param->ptr))->RightAbsolute; + break; + } +} + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x05 PositionIncr + +POSN_INCR PositionIncr; + +void fn_PositionIncr ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + + fn_defaultProcessing(s, param, cmd, msg); + + switch (cmd) { + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + // if switching to control type POSITION, + if ((control_type != CONTROL_TYPE_POSITION) || !enable) { + control_type = CONTROL_TYPE_POSITION; + // then make sure we won't rush off somwehere strange + // by setting our wanted posn to where we currently are... + fn_enable( s, param, PROTOCOL_CMD_READVALRESPONSE, msg); // TODO: I don't like calling this with a param entry which does not fit to the handler.. + } + + enable = 1; + input_timeout_counter = 0; + + // increment our wanted position + PosnData.wanted_posn_mm[0] += ((POSN_INCR*) (param->ptr))->Left; + PosnData.wanted_posn_mm[1] += ((POSN_INCR*) (param->ptr))->Right; + break; + } +} + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x06 PosnData + +PROTOCOL_POSN_DATA PosnData = { + {0, 0}, + + 200, // max pwm in posn mode + 70, // min pwm in posn mode +}; + + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x07 RawPosition + +PROTOCOL_POSN RawPosition; + +void fn_RawPosition ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + switch (cmd) { + case PROTOCOL_CMD_READVAL: + case PROTOCOL_CMD_SILENTREAD: + ((PROTOCOL_POSN*) (param->ptr))->LeftAbsolute = HallData[0].HallPosn; + ((PROTOCOL_POSN*) (param->ptr))->LeftOffset = HallData[0].HallPosn - HallData[0].HallPosn_lastread; + ((PROTOCOL_POSN*) (param->ptr))->RightAbsolute = HallData[1].HallPosn; + ((PROTOCOL_POSN*) (param->ptr))->RightOffset = HallData[1].HallPosn - HallData[1].HallPosn_lastread; + break; + } + + fn_defaultProcessing(s, param, cmd, msg); + + switch (cmd) { + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + HallData[0].HallPosn_lastread = ((PROTOCOL_POSN*) (param->ptr))->LeftAbsolute; + HallData[1].HallPosn_lastread = ((PROTOCOL_POSN*) (param->ptr))->RightAbsolute; + break; + } +} + + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x0A disablepoweroff + +extern uint8_t disablepoweroff; + + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x0B debug_out + +extern uint8_t debug_out; + + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x0C xytPosn + +// ded reckoning posn +extern INTEGER_XYT_POSN xytPosn; + +void fn_xytPosn ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + + fn_defaultProcessing(s, param, cmd, msg); + + switch (cmd) { + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + if (deadreconer) { + // reset xyt + reset( deadreconer, 1 ); + } + break; + } + +} + + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x0D PWMData and 0x0E PWMData.pwm + +PROTOCOL_PWM_DATA PWMData = { + .pwm[0] = 0, + .pwm[1] = 0, + .speed_max_power = 600, + .speed_min_power = -600, + .speed_minimum_pwm = 40 // guard value, below this set to zero +}; + +extern int pwms[2]; + +void fn_PWMData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + switch (cmd) { + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + control_type = CONTROL_TYPE_PWM; + input_timeout_counter = 0; + break; + } + + fn_defaultProcessing(s, param, cmd, msg); + + switch (cmd) { + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + for (int i = 0; i < 2; i++) { + if (((PROTOCOL_PWM_DATA*) (param->ptr))->pwm[i] > ((PROTOCOL_PWM_DATA*) (param->ptr))->speed_max_power) { + ((PROTOCOL_PWM_DATA*) (param->ptr))->pwm[i] = ((PROTOCOL_PWM_DATA*) (param->ptr))->speed_max_power; + } + if (((PROTOCOL_PWM_DATA*) (param->ptr))->pwm[i] < ((PROTOCOL_PWM_DATA*) (param->ptr))->speed_min_power) { + ((PROTOCOL_PWM_DATA*) (param->ptr))->pwm[i] = ((PROTOCOL_PWM_DATA*) (param->ptr))->speed_min_power; + } + if ((((PROTOCOL_PWM_DATA*) (param->ptr))->pwm[i] > 0) && (((PROTOCOL_PWM_DATA*) (param->ptr))->pwm[i] < ((PROTOCOL_PWM_DATA*) (param->ptr))->speed_minimum_pwm)) { + ((PROTOCOL_PWM_DATA*) (param->ptr))->pwm[i] = 0; + } + if ((((PROTOCOL_PWM_DATA*) (param->ptr))->pwm[i] < 0) && (((PROTOCOL_PWM_DATA*) (param->ptr))->pwm[i] > -((PROTOCOL_PWM_DATA*) (param->ptr))->speed_minimum_pwm)) { + ((PROTOCOL_PWM_DATA*) (param->ptr))->pwm[i] = 0; + } + } + break; + } +} + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x21 BuzzerData + +PROTOCOL_BUZZER_DATA BuzzerData = { + .buzzerFreq = 0, + .buzzerPattern = 0, + .buzzerLen = 0, +}; + +extern uint8_t buzzerFreq; // global variable for the buzzer pitch. can be 1, 2, 3, 4, 5, 6, 7... +extern uint8_t buzzerPattern; // global variable for the buzzer pattern. can be 1, 2, 3, 4, 5, 6, 7... +extern uint16_t buzzerLen; + +void fn_BuzzerData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + switch (cmd) { + case PROTOCOL_CMD_READVAL: + case PROTOCOL_CMD_SILENTREAD: + ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerFreq = buzzerFreq; + ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerLen = buzzerLen; + ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerPattern = buzzerPattern; + break; + } + + fn_defaultProcessing(s, param, cmd, msg); + + switch (cmd) { + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + buzzerFreq = ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerFreq; + buzzerLen = ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerLen; + buzzerPattern = ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerPattern; + break; + } +} + + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x80 to 0xA0 FlashContent + +// from main.c +extern void change_PID_constants(); +extern void init_PID_control(); + +extern volatile ELECTRICAL_PARAMS electrical_measurements; + +void fn_FlashContentMagic ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + + fn_defaultProcessing(s, param, cmd, msg); + + switch (cmd) { + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + if (FlashContent.magic != CURRENT_MAGIC){ + char temp[128]; + sprintf(temp, "incorrect magic %d, should be %d\r\nFlash not written\r\n", FlashContent.magic, CURRENT_MAGIC); + consoleLog(temp); + FlashContent.magic = CURRENT_MAGIC; + return; + } + writeFlash( (unsigned char *)&FlashContent, sizeof(FlashContent) ); + consoleLog("wrote flash\r\n"); + break; + } +} + +void fn_FlashContentPID ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + + fn_defaultProcessing(s, param, cmd, msg); + + switch (cmd) { + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + change_PID_constants(); + break; + } +} + +void fn_FlashContentMaxCurrLim ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + + fn_defaultProcessing(s, param, cmd, msg); + + switch (cmd) { + case PROTOCOL_CMD_WRITEVAL: + case PROTOCOL_CMD_READVALRESPONSE: + electrical_measurements.dcCurLim = MIN(DC_CUR_LIMIT, FlashContent.MaxCurrLim / 100); + break; + } +} + + + + + + + + + + +//////////////////////////////////////////////////////////////////////////////////////////// +// initialize protocol and register functions +int setup_protocol(PROTOCOL_STAT *s) { + + protocol_GetTick = HAL_GetTick; + protocol_Delay = HAL_Delay; + protocol_SystemReset =HAL_NVIC_SystemReset; + + + int errors = 0; + + + #ifdef SOFTWARE_SERIAL + + errors += protocol_init(&sSoftwareSerial); + + sSoftwareSerial.send_serial_data=softwareserial_Send; + sSoftwareSerial.send_serial_data_wait=softwareserial_Send_Wait; + sSoftwareSerial.timeout1 = 500; + sSoftwareSerial.timeout2 = 100; + sSoftwareSerial.allow_ascii = 1; + + #endif + + #if defined(SERIAL_USART2_IT) + // initialise, even if CONTROL_SENSOR, as we may switch vuia software to use this.... + extern int USART2_IT_send(unsigned char *data, int len); + + errors += protocol_init(&sUSART2); + + sUSART2.send_serial_data=USART2_IT_send; + sUSART2.send_serial_data_wait=USART2_IT_send; + sUSART2.timeout1 = 500; + sUSART2.timeout2 = 100; + sUSART2.allow_ascii = 1; + + #endif + + #if defined(SERIAL_USART3_IT) && !defined(CONTROL_SENSOR) + + extern int USART3_IT_send(unsigned char *data, int len); + + errors += protocol_init(&sUSART3); + + sUSART3.send_serial_data=USART3_IT_send; + sUSART3.send_serial_data_wait=USART3_IT_send; + sUSART3.timeout1 = 500; + sUSART3.timeout2 = 100; + sUSART3.allow_ascii = 1; + + #endif + + + // initialise ascii protocol functions + main_ascii_init(s); + + + #ifdef CONTROL_SENSOR + errors += setParamVariable( s, 0x01, UI_NONE, &sensor_copy, sizeof(sensor_copy) ); + setParamHandler( s, 0x01, fn_SensorData ); + #endif + + errors += setParamVariable( s, 0x02, UI_NONE, (void *)&HallData, sizeof(HallData) ); + + errors += setParamVariable( s, 0x03, UI_NONE, &SpeedData, sizeof(SpeedData) ); + setParamHandler( s, 0x03, fn_SpeedData ); + + errors += setParamVariable( s, 0x04, UI_NONE, &Position, sizeof(Position) ); + setParamHandler( s, 0x04, fn_Position ); + + errors += setParamVariable( s, 0x05, UI_NONE, &PositionIncr, sizeof(PositionIncr) ); + setParamHandler( s, 0x05, fn_PositionIncr ); + + errors += setParamVariable( s, 0x06, UI_NONE, &PosnData, sizeof(PosnData) ); + + errors += setParamVariable( s, 0x07, UI_NONE, &RawPosition, sizeof(RawPosition) ); + setParamHandler( s, 0x07, fn_RawPosition ); + + errors += setParamVariable( s, 0x08, UI_NONE, (void *)&electrical_measurements, sizeof(ELECTRICAL_PARAMS) ); + + errors += setParamVariable( s, 0x09, UI_CHAR, &protocol_enable, sizeof(enable) ); + setParamHandler( s, 0x09, fn_enable ); + + errors += setParamVariable( s, 0x0A, UI_CHAR, &disablepoweroff, sizeof(disablepoweroff) ); + + errors += setParamVariable( s, 0x0B, UI_CHAR, &debug_out, sizeof(debug_out) ); + + errors += setParamVariable( s, 0x0C, UI_3LONG, &xytPosn, sizeof(xytPosn) ); + setParamHandler( s, 0x0C, fn_xytPosn ); + + errors += setParamVariable( s, 0x0D, UI_NONE, &PWMData, sizeof(PWMData) ); + setParamHandler( s, 0x0D, fn_PWMData ); + + errors += setParamVariable( s, 0x0E, UI_2LONG, &(PWMData.pwm), sizeof(PWMData.pwm) ); + setParamHandler( s, 0x0E, fn_PWMData ); + + errors += setParamVariable( s, 0x21, UI_NONE, &BuzzerData, sizeof(BuzzerData) ); + setParamHandler( s, 0x21, fn_BuzzerData ); + + errors += setParamVariable( s, 0x80, UI_SHORT, &FlashContent.magic, sizeof(short) ); + setParamHandler( s, 0x80, fn_FlashContentMagic ); + + errors += setParamVariable( s, 0x81, UI_SHORT, &FlashContent.PositionKpx100, sizeof(short) ); + setParamHandler( s, 0x81, fn_FlashContentPID ); + + errors += setParamVariable( s, 0x82, UI_SHORT, &FlashContent.PositionKix100, sizeof(short) ); + setParamHandler( s, 0x82, fn_FlashContentPID ); + + errors += setParamVariable( s, 0x83, UI_SHORT, &FlashContent.PositionKdx100, sizeof(short) ); + setParamHandler( s, 0x83, fn_FlashContentPID ); + + errors += setParamVariable( s, 0x84, UI_SHORT, &FlashContent.PositionPWMLimit, sizeof(short) ); + setParamHandler( s, 0x84, fn_FlashContentPID ); + + errors += setParamVariable( s, 0x85, UI_SHORT, &FlashContent.SpeedKpx100, sizeof(short) ); + setParamHandler( s, 0x85, fn_FlashContentPID ); + + errors += setParamVariable( s, 0x86, UI_SHORT, &FlashContent.SpeedKix100, sizeof(short) ); + setParamHandler( s, 0x86, fn_FlashContentPID ); + + errors += setParamVariable( s, 0x87, UI_SHORT, &FlashContent.SpeedKdx100, sizeof(short) ); + setParamHandler( s, 0x87, fn_FlashContentPID ); + + errors += setParamVariable( s, 0x88, UI_SHORT, &FlashContent.SpeedPWMIncrementLimit, sizeof(short) ); + setParamHandler( s, 0x88, fn_FlashContentPID ); + + errors += setParamVariable( s, 0x89, UI_SHORT, &FlashContent.MaxCurrLim, sizeof(short) ); + setParamHandler( s, 0x89, fn_FlashContentMaxCurrLim ); + + errors += setParamVariable( s, 0x90, UI_NONE, &FlashContent.adc, sizeof(PROTOCOL_ADC_SETTINGS) ); + + errors += setParamVariable( s, 0xA0, UI_SHORT, &FlashContent.HoverboardEnable, sizeof(short) ); + return errors; + +} + + +void consoleLog(char *message) { + #ifdef DEBUG_SOFTWARE_SERIAL + if (debug_out) protocol_send_text(&sSoftwareSerial, message, PROTOCOL_SOM_NOACK); + #endif + + #ifdef SERIAL_USART2_IT + if (debug_out) protocol_send_text(&sUSART2, message, PROTOCOL_SOM_NOACK); + #endif + + #if defined(SERIAL_USART3_IT) && !defined(CONTROL_SENSOR) + if (debug_out) protocol_send_text(&sUSART3, message, PROTOCOL_SOM_NOACK); + #endif +} diff --git a/platformio.ini b/platformio.ini index 2de82aef6..4fbb23159 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,6 +18,7 @@ src_dir = Src ;default_envs = VARIANT_HOVERCAR ; Variant for HOVERCAR build ;default_envs = VARIANT_HOVERBOARD ; Variant for HOVERBOARD ;default_envs = VARIANT_TRANSPOTTER ; Variant for TRANSPOTTER build https://github.com/NiklasFauth/hoverboard-firmware-hack/wiki/Build-Instruction:-TranspOtter https://hackaday.io/project/161891-transpotter-ng +default_envs = VARIANT_BIPROPELLANT ; Variant for Bipropellant Protocol ;================================================================ ;================================================================ @@ -206,5 +207,29 @@ build_flags = -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization # -Wl,-lnosys -D VARIANT_TRANSPOTTER - + ;================================================================ + +[env:VARIANT_BIPROPELLANT] +platform = ststm32 +framework = stm32cube +board = genericSTM32F103RC +debug_tool = stlink +upload_protocol = stlink + +; Serial Port settings (make sure the COM port is correct) +monitor_port = COM5 +monitor_speed = 38400 + +build_flags = + -I${PROJECT_DIR}/Src/hbprotocol/ + -DUSE_HAL_DRIVER + -DSTM32F103xE + -Wl,-T./STM32F103RCTx_FLASH.ld + -Wl,-lc + -Wl,-lm + -g -ggdb ; to generate correctly the 'firmware.elf' for STM STUDIO vizualization +# -Wl,-lnosys + -D VARIANT_BIPROPELLANT + +;================================================================ \ No newline at end of file From 69697ab8f36e91e5a0109799159eb1658a3c2708 Mon Sep 17 00:00:00 2001 From: phail Date: Mon, 15 Jun 2020 00:17:49 +0200 Subject: [PATCH 02/16] bipropellant protocol compiling, doesn't do anything yet --- Inc/comms.h | 72 +++++++++------- Inc/config.h | 29 +++++-- Inc/control_structures.h | 130 +++++++++++++++++++++++++++++ Inc/setup.h | 2 + Src/comms.c | 148 ++++++++++++++++++++++++++++++--- Src/main.c | 15 +++- Src/protocolfunctions.c | 175 +++++++-------------------------------- Src/setup.c | 93 +++++++++++++++++++++ platformio.ini | 11 ++- 9 files changed, 474 insertions(+), 201 deletions(-) create mode 100644 Inc/control_structures.h diff --git a/Inc/comms.h b/Inc/comms.h index 6bdb730df..a94fcb0da 100644 --- a/Inc/comms.h +++ b/Inc/comms.h @@ -1,33 +1,47 @@ -/* -* This file is part of the hoverboard-firmware-hack project. -* -* Copyright (C) 2017-2018 Rene Hopf -* Copyright (C) 2017-2018 Nico Stute -* Copyright (C) 2017-2018 Niklas Fauth -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -*/ - -// Define to prevent recursive inclusion -#ifndef COMMS_H -#define COMMS_H - -#include "stm32f1xx_hal.h" +#pragma once -void setScopeChannel(uint8_t ch, int16_t val); -void consoleScope(void); -void consoleLog(char *message); +#define SERIAL_USART_BUFFER_SIZE 1024 // TODO: implement send_wait routine.. +typedef struct tag_serial_usart_buffer { + SERIAL_USART_IT_BUFFERTYPE buff[SERIAL_USART_BUFFER_SIZE]; + int head; + int tail; + + // count of buffer overflows + unsigned int overflow; + +} SERIAL_USART_BUFFER; + +#if defined(SERIAL_USART2_IT) + + extern volatile SERIAL_USART_BUFFER usart2_it_TXbuffer; + extern volatile SERIAL_USART_BUFFER usart2_it_RXbuffer; + + int USART2_IT_starttx(); + int USART2_IT_send(unsigned char *data, int len); + void USART2_IT_IRQ(USART_TypeDef *us); #endif +#if defined(SERIAL_USART3_IT) + + extern volatile SERIAL_USART_BUFFER usart3_it_TXbuffer; + extern volatile SERIAL_USART_BUFFER usart3_it_RXbuffer; + + int USART3_IT_starttx(); + int USART3_IT_send(unsigned char *data, int len); + void USART3_IT_IRQ(USART_TypeDef *us); + +#endif + +int serial_usart_buffer_count(volatile SERIAL_USART_BUFFER *usart_buf); +void serial_usart_buffer_push (volatile SERIAL_USART_BUFFER *usart_buf, SERIAL_USART_IT_BUFFERTYPE value); +SERIAL_USART_IT_BUFFERTYPE serial_usart_buffer_pop (volatile SERIAL_USART_BUFFER *usart_buf); + + + +void setScopeChannel(uint8_t ch, int16_t val); +void consoleScope(); +///////////////////////////////////////////////////////// + + +#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) \ No newline at end of file diff --git a/Inc/config.h b/Inc/config.h index ccddf5835..e4310273b 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -74,7 +74,7 @@ #define BAT_BLINK_INTERVAL 80 // battery led blink interval (80 loops * 5ms ~= 400ms) #define BAT_LVL5 (390 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Green blink: no beep #define BAT_LVL4 (380 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Yellow: no beep -#define BAT_LVL3 (370 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Yellow blink: no beep +#define BAT_LVL3 (370 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Yellow blink: no beep #define BAT_LVL2 (360 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Red: gently beep at this voltage level. [V*100/cell]. In this case 3.60 V/cell #define BAT_LVL1 (350 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // Red blink: fast beep. Your battery is almost empty. Charge now! [V*100/cell]. In this case 3.50 V/cell #define BAT_DEAD (337 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE // All leds off: undervoltage poweroff. (while not driving) [V*100/cell]. In this case 3.37 V/cell @@ -253,6 +253,17 @@ #endif // ######################## END OF VARIANT_USART SETTINGS ######################### +// ############################ VARIANT_BIPROPELLANT SETTINGS ############################ +#ifdef VARIANT_BIPROPELLANT +// #define SERIAL_USART2_IT + #define SERIAL_USART3_IT + #define USART2_BAUD 115200 + #define USART3_BAUD 115200 +#endif + #define SERIAL_USART_IT_BUFFERTYPE unsigned char + #define USART2_WORDLENGTH UART_WORDLENGTH_8B + #define USART3_WORDLENGTH UART_WORDLENGTH_8B +// ######################## END OF VARIANT_USART SETTINGS ######################### // ################################# VARIANT_NUNCHUK SETTINGS ############################ @@ -319,7 +330,7 @@ #define PWM_CH1_MAX 1000 // (0 - 1000) #define PWM_CH1_MIN -1000 // (-1000 - 0) #define PWM_CH2_MAX 1000 // (0 - 1000) - #define PWM_CH2_MIN -1000 // (-1000 - 0) + #define PWM_CH2_MIN -1000 // (-1000 - 0) #define FILTER 6553 // 0.1f [-] fixdt(0,16,16) lower value == softer filter [0, 65535] = [0.0 - 1.0]. #define SPEED_COEFFICIENT 16384 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14 #define STEER_COEFFICIENT 16384 // 1.0f [-] fixdt(1,16,14) higher value == stronger. [0, 65535] = [-2.0 - 2.0]. In this case 16384 = 1.0 * 2^14. If you do not want any steering, set it to 0. @@ -334,7 +345,7 @@ // ################################# VARIANT_IBUS SETTINGS ############################## #ifdef VARIANT_IBUS -/* CONTROL VIA RC REMOTE WITH FLYSKY IBUS PROTOCOL +/* CONTROL VIA RC REMOTE WITH FLYSKY IBUS PROTOCOL * Connected to Left sensor board cable. Channel 1: steering, Channel 2: speed. */ #define CONTROL_IBUS // use IBUS as input @@ -365,11 +376,11 @@ #define ADC2_MAX 2200 // max ADC2-value while poti at maximum-position (0 - 4095) #define SPEED_COEFFICIENT 16384 // 1.0f #define STEER_COEFFICIENT 0 // 0.0f - // #define INVERT_R_DIRECTION // Invert rotation of right motor - // #define INVERT_L_DIRECTION // Invert rotation of left motor + // #define INVERT_R_DIRECTION // Invert rotation of right motor + // #define INVERT_L_DIRECTION // Invert rotation of left motor #define SIDEBOARD_SERIAL_USART3 #define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! - // #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! + // #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! #endif // Multiple tap detection: default DOUBLE Tap on Brake pedal (4 pulses) @@ -385,10 +396,10 @@ // Communication: [DONE] // Balancing controller: [TODO] #ifdef VARIANT_HOVERBOARD - #define SIDEBOARD_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! + #define SIDEBOARD_SERIAL_USART2 // left sensor board cable, disable if ADC or PPM is used! #define FEEDBACK_SERIAL_USART2 - #define SIDEBOARD_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! - #define FEEDBACK_SERIAL_USART3 + #define SIDEBOARD_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! + #define FEEDBACK_SERIAL_USART3 #endif // ######################## END OF VARIANT_HOVERBOARD SETTINGS ######################### diff --git a/Inc/control_structures.h b/Inc/control_structures.h new file mode 100644 index 000000000..6325d88b1 --- /dev/null +++ b/Inc/control_structures.h @@ -0,0 +1,130 @@ +/* +* This file is part of the hoverboard-firmware-hack project. +* +* Copyright (C) 2018 Simon Hailes +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +//////////////////////////////////////////////////////////////////// +// structures used in main repo which may mirror protocol structures +//////////////////////////////////////////////////////////////////// +#pragma once +#include "protocol.h" + +//// control structures used in firmware + +// used in main +extern PROTOCOL_POSN_DATA PosnData; + +extern PROTOCOL_SPEED_DATA SpeedData; + +extern PROTOCOL_PWM_DATA PWMData; + + +#pragma pack(push, 4) // int32_t and float are 4 byte each +typedef struct tag_HALL_DATA_STRUCT{ + int32_t HallPosn; // 90 per revolution + int32_t HallSpeed; // speed part calibrated to speed demand value + + float HallPosnMultiplier; // m per hall segment + + int32_t HallPosn_lastread; // posn offset set via protocol in raw value + int32_t HallPosn_mm; // posn in mm + int32_t HallPosn_mm_lastread; // posn offset set via protocol in mm + int32_t HallSpeed_mm_per_s; // speed in m/s + + uint32_t HallTimeDiff; + uint32_t HallSkipped; +} HALL_DATA_STRUCT; +#pragma pack(pop) + +#pragma pack(push, 4) // all used types (float and int) are 4 bytes + +typedef struct tag_MOTOR_ELECTRICAL{ + float dcAmps; + float dcAmpsAvgAcc; + float dcAmpsAvg; + int r1; + int r2; + int q; + + int dcAmpsx100; + + int pwm_limiter; + int pwm_requested; + int pwm_actual; + + unsigned int limiter_count; +} MOTOR_ELECTRICAL; +#pragma pack(pop) + +#pragma pack(push, 4) // all used types (float and int) are 4 bytes +typedef struct tag_ELECTRICAL_PARAMS{ + int bat_raw; + float batteryVoltage; + + int board_temp_raw; + float board_temp_filtered; + float board_temp_deg_c; + + int charging; + + int dcCurLim; // amps*100 + int dc_adc_limit; // limit expressed in terms of ADC units. + + MOTOR_ELECTRICAL motors[2]; + +} ELECTRICAL_PARAMS; +#pragma pack(pop) + +#pragma pack(push, 1) +typedef struct tag_sensor_frame{ + unsigned char header_00; // this byte gets 0x100 (9 bit serial) + short Angle; + short Angle_duplicate; + unsigned char AA_55; + unsigned char Accelleration; + unsigned char Accelleration_duplicate; + short Roll; +} SENSOR_FRAME; +#pragma pack(pop) + +#pragma pack(push, 4) // since on 'int32_t' are used, alignment can be optimized for 4 bytes +typedef struct INTEGER_XYT_POSN_tag { + int32_t x; + int32_t y; + int32_t degrees; +} INTEGER_XYT_POSN; +#pragma pack(pop) + + +#pragma pack(push, 4) // all used data types are 4 byte +typedef struct tag_POSN { + int32_t LeftAbsolute; + int32_t RightAbsolute; + int32_t LeftOffset; + int32_t RightOffset; +} POSN; +#pragma pack(pop) + +#pragma pack(push, 4) // all used data types are 4 byte +typedef struct tag_POSN_INCR { + int32_t Left; + int32_t Right; +} POSN_INCR; +#pragma pack(pop) + + +extern int control_type; \ No newline at end of file diff --git a/Inc/setup.h b/Inc/setup.h index b7dd9d033..d3e18ffdf 100644 --- a/Inc/setup.h +++ b/Inc/setup.h @@ -31,6 +31,8 @@ void MX_ADC1_Init(void); void MX_ADC2_Init(void); void UART2_Init(void); void UART3_Init(void); +void USART2_IT_init(); +void USART3_IT_init(); #endif diff --git a/Src/comms.c b/Src/comms.c index 61ee47dbe..48924e219 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -11,6 +11,19 @@ extern UART_HandleTypeDef huart3; static volatile uint8_t uart_buf[100]; static volatile int16_t ch_buf[8]; + +#if defined(SERIAL_USART2_IT) + volatile SERIAL_USART_BUFFER usart2_it_TXbuffer; + volatile SERIAL_USART_BUFFER usart2_it_RXbuffer; +#endif +#if defined(SERIAL_USART3_IT) + volatile SERIAL_USART_BUFFER usart3_it_TXbuffer; + volatile SERIAL_USART_BUFFER usart3_it_RXbuffer; +#endif + +uint8_t debug_out = 1; +uint8_t enablescope = 1; + //volatile char char_buf[300]; void setScopeChannel(uint8_t ch, int16_t val) { @@ -18,6 +31,9 @@ void setScopeChannel(uint8_t ch, int16_t val) { } void consoleScope(void) { + if (!enablescope) + return; + #if defined DEBUG_SERIAL_SERVOTERM && (defined DEBUG_SERIAL_USART2 || defined DEBUG_SERIAL_USART3) uart_buf[0] = 0xff; uart_buf[1] = CLAMP(ch_buf[0]+127, 0, 255); @@ -64,18 +80,126 @@ void consoleScope(void) { } -void consoleLog(char *message) -{ - #if defined DEBUG_SERIAL_ASCII && (defined DEBUG_SERIAL_USART2 || defined DEBUG_SERIAL_USART3) - #ifdef DEBUG_SERIAL_USART2 - if(__HAL_DMA_GET_COUNTER(huart2.hdmatx) == 0) { - HAL_UART_Transmit_DMA(&huart2, (uint8_t *)message, strlen((char *)(uintptr_t)message)); +#ifdef SERIAL_USART2_IT + +int USART2_IT_starttx() { + __HAL_UART_ENABLE_IT(&huart2, UART_IT_TXE); + return 1; +} + +int USART2_IT_send(unsigned char *data, int len) { + + int count = serial_usart_buffer_count(&usart2_it_TXbuffer); + if (count + len + 1 > SERIAL_USART_BUFFER_SIZE-3){ + usart2_it_TXbuffer.overflow++; + return -1; } - #endif - #ifdef DEBUG_SERIAL_USART3 - if(__HAL_DMA_GET_COUNTER(huart3.hdmatx) == 0) { - HAL_UART_Transmit_DMA(&huart3, (uint8_t *)message, strlen((char *)(uintptr_t)message)); + + for (int i = 0; i < len; i++){ + serial_usart_buffer_push(&usart2_it_TXbuffer, (SERIAL_USART_IT_BUFFERTYPE) data[i]); } - #endif - #endif + + return USART2_IT_starttx(); +} + +////////////////////////////////////////////////////// +// called from actual IRQ routines +void USART2_IT_IRQ(USART_TypeDef *us) { + volatile uint32_t *SR = &us->SR; // USART Status register + volatile uint32_t *DR = &us->DR; // USART Data register + volatile uint32_t *CR1 = &us->CR1; // USART Control register 1 + + // Transmit + if ((*SR) & UART_FLAG_TXE) { + if (serial_usart_buffer_count(&usart2_it_TXbuffer) == 0) { + *CR1 = (*CR1 & ~(USART_CR1_TXEIE | USART_CR1_TCIE)); + } else { + *DR = (serial_usart_buffer_pop(&usart2_it_TXbuffer) & 0x1ff); + } + } + + // Receive + if (((*SR) & UART_FLAG_RXNE)) { + SERIAL_USART_IT_BUFFERTYPE rword = (*DR) & 0x01FF; + serial_usart_buffer_push(&usart2_it_RXbuffer, rword); + } + + return; +} + +#endif + +#ifdef SERIAL_USART3_IT + +int USART3_IT_starttx() { + __HAL_UART_ENABLE_IT(&huart3, UART_IT_TXE); + return 1; +} + +int USART3_IT_send(unsigned char *data, int len) { + + int count = serial_usart_buffer_count(&usart3_it_TXbuffer); + if (count + len + 1 > SERIAL_USART_BUFFER_SIZE-3){ + usart3_it_TXbuffer.overflow++; + return -1; + } + + for (int i = 0; i < len; i++){ + serial_usart_buffer_push(&usart3_it_TXbuffer, (SERIAL_USART_IT_BUFFERTYPE) data[i]); + } + + return USART3_IT_starttx(); +} + +////////////////////////////////////////////////////// +// called from actual IRQ routines +void USART3_IT_IRQ(USART_TypeDef *us) { + volatile uint32_t *SR = &us->SR; // USART Status register + volatile uint32_t *DR = &us->DR; // USART Data register + volatile uint32_t *CR1 = &us->CR1; // USART Control register 1 + + // Transmit + if ((*SR) & UART_FLAG_TXE) { + if (serial_usart_buffer_count(&usart3_it_TXbuffer) == 0) { + *CR1 = (*CR1 & ~(USART_CR1_TXEIE | USART_CR1_TCIE)); + } else { + *DR = (serial_usart_buffer_pop(&usart3_it_TXbuffer) & 0x1ff); + } + } + + // Receive + if (((*SR) & UART_FLAG_RXNE)) { + SERIAL_USART_IT_BUFFERTYPE rword = (*DR) & 0x01FF; + serial_usart_buffer_push(&usart3_it_RXbuffer, rword); + } + + return; +} + +#endif + +int serial_usart_buffer_count(volatile SERIAL_USART_BUFFER *usart_buf) { + int count = usart_buf->head - usart_buf->tail; + if (count < 0) count += SERIAL_USART_BUFFER_SIZE; + return count; +} + +void serial_usart_buffer_push(volatile SERIAL_USART_BUFFER *usart_buf, SERIAL_USART_IT_BUFFERTYPE value) { + int count = serial_usart_buffer_count(usart_buf); + if (count >= SERIAL_USART_BUFFER_SIZE-2){ + usart_buf->overflow++; + return; + } + + usart_buf->buff[usart_buf->head] = value; + usart_buf->head = ((usart_buf->head + 1 ) % SERIAL_USART_BUFFER_SIZE); +} + +SERIAL_USART_IT_BUFFERTYPE serial_usart_buffer_pop(volatile SERIAL_USART_BUFFER *usart_buf) { + SERIAL_USART_IT_BUFFERTYPE t = 0; + if (usart_buf->head != usart_buf->tail){ + t = usart_buf->buff[usart_buf->tail]; + usart_buf->tail = ((usart_buf->tail + 1 ) % SERIAL_USART_BUFFER_SIZE); + } + return t; } diff --git a/Src/main.c b/Src/main.c index 35f594ad1..21da061f8 100644 --- a/Src/main.c +++ b/Src/main.c @@ -197,10 +197,17 @@ int main(void) { int16_t board_temp_adcFilt = adc_buffer.temp; int16_t board_temp_deg_c; - #if defined(CONTROL_SERIAL_USART2) && defined(VARIANT_BIPROPELLANT) + #ifdef SERIAL_USART2_IT + USART2_IT_init(); + #endif + #ifdef SERIAL_USART3_IT + USART3_IT_init(); + #endif + + #if defined(SERIAL_USART2_IT) && defined(VARIANT_BIPROPELLANT) setup_protocol(&sUSART2); #endif - #if defined(CONTROL_SERIAL_USART3) && defined(VARIANT_BIPROPELLANT) + #if defined(SERIAL_USART3_IT) && defined(VARIANT_BIPROPELLANT) setup_protocol(&sUSART3); #endif @@ -211,13 +218,13 @@ int main(void) { readCommand(); // Read Command: cmd1, cmd2 calcAvgSpeed(); // Calculate average measured speed: speedAvg, speedAvgAbs - #if defined(CONTROL_SERIAL_USART2) && defined(VARIANT_BIPROPELLANT) + #if defined(SERIAL_USART2_IT) && defined(VARIANT_BIPROPELLANT) while ( serial_usart_buffer_count(&usart2_it_RXbuffer) > 0 ) { protocol_byte( &sUSART2, (unsigned char) serial_usart_buffer_pop(&usart2_it_RXbuffer) ); } protocol_tick( &sUSART2 ); #endif - #if defined(CONTROL_SERIAL_USART3) && defined(VARIANT_BIPROPELLANT) + #if defined(SERIAL_USART3_IT) && defined(VARIANT_BIPROPELLANT) while ( serial_usart_buffer_count(&usart3_it_RXbuffer) > 0 ) { protocol_byte( &sUSART3, (unsigned char) serial_usart_buffer_pop(&usart3_it_RXbuffer) ); } diff --git a/Src/protocolfunctions.c b/Src/protocolfunctions.c index df8de7d1f..c4e3d71cc 100644 --- a/Src/protocolfunctions.c +++ b/Src/protocolfunctions.c @@ -1,27 +1,17 @@ #include "defines.h" #include "config.h" #include "protocolfunctions.h" -#include "bldc.h" -#include "softwareserial.h" -#include "flashcontent.h" -#include "flashaccess.h" + #include "comms.h" #include "stm32f1xx_hal.h" -#ifdef CONTROL_SENSOR - #include "sensorcoms.h" -#endif -#include "hallinterrupts.h" -#include "deadreckoner.h" + #include #include #include "control_structures.h" -#ifdef SOFTWARE_SERIAL - PROTOCOL_STAT sSoftwareSerial; -#endif #if defined(SERIAL_USART2_IT) PROTOCOL_STAT sUSART2; #endif @@ -55,15 +45,15 @@ void fn_enable ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL if (!protocol_enable) { // assume we will enable, // set wanted posn to current posn, else we may rush into a wall - PosnData.wanted_posn_mm[0] = HallData[0].HallPosn_mm; - PosnData.wanted_posn_mm[1] = HallData[1].HallPosn_mm; +// PosnData.wanted_posn_mm[0] = HallData[0].HallPosn_mm; +// PosnData.wanted_posn_mm[1] = HallData[1].HallPosn_mm; // clear speeds to zero SpeedData.wanted_speed_mm_per_sec[0] = 0; SpeedData.wanted_speed_mm_per_sec[1] = 0; PWMData.pwm[0] = 0; PWMData.pwm[1] = 0; - init_PID_control(); +// init_PID_control(); } enable = protocol_enable; break; @@ -116,8 +106,8 @@ void fn_SpeedData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTO switch (cmd) { case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: - control_type = CONTROL_TYPE_SPEED; - input_timeout_counter = 0; +// control_type = CONTROL_TYPE_SPEED; +// input_timeout_counter = 0; break; } fn_defaultProcessing(s, param, cmd, msg); @@ -132,10 +122,12 @@ void fn_Position ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOC switch (cmd) { case PROTOCOL_CMD_READVAL: case PROTOCOL_CMD_SILENTREAD: + /* ((POSN*) (param->ptr))->LeftAbsolute = HallData[0].HallPosn_mm; ((POSN*) (param->ptr))->LeftOffset = HallData[0].HallPosn_mm - HallData[0].HallPosn_mm_lastread; ((POSN*) (param->ptr))->RightAbsolute = HallData[1].HallPosn_mm; ((POSN*) (param->ptr))->RightOffset = HallData[1].HallPosn_mm - HallData[1].HallPosn_mm_lastread; + */ break; } @@ -144,8 +136,10 @@ void fn_Position ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOC switch (cmd) { case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: + /* HallData[0].HallPosn_mm_lastread = ((POSN*) (param->ptr))->LeftAbsolute; HallData[1].HallPosn_mm_lastread = ((POSN*) (param->ptr))->RightAbsolute; + */ break; } } @@ -162,7 +156,7 @@ void fn_PositionIncr ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PR switch (cmd) { case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: - // if switching to control type POSITION, +/* // if switching to control type POSITION, if ((control_type != CONTROL_TYPE_POSITION) || !enable) { control_type = CONTROL_TYPE_POSITION; // then make sure we won't rush off somwehere strange @@ -176,6 +170,7 @@ void fn_PositionIncr ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PR // increment our wanted position PosnData.wanted_posn_mm[0] += ((POSN_INCR*) (param->ptr))->Left; PosnData.wanted_posn_mm[1] += ((POSN_INCR*) (param->ptr))->Right; + */ break; } } @@ -200,10 +195,12 @@ void fn_RawPosition ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PRO switch (cmd) { case PROTOCOL_CMD_READVAL: case PROTOCOL_CMD_SILENTREAD: + /* ((PROTOCOL_POSN*) (param->ptr))->LeftAbsolute = HallData[0].HallPosn; ((PROTOCOL_POSN*) (param->ptr))->LeftOffset = HallData[0].HallPosn - HallData[0].HallPosn_lastread; ((PROTOCOL_POSN*) (param->ptr))->RightAbsolute = HallData[1].HallPosn; ((PROTOCOL_POSN*) (param->ptr))->RightOffset = HallData[1].HallPosn - HallData[1].HallPosn_lastread; + */ break; } @@ -212,8 +209,10 @@ void fn_RawPosition ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PRO switch (cmd) { case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: + /* HallData[0].HallPosn_lastread = ((PROTOCOL_POSN*) (param->ptr))->LeftAbsolute; HallData[1].HallPosn_lastread = ((PROTOCOL_POSN*) (param->ptr))->RightAbsolute; + */ break; } } @@ -231,28 +230,6 @@ extern uint8_t disablepoweroff; extern uint8_t debug_out; -//////////////////////////////////////////////////////////////////////////////////////////// -// Variable & Functions for 0x0C xytPosn - -// ded reckoning posn -extern INTEGER_XYT_POSN xytPosn; - -void fn_xytPosn ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { - - fn_defaultProcessing(s, param, cmd, msg); - - switch (cmd) { - case PROTOCOL_CMD_WRITEVAL: - case PROTOCOL_CMD_READVALRESPONSE: - if (deadreconer) { - // reset xyt - reset( deadreconer, 1 ); - } - break; - } - -} - //////////////////////////////////////////////////////////////////////////////////////////// // Variable & Functions for 0x0D PWMData and 0x0E PWMData.pwm @@ -271,8 +248,8 @@ void fn_PWMData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCO switch (cmd) { case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: - control_type = CONTROL_TYPE_PWM; - input_timeout_counter = 0; +// control_type = CONTROL_TYPE_PWM; +// input_timeout_counter = 0; break; } @@ -316,9 +293,11 @@ void fn_BuzzerData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROT switch (cmd) { case PROTOCOL_CMD_READVAL: case PROTOCOL_CMD_SILENTREAD: + /* ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerFreq = buzzerFreq; ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerLen = buzzerLen; ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerPattern = buzzerPattern; + */ break; } @@ -327,9 +306,11 @@ void fn_BuzzerData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROT switch (cmd) { case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: + /* buzzerFreq = ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerFreq; buzzerLen = ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerLen; buzzerPattern = ((PROTOCOL_BUZZER_DATA*) (param->ptr))->buzzerPattern; + */ break; } } @@ -342,56 +323,7 @@ void fn_BuzzerData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROT extern void change_PID_constants(); extern void init_PID_control(); -extern volatile ELECTRICAL_PARAMS electrical_measurements; - -void fn_FlashContentMagic ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { - - fn_defaultProcessing(s, param, cmd, msg); - - switch (cmd) { - case PROTOCOL_CMD_WRITEVAL: - case PROTOCOL_CMD_READVALRESPONSE: - if (FlashContent.magic != CURRENT_MAGIC){ - char temp[128]; - sprintf(temp, "incorrect magic %d, should be %d\r\nFlash not written\r\n", FlashContent.magic, CURRENT_MAGIC); - consoleLog(temp); - FlashContent.magic = CURRENT_MAGIC; - return; - } - writeFlash( (unsigned char *)&FlashContent, sizeof(FlashContent) ); - consoleLog("wrote flash\r\n"); - break; - } -} - -void fn_FlashContentPID ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { - - fn_defaultProcessing(s, param, cmd, msg); - - switch (cmd) { - case PROTOCOL_CMD_WRITEVAL: - case PROTOCOL_CMD_READVALRESPONSE: - change_PID_constants(); - break; - } -} - -void fn_FlashContentMaxCurrLim ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { - - fn_defaultProcessing(s, param, cmd, msg); - - switch (cmd) { - case PROTOCOL_CMD_WRITEVAL: - case PROTOCOL_CMD_READVALRESPONSE: - electrical_measurements.dcCurLim = MIN(DC_CUR_LIMIT, FlashContent.MaxCurrLim / 100); - break; - } -} - - - - - +//extern volatile ELECTRICAL_PARAMS electrical_measurements; @@ -408,19 +340,6 @@ int setup_protocol(PROTOCOL_STAT *s) { int errors = 0; - - #ifdef SOFTWARE_SERIAL - - errors += protocol_init(&sSoftwareSerial); - - sSoftwareSerial.send_serial_data=softwareserial_Send; - sSoftwareSerial.send_serial_data_wait=softwareserial_Send_Wait; - sSoftwareSerial.timeout1 = 500; - sSoftwareSerial.timeout2 = 100; - sSoftwareSerial.allow_ascii = 1; - - #endif - #if defined(SERIAL_USART2_IT) // initialise, even if CONTROL_SENSOR, as we may switch vuia software to use this.... extern int USART2_IT_send(unsigned char *data, int len); @@ -451,7 +370,7 @@ int setup_protocol(PROTOCOL_STAT *s) { // initialise ascii protocol functions - main_ascii_init(s); +// main_ascii_init(s); #ifdef CONTROL_SENSOR @@ -459,7 +378,7 @@ int setup_protocol(PROTOCOL_STAT *s) { setParamHandler( s, 0x01, fn_SensorData ); #endif - errors += setParamVariable( s, 0x02, UI_NONE, (void *)&HallData, sizeof(HallData) ); +// errors += setParamVariable( s, 0x02, UI_NONE, (void *)&HallData, sizeof(HallData) ); errors += setParamVariable( s, 0x03, UI_NONE, &SpeedData, sizeof(SpeedData) ); setParamHandler( s, 0x03, fn_SpeedData ); @@ -467,25 +386,22 @@ int setup_protocol(PROTOCOL_STAT *s) { errors += setParamVariable( s, 0x04, UI_NONE, &Position, sizeof(Position) ); setParamHandler( s, 0x04, fn_Position ); - errors += setParamVariable( s, 0x05, UI_NONE, &PositionIncr, sizeof(PositionIncr) ); - setParamHandler( s, 0x05, fn_PositionIncr ); + errors += setParamVariable( s, 0x06, UI_NONE, &PosnData, sizeof(PosnData) ); errors += setParamVariable( s, 0x07, UI_NONE, &RawPosition, sizeof(RawPosition) ); setParamHandler( s, 0x07, fn_RawPosition ); - errors += setParamVariable( s, 0x08, UI_NONE, (void *)&electrical_measurements, sizeof(ELECTRICAL_PARAMS) ); +// errors += setParamVariable( s, 0x08, UI_NONE, (void *)&electrical_measurements, sizeof(ELECTRICAL_PARAMS) ); errors += setParamVariable( s, 0x09, UI_CHAR, &protocol_enable, sizeof(enable) ); setParamHandler( s, 0x09, fn_enable ); - errors += setParamVariable( s, 0x0A, UI_CHAR, &disablepoweroff, sizeof(disablepoweroff) ); +// errors += setParamVariable( s, 0x0A, UI_CHAR, &disablepoweroff, sizeof(disablepoweroff) ); errors += setParamVariable( s, 0x0B, UI_CHAR, &debug_out, sizeof(debug_out) ); - errors += setParamVariable( s, 0x0C, UI_3LONG, &xytPosn, sizeof(xytPosn) ); - setParamHandler( s, 0x0C, fn_xytPosn ); errors += setParamVariable( s, 0x0D, UI_NONE, &PWMData, sizeof(PWMData) ); setParamHandler( s, 0x0D, fn_PWMData ); @@ -496,39 +412,6 @@ int setup_protocol(PROTOCOL_STAT *s) { errors += setParamVariable( s, 0x21, UI_NONE, &BuzzerData, sizeof(BuzzerData) ); setParamHandler( s, 0x21, fn_BuzzerData ); - errors += setParamVariable( s, 0x80, UI_SHORT, &FlashContent.magic, sizeof(short) ); - setParamHandler( s, 0x80, fn_FlashContentMagic ); - - errors += setParamVariable( s, 0x81, UI_SHORT, &FlashContent.PositionKpx100, sizeof(short) ); - setParamHandler( s, 0x81, fn_FlashContentPID ); - - errors += setParamVariable( s, 0x82, UI_SHORT, &FlashContent.PositionKix100, sizeof(short) ); - setParamHandler( s, 0x82, fn_FlashContentPID ); - - errors += setParamVariable( s, 0x83, UI_SHORT, &FlashContent.PositionKdx100, sizeof(short) ); - setParamHandler( s, 0x83, fn_FlashContentPID ); - - errors += setParamVariable( s, 0x84, UI_SHORT, &FlashContent.PositionPWMLimit, sizeof(short) ); - setParamHandler( s, 0x84, fn_FlashContentPID ); - - errors += setParamVariable( s, 0x85, UI_SHORT, &FlashContent.SpeedKpx100, sizeof(short) ); - setParamHandler( s, 0x85, fn_FlashContentPID ); - - errors += setParamVariable( s, 0x86, UI_SHORT, &FlashContent.SpeedKix100, sizeof(short) ); - setParamHandler( s, 0x86, fn_FlashContentPID ); - - errors += setParamVariable( s, 0x87, UI_SHORT, &FlashContent.SpeedKdx100, sizeof(short) ); - setParamHandler( s, 0x87, fn_FlashContentPID ); - - errors += setParamVariable( s, 0x88, UI_SHORT, &FlashContent.SpeedPWMIncrementLimit, sizeof(short) ); - setParamHandler( s, 0x88, fn_FlashContentPID ); - - errors += setParamVariable( s, 0x89, UI_SHORT, &FlashContent.MaxCurrLim, sizeof(short) ); - setParamHandler( s, 0x89, fn_FlashContentMaxCurrLim ); - - errors += setParamVariable( s, 0x90, UI_NONE, &FlashContent.adc, sizeof(PROTOCOL_ADC_SETTINGS) ); - - errors += setParamVariable( s, 0xA0, UI_SHORT, &FlashContent.HoverboardEnable, sizeof(short) ); return errors; } diff --git a/Src/setup.c b/Src/setup.c index d859c9e61..41aefccff 100644 --- a/Src/setup.c +++ b/Src/setup.c @@ -38,6 +38,9 @@ pb10 usart3 dma1 channel2/3 #include "defines.h" #include "config.h" #include "setup.h" +#include "comms.h" +#include +#include "protocolfunctions.h" TIM_HandleTypeDef htim_right; TIM_HandleTypeDef htim_left; @@ -54,6 +57,10 @@ DMA_HandleTypeDef hdma_usart3_tx; volatile adc_buf_t adc_buffer; +#ifdef SERIAL_USART2_IT + static int USART2WordLength = USART2_WORDLENGTH; +#endif + #if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) /* USART2 init function */ void UART2_Init(void) @@ -278,6 +285,92 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) } #endif +#ifdef SERIAL_USART2_IT +void USART2_IT_init(){ + memset((void*)&usart2_it_TXbuffer, 0, sizeof(usart2_it_TXbuffer)); + memset((void*)&usart2_it_RXbuffer, 0, sizeof(usart2_it_RXbuffer)); + + HAL_NVIC_DisableIRQ(USART2_IRQn); + + memset(&huart2, 0, sizeof(huart2)); + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_USART2_CLK_ENABLE(); + + huart2.Instance = USART2; + huart2.Init.BaudRate = USART2_BAUD; + huart2.Init.WordLength = USART2WordLength; + huart2.Init.StopBits = UART_STOPBITS_1; + huart2.Init.Parity = UART_PARITY_NONE; + huart2.Init.Mode = UART_MODE_TX_RX; + huart2.Init.OverSampling = UART_OVERSAMPLING_16; + huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; + HAL_UART_Init(&huart2); + + GPIO_InitTypeDef GPIO_InitStruct; + memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct)); + GPIO_InitStruct.Pin = GPIO_PIN_2; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct)); + GPIO_InitStruct.Pin = GPIO_PIN_3; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + // start interrupt receive? + HAL_NVIC_SetPriority(USART2_IRQn, 1, 0); + HAL_NVIC_EnableIRQ(USART2_IRQn); + + __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE); +} +#endif + +#ifdef SERIAL_USART3_IT +void USART3_IT_init(){ + memset((void *)&usart3_it_TXbuffer, 0, sizeof(usart3_it_TXbuffer)); + memset((void *)&usart3_it_RXbuffer, 0, sizeof(usart3_it_RXbuffer)); + + memset(&huart3, 0, sizeof(huart3)); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_USART3_CLK_ENABLE(); + + huart3.Instance = USART3; + huart3.Init.BaudRate = USART3_BAUD; + huart3.Init.WordLength = USART3_WORDLENGTH; + huart3.Init.StopBits = UART_STOPBITS_1; + huart3.Init.Parity = UART_PARITY_NONE; + huart3.Init.Mode = UART_MODE_TX_RX; + huart3.Init.OverSampling = UART_OVERSAMPLING_16; + huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; + HAL_UART_Init(&huart3); + + GPIO_InitTypeDef GPIO_InitStruct; + memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct)); + GPIO_InitStruct.Pin = GPIO_PIN_10; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct)); + GPIO_InitStruct.Pin = GPIO_PIN_11; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + // start interrupt receive? + HAL_NVIC_SetPriority(USART3_IRQn, 1, 0); + HAL_NVIC_EnableIRQ(USART3_IRQn); + + __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE); +} +#endif + DMA_HandleTypeDef hdma_i2c2_rx; DMA_HandleTypeDef hdma_i2c2_tx; diff --git a/platformio.ini b/platformio.ini index 4fbb23159..03ec74ff3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,7 +18,7 @@ src_dir = Src ;default_envs = VARIANT_HOVERCAR ; Variant for HOVERCAR build ;default_envs = VARIANT_HOVERBOARD ; Variant for HOVERBOARD ;default_envs = VARIANT_TRANSPOTTER ; Variant for TRANSPOTTER build https://github.com/NiklasFauth/hoverboard-firmware-hack/wiki/Build-Instruction:-TranspOtter https://hackaday.io/project/161891-transpotter-ng -default_envs = VARIANT_BIPROPELLANT ; Variant for Bipropellant Protocol +;default_envs = VARIANT_BIPROPELLANT ; Variant for Bipropellant Protocol ;================================================================ ;================================================================ @@ -35,6 +35,7 @@ monitor_port = COM5 monitor_speed = 38400 build_flags = + -I${PROJECT_DIR}/Src/hbprotocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -58,6 +59,7 @@ monitor_port = COM5 monitor_speed = 38400 build_flags = + -I${PROJECT_DIR}/Src/hbprotocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -77,6 +79,7 @@ debug_tool = stlink upload_protocol = stlink build_flags = + -I${PROJECT_DIR}/Src/hbprotocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -96,6 +99,7 @@ debug_tool = stlink upload_protocol = stlink build_flags = + -I${PROJECT_DIR}/Src/hbprotocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -115,6 +119,7 @@ debug_tool = stlink upload_protocol = stlink build_flags = + -I${PROJECT_DIR}/Src/hbprotocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -134,6 +139,7 @@ debug_tool = stlink upload_protocol = stlink build_flags = + -I${PROJECT_DIR}/Src/hbprotocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -157,6 +163,7 @@ monitor_port = COM5 monitor_speed = 38400 build_flags = + -I${PROJECT_DIR}/Src/hbprotocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -180,6 +187,7 @@ monitor_port = COM5 monitor_speed = 38400 build_flags = + -I${PROJECT_DIR}/Src/hbprotocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -199,6 +207,7 @@ debug_tool = stlink upload_protocol = stlink build_flags = + -I${PROJECT_DIR}/Src/hbprotocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld From 6785f4cb6989a3366d735232f43cdaf9cae03fbc Mon Sep 17 00:00:00 2001 From: phail Date: Sun, 21 Jun 2020 13:28:07 +0200 Subject: [PATCH 03/16] add UART IRQ Function --- Src/stm32f1xx_it.c | 24 ++++++++++++++++++++++-- platformio.ini | 4 ++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Src/stm32f1xx_it.c b/Src/stm32f1xx_it.c index 278fcca71..886f77a5c 100644 --- a/Src/stm32f1xx_it.c +++ b/Src/stm32f1xx_it.c @@ -236,7 +236,7 @@ void DMA1_Channel5_IRQHandler(void) void EXTI3_IRQHandler(void) { __HAL_GPIO_EXTI_CLEAR_IT(PPM_PIN); - PPM_ISR_Callback(); + PPM_ISR_Callback(); } #endif #ifdef CONTROL_PPM_RIGHT @@ -259,7 +259,7 @@ void EXTI2_IRQHandler(void) void EXTI3_IRQHandler(void) { __HAL_GPIO_EXTI_CLEAR_IT(PWM_PIN_CH2); - PWM_ISR_CH2_Callback(); + PWM_ISR_CH2_Callback(); } #endif #ifdef CONTROL_PWM_RIGHT @@ -371,6 +371,26 @@ void USART3_IRQHandler(void) } #endif +///////////////////////////////////////// +// UART interrupts + +#if defined(SERIAL_USART2_IT) +void USART2_IT_IRQ(USART_TypeDef *us); + +void USART2_IRQHandler(void){ + USART2_IT_IRQ(USART2); +} +#endif + +#if defined(SERIAL_USART3_IT) +void USART3_IT_IRQ(USART_TypeDef *us); + +void USART3_IRQHandler(void){ + USART3_IT_IRQ(USART3); +} +#endif + + /******************************************************************************/ /* STM32F1xx Peripheral Interrupt Handlers */ /* Add here the Interrupt Handlers for the used peripherals. */ diff --git a/platformio.ini b/platformio.ini index 03ec74ff3..f0bcd2554 100644 --- a/platformio.ini +++ b/platformio.ini @@ -227,8 +227,8 @@ debug_tool = stlink upload_protocol = stlink ; Serial Port settings (make sure the COM port is correct) -monitor_port = COM5 -monitor_speed = 38400 +monitor_port = /dev/ttyUSB0 +monitor_speed = 115200 build_flags = -I${PROJECT_DIR}/Src/hbprotocol/ From 9d5d190152608d02181eeb78409ea3f34d33767d Mon Sep 17 00:00:00 2001 From: phail Date: Sun, 21 Jun 2020 17:22:36 +0200 Subject: [PATCH 04/16] add ASCII protocol enable pwm motor control --- Inc/control_structures.h | 5 +- Src/ascii_proto_funcs.c | 565 +++++++++++++++++++++++++++++++++++++++ Src/comms.c | 11 +- Src/main.c | 29 +- Src/protocolfunctions.c | 5 +- 5 files changed, 599 insertions(+), 16 deletions(-) create mode 100644 Src/ascii_proto_funcs.c diff --git a/Inc/control_structures.h b/Inc/control_structures.h index 6325d88b1..53f71dbcf 100644 --- a/Inc/control_structures.h +++ b/Inc/control_structures.h @@ -124,7 +124,4 @@ typedef struct tag_POSN_INCR { int32_t Left; int32_t Right; } POSN_INCR; -#pragma pack(pop) - - -extern int control_type; \ No newline at end of file +#pragma pack(pop) \ No newline at end of file diff --git a/Src/ascii_proto_funcs.c b/Src/ascii_proto_funcs.c new file mode 100644 index 000000000..c9f90b010 --- /dev/null +++ b/Src/ascii_proto_funcs.c @@ -0,0 +1,565 @@ +/* +* This file is part of the hoverboard-firmware-hack project. +* +* Copyright (C) 2018 Simon Hailes +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +#include "stm32f1xx_hal.h" +#include "defines.h" +#include "config.h" + + +#include "protocolfunctions.h" +#include "protocol.h" +#include "comms.h" + +#include +#include +#include +#include "control_structures.h" + + +////////////////////////////////////////////////////////// +// +// ASCII protocol: +// this accepts command sup to 10 bytes long terminated with CR. +// one of these commands (I) can enable an 'immediate' mode. +// In 'immediate' mode, keypresses cause immediate action; +// for example, controlling speed, or getting real-time feedback. +// +////////////////////////////////////////////////////////// + + +/////////////////////////////////////////////// +// extern variables you want to read/write here +#ifdef CONTROL_SENSOR +extern SENSOR_DATA sensor_data[2]; +extern int sensor_control; +extern int sensor_stabilise; +#endif + +// from main.c +extern void change_PID_constants(); + +extern uint8_t enable; // global variable for motor enable + + +extern uint8_t debug_out; +extern uint8_t buzzerFreq; // global variable for the buzzer pitch. can be 1, 2, 3, 4, 5, 6, 7... +extern uint8_t buzzerPattern; // global variable for the buzzer pattern. can be 1, 2, 3, 4, 5, 6, 7... +extern uint8_t enablescope; // enable scope on values + +static int speedB = 0; +static int steerB = 0; + + +/////////////////////////////////////////////// + + +extern int protocol_post(PROTOCOL_STAT *s, PROTOCOL_MSG3full *msg); + + + +int immediate_dir(PROTOCOL_STAT *s, char byte, char *ascii_out) { + int dir = 1; + switch(byte){ + case 'S': + case 's': + dir = -1; + case 'W': + case 'w': + if (!enable) { speedB = 0; steerB = 0; } + enable = 1; + speedB += 10*dir; + PWMData.pwm[1] = CLAMP(speedB - steerB, -1000, 1000); + PWMData.pwm[0] = CLAMP(speedB + steerB, -1000, 1000); + sprintf(ascii_out, "speed now %d, steer now %d, pwm %ld, pwm %ld\r\n", speedB, steerB, PWMData.pwm[0], PWMData.pwm[1]); + break; + + case 'A': + case 'a': + dir = -1; + case 'D': + case 'd': + if (!enable) { speedB = 0; steerB = 0; } + enable = 1; + steerB += 10*dir; + PWMData.pwm[1] = CLAMP(speedB - steerB, -1000, 1000); + PWMData.pwm[0] = CLAMP(speedB + steerB, -1000, 1000); + sprintf(ascii_out, "speed now %d, steer now %d, pwm %ld, pwm %ld\r\n", speedB, steerB, PWMData.pwm[0], PWMData.pwm[1]); + break; + } + + return 1; +} + + +int immediate_stop(PROTOCOL_STAT *s, char byte, char *ascii_out) { + speedB = 0; + steerB = 0; + PWMData.pwm[1] = CLAMP(speedB - steerB, -1000, 1000); + PWMData.pwm[0] = CLAMP(speedB + steerB, -1000, 1000); + SpeedData.wanted_speed_mm_per_sec[0] = SpeedData.wanted_speed_mm_per_sec[1] = speedB; + enable = 0; + sprintf(ascii_out, "Stop set\r\n"); + return 1; +} + +int immediate_quit(PROTOCOL_STAT *s, char byte, char *ascii_out) { + s->ascii.enable_immediate = 0; + speedB = 0; + steerB = 0; + PWMData.pwm[1] = CLAMP(speedB - steerB, -1000, 1000); + PWMData.pwm[0] = CLAMP(speedB + steerB, -1000, 1000); + SpeedData.wanted_speed_mm_per_sec[0] = SpeedData.wanted_speed_mm_per_sec[1] = speedB; +#ifdef CONTROL_SENSOR + sensor_control = 0; +#endif + enable = 0; + sprintf(ascii_out, "Immediate commands disabled\r\n"); + return 1; +} + +int immediate_hall(PROTOCOL_STAT *s, char byte, char *ascii_out) { + return 1; +} + +int immediate_sensors(PROTOCOL_STAT *s, char byte, char *ascii_out) { + sprintf(ascii_out, "Sensor Data not available\r\n"); + return 1; +} + +int immediate_electrical(PROTOCOL_STAT *s, char byte, char *ascii_out) { + return 1; +} + +int immediate_stm32(PROTOCOL_STAT *s, char byte, char *ascii_out) { +// case 'G': + sprintf(ascii_out, + "A:%04X B:%04X C:%04X D:%04X E:%04X\r\n"\ + "Button: %d Charge:%d\r\n", + (int)GPIOA->IDR, (int)GPIOB->IDR, (int)GPIOC->IDR, (int)GPIOD->IDR, (int)GPIOE->IDR, + (int)(BUTTON_PORT->IDR & BUTTON_PIN)?1:0, + (int)(CHARGER_PORT->IDR & CHARGER_PIN)?1:0 + ); + return 1; +} + +int immediate_toggle_control(PROTOCOL_STAT *s, char byte, char *ascii_out) { +//case 'O': + //stop all + immediate_stop(s, byte, ascii_out); + ascii_out += strlen(ascii_out); + return 1; +} + +int line_set_alarm(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 'A': + int a = 0; + int b = 0; + int c = 0; + if (strlen(cmd) > 1){ + sscanf(cmd+1, "%d %d %d", &a, &b, &c); + } + if (a && (0==c)){ + c = 1000; + } + + buzzerFreq = a; + buzzerPattern = b; + sprintf(ascii_out, "Alarm set to %d %d %d\r\n", a, b, c); + return 1; +} + +int line_toggle_sensor_control(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +#ifdef CONTROL_SENSOR +//case 'B': + sensor_control ^= 1; + control_type = 0; + speedB = 0; + steerB = 0; + SpeedData.wanted_speed_mm_per_sec[0] = SpeedData.wanted_speed_mm_per_sec[1] = speedB; + PosnData.wanted_posn_mm[0] = HallData[0].HallPosn_mm; + PosnData.wanted_posn_mm[1] = HallData[1].HallPosn_mm; + sprintf(ascii_out, "Sensor control now %d\r\n", sensor_control); +#endif + return 1; +} + + +int line_electrical(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 'c': + immediate_electrical(s, *cmd, ascii_out); + return 1; +} + +int line_main_timing_stats(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 's': // display stats from main timing + // we don't have float printing + return 1; +} + + +int line_debug_control(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 'E': + if (strlen(cmd) == 1){ + debug_out = 0; + enablescope = 0; + } else { + if ((cmd[1] | 0x20) == 's'){ + enablescope = 1; + debug_out = 1; + } + if ((cmd[1] | 0x20) == 'c'){ + debug_out = 1; + } + } + sprintf(ascii_out, "debug_out now %d\r\nenablescope now %d\r\n", debug_out, enablescope); + return 1; +} + + + +// NOTE: needs params +int line_generic_var(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 'f': // setting any parameter marked with uistr + int len = strlen(cmd); + if (len == 1){ + sprintf(ascii_out, "no flash var given\r\n"); + } else { + if ((cmd[1] | 0x20) == 'i'){ // initilaise + sprintf(ascii_out, "Flash initialised\r\n"); + } else { + if ((cmd[1] | 0x20) == 'a'){ + // read all + for (int i = 0; i < (sizeof(s->params)/sizeof(s->params[0])); i++){ + if(s->params[i] != NULL) { + if (s->params[i]->uistr){ + switch (s->params[i]->ui_type){ + case UI_SHORT: + { + // read it + + PROTOCOL_MSG3full newMsg; + memset((void*)&newMsg,0x00,sizeof(PROTOCOL_MSG3full)); + + if (s->params[i]->fn) s->params[i]->fn( s, s->params[i], PROTOCOL_CMD_SILENTREAD, &newMsg); + + sprintf(ascii_out, "%s(%s): %d\r\n", + (s->params[i]->description)?s->params[i]->description:"", + s->params[i]->uistr, + (int)*(short *)newMsg.content); + s->send_serial_data_wait((unsigned char *)ascii_out, strlen(ascii_out)); + ascii_out[0] = 0; // don't print last one twice + break; + } + default: + break; + } + } + } + } + } else { + int i = 0; + int count = (sizeof(s->params)/sizeof(s->params[0])); + for (i = 0; i < count; i++){ + if(s->params[i] != NULL) { + if (s->params[i]->uistr){ + if (!strncmp(s->params[i]->uistr, &cmd[1], strlen(s->params[i]->uistr))){ + switch (s->params[i]->ui_type){ + case UI_SHORT: + // if number supplied, write + if ((cmd[1+strlen(s->params[i]->uistr)] >= '0') && (cmd[1+strlen(s->params[i]->uistr)] <= '9')){ + + PROTOCOL_MSG3full newMsg; + memset((void*)&newMsg,0x00,sizeof(PROTOCOL_MSG3full)); + + *((short *)newMsg.content) = atoi(&cmd[1+strlen(s->params[i]->uistr)]); + newMsg.code = s->params[i]->code; + newMsg.cmd = PROTOCOL_CMD_READVALRESPONSE; + + if (s->params[i]->fn) s->params[i]->fn( s, s->params[i], PROTOCOL_CMD_READVALRESPONSE, &newMsg); + + + sprintf(ascii_out, "flash var %s(%s) now %d\r\n", + (s->params[i]->description)?s->params[i]->description:"", + s->params[i]->uistr, + (int)*(short *)s->params[i]->ptr); + } else { + // read it + PROTOCOL_MSG3full newMsg; + memset((void*)&newMsg,0x00,sizeof(PROTOCOL_MSG3full)); + + if (s->params[i]->fn) s->params[i]->fn( s, s->params[i], PROTOCOL_CMD_SILENTREAD, &newMsg); + + sprintf(ascii_out, "%s(%s): %d\r\n", + (s->params[i]->description)?s->params[i]->description:"", + s->params[i]->uistr, + (int)*(short *)s->params[i]->ptr + ); + s->send_serial_data_wait((unsigned char *)ascii_out, strlen(ascii_out)); + ascii_out[0] = 0; // don't print last one twice + } + break; + default: + sprintf(ascii_out, "flash var %s(%s) unsupported type\r\n", + (s->params[i]->description)?s->params[i]->description:"", + s->params[i]->uistr + ); + break; + } + break; // found our param, now leave + } + } + } + } + if (i == count){ + sprintf(ascii_out, "unknown flash data %s\r\n", cmd); + } + } + } + } + return 1; +} + + +// must be after all params added? +char *get_F_description(PROTOCOL_STAT *s) { + + char *p = NULL; + int len = 0; + + // first loop gets len, 2nd loop makes string + for (int l = 0; l < 2; l++) { + char *t = "print/set a flash constant (Fa to print all, Fi to default all):\r\n" + " Fss - print, Fss - set\r\n"; + + len = strlen(t); + + if (p) strcat(p, t); + + for (int i = 0; i < (sizeof(s->params)/sizeof(s->params[0])); i++){ + if(s->params[i] != NULL) { + if (s->params[i]->uistr){ + char tmp[256]; + snprintf(tmp, sizeof(tmp)-1, + " F%s - %s\r\n", + s->params[i]->uistr, + (s->params[i]->description)?s->params[i]->description:"" + ); + len += strlen(tmp); + if (p) strcat(p, tmp); + } + } + } + + if (NULL == p) { + p = malloc(len+1); + *p = 0; + } + + } + return p; +} + + +int line_stm32(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 'G': + immediate_stm32(s, *cmd, ascii_out); + return 1; +} + +int line_hall(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 'H': + immediate_hall(s, *cmd, ascii_out); + return 1; +} + +int line_immediate(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 'I': + speedB = 0; + steerB = 0; + PWMData.pwm[1] = CLAMP(speedB - steerB, -1000, 1000); + PWMData.pwm[0] = CLAMP(speedB + steerB, -1000, 1000); + SpeedData.wanted_speed_mm_per_sec[0] = SpeedData.wanted_speed_mm_per_sec[1] = speedB; + if (strlen(cmd) == 1){ + s->ascii.enable_immediate = 1; + sprintf(ascii_out, "Immediate commands enabled - WASDXHCGQ\r\n>"); + } else { + switch (cmd[1] | 0x20){ + case 's': + s->ascii.enable_immediate = 1; + sprintf(ascii_out, "Immediate commands enabled - WASDXHCGQ - Speed control\r\n>"); + break; + case 'p': + s->ascii.enable_immediate = 1; + sprintf(ascii_out, "Immediate commands enabled - WASDXHCGQ - Position control\r\n>"); + break; + case 'w': + s->ascii.enable_immediate = 1; + sprintf(ascii_out, "Immediate commands enabled - WASDXHCGQ - Power (pWm) control\r\n>"); + break; + } + } + return 1; +} + +int line_sensors(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 'N': + immediate_sensors(s, *cmd, ascii_out); + return 1; +} + +int line_poweroff_control(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 'P': + if (strlen(cmd) == 1){ + + } else { + if ((cmd[1] | 0x20) == 'r'){ + sprintf(ascii_out, "Reset in 500ms\r\n"); + s->send_serial_data_wait((unsigned char *)ascii_out, strlen(ascii_out)); + HAL_Delay(500); + HAL_NVIC_SystemReset(); + } + + if ((cmd[1] | 0x20) == 'e'){ + + } else { + int s = -1; + sscanf(cmd+1, "%d", &s); + if (s >= 0){ + + } + } + } + + return 1; +} + +int line_test_message(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 'T': + if (strlen(cmd) < 2){ + sprintf(ascii_out, "Test command needs A N or T qualifier\r\n"); + } else { + // send a test message in machine protocol + switch (cmd[1]){ + case 'A': + case 'a': + //protocol_send_ack(); + break; + case 'N': + case 'n': + //protocol_send_nack(); + break; + case 'T': + case 't':{ + char tmp[] = { PROTOCOL_SOM_ACK, 0, 5, PROTOCOL_CMD_TEST, 'T', 'e', 's', 't' }; + protocol_post(s, (PROTOCOL_MSG3full*)tmp); + } + break; + } + // CR before prompt.... after message + sprintf(ascii_out, "\r\n"); + } + return 1; +} + + +int line_reset_firmware(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +//case 'R': + if (cmd[1] == '!'){ + sprintf(ascii_out, "\r\n!!!!!Resetting!!!!!\r\n"); + s->send_serial_data_wait((unsigned char *)ascii_out, strlen(ascii_out)); + ascii_out[0] = 0; + HAL_Delay(500); + HAL_NVIC_SystemReset(); + } + return 1; +} + +int line_read_memory(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { +// memory read hex address +//case 'M': + unsigned char tmp[100]; + unsigned char *addr = 0; + unsigned int len = 4; + if (cmd[1] == 'f') { + + } else { + sscanf(&cmd[1], "%lx,%x", (uint32_t *)&addr, &len); + } + strcat( ascii_out, "\r\n" ); + for (int a = 0; a < len; a++) { + char t[5]; + sprintf(t, "%2.2X ", *(addr+a)); + strcat( ascii_out, t ); + if (!((a+1)&0xf)){ + strcat( ascii_out, "\r\n" ); + s->send_serial_data_wait((unsigned char *)ascii_out, strlen(ascii_out)); + ascii_out[0] = 0; + } + } + strcat( ascii_out, "\r\n" ); + return 1; +} + + + +///////////////////////////////////////////// +// single byte commands at start of command +// - i.e. only after CR of LF and ascii buffer empty +int main_ascii_init(PROTOCOL_STAT *s){ + + ascii_add_immediate( 'W', immediate_dir, "Faster" ); + ascii_add_immediate( 'S', immediate_dir, "Slower"); + ascii_add_immediate( 'D', immediate_dir, "Lefter"); + ascii_add_immediate( 'A', immediate_dir, "Righter"); + ascii_add_immediate( 'X', immediate_stop, "DisableDrive"); + ascii_add_immediate( 'Q', immediate_quit, "Quit Immediate"); + ascii_add_immediate( 'H', immediate_hall, "display hall data"); + ascii_add_immediate( 'N', immediate_sensors, "display sensor data"); + ascii_add_immediate( 'C', immediate_electrical, "display electrical measurements"); + ascii_add_immediate( 'G', immediate_stm32, "display stm32 specific"); + ascii_add_immediate( 'O', immediate_toggle_control, "toggle control mode"); + + + ascii_add_line_fn( 'A', line_set_alarm, "set alarm"); + ascii_add_line_fn( 'B', line_toggle_sensor_control, "toggle sensor control"); + ascii_add_line_fn( 'C', line_electrical, "show electrical measurements"); + ascii_add_line_fn( 'S', line_main_timing_stats, "show main loop timing stats"); + ascii_add_line_fn( 'E', line_debug_control, "dEbug control, E->off, Ec->console on, Es->console+scope"); + + ascii_add_line_fn( 'R', line_reset_firmware, " - R! -> Reset Firmware"); + ascii_add_line_fn( 'T', line_test_message, "tt - send a test protocol message "); + ascii_add_line_fn( 'P', line_poweroff_control, " P -power control\r\n" + " P -disablepoweroff\r\n" + " PE enable poweroff\r\n" + " Pn power off in n seconds\r\n" + " Pr software reset\r\n" ); + + ascii_add_line_fn( 'M', line_read_memory, " M - dump memory\r\n" + " Mf - dump flash\r\n" + " M, - dump mem\r\n"); + ascii_add_line_fn( 'N', line_sensors, "display sensor data"); + ascii_add_line_fn( 'I', line_immediate, "enable immediate commands - Ip/Is/Iw - direct to posn/speed/pwm control\r\n"); + ascii_add_line_fn( 'G', line_stm32, "display stm32 specific"); + + ascii_add_line_fn( 'F', line_generic_var, get_F_description(s)); + + return 1; +} +///////////////////////////////////////////// + diff --git a/Src/comms.c b/Src/comms.c index 48924e219..e44621407 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -64,12 +64,12 @@ void consoleScope(void) { strLength = sprintf((char *)(uintptr_t)uart_buf, "1:%i 2:%i 3:%i 4:%i 5:%i 6:%i 7:%i 8:%i\r\n", ch_buf[0], ch_buf[1], ch_buf[2], ch_buf[3], ch_buf[4], ch_buf[5], ch_buf[6], ch_buf[7]); - + #ifdef DEBUG_SERIAL_USART2 if(__HAL_DMA_GET_COUNTER(huart2.hdmatx) == 0) { HAL_UART_Transmit_DMA(&huart2, (uint8_t *)uart_buf, strLength); } - #endif + #endif #ifdef DEBUG_SERIAL_USART3 if(__HAL_DMA_GET_COUNTER(huart3.hdmatx) == 0) { HAL_UART_Transmit_DMA(&huart3, (uint8_t *)uart_buf, strLength); @@ -77,6 +77,13 @@ void consoleScope(void) { #endif #endif + #if defined DEBUG_SERIAL_ASCII && defined DEBUG_SOFTWARE_SERIAL + if (debug_out){ + memset((void *)uart_buf, 0, sizeof(uart_buf)); + sprintf((void *)uart_buf, "1:%i 2:%i 3:%i 4:%i 5:%i 6:%i 7:%i 8:%i\r\n", ch_buf[0], ch_buf[1], ch_buf[2], ch_buf[3], ch_buf[4], ch_buf[5], ch_buf[6], ch_buf[7]); + softwareserial_Send((unsigned char *)uart_buf, strlen((char*)uart_buf)); + } + #endif } diff --git a/Src/main.c b/Src/main.c index 21da061f8..9ad96d268 100644 --- a/Src/main.c +++ b/Src/main.c @@ -30,6 +30,7 @@ #include "BLDC_controller.h" /* BLDC's header file */ #include "rtwtypes.h" #include "protocolfunctions.h" +#include "control_structures.h" #if defined(DEBUG_I2C_LCD) || defined(SUPPORT_LCD) #include "hd44780.h" @@ -219,16 +220,28 @@ int main(void) { calcAvgSpeed(); // Calculate average measured speed: speedAvg, speedAvgAbs #if defined(SERIAL_USART2_IT) && defined(VARIANT_BIPROPELLANT) - while ( serial_usart_buffer_count(&usart2_it_RXbuffer) > 0 ) { - protocol_byte( &sUSART2, (unsigned char) serial_usart_buffer_pop(&usart2_it_RXbuffer) ); - } - protocol_tick( &sUSART2 ); + while ( serial_usart_buffer_count(&usart2_it_RXbuffer) > 0 ) { + protocol_byte( &sUSART2, (unsigned char) serial_usart_buffer_pop(&usart2_it_RXbuffer) ); + } + protocol_tick( &sUSART2 ); + if(sUSART3.ascii.enable_immediate) + { + timeout = 0; + } + cmd2 = (PWMData.pwm[0] + PWMData.pwm[1]) /2; // Speed + cmd1 = PWMData.pwm[0] - cmd2; // Steer #endif #if defined(SERIAL_USART3_IT) && defined(VARIANT_BIPROPELLANT) - while ( serial_usart_buffer_count(&usart3_it_RXbuffer) > 0 ) { - protocol_byte( &sUSART3, (unsigned char) serial_usart_buffer_pop(&usart3_it_RXbuffer) ); - } - protocol_tick( &sUSART3 ); + while ( serial_usart_buffer_count(&usart3_it_RXbuffer) > 0 ) { + protocol_byte( &sUSART3, (unsigned char) serial_usart_buffer_pop(&usart3_it_RXbuffer) ); + } + protocol_tick( &sUSART3 ); + if(sUSART3.ascii.enable_immediate) + { + timeout = 0; + } + cmd2 = (PWMData.pwm[0] + PWMData.pwm[1]) /2; // Speed + cmd1 = PWMData.pwm[0] - cmd2; // Steer #endif #ifndef VARIANT_TRANSPOTTER diff --git a/Src/protocolfunctions.c b/Src/protocolfunctions.c index c4e3d71cc..1161534ee 100644 --- a/Src/protocolfunctions.c +++ b/Src/protocolfunctions.c @@ -20,6 +20,7 @@ #endif extern volatile uint32_t input_timeout_counter; // global variable for input timeout +extern uint32_t timeout; //////////////////////////////////////////////////////////////////////////////////////////// @@ -249,7 +250,7 @@ void fn_PWMData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCO case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: // control_type = CONTROL_TYPE_PWM; -// input_timeout_counter = 0; + timeout= 0; break; } @@ -370,7 +371,7 @@ int setup_protocol(PROTOCOL_STAT *s) { // initialise ascii protocol functions -// main_ascii_init(s); + main_ascii_init(s); #ifdef CONTROL_SENSOR From 58fb0ab00b6189173d387558726f20efa9471e0c Mon Sep 17 00:00:00 2001 From: phail Date: Sun, 21 Jun 2020 17:35:20 +0200 Subject: [PATCH 05/16] Add hbprotocol to makefile --- Makefile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Makefile b/Makefile index 6880e1e5f..6dea00558 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,12 @@ Src/hd44780.c \ Src/pcf8574.c \ Src/comms.c \ Src/stm32f1xx_it.c \ +Src/hbprotocol/protocol.c \ +Src/hbprotocol/machine_protocol.c \ +Src/hbprotocol/ascii_protocol.c \ +Src/hbprotocol/cobsr.c \ +Src/ascii_proto_funcs.c \ +Src/protocolfunctions.c \ Src/BLDC_controller_data.c \ Src/BLDC_controller.c @@ -97,6 +103,7 @@ AS_INCLUDES = # C includes C_INCLUDES = \ -IInc \ +-ISrc/hbprotocol \ -IDrivers/STM32F1xx_HAL_Driver/Inc \ -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \ -IDrivers/CMSIS/Device/ST/STM32F1xx/Include \ From c8e089d82656195557a609d4dc2698e7fe2d5c66 Mon Sep 17 00:00:00 2001 From: phail Date: Fri, 26 Jun 2020 19:18:01 +0200 Subject: [PATCH 06/16] clean up unused stuff set ctrlModReq mode from hbprotocol --- Inc/config.h | 4 +- Inc/control_structures.h | 37 -------- Inc/util.h | 2 + Src/ascii_proto_funcs.c | 32 ------- Src/bldc.c | 21 +++-- Src/comms.c | 10 +++ Src/protocolfunctions.c | 176 ++------------------------------------- 7 files changed, 30 insertions(+), 252 deletions(-) diff --git a/Inc/config.h b/Inc/config.h index e4310273b..f368c00a3 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -32,7 +32,7 @@ #else #define DELAY_IN_MAIN_LOOP 5 // in ms. default 5. it is independent of all the timing critical stuff. do not touch if you do not know what you are doing. #endif -#define TIMEOUT 5 // number of wrong / missing input commands before emergency off +#define TIMEOUT 30 // number of wrong / missing input commands before emergency off #define A2BIT_CONV 50 // A to bit for current conversion on ADC. Example: 1 A = 50, 2 A = 100, etc // ADC conversion time definitions @@ -259,6 +259,8 @@ #define SERIAL_USART3_IT #define USART2_BAUD 115200 #define USART3_BAUD 115200 + #undef TIMEOUT + #define TIMEOUT 30 // number of wrong / missing input commands before emergency off #endif #define SERIAL_USART_IT_BUFFERTYPE unsigned char #define USART2_WORDLENGTH UART_WORDLENGTH_8B diff --git a/Inc/control_structures.h b/Inc/control_structures.h index 53f71dbcf..76912a79a 100644 --- a/Inc/control_structures.h +++ b/Inc/control_structures.h @@ -87,41 +87,4 @@ typedef struct tag_ELECTRICAL_PARAMS{ MOTOR_ELECTRICAL motors[2]; } ELECTRICAL_PARAMS; -#pragma pack(pop) - -#pragma pack(push, 1) -typedef struct tag_sensor_frame{ - unsigned char header_00; // this byte gets 0x100 (9 bit serial) - short Angle; - short Angle_duplicate; - unsigned char AA_55; - unsigned char Accelleration; - unsigned char Accelleration_duplicate; - short Roll; -} SENSOR_FRAME; -#pragma pack(pop) - -#pragma pack(push, 4) // since on 'int32_t' are used, alignment can be optimized for 4 bytes -typedef struct INTEGER_XYT_POSN_tag { - int32_t x; - int32_t y; - int32_t degrees; -} INTEGER_XYT_POSN; -#pragma pack(pop) - - -#pragma pack(push, 4) // all used data types are 4 byte -typedef struct tag_POSN { - int32_t LeftAbsolute; - int32_t RightAbsolute; - int32_t LeftOffset; - int32_t RightOffset; -} POSN; -#pragma pack(pop) - -#pragma pack(push, 4) // all used data types are 4 byte -typedef struct tag_POSN_INCR { - int32_t Left; - int32_t Right; -} POSN_INCR; #pragma pack(pop) \ No newline at end of file diff --git a/Inc/util.h b/Inc/util.h index a09136a1b..74880a1b3 100644 --- a/Inc/util.h +++ b/Inc/util.h @@ -107,5 +107,7 @@ typedef struct { } MultipleTap; void multipleTapDet(int16_t u, uint32_t timeNow, MultipleTap *x); +extern uint8_t ctrlModReq; + #endif diff --git a/Src/ascii_proto_funcs.c b/Src/ascii_proto_funcs.c index c9f90b010..ccfde7875 100644 --- a/Src/ascii_proto_funcs.c +++ b/Src/ascii_proto_funcs.c @@ -45,15 +45,8 @@ /////////////////////////////////////////////// // extern variables you want to read/write here -#ifdef CONTROL_SENSOR -extern SENSOR_DATA sensor_data[2]; -extern int sensor_control; -extern int sensor_stabilise; -#endif // from main.c -extern void change_PID_constants(); - extern uint8_t enable; // global variable for motor enable @@ -69,10 +62,6 @@ static int steerB = 0; /////////////////////////////////////////////// -extern int protocol_post(PROTOCOL_STAT *s, PROTOCOL_MSG3full *msg); - - - int immediate_dir(PROTOCOL_STAT *s, char byte, char *ascii_out) { int dir = 1; switch(byte){ @@ -125,9 +114,6 @@ int immediate_quit(PROTOCOL_STAT *s, char byte, char *ascii_out) { PWMData.pwm[1] = CLAMP(speedB - steerB, -1000, 1000); PWMData.pwm[0] = CLAMP(speedB + steerB, -1000, 1000); SpeedData.wanted_speed_mm_per_sec[0] = SpeedData.wanted_speed_mm_per_sec[1] = speedB; -#ifdef CONTROL_SENSOR - sensor_control = 0; -#endif enable = 0; sprintf(ascii_out, "Immediate commands disabled\r\n"); return 1; @@ -184,22 +170,6 @@ int line_set_alarm(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { return 1; } -int line_toggle_sensor_control(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { -#ifdef CONTROL_SENSOR -//case 'B': - sensor_control ^= 1; - control_type = 0; - speedB = 0; - steerB = 0; - SpeedData.wanted_speed_mm_per_sec[0] = SpeedData.wanted_speed_mm_per_sec[1] = speedB; - PosnData.wanted_posn_mm[0] = HallData[0].HallPosn_mm; - PosnData.wanted_posn_mm[1] = HallData[1].HallPosn_mm; - sprintf(ascii_out, "Sensor control now %d\r\n", sensor_control); -#endif - return 1; -} - - int line_electrical(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { //case 'c': immediate_electrical(s, *cmd, ascii_out); @@ -493,7 +463,6 @@ int line_reset_firmware(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { int line_read_memory(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { // memory read hex address //case 'M': - unsigned char tmp[100]; unsigned char *addr = 0; unsigned int len = 4; if (cmd[1] == 'f') { @@ -537,7 +506,6 @@ int main_ascii_init(PROTOCOL_STAT *s){ ascii_add_line_fn( 'A', line_set_alarm, "set alarm"); - ascii_add_line_fn( 'B', line_toggle_sensor_control, "toggle sensor control"); ascii_add_line_fn( 'C', line_electrical, "show electrical measurements"); ascii_add_line_fn( 'S', line_main_timing_stats, "show main loop timing stats"); ascii_add_line_fn( 'E', line_debug_control, "dEbug control, E->off, Ec->console on, Es->console+scope"); diff --git a/Src/bldc.c b/Src/bldc.c index 639d8bdf4..33af34714 100644 --- a/Src/bldc.c +++ b/Src/bldc.c @@ -47,7 +47,6 @@ extern ExtY rtY_Right; /* External outputs */ static int16_t pwm_margin = 110; /* This margin allows to always have a window in the PWM signal for proper Phase currents measurement */ -extern uint8_t ctrlModReq; static int16_t curDC_max = (I_DC_MAX * A2BIT_CONV); int16_t curL_phaA = 0, curL_phaB = 0, curL_DC = 0; int16_t curR_phaB = 0, curR_phaC = 0, curR_DC = 0; @@ -108,7 +107,7 @@ void DMA1_Channel1_IRQHandler(void) { curL_phaA = (int16_t)(offsetrlA - adc_buffer.rlA); curL_phaB = (int16_t)(offsetrlB - adc_buffer.rlB); curL_DC = (int16_t)(offsetdcl - adc_buffer.dcl); - + // Get Right motor currents curR_phaB = (int16_t)(offsetrrB - adc_buffer.rrB); curR_phaC = (int16_t)(offsetrrC - adc_buffer.rrC); @@ -152,8 +151,8 @@ void DMA1_Channel1_IRQHandler(void) { /* Make sure to stop BOTH motors in case of an error */ enableFin = enable && !rtY_Left.z_errCode && !rtY_Right.z_errCode; - - // ========================= LEFT MOTOR ============================ + + // ========================= LEFT MOTOR ============================ // Get hall sensors values uint8_t hall_ul = !(LEFT_HALL_U_PORT->IDR & LEFT_HALL_U_PIN); uint8_t hall_vl = !(LEFT_HALL_V_PORT->IDR & LEFT_HALL_V_PIN); @@ -161,17 +160,17 @@ void DMA1_Channel1_IRQHandler(void) { /* Set motor inputs here */ rtU_Left.b_motEna = enableFin; - rtU_Left.z_ctrlModReq = ctrlModReq; + rtU_Left.z_ctrlModReq = ctrlModReq; rtU_Left.r_inpTgt = pwml; rtU_Left.b_hallA = hall_ul; rtU_Left.b_hallB = hall_vl; rtU_Left.b_hallC = hall_wl; rtU_Left.i_phaAB = curL_phaA; rtU_Left.i_phaBC = curL_phaB; - rtU_Left.i_DCLink = curL_DC; - + rtU_Left.i_DCLink = curL_DC; + /* Step the controller */ - #ifdef MOTOR_LEFT_ENA + #ifdef MOTOR_LEFT_ENA BLDC_controller_step(rtM_Left); #endif @@ -188,9 +187,9 @@ void DMA1_Channel1_IRQHandler(void) { LEFT_TIM->LEFT_TIM_V = (uint16_t)CLAMP(vl + pwm_res / 2, pwm_margin, pwm_res-pwm_margin); LEFT_TIM->LEFT_TIM_W = (uint16_t)CLAMP(wl + pwm_res / 2, pwm_margin, pwm_res-pwm_margin); // ================================================================= - - // ========================= RIGHT MOTOR =========================== + + // ========================= RIGHT MOTOR =========================== // Get hall sensors values uint8_t hall_ur = !(RIGHT_HALL_U_PORT->IDR & RIGHT_HALL_U_PIN); uint8_t hall_vr = !(RIGHT_HALL_V_PORT->IDR & RIGHT_HALL_V_PIN); @@ -228,7 +227,7 @@ void DMA1_Channel1_IRQHandler(void) { /* Indicate task complete */ OverrunFlag = false; - + // ############################################################################### } diff --git a/Src/comms.c b/Src/comms.c index e44621407..4ab5d503f 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -9,6 +9,16 @@ extern UART_HandleTypeDef huart2; extern UART_HandleTypeDef huart3; + +#ifdef DEBUG_SERIAL_USART3 +#define UART_DMA_CHANNEL DMA1_Channel2 +#endif + +#ifdef DEBUG_SERIAL_USART2 +#define UART_DMA_CHANNEL DMA1_Channel7 +#endif + + static volatile uint8_t uart_buf[100]; static volatile int16_t ch_buf[8]; diff --git a/Src/protocolfunctions.c b/Src/protocolfunctions.c index 1161534ee..d2d5bbda5 100644 --- a/Src/protocolfunctions.c +++ b/Src/protocolfunctions.c @@ -3,7 +3,7 @@ #include "protocolfunctions.h" #include "comms.h" - +#include "util.h" #include "stm32f1xx_hal.h" @@ -15,19 +15,17 @@ #if defined(SERIAL_USART2_IT) PROTOCOL_STAT sUSART2; #endif -#if defined(SERIAL_USART3_IT) && !defined(CONTROL_SENSOR) +#if defined(SERIAL_USART3_IT) PROTOCOL_STAT sUSART3; #endif extern volatile uint32_t input_timeout_counter; // global variable for input timeout extern uint32_t timeout; - //////////////////////////////////////////////////////////////////////////////////////////// // Variable & Functions for 0x09 enable extern uint8_t enable; // global variable for motor enable -extern void init_PID_control(); // from main extern int main_ascii_init(PROTOCOL_STAT *s); // from ascii_proto_funcs.c ////////////////////////////////////////////// @@ -44,17 +42,11 @@ void fn_enable ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: if (!protocol_enable) { - // assume we will enable, - // set wanted posn to current posn, else we may rush into a wall -// PosnData.wanted_posn_mm[0] = HallData[0].HallPosn_mm; -// PosnData.wanted_posn_mm[1] = HallData[1].HallPosn_mm; - // clear speeds to zero SpeedData.wanted_speed_mm_per_sec[0] = 0; SpeedData.wanted_speed_mm_per_sec[1] = 0; PWMData.pwm[0] = 0; PWMData.pwm[1] = 0; -// init_PID_control(); } enable = protocol_enable; break; @@ -62,30 +54,6 @@ void fn_enable ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL fn_defaultProcessing(s, param, cmd, msg); } - -#ifdef CONTROL_SENSOR -//////////////////////////////////////////////////////////////////////////////////////////// -// Variable & Functions for 0x01 sensor_data - -extern SENSOR_DATA sensor_data[2]; - -// used to send only pertinent data, not the whole structure -PROTOCOL_SENSOR_FRAME sensor_copy[2]; - -void fn_SensorData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { - switch (cmd) { - case PROTOCOL_CMD_READVAL: - case PROTOCOL_CMD_SILENTREAD: - // copy just sensor input data - memcpy(&sensor_copy[0], &sensor_data[0].complete, sizeof(sensor_copy[0])); - memcpy(&sensor_copy[1], &sensor_data[1].complete, sizeof(sensor_copy[1])); - break; - } - fn_defaultProcessingReadOnly(s, param, cmd, msg); -} - -#endif - //////////////////////////////////////////////////////////////////////////////////////////// // Variable & Functions for 0x02 HallData @@ -107,117 +75,11 @@ void fn_SpeedData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTO switch (cmd) { case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: -// control_type = CONTROL_TYPE_SPEED; -// input_timeout_counter = 0; break; } fn_defaultProcessing(s, param, cmd, msg); } -//////////////////////////////////////////////////////////////////////////////////////////// -// Variable & Functions for 0x04 Position - -POSN Position; - -void fn_Position ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { - switch (cmd) { - case PROTOCOL_CMD_READVAL: - case PROTOCOL_CMD_SILENTREAD: - /* - ((POSN*) (param->ptr))->LeftAbsolute = HallData[0].HallPosn_mm; - ((POSN*) (param->ptr))->LeftOffset = HallData[0].HallPosn_mm - HallData[0].HallPosn_mm_lastread; - ((POSN*) (param->ptr))->RightAbsolute = HallData[1].HallPosn_mm; - ((POSN*) (param->ptr))->RightOffset = HallData[1].HallPosn_mm - HallData[1].HallPosn_mm_lastread; - */ - break; - } - - fn_defaultProcessing(s, param, cmd, msg); - - switch (cmd) { - case PROTOCOL_CMD_WRITEVAL: - case PROTOCOL_CMD_READVALRESPONSE: - /* - HallData[0].HallPosn_mm_lastread = ((POSN*) (param->ptr))->LeftAbsolute; - HallData[1].HallPosn_mm_lastread = ((POSN*) (param->ptr))->RightAbsolute; - */ - break; - } -} - -//////////////////////////////////////////////////////////////////////////////////////////// -// Variable & Functions for 0x05 PositionIncr - -POSN_INCR PositionIncr; - -void fn_PositionIncr ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { - - fn_defaultProcessing(s, param, cmd, msg); - - switch (cmd) { - case PROTOCOL_CMD_WRITEVAL: - case PROTOCOL_CMD_READVALRESPONSE: -/* // if switching to control type POSITION, - if ((control_type != CONTROL_TYPE_POSITION) || !enable) { - control_type = CONTROL_TYPE_POSITION; - // then make sure we won't rush off somwehere strange - // by setting our wanted posn to where we currently are... - fn_enable( s, param, PROTOCOL_CMD_READVALRESPONSE, msg); // TODO: I don't like calling this with a param entry which does not fit to the handler.. - } - - enable = 1; - input_timeout_counter = 0; - - // increment our wanted position - PosnData.wanted_posn_mm[0] += ((POSN_INCR*) (param->ptr))->Left; - PosnData.wanted_posn_mm[1] += ((POSN_INCR*) (param->ptr))->Right; - */ - break; - } -} - -//////////////////////////////////////////////////////////////////////////////////////////// -// Variable & Functions for 0x06 PosnData - -PROTOCOL_POSN_DATA PosnData = { - {0, 0}, - - 200, // max pwm in posn mode - 70, // min pwm in posn mode -}; - - -//////////////////////////////////////////////////////////////////////////////////////////// -// Variable & Functions for 0x07 RawPosition - -PROTOCOL_POSN RawPosition; - -void fn_RawPosition ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { - switch (cmd) { - case PROTOCOL_CMD_READVAL: - case PROTOCOL_CMD_SILENTREAD: - /* - ((PROTOCOL_POSN*) (param->ptr))->LeftAbsolute = HallData[0].HallPosn; - ((PROTOCOL_POSN*) (param->ptr))->LeftOffset = HallData[0].HallPosn - HallData[0].HallPosn_lastread; - ((PROTOCOL_POSN*) (param->ptr))->RightAbsolute = HallData[1].HallPosn; - ((PROTOCOL_POSN*) (param->ptr))->RightOffset = HallData[1].HallPosn - HallData[1].HallPosn_lastread; - */ - break; - } - - fn_defaultProcessing(s, param, cmd, msg); - - switch (cmd) { - case PROTOCOL_CMD_WRITEVAL: - case PROTOCOL_CMD_READVALRESPONSE: - /* - HallData[0].HallPosn_lastread = ((PROTOCOL_POSN*) (param->ptr))->LeftAbsolute; - HallData[1].HallPosn_lastread = ((PROTOCOL_POSN*) (param->ptr))->RightAbsolute; - */ - break; - } -} - //////////////////////////////////////////////////////////////////////////////////////////// // Variable & Functions for 0x0A disablepoweroff @@ -249,7 +111,7 @@ void fn_PWMData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCO switch (cmd) { case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: -// control_type = CONTROL_TYPE_PWM; + ctrlModReq = 3; // 1 = VOLTAGE mode (default), 2 = SPEED mode, 3 = TORQUE mode. timeout= 0; break; } @@ -288,7 +150,6 @@ PROTOCOL_BUZZER_DATA BuzzerData = { extern uint8_t buzzerFreq; // global variable for the buzzer pitch. can be 1, 2, 3, 4, 5, 6, 7... extern uint8_t buzzerPattern; // global variable for the buzzer pattern. can be 1, 2, 3, 4, 5, 6, 7... -extern uint16_t buzzerLen; void fn_BuzzerData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { switch (cmd) { @@ -316,14 +177,6 @@ void fn_BuzzerData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROT } } - -//////////////////////////////////////////////////////////////////////////////////////////// -// Variable & Functions for 0x80 to 0xA0 FlashContent - -// from main.c -extern void change_PID_constants(); -extern void init_PID_control(); - //extern volatile ELECTRICAL_PARAMS electrical_measurements; @@ -342,7 +195,6 @@ int setup_protocol(PROTOCOL_STAT *s) { int errors = 0; #if defined(SERIAL_USART2_IT) - // initialise, even if CONTROL_SENSOR, as we may switch vuia software to use this.... extern int USART2_IT_send(unsigned char *data, int len); errors += protocol_init(&sUSART2); @@ -355,7 +207,7 @@ int setup_protocol(PROTOCOL_STAT *s) { #endif - #if defined(SERIAL_USART3_IT) && !defined(CONTROL_SENSOR) + #if defined(SERIAL_USART3_IT) extern int USART3_IT_send(unsigned char *data, int len); @@ -374,26 +226,12 @@ int setup_protocol(PROTOCOL_STAT *s) { main_ascii_init(s); - #ifdef CONTROL_SENSOR - errors += setParamVariable( s, 0x01, UI_NONE, &sensor_copy, sizeof(sensor_copy) ); - setParamHandler( s, 0x01, fn_SensorData ); - #endif // errors += setParamVariable( s, 0x02, UI_NONE, (void *)&HallData, sizeof(HallData) ); errors += setParamVariable( s, 0x03, UI_NONE, &SpeedData, sizeof(SpeedData) ); setParamHandler( s, 0x03, fn_SpeedData ); - errors += setParamVariable( s, 0x04, UI_NONE, &Position, sizeof(Position) ); - setParamHandler( s, 0x04, fn_Position ); - - - - errors += setParamVariable( s, 0x06, UI_NONE, &PosnData, sizeof(PosnData) ); - - errors += setParamVariable( s, 0x07, UI_NONE, &RawPosition, sizeof(RawPosition) ); - setParamHandler( s, 0x07, fn_RawPosition ); - // errors += setParamVariable( s, 0x08, UI_NONE, (void *)&electrical_measurements, sizeof(ELECTRICAL_PARAMS) ); errors += setParamVariable( s, 0x09, UI_CHAR, &protocol_enable, sizeof(enable) ); @@ -419,15 +257,11 @@ int setup_protocol(PROTOCOL_STAT *s) { void consoleLog(char *message) { - #ifdef DEBUG_SOFTWARE_SERIAL - if (debug_out) protocol_send_text(&sSoftwareSerial, message, PROTOCOL_SOM_NOACK); - #endif - #ifdef SERIAL_USART2_IT if (debug_out) protocol_send_text(&sUSART2, message, PROTOCOL_SOM_NOACK); #endif - #if defined(SERIAL_USART3_IT) && !defined(CONTROL_SENSOR) + #if defined(SERIAL_USART3_IT) if (debug_out) protocol_send_text(&sUSART3, message, PROTOCOL_SOM_NOACK); #endif } From 7b2e030a3e10fc948613e3a088f803545471fa8d Mon Sep 17 00:00:00 2001 From: phail Date: Sun, 28 Jun 2020 17:04:31 +0200 Subject: [PATCH 07/16] bipropellant protocol can be used with DMA based USART --- Inc/config.h | 14 ++- Inc/util.h | 12 +-- Src/comms.c | 32 ++++--- Src/main.c | 46 +++++---- Src/protocolfunctions.c | 37 +++++++- Src/setup.c | 47 ++++----- Src/stm32f1xx_it.c | 12 +-- Src/util.c | 205 +++++++++++++++++++++------------------- 8 files changed, 231 insertions(+), 174 deletions(-) diff --git a/Inc/config.h b/Inc/config.h index f368c00a3..c4e6c5686 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -256,11 +256,16 @@ // ############################ VARIANT_BIPROPELLANT SETTINGS ############################ #ifdef VARIANT_BIPROPELLANT // #define SERIAL_USART2_IT - #define SERIAL_USART3_IT +// #define SERIAL_USART3_IT +// #define SERIAL_USART2_DMA + #define SERIAL_USART3_DMA #define USART2_BAUD 115200 #define USART3_BAUD 115200 #undef TIMEOUT #define TIMEOUT 30 // number of wrong / missing input commands before emergency off + #undef BEEPS_BACKWARD + #define BEEPS_BACKWARD 0 // 0 or 1 + #define SERIAL_BUFFER_SIZE 64 // [bytes] Size of Serial Rx buffer. Make sure it is always larger than the structure size #endif #define SERIAL_USART_IT_BUFFERTYPE unsigned char #define USART2_WORDLENGTH UART_WORDLENGTH_8B @@ -443,6 +448,13 @@ #define USART3_BAUD 38400 // UART3 baud rate (short wired cable) #define USART3_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B #endif +#if defined(DEBUG_SERIAL_USART2) + #define SERIAL_USART2_DMA +#endif +#if defined(DEBUG_SERIAL_USART3) + #define SERIAL_USART3_DMA +#endif + // ########################### UART SETIINGS ############################ diff --git a/Inc/util.h b/Inc/util.h index 74880a1b3..882d57d7b 100644 --- a/Inc/util.h +++ b/Inc/util.h @@ -26,20 +26,20 @@ // Rx Structures USART #if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3) - #ifdef CONTROL_IBUS + #ifdef CONTROL_IBUS typedef struct{ uint8_t start; - uint8_t type; + uint8_t type; uint8_t channels[IBUS_NUM_CHANNELS*2]; uint8_t checksuml; - uint8_t checksumh; + uint8_t checksumh; } SerialCommand; #else typedef struct{ - uint16_t start; + uint16_t start; int16_t steer; int16_t speed; - uint16_t checksum; + uint16_t checksum; } SerialCommand; #endif #endif @@ -79,7 +79,7 @@ void poweroffPressCheck(void); void readCommand(void); void usart2_rx_check(void); void usart3_rx_check(void); -#if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3) +#if defined(SERIAL_USART2_DMA) || defined(SERIAL_USART3_DMA) void usart_process_debug(uint8_t *userCommand, uint32_t len); #endif #if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3) diff --git a/Src/comms.c b/Src/comms.c index 4ab5d503f..dcd816742 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -9,16 +9,6 @@ extern UART_HandleTypeDef huart2; extern UART_HandleTypeDef huart3; - -#ifdef DEBUG_SERIAL_USART3 -#define UART_DMA_CHANNEL DMA1_Channel2 -#endif - -#ifdef DEBUG_SERIAL_USART2 -#define UART_DMA_CHANNEL DMA1_Channel7 -#endif - - static volatile uint8_t uart_buf[100]; static volatile int16_t ch_buf[8]; @@ -58,12 +48,12 @@ void consoleScope(void) { #ifdef DEBUG_SERIAL_USART2 if(__HAL_DMA_GET_COUNTER(huart2.hdmatx) == 0) { - HAL_UART_Transmit_DMA(&huart2, (uint8_t *)uart_buf, strLength); + HAL_UART_Transmit_DMA(&huart2, (uint8_t *)uart_buf, strLength); } #endif #ifdef DEBUG_SERIAL_USART3 if(__HAL_DMA_GET_COUNTER(huart3.hdmatx) == 0) { - HAL_UART_Transmit_DMA(&huart3, (uint8_t *)uart_buf, strLength); + HAL_UART_Transmit_DMA(&huart3, (uint8_t *)uart_buf, strLength); } #endif #endif @@ -77,12 +67,12 @@ void consoleScope(void) { #ifdef DEBUG_SERIAL_USART2 if(__HAL_DMA_GET_COUNTER(huart2.hdmatx) == 0) { - HAL_UART_Transmit_DMA(&huart2, (uint8_t *)uart_buf, strLength); + HAL_UART_Transmit_DMA(&huart2, (uint8_t *)uart_buf, strLength); } #endif #ifdef DEBUG_SERIAL_USART3 if(__HAL_DMA_GET_COUNTER(huart3.hdmatx) == 0) { - HAL_UART_Transmit_DMA(&huart3, (uint8_t *)uart_buf, strLength); + HAL_UART_Transmit_DMA(&huart3, (uint8_t *)uart_buf, strLength); } #endif #endif @@ -146,6 +136,20 @@ void USART2_IT_IRQ(USART_TypeDef *us) { #endif +#if defined(SERIAL_USART2_DMA) && VARIANT_BIPROPELLANT +int USART2_DMA_send(unsigned char *data, int len) { + HAL_UART_Transmit_DMA(&huart2, (uint8_t *)data, len); + return 1; +} +#endif + +#if defined(SERIAL_USART3_DMA) && VARIANT_BIPROPELLANT +int USART3_DMA_send(unsigned char *data, int len) { + HAL_UART_Transmit_DMA(&huart3, (uint8_t *)data, len); + return 1; +} +#endif + #ifdef SERIAL_USART3_IT int USART3_IT_starttx() { diff --git a/Src/main.c b/Src/main.c index 9ad96d268..8db952797 100644 --- a/Src/main.c +++ b/Src/main.c @@ -128,12 +128,12 @@ static uint8_t sideboard_leds_R; #ifdef VARIANT_TRANSPOTTER extern uint8_t nunchuk_connected; - extern float setDistance; + extern float setDistance; static uint8_t checkRemote = 0; static uint16_t distance; static float steering; - static int distanceErr; + static int distanceErr; static int lastDistance = 0; static uint16_t transpotter_counter = 0; #endif @@ -186,7 +186,7 @@ int main(void) { HAL_GPIO_WritePin(OFF_PORT, OFF_PIN, GPIO_PIN_SET); HAL_ADC_Start(&hadc1); - HAL_ADC_Start(&hadc2); + HAL_ADC_Start(&hadc2); poweronMelody(); HAL_GPIO_WritePin(LED_PORT, LED_PIN, GPIO_PIN_SET); @@ -205,12 +205,6 @@ int main(void) { USART3_IT_init(); #endif - #if defined(SERIAL_USART2_IT) && defined(VARIANT_BIPROPELLANT) - setup_protocol(&sUSART2); - #endif - #if defined(SERIAL_USART3_IT) && defined(VARIANT_BIPROPELLANT) - setup_protocol(&sUSART3); - #endif while(1) { @@ -219,10 +213,12 @@ int main(void) { readCommand(); // Read Command: cmd1, cmd2 calcAvgSpeed(); // Calculate average measured speed: speedAvg, speedAvgAbs - #if defined(SERIAL_USART2_IT) && defined(VARIANT_BIPROPELLANT) + #if defined(VARIANT_BIPROPELLANT) && (defined(SERIAL_USART2_IT) || defined(SERIAL_USART2_DMA)) + #if defined(SERIAL_USART2_IT) while ( serial_usart_buffer_count(&usart2_it_RXbuffer) > 0 ) { protocol_byte( &sUSART2, (unsigned char) serial_usart_buffer_pop(&usart2_it_RXbuffer) ); } + #endif protocol_tick( &sUSART2 ); if(sUSART3.ascii.enable_immediate) { @@ -231,10 +227,12 @@ int main(void) { cmd2 = (PWMData.pwm[0] + PWMData.pwm[1]) /2; // Speed cmd1 = PWMData.pwm[0] - cmd2; // Steer #endif - #if defined(SERIAL_USART3_IT) && defined(VARIANT_BIPROPELLANT) + #if defined(VARIANT_BIPROPELLANT) && (defined(SERIAL_USART3_IT) || defined(SERIAL_USART3_DMA)) + #if defined(SERIAL_USART3_IT) while ( serial_usart_buffer_count(&usart3_it_RXbuffer) > 0 ) { protocol_byte( &sUSART3, (unsigned char) serial_usart_buffer_pop(&usart3_it_RXbuffer) ); } + #endif protocol_tick( &sUSART3 ); if(sUSART3.ascii.enable_immediate) { @@ -253,10 +251,10 @@ int main(void) { consoleLog("-- Motors enabled --\r\n"); } - // ####### VARIANT_HOVERCAR ####### + // ####### VARIANT_HOVERCAR ####### #ifdef VARIANT_HOVERCAR // Calculate speed Blend, a number between [0, 1] in fixdt(0,16,15) - uint16_t speedBlend; + uint16_t speedBlend; speedBlend = (uint16_t)(((CLAMP(speedAvgAbs,10,60) - 10) << 15) / 50); // speedBlend [0,1] is within [10 rpm, 60rpm] // Check if Hovercar is physically close to standstill to enable Double tap detection on Brake pedal for Reverse functionality @@ -264,16 +262,16 @@ int main(void) { multipleTapDet(cmd1, HAL_GetTick(), &MultipleTapBreak); // Break pedal in this case is "cmd1" variable } - // If Brake pedal (cmd1) is pressed, bring to 0 also the Throttle pedal (cmd2) to avoid "Double pedal" driving + // If Brake pedal (cmd1) is pressed, bring to 0 also the Throttle pedal (cmd2) to avoid "Double pedal" driving if (cmd1 > 20) { cmd2 = (int16_t)((cmd2 * speedBlend) >> 15); } - // Make sure the Brake pedal is opposite to the direction of motion AND it goes to 0 as we reach standstill (to avoid Reverse driving by Brake pedal) + // Make sure the Brake pedal is opposite to the direction of motion AND it goes to 0 as we reach standstill (to avoid Reverse driving by Brake pedal) if (speedAvg > 0) { cmd1 = (int16_t)((-cmd1 * speedBlend) >> 15); } else { - cmd1 = (int16_t)(( cmd1 * speedBlend) >> 15); + cmd1 = (int16_t)(( cmd1 * speedBlend) >> 15); } #endif @@ -283,12 +281,12 @@ int main(void) { filtLowPass32(steerRateFixdt >> 4, FILTER, &steerFixdt); filtLowPass32(speedRateFixdt >> 4, FILTER, &speedFixdt); steer = (int16_t)(steerFixdt >> 16); // convert fixed-point to integer - speed = (int16_t)(speedFixdt >> 16); // convert fixed-point to integer + speed = (int16_t)(speedFixdt >> 16); // convert fixed-point to integer // ####### VARIANT_HOVERCAR ####### - #ifdef VARIANT_HOVERCAR + #ifdef VARIANT_HOVERCAR if (!MultipleTapBreak.b_multipleTap) { // Check driving direction - speed = steer + speed; // Forward driving + speed = steer + speed; // Forward driving } else { speed = steer - speed; // Reverse driving } @@ -391,7 +389,7 @@ int main(void) { nunchuk_connected = 1; } } - } + } #endif #ifdef SUPPORT_LCD @@ -465,7 +463,7 @@ int main(void) { #if defined(FEEDBACK_SERIAL_USART2) if(__HAL_DMA_GET_COUNTER(huart2.hdmatx) == 0) { Feedback.cmdLed = (uint16_t)sideboard_leds_L; - Feedback.checksum = (uint16_t)(Feedback.start ^ Feedback.cmd1 ^ Feedback.cmd2 ^ Feedback.speedR_meas ^ Feedback.speedL_meas + Feedback.checksum = (uint16_t)(Feedback.start ^ Feedback.cmd1 ^ Feedback.cmd2 ^ Feedback.speedR_meas ^ Feedback.speedL_meas ^ Feedback.batVoltage ^ Feedback.boardTemp ^ Feedback.cmdLed); HAL_UART_Transmit_DMA(&huart2, (uint8_t *)&Feedback, sizeof(Feedback)); @@ -474,12 +472,12 @@ int main(void) { #if defined(FEEDBACK_SERIAL_USART3) if(__HAL_DMA_GET_COUNTER(huart3.hdmatx) == 0) { Feedback.cmdLed = (uint16_t)sideboard_leds_R; - Feedback.checksum = (uint16_t)(Feedback.start ^ Feedback.cmd1 ^ Feedback.cmd2 ^ Feedback.speedR_meas ^ Feedback.speedL_meas + Feedback.checksum = (uint16_t)(Feedback.start ^ Feedback.cmd1 ^ Feedback.cmd2 ^ Feedback.speedR_meas ^ Feedback.speedL_meas ^ Feedback.batVoltage ^ Feedback.boardTemp ^ Feedback.cmdLed); HAL_UART_Transmit_DMA(&huart3, (uint8_t *)&Feedback, sizeof(Feedback)); } - #endif + #endif } #endif @@ -493,7 +491,7 @@ int main(void) { enable = 0; buzzerFreq = 8; buzzerPattern = 1; - } else if (timeoutFlagADC || timeoutFlagSerial) { // beep in case of ADC or Serial timeout - fast beep + } else if (timeoutFlagADC || timeoutFlagSerial) { // beep in case of ADC or Serial timeout - fast beep buzzerFreq = 24; buzzerPattern = 1; } else if (TEMP_WARNING_ENABLE && board_temp_deg_c >= TEMP_WARNING) { // beep if mainboard gets hot diff --git a/Src/protocolfunctions.c b/Src/protocolfunctions.c index d2d5bbda5..feb8f3604 100644 --- a/Src/protocolfunctions.c +++ b/Src/protocolfunctions.c @@ -12,10 +12,10 @@ #include "control_structures.h" -#if defined(SERIAL_USART2_IT) +#if defined(SERIAL_USART2_IT) || defined(SERIAL_USART2_DMA) PROTOCOL_STAT sUSART2; #endif -#if defined(SERIAL_USART3_IT) +#if defined(SERIAL_USART3_IT) || defined(SERIAL_USART3_DMA) PROTOCOL_STAT sUSART3; #endif @@ -111,7 +111,7 @@ void fn_PWMData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCO switch (cmd) { case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: - ctrlModReq = 3; // 1 = VOLTAGE mode (default), 2 = SPEED mode, 3 = TORQUE mode. + ctrlModReq = 1; // 1 = VOLTAGE mode (default), 2 = SPEED mode, 3 = TORQUE mode. timeout= 0; break; } @@ -221,6 +221,33 @@ int setup_protocol(PROTOCOL_STAT *s) { #endif + #if defined(SERIAL_USART2_DMA) + + extern int USART2_DMA_send(unsigned char *data, int len); + + errors += protocol_init(&sUSART2); + + sUSART2.send_serial_data=USART2_DMA_send; + sUSART2.send_serial_data_wait=USART2_DMA_send; + sUSART2.timeout1 = 500; + sUSART2.timeout2 = 100; + sUSART2.allow_ascii = 1; + + #endif + + #if defined(SERIAL_USART3_DMA) + + extern int USART3_DMA_send(unsigned char *data, int len); + + errors += protocol_init(&sUSART3); + + sUSART3.send_serial_data=USART3_DMA_send; + sUSART3.send_serial_data_wait=USART3_DMA_send; + sUSART3.timeout1 = 500; + sUSART3.timeout2 = 100; + sUSART3.allow_ascii = 1; + + #endif // initialise ascii protocol functions main_ascii_init(s); @@ -257,11 +284,11 @@ int setup_protocol(PROTOCOL_STAT *s) { void consoleLog(char *message) { - #ifdef SERIAL_USART2_IT + #if defined(SERIAL_USART2_IT) || defined(SERIAL_USART2_DMA) if (debug_out) protocol_send_text(&sUSART2, message, PROTOCOL_SOM_NOACK); #endif - #if defined(SERIAL_USART3_IT) + #if defined(SERIAL_USART3_IT) || defined(SERIAL_USART3_DMA) if (debug_out) protocol_send_text(&sUSART3, message, PROTOCOL_SOM_NOACK); #endif } diff --git a/Src/setup.c b/Src/setup.c index 41aefccff..d9c51cbc3 100644 --- a/Src/setup.c +++ b/Src/setup.c @@ -61,20 +61,20 @@ volatile adc_buf_t adc_buffer; static int USART2WordLength = USART2_WORDLENGTH; #endif -#if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) +#if defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) /* USART2 init function */ void UART2_Init(void) { /* DMA controller clock enable */ __HAL_RCC_DMA1_CLK_ENABLE(); - + /* DMA1_Channel6_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn); /* DMA1_Channel7_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn); - + huart2.Instance = USART2; huart2.Init.BaudRate = USART2_BAUD; huart2.Init.WordLength = USART2_WORDLENGTH; @@ -87,7 +87,7 @@ volatile adc_buf_t adc_buffer; } #endif -#if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) /* USART3 init function */ void UART3_Init(void) { @@ -101,7 +101,7 @@ void UART3_Init(void) /* DMA1_Channel3_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn); - + huart3.Instance = USART3; huart3.Init.BaudRate = USART3_BAUD; huart3.Init.WordLength = USART3_WORDLENGTH; @@ -114,8 +114,9 @@ void UART3_Init(void) } #endif -#if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || \ - defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || \ + defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || \ + defined(SERIAL_USART2_DMA) || defined(SERIAL_USART3_DMA) void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; @@ -126,11 +127,11 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) /* USER CODE END USART2_MspInit 0 */ /* USART2 clock enable */ __HAL_RCC_USART2_CLK_ENABLE(); - + __HAL_RCC_GPIOA_CLK_ENABLE(); - /**USART2 GPIO Configuration + /**USART2 GPIO Configuration PA2 ------> USART2_TX - PA3 ------> USART2_RX + PA3 ------> USART2_RX */ GPIO_InitStruct.Pin = GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -181,11 +182,11 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) /* USER CODE END USART3_MspInit 0 */ /* USART3 clock enable */ __HAL_RCC_USART3_CLK_ENABLE(); - + __HAL_RCC_GPIOB_CLK_ENABLE(); - /**USART3 GPIO Configuration + /**USART3 GPIO Configuration PB10 ------> USART3_TX - PB11 ------> USART3_RX + PB11 ------> USART3_RX */ GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; @@ -241,10 +242,10 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) /* USER CODE END USART2_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_USART2_CLK_DISABLE(); - - /**USART2 GPIO Configuration + + /**USART2 GPIO Configuration PA2 ------> USART2_TX - PA3 ------> USART2_RX + PA3 ------> USART2_RX */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3); @@ -265,10 +266,10 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) /* USER CODE END USART3_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_USART3_CLK_DISABLE(); - - /**USART3 GPIO Configuration + + /**USART3 GPIO Configuration PB10 ------> USART3_TX - PB11 ------> USART3_RX + PB11 ------> USART3_RX */ HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11); @@ -282,7 +283,7 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) /* USER CODE END USART3_MspDeInit 1 */ } -} +} #endif #ifdef SERIAL_USART2_IT @@ -490,7 +491,7 @@ void MX_GPIO_Init(void) { GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Pin = CHARGER_PIN; HAL_GPIO_Init(CHARGER_PORT, &GPIO_InitStruct); - + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Pin = BUTTON_PIN; @@ -668,7 +669,7 @@ void MX_TIM_Init(void) { HAL_TIM_PWM_Start(&htim_left, TIM_CHANNEL_3); HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_1); HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_2); - HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_3); + HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&htim_right, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim_right, TIM_CHANNEL_2); @@ -767,7 +768,7 @@ void MX_ADC2_Init(void) { hadc2.Init.NbrOfConversion = 5; HAL_ADC_Init(&hadc2); - + sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5; sConfig.Channel = ADC_CHANNEL_10; // pc0 right cur -> left sConfig.Rank = 1; diff --git a/Src/stm32f1xx_it.c b/Src/stm32f1xx_it.c index 886f77a5c..288910ca5 100644 --- a/Src/stm32f1xx_it.c +++ b/Src/stm32f1xx_it.c @@ -251,7 +251,7 @@ void EXTI15_10_IRQHandler(void) #ifdef CONTROL_PWM_LEFT void EXTI2_IRQHandler(void) -{ +{ __HAL_GPIO_EXTI_CLEAR_IT(PWM_PIN_CH1); PWM_ISR_CH1_Callback(); } @@ -276,7 +276,7 @@ void EXTI15_10_IRQHandler(void) } #endif -#if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) +#if defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) void DMA1_Channel6_IRQHandler(void) { /* USER CODE BEGIN DMA1_Channel4_IRQn 0 */ @@ -303,7 +303,7 @@ void DMA1_Channel7_IRQHandler(void) } #endif -#if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) /** * @brief This function handles DMA1 channel2 global interrupt. */ @@ -333,7 +333,7 @@ void DMA1_Channel3_IRQHandler(void) } #endif -#if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) +#if defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) /** * @brief This function handles USART2 global interrupt. */ @@ -352,7 +352,7 @@ void USART2_IRQHandler(void) } #endif -#if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) /** * @brief This function handles USART3 global interrupt. */ @@ -363,7 +363,7 @@ void USART3_IRQHandler(void) /* USER CODE END USART2_IRQn 0 */ HAL_UART_IRQHandler(&huart3); /* USER CODE BEGIN USART2_IRQn 1 */ - if(RESET != __HAL_UART_GET_IT_SOURCE(&huart3, UART_IT_IDLE)) { // Check for IDLE line interrupt + if(RESET != __HAL_UART_GET_IT_SOURCE(&huart3, UART_IT_IDLE)) { // Check for IDLE line interrupt __HAL_UART_CLEAR_IDLEFLAG(&huart3); // Clear IDLE line flag (otherwise it will continue to enter interrupt) usart3_rx_check(); // Check for data to process } diff --git a/Src/util.c b/Src/util.c index f9bd78bf3..edff85c65 100644 --- a/Src/util.c +++ b/Src/util.c @@ -29,6 +29,7 @@ #include "util.h" #include "BLDC_controller.h" #include "rtwtypes.h" +#include "protocolfunctions.h" #if defined(DEBUG_I2C_LCD) || defined(SUPPORT_LCD) #include "hd44780.h" @@ -100,7 +101,7 @@ uint8_t timeoutFlagADC = 0; // Timeout Flag for ADC Protection: 0 uint8_t timeoutFlagSerial = 0; // Timeout Flag for Rx Serial command: 0 = OK, 1 = Problem detected (line disconnected or wrong Rx data) uint8_t ctrlModReqRaw = CTRL_MOD_REQ; -uint8_t ctrlModReq = CTRL_MOD_REQ; // Final control mode request +uint8_t ctrlModReq = CTRL_MOD_REQ; // Final control mode request #if defined(DEBUG_I2C_LCD) || defined(SUPPORT_LCD) LCD_PCF8574_HandleTypeDef lcd; @@ -153,7 +154,7 @@ static int16_t INPUT_MIN; // [-] Input target minimum limitation static int16_t timeoutCntADC = 0; // Timeout counter for ADC Protection #endif -#if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) +#if defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) static uint8_t rx_buffer_L[SERIAL_BUFFER_SIZE]; // USART Rx DMA circular buffer static uint32_t rx_buffer_L_len = ARRAY_LEN(rx_buffer_L); #endif @@ -167,7 +168,7 @@ SerialSideboard Sideboard_L_raw; static uint32_t Sideboard_L_len = sizeof(Sideboard_L); #endif -#if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) static uint8_t rx_buffer_R[SERIAL_BUFFER_SIZE]; // USART Rx DMA circular buffer static uint32_t rx_buffer_R_len = ARRAY_LEN(rx_buffer_R); #endif @@ -210,13 +211,13 @@ static uint8_t brakePressed; /* =========================== Initialization Functions =========================== */ void BLDC_Init(void) { - /* Set BLDC controller parameters */ + /* Set BLDC controller parameters */ rtP_Left.b_selPhaABCurrMeas = 1; // Left motor measured current phases {Green, Blue} = {iA, iB} -> do NOT change rtP_Left.z_ctrlTypSel = CTRL_TYP_SEL; - rtP_Left.b_diagEna = DIAG_ENA; + rtP_Left.b_diagEna = DIAG_ENA; rtP_Left.i_max = (I_MOT_MAX * A2BIT_CONV) << 4; // fixdt(1,16,4) rtP_Left.n_max = N_MOT_MAX << 4; // fixdt(1,16,4) - rtP_Left.b_fieldWeakEna = FIELD_WEAK_ENA; + rtP_Left.b_fieldWeakEna = FIELD_WEAK_ENA; rtP_Left.id_fieldWeakMax = (FIELD_WEAK_MAX * A2BIT_CONV) << 4; // fixdt(1,16,4) rtP_Left.a_phaAdvMax = PHASE_ADV_MAX << 4; // fixdt(1,16,4) rtP_Left.r_fieldWeakHi = FIELD_WEAK_HI << 4; // fixdt(1,16,4) @@ -242,7 +243,7 @@ void BLDC_Init(void) { BLDC_controller_initialize(rtM_Right); } -void Input_Lim_Init(void) { // Input Limitations - ! Do NOT touch ! +void Input_Lim_Init(void) { // Input Limitations - ! Do NOT touch ! if (rtP_Left.b_fieldWeakEna || rtP_Right.b_fieldWeakEna) { INPUT_MAX = MAX( 1000, FIELD_WEAK_HI); INPUT_MIN = MIN(-1000,-FIELD_WEAK_HI); @@ -266,26 +267,33 @@ void Input_Init(void) { Nunchuk_Init(); #endif - #if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) + #if (defined(SERIAL_USART2_IT) || defined(SERIAL_USART2_DMA)) && defined(VARIANT_BIPROPELLANT) + setup_protocol(&sUSART2); + #endif + #if (defined(SERIAL_USART3_IT) || defined(SERIAL_USART3_DMA)) && defined(VARIANT_BIPROPELLANT) + setup_protocol(&sUSART3); + #endif + + #if defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) UART2_Init(); #endif - #if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) + #if defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) UART3_Init(); #endif - #if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) + #if defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) HAL_UART_Receive_DMA(&huart2, (uint8_t *)rx_buffer_L, sizeof(rx_buffer_L)); UART_DisableRxErrors(&huart2); #endif - #if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) + #if defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) HAL_UART_Receive_DMA(&huart3, (uint8_t *)rx_buffer_R, sizeof(rx_buffer_R)); UART_DisableRxErrors(&huart3); #endif - #ifdef CONTROL_ADC + #ifdef CONTROL_ADC uint16_t writeCheck, i_max, n_max; - HAL_FLASH_Unlock(); - EE_Init(); /* EEPROM Init */ + HAL_FLASH_Unlock(); + EE_Init(); /* EEPROM Init */ EE_ReadVariable(VirtAddVarTab[0], &writeCheck); if (writeCheck == FLASH_WRITE_KEY) { EE_ReadVariable(VirtAddVarTab[1], &ADC1_MIN_CAL); @@ -305,10 +313,10 @@ void Input_Init(void) { #endif - #ifdef VARIANT_TRANSPOTTER + #ifdef VARIANT_TRANSPOTTER enable = 1; - - HAL_FLASH_Unlock(); + + HAL_FLASH_Unlock(); EE_Init(); /* EEPROM Init */ EE_ReadVariable(VirtAddVarTab[0], &saveValue); HAL_FLASH_Lock(); @@ -362,8 +370,9 @@ void Input_Init(void) { * @param huart: UART handle. * @retval None */ -#if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || \ - defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || \ + defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || \ + defined(SERIAL_USART2_DMA) || defined(SERIAL_USART3_DMA) void UART_DisableRxErrors(UART_HandleTypeDef *huart) { /* Disable PE (Parity Error) interrupts */ @@ -418,7 +427,7 @@ void calcAvgSpeed(void) { // Handle the case when SPEED_COEFFICIENT sign is negative (which is when most significant bit is 1) if (SPEED_COEFFICIENT & (1 << 16)) { speedAvg = -speedAvg; - } + } speedAvgAbs = abs(speedAvg); } @@ -437,7 +446,7 @@ void adcCalibLim(void) { } #ifdef CONTROL_ADC consoleLog("ADC calibration started... "); - + // Inititalization: MIN = a high values, MAX = a low value, int32_t adc1_fixdt = adc_buffer.l_tx2 << 16; int32_t adc2_fixdt = adc_buffer.l_rx2 << 16; @@ -448,7 +457,7 @@ void adcCalibLim(void) { uint16_t ADC2_MIN_temp = 4095; uint16_t ADC2_MID_temp = 0; uint16_t ADC2_MAX_temp = 0; - + adc_cal_valid = 1; // Extract MIN, MAX and MID from ADC while the power button is not pressed @@ -458,16 +467,16 @@ void adcCalibLim(void) { ADC1_MID_temp = (uint16_t)CLAMP(adc1_fixdt >> 16, 0, 4095); // convert fixed-point to integer ADC2_MID_temp = (uint16_t)CLAMP(adc2_fixdt >> 16, 0, 4095); ADC1_MIN_temp = MIN(ADC1_MIN_temp, ADC1_MID_temp); - ADC1_MAX_temp = MAX(ADC1_MAX_temp, ADC1_MID_temp); + ADC1_MAX_temp = MAX(ADC1_MAX_temp, ADC1_MID_temp); ADC2_MIN_temp = MIN(ADC2_MIN_temp, ADC2_MID_temp); - ADC2_MAX_temp = MAX(ADC2_MAX_temp, ADC2_MID_temp); + ADC2_MAX_temp = MAX(ADC2_MAX_temp, ADC2_MID_temp); adc_cal_timeout++; HAL_Delay(5); } - // ADC calibration checks + // ADC calibration checks #ifdef ADC_PROTECT_ENA - if ((ADC1_MIN_temp + 150 - ADC_PROTECT_THRESH) > 0 && (ADC1_MAX_temp - 150 + ADC_PROTECT_THRESH) < 4095 && + if ((ADC1_MIN_temp + 150 - ADC_PROTECT_THRESH) > 0 && (ADC1_MAX_temp - 150 + ADC_PROTECT_THRESH) < 4095 && (ADC2_MIN_temp + 150 - ADC_PROTECT_THRESH) > 0 && (ADC2_MAX_temp - 150 + ADC_PROTECT_THRESH) < 4095) { adc_cal_valid = 1; } else { @@ -480,10 +489,10 @@ void adcCalibLim(void) { if (adc_cal_valid && (ADC1_MAX_temp - ADC1_MIN_temp) > 500 && (ADC2_MAX_temp - ADC2_MIN_temp) > 500) { ADC1_MIN_CAL = ADC1_MIN_temp + 150; ADC1_MID_CAL = ADC1_MID_temp; - ADC1_MAX_CAL = ADC1_MAX_temp - 150; + ADC1_MAX_CAL = ADC1_MAX_temp - 150; ADC2_MIN_CAL = ADC2_MIN_temp + 150; ADC2_MID_CAL = ADC2_MID_temp; - ADC2_MAX_CAL = ADC2_MAX_temp - 150; + ADC2_MAX_CAL = ADC2_MAX_temp - 150; consoleLog("OK\n"); } else { adc_cal_valid = 0; @@ -519,11 +528,11 @@ void updateCurSpdLim(void) { filtLowPass32(adc_buffer.l_tx2, FILTER, &adc1_fixdt); filtLowPass32(adc_buffer.l_rx2, FILTER, &adc2_fixdt); cur_spd_timeout++; - HAL_Delay(5); + HAL_Delay(5); } // Calculate scaling factors - cur_factor = CLAMP((adc1_fixdt - (ADC1_MIN_CAL << 16)) / (ADC1_MAX_CAL - ADC1_MIN_CAL), 6553, 65535); // ADC1, MIN_cur(10%) = 1.5 A + cur_factor = CLAMP((adc1_fixdt - (ADC1_MIN_CAL << 16)) / (ADC1_MAX_CAL - ADC1_MIN_CAL), 6553, 65535); // ADC1, MIN_cur(10%) = 1.5 A spd_factor = CLAMP((adc2_fixdt - (ADC2_MIN_CAL << 16)) / (ADC2_MAX_CAL - ADC2_MIN_CAL), 3276, 65535); // ADC2, MIN_spd(5%) = 50 rpm // Update maximum limits @@ -564,7 +573,7 @@ void saveConfig() { EE_WriteVariable(VirtAddVarTab[8], rtP_Left.n_max); HAL_FLASH_Lock(); } - #endif + #endif } /* @@ -612,17 +621,17 @@ void poweroffPressCheck(void) { uint16_t cnt_press = 0; while(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { HAL_Delay(10); - if (cnt_press++ == 5 * 100) { shortBeep(5); } + if (cnt_press++ == 5 * 100) { shortBeep(5); } } if (cnt_press >= 5 * 100) { // Check if press is more than 5 sec - HAL_Delay(300); + HAL_Delay(300); if (HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { // Double press: Adjust Max Current, Max Speed - while(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { HAL_Delay(10); } + while(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { HAL_Delay(10); } longBeep(8); updateCurSpdLim(); shortBeep(5); } else { // Long press: Calibrate ADC Limits - longBeep(16); + longBeep(16); adcCalibLim(); shortBeep(5); } @@ -671,7 +680,7 @@ void readCommand(void) { Nunchuk_Read(); cmd1 = CLAMP((nunchuk_data[0] - 127) * 8, INPUT_MIN, INPUT_MAX); // x - axis. Nunchuk joystick readings range 30 - 230 cmd2 = CLAMP((nunchuk_data[1] - 128) * 8, INPUT_MIN, INPUT_MAX); // y - axis - + #ifdef SUPPORT_BUTTONS button1 = (uint8_t)nunchuk_data[5] & 1; button2 = (uint8_t)(nunchuk_data[5] >> 1) & 1; @@ -704,27 +713,27 @@ void readCommand(void) { #ifdef CONTROL_ADC // ADC values range: 0-4095, see ADC-calibration in config.h #ifdef ADC1_MID_POT - cmd1 = CLAMP((adc_buffer.l_tx2 - ADC1_MID_CAL) * INPUT_MAX / (ADC1_MAX_CAL - ADC1_MID_CAL), 0, INPUT_MAX) - -CLAMP((ADC1_MID_CAL - adc_buffer.l_tx2) * INPUT_MAX / (ADC1_MID_CAL - ADC1_MIN_CAL), 0, INPUT_MAX); // ADC1 + cmd1 = CLAMP((adc_buffer.l_tx2 - ADC1_MID_CAL) * INPUT_MAX / (ADC1_MAX_CAL - ADC1_MID_CAL), 0, INPUT_MAX) + -CLAMP((ADC1_MID_CAL - adc_buffer.l_tx2) * INPUT_MAX / (ADC1_MID_CAL - ADC1_MIN_CAL), 0, INPUT_MAX); // ADC1 #else cmd1 = CLAMP((adc_buffer.l_tx2 - ADC1_MIN_CAL) * INPUT_MAX / (ADC1_MAX_CAL - ADC1_MIN_CAL), 0, INPUT_MAX); // ADC1 #endif #ifdef ADC2_MID_POT - cmd2 = CLAMP((adc_buffer.l_rx2 - ADC2_MID_CAL) * INPUT_MAX / (ADC2_MAX_CAL - ADC2_MID_CAL), 0, INPUT_MAX) - -CLAMP((ADC2_MID_CAL - adc_buffer.l_rx2) * INPUT_MAX / (ADC2_MID_CAL - ADC2_MIN_CAL), 0, INPUT_MAX); // ADC2 + cmd2 = CLAMP((adc_buffer.l_rx2 - ADC2_MID_CAL) * INPUT_MAX / (ADC2_MAX_CAL - ADC2_MID_CAL), 0, INPUT_MAX) + -CLAMP((ADC2_MID_CAL - adc_buffer.l_rx2) * INPUT_MAX / (ADC2_MID_CAL - ADC2_MIN_CAL), 0, INPUT_MAX); // ADC2 #else cmd2 = CLAMP((adc_buffer.l_rx2 - ADC2_MIN_CAL) * INPUT_MAX / (ADC2_MAX_CAL - ADC2_MIN_CAL), 0, INPUT_MAX); // ADC2 #endif #ifdef ADC_PROTECT_ENA - if (adc_buffer.l_tx2 >= (ADC1_MIN_CAL - ADC_PROTECT_THRESH) && adc_buffer.l_tx2 <= (ADC1_MAX_CAL + ADC_PROTECT_THRESH) && + if (adc_buffer.l_tx2 >= (ADC1_MIN_CAL - ADC_PROTECT_THRESH) && adc_buffer.l_tx2 <= (ADC1_MAX_CAL + ADC_PROTECT_THRESH) && adc_buffer.l_rx2 >= (ADC2_MIN_CAL - ADC_PROTECT_THRESH) && adc_buffer.l_rx2 <= (ADC2_MAX_CAL + ADC_PROTECT_THRESH)) { - if (timeoutFlagADC) { // Check for previous timeout flag + if (timeoutFlagADC) { // Check for previous timeout flag if (timeoutCntADC-- <= 0) // Timeout de-qualification - timeoutFlagADC = 0; // Timeout flag cleared + timeoutFlagADC = 0; // Timeout flag cleared } else { - timeoutCntADC = 0; // Reset the timeout counter + timeoutCntADC = 0; // Reset the timeout counter } } else { if (timeoutCntADC++ >= ADC_PROTECT_TIMEOUT) { // Timeout qualification @@ -739,7 +748,7 @@ void readCommand(void) { cmd2 = 0; } else { ctrlModReq = ctrlModReqRaw; // Follow the Mode request - } + } #endif #if defined(SUPPORT_BUTTONS_LEFT) || defined(SUPPORT_BUTTONS_RIGHT) @@ -751,12 +760,12 @@ void readCommand(void) { #if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3) // Handle received data validity, timeout and fix out-of-sync if necessary - #ifdef CONTROL_IBUS + #ifdef CONTROL_IBUS for (uint8_t i = 0; i < (IBUS_NUM_CHANNELS * 2); i+=2) { ibus_captured_value[(i/2)] = CLAMP(command.channels[i] + (command.channels[i+1] << 8) - 1000, 0, INPUT_MAX); // 1000-2000 -> 0-1000 } cmd1 = CLAMP((ibus_captured_value[0] - 500) * 2, INPUT_MIN, INPUT_MAX); - cmd2 = CLAMP((ibus_captured_value[1] - 500) * 2, INPUT_MIN, INPUT_MAX); + cmd2 = CLAMP((ibus_captured_value[1] - 500) * 2, INPUT_MIN, INPUT_MAX); #else if (IN_RANGE(command.steer, INPUT_MIN, INPUT_MAX) && IN_RANGE(command.speed, INPUT_MIN, INPUT_MAX)) { cmd1 = command.steer; @@ -797,7 +806,7 @@ void readCommand(void) { timeoutFlagSerial = timeoutFlagSerial_L || timeoutFlagSerial_R; #endif - #ifdef VARIANT_HOVERCAR + #ifdef VARIANT_HOVERCAR brakePressed = (uint8_t)(cmd1 > 50); #endif @@ -821,27 +830,27 @@ void readCommand(void) { */ void usart2_rx_check(void) { - #if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) + #if defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) static uint32_t old_pos; uint32_t pos; pos = rx_buffer_L_len - __HAL_DMA_GET_COUNTER(huart2.hdmarx); // Calculate current position in buffer #endif - #if defined(DEBUG_SERIAL_USART2) - if (pos != old_pos) { // Check change in received data + #if defined(SERIAL_USART2_DMA) + 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 - if (pos > 0) { // Check and continue with beginning of buffer - usart_process_debug(&rx_buffer_L[0], pos); // Process remaining data + usart_process_debug(&rx_buffer_L[old_pos], rx_buffer_L_len - old_pos); // First Process 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 } } } - #endif // DEBUG_SERIAL_USART2 + #endif #ifdef CONTROL_SERIAL_USART2 - uint8_t *ptr; + uint8_t *ptr; if (pos != old_pos) { // Check change in received data ptr = (uint8_t *)&command_raw; // Initialize the pointer with command_raw address if (pos > old_pos && (pos - old_pos) == command_len) { // "Linear" buffer mode: check if current position is over previous one AND data length equals expected length @@ -850,7 +859,7 @@ void usart2_rx_check(void) } else if ((rx_buffer_L_len - old_pos + pos) == command_len) { // "Overflow" buffer mode: check if data length equals expected length 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 - ptr += rx_buffer_L_len - old_pos; // Move to correct position in command_raw + ptr += rx_buffer_L_len - old_pos; // Move to correct position in command_raw memcpy(ptr, &rx_buffer_L[0], pos); // Copy remaining data } usart_process_command(&command_raw, &command, 2); // Process data @@ -859,7 +868,7 @@ void usart2_rx_check(void) #endif // CONTROL_SERIAL_USART2 #ifdef SIDEBOARD_SERIAL_USART2 - uint8_t *ptr; + uint8_t *ptr; if (pos != old_pos) { // Check change in received data ptr = (uint8_t *)&Sideboard_L_raw; // Initialize the pointer with Sideboard_raw address if (pos > old_pos && (pos - old_pos) == Sideboard_L_len) { // "Linear" buffer mode: check if current position is over previous one AND data length equals expected length @@ -868,7 +877,7 @@ void usart2_rx_check(void) } else if ((rx_buffer_L_len - old_pos + pos) == Sideboard_L_len) { // "Overflow" buffer mode: check if data length equals expected length 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 - ptr += rx_buffer_L_len - old_pos; // Move to correct position in Sideboard_raw + ptr += rx_buffer_L_len - old_pos; // Move to correct position in Sideboard_raw memcpy(ptr, &rx_buffer_L[0], pos); // Copy remaining data } usart_process_sideboard(&Sideboard_L_raw, &Sideboard_L, 2); // Process data @@ -876,7 +885,7 @@ void usart2_rx_check(void) } #endif // SIDEBOARD_SERIAL_USART2 - #if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) + #if defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) old_pos = pos; // Update old position if (old_pos == rx_buffer_L_len) { // Check and manually update if we reached end of buffer old_pos = 0; @@ -891,27 +900,27 @@ void usart2_rx_check(void) */ void usart3_rx_check(void) { - #if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) + #if defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) static uint32_t old_pos; - uint32_t pos; + uint32_t pos; pos = rx_buffer_R_len - __HAL_DMA_GET_COUNTER(huart3.hdmarx); // Calculate current position in buffer #endif - #if defined(DEBUG_SERIAL_USART3) - if (pos != old_pos) { // Check change in received data + #if defined(SERIAL_USART3_DMA) + 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 - usart_process_debug(&rx_buffer_R[old_pos], rx_buffer_R_len - old_pos); // First Process data from the end of buffer - if (pos > 0) { // Check and continue with beginning of buffer - usart_process_debug(&rx_buffer_R[0], pos); // Process remaining data + usart_process_debug(&rx_buffer_R[old_pos], rx_buffer_R_len - old_pos); // First Process data from the end of buffer + if (pos > 0) { // Check and continue with beginning of buffer + usart_process_debug(&rx_buffer_R[0], pos); // Process remaining data } } } - #endif // DEBUG_SERIAL_USART3 + #endif #ifdef CONTROL_SERIAL_USART3 - uint8_t *ptr; + uint8_t *ptr; if (pos != old_pos) { // Check change in received data ptr = (uint8_t *)&command_raw; // Initialize the pointer with command_raw address if (pos > old_pos && (pos - old_pos) == command_len) { // "Linear" buffer mode: check if current position is over previous one AND data length equals expected length @@ -920,7 +929,7 @@ void usart3_rx_check(void) } else if ((rx_buffer_R_len - old_pos + pos) == command_len) { // "Overflow" buffer mode: check if data length equals expected length 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; // Move to correct position in command_raw + ptr += rx_buffer_R_len - old_pos; // Move to correct position in command_raw memcpy(ptr, &rx_buffer_R[0], pos); // Copy remaining data } usart_process_command(&command_raw, &command, 3); // Process data @@ -938,7 +947,7 @@ void usart3_rx_check(void) } else if ((rx_buffer_R_len - old_pos + pos) == Sideboard_R_len) { // "Overflow" buffer mode: check if data length equals expected length 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; // Move to correct position in Sideboard_raw + ptr += rx_buffer_R_len - old_pos; // Move to correct position in Sideboard_raw memcpy(ptr, &rx_buffer_R[0], pos); // Copy remaining data } usart_process_sideboard(&Sideboard_R_raw, &Sideboard_R, 3); // Process data @@ -946,7 +955,7 @@ void usart3_rx_check(void) } #endif // SIDEBOARD_SERIAL_USART3 - #if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) + #if defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) old_pos = pos; // Update old position if (old_pos == rx_buffer_R_len) { // Check and manually update if we reached end of buffer old_pos = 0; @@ -957,14 +966,20 @@ void usart3_rx_check(void) /* * Process Rx debug user command input */ -#if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3) +#if defined(SERIAL_USART2_DMA) || defined(SERIAL_USART3_DMA) void usart_process_debug(uint8_t *userCommand, uint32_t len) { for (; len > 0; len--, userCommand++) { + #if defined(VARIANT_BIPROPELLANT) && defined(SERIAL_USART2_DMA) + protocol_byte( &sUSART2, (unsigned char) *userCommand ); + #elif defined(VARIANT_BIPROPELLANT) && defined(SERIAL_USART3_DMA) + protocol_byte( &sUSART3, (unsigned char) *userCommand ); + #else if (*userCommand != '\n' && *userCommand != '\r') { // Do not accept 'new line' and 'carriage return' commands - consoleLog("-- Command received --\r\n"); + consoleLog("-- Command received --\r\n"); // handle_input(*userCommand); // -> Create this function to handle the user commands } + #endif } } #endif // SERIAL_DEBUG @@ -1001,7 +1016,7 @@ void usart_process_command(SerialCommand *command_in, SerialCommand *command_out uint16_t checksum; if (command_in->start == SERIAL_START_FRAME) { checksum = (uint16_t)(command_in->start ^ command_in->steer ^ command_in->speed); - if (command_in->checksum == checksum) { + if (command_in->checksum == checksum) { *command_out = *command_in; if (usart_idx == 2) { // Sideboard USART2 #ifdef CONTROL_SERIAL_USART2 @@ -1026,11 +1041,11 @@ void usart_process_command(SerialCommand *command_in, SerialCommand *command_out */ #if defined(SIDEBOARD_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART3) void usart_process_sideboard(SerialSideboard *Sideboard_in, SerialSideboard *Sideboard_out, uint8_t usart_idx) -{ +{ uint16_t checksum; if (Sideboard_in->start == SERIAL_START_FRAME) { checksum = (uint16_t)(Sideboard_in->start ^ Sideboard_in->roll ^ Sideboard_in->pitch ^ Sideboard_in->yaw ^ Sideboard_in->sensors); - if (Sideboard_in->checksum == checksum) { + if (Sideboard_in->checksum == checksum) { *Sideboard_out = *Sideboard_in; if (usart_idx == 2) { // Sideboard USART2 #ifdef SIDEBOARD_SERIAL_USART2 @@ -1087,7 +1102,7 @@ void sideboardLeds(uint8_t *leds) { // Battery Level Indicator: use LED1, LED2, LED3 if (main_loop_counter % BAT_BLINK_INTERVAL == 0) { // | RED (LED1) | YELLOW (LED3) | GREEN (LED2) | if (batVoltage < BAT_DEAD) { // | 0 | 0 | 0 | - *leds &= ~LED1_SET & ~LED3_SET & ~LED2_SET; + *leds &= ~LED1_SET & ~LED3_SET & ~LED2_SET; } else if (batVoltage < BAT_LVL1) { // | B | 0 | 0 | *leds ^= LED1_SET; *leds &= ~LED3_SET & ~LED2_SET; @@ -1159,7 +1174,7 @@ void sideboardSensors(uint8_t sensors) { case 4: // COMMUTATION rtP_Left.z_ctrlTypSel = 0; rtP_Right.z_ctrlTypSel = 0; - break; + break; } shortBeepMany(sensor1_index + 1); } @@ -1170,17 +1185,17 @@ void sideboardSensors(uint8_t sensors) { if (sensor2_index > 1) { sensor2_index = 0; } switch (sensor2_index) { case 0: // FW Disabled - rtP_Left.b_fieldWeakEna = 0; + rtP_Left.b_fieldWeakEna = 0; rtP_Right.b_fieldWeakEna = 0; Input_Lim_Init(); break; case 1: // FW Enabled - rtP_Left.b_fieldWeakEna = 1; + rtP_Left.b_fieldWeakEna = 1; rtP_Right.b_fieldWeakEna = 1; Input_Lim_Init(); - break; + break; } - shortBeepMany(sensor2_index + 1); + shortBeepMany(sensor2_index + 1); } #endif } @@ -1193,18 +1208,18 @@ void sideboardSensors(uint8_t sensors) { * Max: 32767.99998474121 * Min: -32768 * Res: 1.52587890625e-05 - * + * * Inputs: u = int16 or int32 * Outputs: y = fixdt(1,32,16) * Parameters: coef = fixdt(0,16,16) = [0,65535U] - * - * Example: + * + * Example: * If coef = 0.8 (in floating point), then coef = 0.8 * 2^16 = 52429 (in fixed-point) * filtLowPass16(u, 52429, &y); * yint = (int16_t)(y >> 16); // the integer output is the fixed-point ouput shifted by 16 bits */ void filtLowPass32(int32_t u, uint16_t coef, int32_t *y) { - int64_t tmp; + int64_t tmp; tmp = ((int64_t)((u << 4) - (*y >> 12)) * coef) >> 4; tmp = CLAMP(tmp, -2147483648LL, 2147483647LL); // Overflow protection: 2147483647LL = 2^31 - 1 *y = (int32_t)tmp + (*y); @@ -1215,9 +1230,9 @@ void sideboardSensors(uint8_t sensors) { // Parameters: coef = fixdt(0,16,16) = [0,65535U] // yint = (int16_t)(y >> 20); // the integer output is the fixed-point ouput shifted by 20 bits // void filtLowPass32(int16_t u, uint16_t coef, int32_t *y) { - // int32_t tmp; - // tmp = (int16_t)(u << 4) - (*y >> 16); - // tmp = CLAMP(tmp, -32768, 32767); // Overflow protection + // int32_t tmp; + // tmp = (int16_t)(u << 4) - (*y >> 16); + // tmp = CLAMP(tmp, -32768, 32767); // Overflow protection // *y = coef * tmp + (*y); // } @@ -1246,7 +1261,7 @@ void rateLimiter16(int16_t u, int16_t rate, int16_t *y) { } - /* mixerFcn(rtu_speed, rtu_steer, &rty_speedR, &rty_speedL); + /* mixerFcn(rtu_speed, rtu_steer, &rty_speedR, &rty_speedL); * Inputs: rtu_speed, rtu_steer = fixdt(1,16,4) * Outputs: rty_speedR, rty_speedL = int16_t * Parameters: SPEED_COEFFICIENT, STEER_COEFFICIENT = fixdt(0,16,14) @@ -1259,9 +1274,9 @@ void mixerFcn(int16_t rtu_speed, int16_t rtu_steer, int16_t *rty_speedR, int16_t prodSpeed = (int16_t)((rtu_speed * (int16_t)SPEED_COEFFICIENT) >> 14); prodSteer = (int16_t)((rtu_steer * (int16_t)STEER_COEFFICIENT) >> 14); - tmp = prodSpeed - prodSteer; + tmp = prodSpeed - prodSteer; tmp = CLAMP(tmp, -32768, 32767); // Overflow protection - *rty_speedR = (int16_t)(tmp >> 4); // Convert from fixed-point to int + *rty_speedR = (int16_t)(tmp >> 4); // Convert from fixed-point to int *rty_speedR = CLAMP(*rty_speedR, INPUT_MIN, INPUT_MAX); tmp = prodSpeed + prodSteer; @@ -1276,7 +1291,7 @@ void mixerFcn(int16_t rtu_speed, int16_t rtu_steer, int16_t *rty_speedR, int16_t /* multipleTapDet(int16_t u, uint32_t timeNow, MultipleTap *x) * This function detects multiple tap presses, such as double tapping, triple tapping, etc. - * Inputs: u = int16_t (input signal); timeNow = uint32_t (current time) + * Inputs: u = int16_t (input signal); timeNow = uint32_t (current time) * Outputs: x->b_multipleTap (get the output here) */ void multipleTapDet(int16_t u, uint32_t timeNow, MultipleTap *x) { @@ -1285,7 +1300,7 @@ void multipleTapDet(int16_t u, uint32_t timeNow, MultipleTap *x) { uint8_t b_pulse; uint8_t z_pulseCnt; uint8_t z_pulseCntRst; - uint32_t t_time; + uint32_t t_time; // Detect hysteresis if (x->b_hysteresis) { From ab8312b2ad84cd7f195abcc17cac06049860fdc1 Mon Sep 17 00:00:00 2001 From: phail Date: Sun, 28 Jun 2020 17:28:07 +0200 Subject: [PATCH 08/16] remove interrupt based USART revert unneccessary code changes --- Inc/comms.h | 68 ++++++++----------- Inc/config.h | 16 ++--- Inc/control_structures.h | 2 - Inc/protocolfunctions.h | 2 - Inc/setup.h | 2 - Src/comms.c | 139 --------------------------------------- Src/main.c | 24 +------ Src/protocolfunctions.c | 35 ++-------- Src/setup.c | 102 ++-------------------------- Src/stm32f1xx_it.c | 28 ++------ Src/util.c | 29 ++++---- 11 files changed, 61 insertions(+), 386 deletions(-) diff --git a/Inc/comms.h b/Inc/comms.h index a94fcb0da..9893719c9 100644 --- a/Inc/comms.h +++ b/Inc/comms.h @@ -1,47 +1,31 @@ -#pragma once - -#define SERIAL_USART_BUFFER_SIZE 1024 // TODO: implement send_wait routine.. -typedef struct tag_serial_usart_buffer { - SERIAL_USART_IT_BUFFERTYPE buff[SERIAL_USART_BUFFER_SIZE]; - int head; - int tail; - - // count of buffer overflows - unsigned int overflow; - -} SERIAL_USART_BUFFER; - -#if defined(SERIAL_USART2_IT) - - extern volatile SERIAL_USART_BUFFER usart2_it_TXbuffer; - extern volatile SERIAL_USART_BUFFER usart2_it_RXbuffer; - - int USART2_IT_starttx(); - int USART2_IT_send(unsigned char *data, int len); - void USART2_IT_IRQ(USART_TypeDef *us); - -#endif - -#if defined(SERIAL_USART3_IT) - - extern volatile SERIAL_USART_BUFFER usart3_it_TXbuffer; - extern volatile SERIAL_USART_BUFFER usart3_it_RXbuffer; - - int USART3_IT_starttx(); - int USART3_IT_send(unsigned char *data, int len); - void USART3_IT_IRQ(USART_TypeDef *us); - -#endif - -int serial_usart_buffer_count(volatile SERIAL_USART_BUFFER *usart_buf); -void serial_usart_buffer_push (volatile SERIAL_USART_BUFFER *usart_buf, SERIAL_USART_IT_BUFFERTYPE value); -SERIAL_USART_IT_BUFFERTYPE serial_usart_buffer_pop (volatile SERIAL_USART_BUFFER *usart_buf); - +/* +* This file is part of the hoverboard-firmware-hack project. +* +* Copyright (C) 2017-2018 Rene Hopf +* Copyright (C) 2017-2018 Nico Stute +* Copyright (C) 2017-2018 Niklas Fauth +* +* This program is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program. If not, see . +*/ + +// Define to prevent recursive inclusion +#ifndef COMMS_H +#define COMMS_H void setScopeChannel(uint8_t ch, int16_t val); -void consoleScope(); -///////////////////////////////////////////////////////// +void consoleScope(void); +#endif -#define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) \ No newline at end of file diff --git a/Inc/config.h b/Inc/config.h index c4e6c5686..43c8671fe 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -255,8 +255,6 @@ // ############################ VARIANT_BIPROPELLANT SETTINGS ############################ #ifdef VARIANT_BIPROPELLANT -// #define SERIAL_USART2_IT -// #define SERIAL_USART3_IT // #define SERIAL_USART2_DMA #define SERIAL_USART3_DMA #define USART2_BAUD 115200 @@ -266,11 +264,10 @@ #undef BEEPS_BACKWARD #define BEEPS_BACKWARD 0 // 0 or 1 #define SERIAL_BUFFER_SIZE 64 // [bytes] Size of Serial Rx buffer. Make sure it is always larger than the structure size + #define USART2_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B + #define USART3_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B #endif - #define SERIAL_USART_IT_BUFFERTYPE unsigned char - #define USART2_WORDLENGTH UART_WORDLENGTH_8B - #define USART3_WORDLENGTH UART_WORDLENGTH_8B -// ######################## END OF VARIANT_USART SETTINGS ######################### +// ######################## END OF VARIANT_BIPROPELLANT SETTINGS ######################### // ################################# VARIANT_NUNCHUK SETTINGS ############################ @@ -383,11 +380,11 @@ #define ADC2_MAX 2200 // max ADC2-value while poti at maximum-position (0 - 4095) #define SPEED_COEFFICIENT 16384 // 1.0f #define STEER_COEFFICIENT 0 // 0.0f - // #define INVERT_R_DIRECTION // Invert rotation of right motor - // #define INVERT_L_DIRECTION // Invert rotation of left motor + // #define INVERT_R_DIRECTION // Invert rotation of right motor + // #define INVERT_L_DIRECTION // Invert rotation of left motor #define SIDEBOARD_SERIAL_USART3 #define FEEDBACK_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! - // #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! + // #define DEBUG_SERIAL_USART3 // right sensor board cable, disable if I2C (nunchuk or lcd) is used! #endif // Multiple tap detection: default DOUBLE Tap on Brake pedal (4 pulses) @@ -454,7 +451,6 @@ #if defined(DEBUG_SERIAL_USART3) #define SERIAL_USART3_DMA #endif - // ########################### UART SETIINGS ############################ diff --git a/Inc/control_structures.h b/Inc/control_structures.h index 76912a79a..70d83d982 100644 --- a/Inc/control_structures.h +++ b/Inc/control_structures.h @@ -26,8 +26,6 @@ //// control structures used in firmware // used in main -extern PROTOCOL_POSN_DATA PosnData; - extern PROTOCOL_SPEED_DATA SpeedData; extern PROTOCOL_PWM_DATA PWMData; diff --git a/Inc/protocolfunctions.h b/Inc/protocolfunctions.h index 5ab5aece8..76e29088a 100644 --- a/Inc/protocolfunctions.h +++ b/Inc/protocolfunctions.h @@ -4,8 +4,6 @@ int setup_protocol(PROTOCOL_STAT *s); - -extern PROTOCOL_STAT sSoftwareSerial; extern PROTOCOL_STAT sUSART2; extern PROTOCOL_STAT sUSART3; diff --git a/Inc/setup.h b/Inc/setup.h index d3e18ffdf..b7dd9d033 100644 --- a/Inc/setup.h +++ b/Inc/setup.h @@ -31,8 +31,6 @@ void MX_ADC1_Init(void); void MX_ADC2_Init(void); void UART2_Init(void); void UART3_Init(void); -void USART2_IT_init(); -void USART3_IT_init(); #endif diff --git a/Src/comms.c b/Src/comms.c index dcd816742..e8eae7d4e 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -12,15 +12,6 @@ extern UART_HandleTypeDef huart3; static volatile uint8_t uart_buf[100]; static volatile int16_t ch_buf[8]; -#if defined(SERIAL_USART2_IT) - volatile SERIAL_USART_BUFFER usart2_it_TXbuffer; - volatile SERIAL_USART_BUFFER usart2_it_RXbuffer; -#endif -#if defined(SERIAL_USART3_IT) - volatile SERIAL_USART_BUFFER usart3_it_TXbuffer; - volatile SERIAL_USART_BUFFER usart3_it_RXbuffer; -#endif - uint8_t debug_out = 1; uint8_t enablescope = 1; @@ -77,65 +68,9 @@ void consoleScope(void) { #endif #endif - #if defined DEBUG_SERIAL_ASCII && defined DEBUG_SOFTWARE_SERIAL - if (debug_out){ - memset((void *)uart_buf, 0, sizeof(uart_buf)); - sprintf((void *)uart_buf, "1:%i 2:%i 3:%i 4:%i 5:%i 6:%i 7:%i 8:%i\r\n", ch_buf[0], ch_buf[1], ch_buf[2], ch_buf[3], ch_buf[4], ch_buf[5], ch_buf[6], ch_buf[7]); - softwareserial_Send((unsigned char *)uart_buf, strlen((char*)uart_buf)); - } - #endif - -} - -#ifdef SERIAL_USART2_IT - -int USART2_IT_starttx() { - __HAL_UART_ENABLE_IT(&huart2, UART_IT_TXE); - return 1; -} - -int USART2_IT_send(unsigned char *data, int len) { - - int count = serial_usart_buffer_count(&usart2_it_TXbuffer); - if (count + len + 1 > SERIAL_USART_BUFFER_SIZE-3){ - usart2_it_TXbuffer.overflow++; - return -1; - } - - for (int i = 0; i < len; i++){ - serial_usart_buffer_push(&usart2_it_TXbuffer, (SERIAL_USART_IT_BUFFERTYPE) data[i]); - } - - return USART2_IT_starttx(); -} - -////////////////////////////////////////////////////// -// called from actual IRQ routines -void USART2_IT_IRQ(USART_TypeDef *us) { - volatile uint32_t *SR = &us->SR; // USART Status register - volatile uint32_t *DR = &us->DR; // USART Data register - volatile uint32_t *CR1 = &us->CR1; // USART Control register 1 - // Transmit - if ((*SR) & UART_FLAG_TXE) { - if (serial_usart_buffer_count(&usart2_it_TXbuffer) == 0) { - *CR1 = (*CR1 & ~(USART_CR1_TXEIE | USART_CR1_TCIE)); - } else { - *DR = (serial_usart_buffer_pop(&usart2_it_TXbuffer) & 0x1ff); - } - } - - // Receive - if (((*SR) & UART_FLAG_RXNE)) { - SERIAL_USART_IT_BUFFERTYPE rword = (*DR) & 0x01FF; - serial_usart_buffer_push(&usart2_it_RXbuffer, rword); - } - - return; } -#endif - #if defined(SERIAL_USART2_DMA) && VARIANT_BIPROPELLANT int USART2_DMA_send(unsigned char *data, int len) { HAL_UART_Transmit_DMA(&huart2, (uint8_t *)data, len); @@ -150,77 +85,3 @@ int USART3_DMA_send(unsigned char *data, int len) { } #endif -#ifdef SERIAL_USART3_IT - -int USART3_IT_starttx() { - __HAL_UART_ENABLE_IT(&huart3, UART_IT_TXE); - return 1; -} - -int USART3_IT_send(unsigned char *data, int len) { - - int count = serial_usart_buffer_count(&usart3_it_TXbuffer); - if (count + len + 1 > SERIAL_USART_BUFFER_SIZE-3){ - usart3_it_TXbuffer.overflow++; - return -1; - } - - for (int i = 0; i < len; i++){ - serial_usart_buffer_push(&usart3_it_TXbuffer, (SERIAL_USART_IT_BUFFERTYPE) data[i]); - } - - return USART3_IT_starttx(); -} - -////////////////////////////////////////////////////// -// called from actual IRQ routines -void USART3_IT_IRQ(USART_TypeDef *us) { - volatile uint32_t *SR = &us->SR; // USART Status register - volatile uint32_t *DR = &us->DR; // USART Data register - volatile uint32_t *CR1 = &us->CR1; // USART Control register 1 - - // Transmit - if ((*SR) & UART_FLAG_TXE) { - if (serial_usart_buffer_count(&usart3_it_TXbuffer) == 0) { - *CR1 = (*CR1 & ~(USART_CR1_TXEIE | USART_CR1_TCIE)); - } else { - *DR = (serial_usart_buffer_pop(&usart3_it_TXbuffer) & 0x1ff); - } - } - - // Receive - if (((*SR) & UART_FLAG_RXNE)) { - SERIAL_USART_IT_BUFFERTYPE rword = (*DR) & 0x01FF; - serial_usart_buffer_push(&usart3_it_RXbuffer, rword); - } - - return; -} - -#endif - -int serial_usart_buffer_count(volatile SERIAL_USART_BUFFER *usart_buf) { - int count = usart_buf->head - usart_buf->tail; - if (count < 0) count += SERIAL_USART_BUFFER_SIZE; - return count; -} - -void serial_usart_buffer_push(volatile SERIAL_USART_BUFFER *usart_buf, SERIAL_USART_IT_BUFFERTYPE value) { - int count = serial_usart_buffer_count(usart_buf); - if (count >= SERIAL_USART_BUFFER_SIZE-2){ - usart_buf->overflow++; - return; - } - - usart_buf->buff[usart_buf->head] = value; - usart_buf->head = ((usart_buf->head + 1 ) % SERIAL_USART_BUFFER_SIZE); -} - -SERIAL_USART_IT_BUFFERTYPE serial_usart_buffer_pop(volatile SERIAL_USART_BUFFER *usart_buf) { - SERIAL_USART_IT_BUFFERTYPE t = 0; - if (usart_buf->head != usart_buf->tail){ - t = usart_buf->buff[usart_buf->tail]; - usart_buf->tail = ((usart_buf->tail + 1 ) % SERIAL_USART_BUFFER_SIZE); - } - return t; -} diff --git a/Src/main.c b/Src/main.c index 8db952797..941cfcf39 100644 --- a/Src/main.c +++ b/Src/main.c @@ -198,14 +198,6 @@ int main(void) { int16_t board_temp_adcFilt = adc_buffer.temp; int16_t board_temp_deg_c; - #ifdef SERIAL_USART2_IT - USART2_IT_init(); - #endif - #ifdef SERIAL_USART3_IT - USART3_IT_init(); - #endif - - while(1) { HAL_Delay(DELAY_IN_MAIN_LOOP); //delay in ms @@ -213,26 +205,16 @@ int main(void) { readCommand(); // Read Command: cmd1, cmd2 calcAvgSpeed(); // Calculate average measured speed: speedAvg, speedAvgAbs - #if defined(VARIANT_BIPROPELLANT) && (defined(SERIAL_USART2_IT) || defined(SERIAL_USART2_DMA)) - #if defined(SERIAL_USART2_IT) - while ( serial_usart_buffer_count(&usart2_it_RXbuffer) > 0 ) { - protocol_byte( &sUSART2, (unsigned char) serial_usart_buffer_pop(&usart2_it_RXbuffer) ); - } - #endif + #if defined(VARIANT_BIPROPELLANT) && defined(SERIAL_USART2_DMA) protocol_tick( &sUSART2 ); - if(sUSART3.ascii.enable_immediate) + if(sUSART2.ascii.enable_immediate) { timeout = 0; } cmd2 = (PWMData.pwm[0] + PWMData.pwm[1]) /2; // Speed cmd1 = PWMData.pwm[0] - cmd2; // Steer #endif - #if defined(VARIANT_BIPROPELLANT) && (defined(SERIAL_USART3_IT) || defined(SERIAL_USART3_DMA)) - #if defined(SERIAL_USART3_IT) - while ( serial_usart_buffer_count(&usart3_it_RXbuffer) > 0 ) { - protocol_byte( &sUSART3, (unsigned char) serial_usart_buffer_pop(&usart3_it_RXbuffer) ); - } - #endif + #if defined(VARIANT_BIPROPELLANT) && defined(SERIAL_USART3_DMA) protocol_tick( &sUSART3 ); if(sUSART3.ascii.enable_immediate) { diff --git a/Src/protocolfunctions.c b/Src/protocolfunctions.c index feb8f3604..1f3579fe1 100644 --- a/Src/protocolfunctions.c +++ b/Src/protocolfunctions.c @@ -12,10 +12,10 @@ #include "control_structures.h" -#if defined(SERIAL_USART2_IT) || defined(SERIAL_USART2_DMA) +#if defined(SERIAL_USART2_DMA) PROTOCOL_STAT sUSART2; #endif -#if defined(SERIAL_USART3_IT) || defined(SERIAL_USART3_DMA) +#if defined(SERIAL_USART3_DMA) PROTOCOL_STAT sUSART3; #endif @@ -194,33 +194,6 @@ int setup_protocol(PROTOCOL_STAT *s) { int errors = 0; - #if defined(SERIAL_USART2_IT) - extern int USART2_IT_send(unsigned char *data, int len); - - errors += protocol_init(&sUSART2); - - sUSART2.send_serial_data=USART2_IT_send; - sUSART2.send_serial_data_wait=USART2_IT_send; - sUSART2.timeout1 = 500; - sUSART2.timeout2 = 100; - sUSART2.allow_ascii = 1; - - #endif - - #if defined(SERIAL_USART3_IT) - - extern int USART3_IT_send(unsigned char *data, int len); - - errors += protocol_init(&sUSART3); - - sUSART3.send_serial_data=USART3_IT_send; - sUSART3.send_serial_data_wait=USART3_IT_send; - sUSART3.timeout1 = 500; - sUSART3.timeout2 = 100; - sUSART3.allow_ascii = 1; - - #endif - #if defined(SERIAL_USART2_DMA) extern int USART2_DMA_send(unsigned char *data, int len); @@ -284,11 +257,11 @@ int setup_protocol(PROTOCOL_STAT *s) { void consoleLog(char *message) { - #if defined(SERIAL_USART2_IT) || defined(SERIAL_USART2_DMA) + #if defined(SERIAL_USART2_DMA) if (debug_out) protocol_send_text(&sUSART2, message, PROTOCOL_SOM_NOACK); #endif - #if defined(SERIAL_USART3_IT) || defined(SERIAL_USART3_DMA) + #if defined(SERIAL_USART3_DMA) if (debug_out) protocol_send_text(&sUSART3, message, PROTOCOL_SOM_NOACK); #endif } diff --git a/Src/setup.c b/Src/setup.c index d9c51cbc3..4ab89cb70 100644 --- a/Src/setup.c +++ b/Src/setup.c @@ -38,9 +38,6 @@ pb10 usart3 dma1 channel2/3 #include "defines.h" #include "config.h" #include "setup.h" -#include "comms.h" -#include -#include "protocolfunctions.h" TIM_HandleTypeDef htim_right; TIM_HandleTypeDef htim_left; @@ -57,11 +54,7 @@ DMA_HandleTypeDef hdma_usart3_tx; volatile adc_buf_t adc_buffer; -#ifdef SERIAL_USART2_IT - static int USART2WordLength = USART2_WORDLENGTH; -#endif - -#if defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) +#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) /* USART2 init function */ void UART2_Init(void) { @@ -87,7 +80,7 @@ volatile adc_buf_t adc_buffer; } #endif -#if defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) +#if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) /* USART3 init function */ void UART3_Init(void) { @@ -114,9 +107,8 @@ void UART3_Init(void) } #endif -#if defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || \ - defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || \ - defined(SERIAL_USART2_DMA) || defined(SERIAL_USART3_DMA) +#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || \ + defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; @@ -286,92 +278,6 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) } #endif -#ifdef SERIAL_USART2_IT -void USART2_IT_init(){ - memset((void*)&usart2_it_TXbuffer, 0, sizeof(usart2_it_TXbuffer)); - memset((void*)&usart2_it_RXbuffer, 0, sizeof(usart2_it_RXbuffer)); - - HAL_NVIC_DisableIRQ(USART2_IRQn); - - memset(&huart2, 0, sizeof(huart2)); - __HAL_RCC_GPIOA_CLK_ENABLE(); - __HAL_RCC_USART2_CLK_ENABLE(); - - huart2.Instance = USART2; - huart2.Init.BaudRate = USART2_BAUD; - huart2.Init.WordLength = USART2WordLength; - huart2.Init.StopBits = UART_STOPBITS_1; - huart2.Init.Parity = UART_PARITY_NONE; - huart2.Init.Mode = UART_MODE_TX_RX; - huart2.Init.OverSampling = UART_OVERSAMPLING_16; - huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; - HAL_UART_Init(&huart2); - - GPIO_InitTypeDef GPIO_InitStruct; - memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct)); - GPIO_InitStruct.Pin = GPIO_PIN_2; - GPIO_InitStruct.Pull = GPIO_PULLUP; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct)); - GPIO_InitStruct.Pin = GPIO_PIN_3; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - // start interrupt receive? - HAL_NVIC_SetPriority(USART2_IRQn, 1, 0); - HAL_NVIC_EnableIRQ(USART2_IRQn); - - __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE); -} -#endif - -#ifdef SERIAL_USART3_IT -void USART3_IT_init(){ - memset((void *)&usart3_it_TXbuffer, 0, sizeof(usart3_it_TXbuffer)); - memset((void *)&usart3_it_RXbuffer, 0, sizeof(usart3_it_RXbuffer)); - - memset(&huart3, 0, sizeof(huart3)); - __HAL_RCC_GPIOB_CLK_ENABLE(); - __HAL_RCC_USART3_CLK_ENABLE(); - - huart3.Instance = USART3; - huart3.Init.BaudRate = USART3_BAUD; - huart3.Init.WordLength = USART3_WORDLENGTH; - huart3.Init.StopBits = UART_STOPBITS_1; - huart3.Init.Parity = UART_PARITY_NONE; - huart3.Init.Mode = UART_MODE_TX_RX; - huart3.Init.OverSampling = UART_OVERSAMPLING_16; - huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; - HAL_UART_Init(&huart3); - - GPIO_InitTypeDef GPIO_InitStruct; - memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct)); - GPIO_InitStruct.Pin = GPIO_PIN_10; - GPIO_InitStruct.Pull = GPIO_PULLUP; - GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - memset(&GPIO_InitStruct, 0, sizeof(GPIO_InitStruct)); - GPIO_InitStruct.Pin = GPIO_PIN_11; - GPIO_InitStruct.Pull = GPIO_NOPULL; - GPIO_InitStruct.Mode = GPIO_MODE_INPUT; - GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); - - // start interrupt receive? - HAL_NVIC_SetPriority(USART3_IRQn, 1, 0); - HAL_NVIC_EnableIRQ(USART3_IRQn); - - __HAL_UART_ENABLE_IT(&huart3, UART_IT_RXNE); -} -#endif - DMA_HandleTypeDef hdma_i2c2_rx; DMA_HandleTypeDef hdma_i2c2_tx; diff --git a/Src/stm32f1xx_it.c b/Src/stm32f1xx_it.c index 288910ca5..f7f358f76 100644 --- a/Src/stm32f1xx_it.c +++ b/Src/stm32f1xx_it.c @@ -276,7 +276,7 @@ void EXTI15_10_IRQHandler(void) } #endif -#if defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) +#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) void DMA1_Channel6_IRQHandler(void) { /* USER CODE BEGIN DMA1_Channel4_IRQn 0 */ @@ -303,7 +303,7 @@ void DMA1_Channel7_IRQHandler(void) } #endif -#if defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) +#if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) /** * @brief This function handles DMA1 channel2 global interrupt. */ @@ -333,7 +333,7 @@ void DMA1_Channel3_IRQHandler(void) } #endif -#if defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) +#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) /** * @brief This function handles USART2 global interrupt. */ @@ -352,7 +352,7 @@ void USART2_IRQHandler(void) } #endif -#if defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) +#if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) /** * @brief This function handles USART3 global interrupt. */ @@ -371,26 +371,6 @@ void USART3_IRQHandler(void) } #endif -///////////////////////////////////////// -// UART interrupts - -#if defined(SERIAL_USART2_IT) -void USART2_IT_IRQ(USART_TypeDef *us); - -void USART2_IRQHandler(void){ - USART2_IT_IRQ(USART2); -} -#endif - -#if defined(SERIAL_USART3_IT) -void USART3_IT_IRQ(USART_TypeDef *us); - -void USART3_IRQHandler(void){ - USART3_IT_IRQ(USART3); -} -#endif - - /******************************************************************************/ /* STM32F1xx Peripheral Interrupt Handlers */ /* Add here the Interrupt Handlers for the used peripherals. */ diff --git a/Src/util.c b/Src/util.c index edff85c65..05d324c9d 100644 --- a/Src/util.c +++ b/Src/util.c @@ -154,7 +154,7 @@ static int16_t INPUT_MIN; // [-] Input target minimum limitation static int16_t timeoutCntADC = 0; // Timeout counter for ADC Protection #endif -#if defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) +#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) static uint8_t rx_buffer_L[SERIAL_BUFFER_SIZE]; // USART Rx DMA circular buffer static uint32_t rx_buffer_L_len = ARRAY_LEN(rx_buffer_L); #endif @@ -168,7 +168,7 @@ SerialSideboard Sideboard_L_raw; static uint32_t Sideboard_L_len = sizeof(Sideboard_L); #endif -#if defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) +#if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) static uint8_t rx_buffer_R[SERIAL_BUFFER_SIZE]; // USART Rx DMA circular buffer static uint32_t rx_buffer_R_len = ARRAY_LEN(rx_buffer_R); #endif @@ -267,24 +267,24 @@ void Input_Init(void) { Nunchuk_Init(); #endif - #if (defined(SERIAL_USART2_IT) || defined(SERIAL_USART2_DMA)) && defined(VARIANT_BIPROPELLANT) + #if defined(SERIAL_USART2_DMA) && defined(VARIANT_BIPROPELLANT) setup_protocol(&sUSART2); #endif - #if (defined(SERIAL_USART3_IT) || defined(SERIAL_USART3_DMA)) && defined(VARIANT_BIPROPELLANT) + #if defined(SERIAL_USART3_DMA) && defined(VARIANT_BIPROPELLANT) setup_protocol(&sUSART3); #endif - #if defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) + #if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) UART2_Init(); #endif - #if defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) + #if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) UART3_Init(); #endif - #if defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) + #if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) HAL_UART_Receive_DMA(&huart2, (uint8_t *)rx_buffer_L, sizeof(rx_buffer_L)); UART_DisableRxErrors(&huart2); #endif - #if defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) + #if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) HAL_UART_Receive_DMA(&huart3, (uint8_t *)rx_buffer_R, sizeof(rx_buffer_R)); UART_DisableRxErrors(&huart3); #endif @@ -370,9 +370,8 @@ void Input_Init(void) { * @param huart: UART handle. * @retval None */ -#if defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || \ - defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || \ - defined(SERIAL_USART2_DMA) || defined(SERIAL_USART3_DMA) +#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || \ + defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) void UART_DisableRxErrors(UART_HandleTypeDef *huart) { /* Disable PE (Parity Error) interrupts */ @@ -830,7 +829,7 @@ void readCommand(void) { */ void usart2_rx_check(void) { - #if defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) + #if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) static uint32_t old_pos; uint32_t pos; pos = rx_buffer_L_len - __HAL_DMA_GET_COUNTER(huart2.hdmarx); // Calculate current position in buffer @@ -885,7 +884,7 @@ void usart2_rx_check(void) } #endif // SIDEBOARD_SERIAL_USART2 - #if defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(SERIAL_USART2_DMA) + #if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) old_pos = pos; // Update old position if (old_pos == rx_buffer_L_len) { // Check and manually update if we reached end of buffer old_pos = 0; @@ -900,7 +899,7 @@ void usart2_rx_check(void) */ void usart3_rx_check(void) { - #if defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) + #if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) static uint32_t old_pos; uint32_t pos; pos = rx_buffer_R_len - __HAL_DMA_GET_COUNTER(huart3.hdmarx); // Calculate current position in buffer @@ -955,7 +954,7 @@ void usart3_rx_check(void) } #endif // SIDEBOARD_SERIAL_USART3 - #if defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(SERIAL_USART3_DMA) + #if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) old_pos = pos; // Update old position if (old_pos == rx_buffer_R_len) { // Check and manually update if we reached end of buffer old_pos = 0; From 1adb7f398326f072e13de35ec69ad7fdb61181cf Mon Sep 17 00:00:00 2001 From: phail Date: Sun, 28 Jun 2020 17:45:29 +0200 Subject: [PATCH 09/16] recover consoleLog functionality, remove useless debug_out --- Inc/comms.h | 1 + Inc/config.h | 2 +- Inc/protocolfunctions.h | 2 -- Src/ascii_proto_funcs.c | 22 ---------------------- Src/comms.c | 31 ++++++++++++++++++++++++++++--- Src/protocolfunctions.c | 22 ---------------------- 6 files changed, 30 insertions(+), 50 deletions(-) diff --git a/Inc/comms.h b/Inc/comms.h index 9893719c9..5f0b4b2fa 100644 --- a/Inc/comms.h +++ b/Inc/comms.h @@ -26,6 +26,7 @@ void setScopeChannel(uint8_t ch, int16_t val); void consoleScope(void); +void consoleLog(char *message); #endif diff --git a/Inc/config.h b/Inc/config.h index 43c8671fe..fc467201c 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -32,7 +32,7 @@ #else #define DELAY_IN_MAIN_LOOP 5 // in ms. default 5. it is independent of all the timing critical stuff. do not touch if you do not know what you are doing. #endif -#define TIMEOUT 30 // number of wrong / missing input commands before emergency off +#define TIMEOUT 5 // number of wrong / missing input commands before emergency off #define A2BIT_CONV 50 // A to bit for current conversion on ADC. Example: 1 A = 50, 2 A = 100, etc // ADC conversion time definitions diff --git a/Inc/protocolfunctions.h b/Inc/protocolfunctions.h index 76e29088a..c0ef0e71d 100644 --- a/Inc/protocolfunctions.h +++ b/Inc/protocolfunctions.h @@ -6,5 +6,3 @@ int setup_protocol(PROTOCOL_STAT *s); extern PROTOCOL_STAT sUSART2; extern PROTOCOL_STAT sUSART3; - -void consoleLog(char *message); \ No newline at end of file diff --git a/Src/ascii_proto_funcs.c b/Src/ascii_proto_funcs.c index ccfde7875..9ed063149 100644 --- a/Src/ascii_proto_funcs.c +++ b/Src/ascii_proto_funcs.c @@ -50,7 +50,6 @@ extern uint8_t enable; // global variable for motor enable -extern uint8_t debug_out; extern uint8_t buzzerFreq; // global variable for the buzzer pitch. can be 1, 2, 3, 4, 5, 6, 7... extern uint8_t buzzerPattern; // global variable for the buzzer pattern. can be 1, 2, 3, 4, 5, 6, 7... extern uint8_t enablescope; // enable scope on values @@ -183,26 +182,6 @@ int line_main_timing_stats(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { } -int line_debug_control(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { -//case 'E': - if (strlen(cmd) == 1){ - debug_out = 0; - enablescope = 0; - } else { - if ((cmd[1] | 0x20) == 's'){ - enablescope = 1; - debug_out = 1; - } - if ((cmd[1] | 0x20) == 'c'){ - debug_out = 1; - } - } - sprintf(ascii_out, "debug_out now %d\r\nenablescope now %d\r\n", debug_out, enablescope); - return 1; -} - - - // NOTE: needs params int line_generic_var(PROTOCOL_STAT *s, char *cmd, char *ascii_out) { //case 'f': // setting any parameter marked with uistr @@ -508,7 +487,6 @@ int main_ascii_init(PROTOCOL_STAT *s){ ascii_add_line_fn( 'A', line_set_alarm, "set alarm"); ascii_add_line_fn( 'C', line_electrical, "show electrical measurements"); ascii_add_line_fn( 'S', line_main_timing_stats, "show main loop timing stats"); - ascii_add_line_fn( 'E', line_debug_control, "dEbug control, E->off, Ec->console on, Es->console+scope"); ascii_add_line_fn( 'R', line_reset_firmware, " - R! -> Reset Firmware"); ascii_add_line_fn( 'T', line_test_message, "tt - send a test protocol message "); diff --git a/Src/comms.c b/Src/comms.c index e8eae7d4e..aeb2bdc5f 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -5,6 +5,7 @@ #include "setup.h" #include "config.h" #include "comms.h" +#include "protocolfunctions.h" extern UART_HandleTypeDef huart2; extern UART_HandleTypeDef huart3; @@ -12,11 +13,8 @@ extern UART_HandleTypeDef huart3; static volatile uint8_t uart_buf[100]; static volatile int16_t ch_buf[8]; -uint8_t debug_out = 1; uint8_t enablescope = 1; -//volatile char char_buf[300]; - void setScopeChannel(uint8_t ch, int16_t val) { ch_buf[ch] = val; } @@ -71,6 +69,33 @@ void consoleScope(void) { } +void consoleLog(char *message) +{ + #if defined DEBUG_SERIAL_ASCII && (defined DEBUG_SERIAL_USART2 || defined DEBUG_SERIAL_USART3) + #ifdef DEBUG_SERIAL_USART2 + if(__HAL_DMA_GET_COUNTER(huart2.hdmatx) == 0) { + HAL_UART_Transmit_DMA(&huart2, (uint8_t *)message, strlen((char *)(uintptr_t)message)); + } + #endif + #ifdef DEBUG_SERIAL_USART3 + if(__HAL_DMA_GET_COUNTER(huart3.hdmatx) == 0) { + HAL_UART_Transmit_DMA(&huart3, (uint8_t *)message, strlen((char *)(uintptr_t)message)); + } + #endif + #endif + + #if defined(VARIANT_BIPROPELLANT) + #if defined(SERIAL_USART2_DMA) + protocol_send_text(&sUSART2, message, PROTOCOL_SOM_NOACK); + #endif + + #if defined(SERIAL_USART3_DMA) + protocol_send_text(&sUSART3, message, PROTOCOL_SOM_NOACK); + #endif + #endif +} + + #if defined(SERIAL_USART2_DMA) && VARIANT_BIPROPELLANT int USART2_DMA_send(unsigned char *data, int len) { HAL_UART_Transmit_DMA(&huart2, (uint8_t *)data, len); diff --git a/Src/protocolfunctions.c b/Src/protocolfunctions.c index 1f3579fe1..04fba7c16 100644 --- a/Src/protocolfunctions.c +++ b/Src/protocolfunctions.c @@ -86,14 +86,6 @@ void fn_SpeedData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTO extern uint8_t disablepoweroff; - -//////////////////////////////////////////////////////////////////////////////////////////// -// Variable & Functions for 0x0B debug_out - -extern uint8_t debug_out; - - - //////////////////////////////////////////////////////////////////////////////////////////// // Variable & Functions for 0x0D PWMData and 0x0E PWMData.pwm @@ -239,9 +231,6 @@ int setup_protocol(PROTOCOL_STAT *s) { // errors += setParamVariable( s, 0x0A, UI_CHAR, &disablepoweroff, sizeof(disablepoweroff) ); - errors += setParamVariable( s, 0x0B, UI_CHAR, &debug_out, sizeof(debug_out) ); - - errors += setParamVariable( s, 0x0D, UI_NONE, &PWMData, sizeof(PWMData) ); setParamHandler( s, 0x0D, fn_PWMData ); @@ -254,14 +243,3 @@ int setup_protocol(PROTOCOL_STAT *s) { return errors; } - - -void consoleLog(char *message) { - #if defined(SERIAL_USART2_DMA) - if (debug_out) protocol_send_text(&sUSART2, message, PROTOCOL_SOM_NOACK); - #endif - - #if defined(SERIAL_USART3_DMA) - if (debug_out) protocol_send_text(&sUSART3, message, PROTOCOL_SOM_NOACK); - #endif -} From 943b3314c95b6eb5fc52d67878e10f42a412859a Mon Sep 17 00:00:00 2001 From: phail Date: Mon, 29 Jun 2020 00:38:48 +0200 Subject: [PATCH 10/16] rename bipropellant files --- .gitmodules | 4 ++-- ...ctions.h => bipropellantProtocolMachine.h} | 0 ...ctures.h => bipropellantProtocolStructs.h} | 0 Makefile | 10 +++++----- Src/{hbprotocol => bipropellantProtocol} | 0 ...to_funcs.c => bipropellantProtocolASCII.c} | 4 ++-- ...ctions.c => bipropellantProtocolMachine.c} | 4 ++-- Src/comms.c | 2 +- Src/main.c | 4 ++-- Src/util.c | 2 +- platformio.ini | 20 +++++++++---------- 11 files changed, 25 insertions(+), 25 deletions(-) rename Inc/{protocolfunctions.h => bipropellantProtocolMachine.h} (100%) rename Inc/{control_structures.h => bipropellantProtocolStructs.h} (100%) rename Src/{hbprotocol => bipropellantProtocol} (100%) rename Src/{ascii_proto_funcs.c => bipropellantProtocolASCII.c} (99%) rename Src/{protocolfunctions.c => bipropellantProtocolMachine.c} (98%) diff --git a/.gitmodules b/.gitmodules index 64933deb2..8a6de826a 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ -[submodule "Src/hbprotocol"] - path = Src/hbprotocol +[submodule "Src/bipropellantProtocol"] + path = Src/bipropellantProtocol url = https://github.com/bipropellant/hbprotocol.git diff --git a/Inc/protocolfunctions.h b/Inc/bipropellantProtocolMachine.h similarity index 100% rename from Inc/protocolfunctions.h rename to Inc/bipropellantProtocolMachine.h diff --git a/Inc/control_structures.h b/Inc/bipropellantProtocolStructs.h similarity index 100% rename from Inc/control_structures.h rename to Inc/bipropellantProtocolStructs.h diff --git a/Makefile b/Makefile index 6dea00558..4152dfa1a 100644 --- a/Makefile +++ b/Makefile @@ -46,10 +46,10 @@ Src/hd44780.c \ Src/pcf8574.c \ Src/comms.c \ Src/stm32f1xx_it.c \ -Src/hbprotocol/protocol.c \ -Src/hbprotocol/machine_protocol.c \ -Src/hbprotocol/ascii_protocol.c \ -Src/hbprotocol/cobsr.c \ +Src/bipropellantProtocol/protocol.c \ +Src/bipropellantProtocol/machine_protocol.c \ +Src/bipropellantProtocol/ascii_protocol.c \ +Src/bipropellantProtocol/cobsr.c \ Src/ascii_proto_funcs.c \ Src/protocolfunctions.c \ Src/BLDC_controller_data.c \ @@ -103,7 +103,7 @@ AS_INCLUDES = # C includes C_INCLUDES = \ -IInc \ --ISrc/hbprotocol \ +-ISrc/bipropellantProtocol \ -IDrivers/STM32F1xx_HAL_Driver/Inc \ -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \ -IDrivers/CMSIS/Device/ST/STM32F1xx/Include \ diff --git a/Src/hbprotocol b/Src/bipropellantProtocol similarity index 100% rename from Src/hbprotocol rename to Src/bipropellantProtocol diff --git a/Src/ascii_proto_funcs.c b/Src/bipropellantProtocolASCII.c similarity index 99% rename from Src/ascii_proto_funcs.c rename to Src/bipropellantProtocolASCII.c index 9ed063149..02d2aeebc 100644 --- a/Src/ascii_proto_funcs.c +++ b/Src/bipropellantProtocolASCII.c @@ -22,14 +22,14 @@ #include "config.h" -#include "protocolfunctions.h" +#include "bipropellantProtocolMachine.h" #include "protocol.h" #include "comms.h" #include #include #include -#include "control_structures.h" +#include "bipropellantProtocolStructs.h" ////////////////////////////////////////////////////////// diff --git a/Src/protocolfunctions.c b/Src/bipropellantProtocolMachine.c similarity index 98% rename from Src/protocolfunctions.c rename to Src/bipropellantProtocolMachine.c index 04fba7c16..de158f504 100644 --- a/Src/protocolfunctions.c +++ b/Src/bipropellantProtocolMachine.c @@ -1,6 +1,6 @@ #include "defines.h" #include "config.h" -#include "protocolfunctions.h" +#include "bipropellantProtocolMachine.h" #include "comms.h" #include "util.h" @@ -10,7 +10,7 @@ #include #include -#include "control_structures.h" +#include "bipropellantProtocolStructs.h" #if defined(SERIAL_USART2_DMA) PROTOCOL_STAT sUSART2; diff --git a/Src/comms.c b/Src/comms.c index aeb2bdc5f..7c9c875a5 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -5,7 +5,7 @@ #include "setup.h" #include "config.h" #include "comms.h" -#include "protocolfunctions.h" +#include "bipropellantProtocolMachine.h" extern UART_HandleTypeDef huart2; extern UART_HandleTypeDef huart3; diff --git a/Src/main.c b/Src/main.c index 941cfcf39..438b068fd 100644 --- a/Src/main.c +++ b/Src/main.c @@ -29,8 +29,8 @@ #include "comms.h" #include "BLDC_controller.h" /* BLDC's header file */ #include "rtwtypes.h" -#include "protocolfunctions.h" -#include "control_structures.h" +#include "bipropellantProtocolMachine.h" +#include "bipropellantProtocolStructs.h" #if defined(DEBUG_I2C_LCD) || defined(SUPPORT_LCD) #include "hd44780.h" diff --git a/Src/util.c b/Src/util.c index 05d324c9d..22c668dea 100644 --- a/Src/util.c +++ b/Src/util.c @@ -29,7 +29,7 @@ #include "util.h" #include "BLDC_controller.h" #include "rtwtypes.h" -#include "protocolfunctions.h" +#include "bipropellantProtocolMachine.h" #if defined(DEBUG_I2C_LCD) || defined(SUPPORT_LCD) #include "hd44780.h" diff --git a/platformio.ini b/platformio.ini index f0bcd2554..ba3f0318a 100644 --- a/platformio.ini +++ b/platformio.ini @@ -35,7 +35,7 @@ monitor_port = COM5 monitor_speed = 38400 build_flags = - -I${PROJECT_DIR}/Src/hbprotocol/ + -I${PROJECT_DIR}/Src/bipropellantProtocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -59,7 +59,7 @@ monitor_port = COM5 monitor_speed = 38400 build_flags = - -I${PROJECT_DIR}/Src/hbprotocol/ + -I${PROJECT_DIR}/Src/bipropellantProtocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -79,7 +79,7 @@ debug_tool = stlink upload_protocol = stlink build_flags = - -I${PROJECT_DIR}/Src/hbprotocol/ + -I${PROJECT_DIR}/Src/bipropellantProtocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -99,7 +99,7 @@ debug_tool = stlink upload_protocol = stlink build_flags = - -I${PROJECT_DIR}/Src/hbprotocol/ + -I${PROJECT_DIR}/Src/bipropellantProtocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -119,7 +119,7 @@ debug_tool = stlink upload_protocol = stlink build_flags = - -I${PROJECT_DIR}/Src/hbprotocol/ + -I${PROJECT_DIR}/Src/bipropellantProtocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -139,7 +139,7 @@ debug_tool = stlink upload_protocol = stlink build_flags = - -I${PROJECT_DIR}/Src/hbprotocol/ + -I${PROJECT_DIR}/Src/bipropellantProtocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -163,7 +163,7 @@ monitor_port = COM5 monitor_speed = 38400 build_flags = - -I${PROJECT_DIR}/Src/hbprotocol/ + -I${PROJECT_DIR}/Src/bipropellantProtocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -187,7 +187,7 @@ monitor_port = COM5 monitor_speed = 38400 build_flags = - -I${PROJECT_DIR}/Src/hbprotocol/ + -I${PROJECT_DIR}/Src/bipropellantProtocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -207,7 +207,7 @@ debug_tool = stlink upload_protocol = stlink build_flags = - -I${PROJECT_DIR}/Src/hbprotocol/ + -I${PROJECT_DIR}/Src/bipropellantProtocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld @@ -231,7 +231,7 @@ monitor_port = /dev/ttyUSB0 monitor_speed = 115200 build_flags = - -I${PROJECT_DIR}/Src/hbprotocol/ + -I${PROJECT_DIR}/Src/bipropellantProtocol/ -DUSE_HAL_DRIVER -DSTM32F103xE -Wl,-T./STM32F103RCTx_FLASH.ld From dd00b807794e299f08292eb47516ee1f426e76c8 Mon Sep 17 00:00:00 2001 From: phail Date: Mon, 29 Jun 2020 01:03:54 +0200 Subject: [PATCH 11/16] avoid pragma once because Keil doesn't like it --- Inc/bipropellantProtocolMachine.h | 6 +++++- Inc/bipropellantProtocolStructs.h | 10 ++++++++-- Src/bipropellantProtocol | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Inc/bipropellantProtocolMachine.h b/Inc/bipropellantProtocolMachine.h index c0ef0e71d..aa8bda3ac 100644 --- a/Inc/bipropellantProtocolMachine.h +++ b/Inc/bipropellantProtocolMachine.h @@ -1,4 +1,6 @@ -#pragma once +// Define to prevent recursive inclusion +#ifndef BIPROPELLANTPROTOCOLMACHINE_H +#define BIPROPELLANTPROTOCOLMACHINE_H #include "protocol.h" @@ -6,3 +8,5 @@ int setup_protocol(PROTOCOL_STAT *s); extern PROTOCOL_STAT sUSART2; extern PROTOCOL_STAT sUSART3; + +#endif \ No newline at end of file diff --git a/Inc/bipropellantProtocolStructs.h b/Inc/bipropellantProtocolStructs.h index 70d83d982..39e8e97ba 100644 --- a/Inc/bipropellantProtocolStructs.h +++ b/Inc/bipropellantProtocolStructs.h @@ -20,7 +20,11 @@ //////////////////////////////////////////////////////////////////// // structures used in main repo which may mirror protocol structures //////////////////////////////////////////////////////////////////// -#pragma once + +// Define to prevent recursive inclusion +#ifndef BIPROPELLANTPROTOCOLSTRUCTS_H +#define BIPROPELLANTPROTOCOLSTRUCTS_H + #include "protocol.h" //// control structures used in firmware @@ -85,4 +89,6 @@ typedef struct tag_ELECTRICAL_PARAMS{ MOTOR_ELECTRICAL motors[2]; } ELECTRICAL_PARAMS; -#pragma pack(pop) \ No newline at end of file +#pragma pack(pop) + +#endif \ No newline at end of file diff --git a/Src/bipropellantProtocol b/Src/bipropellantProtocol index 9459e90a7..fa5e754b9 160000 --- a/Src/bipropellantProtocol +++ b/Src/bipropellantProtocol @@ -1 +1 @@ -Subproject commit 9459e90a7831ed41d2671b5b5363590741ff5dde +Subproject commit fa5e754b9ac0cb160c94e9cb3fc0f688d597f40a From 8f730835051f7a9cff6ad9505abcadc21e4b83ee Mon Sep 17 00:00:00 2001 From: phail Date: Mon, 29 Jun 2020 02:10:53 +0200 Subject: [PATCH 12/16] clean up ifdef, fix makefile --- Inc/config.h | 12 +- Inc/util.h | 9 - Makefile | 4 +- Src/bipropellantProtocolMachine.c | 8 +- Src/comms.c | 8 +- Src/main.c | 31 ++-- Src/setup.c | 7 +- Src/stm32f1xx_it.c | 8 +- Src/util.c | 287 +++++++++++++++++------------- 9 files changed, 199 insertions(+), 175 deletions(-) diff --git a/Inc/config.h b/Inc/config.h index fc467201c..e8df2d489 100644 --- a/Inc/config.h +++ b/Inc/config.h @@ -255,8 +255,8 @@ // ############################ VARIANT_BIPROPELLANT SETTINGS ############################ #ifdef VARIANT_BIPROPELLANT -// #define SERIAL_USART2_DMA - #define SERIAL_USART3_DMA +// #define USART2_ENABLE + #define USART3_ENABLE #define USART2_BAUD 115200 #define USART3_BAUD 115200 #undef TIMEOUT @@ -445,11 +445,11 @@ #define USART3_BAUD 38400 // UART3 baud rate (short wired cable) #define USART3_WORDLENGTH UART_WORDLENGTH_8B // UART_WORDLENGTH_8B or UART_WORDLENGTH_9B #endif -#if defined(DEBUG_SERIAL_USART2) - #define SERIAL_USART2_DMA +#if defined(DEBUG_SERIAL_USART2) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) + #define USART2_ENABLE #endif -#if defined(DEBUG_SERIAL_USART3) - #define SERIAL_USART3_DMA +#if defined(DEBUG_SERIAL_USART3) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) + #define USART3_ENABLE #endif // ########################### UART SETIINGS ############################ diff --git a/Inc/util.h b/Inc/util.h index 882d57d7b..39338dbb8 100644 --- a/Inc/util.h +++ b/Inc/util.h @@ -79,15 +79,6 @@ void poweroffPressCheck(void); void readCommand(void); void usart2_rx_check(void); void usart3_rx_check(void); -#if defined(SERIAL_USART2_DMA) || defined(SERIAL_USART3_DMA) -void usart_process_debug(uint8_t *userCommand, uint32_t len); -#endif -#if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3) -void usart_process_command(SerialCommand *command_in, SerialCommand *command_out, uint8_t usart_idx); -#endif -#if defined(SIDEBOARD_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART3) -void usart_process_sideboard(SerialSideboard *Sideboard_in, SerialSideboard *Sideboard_out, uint8_t usart_idx); -#endif // Sideboard functions void sideboardLeds(uint8_t *leds); diff --git a/Makefile b/Makefile index 4152dfa1a..b3a4357a9 100644 --- a/Makefile +++ b/Makefile @@ -50,8 +50,8 @@ Src/bipropellantProtocol/protocol.c \ Src/bipropellantProtocol/machine_protocol.c \ Src/bipropellantProtocol/ascii_protocol.c \ Src/bipropellantProtocol/cobsr.c \ -Src/ascii_proto_funcs.c \ -Src/protocolfunctions.c \ +Src/bipropellantProtocolASCII.c \ +Src/bipropellantProtocolASCII.c \ Src/BLDC_controller_data.c \ Src/BLDC_controller.c diff --git a/Src/bipropellantProtocolMachine.c b/Src/bipropellantProtocolMachine.c index de158f504..9e88bc254 100644 --- a/Src/bipropellantProtocolMachine.c +++ b/Src/bipropellantProtocolMachine.c @@ -12,10 +12,10 @@ #include "bipropellantProtocolStructs.h" -#if defined(SERIAL_USART2_DMA) +#if defined(USART2_ENABLE) PROTOCOL_STAT sUSART2; #endif -#if defined(SERIAL_USART3_DMA) +#if defined(USART3_ENABLE) PROTOCOL_STAT sUSART3; #endif @@ -186,7 +186,7 @@ int setup_protocol(PROTOCOL_STAT *s) { int errors = 0; - #if defined(SERIAL_USART2_DMA) + #if defined(USART2_ENABLE) extern int USART2_DMA_send(unsigned char *data, int len); @@ -200,7 +200,7 @@ int setup_protocol(PROTOCOL_STAT *s) { #endif - #if defined(SERIAL_USART3_DMA) + #if defined(USART3_ENABLE) extern int USART3_DMA_send(unsigned char *data, int len); diff --git a/Src/comms.c b/Src/comms.c index 7c9c875a5..155be2bf1 100644 --- a/Src/comms.c +++ b/Src/comms.c @@ -85,25 +85,25 @@ void consoleLog(char *message) #endif #if defined(VARIANT_BIPROPELLANT) - #if defined(SERIAL_USART2_DMA) + #if defined(USART2_ENABLE) protocol_send_text(&sUSART2, message, PROTOCOL_SOM_NOACK); #endif - #if defined(SERIAL_USART3_DMA) + #if defined(USART3_ENABLE) protocol_send_text(&sUSART3, message, PROTOCOL_SOM_NOACK); #endif #endif } -#if defined(SERIAL_USART2_DMA) && VARIANT_BIPROPELLANT +#if defined(USART2_ENABLE) int USART2_DMA_send(unsigned char *data, int len) { HAL_UART_Transmit_DMA(&huart2, (uint8_t *)data, len); return 1; } #endif -#if defined(SERIAL_USART3_DMA) && VARIANT_BIPROPELLANT +#if defined(USART3_ENABLE) int USART3_DMA_send(unsigned char *data, int len) { HAL_UART_Transmit_DMA(&huart3, (uint8_t *)data, len); return 1; diff --git a/Src/main.c b/Src/main.c index 438b068fd..d8427795f 100644 --- a/Src/main.c +++ b/Src/main.c @@ -205,23 +205,24 @@ int main(void) { readCommand(); // Read Command: cmd1, cmd2 calcAvgSpeed(); // Calculate average measured speed: speedAvg, speedAvgAbs - #if defined(VARIANT_BIPROPELLANT) && defined(SERIAL_USART2_DMA) - protocol_tick( &sUSART2 ); - if(sUSART2.ascii.enable_immediate) - { - timeout = 0; - } - cmd2 = (PWMData.pwm[0] + PWMData.pwm[1]) /2; // Speed - cmd1 = PWMData.pwm[0] - cmd2; // Steer - #endif - #if defined(VARIANT_BIPROPELLANT) && defined(SERIAL_USART3_DMA) - protocol_tick( &sUSART3 ); - if(sUSART3.ascii.enable_immediate) - { - timeout = 0; - } + #if defined(VARIANT_BIPROPELLANT) + #if defined(USART2_ENABLE) + protocol_tick( &sUSART2 ); + if(sUSART2.ascii.enable_immediate) + { + timeout = 0; + } + #endif + #if defined(USART3_ENABLE) + protocol_tick( &sUSART3 ); + if(sUSART3.ascii.enable_immediate) + { + timeout = 0; + } + #endif cmd2 = (PWMData.pwm[0] + PWMData.pwm[1]) /2; // Speed cmd1 = PWMData.pwm[0] - cmd2; // Steer + #endif #ifndef VARIANT_TRANSPOTTER diff --git a/Src/setup.c b/Src/setup.c index 4ab89cb70..3d7033617 100644 --- a/Src/setup.c +++ b/Src/setup.c @@ -54,7 +54,7 @@ DMA_HandleTypeDef hdma_usart3_tx; volatile adc_buf_t adc_buffer; -#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) +#if defined(USART2_ENABLE) /* USART2 init function */ void UART2_Init(void) { @@ -80,7 +80,7 @@ volatile adc_buf_t adc_buffer; } #endif -#if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(USART3_ENABLE) || defined(FEEDBACK_SERIAL_USART3) /* USART3 init function */ void UART3_Init(void) { @@ -107,8 +107,7 @@ void UART3_Init(void) } #endif -#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || \ - defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(USART2_ENABLE) || defined(USART3_ENABLE) void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; diff --git a/Src/stm32f1xx_it.c b/Src/stm32f1xx_it.c index f7f358f76..eb4936b41 100644 --- a/Src/stm32f1xx_it.c +++ b/Src/stm32f1xx_it.c @@ -276,7 +276,7 @@ void EXTI15_10_IRQHandler(void) } #endif -#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) +#if defined(USART2_ENABLE) void DMA1_Channel6_IRQHandler(void) { /* USER CODE BEGIN DMA1_Channel4_IRQn 0 */ @@ -303,7 +303,7 @@ void DMA1_Channel7_IRQHandler(void) } #endif -#if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(USART3_ENABLE) || defined(FEEDBACK_SERIAL_USART3) /** * @brief This function handles DMA1 channel2 global interrupt. */ @@ -333,7 +333,7 @@ void DMA1_Channel3_IRQHandler(void) } #endif -#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) +#if defined(USART2_ENABLE) /** * @brief This function handles USART2 global interrupt. */ @@ -352,7 +352,7 @@ void USART2_IRQHandler(void) } #endif -#if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(USART3_ENABLE) || defined(FEEDBACK_SERIAL_USART3) /** * @brief This function handles USART3 global interrupt. */ diff --git a/Src/util.c b/Src/util.c index 22c668dea..56d33ae8f 100644 --- a/Src/util.c +++ b/Src/util.c @@ -154,7 +154,7 @@ static int16_t INPUT_MIN; // [-] Input target minimum limitation static int16_t timeoutCntADC = 0; // Timeout counter for ADC Protection #endif -#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) +#if defined(USART2_ENABLE) static uint8_t rx_buffer_L[SERIAL_BUFFER_SIZE]; // USART Rx DMA circular buffer static uint32_t rx_buffer_L_len = ARRAY_LEN(rx_buffer_L); #endif @@ -168,7 +168,7 @@ SerialSideboard Sideboard_L_raw; static uint32_t Sideboard_L_len = sizeof(Sideboard_L); #endif -#if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(USART3_ENABLE) static uint8_t rx_buffer_R[SERIAL_BUFFER_SIZE]; // USART Rx DMA circular buffer static uint32_t rx_buffer_R_len = ARRAY_LEN(rx_buffer_R); #endif @@ -267,24 +267,22 @@ void Input_Init(void) { Nunchuk_Init(); #endif - #if defined(SERIAL_USART2_DMA) && defined(VARIANT_BIPROPELLANT) - setup_protocol(&sUSART2); - #endif - #if defined(SERIAL_USART3_DMA) && defined(VARIANT_BIPROPELLANT) - setup_protocol(&sUSART3); + #if defined(VARIANT_BIPROPELLANT) + #if defined(USART2_ENABLE) + setup_protocol(&sUSART2); + #endif + #if defined(USART3_ENABLE) + setup_protocol(&sUSART3); + #endif #endif - #if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(FEEDBACK_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) + #if defined(USART2_ENABLE) UART2_Init(); - #endif - #if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(FEEDBACK_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) - UART3_Init(); - #endif - #if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) HAL_UART_Receive_DMA(&huart2, (uint8_t *)rx_buffer_L, sizeof(rx_buffer_L)); UART_DisableRxErrors(&huart2); #endif - #if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) + #if defined(USART3_ENABLE) + UART3_Init(); HAL_UART_Receive_DMA(&huart3, (uint8_t *)rx_buffer_R, sizeof(rx_buffer_R)); UART_DisableRxErrors(&huart3); #endif @@ -370,8 +368,7 @@ void Input_Init(void) { * @param huart: UART handle. * @retval None */ -#if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) || \ - defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(USART2_ENABLE) || defined(USART3_ENABLE) void UART_DisableRxErrors(UART_HandleTypeDef *huart) { /* Disable PE (Parity Error) interrupts */ @@ -382,6 +379,121 @@ void UART_DisableRxErrors(UART_HandleTypeDef *huart) } #endif +/* ====================== Private (static) Functions ======================= */ + +#if defined(VARIANT_BIPROPELLANT) + #if defined(USART2_ENABLE) + static void usart_process_bipropellantProtocolUSART2(uint8_t *data, uint32_t len) + { + for (; len > 0; len--, data++) { + protocol_byte( &sUSART2, (unsigned char) *data ); + } + } + #endif + + #if defined(USART3_ENABLE) + static void usart_process_bipropellantProtocolUSART3(uint8_t *data, uint32_t len) + { + for (; len > 0; len--, data++) { + protocol_byte( &sUSART3, (unsigned char) *data ); + } + } + #endif +#endif //VARIANT_BIPROPELLANT + +/* + * Process Rx debug user command input + */ +#if defined(DEBUG_SERIAL_USART2) || defined(DEBUG_SERIAL_USART3) +static void usart_process_debug(uint8_t *userCommand, uint32_t len) +{ + for (; len > 0; len--, userCommand++) { + if (*userCommand != '\n' && *userCommand != '\r') { // Do not accept 'new line' and 'carriage return' commands + consoleLog("-- Command received --\r\n"); + // handle_input(*userCommand); // -> Create this function to handle the user commands + } + } +} +#endif + +/* + * Process command Rx data + * - if the command_in data is valid (correct START_FRAME and checksum) copy the command_in to command_out + */ +#if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3) +static void usart_process_command(SerialCommand *command_in, SerialCommand *command_out, uint8_t usart_idx) +{ + #ifdef CONTROL_IBUS + if (command_in->start == IBUS_LENGTH && command_in->type == IBUS_COMMAND) { + ibus_chksum = 0xFFFF - IBUS_LENGTH - IBUS_COMMAND; + for (uint8_t i = 0; i < (IBUS_NUM_CHANNELS * 2); i++) { + ibus_chksum -= command_in->channels[i]; + } + if (ibus_chksum == (uint16_t)((command_in->checksumh << 8) + command_in->checksuml)) { + *command_out = *command_in; + if (usart_idx == 2) { // Sideboard USART2 + #ifdef CONTROL_SERIAL_USART2 + timeoutCntSerial_L = 0; // Reset timeout counter + timeoutFlagSerial_L = 0; // Clear timeout flag + #endif + } else if (usart_idx == 3) { // Sideboard USART3 + #ifdef CONTROL_SERIAL_USART3 + timeoutCntSerial_R = 0; // Reset timeout counter + timeoutFlagSerial_R = 0; // Clear timeout flag + #endif + } + } + } + #else + uint16_t checksum; + if (command_in->start == SERIAL_START_FRAME) { + checksum = (uint16_t)(command_in->start ^ command_in->steer ^ command_in->speed); + if (command_in->checksum == checksum) { + *command_out = *command_in; + if (usart_idx == 2) { // Sideboard USART2 + #ifdef CONTROL_SERIAL_USART2 + timeoutCntSerial_L = 0; // Reset timeout counter + timeoutFlagSerial_L = 0; // Clear timeout flag + #endif + } else if (usart_idx == 3) { // Sideboard USART3 + #ifdef CONTROL_SERIAL_USART3 + timeoutCntSerial_R = 0; // Reset timeout counter + timeoutFlagSerial_R = 0; // Clear timeout flag + #endif + } + } + } + #endif +} +#endif + +/* + * Process Sideboard Rx data + * - if the Sideboard_in data is valid (correct START_FRAME and checksum) copy the Sideboard_in to Sideboard_out + */ +#if defined(SIDEBOARD_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART3) +static void usart_process_sideboard(SerialSideboard *Sideboard_in, SerialSideboard *Sideboard_out, uint8_t usart_idx) +{ + uint16_t checksum; + if (Sideboard_in->start == SERIAL_START_FRAME) { + checksum = (uint16_t)(Sideboard_in->start ^ Sideboard_in->roll ^ Sideboard_in->pitch ^ Sideboard_in->yaw ^ Sideboard_in->sensors); + if (Sideboard_in->checksum == checksum) { + *Sideboard_out = *Sideboard_in; + if (usart_idx == 2) { // Sideboard USART2 + #ifdef SIDEBOARD_SERIAL_USART2 + timeoutCntSerial_L = 0; // Reset timeout counter + timeoutFlagSerial_L = 0; // Clear timeout flag + #endif + } else if (usart_idx == 3) { // Sideboard USART3 + #ifdef SIDEBOARD_SERIAL_USART3 + timeoutCntSerial_R = 0; // Reset timeout counter + timeoutFlagSerial_R = 0; // Clear timeout flag + #endif + } + } + } +} +#endif /* =========================== General Functions =========================== */ @@ -829,13 +941,12 @@ void readCommand(void) { */ void usart2_rx_check(void) { - #if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) +#if defined(USART2_ENABLE) static uint32_t old_pos; uint32_t pos; pos = rx_buffer_L_len - __HAL_DMA_GET_COUNTER(huart2.hdmarx); // Calculate current position in buffer - #endif - #if defined(SERIAL_USART2_DMA) + #if defined(DEBUG_SERIAL_USART2) 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 @@ -848,6 +959,19 @@ void usart2_rx_check(void) } #endif + #if defined(VARIANT_BIPROPELLANT) + 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_bipropellantProtocolUSART2(&rx_buffer_L[old_pos], pos - old_pos); // Process data + } else { // "Overflow" buffer mode + usart_process_bipropellantProtocolUSART2(&rx_buffer_L[old_pos], rx_buffer_L_len - old_pos); // First Process data from the end of buffer + if (pos > 0) { // Check and continue with beginning of buffer + usart_process_bipropellantProtocolUSART2(&rx_buffer_L[0], pos); // Process remaining data + } + } + } + #endif + #ifdef CONTROL_SERIAL_USART2 uint8_t *ptr; if (pos != old_pos) { // Check change in received data @@ -884,12 +1008,11 @@ void usart2_rx_check(void) } #endif // SIDEBOARD_SERIAL_USART2 - #if defined(SERIAL_USART2_DMA) || defined(CONTROL_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART2) old_pos = pos; // Update old position if (old_pos == rx_buffer_L_len) { // Check and manually update if we reached end of buffer old_pos = 0; } - #endif +#endif // USART2_ENABLE } @@ -899,13 +1022,12 @@ void usart2_rx_check(void) */ void usart3_rx_check(void) { - #if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) +#if defined(USART3_ENABLE) static uint32_t old_pos; uint32_t pos; pos = rx_buffer_R_len - __HAL_DMA_GET_COUNTER(huart3.hdmarx); // Calculate current position in buffer - #endif - #if defined(SERIAL_USART3_DMA) + #if defined(DEBUG_SERIAL_USART3) 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 @@ -918,6 +1040,19 @@ void usart3_rx_check(void) } #endif + #if defined(VARIANT_BIPROPELLANT) + 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_bipropellantProtocolUSART3(&rx_buffer_R[old_pos], pos - old_pos); // Process data + } else { // "Overflow" buffer mode + usart_process_bipropellantProtocolUSART3(&rx_buffer_R[old_pos], rx_buffer_R_len - old_pos); // First Process data from the end of buffer + if (pos > 0) { // Check and continue with beginning of buffer + usart_process_bipropellantProtocolUSART3(&rx_buffer_R[0], pos); // Process remaining data + } + } + } + #endif + #ifdef CONTROL_SERIAL_USART3 uint8_t *ptr; if (pos != old_pos) { // Check change in received data @@ -954,115 +1089,13 @@ void usart3_rx_check(void) } #endif // SIDEBOARD_SERIAL_USART3 - #if defined(SERIAL_USART3_DMA) || defined(CONTROL_SERIAL_USART3) || defined(SIDEBOARD_SERIAL_USART3) old_pos = pos; // Update old position if (old_pos == rx_buffer_R_len) { // Check and manually update if we reached end of buffer old_pos = 0; } - #endif +#endif // USART3_ENABLE } -/* - * Process Rx debug user command input - */ -#if defined(SERIAL_USART2_DMA) || defined(SERIAL_USART3_DMA) -void usart_process_debug(uint8_t *userCommand, uint32_t len) -{ - for (; len > 0; len--, userCommand++) { - #if defined(VARIANT_BIPROPELLANT) && defined(SERIAL_USART2_DMA) - protocol_byte( &sUSART2, (unsigned char) *userCommand ); - #elif defined(VARIANT_BIPROPELLANT) && defined(SERIAL_USART3_DMA) - protocol_byte( &sUSART3, (unsigned char) *userCommand ); - #else - if (*userCommand != '\n' && *userCommand != '\r') { // Do not accept 'new line' and 'carriage return' commands - consoleLog("-- Command received --\r\n"); - // handle_input(*userCommand); // -> Create this function to handle the user commands - } - #endif - } -} -#endif // SERIAL_DEBUG - -/* - * Process command Rx data - * - if the command_in data is valid (correct START_FRAME and checksum) copy the command_in to command_out - */ -#if defined(CONTROL_SERIAL_USART2) || defined(CONTROL_SERIAL_USART3) -void usart_process_command(SerialCommand *command_in, SerialCommand *command_out, uint8_t usart_idx) -{ - #ifdef CONTROL_IBUS - if (command_in->start == IBUS_LENGTH && command_in->type == IBUS_COMMAND) { - ibus_chksum = 0xFFFF - IBUS_LENGTH - IBUS_COMMAND; - for (uint8_t i = 0; i < (IBUS_NUM_CHANNELS * 2); i++) { - ibus_chksum -= command_in->channels[i]; - } - if (ibus_chksum == (uint16_t)((command_in->checksumh << 8) + command_in->checksuml)) { - *command_out = *command_in; - if (usart_idx == 2) { // Sideboard USART2 - #ifdef CONTROL_SERIAL_USART2 - timeoutCntSerial_L = 0; // Reset timeout counter - timeoutFlagSerial_L = 0; // Clear timeout flag - #endif - } else if (usart_idx == 3) { // Sideboard USART3 - #ifdef CONTROL_SERIAL_USART3 - timeoutCntSerial_R = 0; // Reset timeout counter - timeoutFlagSerial_R = 0; // Clear timeout flag - #endif - } - } - } - #else - uint16_t checksum; - if (command_in->start == SERIAL_START_FRAME) { - checksum = (uint16_t)(command_in->start ^ command_in->steer ^ command_in->speed); - if (command_in->checksum == checksum) { - *command_out = *command_in; - if (usart_idx == 2) { // Sideboard USART2 - #ifdef CONTROL_SERIAL_USART2 - timeoutCntSerial_L = 0; // Reset timeout counter - timeoutFlagSerial_L = 0; // Clear timeout flag - #endif - } else if (usart_idx == 3) { // Sideboard USART3 - #ifdef CONTROL_SERIAL_USART3 - timeoutCntSerial_R = 0; // Reset timeout counter - timeoutFlagSerial_R = 0; // Clear timeout flag - #endif - } - } - } - #endif -} -#endif - -/* - * Process Sideboard Rx data - * - if the Sideboard_in data is valid (correct START_FRAME and checksum) copy the Sideboard_in to Sideboard_out - */ -#if defined(SIDEBOARD_SERIAL_USART2) || defined(SIDEBOARD_SERIAL_USART3) -void usart_process_sideboard(SerialSideboard *Sideboard_in, SerialSideboard *Sideboard_out, uint8_t usart_idx) -{ - uint16_t checksum; - if (Sideboard_in->start == SERIAL_START_FRAME) { - checksum = (uint16_t)(Sideboard_in->start ^ Sideboard_in->roll ^ Sideboard_in->pitch ^ Sideboard_in->yaw ^ Sideboard_in->sensors); - if (Sideboard_in->checksum == checksum) { - *Sideboard_out = *Sideboard_in; - if (usart_idx == 2) { // Sideboard USART2 - #ifdef SIDEBOARD_SERIAL_USART2 - timeoutCntSerial_L = 0; // Reset timeout counter - timeoutFlagSerial_L = 0; // Clear timeout flag - #endif - } else if (usart_idx == 3) { // Sideboard USART3 - #ifdef SIDEBOARD_SERIAL_USART3 - timeoutCntSerial_R = 0; // Reset timeout counter - timeoutFlagSerial_R = 0; // Clear timeout flag - #endif - } - } - } -} -#endif - - /* =========================== Sideboard Functions =========================== */ /* From 4a1409ce7bff1800fc261861c64774ba0ac37e93 Mon Sep 17 00:00:00 2001 From: phail Date: Mon, 29 Jun 2020 02:16:40 +0200 Subject: [PATCH 13/16] fix Makefile.. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b3a4357a9..e34db835a 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ Src/bipropellantProtocol/machine_protocol.c \ Src/bipropellantProtocol/ascii_protocol.c \ Src/bipropellantProtocol/cobsr.c \ Src/bipropellantProtocolASCII.c \ -Src/bipropellantProtocolASCII.c \ +Src/bipropellantProtocolMachine.c \ Src/BLDC_controller_data.c \ Src/BLDC_controller.c From c1d53b87a3685bfee58dc8e19135e642cf7ee462 Mon Sep 17 00:00:00 2001 From: phail Date: Mon, 29 Jun 2020 15:51:24 +0200 Subject: [PATCH 14/16] refactor enable motors variable --- Inc/bldc.h | 9 +++++++++ Src/bipropellantProtocolASCII.c | 23 +++++++++++++---------- Src/bipropellantProtocolMachine.c | 18 +++++++----------- Src/bldc.c | 19 +++++++++++++++---- Src/main.c | 21 ++++++++++----------- Src/util.c | 17 ++++++++--------- 6 files changed, 62 insertions(+), 45 deletions(-) create mode 100644 Inc/bldc.h diff --git a/Inc/bldc.h b/Inc/bldc.h new file mode 100644 index 000000000..f0ee582ec --- /dev/null +++ b/Inc/bldc.h @@ -0,0 +1,9 @@ +#ifndef BLDC_H +#define BLDC_H + +#include + +uint8_t bldc_getMotorsEnable(void); +void bldc_setMotorsEnable(uint8_t enable); + +#endif \ No newline at end of file diff --git a/Src/bipropellantProtocolASCII.c b/Src/bipropellantProtocolASCII.c index 02d2aeebc..336ac58b8 100644 --- a/Src/bipropellantProtocolASCII.c +++ b/Src/bipropellantProtocolASCII.c @@ -20,7 +20,7 @@ #include "stm32f1xx_hal.h" #include "defines.h" #include "config.h" - +#include "bldc.h" #include "bipropellantProtocolMachine.h" #include "protocol.h" @@ -47,9 +47,6 @@ // extern variables you want to read/write here // from main.c -extern uint8_t enable; // global variable for motor enable - - extern uint8_t buzzerFreq; // global variable for the buzzer pitch. can be 1, 2, 3, 4, 5, 6, 7... extern uint8_t buzzerPattern; // global variable for the buzzer pattern. can be 1, 2, 3, 4, 5, 6, 7... extern uint8_t enablescope; // enable scope on values @@ -69,8 +66,11 @@ int immediate_dir(PROTOCOL_STAT *s, char byte, char *ascii_out) { dir = -1; case 'W': case 'w': - if (!enable) { speedB = 0; steerB = 0; } - enable = 1; + if ( !bldc_getMotorsEnable() ) + { + speedB = 0; steerB = 0; + } + bldc_setMotorsEnable(1); speedB += 10*dir; PWMData.pwm[1] = CLAMP(speedB - steerB, -1000, 1000); PWMData.pwm[0] = CLAMP(speedB + steerB, -1000, 1000); @@ -82,8 +82,11 @@ int immediate_dir(PROTOCOL_STAT *s, char byte, char *ascii_out) { dir = -1; case 'D': case 'd': - if (!enable) { speedB = 0; steerB = 0; } - enable = 1; + if ( !bldc_getMotorsEnable() ) + { + speedB = 0; steerB = 0; + } + bldc_setMotorsEnable(1); steerB += 10*dir; PWMData.pwm[1] = CLAMP(speedB - steerB, -1000, 1000); PWMData.pwm[0] = CLAMP(speedB + steerB, -1000, 1000); @@ -101,7 +104,7 @@ int immediate_stop(PROTOCOL_STAT *s, char byte, char *ascii_out) { PWMData.pwm[1] = CLAMP(speedB - steerB, -1000, 1000); PWMData.pwm[0] = CLAMP(speedB + steerB, -1000, 1000); SpeedData.wanted_speed_mm_per_sec[0] = SpeedData.wanted_speed_mm_per_sec[1] = speedB; - enable = 0; + bldc_setMotorsEnable(0); sprintf(ascii_out, "Stop set\r\n"); return 1; } @@ -113,7 +116,7 @@ int immediate_quit(PROTOCOL_STAT *s, char byte, char *ascii_out) { PWMData.pwm[1] = CLAMP(speedB - steerB, -1000, 1000); PWMData.pwm[0] = CLAMP(speedB + steerB, -1000, 1000); SpeedData.wanted_speed_mm_per_sec[0] = SpeedData.wanted_speed_mm_per_sec[1] = speedB; - enable = 0; + bldc_setMotorsEnable(0); sprintf(ascii_out, "Immediate commands disabled\r\n"); return 1; } diff --git a/Src/bipropellantProtocolMachine.c b/Src/bipropellantProtocolMachine.c index 9e88bc254..8de8c9e71 100644 --- a/Src/bipropellantProtocolMachine.c +++ b/Src/bipropellantProtocolMachine.c @@ -5,7 +5,7 @@ #include "comms.h" #include "util.h" #include "stm32f1xx_hal.h" - +#include "bldc.h" #include #include @@ -19,36 +19,32 @@ PROTOCOL_STAT sUSART3; #endif -extern volatile uint32_t input_timeout_counter; // global variable for input timeout extern uint32_t timeout; //////////////////////////////////////////////////////////////////////////////////////////// // Variable & Functions for 0x09 enable -extern uint8_t enable; // global variable for motor enable extern int main_ascii_init(PROTOCOL_STAT *s); // from ascii_proto_funcs.c ////////////////////////////////////////////// // make values safe before we change enable... -char protocol_enable = 0; +static char protocol_enable = 0; void fn_enable ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { switch (cmd) { case PROTOCOL_CMD_READVAL: case PROTOCOL_CMD_SILENTREAD: - protocol_enable = enable; + protocol_enable = bldc_getMotorsEnable(); break; case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: - if (!protocol_enable) { - // clear speeds to zero - SpeedData.wanted_speed_mm_per_sec[0] = 0; - SpeedData.wanted_speed_mm_per_sec[1] = 0; + if ( bldc_getMotorsEnable() != protocol_enable) { + // clear speeds to zero every time it is changed PWMData.pwm[0] = 0; PWMData.pwm[1] = 0; } - enable = protocol_enable; + bldc_setMotorsEnable(protocol_enable); break; } fn_defaultProcessing(s, param, cmd, msg); @@ -226,7 +222,7 @@ int setup_protocol(PROTOCOL_STAT *s) { // errors += setParamVariable( s, 0x08, UI_NONE, (void *)&electrical_measurements, sizeof(ELECTRICAL_PARAMS) ); - errors += setParamVariable( s, 0x09, UI_CHAR, &protocol_enable, sizeof(enable) ); + errors += setParamVariable( s, 0x09, UI_CHAR, &protocol_enable, sizeof(protocol_enable) ); setParamHandler( s, 0x09, fn_enable ); // errors += setParamVariable( s, 0x0A, UI_CHAR, &disablepoweroff, sizeof(disablepoweroff) ); diff --git a/Src/bldc.c b/Src/bldc.c index 33af34714..dcee87df6 100644 --- a/Src/bldc.c +++ b/Src/bldc.c @@ -62,7 +62,7 @@ uint8_t buzzerFreq = 0; uint8_t buzzerPattern = 0; static uint32_t buzzerTimer = 0; -uint8_t enable = 0; // initially motors are disabled for SAFETY +static volatile uint8_t m_motorsEnable = 0; // initially motors are disabled for SAFETY static uint8_t enableFin = 0; static const uint16_t pwm_res = 64000000 / 2 / PWM_FREQ; // = 2000 @@ -78,6 +78,17 @@ static int16_t offsetdcr = 2000; int16_t batVoltage = (400 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE; static int32_t batVoltageFixdt = (400 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE << 16; // Fixed-point filter output initialized at 400 V*100/cell = 4 V/cell converted to fixed-point + +uint8_t bldc_getMotorsEnable(void) +{ + return m_motorsEnable; +} + +void bldc_setMotorsEnable(uint8_t enable) +{ + m_motorsEnable = enable; +} + // ================================= // DMA interrupt frequency =~ 16 kHz // ================================= @@ -115,13 +126,13 @@ void DMA1_Channel1_IRQHandler(void) { // Disable PWM when current limit is reached (current chopping) // This is the Level 2 of current protection. The Level 1 should kick in first given by I_MOT_MAX - if(ABS(curL_DC) > curDC_max || timeout > TIMEOUT || enable == 0) { + if(ABS(curL_DC) > curDC_max || timeout > TIMEOUT || m_motorsEnable == 0) { LEFT_TIM->BDTR &= ~TIM_BDTR_MOE; } else { LEFT_TIM->BDTR |= TIM_BDTR_MOE; } - if(ABS(curR_DC) > curDC_max || timeout > TIMEOUT || enable == 0) { + if(ABS(curR_DC) > curDC_max || timeout > TIMEOUT || m_motorsEnable == 0) { RIGHT_TIM->BDTR &= ~TIM_BDTR_MOE; } else { RIGHT_TIM->BDTR |= TIM_BDTR_MOE; @@ -150,7 +161,7 @@ void DMA1_Channel1_IRQHandler(void) { OverrunFlag = true; /* Make sure to stop BOTH motors in case of an error */ - enableFin = enable && !rtY_Left.z_errCode && !rtY_Right.z_errCode; + enableFin = m_motorsEnable && !rtY_Left.z_errCode && !rtY_Right.z_errCode; // ========================= LEFT MOTOR ============================ // Get hall sensors values diff --git a/Src/main.c b/Src/main.c index d8427795f..24dd9433e 100644 --- a/Src/main.c +++ b/Src/main.c @@ -27,6 +27,7 @@ #include "config.h" #include "util.h" #include "comms.h" +#include "bldc.h" #include "BLDC_controller.h" /* BLDC's header file */ #include "rtwtypes.h" #include "bipropellantProtocolMachine.h" @@ -76,8 +77,6 @@ extern volatile int pwmr; // global variable for pwm right. -1000 extern uint8_t buzzerFreq; // global variable for the buzzer pitch. can be 1, 2, 3, 4, 5, 6, 7... extern uint8_t buzzerPattern; // global variable for the buzzer pattern. can be 1, 2, 3, 4, 5, 6, 7... -extern uint8_t enable; // global variable for motor enable - extern volatile uint32_t timeout; // global variable for timeout extern int16_t batVoltage; // global variable for battery voltage @@ -227,10 +226,10 @@ int main(void) { #ifndef VARIANT_TRANSPOTTER // ####### MOTOR ENABLING: Only if the initial input is very small (for SAFETY) ####### - if (enable == 0 && (!rtY_Left.z_errCode && !rtY_Right.z_errCode) && (cmd1 > -50 && cmd1 < 50) && (cmd2 > -50 && cmd2 < 50)){ + if (bldc_getMotorsEnable() == 0 && (!rtY_Left.z_errCode && !rtY_Right.z_errCode) && (cmd1 > -50 && cmd1 < 50) && (cmd2 > -50 && cmd2 < 50)){ shortBeep(6); // make 2 beeps indicating the motor enable shortBeep(4); HAL_Delay(100); - enable = 1; // enable motors + bldc_setMotorsEnable(1); // enable motors consoleLog("-- Motors enabled --\r\n"); } @@ -305,7 +304,7 @@ int main(void) { speedR = speedR * 0.8f + (CLAMP(distanceErr - (steering*((float)MAX(ABS(distanceErr), 50)) * ROT_P), -850, 850) * -0.2f); if ((speedL < lastSpeedL + 50 && speedL > lastSpeedL - 50) && (speedR < lastSpeedR + 50 && speedR > lastSpeedR - 50)) { if (distanceErr > 0) { - enable = 1; + bldc_setMotorsEnable(1); } if (distanceErr > -300) { #ifdef INVERT_R_DIRECTION @@ -323,11 +322,11 @@ int main(void) { if (!HAL_GPIO_ReadPin(LED_PORT, LED_PIN)) { //enable = 1; } else { - enable = 0; + bldc_setMotorsEnable(0); } } } else { - enable = 0; + bldc_setMotorsEnable(0); } } timeout = 0; @@ -336,7 +335,7 @@ int main(void) { if (timeout > TIMEOUT) { pwml = 0; pwmr = 0; - enable = 0; + bldc_setMotorsEnable(0); #ifdef SUPPORT_LCD LCD_SetLocation(&lcd, 0, 0); LCD_WriteString(&lcd, "Len:"); LCD_SetLocation(&lcd, 8, 0); LCD_WriteString(&lcd, "m("); @@ -347,7 +346,7 @@ int main(void) { } if ((distance / 1345.0) - setDistance > 0.5 && (lastDistance / 1345.0) - setDistance > 0.5) { // Error, robot too far away! - enable = 0; + bldc_setMotorsEnable(0); longBeep(5); #ifdef SUPPORT_LCD LCD_ClearDisplay(&lcd); @@ -377,7 +376,7 @@ int main(void) { #ifdef SUPPORT_LCD if (transpotter_counter % 100 == 0) { - if (LCDerrorFlag == 1 && enable == 0) { + if (LCDerrorFlag == 1 && bldc_getMotorsEnable() == 0) { } else { if (nunchuk_connected == 0) { @@ -471,7 +470,7 @@ int main(void) { if ((TEMP_POWEROFF_ENABLE && board_temp_deg_c >= TEMP_POWEROFF && speedAvgAbs < 20) || (batVoltage < BAT_DEAD && speedAvgAbs < 20)) { // poweroff before mainboard burns OR low bat 3 poweroff(); } else if (rtY_Left.z_errCode || rtY_Right.z_errCode) { // disable motors and beep in case of Motor error - fast beep - enable = 0; + bldc_setMotorsEnable(0); buzzerFreq = 8; buzzerPattern = 1; } else if (timeoutFlagADC || timeoutFlagSerial) { // beep in case of ADC or Serial timeout - fast beep diff --git a/Src/util.c b/Src/util.c index 56d33ae8f..68ec64db9 100644 --- a/Src/util.c +++ b/Src/util.c @@ -27,6 +27,7 @@ #include "comms.h" #include "eeprom.h" #include "util.h" +#include "bldc.h" #include "BLDC_controller.h" #include "rtwtypes.h" #include "bipropellantProtocolMachine.h" @@ -50,8 +51,6 @@ extern uint8_t backwardDrive; extern uint8_t buzzerFreq; // global variable for the buzzer pitch. can be 1, 2, 3, 4, 5, 6, 7... extern uint8_t buzzerPattern; // global variable for the buzzer pattern. can be 1, 2, 3, 4, 5, 6, 7... -extern uint8_t enable; // global variable for motor enable - extern uint8_t nunchuk_data[6]; extern volatile uint32_t timeout; // global variable for timeout extern volatile uint32_t main_loop_counter; @@ -312,7 +311,7 @@ void Input_Init(void) { #endif #ifdef VARIANT_TRANSPOTTER - enable = 1; + bldc_setMotorsEnable(1); HAL_FLASH_Unlock(); EE_Init(); /* EEPROM Init */ @@ -713,7 +712,7 @@ int addDeadBand(int16_t u, int16_t deadBand, int16_t min, int16_t max) { void poweroff(void) { buzzerPattern = 0; - enable = 0; + bldc_setMotorsEnable(0); consoleLog("-- Motors disabled --\r\n"); for (int i = 0; i < 8; i++) { buzzerFreq = (uint8_t)i; @@ -728,7 +727,7 @@ void poweroff(void) { void poweroffPressCheck(void) { #if defined(CONTROL_ADC) if(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { - enable = 0; + bldc_setMotorsEnable(0); uint16_t cnt_press = 0; while(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { HAL_Delay(10); @@ -752,7 +751,7 @@ void poweroffPressCheck(void) { } #elif defined(VARIANT_TRANSPOTTER) if(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { - enable = 0; + bldc_setMotorsEnable(0); while(HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { HAL_Delay(10); } shortBeep(5); HAL_Delay(300); @@ -773,7 +772,7 @@ void poweroffPressCheck(void) { } #else if (HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) { - enable = 0; // disable motors + bldc_setMotorsEnable(0); while (HAL_GPIO_ReadPin(BUTTON_PORT, BUTTON_PIN)) {} // wait until button is released poweroff(); // release power-latch } @@ -1107,9 +1106,9 @@ void sideboardLeds(uint8_t *leds) { // Enable flag: use LED4 (bottom Blue) // enable == 1, turn on led // enable == 0, blink led - if (enable) { + if (bldc_getMotorsEnable()) { *leds |= LED4_SET; - } else if (!enable && (main_loop_counter % 20 == 0)) { + } else if (!bldc_getMotorsEnable() && (main_loop_counter % 20 == 0)) { *leds ^= LED4_SET; } From ba210db4434b21a9c7d09e067f4a0c2f463582ca Mon Sep 17 00:00:00 2001 From: phail Date: Mon, 29 Jun 2020 17:18:29 +0200 Subject: [PATCH 15/16] add readout of electrical parameters --- Inc/bldc.h | 3 +++ Inc/setup.h | 3 +++ Src/bipropellantProtocolMachine.c | 45 ++++++++++++++++++++----------- Src/bldc.c | 8 +++--- Src/main.c | 2 -- Src/util.c | 2 -- 6 files changed, 39 insertions(+), 24 deletions(-) diff --git a/Inc/bldc.h b/Inc/bldc.h index f0ee582ec..1fc0392c9 100644 --- a/Inc/bldc.h +++ b/Inc/bldc.h @@ -6,4 +6,7 @@ uint8_t bldc_getMotorsEnable(void); void bldc_setMotorsEnable(uint8_t enable); +extern int16_t batVoltage; +extern int32_t batVoltageFixdt; + #endif \ No newline at end of file diff --git a/Inc/setup.h b/Inc/setup.h index b7dd9d033..ce87e9305 100644 --- a/Inc/setup.h +++ b/Inc/setup.h @@ -24,6 +24,7 @@ #define SETUP_H #include "stm32f1xx_hal.h" +#include "defines.h" void MX_GPIO_Init(void); void MX_TIM_Init(void); @@ -32,5 +33,7 @@ void MX_ADC2_Init(void); void UART2_Init(void); void UART3_Init(void); +extern volatile adc_buf_t adc_buffer; + #endif diff --git a/Src/bipropellantProtocolMachine.c b/Src/bipropellantProtocolMachine.c index 8de8c9e71..d88162811 100644 --- a/Src/bipropellantProtocolMachine.c +++ b/Src/bipropellantProtocolMachine.c @@ -6,6 +6,7 @@ #include "util.h" #include "stm32f1xx_hal.h" #include "bldc.h" +#include "setup.h" #include #include @@ -21,15 +22,14 @@ extern uint32_t timeout; -//////////////////////////////////////////////////////////////////////////////////////////// -// Variable & Functions for 0x09 enable - extern int main_ascii_init(PROTOCOL_STAT *s); // from ascii_proto_funcs.c -////////////////////////////////////////////// -// make values safe before we change enable... + +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x09 enable static char protocol_enable = 0; + void fn_enable ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { switch (cmd) { case PROTOCOL_CMD_READVAL: @@ -76,12 +76,6 @@ void fn_SpeedData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTO fn_defaultProcessing(s, param, cmd, msg); } - -//////////////////////////////////////////////////////////////////////////////////////////// -// Variable & Functions for 0x0A disablepoweroff - -extern uint8_t disablepoweroff; - //////////////////////////////////////////////////////////////////////////////////////////// // Variable & Functions for 0x0D PWMData and 0x0E PWMData.pwm @@ -165,10 +159,33 @@ void fn_BuzzerData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROT } } -//extern volatile ELECTRICAL_PARAMS electrical_measurements; +//////////////////////////////////////////////////////////////////////////////////////////// +// Variable & Functions for 0x08 Electrical Params +extern int16_t curDC_max; +extern int16_t curL_DC; +extern int16_t curR_DC; +void fn_electrictalParams ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCOL_MSG3full *msg ) { + switch (cmd) { + case PROTOCOL_CMD_READVAL: + case PROTOCOL_CMD_SILENTREAD: + if(param != NULL && param->ptr != NULL && param->len == sizeof(ELECTRICAL_PARAMS)) + { + ELECTRICAL_PARAMS *measured = (ELECTRICAL_PARAMS*) param->ptr; + memset(measured, 0, sizeof(ELECTRICAL_PARAMS)); + measured->bat_raw = adc_buffer.batt1; + measured->batteryVoltage = (float)batVoltageFixdt / (float)0xFFFF; + measured->board_temp_raw = adc_buffer.temp; + measured->dcCurLim = curDC_max; + measured->motors[0].dcAmps = curL_DC; + measured->motors[1].dcAmps = curR_DC; + } + break; + } + fn_defaultProcessingReadOnly(s, param, cmd, msg); +} //////////////////////////////////////////////////////////////////////////////////////////// @@ -220,13 +237,11 @@ int setup_protocol(PROTOCOL_STAT *s) { errors += setParamVariable( s, 0x03, UI_NONE, &SpeedData, sizeof(SpeedData) ); setParamHandler( s, 0x03, fn_SpeedData ); -// errors += setParamVariable( s, 0x08, UI_NONE, (void *)&electrical_measurements, sizeof(ELECTRICAL_PARAMS) ); + setParamHandler( s, 0x08, fn_electrictalParams ); errors += setParamVariable( s, 0x09, UI_CHAR, &protocol_enable, sizeof(protocol_enable) ); setParamHandler( s, 0x09, fn_enable ); -// errors += setParamVariable( s, 0x0A, UI_CHAR, &disablepoweroff, sizeof(disablepoweroff) ); - errors += setParamVariable( s, 0x0D, UI_NONE, &PWMData, sizeof(PWMData) ); setParamHandler( s, 0x0D, fn_PWMData ); diff --git a/Src/bldc.c b/Src/bldc.c index dcee87df6..ee1b448d0 100644 --- a/Src/bldc.c +++ b/Src/bldc.c @@ -47,15 +47,13 @@ extern ExtY rtY_Right; /* External outputs */ static int16_t pwm_margin = 110; /* This margin allows to always have a window in the PWM signal for proper Phase currents measurement */ -static int16_t curDC_max = (I_DC_MAX * A2BIT_CONV); +int16_t curDC_max = (I_DC_MAX * A2BIT_CONV); int16_t curL_phaA = 0, curL_phaB = 0, curL_DC = 0; int16_t curR_phaB = 0, curR_phaC = 0, curR_DC = 0; volatile int pwml = 0; volatile int pwmr = 0; -extern volatile adc_buf_t adc_buffer; - extern volatile uint32_t timeout; uint8_t buzzerFreq = 0; @@ -75,8 +73,8 @@ static int16_t offsetrrC = 2000; static int16_t offsetdcl = 2000; static int16_t offsetdcr = 2000; -int16_t batVoltage = (400 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE; -static int32_t batVoltageFixdt = (400 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE << 16; // Fixed-point filter output initialized at 400 V*100/cell = 4 V/cell converted to fixed-point +int16_t batVoltage = (400 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE; +int32_t batVoltageFixdt = (400 * BAT_CELLS * BAT_CALIB_ADC) / BAT_CALIB_REAL_VOLTAGE << 16; // Fixed-point filter output initialized at 400 V*100/cell = 4 V/cell converted to fixed-point uint8_t bldc_getMotorsEnable(void) diff --git a/Src/main.c b/Src/main.c index 24dd9433e..f6e99c3d6 100644 --- a/Src/main.c +++ b/Src/main.c @@ -46,7 +46,6 @@ extern TIM_HandleTypeDef htim_left; extern TIM_HandleTypeDef htim_right; extern ADC_HandleTypeDef hadc1; extern ADC_HandleTypeDef hadc2; -extern volatile adc_buf_t adc_buffer; #if defined(DEBUG_I2C_LCD) || defined(SUPPORT_LCD) extern LCD_PCF8574_HandleTypeDef lcd; extern uint8_t LCDerrorFlag; @@ -78,7 +77,6 @@ extern uint8_t buzzerFreq; // global variable for the buzzer pitch. extern uint8_t buzzerPattern; // global variable for the buzzer pattern. can be 1, 2, 3, 4, 5, 6, 7... extern volatile uint32_t timeout; // global variable for timeout -extern int16_t batVoltage; // global variable for battery voltage #if defined(SIDEBOARD_SERIAL_USART2) extern SerialSideboard Sideboard_L; diff --git a/Src/util.c b/Src/util.c index 68ec64db9..b27d0f218 100644 --- a/Src/util.c +++ b/Src/util.c @@ -41,12 +41,10 @@ //------------------------------------------------------------------------ // Global variables set externally //------------------------------------------------------------------------ -extern volatile adc_buf_t adc_buffer; extern I2C_HandleTypeDef hi2c2; extern UART_HandleTypeDef huart2; extern UART_HandleTypeDef huart3; -extern int16_t batVoltage; extern uint8_t backwardDrive; extern uint8_t buzzerFreq; // global variable for the buzzer pitch. can be 1, 2, 3, 4, 5, 6, 7... extern uint8_t buzzerPattern; // global variable for the buzzer pattern. can be 1, 2, 3, 4, 5, 6, 7... From 0d123ad0aa2c4ba6485a5afcc89222893c52fd70 Mon Sep 17 00:00:00 2001 From: EmanuelFeru Date: Thu, 2 Jul 2020 12:30:19 +0200 Subject: [PATCH 16/16] Fixed build - replaced `enable` with `m_motorsEnable` - fix renamig of `timeout` to `timeoutCnt` --- Src/bipropellantProtocolMachine.c | 4 ++-- Src/bldc.c | 4 ++-- Src/main.c | 4 +--- Src/util.c | 10 +++++----- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Src/bipropellantProtocolMachine.c b/Src/bipropellantProtocolMachine.c index d88162811..142b1ae05 100644 --- a/Src/bipropellantProtocolMachine.c +++ b/Src/bipropellantProtocolMachine.c @@ -20,7 +20,7 @@ PROTOCOL_STAT sUSART3; #endif -extern uint32_t timeout; +extern uint32_t timeoutCnt; extern int main_ascii_init(PROTOCOL_STAT *s); // from ascii_proto_funcs.c @@ -94,7 +94,7 @@ void fn_PWMData ( PROTOCOL_STAT *s, PARAMSTAT *param, unsigned char cmd, PROTOCO case PROTOCOL_CMD_WRITEVAL: case PROTOCOL_CMD_READVALRESPONSE: ctrlModReq = 1; // 1 = VOLTAGE mode (default), 2 = SPEED mode, 3 = TORQUE mode. - timeout= 0; + timeoutCnt = 0; break; } diff --git a/Src/bldc.c b/Src/bldc.c index 0ab1846f0..bb604f420 100644 --- a/Src/bldc.c +++ b/Src/bldc.c @@ -124,13 +124,13 @@ void DMA1_Channel1_IRQHandler(void) { // Disable PWM when current limit is reached (current chopping) // This is the Level 2 of current protection. The Level 1 should kick in first given by I_MOT_MAX - if(ABS(curL_DC) > curDC_max || enable == 0) { + if(ABS(curL_DC) > curDC_max || m_motorsEnable == 0) { LEFT_TIM->BDTR &= ~TIM_BDTR_MOE; } else { LEFT_TIM->BDTR |= TIM_BDTR_MOE; } - if(ABS(curR_DC) > curDC_max || enable == 0) { + if(ABS(curR_DC) > curDC_max || m_motorsEnable == 0) { RIGHT_TIM->BDTR &= ~TIM_BDTR_MOE; } else { RIGHT_TIM->BDTR |= TIM_BDTR_MOE; diff --git a/Src/main.c b/Src/main.c index a74445015..41b0f9a18 100644 --- a/Src/main.c +++ b/Src/main.c @@ -77,8 +77,6 @@ extern volatile int pwmr; // global variable for pwm right. -1000 extern uint8_t buzzerFreq; // global variable for the buzzer pitch. can be 1, 2, 3, 4, 5, 6, 7... extern uint8_t buzzerPattern; // global variable for the buzzer pattern. can be 1, 2, 3, 4, 5, 6, 7... -extern uint8_t enable; // global variable for motor enable - extern int16_t batVoltage; // global variable for battery voltage #if defined(SIDEBOARD_SERIAL_USART2) @@ -217,7 +215,7 @@ int main(void) { protocol_tick( &sUSART3 ); if(sUSART3.ascii.enable_immediate) { - timeout = 0; + timeoutCnt = 0; } #endif cmd2 = (PWMData.pwm[0] + PWMData.pwm[1]) /2; // Speed diff --git a/Src/util.c b/Src/util.c index 503a1198c..709fa8b39 100644 --- a/Src/util.c +++ b/Src/util.c @@ -821,15 +821,15 @@ void readCommand(void) { #ifdef CONTROL_ADC // ADC values range: 0-4095, see ADC-calibration in config.h #ifdef ADC1_MID_POT - cmd1 = CLAMP((adc_buffer.l_tx2 - ADC1_MID_CAL) * INPUT_MAX / (ADC1_MAX_CAL - ADC1_MID_CAL), 0, INPUT_MAX) - +CLAMP((ADC1_MID_CAL - adc_buffer.l_tx2) * INPUT_MIN / (ADC1_MID_CAL - ADC1_MIN_CAL), INPUT_MIN, 0); // ADC1 + cmd1 = CLAMP((adc_buffer.l_tx2 - ADC1_MID_CAL) * INPUT_MAX / (ADC1_MAX_CAL - ADC1_MID_CAL), 0, INPUT_MAX) + +CLAMP((ADC1_MID_CAL - adc_buffer.l_tx2) * INPUT_MIN / (ADC1_MID_CAL - ADC1_MIN_CAL), INPUT_MIN, 0); // ADC1 #else cmd1 = CLAMP((adc_buffer.l_tx2 - ADC1_MIN_CAL) * INPUT_MAX / (ADC1_MAX_CAL - ADC1_MIN_CAL), 0, INPUT_MAX); // ADC1 #endif #ifdef ADC2_MID_POT - cmd2 = CLAMP((adc_buffer.l_rx2 - ADC2_MID_CAL) * INPUT_MAX / (ADC2_MAX_CAL - ADC2_MID_CAL), 0, INPUT_MAX) - +CLAMP((ADC2_MID_CAL - adc_buffer.l_rx2) * INPUT_MIN / (ADC2_MID_CAL - ADC2_MIN_CAL), INPUT_MIN, 0); // ADC2 + cmd2 = CLAMP((adc_buffer.l_rx2 - ADC2_MID_CAL) * INPUT_MAX / (ADC2_MAX_CAL - ADC2_MID_CAL), 0, INPUT_MAX) + +CLAMP((ADC2_MID_CAL - adc_buffer.l_rx2) * INPUT_MIN / (ADC2_MID_CAL - ADC2_MIN_CAL), INPUT_MIN, 0); // ADC2 #else cmd2 = CLAMP((adc_buffer.l_rx2 - ADC2_MIN_CAL) * INPUT_MAX / (ADC2_MAX_CAL - ADC2_MIN_CAL), 0, INPUT_MAX); // ADC2 #endif @@ -914,7 +914,7 @@ void readCommand(void) { timeoutFlagSerial = timeoutFlagSerial_L || timeoutFlagSerial_R; #endif - #ifdef VARIANT_HOVERCAR + #ifdef VARIANT_HOVERCAR brakePressed = (uint8_t)(cmd1 > 50); #endif