diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index 37fde3056d..dda7afe88c 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -468,7 +468,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 09e8741b1c..9186bfac99 100644 --- a/firmware/console/binary/tunerstudio_io.cpp +++ b/firmware/console/binary/tunerstudio_io.cpp @@ -25,9 +25,19 @@ extern LoggingWithStorage tsLogger; #if HAL_USE_SERIAL_USB // Assert that the USB tx/rx buffers are large enough to fit one full packet static_assert(SERIAL_USB_BUFFERS_SIZE >= BLOCKING_FACTOR + 10); -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 #elif TS_UART_MODE /* Note: This structure is modified from the default ChibiOS layout! */ @@ -36,8 +46,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 */ @@ -46,9 +58,9 @@ void startTsPort(ts_channel_s *tsChannel) { tsChannel->channel = (BaseChannel *) NULL; #if EFI_PROD_CODE - #if defined(CONSOLE_USB_DEVICE) + #if defined(TS_USB_DEVICE) #if defined(TS_UART_DEVICE) -#error "cannot have TS_UART_DEVICE and CONSOLE_USB_DEVICE" +#error "cannot have TS_UART_DEVICE and TS_USB_DEVICE" #endif print("TunerStudio over USB serial"); /** @@ -57,9 +69,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)) { @@ -89,6 +101,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 */ @@ -110,6 +135,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 */ @@ -126,12 +156,15 @@ void sr5WriteData(ts_channel_s *tsChannel, const uint8_t * buffer, int size) { #if (PRIMARY_UART_DMA_MODE || TS_UART_DMA_MODE || TS_UART_MODE) && EFI_PROD_CODE if (tsChannel->uartp != nullptr) { - int transferred = size; - uartSendTimeout(tsChannel->uartp, (size_t *)&transferred, buffer, BINARY_IO_TIMEOUT); - return; + int transferred = size; + uartSendTimeout(tsChannel->uartp, (size_t *)&transferred, buffer, BINARY_IO_TIMEOUT); + return; } -#endif // UART - +#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; @@ -173,6 +206,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; @@ -194,6 +232,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; @@ -208,14 +259,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); + } } } @@ -229,3 +283,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 ec075b5359..48a042d63e 100644 --- a/firmware/console/binary/tunerstudio_io.h +++ b/firmware/console/binary/tunerstudio_io.h @@ -47,10 +47,6 @@ struct ts_channel_s { // 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); @@ -67,4 +63,5 @@ void sendOkResponse(ts_channel_s *tsChannel, ts_response_format_e mode); 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); diff --git a/firmware/console/console_io.cpp b/firmware/console/console_io.cpp index b1d5d5e096..1b50de0de0 100644 --- a/firmware/console/console_io.cpp +++ b/firmware/console/console_io.cpp @@ -49,9 +49,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 @@ -216,11 +222,11 @@ ts_channel_s primaryChannel; #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) { #if PRIMARY_UART_DMA_MODE @@ -238,8 +244,8 @@ 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 nullptr; #endif /* HAL_USE_SERIAL_USB */ @@ -289,6 +295,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) || defined(EFI_CONSOLE_UART_DEVICE)) && ! EFI_SIMULATOR efiSetPadMode("console RX", EFI_CONSOLE_RX_BRAIN_PIN, PAL_MODE_ALTERNATE(EFI_CONSOLE_AF));