diff --git a/applications/app.c b/applications/app.c index cf7e51f2d3..eb4af77e5c 100644 --- a/applications/app.c +++ b/applications/app.c @@ -51,7 +51,7 @@ void app_set_configuration(app_configuration *conf) { app_ppm_stop(); app_adc_stop(); - app_uartcomm_stop(); + app_uartcomm_stop(UART_PORT_COMM_HEADER); app_nunchuk_stop(); app_balance_stop(); app_pas_stop(); @@ -84,19 +84,19 @@ void app_set_configuration(app_configuration *conf) { case APP_UART: hw_stop_i2c(); - app_uartcomm_start(); + app_uartcomm_start(UART_PORT_COMM_HEADER); break; case APP_PPM_UART: hw_stop_i2c(); app_ppm_start(); - app_uartcomm_start(); + app_uartcomm_start(UART_PORT_COMM_HEADER); break; case APP_ADC_UART: hw_stop_i2c(); app_adc_start(false); - app_uartcomm_start(); + app_uartcomm_start(UART_PORT_COMM_HEADER); break; case APP_NUNCHUK: @@ -107,7 +107,7 @@ void app_set_configuration(app_configuration *conf) { app_balance_start(); if(appconf.imu_conf.type == IMU_TYPE_INTERNAL){ hw_stop_i2c(); - app_uartcomm_start(); + app_uartcomm_start(UART_PORT_COMM_HEADER); } break; @@ -141,7 +141,8 @@ void app_set_configuration(app_configuration *conf) { app_ppm_configure(&appconf.app_ppm_conf); app_adc_configure(&appconf.app_adc_conf); app_pas_configure(&appconf.app_pas_conf); - app_uartcomm_configure(appconf.app_uart_baudrate, appconf.permanent_uart_enabled); + app_uartcomm_configure(appconf.app_uart_baudrate, true, UART_PORT_COMM_HEADER); + app_uartcomm_configure(0, appconf.permanent_uart_enabled, UART_PORT_BUILTIN); app_nunchuk_configure(&appconf.app_chuk_conf); #ifdef APP_CUSTOM_TO_USE diff --git a/applications/app.h b/applications/app.h index 1e47e0b93c..adf094ad74 100644 --- a/applications/app.h +++ b/applications/app.h @@ -43,12 +43,17 @@ float app_adc_get_voltage(void); float app_adc_get_decoded_level2(void); float app_adc_get_voltage2(void); -void app_uartcomm_start(void); -void app_uartcomm_start_permanent(void); -void app_uartcomm_stop(void); -void app_uartcomm_configure(uint32_t baudrate, bool permanent_enabled); -void app_uartcomm_send_packet(unsigned char *data, unsigned int len); -void app_uartcomm_send_packet_p(unsigned char *data, unsigned int len); +typedef enum { + UART_PORT_COMM_HEADER = 0, + UART_PORT_BUILTIN, + UART_PORT_EXTRA_HEADER +} UART_PORT; + +void app_uartcomm_initialize(void); +void app_uartcomm_start(UART_PORT port_number); +void app_uartcomm_stop(UART_PORT port_number); +void app_uartcomm_configure(uint32_t baudrate, bool permanent_enabled, UART_PORT port_number); +void app_uartcomm_send_packet(unsigned char *data, unsigned int len, UART_PORT port_number); void app_nunchuk_start(void); void app_nunchuk_stop(void); diff --git a/applications/app_uartcomm.c b/applications/app_uartcomm.c index 29397a6362..689e1d9540 100644 --- a/applications/app_uartcomm.c +++ b/applications/app_uartcomm.c @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . - */ + */ #include "app.h" #include "ch.h" @@ -24,12 +24,21 @@ #include "packet.h" #include "commands.h" -#include - // Settings + +// By default 7/8 pin uart is index 0, builtin BLE is index 1, and third uart is index 2 #define BAUDRATE 115200 -#define PACKET_HANDLER 1 -#define PACKET_HANDLER_P 2 +#ifndef UART_NUMBER +#ifdef HW_UART_3_DEV +#define UART_NUMBER 3 +#else +#ifdef HW_UART_P_DEV +#define UART_NUMBER 2 +#else +#define UART_NUMBER 1 +#endif +#endif +#endif // Threads static THD_FUNCTION(packet_process_thread, arg); @@ -37,93 +46,101 @@ static THD_WORKING_AREA(packet_process_thread_wa, 4096); // Variables static volatile bool thread_is_running = false; -static volatile bool uart_is_running = false; -static mutex_t send_mutex; -static bool send_mutex_init_done = false; +static volatile bool uart_is_running[UART_NUMBER] = {false}; +static mutex_t send_mutex[UART_NUMBER]; +static bool send_mutex_init_done[UART_NUMBER] = {false}; +static SerialConfig uart_cfg[UART_NUMBER] = {{ + BAUDRATE, + 0, + USART_CR2_LINEN, + 0 +}}; -#ifdef HW_UART_P_DEV -static mutex_t send_mutex_p; -static bool send_mutex_p_init_done = false; -#endif +// Different for Rx and Tx because it is possible for hardware to use different UART driver +// for Rx and Tx, feature not a bug XD +static SerialDriver *serialPortDriverTx[UART_NUMBER]; +static SerialDriver *serialPortDriverRx[UART_NUMBER]; +static stm32_gpio_t * TxGpioPort[UART_NUMBER]; +static stm32_gpio_t * RxGpioPort[UART_NUMBER]; +static uint8_t TxGpioPin[UART_NUMBER], RxGpioPin[UART_NUMBER], gpioAF[UART_NUMBER]; +static PACKET_STATE_t packet_state[UART_NUMBER]; // Private functions -static void process_packet(unsigned char *data, unsigned int len); -static void send_packet(unsigned char *data, unsigned int len); +static void process_packet(unsigned char *data, unsigned int len, unsigned int port_number); +static void write_packet(unsigned char *data, unsigned int len, unsigned int port_number); -#ifdef HW_UART_P_DEV -static void process_packet_p(unsigned char *data, unsigned int len); -static void send_packet_p(unsigned char *data, unsigned int len); -#endif +static void process_packet_1(unsigned char *data, unsigned int len) {process_packet(data,len,UART_PORT_COMM_HEADER);} +static void write_packet_1(unsigned char *data, unsigned int len) {write_packet(data,len,UART_PORT_COMM_HEADER);} +static void send_packet_1(unsigned char *data, unsigned int len) {app_uartcomm_send_packet(data,len,UART_PORT_COMM_HEADER);} -static SerialConfig uart_cfg = { - BAUDRATE, - 0, - USART_CR2_LINEN, - 0 -}; +static void process_packet_2(unsigned char *data, unsigned int len) {process_packet(data,len,UART_PORT_BUILTIN);} +static void write_packet_2(unsigned char *data, unsigned int len) {write_packet(data,len,UART_PORT_BUILTIN);} +static void send_packet_2(unsigned char *data, unsigned int len) {app_uartcomm_send_packet(data,len,UART_PORT_BUILTIN);} -#ifdef HW_UART_P_DEV -static volatile bool from_p_uart = false; -static volatile bool uart_p_is_running = false; -static SerialConfig uart_p_cfg = { - HW_UART_P_BAUD, - 0, - USART_CR2_LINEN, - 0 -}; -#endif +static void process_packet_3(unsigned char *data, unsigned int len) {process_packet(data,len,UART_PORT_EXTRA_HEADER);} +static void write_packet_3(unsigned char *data, unsigned int len) {write_packet(data,len,UART_PORT_EXTRA_HEADER);} +static void send_packet_3(unsigned char *data, unsigned int len) {app_uartcomm_send_packet(data,len,UART_PORT_EXTRA_HEADER);} -static void process_packet(unsigned char *data, unsigned int len) { - commands_process_packet(data, len, app_uartcomm_send_packet); -} +typedef void (*data_func) (unsigned char *data, unsigned int len); +static data_func write_functions[3] = {write_packet_1, write_packet_2, write_packet_3}; +static data_func process_functions[3] = {process_packet_1, process_packet_2, process_packet_3}; +static data_func send_functions[3] = {send_packet_1, send_packet_2, send_packet_3}; -#ifdef HW_UART_P_DEV -static void process_packet_p(unsigned char *data, unsigned int len) { - commands_process_packet(data, len, app_uartcomm_send_packet_p); +static void write_packet(unsigned char *data, unsigned int len, unsigned int port_number) { + if (port_number >= UART_NUMBER) { + return; + } + + if (uart_is_running[port_number]) { + sdWrite(serialPortDriverTx[port_number], data, len); + } } -#endif -static void send_packet(unsigned char *data, unsigned int len) { - if (uart_is_running) { - sdWrite(&HW_UART_DEV, data, len); +static void process_packet(unsigned char *data, unsigned int len, unsigned int port_number) { + if (port_number >= UART_NUMBER) { + return; } + + commands_process_packet(data, len, send_functions[port_number]); } +void app_uartcomm_initialize(void) { + serialPortDriverTx[0] = &HW_UART_DEV; + serialPortDriverRx[0] = &HW_UART_DEV; + uart_cfg[0].speed = BAUDRATE; + RxGpioPort[0] = HW_UART_RX_PORT; RxGpioPin[0] = HW_UART_RX_PIN; + TxGpioPort[0] = HW_UART_TX_PORT; TxGpioPin[0] = HW_UART_TX_PIN; + gpioAF[0] = HW_UART_GPIO_AF; + #ifdef HW_UART_P_DEV -static void send_packet_p(unsigned char *data, unsigned int len) { - if (uart_p_is_running) { #ifdef HW_UART_P_DEV_TX - sdWrite(&HW_UART_P_DEV_TX, data, len); + serialPortDriverTx[1] = &HW_UART_P_DEV_TX; #else - sdWrite(&HW_UART_P_DEV, data, len); + serialPortDriverTx[1] = &HW_UART_P_DEV; #endif - } -} + serialPortDriverRx[1] = &HW_UART_P_DEV; + uart_cfg[1].speed = HW_UART_P_BAUD; + RxGpioPort[1] = HW_UART_P_RX_PORT; RxGpioPin[1] = HW_UART_P_RX_PIN; + TxGpioPort[1] = HW_UART_P_TX_PORT; TxGpioPin[1] = HW_UART_P_TX_PIN; + gpioAF[1] = HW_UART_P_GPIO_AF; #endif -void app_uartcomm_start(void) { - packet_init(send_packet, process_packet, PACKET_HANDLER); +#ifdef HW_UART_3_DEV + serialPortDriverTx[2] = &HW_UART_3_DEV; + serialPortDriverRx[2] = &HW_UART_3_DEV; + uart_cfg[2].speed = HW_UART_3_BAUD; + RxGpioPort[2] = HW_UART_3_RX_PORT; RxGpioPin[2] = HW_UART_3_RX_PIN; + TxGpioPort[2] = HW_UART_3_TX_PORT;TxGpioPin[2] = HW_UART_3_TX_PIN; + gpioAF[2] = HW_UART_3_GPIO_AF; +#endif +} - if (!thread_is_running) { - chThdCreateStatic(packet_process_thread_wa, sizeof(packet_process_thread_wa), - NORMALPRIO, packet_process_thread, NULL); - thread_is_running = true; +void app_uartcomm_start(UART_PORT port_number) { + if(port_number >= UART_NUMBER){ + return; } - sdStart(&HW_UART_DEV, &uart_cfg); - palSetPadMode(HW_UART_TX_PORT, HW_UART_TX_PIN, PAL_MODE_ALTERNATE(HW_UART_GPIO_AF) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_PULLUP); - palSetPadMode(HW_UART_RX_PORT, HW_UART_RX_PIN, PAL_MODE_ALTERNATE(HW_UART_GPIO_AF) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_PULLUP); - - uart_is_running = true; -} - -void app_uartcomm_start_permanent(void) { -#ifdef HW_UART_P_DEV - packet_init(send_packet_p, process_packet_p, PACKET_HANDLER_P); + packet_init(write_functions[port_number], process_functions[port_number], &packet_state[port_number]); if (!thread_is_running) { chThdCreateStatic(packet_process_thread_wa, sizeof(packet_process_thread_wa), @@ -131,83 +148,70 @@ void app_uartcomm_start_permanent(void) { thread_is_running = true; } - sdStart(&HW_UART_P_DEV, &uart_p_cfg); + sdStart(serialPortDriverRx[port_number], &uart_cfg[port_number]); + sdStart(serialPortDriverTx[port_number], &uart_cfg[port_number]); + uart_is_running[port_number] = true; -#ifdef HW_UART_P_DEV_TX - sdStart(&HW_UART_P_DEV_TX, &uart_p_cfg); -#endif - - palSetPadMode(HW_UART_P_TX_PORT, HW_UART_P_TX_PIN, PAL_MODE_ALTERNATE(HW_UART_P_GPIO_AF) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_PULLUP); - palSetPadMode(HW_UART_P_RX_PORT, HW_UART_P_RX_PIN, PAL_MODE_ALTERNATE(HW_UART_P_GPIO_AF) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_PULLUP); - - uart_p_is_running = true; -#endif + palSetPadMode(TxGpioPort[port_number], TxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) | + PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_PULLUP); + palSetPadMode(RxGpioPort[port_number], RxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) | + PAL_STM32_OSPEED_HIGHEST | PAL_STM32_PUDR_PULLUP); } -void app_uartcomm_stop(void) { - if (uart_is_running) { - sdStop(&HW_UART_DEV); - palSetPadMode(HW_UART_TX_PORT, HW_UART_TX_PIN, PAL_MODE_INPUT_PULLUP); - palSetPadMode(HW_UART_RX_PORT, HW_UART_RX_PIN, PAL_MODE_INPUT_PULLUP); - uart_is_running = false; +void app_uartcomm_stop(UART_PORT port_number) { + if(port_number >= UART_NUMBER) { + return; } + if (uart_is_running[port_number]) { + sdStop(serialPortDriverRx[port_number]); + sdStop(serialPortDriverTx[port_number]); + palSetPadMode(TxGpioPort[port_number], TxGpioPin[port_number], PAL_MODE_INPUT_PULLUP); + palSetPadMode(RxGpioPort[port_number], RxGpioPin[port_number], PAL_MODE_INPUT_PULLUP); + uart_is_running[port_number] = false; + } // Notice that the processing thread is kept running in case this call is made from it. } -void app_uartcomm_send_packet(unsigned char *data, unsigned int len) { - if (!send_mutex_init_done) { - chMtxObjectInit(&send_mutex); - send_mutex_init_done = true; +void app_uartcomm_send_packet(unsigned char *data, unsigned int len, UART_PORT port_number) { + if (port_number >= UART_NUMBER) { + return; } - chMtxLock(&send_mutex); - packet_send_packet(data, len, PACKET_HANDLER); - chMtxUnlock(&send_mutex); -} - -void app_uartcomm_send_packet_p(unsigned char *data, unsigned int len) { -#ifdef HW_UART_P_DEV - if (!send_mutex_p_init_done) { - chMtxObjectInit(&send_mutex_p); - send_mutex_p_init_done = true; + if (!send_mutex_init_done[port_number]) { + chMtxObjectInit(&send_mutex[port_number]); + send_mutex_init_done[port_number] = true; } - chMtxLock(&send_mutex_p); - packet_send_packet(data, len, PACKET_HANDLER_P); - chMtxUnlock(&send_mutex_p); -#else - (void)data; - (void)len; -#endif + chMtxLock(&send_mutex[port_number]); + packet_send_packet(data, len, &packet_state[port_number]); + chMtxUnlock(&send_mutex[port_number]); } -void app_uartcomm_configure(uint32_t baudrate, bool permanent_enabled) { - uart_cfg.speed = baudrate; +void app_uartcomm_configure(uint32_t baudrate, bool enabled, UART_PORT port_number) { + if (port_number >= UART_NUMBER) { + return; + } - if (thread_is_running && uart_is_running) { - sdStart(&HW_UART_DEV, &uart_cfg); + if (baudrate > 0) { + uart_cfg[port_number].speed = baudrate; } -#ifdef HW_UART_P_DEV - if (permanent_enabled) { - palSetPadMode(HW_UART_P_TX_PORT, HW_UART_P_TX_PIN, PAL_MODE_ALTERNATE(HW_UART_P_GPIO_AF) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_PULLUP); - palSetPadMode(HW_UART_P_RX_PORT, HW_UART_P_RX_PIN, PAL_MODE_ALTERNATE(HW_UART_P_GPIO_AF) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_PULLUP); + if (thread_is_running && uart_is_running[port_number]) { + sdStart(serialPortDriverRx[port_number], &uart_cfg[port_number]); + } + + if (enabled) { + palSetPadMode(TxGpioPort[port_number], TxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) | + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_PULLUP); + palSetPadMode(RxGpioPort[port_number], RxGpioPin[port_number], PAL_MODE_ALTERNATE(gpioAF[port_number]) | + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_PULLUP); } else { - palSetPadMode(HW_UART_P_TX_PORT, HW_UART_P_TX_PIN, PAL_MODE_INPUT); - palSetPadMode(HW_UART_P_RX_PORT, HW_UART_P_RX_PIN, PAL_MODE_INPUT); + palSetPadMode(TxGpioPort[port_number], TxGpioPin[port_number], PAL_MODE_INPUT); + palSetPadMode(RxGpioPort[port_number], RxGpioPin[port_number], PAL_MODE_INPUT); } -#else - (void)permanent_enabled; -#endif } static THD_FUNCTION(packet_process_thread, arg) { @@ -215,13 +219,10 @@ static THD_FUNCTION(packet_process_thread, arg) { chRegSetThreadName("uartcomm proc"); - event_listener_t el; - chEvtRegisterMaskWithFlags(&HW_UART_DEV.event, &el, EVENT_MASK(0), CHN_INPUT_AVAILABLE); - -#ifdef HW_UART_P_DEV - event_listener_t elp; - chEvtRegisterMaskWithFlags(&HW_UART_P_DEV.event, &elp, EVENT_MASK(0), CHN_INPUT_AVAILABLE); -#endif + event_listener_t el[UART_NUMBER]; + for(int port_number = 0; port_number < UART_NUMBER; port_number++) { + chEvtRegisterMaskWithFlags(&(*serialPortDriverRx[port_number]).event, &el[port_number], EVENT_MASK(0), CHN_INPUT_AVAILABLE); + } for(;;) { chEvtWaitAnyTimeout(ALL_EVENTS, ST2MS(10)); @@ -229,26 +230,16 @@ static THD_FUNCTION(packet_process_thread, arg) { bool rx = true; while (rx) { rx = false; - - if (uart_is_running) { - msg_t res = sdGetTimeout(&HW_UART_DEV, TIME_IMMEDIATE); - if (res != MSG_TIMEOUT) { -#ifdef HW_UART_P_DEV - from_p_uart = false; -#endif - packet_process_byte(res, PACKET_HANDLER); - rx = true; + for(int port_number = 0; port_number < UART_NUMBER; port_number++) { + if (uart_is_running[port_number]) { + msg_t res = sdGetTimeout(serialPortDriverRx[port_number], TIME_IMMEDIATE); + if (res != MSG_TIMEOUT) { + packet_process_byte(res, &packet_state[port_number]); + rx = true; + } } } - -#ifdef HW_UART_P_DEV - msg_t res = sdGetTimeout(&HW_UART_P_DEV, TIME_IMMEDIATE); - if (res != MSG_TIMEOUT) { - from_p_uart = true; - packet_process_byte(res, PACKET_HANDLER_P); - rx = true; - } -#endif } } } + diff --git a/build_all/UBOX_75_100/VESC_default.bin b/build_all/UBOX_75_100/VESC_default.bin new file mode 100755 index 0000000000..8e968ef888 Binary files /dev/null and b/build_all/UBOX_75_100/VESC_default.bin differ diff --git a/build_all/rebuild_all b/build_all/rebuild_all index d3c097b360..26b3e138c0 100755 --- a/build_all/rebuild_all +++ b/build_all/rebuild_all @@ -536,3 +536,22 @@ cp $FWPATH/build/BLDC_4_ChibiOS.bin $COPYDIR/VESC_default_no_hw_limits.bin cd $FWPATH make clean cd $DIR + + +#################### UBOX 75/100 ######################## + +COPYDIR=UBOX_75_100 +mkdir -p $COPYDIR +rm -f $COPYDIR/* + +# default +cd $FWPATH +touch conf_general.h +make -j8 build_args='-DHW_SOURCE=\"hw_ubox_75_100.c\" -DHW_HEADER=\"hw_ubox_75_100.h\"' USE_VERBOSE_COMPILE=no +cd $DIR +cp $FWPATH/build/BLDC_4_ChibiOS.bin $COPYDIR/VESC_default.bin + +# Clean +cd $FWPATH +make clean +cd $DIR diff --git a/comm_usb.c b/comm_usb.c index 6ce2d956ae..65e07ee8fd 100644 --- a/comm_usb.c +++ b/comm_usb.c @@ -24,9 +24,6 @@ #include "comm_usb_serial.h" #include "commands.h" -// Settings -#define PACKET_HANDLER 0 - // Private variables #define SERIAL_RX_BUFFER_SIZE 2048 static uint8_t serial_rx_buffer[SERIAL_RX_BUFFER_SIZE]; @@ -38,6 +35,7 @@ static mutex_t send_mutex; static thread_t *process_tp; static volatile unsigned int write_timeout_cnt = 0; static volatile bool was_timeout = false; +static PACKET_STATE_t packet_state; // Private functions static void process_packet(unsigned char *data, unsigned int len); @@ -82,7 +80,7 @@ static THD_FUNCTION(serial_process_thread, arg) { chEvtWaitAny((eventmask_t) 1); while (serial_rx_read_pos != serial_rx_write_pos) { - packet_process_byte(serial_rx_buffer[serial_rx_read_pos++], PACKET_HANDLER); + packet_process_byte(serial_rx_buffer[serial_rx_read_pos++], &packet_state); if (serial_rx_read_pos == SERIAL_RX_BUFFER_SIZE) { serial_rx_read_pos = 0; @@ -118,7 +116,7 @@ static void send_packet_raw(unsigned char *buffer, unsigned int len) { void comm_usb_init(void) { comm_usb_serial_init(); - packet_init(send_packet_raw, process_packet, PACKET_HANDLER); + packet_init(send_packet_raw, process_packet, &packet_state); chMtxObjectInit(&send_mutex); @@ -129,7 +127,7 @@ void comm_usb_init(void) { void comm_usb_send_packet(unsigned char *data, unsigned int len) { chMtxLock(&send_mutex); - packet_send_packet(data, len, PACKET_HANDLER); + packet_send_packet(data, len, &packet_state); chMtxUnlock(&send_mutex); } diff --git a/commands.c b/commands.c index 63bc236695..839ffc55c8 100644 --- a/commands.c +++ b/commands.c @@ -1450,9 +1450,9 @@ void commands_set_ble_name(char* name) { buffer[ind++] = '\0'; #ifdef HW_UART_P_DEV - app_uartcomm_send_packet_p(buffer, ind); + app_uartcomm_send_packet(buffer, ind, UART_PORT_BUILTIN); #else - app_uartcomm_send_packet(buffer, ind); + app_uartcomm_send_packet(buffer, ind, UART_PORT_COMM_HEADER); #endif } @@ -1469,9 +1469,9 @@ void commands_set_ble_pin(char* pin) { ind += pin_len; buffer[ind++] = '\0'; #ifdef HW_UART_P_DEV - app_uartcomm_send_packet_p(buffer, ind); + app_uartcomm_send_packet(buffer, ind, UART_PORT_BUILTIN); #else - app_uartcomm_send_packet(buffer, ind); + app_uartcomm_send_packet(buffer, ind, UART_PORT_COMM_HEADER); #endif } diff --git a/conf_general.h b/conf_general.h index ea84427825..8df381981f 100644 --- a/conf_general.h +++ b/conf_general.h @@ -22,9 +22,9 @@ // Firmware version #define FW_VERSION_MAJOR 5 -#define FW_VERSION_MINOR 02 +#define FW_VERSION_MINOR 03 // Set to 0 for building a release and iterate during beta test builds -#define FW_TEST_VERSION_NUMBER 0 +#define FW_TEST_VERSION_NUMBER 2 #include "datatypes.h" @@ -191,10 +191,6 @@ /* * Select default user motor configuration */ -//#include "mcconf_sten.h" -//#include "mcconf_sp_540kv.h" -//#include "mcconf_castle_2028.h" -//#include "mcconf_ellwee.h" //#include "conf_test.h" /* @@ -202,7 +198,6 @@ */ //#include "appconf_example_ppm.h" //#include "appconf_custom.h" -//#include "appconf_ellwee.h" /* * Set APP_CUSTOM_TO_USE to the name of the main C file of the custom application. diff --git a/crc.c b/crc.c index 34b99e8bea..57ee31b154 100644 --- a/crc.c +++ b/crc.c @@ -18,7 +18,9 @@ */ #include "crc.h" +#ifndef NO_STM32 #include "stm32f4xx.h" +#endif // CRC Table const unsigned short crc16_tab[] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, @@ -60,6 +62,7 @@ unsigned short crc16(unsigned char *buf, unsigned int len) { return cksum; } +#ifndef NO_STM32 /** * @brief Computes the 32-bit CRC of a given buffer of data word(32-bit) using * Hardware Acceleration. @@ -86,3 +89,4 @@ void crc32_reset(void) { /* Reset CRC generator */ CRC->CR |= CRC_CR_RESET; } +#endif diff --git a/hwconf/hw_stormcore_100d.h b/hwconf/hw_stormcore_100d.h index 331a454314..56bb73d797 100644 --- a/hwconf/hw_stormcore_100d.h +++ b/hwconf/hw_stormcore_100d.h @@ -36,8 +36,8 @@ #define HW_HAS_3_SHUNTS #define DRV8323S_CUSTOM_SETTINGS(); drv8323s_set_current_amp_gain(CURRENT_AMP_GAIN); \ - drv8323s_write_reg(3,0x3af); \ - drv8323s_write_reg(4,0x7af); + drv8323s_write_reg(3,0x3af); \ + drv8323s_write_reg(4,0x7af); @@ -74,14 +74,14 @@ #define HW_SHUTDOWN_HOLD_ON(); #define HW_SAMPLE_SHUTDOWN() 1 #define HW_SHUTDOWN_HOLD_OFF() palClearPad(SWITCH_OUT_GPIO, SWITCH_OUT_PIN); \ - palClearPad(SWITCH_PRECHARGED_GPIO, SWITCH_PRECHARGED_PIN); + palClearPad(SWITCH_PRECHARGED_GPIO, SWITCH_PRECHARGED_PIN); #define DCCAL_ON() //drv8323s_dccal_on() #define DCCAL_OFF() //drv8323s_dccal_off() #define HW_EARLY_INIT() smart_switch_pin_init(); \ - smart_switch_thread_start(); + smart_switch_thread_start(); diff --git a/hwconf/hw_stormcore_100s.h b/hwconf/hw_stormcore_100s.h index 0ab624960c..1852ba805b 100644 --- a/hwconf/hw_stormcore_100s.h +++ b/hwconf/hw_stormcore_100s.h @@ -29,8 +29,8 @@ #define HW_HAS_3_SHUNTS #define DRV8323S_CUSTOM_SETTINGS() drv8323s_set_current_amp_gain(CURRENT_AMP_GAIN); \ - drv8323s_write_reg(3,0x3af); \ - drv8323s_write_reg(4,0x7af); + drv8323s_write_reg(3,0x3af); \ + drv8323s_write_reg(4,0x7af); // Macros #define ENABLE_GATE() palSetPad(GPIOB, 5) diff --git a/hwconf/hw_stormcore_60d.c b/hwconf/hw_stormcore_60d.c index 09fd5a863c..8f0586d953 100644 --- a/hwconf/hw_stormcore_60d.c +++ b/hwconf/hw_stormcore_60d.c @@ -69,88 +69,88 @@ void hw_init_gpio(void) { PHASE_FILTER_OFF(); mc_interface_select_motor_thread(1); palSetPadMode(PHASE_FILTER_GPIO, PHASE_FILTER_PIN, - PAL_MODE_OUTPUT_PUSHPULL | - PAL_STM32_OSPEED_HIGHEST); + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); PHASE_FILTER_OFF(); #endif // LEDs palSetPadMode(GPIOA, 8, - PAL_MODE_OUTPUT_PUSHPULL | - PAL_STM32_OSPEED_HIGHEST); + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOC, 9, - PAL_MODE_OUTPUT_PUSHPULL | - PAL_STM32_OSPEED_HIGHEST); + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); //Temp switches palSetPadMode(ADC_SW_EN_PORT, ADC_SW_EN_PIN, - PAL_MODE_OUTPUT_PUSHPULL | - PAL_STM32_OSPEED_HIGHEST); + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); palSetPadMode(ADC_SW_1_PORT, ADC_SW_1_PIN , - PAL_MODE_OUTPUT_PUSHPULL | - PAL_STM32_OSPEED_HIGHEST); + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); palSetPadMode(ADC_SW_2_PORT, ADC_SW_2_PIN, - PAL_MODE_OUTPUT_PUSHPULL | - PAL_STM32_OSPEED_HIGHEST); + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); palSetPadMode(ADC_SW_3_PORT, ADC_SW_3_PIN , - PAL_MODE_OUTPUT_PUSHPULL | - PAL_STM32_OSPEED_HIGHEST); + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); ENABLE_MOS_TEMP1(); // GPIOC (ENABLE_GATE) palSetPadMode(GPIOE, 14, - PAL_MODE_OUTPUT_PUSHPULL | - PAL_STM32_OSPEED_HIGHEST); + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); palSetPadMode(GPIOD, 4, - PAL_MODE_OUTPUT_PUSHPULL | - PAL_STM32_OSPEED_HIGHEST); + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); DISABLE_GATE(); // GPIOB (DCCAL) // GPIOA Configuration: Channel 1 to 3 as alternate function push-pull palSetPadMode(GPIOE, 8, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); palSetPadMode(GPIOE, 9, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); palSetPadMode(GPIOE, 10, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); palSetPadMode(GPIOE, 11, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); palSetPadMode(GPIOE, 12, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); palSetPadMode(GPIOE, 13, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); palSetPadMode(GPIOC, 6, PAL_MODE_ALTERNATE(GPIO_AF_TIM8) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); palSetPadMode(GPIOC, 7, PAL_MODE_ALTERNATE(GPIO_AF_TIM8) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); palSetPadMode(GPIOC, 8, PAL_MODE_ALTERNATE(GPIO_AF_TIM8) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(GPIO_AF_TIM8) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(GPIO_AF_TIM8) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); palSetPadMode(GPIOA, 7, PAL_MODE_ALTERNATE(GPIO_AF_TIM8) | - PAL_STM32_OSPEED_HIGHEST | - PAL_STM32_PUDR_FLOATING); + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); // Hall sensors palSetPadMode(HW_HALL_ENC_GPIO1, HW_HALL_ENC_PIN1, PAL_MODE_INPUT_PULLUP); diff --git a/hwconf/hw_stormcore_60d.h b/hwconf/hw_stormcore_60d.h index bc02b39af4..e080f768da 100644 --- a/hwconf/hw_stormcore_60d.h +++ b/hwconf/hw_stormcore_60d.h @@ -93,7 +93,7 @@ #define HW_UART_P_BAUD 115200 #define HW_UART_P_DEV SD1 -#define HW_UART_P_GPIO_AF GPIO_AF_USART1 +#define HW_UART_P_GPIO_AF GPIO_AF_USART1 #define HW_UART_P_TX_PORT GPIOA #define HW_UART_P_TX_PIN 9 #define HW_UART_P_RX_PORT GPIOA diff --git a/hwconf/hw_ubox_75_100.c b/hwconf/hw_ubox_75_100.c new file mode 100644 index 0000000000..0b1159532a --- /dev/null +++ b/hwconf/hw_ubox_75_100.c @@ -0,0 +1,262 @@ +/* + Copyright 2018 Benjamin Vedder benjamin@vedder.se + + 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 "hw.h" + +#include "ch.h" +#include "hal.h" +#include "stm32f4xx_conf.h" +#include "utils.h" +#include +#include "mc_interface.h" + +// Variables +static volatile bool i2c_running = false; + +// I2C configuration +static const I2CConfig i2cfg = { + OPMODE_I2C, + 100000, + STD_DUTY_CYCLE +}; + +void hw_init_gpio(void) { + // GPIO clock enable + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); + + // LEDs + palSetPadMode(LED_GREEN_GPIO, LED_GREEN_PIN, + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); + palSetPadMode(LED_RED_GPIO, LED_RED_PIN, + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); + + // GPIOA Configuration: Channel 1 to 3 as alternate function push-pull + palSetPadMode(GPIOA, 8, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); + palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); + palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); + + palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); + palSetPadMode(GPIOB, 14, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); + palSetPadMode(GPIOB, 15, PAL_MODE_ALTERNATE(GPIO_AF_TIM1) | + PAL_STM32_OSPEED_HIGHEST | + PAL_STM32_PUDR_FLOATING); + + // Hall sensors + palSetPadMode(HW_HALL_ENC_GPIO1, HW_HALL_ENC_PIN1, PAL_MODE_INPUT_PULLUP); + palSetPadMode(HW_HALL_ENC_GPIO2, HW_HALL_ENC_PIN2, PAL_MODE_INPUT_PULLUP); + palSetPadMode(HW_HALL_ENC_GPIO3, HW_HALL_ENC_PIN3, PAL_MODE_INPUT_PULLUP); + + // Phase filters + palSetPadMode(PHASE_FILTER_GPIO, PHASE_FILTER_PIN, + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); + PHASE_FILTER_OFF(); + + // Current filter + palSetPadMode(GPIOD, 2, + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); + + CURRENT_FILTER_OFF(); + + // AUX pin + AUX_OFF(); + palSetPadMode(AUX_GPIO, AUX_PIN, + PAL_MODE_OUTPUT_PUSHPULL | + PAL_STM32_OSPEED_HIGHEST); + + // ADC Pins + palSetPadMode(GPIOA, 0, PAL_MODE_INPUT_ANALOG); + palSetPadMode(GPIOA, 1, PAL_MODE_INPUT_ANALOG); + palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_ANALOG); + palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_ANALOG); + palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG); + palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_ANALOG); + + palSetPadMode(GPIOB, 0, PAL_MODE_INPUT_ANALOG); + palSetPadMode(GPIOB, 1, PAL_MODE_INPUT_ANALOG); + + palSetPadMode(GPIOC, 0, PAL_MODE_INPUT_ANALOG); + palSetPadMode(GPIOC, 1, PAL_MODE_INPUT_ANALOG); + palSetPadMode(GPIOC, 2, PAL_MODE_INPUT_ANALOG); + palSetPadMode(GPIOC, 3, PAL_MODE_INPUT_ANALOG); + palSetPadMode(GPIOC, 4, PAL_MODE_INPUT_ANALOG); + palSetPadMode(GPIOC, 5, PAL_MODE_INPUT_ANALOG); +} + +void hw_setup_adc_channels(void) { + // ADC1 regular channels + ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 2, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 3, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 4, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC1, ADC_Channel_Vrefint, 5, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 6, ADC_SampleTime_15Cycles); + + // ADC2 regular channels + ADC_RegularChannelConfig(ADC2, ADC_Channel_1, 1, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 2, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC2, ADC_Channel_6, 3, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC2, ADC_Channel_15, 4, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC2, ADC_Channel_0, 5, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC2, ADC_Channel_9, 6, ADC_SampleTime_15Cycles); + + // ADC3 regular channels + ADC_RegularChannelConfig(ADC3, ADC_Channel_2, 1, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC3, ADC_Channel_12, 2, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC3, ADC_Channel_3, 3, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC3, ADC_Channel_13, 4, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC3, ADC_Channel_1, 5, ADC_SampleTime_15Cycles); + ADC_RegularChannelConfig(ADC3, ADC_Channel_2, 6, ADC_SampleTime_15Cycles); + + // Injected channels + ADC_InjectedChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_15Cycles); + ADC_InjectedChannelConfig(ADC2, ADC_Channel_11, 1, ADC_SampleTime_15Cycles); + ADC_InjectedChannelConfig(ADC3, ADC_Channel_12, 1, ADC_SampleTime_15Cycles); + ADC_InjectedChannelConfig(ADC1, ADC_Channel_10, 2, ADC_SampleTime_15Cycles); + ADC_InjectedChannelConfig(ADC2, ADC_Channel_11, 2, ADC_SampleTime_15Cycles); + ADC_InjectedChannelConfig(ADC3, ADC_Channel_12, 2, ADC_SampleTime_15Cycles); + ADC_InjectedChannelConfig(ADC1, ADC_Channel_10, 3, ADC_SampleTime_15Cycles); + ADC_InjectedChannelConfig(ADC2, ADC_Channel_11, 3, ADC_SampleTime_15Cycles); + ADC_InjectedChannelConfig(ADC3, ADC_Channel_12, 3, ADC_SampleTime_15Cycles); +} + +void hw_start_i2c(void) { + i2cAcquireBus(&HW_I2C_DEV); + + if (!i2c_running) { + palSetPadMode(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN, + PAL_MODE_ALTERNATE(HW_I2C_GPIO_AF) | + PAL_STM32_OTYPE_OPENDRAIN | + PAL_STM32_OSPEED_MID1 | + PAL_STM32_PUDR_PULLUP); + palSetPadMode(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN, + PAL_MODE_ALTERNATE(HW_I2C_GPIO_AF) | + PAL_STM32_OTYPE_OPENDRAIN | + PAL_STM32_OSPEED_MID1 | + PAL_STM32_PUDR_PULLUP); + + i2cStart(&HW_I2C_DEV, &i2cfg); + i2c_running = true; + } + + i2cReleaseBus(&HW_I2C_DEV); +} + +void hw_stop_i2c(void) { + i2cAcquireBus(&HW_I2C_DEV); + + if (i2c_running) { + palSetPadMode(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN, PAL_MODE_INPUT); + palSetPadMode(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN, PAL_MODE_INPUT); + + i2cStop(&HW_I2C_DEV); + i2c_running = false; + + } + + i2cReleaseBus(&HW_I2C_DEV); +} + +/** + * Try to restore the i2c bus + */ +void hw_try_restore_i2c(void) { + if (i2c_running) { + i2cAcquireBus(&HW_I2C_DEV); + + palSetPadMode(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN, + PAL_STM32_OTYPE_OPENDRAIN | + PAL_STM32_OSPEED_MID1 | + PAL_STM32_PUDR_PULLUP); + + palSetPadMode(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN, + PAL_STM32_OTYPE_OPENDRAIN | + PAL_STM32_OSPEED_MID1 | + PAL_STM32_PUDR_PULLUP); + + palSetPad(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN); + palSetPad(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN); + + chThdSleep(1); + + for(int i = 0;i < 16;i++) { + palClearPad(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN); + chThdSleep(1); + palSetPad(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN); + chThdSleep(1); + } + + // Generate start then stop condition + palClearPad(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN); + chThdSleep(1); + palClearPad(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN); + chThdSleep(1); + palSetPad(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN); + chThdSleep(1); + palSetPad(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN); + + palSetPadMode(HW_I2C_SCL_PORT, HW_I2C_SCL_PIN, + PAL_MODE_ALTERNATE(HW_I2C_GPIO_AF) | + PAL_STM32_OTYPE_OPENDRAIN | + PAL_STM32_OSPEED_MID1 | + PAL_STM32_PUDR_PULLUP); + + palSetPadMode(HW_I2C_SDA_PORT, HW_I2C_SDA_PIN, + PAL_MODE_ALTERNATE(HW_I2C_GPIO_AF) | + PAL_STM32_OTYPE_OPENDRAIN | + PAL_STM32_OSPEED_MID1 | + PAL_STM32_PUDR_PULLUP); + + HW_I2C_DEV.state = I2C_STOP; + i2cStart(&HW_I2C_DEV, &i2cfg); + + i2cReleaseBus(&HW_I2C_DEV); + } +} + +float hw75_300_get_temp(void) { + float t1 = (1.0 / ((logf(NTC_RES(ADC_Value[ADC_IND_TEMP_MOS]) / 10000.0) / 3380.0) + (1.0 / 298.15)) - 273.15); + float t2 = (1.0 / ((logf(NTC_RES(ADC_Value[ADC_IND_TEMP_MOS_2]) / 10000.0) / 3380.0) + (1.0 / 298.15)) - 273.15); + float t3 = (1.0 / ((logf(NTC_RES(ADC_Value[ADC_IND_TEMP_MOS_3]) / 10000.0) / 3380.0) + (1.0 / 298.15)) - 273.15); + float res = 0.0; + + if (t1 > t2 && t1 > t3) { + res = t1; + } else if (t2 > t1 && t2 > t3) { + res = t2; + } else { + res = t3; + } + + return res; +} diff --git a/hwconf/hw_ubox_75_100.h b/hwconf/hw_ubox_75_100.h new file mode 100644 index 0000000000..7acb4fb53f --- /dev/null +++ b/hwconf/hw_ubox_75_100.h @@ -0,0 +1,305 @@ +/* + Copyright 2018 Benjamin Vedder benjamin@vedder.se + + This file is part of the VESC firmware. + + The VESC firmware 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. + + The VESC firmware 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 . + */ + +#ifndef HW_75_300_H_ +#define HW_75_300_H_ + +#define HW_NAME "UBOX_75_100" + +// HW properties +#define HW_HAS_3_SHUNTS +#define HW_HAS_PHASE_SHUNTS +//#define HW_HAS_PHASE_FILTERS + +// Macros +#ifdef HW75_300_VEDDER_FIRST_PCB +#define LED_GREEN_GPIO GPIOB +#define LED_GREEN_PIN 0 +#define LED_RED_GPIO GPIOB +#define LED_RED_PIN 1 +#else +#define LED_GREEN_GPIO GPIOB +#define LED_GREEN_PIN 5 +#define LED_RED_GPIO GPIOB +#define LED_RED_PIN 7 +#endif + +#define LED_GREEN_ON() palSetPad(LED_GREEN_GPIO, LED_GREEN_PIN) +#define LED_GREEN_OFF() palClearPad(LED_GREEN_GPIO, LED_GREEN_PIN) +#define LED_RED_ON() palSetPad(LED_RED_GPIO, LED_RED_PIN) +#define LED_RED_OFF() palClearPad(LED_RED_GPIO, LED_RED_PIN) + +#ifdef HW75_300_REV_2 +#define PHASE_FILTER_GPIO GPIOC +#define PHASE_FILTER_PIN 9 +#else +#define PHASE_FILTER_GPIO GPIOC +#define PHASE_FILTER_PIN 11 +#endif +#define PHASE_FILTER_ON() palSetPad(PHASE_FILTER_GPIO, PHASE_FILTER_PIN) +#define PHASE_FILTER_OFF() palClearPad(PHASE_FILTER_GPIO, PHASE_FILTER_PIN) + +#define AUX_GPIO GPIOC +#define AUX_PIN 12 +#define AUX_ON() palSetPad(AUX_GPIO, AUX_PIN) +#define AUX_OFF() palClearPad(AUX_GPIO, AUX_PIN) + +#define CURRENT_FILTER_ON() palSetPad(GPIOD, 2) +#define CURRENT_FILTER_OFF() palClearPad(GPIOD, 2) + +/* + * ADC Vector + * + * 0 (1): IN0 SENS1 + * 1 (2): IN1 SENS2 + * 2 (3): IN2 SENS3 + * 3 (1): IN10 CURR1 + * 4 (2): IN11 CURR2 + * 5 (3): IN12 CURR3 + * 6 (1): IN5 ADC_EXT1 + * 7 (2): IN6 ADC_EXT2 + * 8 (3): IN3 TEMP_MOS + * 9 (1): IN14 TEMP_MOTOR + * 10 (2): IN15 ADC_EXT3 + * 11 (3): IN13 AN_IN + * 12 (1): Vrefint + * 13 (2): IN0 SENS1 + * 14 (3): IN1 SENS2 + * 15 (1): IN8 TEMP_MOS_2 + * 16 (2): IN9 TEMP_MOS_3 + * 17 (3): IN3 SENS3 + */ + +#define HW_ADC_CHANNELS 18 +#define HW_ADC_INJ_CHANNELS 3 +#define HW_ADC_NBR_CONV 6 + +// ADC Indexes +#define ADC_IND_SENS1 0 +#define ADC_IND_SENS2 1 +#define ADC_IND_SENS3 2 +#define ADC_IND_CURR1 3 +#define ADC_IND_CURR2 4 +#define ADC_IND_CURR3 5 +#define ADC_IND_VIN_SENS 11 +#define ADC_IND_EXT 6 +#define ADC_IND_EXT2 7 +#define ADC_IND_EXT3 10 +#ifdef HW75_300_VEDDER_FIRST_PCB +#define ADC_IND_TEMP_MOS 8 +#define ADC_IND_TEMP_MOS_2 8 +#define ADC_IND_TEMP_MOS_3 8 +#else +#define ADC_IND_TEMP_MOS 8 +#define ADC_IND_TEMP_MOS_2 15 +#define ADC_IND_TEMP_MOS_3 16 +#endif +#define ADC_IND_TEMP_MOTOR 9 +#define ADC_IND_VREFINT 12 + +// ADC macros and settings + +// Component parameters (can be overridden) +#ifndef V_REG +#define V_REG 3.3//3.44 +#endif +#ifndef VIN_R1 +#define VIN_R1 56000.0 +#endif +#ifndef VIN_R2 +#define VIN_R2 2200.0 +#endif +#ifndef CURRENT_AMP_GAIN +#define CURRENT_AMP_GAIN 20.0 +#endif +#ifndef CURRENT_SHUNT_RES +#define CURRENT_SHUNT_RES (0.0005 / 3.0) +#endif + +// Input voltage +#define GET_INPUT_VOLTAGE() ((V_REG / 4095.0) * (float)ADC_Value[ADC_IND_VIN_SENS] * ((VIN_R1 + VIN_R2) / VIN_R2)) + +// NTC Termistors +#define NTC_RES(adc_val) ((4095.0 * 10000.0) / adc_val - 10000.0) +#define NTC_TEMP(adc_ind) hw75_300_get_temp() + +#define NTC_RES_MOTOR(adc_val) (10000.0 / ((4095.0 / (float)adc_val) - 1.0)) // Motor temp sensor on low side + +#ifdef HW75_300_VEDDER_FIRST_PCB +#define NTC_TEMP_MOTOR(beta) (-20) +#else +#define NTC_TEMP_MOTOR(beta) (1.0 / ((logf(NTC_RES_MOTOR(ADC_Value[ADC_IND_TEMP_MOTOR]) / 10000.0) / beta) + (1.0 / 298.15)) - 273.15) +#endif + +#ifndef HW75_300_VEDDER_FIRST_PCB +#define NTC_TEMP_MOS1() (1.0 / ((logf(NTC_RES(ADC_Value[ADC_IND_TEMP_MOS]) / 10000.0) / 3380.0) + (1.0 / 298.15)) - 273.15) +#define NTC_TEMP_MOS2() (1.0 / ((logf(NTC_RES(ADC_Value[ADC_IND_TEMP_MOS_2]) / 10000.0) / 3380.0) + (1.0 / 298.15)) - 273.15) +#define NTC_TEMP_MOS3() (1.0 / ((logf(NTC_RES(ADC_Value[ADC_IND_TEMP_MOS_3]) / 10000.0) / 3380.0) + (1.0 / 298.15)) - 273.15) +#endif + +// Voltage on ADC channel +#define ADC_VOLTS(ch) ((float)ADC_Value[ch] / 4096.0 * V_REG) + +// Double samples in beginning and end for positive current measurement. +// Useful when the shunt sense traces have noise that causes offset. +#ifndef CURR1_DOUBLE_SAMPLE +#define CURR1_DOUBLE_SAMPLE 0 +#endif +#ifndef CURR2_DOUBLE_SAMPLE +#define CURR2_DOUBLE_SAMPLE 0 +#endif +#ifndef CURR3_DOUBLE_SAMPLE +#define CURR3_DOUBLE_SAMPLE 0 +#endif + +// COMM-port ADC GPIOs +#define HW_ADC_EXT_GPIO GPIOA +#define HW_ADC_EXT_PIN 5 +#define HW_ADC_EXT2_GPIO GPIOA +#define HW_ADC_EXT2_PIN 6 + +// UART Peripheral +#define HW_UART_DEV SD3 +#define HW_UART_GPIO_AF GPIO_AF_USART3 +#define HW_UART_TX_PORT GPIOB +#define HW_UART_TX_PIN 10 +#define HW_UART_RX_PORT GPIOB +#define HW_UART_RX_PIN 11 + +#if defined(HW75_300_REV_2) || defined(HW75_300_REV_3) +// Permanent UART Peripheral (for NRF51) +#define HW_UART_P_BAUD 115200 +#define HW_UART_P_DEV SD4 +#define HW_UART_P_GPIO_AF GPIO_AF_UART4 +#define HW_UART_P_TX_PORT GPIOC +#define HW_UART_P_TX_PIN 10 +#define HW_UART_P_RX_PORT GPIOC +#define HW_UART_P_RX_PIN 11 +#endif + +#ifdef HW75_300_REV_3 +// NRF SWD +#define NRF5x_SWDIO_GPIO GPIOA +#define NRF5x_SWDIO_PIN 15 +#define NRF5x_SWCLK_GPIO GPIOB +#define NRF5x_SWCLK_PIN 3 +#endif + +// ICU Peripheral for servo decoding +#define HW_USE_SERVO_TIM4 +#define HW_ICU_TIMER TIM4 +#define HW_ICU_TIM_CLK_EN() RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE) +#define HW_ICU_DEV ICUD4 +#define HW_ICU_CHANNEL ICU_CHANNEL_1 +#define HW_ICU_GPIO_AF GPIO_AF_TIM4 +#define HW_ICU_GPIO GPIOB +#define HW_ICU_PIN 6 + +// I2C Peripheral +#define HW_I2C_DEV I2CD2 +#define HW_I2C_GPIO_AF GPIO_AF_I2C2 +#define HW_I2C_SCL_PORT GPIOB +#define HW_I2C_SCL_PIN 10 +#define HW_I2C_SDA_PORT GPIOB +#define HW_I2C_SDA_PIN 11 + +// Hall/encoder pins +#define HW_HALL_ENC_GPIO1 GPIOC +#define HW_HALL_ENC_PIN1 6 +#define HW_HALL_ENC_GPIO2 GPIOC +#define HW_HALL_ENC_PIN2 7 +#define HW_HALL_ENC_GPIO3 GPIOC +#define HW_HALL_ENC_PIN3 8 +#define HW_ENC_TIM TIM3 +#define HW_ENC_TIM_AF GPIO_AF_TIM3 +#define HW_ENC_TIM_CLK_EN() RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE) +#define HW_ENC_EXTI_PORTSRC EXTI_PortSourceGPIOC +#define HW_ENC_EXTI_PINSRC EXTI_PinSource8 +#define HW_ENC_EXTI_CH EXTI9_5_IRQn +#define HW_ENC_EXTI_LINE EXTI_Line8 +#define HW_ENC_EXTI_ISR_VEC EXTI9_5_IRQHandler +#define HW_ENC_TIM_ISR_CH TIM3_IRQn +#define HW_ENC_TIM_ISR_VEC TIM3_IRQHandler + +// SPI pins +#define HW_SPI_DEV SPID1 +#define HW_SPI_GPIO_AF GPIO_AF_SPI1 +#define HW_SPI_PORT_NSS GPIOA +#define HW_SPI_PIN_NSS 4 +#define HW_SPI_PORT_SCK GPIOA +#define HW_SPI_PIN_SCK 5 +#define HW_SPI_PORT_MOSI GPIOA +#define HW_SPI_PIN_MOSI 7 +#define HW_SPI_PORT_MISO GPIOA +#define HW_SPI_PIN_MISO 6 + +// Measurement macros +#define ADC_V_L1 ADC_Value[ADC_IND_SENS1] +#define ADC_V_L2 ADC_Value[ADC_IND_SENS2] +#define ADC_V_L3 ADC_Value[ADC_IND_SENS3] +#define ADC_V_ZERO (ADC_Value[ADC_IND_VIN_SENS] / 2) + +// Macros +#define READ_HALL1() palReadPad(HW_HALL_ENC_GPIO1, HW_HALL_ENC_PIN1) +#define READ_HALL2() palReadPad(HW_HALL_ENC_GPIO2, HW_HALL_ENC_PIN2) +#define READ_HALL3() palReadPad(HW_HALL_ENC_GPIO3, HW_HALL_ENC_PIN3) + +// Override dead time. See the stm32f4 reference manual for calculating this value. +#define HW_DEAD_TIME_NSEC 660.0 + +// Default setting overrides +#ifndef MCCONF_L_MIN_VOLTAGE +#define MCCONF_L_MIN_VOLTAGE 12.0 // Minimum input voltage +#endif +#ifndef MCCONF_L_MAX_VOLTAGE +#define MCCONF_L_MAX_VOLTAGE 72.0 // Maximum input voltage +#endif +#ifndef MCCONF_DEFAULT_MOTOR_TYPE +#define MCCONF_DEFAULT_MOTOR_TYPE MOTOR_TYPE_FOC +#endif +#ifndef MCCONF_FOC_F_SW +#define MCCONF_FOC_F_SW 30000.0 +#endif +#ifndef MCCONF_L_MAX_ABS_CURRENT +#define MCCONF_L_MAX_ABS_CURRENT 160.0//420.0 // The maximum absolute current above which a fault is generated +#endif +#ifndef MCCONF_FOC_SAMPLE_V0_V7 +#define MCCONF_FOC_SAMPLE_V0_V7 false // Run control loop in both v0 and v7 (requires phase shunts) +#endif +#ifndef MCCONF_L_IN_CURRENT_MAX +#define MCCONF_L_IN_CURRENT_MAX 84.0//250.0 // Input current limit in Amperes (Upper) +#endif +#ifndef MCCONF_L_IN_CURRENT_MIN +#define MCCONF_L_IN_CURRENT_MIN -65.0//-200.0 // Input current limit in Amperes (Lower) +#endif + +// Setting limits +#define HW_LIM_CURRENT -135.0, 135.0//-400.0, 400.0 +#define HW_LIM_CURRENT_IN -135.0, 135.0//-400.0, 400.0 +#define HW_LIM_CURRENT_ABS 0.0, 180.0//0.0, 480.0 +#define HW_LIM_VIN 11.0, 72.0 +#define HW_LIM_ERPM -200e3, 200e3 +#define HW_LIM_DUTY_MIN 0.0, 0.1 +#define HW_LIM_DUTY_MAX 0.0, 0.99 +#define HW_LIM_TEMP_FET -40.0, 110.0 + +// HW-specific functions +float hw75_300_get_temp(void); + +#endif /* HW_75_300_H_ */ diff --git a/main.c b/main.c index b870c2c3f0..786fb34345 100644 --- a/main.c +++ b/main.c @@ -78,7 +78,6 @@ // Private variables static THD_WORKING_AREA(periodic_thread_wa, 1024); -static THD_WORKING_AREA(timer_thread_wa, 128); static THD_WORKING_AREA(flash_integrity_check_thread_wa, 256); static THD_FUNCTION(flash_integrity_check_thread, arg) { @@ -180,18 +179,6 @@ static THD_FUNCTION(periodic_thread, arg) { } } -static THD_FUNCTION(timer_thread, arg) { - (void)arg; - - chRegSetThreadName("msec_timer"); - - for(;;) { - packet_timerfunc(); - timeout_feed_WDT(THREAD_TIMER); - chThdSleepMilliseconds(1); - } -} - // When assertions enabled halve PWM frequency. The control loop ISR runs 40% slower void assert_failed(uint8_t* file, uint32_t line) { commands_printf("Wrong parameters value: file %s on line %d\r\n", file, line); @@ -251,10 +238,12 @@ int main(void) { comm_can_init(); #endif + app_uartcomm_initialize(); app_configuration *appconf = mempools_alloc_appconf(); conf_general_read_app_configuration(appconf); app_set_configuration(appconf); - app_uartcomm_start_permanent(); + app_uartcomm_start(UART_PORT_BUILTIN); + app_uartcomm_start(UART_PORT_EXTRA_HEADER); #ifdef HW_HAS_PERMANENT_NRF conf_general_permanent_nrf_found = nrf_driver_init(); @@ -286,7 +275,6 @@ int main(void) { // Threads chThdCreateStatic(periodic_thread_wa, sizeof(periodic_thread_wa), NORMALPRIO, periodic_thread, NULL); - chThdCreateStatic(timer_thread_wa, sizeof(timer_thread_wa), NORMALPRIO, timer_thread, NULL); chThdCreateStatic(flash_integrity_check_thread_wa, sizeof(flash_integrity_check_thread_wa), LOWPRIO, flash_integrity_check_thread, NULL); #if WS2811_TEST diff --git a/mcconf/mcconf_default.h b/mcconf/mcconf_default.h index fef663c13c..26b67e0b1f 100644 --- a/mcconf/mcconf_default.h +++ b/mcconf/mcconf_default.h @@ -351,7 +351,7 @@ #define MCCONF_FOC_CURRENT_FILTER_CONST 0.1 // Filter constant for the filtered currents #endif #ifndef MCCONF_FOC_CC_DECOUPLING -#define MCCONF_FOC_CC_DECOUPLING FOC_CC_DECOUPLING_BEMF // Current controller decoupling +#define MCCONF_FOC_CC_DECOUPLING FOC_CC_DECOUPLING_DISABLED // Current controller decoupling #endif #ifndef MCCONF_FOC_OBSERVER_TYPE #define MCCONF_FOC_OBSERVER_TYPE FOC_OBSERVER_ORTEGA_ORIGINAL // Position observer type for FOC diff --git a/mcuconf.h b/mcuconf.h index 06a5ff97cc..b52c38ba70 100644 --- a/mcuconf.h +++ b/mcuconf.h @@ -273,7 +273,7 @@ * SERIAL driver system settings. */ #define STM32_SERIAL_USE_USART1 TRUE -#define STM32_SERIAL_USE_USART2 FALSE +#define STM32_SERIAL_USE_USART2 TRUE #define STM32_SERIAL_USE_USART3 TRUE #define STM32_SERIAL_USE_UART4 TRUE #define STM32_SERIAL_USE_UART5 TRUE diff --git a/packet.c b/packet.c index cc735d16cd..2ea5d2a36a 100644 --- a/packet.c +++ b/packet.c @@ -1,5 +1,5 @@ /* - Copyright 2016 - 2019 Benjamin Vedder benjamin@vedder.se + Copyright 2016 - 2021 Benjamin Vedder benjamin@vedder.se This file is part of the VESC firmware. @@ -21,135 +21,93 @@ #include "packet.h" #include "crc.h" -/** - * The latest update aims at achieving optimal re-synchronization in the - * case if lost data, at the cost of some performance. - */ - -// Defines -#define BUFFER_LEN (PACKET_MAX_PL_LEN + 8) - -// Private types -typedef struct { - volatile unsigned short rx_timeout; - void(*send_func)(unsigned char *data, unsigned int len); - void(*process_func)(unsigned char *data, unsigned int len); - unsigned int rx_read_ptr; - unsigned int rx_write_ptr; - int bytes_left; - unsigned char rx_buffer[BUFFER_LEN]; - unsigned char tx_buffer[BUFFER_LEN]; -} PACKET_STATE_t; - -// Private variables -static PACKET_STATE_t m_handler_states[PACKET_HANDLERS]; - // Private functions static int try_decode_packet(unsigned char *buffer, unsigned int in_len, void(*process_func)(unsigned char *data, unsigned int len), int *bytes_left); void packet_init(void (*s_func)(unsigned char *data, unsigned int len), - void (*p_func)(unsigned char *data, unsigned int len), int handler_num) { - memset(&m_handler_states[handler_num], 0, sizeof(PACKET_STATE_t)); - m_handler_states[handler_num].send_func = s_func; - m_handler_states[handler_num].process_func = p_func; + void (*p_func)(unsigned char *data, unsigned int len), PACKET_STATE_t *state) { + memset(state, 0, sizeof(PACKET_STATE_t)); + state->send_func = s_func; + state->process_func = p_func; } -void packet_reset(int handler_num) { - m_handler_states[handler_num].rx_read_ptr = 0; - m_handler_states[handler_num].rx_write_ptr = 0; - m_handler_states[handler_num].bytes_left = 0; +void packet_reset(PACKET_STATE_t *state) { + state->rx_read_ptr = 0; + state->rx_write_ptr = 0; + state->bytes_left = 0; } -void packet_send_packet(unsigned char *data, unsigned int len, int handler_num) { +void packet_send_packet(unsigned char *data, unsigned int len, PACKET_STATE_t *state) { if (len == 0 || len > PACKET_MAX_PL_LEN) { return; } int b_ind = 0; - PACKET_STATE_t *handler = &m_handler_states[handler_num]; if (len <= 255) { - handler->tx_buffer[b_ind++] = 2; - handler->tx_buffer[b_ind++] = len; + state->tx_buffer[b_ind++] = 2; + state->tx_buffer[b_ind++] = len; } else if (len <= 65535) { - handler->tx_buffer[b_ind++] = 3; - handler->tx_buffer[b_ind++] = len >> 8; - handler->tx_buffer[b_ind++] = len & 0xFF; + state->tx_buffer[b_ind++] = 3; + state->tx_buffer[b_ind++] = len >> 8; + state->tx_buffer[b_ind++] = len & 0xFF; } else { - handler->tx_buffer[b_ind++] = 4; - handler->tx_buffer[b_ind++] = len >> 16; - handler->tx_buffer[b_ind++] = (len >> 8) & 0x0F; - handler->tx_buffer[b_ind++] = len & 0xFF; + state->tx_buffer[b_ind++] = 4; + state->tx_buffer[b_ind++] = len >> 16; + state->tx_buffer[b_ind++] = (len >> 8) & 0x0F; + state->tx_buffer[b_ind++] = len & 0xFF; } - memcpy(handler->tx_buffer + b_ind, data, len); + memcpy(state->tx_buffer + b_ind, data, len); b_ind += len; unsigned short crc = crc16(data, len); - handler->tx_buffer[b_ind++] = (uint8_t)(crc >> 8); - handler->tx_buffer[b_ind++] = (uint8_t)(crc & 0xFF); - handler->tx_buffer[b_ind++] = 3; - - if (handler->send_func) { - handler->send_func(handler->tx_buffer, b_ind); - } -} + state->tx_buffer[b_ind++] = (uint8_t)(crc >> 8); + state->tx_buffer[b_ind++] = (uint8_t)(crc & 0xFF); + state->tx_buffer[b_ind++] = 3; -/** - * Call this function every millisecond. This is not strictly necessary - * if the timeout is unimportant. - */ -void packet_timerfunc(void) { - for (int i = 0;i < PACKET_HANDLERS;i++) { - if (m_handler_states[i].rx_timeout) { - m_handler_states[i].rx_timeout--; - } else { - packet_reset(i); - } + if (state->send_func) { + state->send_func(state->tx_buffer, b_ind); } } -void packet_process_byte(uint8_t rx_data, int handler_num) { - PACKET_STATE_t *handler = &m_handler_states[handler_num]; - - handler->rx_timeout = PACKET_RX_TIMEOUT; - - unsigned int data_len = handler->rx_write_ptr - handler->rx_read_ptr; +void packet_process_byte(uint8_t rx_data, PACKET_STATE_t *state) { + unsigned int data_len = state->rx_write_ptr - state->rx_read_ptr; // Out of space (should not happen) - if (data_len >= BUFFER_LEN) { - handler->rx_write_ptr = 0; - handler->rx_read_ptr = 0; - handler->bytes_left = 0; - handler->rx_buffer[handler->rx_write_ptr++] = rx_data; + if (data_len >= PACKET_BUFFER_LEN) { + state->rx_write_ptr = 0; + state->rx_read_ptr = 0; + state->bytes_left = 0; + state->rx_buffer[state->rx_write_ptr++] = rx_data; return; } // Everything has to be aligned, so shift buffer if we are out of space. // (as opposed to using a circular buffer) - if (handler->rx_write_ptr >= BUFFER_LEN) { - memmove(handler->rx_buffer, - handler->rx_buffer + handler->rx_read_ptr, + if (state->rx_write_ptr >= PACKET_BUFFER_LEN) { + memmove(state->rx_buffer, + state->rx_buffer + state->rx_read_ptr, data_len); - handler->rx_read_ptr = 0; - handler->rx_write_ptr = data_len; + state->rx_read_ptr = 0; + state->rx_write_ptr = data_len; } - handler->rx_buffer[handler->rx_write_ptr++] = rx_data; + state->rx_buffer[state->rx_write_ptr++] = rx_data; data_len++; - if (handler->bytes_left > 1) { - handler->bytes_left--; + if (state->bytes_left > 1) { + state->bytes_left--; return; } // Try decoding the packet at various offsets until it succeeds, or // until we run out of data. for (;;) { - int res = try_decode_packet(handler->rx_buffer + handler->rx_read_ptr, - data_len, handler->process_func, &handler->bytes_left); + int res = try_decode_packet(state->rx_buffer + state->rx_read_ptr, + data_len, state->process_func, &state->bytes_left); // More data is needed if (res == -2) { @@ -158,18 +116,18 @@ void packet_process_byte(uint8_t rx_data, int handler_num) { if (res > 0) { data_len -= res; - handler->rx_read_ptr += res; + state->rx_read_ptr += res; } else if (res == -1) { // Something went wrong. Move pointer forward and try again. - handler->rx_read_ptr++; + state->rx_read_ptr++; data_len--; } } // Nothing left, move pointers to avoid memmove if (data_len == 0) { - handler->rx_read_ptr = 0; - handler->rx_write_ptr = 0; + state->rx_read_ptr = 0; + state->rx_write_ptr = 0; } } diff --git a/packet.h b/packet.h index f1875f5558..e747d3e5c3 100644 --- a/packet.h +++ b/packet.h @@ -1,5 +1,5 @@ /* - Copyright 2016 - 2019 Benjamin Vedder benjamin@vedder.se + Copyright 2016 - 2021 Benjamin Vedder benjamin@vedder.se This file is part of the VESC firmware. @@ -24,24 +24,28 @@ #include // Settings -#ifndef PACKET_RX_TIMEOUT -#define PACKET_RX_TIMEOUT 1000 -#endif - -#ifndef PACKET_HANDLERS -#define PACKET_HANDLERS 3 -#endif - #ifndef PACKET_MAX_PL_LEN #define PACKET_MAX_PL_LEN 512 #endif +#define PACKET_BUFFER_LEN (PACKET_MAX_PL_LEN + 8) + +// Types +typedef struct { + void(*send_func)(unsigned char *data, unsigned int len); + void(*process_func)(unsigned char *data, unsigned int len); + unsigned int rx_read_ptr; + unsigned int rx_write_ptr; + int bytes_left; + unsigned char rx_buffer[PACKET_BUFFER_LEN]; + unsigned char tx_buffer[PACKET_BUFFER_LEN]; +} PACKET_STATE_t; + // Functions void packet_init(void (*s_func)(unsigned char *data, unsigned int len), - void (*p_func)(unsigned char *data, unsigned int len), int handler_num); -void packet_reset(int handler_num); -void packet_process_byte(uint8_t rx_data, int handler_num); -void packet_timerfunc(void); -void packet_send_packet(unsigned char *data, unsigned int len, int handler_num); + void (*p_func)(unsigned char *data, unsigned int len), PACKET_STATE_t *state); +void packet_reset(PACKET_STATE_t *state); +void packet_process_byte(uint8_t rx_data, PACKET_STATE_t *state); +void packet_send_packet(unsigned char *data, unsigned int len, PACKET_STATE_t *state); #endif /* PACKET_H_ */ diff --git a/tests/packet_recovery/Makefile b/tests/packet_recovery/Makefile index 3f073337e7..053eead7f5 100644 --- a/tests/packet_recovery/Makefile +++ b/tests/packet_recovery/Makefile @@ -1,7 +1,7 @@ TARGET = test LIBS = -lm CC = gcc -CFLAGS = -O2 -g -Wall -Wextra -Wundef -std=gnu99 -I../../ +CFLAGS = -O2 -g -Wall -Wextra -Wundef -std=gnu99 -I../../ -DNO_STM32 SOURCES = main.c ../../packet.c ../../crc.c HEADERS = ../../packet.h ../../crc.h OBJECTS = $(notdir $(SOURCES:.c=.o)) diff --git a/tests/packet_recovery/main.c b/tests/packet_recovery/main.c index d40ea438e9..07f765d569 100644 --- a/tests/packet_recovery/main.c +++ b/tests/packet_recovery/main.c @@ -5,9 +5,10 @@ #include "packet.h" -const unsigned int rand_prepend = 50; +static const unsigned int rand_prepend = 50; static uint8_t buffer[250000]; static unsigned int write = 0; +static PACKET_STATE_t state; void send_packet(unsigned char *data, unsigned int len) { memcpy(buffer + write, data, len); @@ -24,7 +25,7 @@ void process_packet_perf(unsigned char *data, unsigned int len) { } int main(void) { - packet_init(send_packet, process_packet, 0); + packet_init(send_packet, process_packet, &state); srand(104); @@ -36,7 +37,7 @@ int main(void) { } sprintf(asd + rand_prepend, "Offset: %d Test %d", write, i); - packet_send_packet((unsigned char*)asd, strlen(asd + rand_prepend) + rand_prepend + 1, 0); + packet_send_packet((unsigned char*)asd, strlen(asd + rand_prepend) + rand_prepend + 1, &state); } // Ability to recover @@ -46,7 +47,7 @@ int main(void) { //packet_reset(0); for(unsigned int i = offsets[ofs];i < write;i++) { - packet_process_byte(buffer[i], 0); + packet_process_byte(buffer[i], &state); } printf("\r\n"); @@ -63,12 +64,12 @@ int main(void) { buffer[1200]++; buffer[1342]++; for(unsigned int i = 0;i < write;i++) { - packet_process_byte(buffer[i], 0); + packet_process_byte(buffer[i], &state); } // Performance printf("\r\nPerformance Test\r\n"); - packet_init(send_packet, process_packet_perf, 0); + packet_init(send_packet, process_packet_perf, &state); srand(104); write = 0; @@ -82,9 +83,9 @@ int main(void) { start = clock(); for (int i = 0;i < 1e6;i++) { - packet_send_packet(asd, sizeof(asd), 0); + packet_send_packet(asd, sizeof(asd), &state); for (unsigned int j = 0;j < write;j++) { - packet_process_byte(buffer[j], 0); + packet_process_byte(buffer[j], &state); } write = 0; } diff --git a/timeout.c b/timeout.c index 146b6213cd..ef02edca10 100644 --- a/timeout.c +++ b/timeout.c @@ -191,14 +191,12 @@ static THD_FUNCTION(timeout_thread, arg) { if(feed_counter[THREAD_MCPWM] < MIN_THREAD_ITERATIONS) { threads_ok = false; } + #if CAN_ENABLE if(feed_counter[THREAD_CANBUS] < MIN_THREAD_ITERATIONS) { threads_ok = false; } #endif - if(feed_counter[THREAD_TIMER] < MIN_THREAD_ITERATIONS) { - threads_ok = false; - } for( int i = 0; i < MAX_THREADS_MONITOR; i++) { feed_counter[i] = 0; diff --git a/timeout.h b/timeout.h index f794e4d52a..9dd4c2ceef 100644 --- a/timeout.h +++ b/timeout.h @@ -30,7 +30,6 @@ typedef enum { THREAD_MCPWM = 0, THREAD_CANBUS, - THREAD_TIMER, THREAD_USB, THREAD_APP } WWDT_THREAD_TYPES;