diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index 94d07d1b1d..eeff1807e4 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -497,7 +497,6 @@ void runBinaryProtocolLoop(ts_channel_s *tsChannel) { logMsg("received %d\r\n", received); #endif - if (received != 1) { // tunerStudioError("ERROR: no command"); #if EFI_BLUETOOTH_SETUP diff --git a/firmware/console/binary/tunerstudio_io.cpp b/firmware/console/binary/tunerstudio_io.cpp index 1c2c44a54f..5d736671a3 100644 --- a/firmware/console/binary/tunerstudio_io.cpp +++ b/firmware/console/binary/tunerstudio_io.cpp @@ -23,9 +23,19 @@ extern LoggingWithStorage tsLogger; #include "usbconsole.h" #if HAL_USE_SERIAL_USB -extern SerialUSBDriver SDU1; +#define SERIAL_USB_DRIVER SerialUSBDriver +#define TS_USB_DEVICE EFI_CONSOLE_USB_DEVICE // SDU1 #endif /* HAL_USE_SERIAL_USB */ +#ifdef TS_USB_DEVICE +extern SERIAL_USB_DRIVER TS_USB_DEVICE; +#endif /* TS_USB_DEVICE */ + +#ifdef TS_CAN_DEVICE +#include "tunerstudio_can.h" +#endif /* TS_CAN_DEVICE */ + + #if TS_UART_DMA_MODE // Async. FIFO buffer takes some RAM... static uart_dma_s tsUartDma; @@ -77,8 +87,10 @@ static UARTConfig tsUartConfig = { .speed = 0, .cr1 = 0, .cr2 = 0/*USART_CR2_STOP1_BITS*/ | USART_CR2_LINEN, .cr3 = 0, .timeout_cb = NULL, .rxhalf_cb = NULL }; -#else +#elif defined(TS_SERIAL_DEVICE) static SerialConfig tsSerialConfig = { .speed = 0, .cr1 = 0, .cr2 = USART_CR2_STOP1_BITS | USART_CR2_LINEN, .cr3 = 0 }; +#elif defined(TS_CAN_DEVICE) +static CANConfig tsCanConfig = { CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP, CAN_BTR_500 }; #endif /* TS_UART_DMA_MODE */ #endif /* EFI_PROD_CODE */ @@ -87,7 +99,7 @@ void startTsPort(ts_channel_s *tsChannel) { tsChannel->channel = (BaseChannel *) NULL; #if EFI_PROD_CODE - #if defined(CONSOLE_USB_DEVICE) + #if defined(TS_USB_DEVICE) print("TunerStudio over USB serial"); /** * This method contains a long delay, that's the reason why this is not done on the main thread @@ -95,9 +107,9 @@ void startTsPort(ts_channel_s *tsChannel) { */ usb_serial_start(); // if console uses UART then TS uses USB - tsChannel->channel = (BaseChannel *) &CONSOLE_USB_DEVICE; + tsChannel->channel = (BaseChannel *) &TS_USB_DEVICE; return; - #endif /* CONSOLE_USB_DEVICE */ + #endif /* TS_USB_DEVICE */ #if defined(TS_UART_DEVICE) || defined(TS_SERIAL_DEVICE) if (CONFIG(useSerialPort)) { @@ -136,6 +148,19 @@ void startTsPort(ts_channel_s *tsChannel) { #endif } #endif /* TS_UART_DMA_MODE || TS_UART_MODE */ + #if defined(TS_CAN_DEVICE) + /*if (CONFIG(useCanForTs))*/ { + print("TunerStudio over CAN"); + + efiSetPadMode("ts can rx", GPIOG_13/*CONFIG(canRxPin)*/, PAL_MODE_ALTERNATE(TS_CAN_AF)); // CAN2_RX2_0 + efiSetPadMode("ts can tx", GPIOG_14/*CONFIG(canTxPin)*/, PAL_MODE_ALTERNATE(TS_CAN_AF)); // CAN2_TX2_0 + + canStart(&TS_CAN_DEVICE, &tsCanConfig); + canInit(&TS_CAN_DEVICE); + + //tsChannel->channel = (BaseChannel *) &TS_CAN_DEVICE; + } + #endif /* TS_CAN_DEVICE */ #else /* EFI_PROD_CODE */ tsChannel->channel = (BaseChannel *) TS_SIMULATOR_PORT; #endif /* EFI_PROD_CODE */ @@ -157,6 +182,11 @@ bool stopTsPort(ts_channel_s *tsChannel) { sdStop(TS_SERIAL_DEVICE); #endif /* TS_SERIAL_DEVICE */ } + #if defined(TS_CAN_DEVICE) + /*if (CONFIG(useCanForTs))*/ { + canStop(&TS_CAN_DEVICE); + } + #endif /* TS_CAN_DEVICE */ tsChannel->channel = (BaseChannel *) NULL; return true; #else /* EFI_PROD_CODE */ @@ -175,6 +205,10 @@ void sr5WriteData(ts_channel_s *tsChannel, const uint8_t * buffer, int size) { UNUSED(tsChannel); int transferred = size; uartSendTimeout(TS_UART_DEVICE, (size_t *)&transferred, buffer, BINARY_IO_TIMEOUT); +#elif defined(TS_CAN_DEVICE) + UNUSED(tsChannel); + int transferred = size; + canAddToTxStreamTimeout(&TS_CAN_DEVICE, (size_t *)&transferred, buffer, BINARY_IO_TIMEOUT); #else if (tsChannel->channel == nullptr) return; @@ -213,6 +247,11 @@ int sr5ReadDataTimeout(ts_channel_s *tsChannel, uint8_t * buffer, int size, int size_t received = (size_t)size; uartReceiveTimeout(TS_UART_DEVICE, &received, buffer, timeout); return (int)received; +#elif defined(TS_CAN_DEVICE) + UNUSED(tsChannel); + size_t received = (size_t)size; + canStreamReceiveTimeout(&TS_CAN_DEVICE, &received, buffer, timeout); + return (int)received; #else /* TS_UART_DMA_MODE */ if (tsChannel->channel == nullptr) return 0; @@ -232,6 +271,19 @@ void sr5WriteCrcPacket(ts_channel_s *tsChannel, const uint8_t responseCode, cons uint8_t *writeBuffer = tsChannel->writeBuffer; uint8_t *crcBuffer = &tsChannel->writeBuffer[3]; +#if defined(TS_CAN_DEVICE) && defined(TS_CAN_DEVICE_SHORT_PACKETS_IN_ONE_FRAME) + // a special case for short packets: we can sent them in 1 frame, without CRC & size, + // because the CAN protocol is already protected by its own checksum. + if ((size + 1) <= 7) { + sr5WriteData(tsChannel, &responseCode, 1); // header without size + if (size > 0) { + sr5WriteData(tsChannel, (const uint8_t*)buf, size); // body + } + sr5FlushData(tsChannel); + return; + } +#endif /* TS_CAN_DEVICE */ + *(uint16_t *) writeBuffer = SWAP_UINT16(size + 1); // packet size including command *(uint8_t *) (writeBuffer + 2) = responseCode; @@ -246,14 +298,17 @@ void sr5WriteCrcPacket(ts_channel_s *tsChannel, const uint8_t responseCode, cons sr5WriteData(tsChannel, (const uint8_t*)buf, size); // body } sr5WriteData(tsChannel, crcBuffer, 4); // CRC footer + sr5FlushData(tsChannel); } void sr5SendResponse(ts_channel_s *tsChannel, ts_response_format_e mode, const uint8_t * buffer, int size) { if (mode == TS_CRC) { sr5WriteCrcPacket(tsChannel, TS_RESPONSE_OK, buffer, size); } else { - if (size > 0) + if (size > 0) { sr5WriteData(tsChannel, buffer, size); + sr5FlushData(tsChannel); + } } } @@ -267,3 +322,10 @@ bool sr5IsReady(ts_channel_s *tsChannel) { return true; } +void sr5FlushData(ts_channel_s *tsChannel) { +#if defined(TS_CAN_DEVICE) + UNUSED(tsChannel); + canFlushTxStream(&TS_CAN_DEVICE); +#endif /* TS_CAN_DEVICE */ +} + diff --git a/firmware/console/binary/tunerstudio_io.h b/firmware/console/binary/tunerstudio_io.h index 058fb8dd64..0f76c83de3 100644 --- a/firmware/console/binary/tunerstudio_io.h +++ b/firmware/console/binary/tunerstudio_io.h @@ -88,10 +88,6 @@ typedef struct { // todo: double-check this #define CRC_WRAPPING_SIZE (CRC_VALUE_SIZE + 3) -#if HAL_USE_SERIAL_USB -#define CONSOLE_USB_DEVICE SDU1 -#endif /* HAL_USE_SERIAL_USB */ - void startTsPort(ts_channel_s *tsChannel); bool stopTsPort(ts_channel_s *tsChannel); @@ -107,5 +103,6 @@ void sr5SendResponse(ts_channel_s *tsChannel, ts_response_format_e mode, const u int sr5ReadData(ts_channel_s *tsChannel, uint8_t * buffer, int size); int sr5ReadDataTimeout(ts_channel_s *tsChannel, uint8_t * buffer, int size, int timeout); bool sr5IsReady(ts_channel_s *tsChannel); +void sr5FlushData(ts_channel_s *tsChannel); #endif /* CONSOLE_TUNERSTUDIO_TUNERSTUDIO_IO_H_ */ diff --git a/firmware/console/console_io.cpp b/firmware/console/console_io.cpp index bdc1a6fc73..bad0fe0926 100644 --- a/firmware/console/console_io.cpp +++ b/firmware/console/console_io.cpp @@ -33,9 +33,15 @@ EXTERN_ENGINE; #if HAL_USE_SERIAL_USB #include "usbcfg.h" #include "usbconsole.h" -extern SerialUSBDriver SDU1; +#define EFI_CONSOLE_USB_DEVICE SDU1 +#define SERIAL_USB_DRIVER SerialUSBDriver #endif /* HAL_USE_SERIAL_USB */ +#ifdef EFI_CONSOLE_USB_DEVICE +extern SERIAL_USB_DRIVER EFI_CONSOLE_USB_DEVICE; +#endif /* EFI_CONSOLE_USB_DEVICE */ + + // 10 seconds #define CONSOLE_WRITE_TIMEOUT 10000 @@ -201,11 +207,11 @@ static const BaseChannel uartChannel = { .vmt = &uartChannelVmt }; #if EFI_PROD_CODE || EFI_EGT bool isUsbSerial(BaseChannel * channel) { -#if HAL_USE_SERIAL_USB - return channel == (BaseChannel *) &CONSOLE_USB_DEVICE; +#if defined(EFI_CONSOLE_USB_DEVICE) + return channel == (BaseChannel *) &EFI_CONSOLE_USB_DEVICE; #else return false; -#endif +#endif /* EFI_CONSOLE_USB_DEVICE */ } BaseChannel * getConsoleChannel(void) { @@ -217,11 +223,11 @@ BaseChannel * getConsoleChannel(void) { return (BaseChannel *) &uartChannel; #endif /* EFI_CONSOLE_UART_DEVICE */ -#if HAL_USE_SERIAL_USB - return (BaseChannel *) &CONSOLE_USB_DEVICE; +#if defined(EFI_CONSOLE_USB_DEVICE) + return (BaseChannel *) &EFI_CONSOLE_USB_DEVICE; #else return NULL; -#endif /* HAL_USE_SERIAL_USB */ +#endif /* EFI_CONSOLE_USB_DEVICE */ } bool isCommandLineConsoleReady(void) { @@ -266,6 +272,16 @@ static Logging *logger; void startConsole(Logging *sharedLogger, CommandHandler console_line_callback_p) { logger = sharedLogger; console_line_callback = console_line_callback_p; +#if 0 +#if (defined(EFI_CONSOLE_USB_DEVICE) && ! EFI_SIMULATOR) + /** + * This method contains a long delay, that's the reason why this is not done on the main thread + * TODO: actually now with some refactoring this IS on the main thread :( + */ + usb_serial_start(); + isSerialConsoleStarted = true; +#endif /* EFI_CONSOLE_USB_DEVICE */ +#endif #if (defined(EFI_CONSOLE_SERIAL_DEVICE) && ! EFI_SIMULATOR) /*