2015-07-10 06:01:56 -07:00
|
|
|
/**
|
|
|
|
* @file tunerstudio_io.cpp
|
|
|
|
*
|
|
|
|
* @date Mar 8, 2015
|
2017-01-03 03:05:22 -08:00
|
|
|
* @author Andrey Belomutskiy, (c) 2012-2017
|
2015-07-10 06:01:56 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "tunerstudio_io.h"
|
|
|
|
#include "console_io.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#if EFI_SIMULATOR || defined(__DOXYGEN__)
|
|
|
|
#include "rusEfiFunctionalTest.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
EXTERN_ENGINE;
|
|
|
|
|
|
|
|
extern LoggingWithStorage tsLogger;
|
|
|
|
|
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
|
|
|
#include "pin_repository.h"
|
|
|
|
#include "usbconsole.h"
|
|
|
|
#include "map_averaging.h"
|
2017-05-02 10:34:01 -07:00
|
|
|
|
|
|
|
#if HAL_USE_SERIAL_USB || defined(__DOXYGEN__)
|
2015-07-10 06:01:56 -07:00
|
|
|
extern SerialUSBDriver SDU1;
|
|
|
|
#define CONSOLE_DEVICE &SDU1
|
2017-05-02 10:34:01 -07:00
|
|
|
#else
|
|
|
|
#define CONSOLE_DEVICE TS_SERIAL_UART_DEVICE
|
|
|
|
#endif
|
2015-07-10 06:01:56 -07:00
|
|
|
|
|
|
|
static SerialConfig tsSerialConfig = { 0, 0, USART_CR2_STOP1_BITS | USART_CR2_LINEN, 0 };
|
|
|
|
|
|
|
|
void startTsPort(void) {
|
2015-10-23 19:01:44 -07:00
|
|
|
#if EFI_USB_SERIAL || defined(__DOXYGEN__)
|
2017-01-05 02:01:46 -08:00
|
|
|
if (isCommandLineConsoleOverTTL()) {
|
2015-07-10 06:01:56 -07:00
|
|
|
print("TunerStudio over USB serial");
|
|
|
|
/**
|
|
|
|
* This method contains a long delay, that's the reason why this is not done on the main thread
|
|
|
|
*/
|
|
|
|
usb_serial_start();
|
2015-10-23 19:01:44 -07:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if (boardConfiguration->useSerialPort) {
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-11-09 16:03:32 -08:00
|
|
|
print("TunerStudio over USART");
|
|
|
|
mySetPadMode2("tunerstudio rx", engineConfiguration->binarySerialRxPin, PAL_MODE_ALTERNATE(TS_SERIAL_AF));
|
|
|
|
mySetPadMode2("tunerstudio tx", engineConfiguration->binarySerialTxPin, PAL_MODE_ALTERNATE(TS_SERIAL_AF));
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-11-09 16:03:32 -08:00
|
|
|
tsSerialConfig.speed = boardConfiguration->tunerStudioSerialSpeed;
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-11-09 16:03:32 -08:00
|
|
|
sdStart(TS_SERIAL_UART_DEVICE, &tsSerialConfig);
|
|
|
|
}
|
2015-10-23 19:01:44 -07:00
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* EFI_PROD_CODE */
|
|
|
|
|
|
|
|
BaseChannel * getTsSerialDevice(void) {
|
|
|
|
#if EFI_PROD_CODE || defined(__DOXYGEN__)
|
2017-05-02 10:34:01 -07:00
|
|
|
#if EFI_USB_SERIAL || defined(__DOXYGEN__)
|
2017-01-05 02:01:46 -08:00
|
|
|
if (isCommandLineConsoleOverTTL()) {
|
2015-07-10 06:01:56 -07:00
|
|
|
// if console uses UART then TS uses USB
|
2017-05-02 10:34:01 -07:00
|
|
|
return (BaseChannel *) CONSOLE_DEVICE;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2015-07-10 06:01:56 -07:00
|
|
|
return (BaseChannel *) TS_SERIAL_UART_DEVICE;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return (BaseChannel *) TS_SIMULATOR_PORT;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void tunerStudioWriteData(ts_channel_s *tsChannel, const uint8_t * buffer, int size) {
|
2017-03-30 16:55:45 -07:00
|
|
|
efiAssertVoid(getRemainingStack(chThdGetSelfX()) > 64, "tunerStudioWriteData");
|
2015-07-10 06:01:56 -07:00
|
|
|
#if EFI_SIMULATOR || defined(__DOXYGEN__)
|
|
|
|
logMsg("chSequentialStreamWrite [%d]\r\n", size);
|
|
|
|
#endif
|
|
|
|
int transferred = chnWriteTimeout(tsChannel->channel, buffer, size, BINARY_IO_TIMEOUT);
|
|
|
|
#if EFI_SIMULATOR || defined(__DOXYGEN__)
|
|
|
|
logMsg("transferred [%d]\r\n", transferred);
|
|
|
|
#endif
|
|
|
|
if (transferred != size) {
|
|
|
|
#if EFI_SIMULATOR || defined(__DOXYGEN__)
|
|
|
|
logMsg("!!! NOT ACCEPTED %d out of %d !!!", transferred, size);
|
|
|
|
#endif
|
|
|
|
scheduleMsg(&tsLogger, "!!! NOT ACCEPTED %d out of %d !!!", transferred, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds size to the beginning of a packet and a crc32 at the end. Then send the packet.
|
|
|
|
*/
|
2017-01-02 04:02:49 -08:00
|
|
|
void tunerStudioWriteCrcPacket(ts_channel_s *tsChannel, const uint8_t responseCode, const void *buf, const uint16_t size) {
|
2015-07-10 06:01:56 -07:00
|
|
|
uint8_t *writeBuffer = tsChannel->writeBuffer;
|
|
|
|
|
|
|
|
*(uint16_t *) writeBuffer = SWAP_UINT16(size + 1); // packet size including command
|
2017-01-02 04:02:49 -08:00
|
|
|
*(uint8_t *) (writeBuffer + 2) = responseCode;
|
2015-08-22 11:02:14 -07:00
|
|
|
tunerStudioWriteData(tsChannel, writeBuffer, 3); // header
|
|
|
|
|
2015-07-10 06:01:56 -07:00
|
|
|
// CRC on whole packet
|
2015-08-22 11:02:14 -07:00
|
|
|
uint32_t crc = crc32((void *) (writeBuffer + 2), 1); // command part of CRC
|
|
|
|
crc = crc32inc((void *) buf, crc, (uint32_t) (size)); // combined with packet CRC
|
|
|
|
*(uint32_t *) (writeBuffer) = SWAP_UINT32(crc);
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-08-22 11:02:14 -07:00
|
|
|
if (size > 0) {
|
|
|
|
tunerStudioWriteData(tsChannel, (const uint8_t*)buf, size); // body
|
|
|
|
}
|
2015-07-10 06:01:56 -07:00
|
|
|
|
2015-08-22 11:02:14 -07:00
|
|
|
tunerStudioWriteData(tsChannel, writeBuffer, 4); // CRC footer
|
2015-07-10 06:01:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void tsSendResponse(ts_channel_s *tsChannel, ts_response_format_e mode, const uint8_t * buffer, int size) {
|
|
|
|
if (mode == TS_CRC) {
|
|
|
|
tunerStudioWriteCrcPacket(tsChannel, TS_RESPONSE_OK, buffer, size);
|
|
|
|
} else {
|
|
|
|
if (size > 0)
|
|
|
|
tunerStudioWriteData(tsChannel, buffer, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|